From babc4a973ee45412be9c85087b1e5e8b73b46a99 Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Fri, 17 Feb 2023 16:21:52 -0500 Subject: [PATCH] Add Engine::precompile_compatibility_key This method returns a key that can be used to index precompiled binaries from one Engine instance that can be deserialized by another Engine instance. --- crates/wasmtime/src/engine.rs | 12 +++++++++++ crates/wasmtime/src/engine/serialization.rs | 23 +++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rs index 9523278f5460..f2d351c7a3aa 100644 --- a/crates/wasmtime/src/engine.rs +++ b/crates/wasmtime/src/engine.rs @@ -239,6 +239,18 @@ impl Engine { Ok(mmap.to_vec()) } + /// Returns a blob that can be used to check precompiled WebAssembly compatibility. + /// + /// The outputs of [`Engine::precompile_module`] and [`Engine::precompile_component`] + /// are compatible with a different [`Engine`] instance only if the two engines use + /// compatible [`Config`]s. If this blob matches between two [`Engine`]s then binaries + /// from one are guaranteed to deserialize in the other. + #[cfg(compiler)] + #[cfg_attr(nightlydoc, doc(cfg(feature = "cranelift")))] // see build.rs + pub fn precompile_compatibility_key(&self) -> impl AsRef<[u8]> { + serialization::build_compiler_info(&self) + } + pub(crate) fn run_maybe_parallel< A: Send, B: Send, diff --git a/crates/wasmtime/src/engine/serialization.rs b/crates/wasmtime/src/engine/serialization.rs index ad15847bd068..e53747237eb6 100644 --- a/crates/wasmtime/src/engine/serialization.rs +++ b/crates/wasmtime/src/engine/serialization.rs @@ -37,16 +37,8 @@ const VERSION: u8 = 0; /// Produces a blob of bytes by serializing the `engine`'s configuration data to /// be checked, perhaps in a different process, with the `check_compatible` /// method below. -/// -/// The blob of bytes is inserted into the object file specified to become part -/// of the final compiled artifact. #[cfg(compiler)] -pub fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>) { - let section = obj.add_section( - obj.segment_name(StandardSegment::Data).to_vec(), - obj::ELF_WASM_ENGINE.as_bytes().to_vec(), - SectionKind::ReadOnlyData, - ); +pub fn build_compiler_info(engine: &Engine) -> Vec { let mut data = Vec::new(); data.push(VERSION); let version = match &engine.config().module_version { @@ -62,6 +54,19 @@ pub fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>) { data.push(version.len() as u8); data.extend_from_slice(version.as_bytes()); bincode::serialize_into(&mut data, &Metadata::new(engine)).unwrap(); + data +} + +/// Inserts the "compiler info" from build_compiler_info into the object file +/// specified to become part of the final compiled artifact. +#[cfg(compiler)] +pub fn append_compiler_info(engine: &Engine, obj: &mut Object<'_>) { + let section = obj.add_section( + obj.segment_name(StandardSegment::Data).to_vec(), + obj::ELF_WASM_ENGINE.as_bytes().to_vec(), + SectionKind::ReadOnlyData, + ); + let data = build_compiler_info(engine); obj.set_section_data(section, data, 1); }