Skip to content

Commit

Permalink
Make i31.get_{s,u} instructions trap on null i31refs (#9165)
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen authored Aug 23, 2024
1 parent 8e950ae commit 07f3e34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
mut pos: FuncCursor,
i31ref: ir::Value,
) -> WasmResult<ir::Value> {
pos.ins().trapz(i31ref, ir::TrapCode::NullI31Ref);
Ok(pos.ins().sshr_imm(i31ref, 1))
}

Expand All @@ -1742,6 +1743,7 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
mut pos: FuncCursor,
i31ref: ir::Value,
) -> WasmResult<ir::Value> {
pos.ins().trapz(i31ref, ir::TrapCode::NullI31Ref);
Ok(pos.ins().ushr_imm(i31ref, 1))
}

Expand Down
11 changes: 11 additions & 0 deletions tests/misc_testsuite/gc/null-i31ref.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(func (export "get_u-null") (result i32)
(i31.get_u (ref.null i31))
)
(func (export "get_s-null") (result i32)
(i31.get_u (ref.null i31))
)
)

(assert_trap (invoke "get_u-null") "null i31 reference")
(assert_trap (invoke "get_s-null") "null i31 reference")

0 comments on commit 07f3e34

Please sign in to comment.