A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wizer: Make `wasmtime` an optional dependency (#11863)
Alex Crichtonac090f67c9d8
diff --git a/.github/workflows/main.yml b/.github/workflows/main.ymlindex c650765e7c..e23ce342e8 100644--- a/.github/workflows/main.yml+++ b/.github/workflows/main.yml@@ -417,6 +417,12 @@ jobs:-p wasmtime-wasi --no-default-features --features p1-p wasmtime-wasi --no-default-features --features p2-p wasmtime-wasi --no-default-features --features p3++ - name: wasmtime-wizer+ checks: |+ -p wasmtime-wizer --no-default-features+ -p wasmtime-wizer --no-default-features --features wasmtime+ -p wasmtime-wizer --no-default-features --features wasmprinterruns-on: ubuntu-lateststeps:- uses: actions/checkout@v4diff --git a/Cargo.toml b/Cargo.tomlindex f489e16aca..e4e2c29333 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -59,7 +59,7 @@ wasmtime-wasi-keyvalue = { workspace = true, optional = true }wasmtime-wasi-threads = { workspace = true, optional = true }wasmtime-wasi-http = { workspace = true, optional = true }wasmtime-unwinder = { workspace = true }-wasmtime-wizer = { workspace = true, optional = true, features = ['clap'] }+wasmtime-wizer = { workspace = true, optional = true, features = ['clap', 'wasmtime'] }clap = { workspace = true }clap_complete = { workspace = true, optional = true }anyhow = { workspace = true, features = ['std'] }diff --git a/crates/wizer/Cargo.toml b/crates/wizer/Cargo.tomlindex b6fd3c7d08..ec5d9e8809 100644--- a/crates/wizer/Cargo.toml+++ b/crates/wizer/Cargo.toml@@ -30,9 +30,9 @@ log = { workspace = true }rayon = { workspace = true }clap = { workspace = true, optional = true }wasm-encoder = { workspace = true, features = ['wasmparser'] }-wasmparser = { workspace = true }+wasmparser = { workspace = true, features = ['validate', 'features'] }wasmprinter = { workspace = true, optional = true }-wasmtime = { workspace = true, features = ['std', 'runtime', 'cranelift', 'cache', 'threads', 'gc', 'gc-null'] }+wasmtime = { workspace = true, optional = true, features = ['std', 'runtime', 'cranelift', 'cache', 'threads', 'gc', 'gc-null'] }[dev-dependencies]criterion = { workspace = true }@@ -46,3 +46,10 @@ wasmtime-wasi = { workspace = true, features = ["p1"] }# Enable this dependency to get messages with WAT disassemblies when certain# internal panics occur.wasmprinter = ['dep:wasmprinter']++# Enables default runtime implementations based on the `wasmtime` crate.+wasmtime = ['dep:wasmtime']++[[test]]+name = "all"+required-features = ['wasmtime']diff --git a/crates/wizer/fuzz/Cargo.toml b/crates/wizer/fuzz/Cargo.tomlindex 79596fa4d7..23b9c6158b 100644--- a/crates/wizer/fuzz/Cargo.toml+++ b/crates/wizer/fuzz/Cargo.toml@@ -18,7 +18,7 @@ log = { workspace = true }wasm-smith = { workspace = true }wasmprinter = { workspace = true }wasmtime = { workspace = true }-wasmtime-wizer = { workspace = true }+wasmtime-wizer = { workspace = true, features = ["wasmtime"] }[[bin]]name = "same_result"diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rsindex 96224e8428..b978998634 100644--- a/crates/wizer/src/lib.rs+++ b/crates/wizer/src/lib.rs@@ -10,14 +10,15 @@ mod parse;mod rewrite;mod snapshot;-/// Re-export wasmtime so users can align with our version. This is-/// especially useful when providing a custom Linker.-pub use wasmtime;+#[cfg(feature = "wasmtime")]+mod wasmtime;+#[cfg(feature = "wasmtime")]+pub use wasmtime::*;pub use crate::info::ModuleContext;+pub use crate::snapshot::SnapshotVal;use anyhow::Context;use std::collections::{HashMap, HashSet};-use wasmtime::{Extern, Module, Result, Store};const DEFAULT_KEEP_INIT_FUNC: bool = false;@@ -92,7 +93,7 @@ pub struct Wizer {}#[cfg(feature = "clap")]-fn parse_rename(s: &str) -> Result<(String, String)> {+fn parse_rename(s: &str) -> anyhow::Result<(String, String)> {let parts: Vec<&str> = s.splitn(2, '=').collect();if parts.len() != 2 {anyhow::bail!("must contain exactly one equals character ('=')");@@ -174,26 +175,6 @@ impl Wizer {self}- /// Initialize the given Wasm, snapshot it, and return the serialized- /// snapshot as a new, pre-initialized Wasm module.- pub fn run<T>(- &self,- store: &mut Store<T>,- wasm: &[u8],- instantiate: impl FnOnce(&mut Store<T>, &Module) -> Result<wasmtime::Instance>,- ) -> anyhow::Result<Vec<u8>> {- let (cx, instrumented_wasm) = self.instrument(wasm)?;-- let engine = store.engine();- let module = wasmtime::Module::new(engine, &instrumented_wasm)- .context("failed to compile the Wasm module")?;- self.validate_init_func(&module)?;-- let instance = instantiate(store, &module)?;- self.initialize(store, &instance)?;- self.snapshot(cx, store, &instance)- }-/// First half of [`Self::run`] which instruments the provided `wasm` and/// produces a new wasm module which should be run by a runtime.///@@ -245,17 +226,16 @@ impl Wizer {////// This returns a new WebAssembly binary which has all state/// pre-initialized.- pub fn snapshot<T>(+ pub fn snapshot(&self,mut cx: ModuleContext<'_>,- store: &mut Store<T>,- instance: &wasmtime::Instance,+ instance: &mut dyn InstanceState,) -> anyhow::Result<Vec<u8>> {// Parse rename spec.let renames = FuncRenames::parse(&self.func_renames)?;- let snapshot = snapshot::snapshot(&cx, &mut *store, &instance);- let rewritten_wasm = self.rewrite(&mut cx, store, &snapshot, &renames);+ let snapshot = snapshot::snapshot(&cx, instance);+ let rewritten_wasm = self.rewrite(&mut cx, &snapshot, &renames);if cfg!(debug_assertions) {if let Err(error) = self.wasm_validate(&rewritten_wasm) {@@ -351,63 +331,6 @@ impl Wizer {Ok(())}- /// Check that the module exports an initialization function, and that the- /// function has the correct type.- fn validate_init_func(&self, module: &wasmtime::Module) -> anyhow::Result<()> {- log::debug!("Validating the exported initialization function");- match module.get_export(&self.init_func) {- Some(wasmtime::ExternType::Func(func_ty)) => {- if func_ty.params().len() != 0 || func_ty.results().len() != 0 {- anyhow::bail!(- "the Wasm module's `{}` function export does not have type `[] -> []`",- &self.init_func- );- }- }- Some(_) => anyhow::bail!(- "the Wasm module's `{}` export is not a function",- &self.init_func- ),- None => anyhow::bail!(- "the Wasm module does not have a `{}` export",- &self.init_func- ),- }- Ok(())- }-- /// Instantiate the module and call its initialization function.- fn initialize<T>(- &self,- store: &mut Store<T>,- instance: &wasmtime::Instance,- ) -> anyhow::Result<()> {- log::debug!("Calling the initialization function");-- if let Some(export) = instance.get_export(&mut *store, "_initialize") {- if let Extern::Func(func) = export {- func.typed::<(), ()>(&store)- .and_then(|f| f.call(&mut *store, ()))- .context("calling the Reactor initialization function")?;-- if self.init_func == "_initialize" {- // Don't run `_initialize` twice if the it was explicitly- // requested as the init function.- return Ok(());- }- }- }-- let init_func = instance- .get_typed_func::<(), ()>(&mut *store, &self.init_func)- .expect("checked by `validate_init_func`");- init_func- .call(&mut *store, ())- .with_context(|| format!("the `{}` function trapped", self.init_func))?;-- Ok(())- }-fn get_keep_init_func(&self) -> bool {match self.keep_init_func {Some(keep) => keep.unwrap_or(true),@@ -415,3 +338,23 @@ impl Wizer {}}}++/// Abstract ability to load state from a WebAssembly instance after it's been+/// instantiated and some exports have run.+pub trait InstanceState {+ /// Loads the global specified by `name`, returning a `SnapshotVal`.+ ///+ /// # Panics+ ///+ /// This function panics if `name` isn't an exported global or if the type+ /// of the global doesn't fit in `SnapshotVal`.+ fn global_get(&mut self, name: &str) -> SnapshotVal;++ /// Loads the contents of the memory specified by `name`, returning the+ /// entier contents as a `Vec<u8>`.+ ///+ /// # Panics+ ///+ /// This function panics if `name` isn't an exported memory.+ fn memory_contents(&mut self, name: &str) -> Vec<u8>;+}diff --git a/crates/wizer/src/rewrite.rs b/crates/wizer/src/rewrite.rsindex af2a4d2ff7..bf010ce1a5 100644--- a/crates/wizer/src/rewrite.rs+++ b/crates/wizer/src/rewrite.rs@@ -1,6 +1,6 @@//! Final rewrite pass.-use crate::{FuncRenames, Wizer, info::ModuleContext, snapshot::Snapshot};+use crate::{FuncRenames, SnapshotVal, Wizer, info::ModuleContext, snapshot::Snapshot};use std::convert::TryFrom;use wasm_encoder::reencode::{Reencode, RoundtripReencoder};use wasm_encoder::{ConstExpr, SectionId};@@ -9,10 +9,9 @@ impl Wizer {/// Given the initialized snapshot, rewrite the Wasm so that it is already/// initialized.///- pub(crate) fn rewrite<T>(+ pub(crate) fn rewrite(&self,module: &mut ModuleContext<'_>,- store: &wasmtime::Store<T>,snapshot: &Snapshot,renames: &FuncRenames,) -> Vec<u8> {@@ -31,7 +30,7 @@ impl Wizer {data_section.active(seg.memory_index,&ConstExpr::i32_const(seg.offset as i32),- seg.data(store).iter().copied(),+ seg.data().iter().copied(),);}Some(data_section)@@ -86,18 +85,15 @@ impl Wizer {globals.global(glob_ty,&match val {- wasmtime::Val::I32(x) => ConstExpr::i32_const(*x),- wasmtime::Val::I64(x) => ConstExpr::i64_const(*x),- wasmtime::Val::F32(x) => {+ SnapshotVal::I32(x) => ConstExpr::i32_const(*x),+ SnapshotVal::I64(x) => ConstExpr::i64_const(*x),+ SnapshotVal::F32(x) => {ConstExpr::f32_const(wasm_encoder::Ieee32::new(*x))}- wasmtime::Val::F64(x) => {+ SnapshotVal::F64(x) => {ConstExpr::f64_const(wasm_encoder::Ieee64::new(*x))}- wasmtime::Val::V128(x) => {- ConstExpr::v128_const(x.as_u128() as i128)- }- _ => unreachable!(),+ SnapshotVal::V128(x) => ConstExpr::v128_const(x.cast_signed()),},);}diff --git a/crates/wizer/src/snapshot.rs b/crates/wizer/src/snapshot.rsindex 1c081cbf13..4dc3ad7a3a 100644--- a/crates/wizer/src/snapshot.rs+++ b/crates/wizer/src/snapshot.rs@@ -1,9 +1,8 @@+use crate::InstanceState;use crate::info::ModuleContext;use rayon::iter::{IntoParallelIterator, ParallelExtend, ParallelIterator};use std::convert::TryFrom;-use wasmtime::{AsContext, AsContextMut};--const WASM_PAGE_SIZE: u64 = 65_536;+use std::sync::Arc;/// The maximum number of data segments that we will emit. Most/// engines support more than this, but we want to leave some@@ -13,7 +12,7 @@ const MAX_DATA_SEGMENTS: usize = 10_000;/// A "snapshot" of Wasm state from its default value after having been initialized.pub struct Snapshot {/// Maps global index to its initialized value.- pub globals: Vec<wasmtime::Val>,+ pub globals: Vec<SnapshotVal>,/// A new minimum size for each memory (in units of pages).pub memory_mins: Vec<u64>,@@ -22,14 +21,25 @@ pub struct Snapshot {pub data_segments: Vec<DataSegment>,}+/// A value from a snapshot, currently a subset of wasm types that aren't+/// reference types.+#[expect(missing_docs, reason = "self-describing variants")]+pub enum SnapshotVal {+ I32(i32),+ I64(i64),+ F32(u32),+ F64(u64),+ V128(u128),+}+/// A data segment initializer for a memory.-#[derive(Clone, Copy)]+#[derive(Clone)]pub struct DataSegment {/// The index of this data segment's memory.pub memory_index: u32,/// This data segment's initialized memory that it originated from.- pub memory: wasmtime::Memory,+ pub memory: Arc<Vec<u8>>,/// The offset within the memory that `data` should be copied to.pub offset: u32,@@ -39,10 +49,10 @@ pub struct DataSegment {}impl DataSegment {- pub fn data<'a>(&self, ctx: &'a impl AsContext) -> &'a [u8] {+ pub fn data(&self) -> &[u8] {let start = usize::try_from(self.offset).unwrap();let end = start + usize::try_from(self.len).unwrap();- &self.memory.data(ctx)[start..end]+ &self.memory[start..end]}}@@ -67,7 +77,7 @@ impl DataSegment {DataSegment {offset: self.offset,len: self.len + gap + other.len,- ..*self+ ..self.clone()}}}@@ -76,15 +86,11 @@ impl DataSegment {/// defaults.//// TODO: when we support reference types, we will have to snapshot tables.-pub fn snapshot(- module: &ModuleContext<'_>,- ctx: &mut impl AsContextMut,- instance: &wasmtime::Instance,-) -> Snapshot {+pub fn snapshot(module: &ModuleContext<'_>, ctx: &mut dyn InstanceState) -> Snapshot {log::debug!("Snapshotting the initialized state");- let globals = snapshot_globals(module, &mut *ctx, instance);- let (memory_mins, data_segments) = snapshot_memories(module, &mut *ctx, instance);+ let globals = snapshot_globals(module, ctx);+ let (memory_mins, data_segments) = snapshot_memories(module, ctx);Snapshot {globals,@@ -94,11 +100,7 @@ pub fn snapshot(}/// Get the initialized values of all globals.-fn snapshot_globals(- module: &ModuleContext<'_>,- ctx: &mut impl AsContextMut,- instance: &wasmtime::Instance,-) -> Vec<wasmtime::Val> {+fn snapshot_globals(module: &ModuleContext<'_>, ctx: &mut dyn InstanceState) -> Vec<SnapshotVal> {log::debug!("Snapshotting global values");module@@ -106,12 +108,7 @@ fn snapshot_globals(.as_ref().unwrap().iter()- .map(|name| {- let global = instance- .get_global(&mut *ctx, &name)- .expect("defined global missing");- global.get(&mut *ctx)- })+ .map(|name| ctx.global_get(&name)).collect()}@@ -119,8 +116,7 @@ fn snapshot_globals(/// regions of non-zero memory.fn snapshot_memories(module: &ModuleContext<'_>,- ctx: &mut impl AsContextMut,- instance: &wasmtime::Instance,+ instance: &mut dyn InstanceState,) -> (Vec<u64>, Vec<DataSegment>) {log::debug!("Snapshotting memories");@@ -130,19 +126,19 @@ fn snapshot_memories(let iter = module.defined_memories().zip(module.defined_memory_exports.as_ref().unwrap());- for ((memory_index, _), name) in iter {- let memory = instance.get_memory(&mut *ctx, &name).unwrap();- memory_mins.push(memory.size(&*ctx));-- let num_wasm_pages = memory.size(&*ctx);+ for ((memory_index, ty), name) in iter {+ let memory = Arc::new(instance.memory_contents(&name));+ let page_size = 1 << ty.page_size_log2.unwrap_or(16);+ let num_wasm_pages = memory.len() / page_size;+ memory_mins.push(num_wasm_pages as u64);- let memory_data = memory.data(&*ctx);+ let memory_data = &memory[..];// Consider each Wasm page in parallel. Create data segments for each// region of non-zero memory.data_segments.par_extend((0..num_wasm_pages).into_par_iter().flat_map(|i| {- let page_end = ((i + 1) * WASM_PAGE_SIZE) as usize;- let mut start = (i * WASM_PAGE_SIZE) as usize;+ let page_end = (i + 1) * page_size;+ let mut start = i * page_size;let mut segments = vec![];while start < page_end {let nonzero = match memory_data[start..page_end]@@ -159,7 +155,7 @@ fn snapshot_memories(.map_or(page_end, |zero| start + zero);segments.push(DataSegment {memory_index,- memory,+ memory: memory.clone(),offset: u32::try_from(start).unwrap(),len: u32::try_from(end - start).unwrap(),});@@ -186,13 +182,13 @@ fn snapshot_memories(// the data length LEB).const MIN_ACTIVE_SEGMENT_OVERHEAD: u32 = 4;let mut merged_data_segments = Vec::with_capacity(data_segments.len());- merged_data_segments.push(data_segments[0]);+ merged_data_segments.push(data_segments[0].clone());for b in &data_segments[1..] {let a = merged_data_segments.last_mut().unwrap();// Only merge segments for the same memory.if a.memory_index != b.memory_index {- merged_data_segments.push(*b);+ merged_data_segments.push(b.clone());continue;}@@ -200,7 +196,7 @@ fn snapshot_memories(// more size efficient than leaving them apart.let gap = a.gap(b);if gap > MIN_ACTIVE_SEGMENT_OVERHEAD {- merged_data_segments.push(*b);+ merged_data_segments.push(b.clone());continue;}diff --git a/crates/wizer/src/wasmtime.rs b/crates/wizer/src/wasmtime.rsindex e69de29bb2..8225d2190d 100644--- a/crates/wizer/src/wasmtime.rs+++ b/crates/wizer/src/wasmtime.rs@@ -0,0 +1,109 @@+use crate::{InstanceState, SnapshotVal, Wizer};+use anyhow::Context;+use wasmtime::{Extern, Instance, Module, Result, Store, Val};++impl Wizer {+ /// Initialize the given Wasm, snapshot it, and return the serialized+ /// snapshot as a new, pre-initialized Wasm module.+ pub fn run<T>(+ &self,+ store: &mut Store<T>,+ wasm: &[u8],+ instantiate: impl FnOnce(&mut Store<T>, &Module) -> Result<wasmtime::Instance>,+ ) -> anyhow::Result<Vec<u8>> {+ let (cx, instrumented_wasm) = self.instrument(wasm)?;++ let engine = store.engine();+ let module = wasmtime::Module::new(engine, &instrumented_wasm)+ .context("failed to compile the Wasm module")?;+ self.validate_init_func(&module)?;++ let instance = instantiate(store, &module)?;+ self.initialize(store, &instance)?;+ self.snapshot(cx, &mut WasmtimeWizer { store, instance })+ }++ /// Check that the module exports an initialization function, and that the+ /// function has the correct type.+ fn validate_init_func(&self, module: &wasmtime::Module) -> anyhow::Result<()> {+ log::debug!("Validating the exported initialization function");+ match module.get_export(&self.init_func) {+ Some(wasmtime::ExternType::Func(func_ty)) => {+ if func_ty.params().len() != 0 || func_ty.results().len() != 0 {+ anyhow::bail!(+ "the Wasm module's `{}` function export does not have type `[] -> []`",+ &self.init_func+ );+ }+ }+ Some(_) => anyhow::bail!(+ "the Wasm module's `{}` export is not a function",+ &self.init_func+ ),+ None => anyhow::bail!(+ "the Wasm module does not have a `{}` export",+ &self.init_func+ ),+ }+ Ok(())+ }++ /// Instantiate the module and call its initialization function.+ fn initialize<T>(+ &self,+ store: &mut Store<T>,+ instance: &wasmtime::Instance,+ ) -> anyhow::Result<()> {+ log::debug!("Calling the initialization function");++ if let Some(export) = instance.get_export(&mut *store, "_initialize") {+ if let Extern::Func(func) = export {+ func.typed::<(), ()>(&store)+ .and_then(|f| f.call(&mut *store, ()))+ .context("calling the Reactor initialization function")?;++ if self.init_func == "_initialize" {+ // Don't run `_initialize` twice if the it was explicitly+ // requested as the init function.+ return Ok(());+ }+ }+ }++ let init_func = instance+ .get_typed_func::<(), ()>(&mut *store, &self.init_func)+ .expect("checked by `validate_init_func`");+ init_func+ .call(&mut *store, ())+ .with_context(|| format!("the `{}` function trapped", self.init_func))?;++ Ok(())+ }+}++/// Impementation of [`InstanceState`] backed by Wasmtime.+pub struct WasmtimeWizer<'a, T: 'static> {+ /// The Wasmtime-based store that owns the `instance` field.+ pub store: &'a mut Store<T>,+ /// The instance that this will load state from.+ pub instance: Instance,+}++impl<T> InstanceState for WasmtimeWizer<'_, T> {+ fn global_get(&mut self, name: &str) -> SnapshotVal {+ let global = self.instance.get_global(&mut *self.store, name).unwrap();+ match global.get(&mut *self.store) {+ Val::I32(x) => SnapshotVal::I32(x),+ Val::I64(x) => SnapshotVal::I64(x),+ Val::F32(x) => SnapshotVal::F32(x),+ Val::F64(x) => SnapshotVal::F64(x),+ Val::V128(x) => SnapshotVal::V128(x.as_u128()),+ _ => panic!("unsupported global value type"),+ }+ }++ fn memory_contents(&mut self, name: &str) -> Vec<u8> {+ let memory = self.instance.get_memory(&mut *self.store, name).unwrap();+ memory.data(&self.store).to_vec()+ }+}diff --git a/src/commands/wizer.rs b/src/commands/wizer.rsindex 799886eb21..47ee877eec 100644--- a/src/commands/wizer.rs+++ b/src/commands/wizer.rs@@ -81,7 +81,13 @@ impl WizerCommand {// Use our state to capture a snapshot with Wizer and then serialize// that.- let final_wasm = self.wizer.snapshot(cx, &mut store, &instance)?;+ let final_wasm = self.wizer.snapshot(+ cx,+ &mut wasmtime_wizer::WasmtimeWizer {+ store: &mut store,+ instance,+ },+ )?;match &self.output {Some(file) => fs::write(file, &final_wasm).context("failed to write output file")?,