Skip to content

wasm2c: Optimize force_read perf and allow embedder to configure policy - #2808

Open
shravanrn wants to merge 1 commit into
WebAssembly:mainfrom
UT-Security:forcereadopt
Open

wasm2c: Optimize force_read perf and allow embedder to configure policy#2808
shravanrn wants to merge 1 commit into
WebAssembly:mainfrom
UT-Security:forcereadopt

Conversation

@shravanrn

Copy link
Copy Markdown
Collaborator

wasm2c's FORCE_READ_* macros allow spec compliance when using guard pages by preventing elimination of potentially trapping loads, but introduces several low-level performance issues in the generated code. One example is that it prevents load followed by a modification to be prevented from being combined into a “load with modification”​.

Thisoccurs in one the libraries sandboxed by Firefox. The library loads a byte and extends it to 32-bits before processing it --- all in tight loop.

Since wasm2c's load byte corresponds to the following code

u8 result;​
memcpy(&result, &mem[addr], sizeof(u8)); ​
__asm__("" ::"r"(result)); ​
return (u32) result;​

the generated assembly first performs a load with 32-bit load shifted to a byte, sends the byte result to the asm block, and then performs a widening. However a faster option here, is to just read the byte whie sign extending it, and then consume the sign extended result. This only requires moving the last cast prior to the asm. This simple change speeds up this library by 3%. Separately, this PR also allows the embedder to disable force_reads (this is off by default, since it is non conforming), and speeds up the same benchmark by around 5%.

@shravanrn
shravanrn requested review from keithw, sbc100 and zherczeg August 7, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant