Skip to content

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

Cranelift: don't support struct-args in `tail` calling convention. (#14649)

Chris Fallin6fe9f3e00e0f
diff --git a/cranelift/codegen/src/isa/x64/abi.rs b/cranelift/codegen/src/isa/x64/abi.rs
index 39d3f6cf67..8b2afbd93f 100644
--- a/cranelift/codegen/src/isa/x64/abi.rs
+++ b/cranelift/codegen/src/isa/x64/abi.rs
@@ -154,6 +154,11 @@ impl ABIMachineSpec for X64ABIMachineSpec {
let last_param = ix == params.len() - 1;
if let ir::ArgumentPurpose::StructArgument(size) = param.purpose {
+ if is_tail {
+ return Err(crate::CodegenError::Unsupported(
+ "struct arguments (`sarg`) in `tail` signatures".to_owned(),
+ ));
+ }
let offset = next_stack as i64;
let size = size;
assert!(size % 8 == 0, "StructArgument size is not properly aligned");
diff --git a/cranelift/codegen/src/machinst/abi.rs b/cranelift/codegen/src/machinst/abi.rs
index 915720ed64..ba3c0ec223 100644
--- a/cranelift/codegen/src/machinst/abi.rs
+++ b/cranelift/codegen/src/machinst/abi.rs
@@ -1878,6 +1878,12 @@ impl<M: ABIMachineSpec> Callee<M> {
match &sigs.args(sig)[idx] {
&ABIArg::Slots { .. } | &ABIArg::ImplicitPtrArg { .. } => {}
&ABIArg::StructArg { offset, size, .. } => {
+ // The `tail` calling convention doesn't support
+ // struct arguments, because doing so would be
+ // complex: the copies would go to the caller's
+ // own incoming argument area, which may also be
+ // where their sources are.
+ debug_assert!(!is_tail_call);
let tmp = vregs.alloc_with_deferred_error(word_ty).only_reg().unwrap();
insts.push(M::gen_get_stack_addr(
stack_arg(offset),
diff --git a/cranelift/codegen/src/verifier/mod.rs b/cranelift/codegen/src/verifier/mod.rs
index 99e9a10747..3032e661c6 100644
--- a/cranelift/codegen/src/verifier/mod.rs
+++ b/cranelift/codegen/src/verifier/mod.rs
@@ -2087,6 +2087,19 @@ impl<'a> Verifier<'a> {
))?;
}
}
+ CallConv::Tail => {
+ if sig
+ .params
+ .iter()
+ .any(|p| matches!(p.purpose, ArgumentPurpose::StructArgument(_)))
+ {
+ errors.fatal((
+ entity,
+ "Signature with `tail` ABI cannot have struct arguments (`sarg`)"
+ .to_string(),
+ ))?;
+ }
+ }
_ => {}
}
Ok(())
diff --git a/cranelift/filetests/filetests/verifier/tail-sarg.clif b/cranelift/filetests/filetests/verifier/tail-sarg.clif
new file mode 100644
index 0000000000..aab16ae7cc
--- /dev/null
+++ b/cranelift/filetests/filetests/verifier/tail-sarg.clif
@@ -0,0 +1,34 @@
+test verifier
+
+; Struct arguments (`sarg`) are not supported with the `tail` ABI: a
+; `return_call` would have to copy them into the caller's own incoming
+; argument area, which may also be where they're copied from.
+function %f0(i64 sarg(16)) tail { ; error: Signature with `tail` ABI cannot have struct arguments (`sarg`)
+block0(v0: i64):
+ return
+}
+
+function %f1(i64) {
+ fn0 = %g(i64 sarg(16)) tail ; error: Signature with `tail` ABI cannot have struct arguments (`sarg`)
+
+block0(v0: i64):
+ call fn0(v0)
+ return
+}
+
+function %f2(i64, i64) {
+ sig0 = (i64 sarg(16)) tail ; error: Signature with `tail` ABI cannot have struct arguments (`sarg`)
+
+block0(v0: i64, v1: i64):
+ call_indirect sig0, v1(v0)
+ return
+}
+
+; Other ABIs can still use struct arguments.
+function %f3(i64 sarg(16)) system_v {
+ fn0 = %g(i64 sarg(16)) system_v
+
+block0(v0: i64):
+ call fn0(v0)
+ return
+}