From a8266141929ff86aad6ef34f6994c5ab3c437500 Mon Sep 17 00:00:00 2001 From: Lucy Menon <168595099+syntactically@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:07:20 +0000 Subject: [PATCH 1/2] [hyperlight_wasm_aot] Fix wasmtime version reporting `hyperlight-wasm-aot check-wasmtime-version` attempts to report the version of wasmtime that it was compiled with, in order to make it clear what version it is testing the provided file with. Previously, it used a `cargo metadata` invocation at runtime to attempt to obtain this information from the cargo crate dependency graph. Unfortunately, this only appeared to be working because we test running it in the normal cargo source tree: `cargo metadata` looks up the metadata of the directory in which the command is run at runtime. This commit replaces that cargo metadata invocation by one in a new hyperlight-wasm-aot build.rs, and compiles the relevant dependency versions directly into the executable. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com> --- src/hyperlight_wasm_aot/Cargo.toml | 5 ++++- src/hyperlight_wasm_aot/build.rs | 33 +++++++++++++++++++++++++++++ src/hyperlight_wasm_aot/src/main.rs | 13 +----------- 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/hyperlight_wasm_aot/build.rs diff --git a/src/hyperlight_wasm_aot/Cargo.toml b/src/hyperlight_wasm_aot/Cargo.toml index 000b2749..c3363b74 100644 --- a/src/hyperlight_wasm_aot/Cargo.toml +++ b/src/hyperlight_wasm_aot/Cargo.toml @@ -18,9 +18,12 @@ Application to precompile WebAssembly binaries to for hyperlight-wasm. wasmtime = { version = "47.0.4", default-features = false, features = ["cranelift", "pulley", "runtime", "component-model"] } wasmtime_lts = { package = "wasmtime", version = "36.0.14", default-features = false, features = ["cranelift", "pulley", "runtime", "component-model"] } clap = { version = "4.6", features = ["derive"] } +object = { version = "0.39.1", default-features = false, features = ["read_core", "elf"] } +rustc_version = "0.4.1" + +[build-dependencies] cargo_metadata = "0.23" cargo-util-schemas = "=0.14.1" -object = { version = "0.39.1", default-features = false, features = ["read_core", "elf"] } [features] gdb = ["wasmtime/debug-builtins", "wasmtime_lts/debug-builtins"] diff --git a/src/hyperlight_wasm_aot/build.rs b/src/hyperlight_wasm_aot/build.rs new file mode 100644 index 00000000..da7f94ba --- /dev/null +++ b/src/hyperlight_wasm_aot/build.rs @@ -0,0 +1,33 @@ +use cargo_metadata::MetadataCommand; +use cargo_util_schemas::manifest::PackageName; + +fn main() { + let metadata = MetadataCommand::new().exec().unwrap(); + let this_pkg_name = PackageName::new("hyperlight-wasm-aot").unwrap(); + let this_pkg_id = metadata + .workspace_members + .iter() + .find(|x| metadata[x].name == this_pkg_name) + .unwrap(); + let this_pkg = &metadata.resolve.as_ref().unwrap()[this_pkg_id]; + let get_dep = |name| { + let mut iter = this_pkg.deps.iter().filter(|x| x.name == name); + let dep = iter + .next() + .unwrap_or_else(|| panic!("hyperlight-wasm-aot has no {name} dependency")); + if iter.next().is_some() { + panic!("hyperlight-wasm-aot has multiple {name} dependencies"); + } + dep + }; + let wasmtime_lts = &metadata[&get_dep("wasmtime_lts").pkg]; + let wasmtime_latest = &metadata[&get_dep("wasmtime").pkg]; + println!( + "cargo::rustc-env=WASMTIME_LTS_VERSION={}", + wasmtime_lts.version + ); + println!( + "cargo::rustc-env=WASMTIME_LATEST_VERSION={}", + wasmtime_latest.version + ); +} diff --git a/src/hyperlight_wasm_aot/src/main.rs b/src/hyperlight_wasm_aot/src/main.rs index 0b39d404..03c0337a 100644 --- a/src/hyperlight_wasm_aot/src/main.rs +++ b/src/hyperlight_wasm_aot/src/main.rs @@ -262,18 +262,7 @@ fn main() { } => { match wasmtime_version { WasmtimeVersion::Latest => { - // get the wasmtime version used by hyperlight-wasm-aot - let metadata = MetadataCommand::new().exec().unwrap(); - let package_name = PackageName::new("wasmtime".to_string()).unwrap(); - let wasmtime_package: Option<&Package> = metadata - .packages - .iter() - .filter(|p| p.name == package_name) - .find(|p| p.version.major > 36); - let version_number = match wasmtime_package { - Some(pkg) => pkg.version.clone(), - None => panic!("latest wasmtime dependency not found"), - }; + let version_number = std::env!("WASMTIME_LATEST_VERSION"); if debug { println!( "Checking Wasmtime version used to compile debug info enabled file: {} (latest wasmtime)", From fcbe4c399ddf2bb3334d49e9ad45cf96b92734d1 Mon Sep 17 00:00:00 2001 From: Lucy Menon <168595099+syntactically@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:10:12 +0000 Subject: [PATCH 2/2] [hyperlight_wasm_aot] Unify more lts and latest codepaths Previously, both of the hyperlight-wasm-aot commands had completely divergent implementations for the wasmtime LTS and latest wasmtime cases, even though the core logic of both was precisely the same. This was probably mostly due to the annoyance of having to deal with the different types (e.g. `wasmtime::Config` vs `wasmtime_lts::Config`) throughout, making it difficult to provide abstractions which work on either version. This commit adds some macros to make that easier, and converges the code paths. Signed-off-by: Lucy Menon <168595099+syntactically@users.noreply.github.com> --- src/hyperlight_wasm_aot/src/main.rs | 420 +++++++++++++--------------- 1 file changed, 197 insertions(+), 223 deletions(-) diff --git a/src/hyperlight_wasm_aot/src/main.rs b/src/hyperlight_wasm_aot/src/main.rs index 03c0337a..3ed41ca5 100644 --- a/src/hyperlight_wasm_aot/src/main.rs +++ b/src/hyperlight_wasm_aot/src/main.rs @@ -17,12 +17,9 @@ limitations under the License. use std::fmt::Display; use std::path::Path; -use cargo_metadata::{MetadataCommand, Package}; -use cargo_util_schemas::manifest::PackageName; use clap::{Parser, Subcommand, ValueEnum}; use object::read::elf::ElfFile64; use object::{Architecture, Endianness, FileFlags, Object}; -use wasmtime::{Config, Engine, Module, OptLevel, Precompiled}; #[derive(Debug)] enum SupportedTarget { @@ -46,6 +43,91 @@ enum WasmtimeVersion { Lts, Latest, } +impl Display for WasmtimeVersion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + WasmtimeVersion::Lts => write!(f, "LTS"), + WasmtimeVersion::Latest => write!(f, "latest"), + } + } +} +impl WasmtimeVersion { + fn dependency_version(self) -> &'static str { + match self { + WasmtimeVersion::Lts => std::env!("WASMTIME_LTS_VERSION"), + WasmtimeVersion::Latest => std::env!("WASMTIME_LATEST_VERSION"), + } + } +} +enum WasmtimeVersioned { + Lts(Lts), + Latest(Latest), +} +macro_rules! wvt { + ($t:ident) => { + type $t = WasmtimeVersioned; + }; +} +wvt!(Config); +impl Config { + fn new(wv: WasmtimeVersion) -> Config { + match wv { + WasmtimeVersion::Lts => WasmtimeVersioned::Lts(wasmtime_lts::Config::new()), + WasmtimeVersion::Latest => WasmtimeVersioned::Latest(wasmtime::Config::new()), + } + } +} + +macro_rules! wv { + (@impl [] [$($acc:tt)*] $v:ident, $($_:tt)*) => { + match $v { + $($acc)* + } + }; + (@impl [nonexhaustive] [$($acc:tt)*] $v:ident, $($x:tt)*) => { + wv!(@impl [] [$($acc)* _ => {}] $v, $($x)*) + }; + (@impl [$variant:ident $module:ident $($scope:tt)*] [$($acc:tt)*] $v:ident, $($x:tt)*) => { + wv!(@impl [$($scope)*] [$($acc)* + WasmtimeVersioned::$variant(ref mut $v) => { + #[allow(unused)] + use $module as w; + $($x)* + }, + ] $v, $($x)*) + }; + (@scoped [$($scope:tt)*], $v:ident . $($x:tt)*) => { + wv!(@impl [$($scope)*] [] $v, $v.$($x)*;) + }; + (@scoped [$($scope:tt)*], $v:ident, $($x:tt)*) => { + wv!(@impl [$($scope)*] [] $v, $($x)*) + }; + (latest, $($x:tt)*) => { + wv!(@scoped [Latest wasmtime nonexhaustive], $($x)*) + }; + (lts, $($x:tt)*) => { + wv!(@scoped [Lts wasmtime_lts nonexhaustive], $($x)*)b + }; + ($($x:tt)*) => { + wv!(@scoped [Lts wasmtime_lts Latest wasmtime], $($x)*) + }; +} +macro_rules! wvwv { + ($v:ident, $($x:tt)*) => { + match $v { + WasmtimeVersioned::Lts($v) => { + #[allow(unused)] + use wasmtime_lts as w; + WasmtimeVersioned::Lts($($x)*) + } + WasmtimeVersioned::Latest($v) => { + #[allow(unused)] + use wasmtime as w; + WasmtimeVersioned::Latest($($x)*) + } + } + } +} #[derive(Parser)] #[command(name = "hyperlight-wasm-aot")] @@ -102,74 +184,6 @@ enum Commands { }, } -/// Precompile bytes using the LTS wasmtime version -fn precompile_bytes_lts( - bytes: &[u8], - debug: bool, - minimal: bool, - pulley: bool, - is_component: bool, -) -> Vec { - let mut config = wasmtime_lts::Config::new(); - if pulley { - config.target("pulley64").unwrap(); - } else { - #[cfg(target_arch = "x86_64")] - config.target("x86_64-unknown-none").unwrap(); - #[cfg(target_arch = "aarch64")] - config.target("aarch64-unknown-none").unwrap(); - } - if debug { - config.debug_info(true); - config.cranelift_opt_level(wasmtime_lts::OptLevel::None); - } - if minimal { - config.generate_address_map(false); - config.native_unwind_info(false); - } - let engine = wasmtime_lts::Engine::new(&config).unwrap(); - if is_component { - engine.precompile_component(bytes).unwrap() - } else { - engine.precompile_module(bytes).unwrap() - } -} - -/// Detect and deserialize using the LTS wasmtime version -fn detect_and_deserialize_lts(bytes: &[u8], debug: bool, file: &str) { - let mut config = wasmtime_lts::Config::new(); - #[cfg(target_arch = "x86_64")] - config.target("x86_64-unknown-none").unwrap(); - #[cfg(target_arch = "aarch64")] - config.target("aarch64-unknown-none").unwrap(); - if debug { - config.debug_info(true); - config.cranelift_opt_level(wasmtime_lts::OptLevel::None); - } - let engine = wasmtime_lts::Engine::new(&config).unwrap(); - match wasmtime_lts::Engine::detect_precompiled(bytes) { - Some(wasmtime_lts::Precompiled::Module) => { - println!("The file is a valid AOT compiled Wasmtime module"); - match unsafe { wasmtime_lts::Module::deserialize(&engine, bytes) } { - Ok(_) => println!( - "File {} was AOT compiled with a compatible wasmtime version (LTS)", - file - ), - Err(e) => eprintln!("{}", e), - } - } - Some(wasmtime_lts::Precompiled::Component) => { - println!("The file is an AOT compiled Wasmtime component") - } - None => { - eprintln!( - "Error - {} is not a valid AOT compiled Wasmtime module or component", - file - ); - } - } -} - fn main() { let cli = Cli::parse(); @@ -192,195 +206,155 @@ fn main() { } }; - match wasmtime_version { - WasmtimeVersion::Latest => { - let target = if pulley { - SupportedTarget::WasmtimePulley64 - } else { - #[cfg(target_arch = "x86_64")] - let target = SupportedTarget::X86_64UnknownNone; - #[cfg(target_arch = "aarch64")] - let target = SupportedTarget::AArch64UnknownNone; - target - }; - if debug { - println!( - "Aot Compiling {} to [{}]: {} with debug info and optimizations off (latest wasmtime)", - input, target, outfile - ); - } else { - println!( - "Aot Compiling {} to [{}]: {} (latest wasmtime)", - input, target, outfile - ); - } - let config = get_config(debug, minimal, &target); - let engine = Engine::new(&config).unwrap(); - let bytes = std::fs::read(&input).unwrap(); - let serialized = if component { - engine.precompile_component(&bytes).unwrap() - } else { - engine.precompile_module(&bytes).unwrap() - }; - std::fs::write(outfile, serialized).unwrap(); - } - WasmtimeVersion::Lts => { - let target_name = if pulley { - "pulley64" - } else { - #[cfg(target_arch = "x86_64")] - { - "x86_64-unknown-none" - } - #[cfg(target_arch = "aarch64")] - { - "aarch64-unknown-none" - } - }; - if debug { - println!( - "Aot Compiling {} to [{}]: {} with debug info and optimizations off (LTS wasmtime)", - input, target_name, outfile - ); - } else { - println!( - "Aot Compiling {} to [{}]: {} (LTS wasmtime)", - input, target_name, outfile - ); - } - let bytes = std::fs::read(&input).unwrap(); - let serialized = - precompile_bytes_lts(&bytes, debug, minimal, pulley, component); - std::fs::write(outfile, serialized).unwrap(); - } + let target = if pulley { + SupportedTarget::WasmtimePulley64 + } else { + #[cfg(target_arch = "x86_64")] + let target = SupportedTarget::X86_64UnknownNone; + #[cfg(target_arch = "aarch64")] + let target = SupportedTarget::AArch64UnknownNone; + target + }; + + if debug { + println!( + "Aot Compiling {} to [{}]: {} with debug info and optimizations off ({} wasmtime)", + input, target, outfile, wasmtime_version + ); + } else { + println!( + "Aot Compiling {} to [{}]: {} ({} wasmtime)", + input, target, outfile, wasmtime_version + ); } + + let config = get_config(wasmtime_version, debug, minimal, &target); + let mut engine = wvwv!(config, w::Engine::new(&config).unwrap()); + let bytes = std::fs::read(&input).unwrap(); + let serialized = if component { + wv!(engine, engine.precompile_component(&bytes).unwrap()) + } else { + wv!(engine, engine.precompile_module(&bytes).unwrap()) + }; + std::fs::write(outfile, serialized).unwrap(); } Commands::CheckWasmtimeVersion { file, debug, wasmtime_version, } => { - match wasmtime_version { - WasmtimeVersion::Latest => { - let version_number = std::env!("WASMTIME_LATEST_VERSION"); - if debug { - println!( - "Checking Wasmtime version used to compile debug info enabled file: {} (latest wasmtime)", - file - ); - } else { - println!( - "Checking Wasmtime version used to compile file: {} (latest wasmtime)", - file - ); - } - let bytes = std::fs::read(&file).unwrap(); - let target = match get_aot_target(&bytes) { - Ok(target) => target, - Err(e) => { - eprintln!( - "Error - {} is not a valid precompiled Wasmtime module: {}", - file, e - ); - std::process::exit(1) - } - }; - let config = get_config(debug, false, &target); - let engine = Engine::new(&config).unwrap(); - match Engine::detect_precompiled(&bytes) { - Some(pre_compiled) => { - match pre_compiled { - Precompiled::Module => { - println!("The file is a valid AOT compiled Wasmtime module"); - // It doesnt seem like the functions or data needed to extract the version of wasmtime used to compile the module are exposed in the wasmtime crate - // so we will try and load it and then catch the error and parse the version from the error message :-( - match unsafe { Module::deserialize(&engine, bytes) } { - Ok(_) => println!( + if debug { + println!( + "Checking Wasmtime version used to compile with debug info enabled file: {} ({} wasmtime)", + file, wasmtime_version + ); + } else { + println!( + "Checking Wasmtime version used to compile file: {} ({} wasmtime)", + file, wasmtime_version + ); + } + + // get the wasmtime version used by hyperlight-wasm-aot + let version_number = wasmtime_version.dependency_version(); + + let bytes = std::fs::read(&file).unwrap(); + let target = match get_aot_target(&bytes) { + Ok(target) => target, + Err(e) => { + eprintln!( + "Error - {} is not a valid precompiled Wasmtime module: {}", + file, e + ); + std::process::exit(1) + } + }; + let config = get_config(wasmtime_version, debug, false, &target); + let engine = wvwv!(config, w::Engine::new(&config).unwrap()); + wvwv!( + engine, + match w::Engine::detect_precompiled(&bytes) { + Some(pre_compiled) => { + match pre_compiled { + w::Precompiled::Module => { + println!("The file is a valid AOT compiled Wasmtime module"); + // It doesnt seem like the functions or data needed to extract the version of wasmtime used to compile the module are exposed in the wasmtime crate + // so we will try and load it and then catch the error and parse the version from the error message :-( + match unsafe { w::Module::deserialize(&engine, bytes) } { + Ok(_) => println!( + "File {} was AOT compiled to '{}' with wasmtime version compatible with: {}", + file, target, version_number + ), + Err(e) => { + let error_message = e.to_string(); + if !error_message.starts_with( + "Module was compiled with incompatible Wasmtime version", + ) { + eprintln!("{}", error_message); + return; + } + let version = error_message.trim_start_matches("Module was compiled with incompatible Wasmtime version ").trim(); + println!( "File {} was AOT compiled to '{}' with wasmtime version: {}", - file, target, version_number - ), - Err(e) => { - let error_message = e.to_string(); - if !error_message.starts_with( - "Module was compiled with incompatible Wasmtime version", - ) { - eprintln!("{}", error_message); - return; - } - let version = error_message.trim_start_matches("Module was compiled with incompatible Wasmtime version ").trim(); - println!( - "File {} was AOT compiled to '{}' with wasmtime version: {}", - file, target, version - ); - } - }; - } - Precompiled::Component => { - eprintln!("The file is an AOT compiled Wasmtime component") - } + file, target, version + ); + } + }; + } + w::Precompiled::Component => { + eprintln!("The file is an AOT compiled Wasmtime component") } } - None => { - eprintln!( - "Error - {} is not a valid AOT compiled Wasmtime module or component", - file - ); - } - }; - } - WasmtimeVersion::Lts => { - if debug { - println!( - "Checking Wasmtime version used to compile with debug info enabled file: {} (LTS wasmtime)", - file - ); - } else { - println!( - "Checking Wasmtime version used to compile file: {} (LTS wasmtime)", + } + None => { + eprintln!( + "Error - {} is not a valid AOT compiled Wasmtime module or component", file ); } - let bytes = std::fs::read(&file).unwrap(); - detect_and_deserialize_lts(&bytes, debug, &file); } - } + ); } } } /// Returns a new `Config` for the Wasmtime engine with additional settings for AOT compilation. -fn get_config(debug: bool, minimal: bool, target: &SupportedTarget) -> Config { - let mut config = Config::new(); +fn get_config(wv: WasmtimeVersion, debug: bool, minimal: bool, target: &SupportedTarget) -> Config { + let mut config = Config::new(wv); // Compile for the pulley64 target if specified match target { SupportedTarget::X86_64UnknownNone => { - config.target("x86_64-unknown-none").unwrap(); + wv!(config.target("x86_64-unknown-none").unwrap()); // Enable x86_float_abi_ok only for the latest Wasmtime version. // Safety: - // We are using hyperlight cargo to build the guest which - // sets the Rust target to be compiled with the hard-float ABI manually via - // `-Zbuild-std` and a custom target JSON configuration - // See https://github.com/bytecodealliance/wasmtime/pull/11553 - unsafe { config.x86_float_abi_ok(true) }; + // - If using the new upstream x86_64-unknown-hyperlight + // targets, they are hard float already + // - Otherwise, we use cargo-hyperlight to build the guest, which + // sets the Rust target to be compiled with the hard-float ABI manually via + // `-Zbuild-std` and a custom target JSON configuration + // See https://github.com/bytecodealliance/wasmtime/pull/11553 + unsafe { + wv!(latest, config.x86_float_abi_ok(true)); + } } SupportedTarget::AArch64UnknownNone => { - config.target("aarch64-unknown-none").unwrap(); + wv!(config.target("aarch64-unknown-none").unwrap()); } SupportedTarget::WasmtimePulley64 => { - config.target("pulley64").unwrap(); + wv!(config.target("pulley64").unwrap()); } } // Enable the default features for the Wasmtime engine. if debug { - config.debug_info(true); - config.cranelift_opt_level(OptLevel::None); + wv!(config.debug_info(true)); + wv!(config.cranelift_opt_level(w::OptLevel::None)); } if minimal { - config.generate_address_map(false); - config.native_unwind_info(false); + wv!(config.generate_address_map(false)); + wv!(config.native_unwind_info(false)); } config