WebAssembly / wabt

The WebAssembly Binary Toolkit
Apache License 2.0
6.91k stars 702 forks source link

wasm2c: Fix handling of locals in setjmp targets #2479

Closed SoniEx2 closed 1 month ago

SoniEx2 commented 1 month ago

It is UB to read local variables after a call to setjmp returns, if those variables have been modified between setjmp and longjmp, unless they're marked as volatile. This marks them as volatile.

Closes #2469

Split from #2470

SoniEx2 commented 1 month ago

@sbc100

SoniEx2 commented 1 month ago

(the tests would catch this, despite UBSAN not having a setjmp sanitizer, because CI runs tests on both debug and release builds)

(upstreaming is proving difficult... https://github.com/WebAssembly/exception-handling/pull/332 )

sbc100 commented 1 month ago

(the tests would catch this, despite UBSAN not having a setjmp sanitizer, because CI runs tests on both debug and release builds)

I'm not quite sure what you are saying here. Do you mean the new test you are adding would fail without the fix?

(upstreaming is proving difficult... WebAssembly/exception-handling#332 )

SoniEx2 commented 1 month ago

I'm not quite sure what you are saying here. Do you mean the new test you are adding would fail without the fix?

the test would fail without the fix, yes. like so: (note the -O0 vs -O3)

$ cd /home/soniex2/git/github/wabt && /usr/bin/cmake -E env WASM2C_CC=/usr/bin/clang WASM2C_CFLAGS=\ -fsanitize=undefined\ -fno-sanitize-recover\ -fsanitize-blacklist=/home/soniex2/git/github/wabt/ubsan.blacklist\ -g\ -O0 /usr/bin/python3.12 /home/soniex2/git/github/wabt/test/run-tests.py --bindir /home/soniex2/git/github/wabt/out test/regress/wasm2c-ehv3-setjmp-volatile.txt 
[+1|-0|%100] (0.55s) test/regress/wasm2c-ehv3-setjmp-volatile.txt
$ cd /home/soniex2/git/github/wabt && /usr/bin/cmake -E env WASM2C_CC=/usr/bin/clang WASM2C_CFLAGS=\ -fsanitize=undefined\ -fno-sanitize-recover\ -fsanitize-blacklist=/home/soniex2/git/github/wabt/ubsan.blacklist\ -g\ -O3 /usr/bin/python3.12 /home/soniex2/git/github/wabt/test/run-tests.py --bindir /home/soniex2/git/github/wabt/out test/regress/wasm2c-ehv3-setjmp-volatile.txt 
- test/regress/wasm2c-ehv3-setjmp-volatile.txt
  expected error code 0, got 1.
  STDERR MISMATCH:
  --- expected
  +++ actual
  @@ -0,0 +1 @@
  +wasm2c-ehv3-setjmp-volatile.txt:22: assertion failed: in w2c_wasm2c__ehv3__setjmp__volatile__0__wasm_setjmp0x2Dbait(&wasm2c__ehv3__setjmp__volatile__0__wasm_instance, 0u): expected 1, got 0.
  STDOUT MISMATCH:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -2/2 tests passed.
  +1/2 tests passed.

**** FAILED ******************************************************************
- test/regress/wasm2c-ehv3-setjmp-volatile.txt
    /usr/bin/python3.12 /home/soniex2/git/github/wabt/test/run-spec-wasm2c.py out/test/regress/wasm2c-ehv3-setjmp-volatile.txt --bindir=/home/soniex2/git/github/wabt/out --no-error-cmdline -o out/test/regress/wasm2c-ehv3-setjmp-volatile --enable-exceptions