Skip to content

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

Fix compiling stack-switching with GC (#12967)

Alex Crichtonc80d8d323ffc
diff --git a/crates/cranelift/src/func_environ/gc/enabled.rs b/crates/cranelift/src/func_environ/gc/enabled.rs
index 45f52d6769..73ef862997 100644
--- a/crates/cranelift/src/func_environ/gc/enabled.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled.rs
@@ -212,9 +212,7 @@ fn read_field_at_addr(
}
WasmHeapTopType::Cont => {
// TODO(#10248) GC integration for stack switching
- return Err(wasmtime_environ::WasmError::Unsupported(
- "Stack switching feature not compatible with GC, yet".to_string(),
- ));
+ return stack_switching_unsupported();
}
},
},
@@ -290,13 +288,17 @@ fn write_field_at_addr(
WasmStorageType::I16 => {
builder.ins().istore16(flags, new_val, field_addr, 0);
}
- WasmStorageType::Val(WasmValType::Ref(r)) if r.heap_type.top() == WasmHeapTopType::Func => {
- write_func_ref_at_addr(func_env, builder, r, flags, field_addr, new_val)?;
- }
- WasmStorageType::Val(WasmValType::Ref(r)) => {
- gc_compiler(func_env)?
- .translate_write_gc_reference(func_env, builder, r, field_addr, new_val, flags)?;
- }
+ WasmStorageType::Val(WasmValType::Ref(r)) => match r.heap_type.top() {
+ WasmHeapTopType::Func => {
+ write_func_ref_at_addr(func_env, builder, r, flags, field_addr, new_val)?
+ }
+ WasmHeapTopType::Extern | WasmHeapTopType::Any | WasmHeapTopType::Exn => {
+ gc_compiler(func_env)?.translate_write_gc_reference(
+ func_env, builder, r, field_addr, new_val, flags,
+ )?;
+ }
+ WasmHeapTopType::Cont => return stack_switching_unsupported(),
+ },
WasmStorageType::Val(_) => {
assert_eq!(
builder.func.dfg.value_type(new_val).bytes(),
@@ -1285,9 +1287,7 @@ pub fn translate_ref_test(
}
WasmHeapType::ConcreteCont(_) => {
// TODO(#10248) GC integration for stack switching
- return Err(wasmtime_environ::WasmError::Unsupported(
- "Stack switching feature not compatible with GC, yet".to_string(),
- ));
+ return stack_switching_unsupported();
}
};
builder.ins().jump(continue_block, &[result.into()]);
@@ -1695,3 +1695,9 @@ impl FuncEnvironment<'_> {
result
}
}
+
+fn stack_switching_unsupported<T>() -> WasmResult<T> {
+ Err(wasmtime_environ::WasmError::Unsupported(
+ "Stack switching feature not compatible with GC, yet".to_string(),
+ ))
+}
diff --git a/crates/cranelift/src/func_environ/gc/enabled/drc.rs b/crates/cranelift/src/func_environ/gc/enabled/drc.rs
index 037e09d551..aada2c8f39 100644
--- a/crates/cranelift/src/func_environ/gc/enabled/drc.rs
+++ b/crates/cranelift/src/func_environ/gc/enabled/drc.rs
@@ -217,14 +217,15 @@ impl DrcCompiler {
let flags = ir::MemFlags::trusted().with_endianness(ir::Endianness::Little);
match ty {
- WasmStorageType::Val(WasmValType::Ref(r))
- if r.heap_type.top() == WasmHeapTopType::Func =>
- {
- write_func_ref_at_addr(func_env, builder, r, flags, field_addr, val)?;
- }
- WasmStorageType::Val(WasmValType::Ref(r)) => {
- self.translate_init_gc_reference(func_env, builder, r, field_addr, val, flags)?;
- }
+ WasmStorageType::Val(WasmValType::Ref(r)) => match r.heap_type.top() {
+ WasmHeapTopType::Func => {
+ write_func_ref_at_addr(func_env, builder, r, flags, field_addr, val)?
+ }
+ WasmHeapTopType::Extern | WasmHeapTopType::Any | WasmHeapTopType::Exn => {
+ self.translate_init_gc_reference(func_env, builder, r, field_addr, val, flags)?
+ }
+ WasmHeapTopType::Cont => return super::stack_switching_unsupported(),
+ },
WasmStorageType::I8 => {
assert_eq!(builder.func.dfg.value_type(val), ir::types::I32);
builder.ins().istore8(flags, val, field_addr, 0);
diff --git a/crates/test-util/src/wast.rs b/crates/test-util/src/wast.rs
index 6992ecccbb..59fe07a5fd 100644
--- a/crates/test-util/src/wast.rs
+++ b/crates/test-util/src/wast.rs
@@ -369,7 +369,19 @@ impl Compiler {
/// `Config::compiler_panicking_wasm_features`.
pub fn should_fail(&self, config: &TestConfig) -> bool {
match self {
- Compiler::CraneliftNative => config.legacy_exceptions(),
+ Compiler::CraneliftNative => {
+ if config.legacy_exceptions() {
+ return true;
+ }
+
+ // Stack-switching is only implemented on x86_64 for unix
+ // platforms right now.
+ if config.stack_switching() && !(cfg!(target_arch = "x86_64") && cfg!(unix)) {
+ return true;
+ }
+
+ false
+ }
Compiler::Winch => {
if config.gc()
diff --git a/tests/misc_testsuite/stack-switching-and-gc.wast b/tests/misc_testsuite/stack-switching-and-gc.wast
new file mode 100644
index 0000000000..229f2fe16d
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching-and-gc.wast
@@ -0,0 +1,13 @@
+;;! stack_switching = true
+;;! gc = true
+
+(assert_invalid
+ (module
+ (type $f (func))
+ (type $c (cont $f))
+ (type $s (struct (field (ref null $c))))
+ (func (export "run")
+ (drop (struct.new_default $s))
+ )
+ )
+ "Stack switching feature not compatible with GC")