Use the sym operator for inline assembly (#5459)

* Use the `sym` operator for inline assembly

Avoids extra `#[no_mangle]` functions and undue symbols being exposed
from Wasmtime. This is a newly stabilized feature in Rust 1.66.0. I've
also added a `rust-version` entry to the `wasmtime` crate to try to head
off possible reports in the future about odd error messages or usage of
unstable features if the rustc version is too old.

* Fix a s390x warning

* Add `rust-version` annotation to Wasmtime crate

As the other main entrypoint for embedders.
This commit is contained in:
Alex Crichton
2022-12-16 14:12:24 -06:00
committed by GitHub
parent 25bf8e0e67
commit d9fdbfd50e
12 changed files with 204 additions and 185 deletions

View File

@@ -104,7 +104,12 @@ pub mod trampolines {
// This will delegate to the outer module to the actual
// implementation and automatically perform `catch_unwind` along
// with conversion of the return value in the face of traps.
#[no_mangle]
//
// Note that rust targets which support `global_asm!` can use
// the `sym` operator to get the symbol here, but other targets
// like s390x need to use outlined assembly files which requires
// `no_mangle`.
#[cfg_attr(target_arch = "s390x", no_mangle)]
unsafe extern "C" fn [<impl_ $name>](
vmctx : *mut VMContext,
$( $pname : libcall!(@ty $param), )*

View File

@@ -112,10 +112,11 @@ macro_rules! wasm_to_libcall_trampoline {
stur lr, [x9, #32]
// Tail call to the actual implementation of this libcall.
b ", wasmtime_asm_macros::asm_sym!(stringify!($libcall_impl)), "
b {}
.cfi_endproc
"
",
sym $libcall_impl
);
};
}

View File

@@ -23,7 +23,7 @@ asm_func!(
// Store the last Wasm SP into the `last_wasm_entry_sp` in the limits, if this
// was core Wasm, otherwise store an invalid sentinal value.
sd t1,40(t0)
ld t0,16(a1)
jr t0
@@ -95,23 +95,26 @@ macro_rules! wasm_to_libcall_trampoline {
($libcall:ident ; $libcall_impl:ident) => {
wasmtime_asm_macros::asm_func!(
stringify!($libcall),
"
.cfi_startproc
concat!(
"
.cfi_startproc
// Load the pointer to `VMRuntimeLimits` in `t0`.
ld t0, 8(a0)
// Load the pointer to `VMRuntimeLimits` in `t0`.
ld t0, 8(a0)
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
sd fp, 24(t0)
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
sd fp, 24(t0)
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
sd ra, 32(t0)
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
sd ra, 32(t0)
// Tail call to the actual implementation of this libcall.
j ", wasmtime_asm_macros::asm_sym!(stringify!($libcall_impl)), "
// Tail call to the actual implementation of this libcall.
j {}
.cfi_endproc
"
.cfi_endproc
",
),
sym $libcall_impl,
);
};
}

View File

@@ -22,27 +22,29 @@ cfg_if::cfg_if! {
#[rustfmt::skip]
asm_func!(
"host_to_wasm_trampoline",
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
concat!(
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
// Load the pointer to `VMRuntimeLimits` in `scratch0`.
mov ", scratch0!(), ", 8[", arg1!(), "]
// Load the pointer to `VMRuntimeLimits` in `scratch0`.
mov ", scratch0!(), ", 8[", arg1!(), "]
// Check to see if this is a core `VMContext` (MAGIC == 'core').
cmp DWORD PTR [", arg0!(), "], 0x65726f63
// Check to see if this is a core `VMContext` (MAGIC == 'core').
cmp DWORD PTR [", arg0!(), "], 0x65726f63
// Store the last Wasm SP into the `last_wasm_entry_sp` in the limits, if this
// was core Wasm, otherwise store an invalid sentinal value.
mov ", scratch1!(), ", -1
cmove ", scratch1!(), ", rsp
mov 40[", scratch0!(), "], ", scratch1!(), "
// Store the last Wasm SP into the `last_wasm_entry_sp` in the limits, if this
// was core Wasm, otherwise store an invalid sentinal value.
mov ", scratch1!(), ", -1
cmove ", scratch1!(), ", rsp
mov 40[", scratch0!(), "], ", scratch1!(), "
// Tail call to the callee function pointer in the vmctx.
jmp 16[", arg1!(), "]
// Tail call to the callee function pointer in the vmctx.
jmp 16[", arg1!(), "]
.cfi_endproc
",
.cfi_endproc
",
),
);
#[cfg(test)]
@@ -64,28 +66,30 @@ mod host_to_wasm_trampoline_offsets_tests {
#[rustfmt::skip]
asm_func!(
"wasm_to_host_trampoline",
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
concat!(
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
// Load the pointer to `VMRuntimeLimits` in `scratch0`.
mov ", scratch0!(), ", 8[", arg1!(), "]
// Load the pointer to `VMRuntimeLimits` in `scratch0`.
mov ", scratch0!(), ", 8[", arg1!(), "]
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
mov 24[", scratch0!(), "], rbp
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
mov 24[", scratch0!(), "], rbp
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
mov ", scratch1!(), ", [rsp]
mov 32[", scratch0!(), "], ", scratch1!(), "
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
mov ", scratch1!(), ", [rsp]
mov 32[", scratch0!(), "], ", scratch1!(), "
// Tail call to the actual host function.
//
// This *must* be a tail call so that we do not push to the stack and mess
// up the offsets of stack arguments (if any).
jmp 8[", arg0!(), "]
// Tail call to the actual host function.
//
// This *must* be a tail call so that we do not push to the stack and mess
// up the offsets of stack arguments (if any).
jmp 8[", arg0!(), "]
.cfi_endproc
",
.cfi_endproc
",
),
);
#[cfg(test)]
@@ -111,25 +115,28 @@ macro_rules! wasm_to_libcall_trampoline {
($libcall:ident ; $libcall_impl:ident) => {
wasmtime_asm_macros::asm_func!(
stringify!($libcall),
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
concat!(
"
.cfi_startproc simple
.cfi_def_cfa_offset 0
// Load the pointer to `VMRuntimeLimits` in `", scratch0!(), "`.
mov ", scratch0!(), ", 8[", arg0!(), "]
// Load the pointer to `VMRuntimeLimits` in `", scratch0!(), "`.
mov ", scratch0!(), ", 8[", arg0!(), "]
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
mov 24[", scratch0!(), "], rbp
// Store the last Wasm FP into the `last_wasm_exit_fp` in the limits.
mov 24[", scratch0!(), "], rbp
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
mov ", scratch1!(), ", [rsp]
mov 32[", scratch0!(), "], ", scratch1!(), "
// Store the last Wasm PC into the `last_wasm_exit_pc` in the limits.
mov ", scratch1!(), ", [rsp]
mov 32[", scratch0!(), "], ", scratch1!(), "
// Tail call to the actual implementation of this libcall.
jmp ", wasmtime_asm_macros::asm_sym!(stringify!($libcall_impl)), "
// Tail call to the actual implementation of this libcall.
jmp {}
.cfi_endproc
",
.cfi_endproc
",
),
sym $libcall_impl
);
};
}