Skip to content

wasm2c: Remove unnecessary force_read in bounds check mode for performance - #2814

Merged
shravanrn merged 1 commit into
WebAssembly:mainfrom
UT-Security:boundscheck-perf
Aug 9, 2026
Merged

wasm2c: Remove unnecessary force_read in bounds check mode for performance#2814
shravanrn merged 1 commit into
WebAssembly:mainfrom
UT-Security:boundscheck-perf

Conversation

@shravanrn

Copy link
Copy Markdown
Collaborator

When using wasm2c with guard pages, the force_read macro (containing an asm block) is used to make sure the C compiler doesn't optimize away memory reads which are dead or move them around in Wasm functions. This is because reads in Wasm have a side-effect --- they can trap if they are out-of-bounds of the linear memory. This macro is however unnecessary for bounds check mode (as the bounds check explicitly captures this side effect), and keeping the asm block around has a large performance impact as it hinders various compiler optimizations. For example, when using a Wasm2c sandboxed libwoff2 font compression library in bounds check mode, removing the force_read macro reduces overhead from 55% to 44%. This PR defines the force_read macro to empty for bounds check mode.

@shravanrn
shravanrn requested review from keithw, sbc100 and zherczeg August 8, 2026 19:10
@keithw

keithw commented Aug 8, 2026

Copy link
Copy Markdown
Member

I'm fine with this for now, but for the longer-term I would love to reduce the forest of preprocessor macros (especially, in this case, preprocessor macros that have variable expansions) used to configure the runtime behavior... this is getting pretty hard to reason about!

Are you okay if a future version of wasm2c makes these kinds of choices a transpile-time configuration (when running wasm2c) instead of via a "compile-time" configuration (by setting preprocessor macros)?

@shravanrn

shravanrn commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

I'm fine with this for now, but for the longer-term I would love to reduce the forest of preprocessor macros (especially, in this case, preprocessor macros that have variable expansions) used to configure the runtime behavior... this is getting pretty hard to reason about!

Are you okay if a future version of wasm2c makes these kinds of choices a transpile-time configuration (when running wasm2c) instead of via a "compile-time" configuration (by setting preprocessor macros)?

Yup, I agree this needs a cleanup and at least a basic cleanup is on my medium term to-do list as well.

Re transpile time vs compile time --- It may make sense to bike shed the exact design a bit prior to any implementation, but in principle, this sounds fine to me.

@keithw for the current change, could you sign off on the code review as well, so I can land this?

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shravanrn
shravanrn merged commit 8293228 into WebAssembly:main Aug 9, 2026
17 checks passed
@shravanrn
shravanrn deleted the boundscheck-perf branch August 9, 2026 04:44
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.

3 participants