Skip to content

Cranelift: GC reference reads back as null after a call safepoint (regression from #13228) #13461

Closed

Description

@gfx

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.

Reproduction

A self-contained regression test (.wast) is on this branch:

https://github.com/wado-lang/wasmtime/blob/gfx/repro-gc-safepoint-reload-null/tests/misc_testsuite/gc/safepoint-reload-aliased-ref-null.wast

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 reference

The 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

Activity

  1. added
    wasm-proposal:gc
    on May 23, 2026
  2. alexcrichton commented on May 23, 2026

    @alexcrichton
    Member
  3. vouillon commented on May 23, 2026

    @vouillon
    Contributor

    Swapping these two steps seems to fix the issue:

    // If this instruction defines a needs-stack-map value that is
    // live across a safepoint, then spill the value to its stack
    // slot.
    let mut pos = FuncCursor::new(func).after_inst(inst);
    vals.extend_from_slice(pos.func.dfg.inst_results(inst));
    for val in vals.drain(..) {
    self.rewrite_def(&mut pos, val);
    }
    // If this instruction is a safepoint, then we must add stack
    // map entries for the needs-stack-map values that are live
    // across it.
    if self.liveness.safepoints.contains_key(&inst) {
    self.rewrite_safepoint(func, inst);
    }

    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.

  4. cfallin commented on May 23, 2026

    @cfallin
    Member

    Claude suggests to swap these two steps:

    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!

  5. vouillon commented on May 23, 2026

    @vouillon
    Contributor

    Claude suggests to swap these two steps:

    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!

    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.")

  6. cfallin commented on May 23, 2026

    @cfallin
    Member

    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!

  7. added a commit that references this issue on May 24, 2026
    4e32285
  8. gfx commented on May 24, 2026

    @gfx
    ContributorAuthor

    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; stock v45.0.0 traps, and so does our fork with #13469 already applied.

    Reproduce (failing source + details in this gist: https://gist.github.com/gfx/50948a70258ab94e1dd52011894a4848):

    git clone https://github.com/wado-lang/wado
    cd wado
    git checkout gfx/wasmtime-45
    git submodule update --init vendor/wasmtime   # = wado-lang/wasmtime @ gfx/wasmtime-45 (v45.0.0 + #13469)
    cargo build --release --bin wado
    ./target/release/wado test -O3 package-gale/src/codegen_lr_label_test.wado

    Traps in a GC-reference-heavy codegen function:

    error while executing at wasm backtrace:
        0: ...!./codegen.wado/emit_lr_alt_struct
        3: ...!./codegen.wado/generate
        4: ...!__test_0_labeled_left_recursive_alt_routes_through_gen_scan_lr_functions
    

    The same source on wasmtime 44 (wado main) passes. It needs wado -O3 and reproduces with cranelift opt level none (the test harness uses Config::cranelift_opt_level(OptLevel::None)).

    I tried to reduce this to a standalone wasmtime run case but couldn't get one to fire: invoking the same .wasm through 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.

  9. vouillon commented on May 26, 2026

    @vouillon
    Contributor

    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.

    This other issue should be fixed by #13480.

  10. vouillon commented on May 26, 2026

    @vouillon
    Contributor

    Per our AI guidelines, such a note would be quite welcome -- if written by a human rather than "Claude suggests: ". 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!

    I totally understand. I apologize for my clumsy message. I'll be more careful going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasm-proposal:gc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions