Skip to content

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

winch: preserve typed select result type for GC stack maps (#14455)

Daniel Macovei7220ca88bec2
diff --git a/tests/all/gc.rs b/tests/all/gc.rs
index 3061f578cf..357370cea5 100644
--- a/tests/all/gc.rs
+++ b/tests/all/gc.rs
@@ -6,6 +6,7 @@ use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
use std::task::{Context, Poll};
use wasmtime::*;
+use wasmtime_test_macros::wasmtime_test;
struct SetFlagOnDrop(Arc<AtomicBool>);
@@ -3840,6 +3841,91 @@ fn winch_externref_survives_gc_in_frame() -> Result<()> {
Ok(())
}
+/// Typed null selects retain a valid reference representation across a GC call.
+#[wasmtime_test(
+ strategies(only(Winch)),
+ collectors(All),
+ wasm_features(reference_types)
+)]
+fn typed_select_null_across_gc(config: &mut Config) -> Result<()> {
+ let engine = Engine::new(config)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "" "gc" (func $gc))
+ (func (export "select-null") (param i32) (result externref)
+ ref.null extern
+ ref.null extern
+ local.get 0
+ select (result externref)
+ call $gc))
+ "#,
+ )?;
+ let mut store = Store::new(&engine, 0usize);
+ let gc = Func::wrap(&mut store, |mut cx: Caller<'_, usize>| -> Result<()> {
+ cx.gc(None)?;
+ *cx.data_mut() += 1;
+ Ok(())
+ });
+ let instance = Instance::new(&mut store, &module, &[gc.into()])?;
+ let select =
+ instance.get_typed_func::<i32, Option<Rooted<ExternRef>>>(&mut store, "select-null")?;
+ for cond in [0, 1] {
+ assert!(select.call(&mut store, cond)?.is_none());
+ }
+ assert_eq!(*store.data(), 2, "GC calls did not run");
+ Ok(())
+}
+
+/// A typed select must keep a live externref in the call-site stack map even
+/// when its unselected operand is null.
+#[wasmtime_test(
+ strategies(only(Winch)),
+ collectors(All),
+ wasm_features(reference_types)
+)]
+fn typed_select_preserves_externref_across_gc(config: &mut Config) -> Result<()> {
+ let engine = Engine::new(config)?;
+ let module = Module::new(
+ &engine,
+ r#"
+ (module
+ (import "" "make" (func $make (result externref)))
+ (import "" "gc" (func $gc))
+
+ (func (export "control") (result externref)
+ call $make
+ call $gc)
+
+ (func (export "selected") (result externref)
+ call $make
+ ref.null extern
+ i32.const 1
+ select (result externref)
+ call $gc))
+ "#,
+ )?;
+ let mut store = Store::new(&engine, ());
+ let make = Func::wrap(
+ &mut store,
+ |mut cx: Caller<'_, ()>| -> Result<Option<Rooted<ExternRef>>> {
+ Ok(Some(ExternRef::new(&mut cx, 0xDECAFu32)?))
+ },
+ );
+ let gc = Func::wrap(&mut store, |mut cx: Caller<'_, ()>| cx.gc(None));
+ let instance = Instance::new(&mut store, &module, &[make.into(), gc.into()])?;
+ for export in ["control", "selected"] {
+ let func = instance.get_typed_func::<(), Option<Rooted<ExternRef>>>(&mut store, export)?;
+ let result = func.call(&mut store, ())?.expect("reference became null");
+ let data = result
+ .data(&store)?
+ .and_then(|value| value.downcast_ref::<u32>().copied());
+ assert_eq!(data, Some(0xDECAF), "{export}");
+ }
+ Ok(())
+}
+
/// The write barrier's decrement chain releases an object once a global stops
/// holding the last reference to it.
#[test]
diff --git a/tests/disas/winch/aarch64/select/externref-live-null-stack-map.wat b/tests/disas/winch/aarch64/select/externref-live-null-stack-map.wat
new file mode 100644
index 0000000000..5296564325
--- /dev/null
+++ b/tests/disas/winch/aarch64/select/externref-live-null-stack-map.wat
@@ -0,0 +1,91 @@
+;;! target = "aarch64"
+;;! test = "winch"
+;;! flags = "-W gc-support=y -C collector=copying"
+
+(module
+ (import "" "make" (func $make (result externref)))
+ (func $nop)
+
+ ;; The second operand is a null represented internally as i32, but the
+ ;; selected live reference must appear in the call-site stack map.
+ (func (export "select-live") (result externref)
+ call $make
+ ref.null extern
+ i32.const 1
+ select (result externref)
+ call $nop)
+)
+;; wasm[0]::function[1]::nop:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x10
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0x58
+;; 2c: mov x9, x0
+;; sub x28, x28, #0x10
+;; mov sp, x28
+;; stur x0, [x28, #8]
+;; stur x1, [x28]
+;; add x28, x28, #0x10
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; 58: udf #0xc11f
+;;
+;; wasm[0]::function[2]:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x20
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0x118
+;; 8c: mov x9, x0
+;; sub x28, x28, #0x10
+;; mov sp, x28
+;; stur x0, [x28, #8]
+;; stur x1, [x28]
+;; ldur x3, [x9, #0x48]
+;; ldur x2, [x9, #0x38]
+;; mov x0, x3
+;; mov x1, x9
+;; blr x2
+;; b4: ldur x9, [x28, #8]
+;; mov x1, #1
+;; mov x2, #0
+;; cmp w1, #0
+;; csel x2, x0, x2, ne
+;; sub x28, x28, #4
+;; mov sp, x28
+;; stur w2, [x28]
+;; sub x28, x28, #0xc
+;; mov sp, x28
+;; mov x0, x9
+;; mov x1, x9
+;; bl #0
+;; e8: add x28, x28, #0xc
+;; ╰─╼ stack_map: frame_size=48, frame_offsets=[12]
+;; mov sp, x28
+;; ldur x9, [x28, #0xc]
+;; ldur w0, [x28]
+;; add x28, x28, #4
+;; mov sp, x28
+;; add x28, x28, #0x10
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; 118: udf #0xc11f
diff --git a/tests/disas/winch/aarch64/select/externref-stack-map.wat b/tests/disas/winch/aarch64/select/externref-stack-map.wat
new file mode 100644
index 0000000000..ef8c48d6a2
--- /dev/null
+++ b/tests/disas/winch/aarch64/select/externref-stack-map.wat
@@ -0,0 +1,86 @@
+;;! target = "aarch64"
+;;! test = "winch"
+;;! flags = "-W gc-support=y -C collector=copying"
+
+(module
+ (func $nop)
+
+ ;; Neither null operand carries a reference shadow type. The declared
+ ;; select result type must supply it for the call-site stack map.
+ (func (export "select-null") (param i32) (result externref)
+ ref.null extern
+ ref.null extern
+ local.get 0
+ select (result externref)
+ call $nop)
+)
+;; wasm[0]::function[0]::nop:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x10
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0x58
+;; 2c: mov x9, x0
+;; sub x28, x28, #0x10
+;; mov sp, x28
+;; stur x0, [x28, #8]
+;; stur x1, [x28]
+;; add x28, x28, #0x10
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; 58: udf #0xc11f
+;;
+;; wasm[0]::function[1]:
+;; stp x29, x30, [sp, #-0x10]!
+;; mov x29, sp
+;; str x28, [sp, #-0x10]!
+;; mov x28, sp
+;; ldur x16, [x0, #8]
+;; ldur x16, [x16, #0x18]
+;; mov x17, #0
+;; movk x17, #0x20
+;; add x16, x16, x17
+;; cmp sp, x16
+;; b.lo #0x108
+;; 8c: mov x9, x0
+;; sub x28, x28, #0x18
+;; mov sp, x28
+;; stur x0, [x28, #0x10]
+;; stur x1, [x28, #8]
+;; stur w2, [x28, #4]
+;; ldur w0, [x28, #4]
+;; mov x1, #0
+;; mov x2, #0
+;; cmp w0, #0
+;; csel x1, x2, x1, ne
+;; sub x28, x28, #4
+;; mov sp, x28
+;; stur w1, [x28]
+;; sub x28, x28, #4
+;; mov sp, x28
+;; mov x0, x9
+;; mov x1, x9
+;; bl #0
+;; d8: add x28, x28, #4
+;; ╰─╼ stack_map: frame_size=48, frame_offsets=[4]
+;; mov sp, x28
+;; ldur x9, [x28, #0x14]
+;; ldur w0, [x28]
+;; add x28, x28, #4
+;; mov sp, x28
+;; add x28, x28, #0x18
+;; mov sp, x28
+;; mov sp, x28
+;; ldr x28, [sp], #0x10
+;; ldp x29, x30, [sp], #0x10
+;; ret
+;; 108: udf #0xc11f
diff --git a/tests/disas/winch/x64/select/externref-live-null-stack-map.wat b/tests/disas/winch/x64/select/externref-live-null-stack-map.wat
new file mode 100644
index 0000000000..e510e3c2c7
--- /dev/null
+++ b/tests/disas/winch/x64/select/externref-live-null-stack-map.wat
@@ -0,0 +1,71 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = "-W gc-support=y -C collector=copying"
+
+(module
+ (import "" "make" (func $make (result externref)))
+ (func $nop)
+
+ ;; The second operand is a null represented internally as i32, but the
+ ;; selected live reference must appear in the call-site stack map.
+ (func (export "select-live") (result externref)
+ call $make
+ ref.null extern
+ i32.const 1
+ select (result externref)
+ call $nop)
+)
+;; wasm[0]::function[1]::nop:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x10, %r11
+;; cmpq %rsp, %r11
+;; ja 0x38
+;; 1c: movq %rdi, %r14
+;; subq $0x10, %rsp
+;; movq %rdi, 8(%rsp)
+;; movq %rsi, (%rsp)
+;; addq $0x10, %rsp
+;; popq %rbp
+;; retq
+;; 38: ud2
+;;
+;; wasm[0]::function[2]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x20, %r11
+;; cmpq %rsp, %r11
+;; ja 0xcf
+;; 5c: movq %rdi, %r14
+;; subq $0x10, %rsp
+;; movq %rdi, 8(%rsp)
+;; movq %rsi, (%rsp)
+;; movq 0x48(%r14), %rdx
+;; movq 0x38(%r14), %rcx
+;; movq %rdx, %rdi
+;; movq %r14, %rsi
+;; callq *%rcx
+;; movq 8(%rsp), %r14
+;; movl $1, %ecx
+;; movl $0, %edx
+;; cmpl $0, %ecx
+;; cmovnel %eax, %edx
+;; subq $4, %rsp
+;; movl %edx, (%rsp)
+;; subq $0xc, %rsp
+;; movq %r14, %rdi
+;; movq %r14, %rsi
+;; callq 0
+;; addq $0xc, %rsp
+;; ╰─╼ stack_map: frame_size=32, frame_offsets=[12]
+;; movq 0xc(%rsp), %r14
+;; movl (%rsp), %eax
+;; addq $4, %rsp
+;; addq $0x10, %rsp
+;; popq %rbp
+;; retq
+;; cf: ud2
diff --git a/tests/disas/winch/x64/select/externref-stack-map.wat b/tests/disas/winch/x64/select/externref-stack-map.wat
new file mode 100644
index 0000000000..027aecd623
--- /dev/null
+++ b/tests/disas/winch/x64/select/externref-stack-map.wat
@@ -0,0 +1,66 @@
+;;! target = "x86_64"
+;;! test = "winch"
+;;! flags = "-W gc-support=y -C collector=copying"
+
+(module
+ (func $nop)
+
+ ;; Neither null operand carries a reference shadow type. The declared
+ ;; select result type must supply it for the call-site stack map.
+ (func (export "select-null") (param i32) (result externref)
+ ref.null extern
+ ref.null extern
+ local.get 0
+ select (result externref)
+ call $nop)
+)
+;; wasm[0]::function[0]::nop:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x10, %r11
+;; cmpq %rsp, %r11
+;; ja 0x38
+;; 1c: movq %rdi, %r14
+;; subq $0x10, %rsp
+;; movq %rdi, 8(%rsp)
+;; movq %rsi, (%rsp)
+;; addq $0x10, %rsp
+;; popq %rbp
+;; retq
+;; 38: ud2
+;;
+;; wasm[0]::function[1]:
+;; pushq %rbp
+;; movq %rsp, %rbp
+;; movq 8(%rdi), %r11
+;; movq 0x18(%r11), %r11
+;; addq $0x30, %r11
+;; cmpq %rsp, %r11
+;; ja 0xc3
+;; 5c: movq %rdi, %r14
+;; subq $0x20, %rsp
+;; movq %rdi, 0x18(%rsp)
+;; movq %rsi, 0x10(%rsp)
+;; movl %edx, 0xc(%rsp)
+;; movl 0xc(%rsp), %eax
+;; movl $0, %ecx
+;; movl $0, %edx
+;; cmpl $0, %eax
+;; cmovnel %edx, %ecx
+;; subq $4, %rsp
+;; movl %ecx, (%rsp)
+;; subq $0xc, %rsp
+;; movq %r14, %rdi
+;; movq %r14, %rsi
+;; callq 0
+;; addq $0xc, %rsp
+;; ╰─╼ stack_map: frame_size=48, frame_offsets=[12]
+;; movq 0x1c(%rsp), %r14
+;; movl (%rsp), %eax
+;; addq $4, %rsp
+;; addq $0x20, %rsp
+;; popq %rbp
+;; retq
+;; c3: ud2
diff --git a/winch/codegen/src/codegen/mod.rs b/winch/codegen/src/codegen/mod.rs
index d07f59b1b4..7b57622225 100644
--- a/winch/codegen/src/codegen/mod.rs
+++ b/winch/codegen/src/codegen/mod.rs
@@ -625,6 +625,29 @@ where
Ok(())
}
+ /// Emits a select, using the declared result type for typed selects.
+ pub fn emit_select(&mut self, ty: Option<WasmValType>) -> Result<()> {
+ let cond = self.context.pop_to_reg(self.masm, None)?;
+ let val2 = self.context.pop_to_reg(self.masm, None)?;
+ let val1 = self.context.pop_to_reg(self.masm, None)?;
+ let ty = ty.unwrap_or(val1.ty);
+ self.masm.cmp(cond.reg, RegImm::i32(0), OperandSize::S32)?;
+ // Conditionally move val1 to val2 if the comparison is not zero.
+ self.masm.cmov(
+ writable!(val2.into()),
+ val1.into(),
+ IntCmpKind::Ne,
+ ty.try_into()?,
+ )?;
+ // A null reference can have an integer shadow type. The selected value
+ // must retain the declared reference type so stack maps cover it.
+ self.context.stack.push(TypedReg::new(ty, val2.reg).into());
+ self.context.free_reg(val1.reg);
+ self.context.free_reg(cond);
+
+ Ok(())
+ }
+
/// Pops the value at the stack top and assigns it to the local at
/// the given index, returning the typed register holding the
/// source value.
diff --git a/winch/codegen/src/visitor.rs b/winch/codegen/src/visitor.rs
index f3398fc0df..1c0a74fe5f 100644
--- a/winch/codegen/src/visitor.rs
+++ b/winch/codegen/src/visitor.rs
@@ -7,7 +7,7 @@
use crate::abi::RetArea;
use crate::codegen::{
Callee, CatchInfo, CodeGen, CodeGenError, ConditionalBranch, ControlStackFrame, Emission,
- FnCall, TryTableInfo, UnconditionalBranch, control_index,
+ FnCall, TryTableInfo, TypeConverter, UnconditionalBranch, control_index,
};
use crate::masm::{
AtomicWaitKind, DivKind, Extend, ExtractLaneKind, FloatCmpKind, IntCmpKind, LoadKind,
@@ -29,8 +29,8 @@ use wasmparser::{
};
use wasmtime_cranelift::TRAP_INDIRECT_CALL_TO_NULL;
use wasmtime_environ::{
- DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, TagIndex, TypeIndex,
- WasmCompositeInnerType, WasmHeapType, WasmValType,
+ DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, TagIndex, TypeConvert,
+ TypeIndex, WasmCompositeInnerType, WasmHeapType, WasmValType,
};
/// A macro to define unsupported WebAssembly operators.
@@ -2267,27 +2267,12 @@ where
}
fn visit_select(&mut self) -> Self::Output {
- let cond = self.context.pop_to_reg(self.masm, None)?;
- let val2 = self.context.pop_to_reg(self.masm, None)?;
- let val1 = self.context.pop_to_reg(self.masm, None)?;
- self.masm.cmp(cond.reg, RegImm::i32(0), OperandSize::S32)?;
- // Conditionally move val1 to val2 if the comparison is
- // not zero.
- self.masm.cmov(
- writable!(val2.into()),
- val1.into(),
- IntCmpKind::Ne,
- val1.ty.try_into()?,
- )?;
- self.context.stack.push(val2.into());
- self.context.free_reg(val1.reg);
- self.context.free_reg(cond);
-
- Ok(())
+ self.emit_select(None)
}
- fn visit_typed_select(&mut self, _ty: ValType) -> Self::Output {
- self.visit_select()
+ fn visit_typed_select(&mut self, ty: ValType) -> Self::Output {
+ let ty = TypeConverter::new(self.env.translation, self.env.types).convert_valtype(ty)?;
+ self.emit_select(Some(ty))
}
fn visit_ref_null(&mut self, hty: HeapType) -> Self::Output {