Skip to content

[test] Add test for STACK_OVERFLOW_CHECK=2 with BINARYEN_PASS_DEBUG - #28126

Open
nexxeln wants to merge 1 commit into
emscripten-core:mainfrom
nexxeln:test-stack-check-pass-debug
Open

nexxeln wants to merge 1 commit into
emscripten-core:mainfrom
nexxeln:test-stack-check-pass-debug

Conversation

@nexxeln

@nexxeln nexxeln commented Nov 1, 2026

Copy link
Copy Markdown

With -sSTACK_OVERFLOW_CHECK=2, wasm-emscripten-finalize runs
binaryen's stack-check pass with __handle_stack_overflow as the
handler. The pass builds the call to the handler with the stack
pointer's type as its result type, but the handler returns nothing.
Finalize does not validate after its passes, so these builds still
work. BINARYEN_PASS_DEBUG=1 makes binaryen validate after each pass,
and then the build fails:

[wasm-validator error in function __original_main] unexpected true: if without else must not return a value in body, on
...
Fatal: Last pass (stack-check) broke validation. Run with BINARYEN_PASS_DEBUG=2 in the env to see the earlier state, or 3 to dump byn-* files for each pass

emcc: error: '.../wasm-emscripten-finalize --no-dyncalls --bigint --check-stack-overflow hello_world.wasm -o hello_world.wasm --detect-features' failed (returned 1)

The new test, other.test_stack_overflow_check_pass_debug, sets
BINARYEN_PASS_DEBUG=1. Then it builds and runs hello_world.c with
-sSTACK_OVERFLOW_CHECK=2, and checks that the program prints
"Hello, world!".

The test fails until WebAssembly/binaryen#9335 lands and rolls in. The
reviewer of that PR asked for this order:

  1. Open this PR to show the failure.
  2. Land the binaryen fix. The test passes once a binaryen build with the
    fix reaches test/emsdk_version.txt.
  3. Land this PR.

On macOS arm64, with the emsdk build in test/emsdk_version.txt (binaryen
version_133-147-ga6ed9adf2), the test fails as shown above. With
binaryen built from the WebAssembly/binaryen#9335 branch, it passes. The
existing stack overflow tests in core0, core2, and other give the
same results with both builds.

An AI coding agent wrote this test and this description at my request,
and ran the checks above. It ran them only on macOS, not on Linux like
CI.

With `-sSTACK_OVERFLOW_CHECK=2`, `wasm-emscripten-finalize` runs
binaryen's stack-check pass with `__handle_stack_overflow` as the
handler. The pass builds the call to the handler with the stack
pointer's type as its result type, but the handler returns nothing.
Finalize does not validate after its passes, so these builds still
work. `BINARYEN_PASS_DEBUG=1` makes binaryen validate after each pass,
and then the build fails with "Last pass (stack-check) broke
validation".

The new test builds and runs `hello_world.c` with both settings. It
fails until WebAssembly/binaryen#9335 rolls in.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request adds a new test test_stack_overflow_check_pass_debug to test/test_other.py. This test runs with the BINARYEN_PASS_DEBUG environment variable set to '1' and STACK_OVERFLOW_CHECK set to 2 to ensure that binaryen validates correctly after each pass and catches any invalid IR. There are no review comments, and the changes look correct.

Comment thread test/test_other.py
@with_env_modify({'BINARYEN_PASS_DEBUG': '1'})
def test_stack_overflow_check_pass_debug(self):
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.do_runf('hello_world.c', 'Hello, world!\n')

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.

I think the binaryen fix looks good, but I'm not sure we need to add tests like this (basically a test of binaryen) here in the emscripten repo? This seems like something that should be tested in binaryen. Having said that I guess it could make sense for emscripten to be tested with BINARYEN_PASS_DEBUG in some cases?

@kripken WDYT?

This branch has not been deployed

No deployments
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