Stream: git-wasmtime

Topic: wasmtime / issue #15036 Element segment expressions are e...


view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 21:08):

alexcrichton opened issue #15036:

This test:

(module $M
  (type $t (func (result i32)))
  (table (export "t") 2 funcref)
  (table (export "a") 2 anyref)
  (func (export "call") (param i32) (result i32)
    (call_indirect (type $t) (local.get 0)))
  (func (export "get-a") (param i32) (result i32)
    (i31.get_s (ref.cast (ref i31) (table.get 1 (local.get 0))))))
(register "M" $M)

(assert_trap
  (module
    (type $a (array (mut i64)))
    (import "M" "t" (table 2 funcref))
    (table $t2 1 anyref)
    (func $f (result i32) (i32.const 7))
    (elem (table 0) (i32.const 0) func $f)
    (elem (table $t2) (i32.const 0) anyref
      (array.new_default $a (i32.const 0x7fffffff))))
  "allocation size too large")
(assert_trap (invoke $M "call" (i32.const 0)) "uninitialized element")

fails with:

$ cargo run -- wast foo.wast
Error: failed to run script file 'foo.wast'

Caused by:
    0: failed directive on foo.wast:21
    1: expected trap, got Core([I32(7)])

and the same with the trap in a later element of the same segment:

(assert_trap
  (module
    (type $a (array (mut i64)))
    (import "M" "a" (table 2 anyref))
    (elem (table 0) (i32.const 0) anyref
      (ref.i31 (i32.const 5))
      (array.new_default $a (i32.const 0x7fffffff))))
  "allocation size too large")
(assert_trap (invoke $M "get-a" (i32.const 0)) "null reference")
$ cargo run -- wast foo.wast
Error: failed to run script file 'foo.wast'

Caused by:
    0: failed directive on foo.wast:19
    1: expected trap, got Core([I32(5)])

The spec's instantiation evaluates the element expressions of every segment before running any active segment's table.init, so a trapping expression should leave imported tables untouched.

Fixing this in my opinion is going to need to be balanced with implementation complexity. This is a vanishingly niche use case which in theory shouldn't come along with a large implementation burden. Nevertheless I figured it's worth opening here.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 21:08):

alexcrichton added the bug label to Issue #15036.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2026 at 13:43):

po-nuvai commented on issue #15036:

hi i saw this issue and i got and idea wether it is really working or not idk i will tell you if that is ok i will raise the pr , so the idea is in we need to arrange the table orderly in the segment during that time in an active segment if there is an expression it will became a trap and crash it so before we can treat it as a passive segment during the startup. when i saw the module it give a passive segment slot in which wasm code cant reach there, so we can calculate the value in the other passive segment before any active segment writes to a table. A trap now leaves imported tables unchanged, so later in the normal active-segment order, it is copied into the table (like table.init), and then the slot is freed (like elem.drop), Segments that cannot trap (ref.func, ref.null, ref.i31, global.get) keep the current fast path. So normal modules see no change. I have a local prototype (about 60 lines). Both test cases from this issue pass with it. Does this approach sound acceptable?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2026 at 13:45):

po-nuvai edited a comment on issue #15036:

hi i saw this issue and i got and idea wether it is really working or not idk i will tell you if that is ok i will raise the pr , so the idea is in we need to arrange the table orderly in the segment during that time in an active segment if there is an expression it may became a trap and crash it so before we can treat it as a passive segment during the startup. when i saw the module it give a passive segment slot in which wasm code cant reach there, so we can calculate the value in the other passive segment before any active segment writes to a table. A trap now leaves imported tables unchanged, so later in the normal active-segment order, it is copied into the table (like table.init), and then the slot is freed (like elem.drop), Segments that cannot trap (ref.func, ref.null, ref.i31, global.get) keep the current fast path. So normal modules see no change. I have a local prototype (about 60 lines). Both test cases from this issue pass with it. Does this approach sound acceptable?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2026 at 13:46):

po-nuvai edited a comment on issue #15036:

Hi, I'd like to work on this issue. I have an implementation idea and would appreciate your feedback before opening a PR.

If an active element segment contains an expression that may trap (currently only struct.new* / array.new*, as identified by ConstOp::may_trap()), my proposal is to handle it like a passive segment during module initialization:

  1. When reading the module, allocate a hidden passive segment slot that cannot be accessed by WebAssembly code (i.e., don't add it to passive_elem_map).
  2. Evaluate its values along with the other passive segments, before performing any active segment writes to tables. This ensures that if evaluation traps, imported tables remain unchanged.
  3. Later, process it in the normal active-segment order, copy its contents into the target table (similar to table.init), and then free the temporary slot (similar to elem.drop).

Segments that cannot trap (ref.func, ref.null, ref.i31, global.get) would retain the existing fast path, so normal modules would be unaffected.

I have a small local prototype, and the first test case from the issue passes. For the second test case, I suspect the expected trap message might be incorrect: casting an empty slot to (ref i31) produces a "cast failure" rather than "null reference". Replacing the cast with a ref.is_null check makes the test pass.

I haven't run the full test suite yet.

Does this approach sound reasonable? I'd appreciate your feedback on the design before I proceed with a PR.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 02 2026 at 16:24):

alexcrichton commented on issue #15036:

Thanks for offering to work on this @po-nuvai! Yeah that sounds pretty reasonable to me and pretty closely matches what the spec already says to do in these situations. I think the cost of having an extra allocation around during instantiation is just something we'll have to live with for GC-using modules and if that's ever a problem we can work on optimizing it at that point.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 03 2026 at 07:17):

po-nuvai commented on issue #15036:

Thanks @alexcrichton for the feedback on #15036! This PR follows the design we discussed there.

A few notes for review:

Happy to make any changes. Thanks!

view this post on Zulip Wasmtime GitHub notifications bot (Nov 05 2026 at 21:33):

alexcrichton closed issue #15036:

This test:

(module $M
  (type $t (func (result i32)))
  (table (export "t") 2 funcref)
  (table (export "a") 2 anyref)
  (func (export "call") (param i32) (result i32)
    (call_indirect (type $t) (local.get 0)))
  (func (export "get-a") (param i32) (result i32)
    (i31.get_s (ref.cast (ref i31) (table.get 1 (local.get 0))))))
(register "M" $M)

(assert_trap
  (module
    (type $a (array (mut i64)))
    (import "M" "t" (table 2 funcref))
    (table $t2 1 anyref)
    (func $f (result i32) (i32.const 7))
    (elem (table 0) (i32.const 0) func $f)
    (elem (table $t2) (i32.const 0) anyref
      (array.new_default $a (i32.const 0x7fffffff))))
  "allocation size too large")
(assert_trap (invoke $M "call" (i32.const 0)) "uninitialized element")

fails with:

$ cargo run -- wast foo.wast
Error: failed to run script file 'foo.wast'

Caused by:
    0: failed directive on foo.wast:21
    1: expected trap, got Core([I32(7)])

and the same with the trap in a later element of the same segment:

(assert_trap
  (module
    (type $a (array (mut i64)))
    (import "M" "a" (table 2 anyref))
    (elem (table 0) (i32.const 0) anyref
      (ref.i31 (i32.const 5))
      (array.new_default $a (i32.const 0x7fffffff))))
  "allocation size too large")
(assert_trap (invoke $M "get-a" (i32.const 0)) "null reference")
$ cargo run -- wast foo.wast
Error: failed to run script file 'foo.wast'

Caused by:
    0: failed directive on foo.wast:19
    1: expected trap, got Core([I32(5)])

The spec's instantiation evaluates the element expressions of every segment before running any active segment's table.init, so a trapping expression should leave imported tables untouched.

Fixing this in my opinion is going to need to be balanced with implementation complexity. This is a vanishingly niche use case which in theory shouldn't come along with a large implementation burden. Nevertheless I figured it's worth opening here.


Last updated: Nov 19 2026 at 20:05 UTC