Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix the copying collector bug(s) triggered by the Kotlin benchmarks (#13381)

Nick Fitzgerald939e7c5e6308
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 37d2076d2b..259dc8d34d 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -3087,17 +3087,14 @@ impl FuncEnvironment<'_> {
let gv = builder.ins().global_value(self.pointer_type(), gv);
let src = builder.ins().iadd_imm(gv, i64::from(offset));
- gc::gc_compiler(self)?.translate_read_gc_reference(
- self,
- builder,
- ref_ty,
- src,
- if global_ty.mutability {
- ir::MemFlagsData::trusted()
- } else {
- ir::MemFlagsData::trusted().with_readonly().with_can_move()
- },
- )
+ let flags = if global_ty.mutability || gc::gc_compiler(self)?.is_moving_collector()
+ {
+ ir::MemFlagsData::trusted()
+ } else {
+ ir::MemFlagsData::trusted().with_readonly().with_can_move()
+ };
+ gc::gc_compiler(self)?
+ .translate_read_gc_reference(self, builder, ref_ty, src, flags)
}
}
}
diff --git a/crates/cranelift/src/func_environ/gc.rs b/crates/cranelift/src/func_environ/gc.rs
index 72a8dce1be..f1079a2fbd 100644
--- a/crates/cranelift/src/func_environ/gc.rs
+++ b/crates/cranelift/src/func_environ/gc.rs
@@ -44,6 +44,9 @@ pub trait GcCompiler {
#[cfg_attr(not(feature = "gc"), allow(dead_code))]
fn layouts(&self) -> &dyn GcTypeLayouts;
+ /// Whether or not this `GcCompiler`'s collector can move objects.
+ fn is_moving_collector(&self) -> bool;
+
/// Emit code to allocate a new array.
///
/// The array should be of the given type and its elements initialized as
diff --git a/crates/cranelift/src/func_environ/gc/enabled.rs b/crates/cranelift/src/func_environ/gc/enabled.rs
index 6494a4d7db..24dd6c3bb9 100644
--- a/crates/cranelift/src/func_environ/gc/enabled.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled.rs
@@ -170,10 +170,9 @@ fn emit_gc_kind_assert(
object_size: wasmtime_environ::VM_GC_HEADER_SIZE,
},
);
- let kind_and_reserved_bits =
- builder
- .ins()
- .load(ir::types::I32, GC_MEMFLAGS.with_readonly(), kind_addr, 0);
+ let kind_and_reserved_bits = builder
+ .ins()
+ .load(ir::types::I32, GC_MEMFLAGS, kind_addr, 0);
let kind_mask = builder
.ins()
.iconst(ir::types::I32, i64::from(VMGcKind::MASK));
diff --git a/crates/cranelift/src/func_environ/gc/enabled/copying.rs b/crates/cranelift/src/func_environ/gc/enabled/copying.rs
index 8b361be96e..f8d0cfe483 100644
--- a/crates/cranelift/src/func_environ/gc/enabled/copying.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled/copying.rs
@@ -53,13 +53,13 @@ impl CopyingCompiler {
) -> (ir::Value, ir::Value) {
let bump_ptr = builder.ins().load(
ir::types::I32,
- ir::MemFlagsData::trusted().with_can_move(),
+ ir::MemFlagsData::trusted(),
ptr_to_heap_data,
i32::from(func_env.offsets.ptr.vmcopying_heap_data_bump_ptr()),
);
let active_space_end = builder.ins().load(
ir::types::I32,
- ir::MemFlagsData::trusted().with_readonly().with_can_move(),
+ ir::MemFlagsData::trusted(),
ptr_to_heap_data,
i32::from(func_env.offsets.ptr.vmcopying_heap_data_active_space_end()),
);
@@ -256,6 +256,10 @@ impl GcCompiler for CopyingCompiler {
&self.layouts
}
+ fn is_moving_collector(&self) -> bool {
+ true
+ }
+
fn alloc_array(
&mut self,
func_env: &mut FuncEnvironment<'_>,
diff --git a/crates/cranelift/src/func_environ/gc/enabled/drc.rs b/crates/cranelift/src/func_environ/gc/enabled/drc.rs
index ece70b21fe..af2d622fce 100644
--- a/crates/cranelift/src/func_environ/gc/enabled/drc.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled/drc.rs
@@ -343,6 +343,10 @@ impl GcCompiler for DrcCompiler {
&self.layouts
}
+ fn is_moving_collector(&self) -> bool {
+ false
+ }
+
fn alloc_array(
&mut self,
func_env: &mut FuncEnvironment<'_>,
diff --git a/crates/cranelift/src/func_environ/gc/enabled/null.rs b/crates/cranelift/src/func_environ/gc/enabled/null.rs
index a52ad637bd..369c3c73cb 100644
--- a/crates/cranelift/src/func_environ/gc/enabled/null.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled/null.rs
@@ -182,6 +182,10 @@ impl GcCompiler for NullCompiler {
&self.layouts
}
+ fn is_moving_collector(&self) -> bool {
+ false
+ }
+
fn alloc_array(
&mut self,
func_env: &mut FuncEnvironment<'_>,
diff --git a/tests/disas/gc/copying/array-new-fixed-of-gc-refs.wat b/tests/disas/gc/copying/array-new-fixed-of-gc-refs.wat
index 3f94a93f92..64ea216cc8 100644
--- a/tests/disas/gc/copying/array-new-fixed-of-gc-refs.wat
+++ b/tests/disas/gc/copying/array-new-fixed-of-gc-refs.wat
@@ -30,11 +30,11 @@
;; v67 = stack_addr.i64 ss0
;; store notrap v4, v67
;; @0025 v14 = load.i64 notrap aligned readonly can_move v0+32
-;; @0025 v15 = load.i32 notrap aligned can_move v14
+;; @0025 v15 = load.i32 notrap aligned v14
+;; @0025 v16 = load.i32 notrap aligned v14+4
;; @0025 v22 = uextend.i64 v15
;; v65 = iconst.i64 32
;; @0025 v23 = iadd v22, v65 ; v65 = 32
-;; @0025 v16 = load.i32 notrap aligned readonly can_move v14+4
;; @0025 v24 = uextend.i64 v16
;; @0025 v25 = icmp ule v23, v24
;; @0025 brif v25, block2, block3
diff --git a/tests/disas/gc/copying/array-new-fixed.wat b/tests/disas/gc/copying/array-new-fixed.wat
index c789fa9b90..8ef742dbd7 100644
--- a/tests/disas/gc/copying/array-new-fixed.wat
+++ b/tests/disas/gc/copying/array-new-fixed.wat
@@ -21,11 +21,11 @@
;;
;; block0(v0: i64, v1: i64, v2: i64, v3: i64, v4: i64):
;; @0025 v14 = load.i64 notrap aligned readonly can_move v0+32
-;; @0025 v15 = load.i32 notrap aligned can_move v14
+;; @0025 v15 = load.i32 notrap aligned v14
+;; @0025 v16 = load.i32 notrap aligned v14+4
;; @0025 v22 = uextend.i64 v15
;; v71 = iconst.i64 48
;; @0025 v23 = iadd v22, v71 ; v71 = 48
-;; @0025 v16 = load.i32 notrap aligned readonly can_move v14+4
;; @0025 v24 = uextend.i64 v16
;; @0025 v25 = icmp ule v23, v24
;; @0025 brif v25, block2, block3
diff --git a/tests/disas/gc/copying/array-new.wat b/tests/disas/gc/copying/array-new.wat
index 2baae2bf5a..6d827f9f27 100644
--- a/tests/disas/gc/copying/array-new.wat
+++ b/tests/disas/gc/copying/array-new.wat
@@ -31,7 +31,8 @@
;; v70 = ishl v3, v69 ; v69 = 3
;; @0022 v10 = uadd_overflow_trap v5, v70, user18 ; v5 = 24
;; @0022 v12 = load.i64 notrap aligned readonly can_move v0+32
-;; @0022 v13 = load.i32 notrap aligned can_move v12
+;; @0022 v13 = load.i32 notrap aligned v12
+;; @0022 v14 = load.i32 notrap aligned v12+4
;; @0022 v20 = uextend.i64 v13
;; @0022 v15 = uextend.i64 v10
;; @0022 v16 = iconst.i64 15
@@ -39,7 +40,6 @@
;; @0022 v17 = iconst.i64 -16
;; @0022 v19 = band v18, v17 ; v17 = -16
;; @0022 v21 = iadd v20, v19
-;; @0022 v14 = load.i32 notrap aligned readonly can_move v12+4
;; @0022 v22 = uextend.i64 v14
;; @0022 v23 = icmp ule v21, v22
;; @0022 brif v23, block2, block3
diff --git a/tests/disas/gc/copying/funcref-in-gc-heap-new.wat b/tests/disas/gc/copying/funcref-in-gc-heap-new.wat
index 33799bdbcf..3f6df13873 100644
--- a/tests/disas/gc/copying/funcref-in-gc-heap-new.wat
+++ b/tests/disas/gc/copying/funcref-in-gc-heap-new.wat
@@ -24,11 +24,11 @@
;;
;; block0(v0: i64, v1: i64, v2: i64):
;; @0020 v6 = load.i64 notrap aligned readonly can_move v0+32
-;; @0020 v7 = load.i32 notrap aligned can_move v6
+;; @0020 v7 = load.i32 notrap aligned v6
+;; @0020 v8 = load.i32 notrap aligned v6+4
;; @0020 v14 = uextend.i64 v7
;; v50 = iconst.i64 32
;; @0020 v15 = iadd v14, v50 ; v50 = 32
-;; @0020 v8 = load.i32 notrap aligned readonly can_move v6+4
;; @0020 v16 = uextend.i64 v8
;; @0020 v17 = icmp ule v15, v16
;; @0020 brif v17, block2, block3
diff --git a/tests/disas/gc/copying/struct-new-default.wat b/tests/disas/gc/copying/struct-new-default.wat
index 33e47a637b..85484358d0 100644
--- a/tests/disas/gc/copying/struct-new-default.wat
+++ b/tests/disas/gc/copying/struct-new-default.wat
@@ -23,11 +23,11 @@
;;
;; block0(v0: i64, v1: i64):
;; @0021 v8 = load.i64 notrap aligned readonly can_move v0+32
-;; @0021 v9 = load.i32 notrap aligned can_move v8
+;; @0021 v9 = load.i32 notrap aligned v8
+;; @0021 v10 = load.i32 notrap aligned v8+4
;; @0021 v16 = uextend.i64 v9
;; v50 = iconst.i64 32
;; @0021 v17 = iadd v16, v50 ; v50 = 32
-;; @0021 v10 = load.i32 notrap aligned readonly can_move v8+4
;; @0021 v18 = uextend.i64 v10
;; @0021 v19 = icmp ule v17, v18
;; @0021 brif v19, block2, block3
diff --git a/tests/disas/gc/copying/struct-new.wat b/tests/disas/gc/copying/struct-new.wat
index 2208309bd8..b362928cde 100644
--- a/tests/disas/gc/copying/struct-new.wat
+++ b/tests/disas/gc/copying/struct-new.wat
@@ -26,11 +26,11 @@
;; v52 = stack_addr.i64 ss0
;; store notrap v4, v52
;; @002a v8 = load.i64 notrap aligned readonly can_move v0+32
-;; @002a v9 = load.i32 notrap aligned can_move v8
+;; @002a v9 = load.i32 notrap aligned v8
+;; @002a v10 = load.i32 notrap aligned v8+4
;; @002a v16 = uextend.i64 v9
;; v53 = iconst.i64 32
;; @002a v17 = iadd v16, v53 ; v53 = 32
-;; @002a v10 = load.i32 notrap aligned readonly can_move v8+4
;; @002a v18 = uextend.i64 v10
;; @002a v19 = icmp ule v17, v18
;; @002a brif v19, block2, block3
diff --git a/tests/misc_testsuite/gc/active-space-end-readonly.wast b/tests/misc_testsuite/gc/active-space-end-readonly.wast
new file mode 100644
index 0000000000..83ede6df73
--- /dev/null
+++ b/tests/misc_testsuite/gc/active-space-end-readonly.wast
@@ -0,0 +1,49 @@
+;;! gc = true
+
+;; Test for the `active_space_end` field of `VMCopyingHeapData`. It should not
+;; be loaded with `readonly` + `can_move` flags. That would be buggy because
+;; `active_space_end` changes whenever the semi-spaces flip during GC. With
+;; `readonly` + `can_move`, Cranelift's LICM can hoist the load out of a loop,
+;; and after a flip the stale value can let the inline allocation fast path
+;; bump-allocate past the real end of the active semi-space into the idle
+;; semi-space, corrupting data.
+
+(module
+ (type $s (struct (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ (func (export "test")
+ (local $i i32)
+ (local $ref (ref null $s))
+
+ ;; Flip once so active_space_end = capacity (the larger value).
+ (call $gc)
+
+ ;; Allocate + GC in a tight loop. On each iteration we check that the
+ ;; struct we just allocated still holds the expected value after a GC.
+ (loop $l
+ ;; Allocate a struct with sentinel value 42.
+ (local.set $ref (struct.new $s (i32.const 42)))
+
+ ;; Force a collection, which may flip the semi-spaces.
+ (call $gc)
+
+ ;; The struct should still contain 42.
+ (call $assert_eq
+ (struct.get $s 0 (local.get $ref))
+ (i32.const 42))
+
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $l (i32.lt_u (local.get $i) (i32.const 200)))
+ )
+ )
+)
+
+(assert_return (invoke "test"))
diff --git a/tests/misc_testsuite/gc/bump-ptr-can-move.wast b/tests/misc_testsuite/gc/bump-ptr-can-move.wast
new file mode 100644
index 0000000000..f2e3500dba
--- /dev/null
+++ b/tests/misc_testsuite/gc/bump-ptr-can-move.wast
@@ -0,0 +1,41 @@
+;;! gc = true
+
+;; Test that the bump pointer load from `VMCopyingHeapData` is not GVN'd
+;; or hoisted past a GC call.
+
+(module
+ (type $s (struct (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ ;; Allocate objects in a loop, trigger GC after each one, and verify that
+ ;; each freshly allocated object has the correct value. If the bump pointer
+ ;; load were incorrectly hoisted, successive allocations could collide.
+ (func (export "test")
+ (local $i i32)
+ (local $ref (ref null $s))
+
+ (loop $l
+ ;; Each iteration uses a different value to make corruption detectable.
+ (local.set $ref
+ (struct.new $s (i32.add (local.get $i) (i32.const 100))))
+
+ (call $gc)
+
+ (call $assert_eq
+ (struct.get $s 0 (local.get $ref))
+ (i32.add (local.get $i) (i32.const 100)))
+
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $l (i32.lt_u (local.get $i) (i32.const 50)))
+ )
+ )
+)
+
+(assert_return (invoke "test"))
diff --git a/tests/misc_testsuite/gc/heap-data-ptr-readonly.wast b/tests/misc_testsuite/gc/heap-data-ptr-readonly.wast
new file mode 100644
index 0000000000..188d8b401d
--- /dev/null
+++ b/tests/misc_testsuite/gc/heap-data-ptr-readonly.wast
@@ -0,0 +1,38 @@
+;;! gc = true
+
+;; Test that the pointer to the collector's heap-data structure
+;; (VMCopyingHeapData, etc.) is loaded from vmctx with `readonly` (and, for the
+;; copying collector, also `can_move`) and that this is okay. This pointer is
+;; set once at instantiation, before any Wasm code runs, and never changes
+;; afterwards, so `readonly` is correct from the Wasm code's POV.
+
+(module
+ (type $s (struct (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ ;; Allocate, GC, allocate again, GC again, then read both objects. The
+ ;; heap-data pointer is used internally for every allocation and GC; if it
+ ;; were wrong, either the allocation would crash or the values would be
+ ;; corrupted.
+ (func (export "test")
+ (local $a (ref null $s))
+ (local $b (ref null $s))
+
+ (local.set $a (struct.new $s (i32.const 111)))
+ (call $gc)
+ (local.set $b (struct.new $s (i32.const 222)))
+ (call $gc)
+
+ (call $assert_eq (struct.get $s 0 (local.get $a)) (i32.const 111))
+ (call $assert_eq (struct.get $s 0 (local.get $b)) (i32.const 222))
+ )
+)
+
+(assert_return (invoke "test"))
diff --git a/tests/misc_testsuite/gc/kotlin-benchmark-bug.wast b/tests/misc_testsuite/gc/kotlin-benchmark-bug.wast
new file mode 100644
index 0000000000..07be7c9e3b
--- /dev/null
+++ b/tests/misc_testsuite/gc/kotlin-benchmark-bug.wast
@@ -0,0 +1,51 @@
+;;! gc = true
+
+(module $a
+ (type $s (struct (field i32)))
+ (global (export "g") (ref $s) (struct.new $s (i32.const 42)))
+)
+(register "a" $a)
+
+(module
+ (type $s (struct (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+ (import "a" "g" (global $g (ref $s)))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ (func (export "run")
+ (loop $loop
+ ;; Initial `global.get` should have the correct value.
+ global.get $g
+ struct.get $s 0
+ i32.const 42
+ call $assert_eq
+
+ ;; GC which can relocate the global's object.
+ (call $gc)
+
+ ;; Make sure that the safepoint spiller sees the loop as a loop, but don't
+ ;; actually take the back edge.
+ (if (i32.const 0)
+ (then (br $loop)))
+
+ ;; Get the global again and assert it still has the right value. We should
+ ;; not incorrectly GVN/LICM the `global.get` across the call to `$gc` when
+ ;; we are using a moving collector, which would result in a stale GC
+ ;; reference here.
+ global.get $g
+ struct.get $s 0
+ i32.const 42
+ call $assert_eq
+
+ return
+ )
+ )
+)
+
+(assert_return (invoke "run"))
diff --git a/tests/misc_testsuite/gc/ref-test-readonly.wast b/tests/misc_testsuite/gc/ref-test-readonly.wast
new file mode 100644
index 0000000000..10dbcc9ff4
--- /dev/null
+++ b/tests/misc_testsuite/gc/ref-test-readonly.wast
@@ -0,0 +1,96 @@
+;;! gc = true
+
+;; Tests that the `ref.test` implementation's loads the VMGcKind and
+;; VMSharedTypeIndex from a GC object's header do not reuse stale loads via GVN
+;; misoptimization, which could lead to the type check producing a wrong result.
+
+(module $a
+ (type $s (struct (field i32)))
+ (type $arr (array i32))
+ (global (export "gs") (ref $s) (struct.new $s (i32.const 1)))
+ (global (export "ga") (ref $arr) (array.new $arr (i32.const 2) (i32.const 3)))
+)
+(register "a" $a)
+
+;; Abstract type.
+(module
+ (type $s (struct (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+ (import "a" "gs" (global $gs (ref $s)))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ (func (export "run_kind")
+ (loop $loop
+ ;; Before GC: the struct should pass ref.test for structref.
+ (call $assert_eq
+ (ref.test (ref struct) (global.get $gs))
+ (i32.const 1))
+
+ (call $gc)
+
+ ;; Make sure that the CFG has a loop, but don't actually take it.
+ (if (i32.const 0)
+ (then (br $loop)))
+
+ ;; After GC: must still pass.
+ (call $assert_eq
+ (ref.test (ref struct) (global.get $gs))
+ (i32.const 1))
+ return
+ )
+ )
+)
+
+(assert_return (invoke "run_kind"))
+
+;; Concrete type.
+(module
+ (type $s (struct (field i32)))
+ (type $s2 (struct (field i32) (field i32)))
+
+ (import "wasmtime" "gc" (func $gc))
+ (import "a" "gs" (global $gs (ref $s)))
+
+ (func $assert_eq (param i32 i32)
+ (if (i32.eq (local.get 0) (local.get 1))
+ (then (return)))
+ unreachable
+ )
+
+ (func (export "run_concrete")
+ (loop $loop
+ ;; Before GC: should be a $s.
+ (call $assert_eq
+ (ref.test (ref $s) (global.get $gs))
+ (i32.const 1))
+
+ ;; Also NOT a $s2 (different concrete type).
+ (call $assert_eq
+ (ref.test (ref $s2) (global.get $gs))
+ (i32.const 0))
+
+ (call $gc)
+
+ ;; Make sure that the CFG has a loop, but don't actually take it.
+ (if (i32.const 0)
+ (then (br $loop)))
+
+ ;; After GC: same results.
+ (call $assert_eq
+ (ref.test (ref $s) (global.get $gs))
+ (i32.const 1))
+ (call $assert_eq
+ (ref.test (ref $s2) (global.get $gs))
+ (i32.const 0))
+ return
+ )
+ )
+)
+
+(assert_return (invoke "run_concrete"))