From ff81d020954aa1347f6189d18ca478c9789b8bf6 Mon Sep 17 00:00:00 2001 From: Adam Petro Date: Fri, 30 Jun 2023 18:26:17 -0400 Subject: [PATCH] Attempt to handle the s390x case --- crates/fiber/Cargo.toml | 3 ++- crates/fiber/build.rs | 13 +++++++++++-- crates/fiber/src/unix/s390x.S | 11 ++++++----- crates/runtime/Cargo.toml | 1 + crates/runtime/build.rs | 13 +++++++++++-- crates/runtime/src/lib.rs | 5 ----- crates/runtime/src/libcalls.rs | 1 - crates/runtime/src/trampolines/s390x.S | 14 ++++++++------ crates/versioned-export-macros/src/lib.rs | 16 ++++++++++++++++ 9 files changed, 55 insertions(+), 22 deletions(-) diff --git a/crates/fiber/Cargo.toml b/crates/fiber/Cargo.toml index 7a91485ac4fb..b3ed5a49da7e 100644 --- a/crates/fiber/Cargo.toml +++ b/crates/fiber/Cargo.toml @@ -9,11 +9,11 @@ edition.workspace = true [dependencies] cfg-if = { workspace = true } -wasmtime-versioned-export-macros = { workspace = true } [target.'cfg(unix)'.dependencies] rustix = { workspace = true, features = ["mm", "param"] } wasmtime-asm-macros = { workspace = true } +wasmtime-versioned-export-macros = { workspace = true } [target.'cfg(windows)'.dependencies.windows-sys] workspace = true @@ -24,6 +24,7 @@ features = [ [build-dependencies] cc = "1.0" +wasmtime-versioned-export-macros = { workspace = true } [dev-dependencies] backtrace = "0.3.61" diff --git a/crates/fiber/build.rs b/crates/fiber/build.rs index 6bfc5c4b9daa..d6f2bd9c9404 100644 --- a/crates/fiber/build.rs +++ b/crates/fiber/build.rs @@ -1,4 +1,5 @@ -use std::env; +use std::{env, fs, path::PathBuf}; +use wasmtime_versioned_export_macros::versioned_suffix; fn main() { let mut build = cc::Build::new(); @@ -9,7 +10,15 @@ fn main() { build.file("src/windows.c"); } else if arch == "s390x" { println!("cargo:rerun-if-changed=src/unix/s390x.S"); - build.file("src/unix/s390x.S"); + + // cc does not preprocess macros passed with -D for `.s` files so need to do + // it manually + let asm = fs::read_to_string("src/unix/s390x.S").unwrap(); + let asm = asm.replace("VERSIONED_SUFFIX", versioned_suffix!()); + let out_dir = env::var("OUT_DIR").unwrap(); + let file_path = PathBuf::from(out_dir).join("s390x_preprocessed.S"); + fs::write(&file_path, asm).unwrap(); + build.file(file_path); } else { // assume that this is included via inline assembly in the crate itself, // and the crate will otherwise have a `compile_error!` for unsupported diff --git a/crates/fiber/src/unix/s390x.S b/crates/fiber/src/unix/s390x.S index a5a47118bedb..f8b36946a33a 100644 --- a/crates/fiber/src/unix/s390x.S +++ b/crates/fiber/src/unix/s390x.S @@ -10,11 +10,12 @@ .text -#define GLOBL(fnname) .globl fnname -#define HIDDEN(fnname) .hidden fnname -#define TYPE(fnname) .type fnname,@function -#define FUNCTION(fnname) fnname -#define SIZE(fnname) .size fnname,.-fnname +#define VERSIONED_SYMBOL(a) a ## VERSIONED_SUFFIX +#define GLOBL(fnname) .globl VERSIONED_SYMBOL(fnname) +#define HIDDEN(fnname) .hidden VERSIONED_SYMBOL(fnname) +#define TYPE(fnname) .type VERSIONED_SYMBOL(fnname),@function +#define FUNCTION(fnname) VERSIONED_SYMBOL(fnname) +#define SIZE(fnname) .size VERSIONED_SYMBOL(fnname),.-VERSIONED_SYMBOL(fnname) // fn(top_of_stack(%x0): *mut u8) HIDDEN(wasmtime_fiber_switch) diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index 2962218265b5..b32291c01fc4 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -51,6 +51,7 @@ once_cell = { workspace = true } [build-dependencies] cc = "1.0" +wasmtime-versioned-export-macros = { workspace = true } [features] async = ["wasmtime-fiber"] diff --git a/crates/runtime/build.rs b/crates/runtime/build.rs index 5cf6326b3219..006402238591 100644 --- a/crates/runtime/build.rs +++ b/crates/runtime/build.rs @@ -1,4 +1,5 @@ -use std::env; +use std::{env, fs, path::PathBuf}; +use wasmtime_versioned_export_macros::versioned_suffix; fn main() { let mut build = cc::Build::new(); @@ -9,7 +10,15 @@ fn main() { build.define(&format!("CFG_TARGET_ARCH_{}", arch), None); if arch == "s390x" { println!("cargo:rerun-if-changed=src/trampolines/s390x.S"); - build.file("src/trampolines/s390x.S"); + + // cc does not preprocess macros passed with -D for `.s` files so need to do + // it manually + let asm = fs::read_to_string("src/trampolines/s390x.S").unwrap(); + let asm = asm.replace("VERSIONED_SUFFIX", versioned_suffix!()); + let out_dir = env::var("OUT_DIR").unwrap(); + let file_path = PathBuf::from(out_dir).join("s390x_preprocessed.S"); + fs::write(&file_path, asm).unwrap(); + build.file(file_path); } println!("cargo:rerun-if-changed=src/helpers.c"); build.file("src/helpers.c"); diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 1a90c4fc06c1..e23851799348 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -283,8 +283,3 @@ impl fmt::Display for WasmFault { ) } } - -#[cfg(all(target_arch = "s390x", feature = "versioned-exports"))] -compile_error!( - "Feature `versioned-exports` is not currently supported for the `s390x` architecture" -); diff --git a/crates/runtime/src/libcalls.rs b/crates/runtime/src/libcalls.rs index 6713c218ee37..a4b4db518026 100644 --- a/crates/runtime/src/libcalls.rs +++ b/crates/runtime/src/libcalls.rs @@ -110,7 +110,6 @@ pub mod trampolines { // 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)] #[cfg_attr(target_arch = "s390x", wasmtime_versioned_export_macros::versioned_export)] unsafe extern "C" fn []( vmctx: *mut VMContext, diff --git a/crates/runtime/src/trampolines/s390x.S b/crates/runtime/src/trampolines/s390x.S index e014e1205082..325165a8c0e7 100644 --- a/crates/runtime/src/trampolines/s390x.S +++ b/crates/runtime/src/trampolines/s390x.S @@ -9,12 +9,14 @@ .type host_to_wasm_trampoline,@function .p2align 2 +#define VERSIONED_SYMBOL(a) a ## VERSIONED_SUFFIX + #define LIBCALL_TRAMPOLINE(libcall, libcall_impl) \ - .hidden libcall ; \ - .globl libcall ; \ - .type libcall,@function ; \ + .hidden VERSIONED_SYMBOL(libcall) ; \ + .globl VERSIONED_SYMBOL(libcall) ; \ + .type VERSIONED_SYMBOL(libcall),@function ; \ .p2align 2 ; \ -libcall: ; \ +VERSIONED_SYMBOL(libcall): ; \ .cfi_startproc ; \ \ /* Load the pointer to `VMRuntimeLimits` in `%r1`. */ \ @@ -28,10 +30,10 @@ libcall: ; stg %r14, 32(%r1) ; \ \ /* Tail call to the actual implementation of this libcall. */ \ - jg libcall_impl ; \ + jg VERSIONED_SYMBOL(libcall_impl) ; \ \ .cfi_endproc ; \ - .size libcall,.-libcall + .size VERSIONED_SYMBOL(libcall),.-VERSIONED_SYMBOL(libcall) LIBCALL_TRAMPOLINE(memory32_grow, impl_memory32_grow) LIBCALL_TRAMPOLINE(table_grow_func_ref, impl_table_grow_func_ref) diff --git a/crates/versioned-export-macros/src/lib.rs b/crates/versioned-export-macros/src/lib.rs index 0619ef0f3a2f..4d3022c90fc1 100644 --- a/crates/versioned-export-macros/src/lib.rs +++ b/crates/versioned-export-macros/src/lib.rs @@ -54,3 +54,19 @@ pub fn versioned_stringify_ident(item: proc_macro::TokenStream) -> proc_macro::T .to_token_stream() .into() } + +#[proc_macro] +pub fn versioned_suffix(item: proc_macro::TokenStream) -> proc_macro::TokenStream { + if !item.is_empty() { + return syn::Error::new( + proc_macro2::Span::call_site(), + "`versioned_suffix!` accepts no input", + ) + .to_compile_error() + .into(); + }; + + syn::LitStr::new(version("").as_str(), proc_macro2::Span::call_site()) + .to_token_stream() + .into() +}