Skip to content

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

Account for null collector's 1-byte bump offset in `<NullHeap as GcHeap>::allocated_bytes` (#13252)

Nick Fitzgerald4dfbb10f865c
diff --git a/crates/wasmtime/src/runtime/vm/gc/enabled/null.rs b/crates/wasmtime/src/runtime/vm/gc/enabled/null.rs
index 7cc22551eb..f8da85eeff 100644
--- a/crates/wasmtime/src/runtime/vm/gc/enabled/null.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled/null.rs
@@ -332,9 +332,11 @@ unsafe impl GcHeap for NullHeap {
fn allocated_bytes(&self) -> usize {
// The null collector never frees, so everything from the start of
- // the heap up to the bump pointer is allocated.
+ // the heap up to the bump pointer is allocated. Subtract 1 because
+ // the bump pointer starts at index 1 (index 0 is unused since
+ // `VMGcRef` uses `NonZeroU32`), not because any byte was allocated.
let next = unsafe { *self.next.get() };
- usize::try_from(next.get()).unwrap()
+ usize::try_from(next.get()).unwrap() - 1
}
fn gc<'a>(
diff --git a/tests/misc_testsuite/gc/issue-13247.wast b/tests/misc_testsuite/gc/issue-13247.wast
new file mode 100644
index 0000000000..7d234ef37b
--- /dev/null
+++ b/tests/misc_testsuite/gc/issue-13247.wast
@@ -0,0 +1,32 @@
+;;! gc = true
+
+;; Regression test for https://github.com/bytecodealliance/wasmtime/issues/13247
+;;
+;; The null collector's bump pointer starts at index 1 (index 0 is reserved
+;; because `VMGcRef` uses `NonZeroU32`). The `allocated_bytes()` method was
+;; returning the raw bump pointer value, reporting 1 byte allocated on a fresh
+;; heap with no actual allocations. The GC heap starts with 0-byte capacity, so
+;; triggering a GC before any allocations stored `last_post_gc_allocated_bytes =
+;; 1`, and a subsequent allocation OOM hit `debug_assert!(1 <= 0)` inside
+;; `should_collect_first`.
+;;
+;; The `struct.new` in `test` ensures the compiled module sets
+;; `needs_gc_heap = true`, so the GC store is created during instantiation with
+;; a 0-byte heap. The start function triggers a GC before any heap allocations.
+;; After instantiation, `(ref.extern 1)` allocates an externref through the
+;; runtime path (`retry_after_gc_async`), which is where the assertion fired.
+
+(module
+ (import "wasmtime" "gc" (func $gc))
+ (type $s (struct))
+ (start $init)
+ (func $init
+ call $gc
+ )
+ (func (export "test") (param externref)
+ struct.new $s
+ drop
+ )
+)
+
+(invoke "test" (ref.extern 1))