Skip to content

wasm2c: Optimize force_read performance - #2808

Merged
shravanrn merged 1 commit into
WebAssembly:mainfrom
UT-Security:forcereadopt
Aug 17, 2026
Merged

shravanrn merged 1 commit into
WebAssembly:mainfrom
UT-Security:forcereadopt

Conversation

@shravanrn

@shravanrn shravanrn commented Aug 7, 2026 •

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.

  1. It prevents load followed by a modification to be prevented from being combined into a “load with modification”. This occurs 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%.

  2. The force_read currently uses the general purpose register "r" for force_read_float. This forces C compilers to emit an expensive float-register to int-register conversion on most architectures. The result of this is roughly an extra 7% overhead when running a sandboxed libsoundtouch which is used by Firefox to adjust the playback speed of audio files.

@shravanrn
shravanrn requested review from keithw, sbc100 and zherczeg August 7, 2026 00:14
@shravanrn
shravanrn force-pushed the forcereadopt branch 2 times, most recently from 9d783fb to 8aa754b Compare August 11, 2026 23:17
@shravanrn

Copy link
Copy Markdown
Collaborator Author

@sbc100 If you have a chance this is the other PR that I am really hoping to land soon as it has a large performance delta. Most of the others that I will submit shortly are much more targeted/minor optimizations. Please have a look if possible.

Comment thread wasm2c/wasm-rt.h Resolved
@sbc100

sbc100 commented Aug 16, 2026

Copy link
Copy Markdown
Member

Is the PR description up-to-date? I'm not sure how the patch solves the two issues outlined?

@shravanrn shravanrn changed the title wasm2c: Optimize force_read perf and allow embedder to configure policy wasm2c: Optimize force_read perf by reading after potential data widening Aug 16, 2026
@shravanrn shravanrn changed the title wasm2c: Optimize force_read perf by reading after potential data widening wasm2c: Optimize force_read performance Aug 16, 2026
@shravanrn

Copy link
Copy Markdown
Collaborator Author

Is the PR description up-to-date? I'm not sure how the patch solves the two issues outlined?

Sorry, my bad the second change had gotten lost at some point due to a rebase. I have pushed the updated PR now and removed the NONCONFORMING change.

(No rush on this PR: the NONCONFORMING PR which I will upload shortly is more time sensitive)

@shravanrn
shravanrn merged commit 6feb2ff into WebAssembly:main Aug 17, 2026
17 checks passed
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.

2 participants