Repository navigation
Description
Summary
A Wasm GC (ref null $t) value that is live across a call safepoint can read back as
null after the call, making a ref.as_non_null (or struct.get/array.get) on a
provably non-null value trap with wasm trap: null reference. The same module runs
correctly on 44.0.0.
git bisect (over v44.0.1..v45.0.0) points at:
24938c4 — "Handle alias values in
SafepointSpiller::rewrite_use" (#13228)
That PR makes the safepoint spiller reload aliased needs-stack-map values from their
stack slot after safepoints (needed for the new moving collector). For the affected
value, the post-safepoint reload appears to read a slot that was never spilled, so the
use sees null.
- Reproduces with every collector (
drc/null/copying) and at every
Cranelift opt level, because the reload code is emitted unconditionally. - The follow-up cranelift: include every SSA value bound to a variable in stack maps #13449 ("include every SSA value bound to a variable in stack maps")
does not fix this case. - Still present on
main— testedfa50c6a150(46.0.0-dev).
Reproduction
A self-contained regression test (.wast) is on this branch:
It is a standard tests/misc_testsuite/gc/ test — (assert_return (invoke "run")) on a
GC module. run returns without trapping on 44.0.0 and traps with null reference on
45.0.0 and main.
You can also run the module directly:
$ FEATURES="-Wgc=y -Wfunction-references=y -Wwide-arithmetic=y -Wsimd=y -Wthreads=y -Wreference-types=y"
# 44.0.0 — OK (exit 0)
$ wasmtime run $FEATURES --invoke run repro.wasm
# 45.0.0 / main — traps
$ wasmtime run $FEATURES --invoke run repro.wasm
...
wasm trap: null referenceThe module was produced by a real frontend (the Wado
compiler): it JSON-deserializes a two-field struct in a loop. The trapping
ref.as_non_null reads a struct-access object that is built with struct.new (so it
cannot be null) and is held in a local that is live across the call fetching the next
field; on the second loop iteration the reloaded local reads back null. The module passes
wasm-tools validate --features all.
On minimization
I could not reduce this with wasm-tools shrink (0% — the module is already DCE'd and the
miscompile is register-allocation-sensitive, so any structural mutation might hide it). Happy
to provide the source program or help with a CLIF-level reduction.
Environment
- Cranelift/wasmtime 45.0.0 (
377cd917a) andmain(fa50c6a150, 46.0.0-dev) - host: aarch64-apple-darwin, Cranelift backend
- first bad commit:
24938c41630e2662ebcdb0a37137f02fd48c4d07(Handle alias values inSafepointSpiller::rewrite_use#13228)
Activity
alexcrichton commentedon May 23, 2026
cc @fitzgen
vouillon commentedon May 23, 2026
Swapping these two steps seems to fix the issue:
wasmtime/cranelift/frontend/src/frontend/safepoints.rs
Lines 762 to 776 in fa50c6a
The rational is that one should reserve slots for all the needs-stack-map values before releasing the slot for the value defined by the instruction. Otherwise, one can assign the same slot for both a value live across the instruction and the value just defined.
cfallin commentedon May 23, 2026
Hi @vouillon -- without commenting on the rest of your message, please do not do this. Our AI tool usage policy requires a human in the loop, and copying/pasting something straight from your agent is explicitly against the spirit of our policy. We are talking to you, and you take responsibility for everything you say and all code that you post here. If you do not have enough of a solid grasp on your contributions to speak yourself about them, but have to transparently delegate to your agent, then that is a good sign that you should spend more time understanding it yourself first. (You are of course free to use an agent internally; but we interact with you only.) Doing otherwise is an "extractive contribution" per our policy: it says that you don't want to bother parsing and working through details but just want to throw that work at us. Thanks!
vouillon commentedon May 23, 2026
Sorry, I should have phrased this differently, and should have been clearer. This change fixes the issue, and does not seem to break anything, including a quite large test suite with which I found two other issues (#13449 and #13450). I have also read the AI-generated explanation, and it makes a lot of sense to me. So, even though I have not taken the time to make a PR, I thought it would save you some time to have this information. ("Our golden rule is that a contribution should be worth more to the project than the time it takes to review it.")
cfallin commentedon May 23, 2026
Per our AI guidelines, such a note would be quite welcome -- if written by a human rather than "Claude suggests: <dump>". I hope you can appreciate that we get increasingly large mountains of very verbose AI output, some of it right, some of it wrong, and some of it not even wrong in extraordinarily strange ways, and so the only realistic approach with our limited time is to consider all such text as un-reviewed by a human until proven otherwise. That's the essence behind our policy. A "I've tested this fix and it works, what do you think?" note would have led to a much better reception!
Fix safepoint stack slot reuse
gfx commentedon May 24, 2026
We're hitting another miscompile from #13228 that #13469 does not fix — a second case on a non-moving (
drc) collector, which is what Wado uses.Bisected (clean, over
[#13217..v45.0.0]) to #13228 (24938c4163). The same wado-generated wasm runs on wasmtime 44 and traps on 45; stockv45.0.0traps, and so does our fork with #13469 already applied.Reproduce (failing source + details in this gist: https://gist.github.com/gfx/50948a70258ab94e1dd52011894a4848):
Traps in a GC-reference-heavy codegen function:
The same source on wasmtime 44 (wado
main) passes. It needswado -O3and reproduces with cranelift opt levelnone(the test harness usesConfig::cranelift_opt_level(OptLevel::None)).I tried to reduce this to a standalone
wasmtime runcase but couldn't get one to fire: invoking the same.wasmthrough its component export does not trap — it only reproduces through the test harness's instantiate-and-call path, so it's regalloc-fragile like the original #13461 repro. Happy to dig further if a smaller case would help.Since #13228's own commit message notes these reloads are unnecessary for non-moving collectors, and it has now produced two distinct miscompiles for them (this and #13461), I'd lean toward reverting #13228 rather than fixing it case by case.
vouillon commentedon May 26, 2026
This other issue should be fixed by #13480.
vouillon commentedon May 26, 2026
I totally understand. I apologize for my clumsy message. I'll be more careful going forward.