Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Merge component-init into wasmtime-wizer (#11878)

Alex Crichton278656ce3be0
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 17d136b832..1d7132b1dd 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -383,6 +383,7 @@ jobs:
-p wasmtime-cli --no-default-features --features compile,cranelift
-p wasmtime-cli --no-default-features --features compile,cranelift,component-model
-p wasmtime-cli --no-default-features --features objdump
+ -p wasmtime-cli --no-default-features --features wizer
-p wasmtime-cli --all-features
-p wasmtime-cli --features component-model
@@ -423,6 +424,8 @@ jobs:
-p wasmtime-wizer --no-default-features
-p wasmtime-wizer --no-default-features --features wasmtime
-p wasmtime-wizer --no-default-features --features wasmprinter
+ -p wasmtime-wizer --no-default-features --features component-model
+ -p wasmtime-wizer --no-default-features --all-features
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
diff --git a/Cargo.lock b/Cargo.lock
index f8d9e69267..2898993ce0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5256,6 +5256,7 @@ dependencies = [
"env_logger 0.11.5",
"log",
"rayon",
+ "tokio",
"wasm-encoder 0.240.0",
"wasmparser 0.240.0",
"wasmprinter",
diff --git a/Cargo.toml b/Cargo.toml
index aa7cbc1d91..862472d40d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -527,7 +527,8 @@ pooling-allocator = ["wasmtime/pooling-allocator", "wasmtime-cli-flags/pooling-a
component-model = [
"wasmtime/component-model",
"wasmtime-wast?/component-model",
- "wasmtime-cli-flags/component-model"
+ "wasmtime-cli-flags/component-model",
+ "wasmtime-wizer?/component-model",
]
wat = ["dep:wat", "wasmtime/wat"]
cache = ["dep:wasmtime-cache", "wasmtime-cli-flags/cache"]
@@ -584,6 +585,7 @@ wizer = [
"dep:wasmtime-wasi",
"dep:wasi-common",
"dep:tokio",
+ "wasmtime/wave",
]
[[test]]
diff --git a/crates/wizer/Cargo.toml b/crates/wizer/Cargo.toml
index ec5d9e8809..80ae140e3d 100644
--- a/crates/wizer/Cargo.toml
+++ b/crates/wizer/Cargo.toml
@@ -32,7 +32,7 @@ clap = { workspace = true, optional = true }
wasm-encoder = { workspace = true, features = ['wasmparser'] }
wasmparser = { workspace = true, features = ['validate', 'features'] }
wasmprinter = { workspace = true, optional = true }
-wasmtime = { workspace = true, optional = true, features = ['std', 'runtime', 'cranelift', 'cache', 'threads', 'gc', 'gc-null'] }
+wasmtime = { workspace = true, optional = true, features = ['std', 'runtime', 'cranelift', 'cache', 'threads', 'gc', 'gc-null', 'async'] }
[dev-dependencies]
criterion = { workspace = true }
@@ -41,6 +41,7 @@ wasmprinter = { workspace = true }
wat = { workspace = true }
wasmtime = { workspace = true, features = ['default'] }
wasmtime-wasi = { workspace = true, features = ["p1"] }
+tokio = { workspace = true, features = ['macros'] }
[features]
# Enable this dependency to get messages with WAT disassemblies when certain
@@ -50,6 +51,9 @@ wasmprinter = ['dep:wasmprinter']
# Enables default runtime implementations based on the `wasmtime` crate.
wasmtime = ['dep:wasmtime']
+# Enable support for wizening components.
+component-model = ['wasmtime?/component-model']
+
[[test]]
name = "all"
required-features = ['wasmtime']
diff --git a/crates/wizer/README.md b/crates/wizer/README.md
index b41c152273..897178b1be 100644
--- a/crates/wizer/README.md
+++ b/crates/wizer/README.md
@@ -66,10 +66,10 @@ which `wasm-opt` can remove.
## Example Usage
First, make sure your Wasm module exports an initialization function named
-`wizer.initialize`. For example, in Rust you can export it like this:
+`wizer-initialize`. For example, in Rust you can export it like this:
```rust
-#[unsafe(export_name = "wizer.initialize")]
+#[unsafe(export_name = "wizer-initialize")]
pub extern "C" fn init() {
// Your initialization code goes here...
}
diff --git a/crates/wizer/benches/regex-bench/src/lib.rs b/crates/wizer/benches/regex-bench/src/lib.rs
index 54e2d16797..de6d34caf4 100644
--- a/crates/wizer/benches/regex-bench/src/lib.rs
+++ b/crates/wizer/benches/regex-bench/src/lib.rs
@@ -4,7 +4,7 @@ use std::sync::LazyLock;
/// A regex that matches numbers that start with "1".
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^1\d*$").unwrap());
-#[unsafe(export_name = "wizer.initialize")]
+#[unsafe(export_name = "wizer-initialize")]
pub extern "C" fn init() {
LazyLock::force(&REGEX);
}
diff --git a/crates/wizer/benches/regex.rs b/crates/wizer/benches/regex.rs
index 8ff7a0d84e..febfefb828 100644
--- a/crates/wizer/benches/regex.rs
+++ b/crates/wizer/benches/regex.rs
@@ -1,7 +1,9 @@
use criterion::{Criterion, criterion_group, criterion_main};
use std::cell::LazyCell;
use std::convert::TryFrom;
+use std::pin::pin;
use std::process::Command;
+use std::task::{Context, Poll, Waker};
use wasmtime_wasi::p1::WasiP1Ctx;
fn run_iter(linker: &wasmtime::Linker<WasiP1Ctx>, module: &wasmtime::Module) {
@@ -52,16 +54,25 @@ fn bench_regex(c: &mut Criterion) {
let wasi = wasmtime_wasi::WasiCtxBuilder::new().build_p1();
let mut store = wasmtime::Store::new(linker.engine(), wasi);
- let wizened = wasmtime_wizer::Wizer::new()
- .run(&mut store, &control, |store, module| {
- linker.instantiate(store, module)
- })
- .unwrap();
+ let wizened = assert_ready(wasmtime_wizer::Wizer::new().run(
+ &mut store,
+ &control,
+ async |store, module| linker.instantiate(store, module),
+ ))
+ .unwrap();
let module = wasmtime::Module::new(&engine, &wizened).unwrap();
b.iter(|| run_iter(&linker, &module));
});
group.finish();
}
+fn assert_ready<F: Future>(f: F) -> F::Output {
+ let mut context = Context::from_waker(Waker::noop());
+ match pin!(f).poll(&mut context) {
+ Poll::Ready(ret) => ret,
+ Poll::Pending => panic!("future wasn't ready"),
+ }
+}
+
criterion_group!(benches, bench_regex);
criterion_main!(benches);
diff --git a/crates/wizer/benches/uap-bench/src/lib.rs b/crates/wizer/benches/uap-bench/src/lib.rs
index 03729f9ab4..45259a22bb 100644
--- a/crates/wizer/benches/uap-bench/src/lib.rs
+++ b/crates/wizer/benches/uap-bench/src/lib.rs
@@ -33,7 +33,7 @@ struct UserAgentParserEntry {
// os_v3_replacement: Option<String>,
}
-#[unsafe(export_name = "wizer.initialize")]
+#[unsafe(export_name = "wizer-initialize")]
pub extern "C" fn init() {
LazyLock::force(&UA_REGEX_SET);
}
diff --git a/crates/wizer/benches/uap.rs b/crates/wizer/benches/uap.rs
index 991a9bc2d7..9e33e43b50 100644
--- a/crates/wizer/benches/uap.rs
+++ b/crates/wizer/benches/uap.rs
@@ -1,7 +1,9 @@
use criterion::{Criterion, criterion_group, criterion_main};
use std::cell::LazyCell;
use std::convert::TryFrom;
+use std::pin::pin;
use std::process::Command;
+use std::task::{Context, Poll, Waker};
use wasmtime_wasi::p1::WasiP1Ctx;
fn run_iter(linker: &wasmtime::Linker<WasiP1Ctx>, module: &wasmtime::Module) {
@@ -65,11 +67,12 @@ fn bench_uap(c: &mut Criterion) {
let wizer = LazyCell::new(|| {
let wasi = wasmtime_wasi::WasiCtxBuilder::new().build_p1();
let mut store = wasmtime::Store::new(linker.engine(), wasi);
- let wizened = wasmtime_wizer::Wizer::new()
- .run(&mut store, &control_wasm, |store, module| {
- linker.instantiate(store, module)
- })
- .unwrap();
+ let wizened = assert_ready(wasmtime_wizer::Wizer::new().run(
+ &mut store,
+ &control_wasm,
+ async |store, module| linker.instantiate(store, module),
+ ))
+ .unwrap();
wasmtime::Module::new(&engine, &wizened).unwrap()
});
group.bench_function("wizer", |b| {
@@ -79,5 +82,13 @@ fn bench_uap(c: &mut Criterion) {
group.finish();
}
+fn assert_ready<F: Future>(f: F) -> F::Output {
+ let mut context = Context::from_waker(Waker::noop());
+ match pin!(f).poll(&mut context) {
+ Poll::Ready(ret) => ret,
+ Poll::Pending => panic!("future wasn't ready"),
+ }
+}
+
criterion_group!(benches, bench_uap);
criterion_main!(benches);
diff --git a/crates/wizer/fuzz/fuzz_targets/same_result.rs b/crates/wizer/fuzz/fuzz_targets/same_result.rs
index a6c74bd4c7..97a7994705 100644
--- a/crates/wizer/fuzz/fuzz_targets/same_result.rs
+++ b/crates/wizer/fuzz/fuzz_targets/same_result.rs
@@ -19,6 +19,8 @@ use libfuzzer_sys::{
arbitrary::{Arbitrary, Unstructured},
fuzz_target,
};
+use std::pin::pin;
+use std::task::{Context, Poll, Waker};
use wasm_smith::MemoryOffsetChoices;
use wasmtime::*;
@@ -102,9 +104,9 @@ fuzz_target!(|data: &[u8]| {
let mut store = Store::new(&engine, ());
wizer.init_func(init_func);
- match wizer.run(&mut store, &wasm, |store, module| {
+ match assert_ready(wizer.run(&mut store, &wasm, async |store, module| {
Instance::new(store, module, &[])
- }) {
+ })) {
Err(_) => continue 'init_loop,
Ok(s) => s,
}
@@ -207,6 +209,14 @@ fuzz_target!(|data: &[u8]| {
}
});
+fn assert_ready<F: Future>(f: F) -> F::Output {
+ let mut context = Context::from_waker(Waker::noop());
+ match pin!(f).poll(&mut context) {
+ Poll::Ready(ret) => ret,
+ Poll::Pending => panic!("future wasn't ready"),
+ }
+}
+
fn assert_val_eq(a: &Val, b: &Val) {
match (a, b) {
(Val::I32(a), Val::I32(b)) => assert_eq!(a, b),
diff --git a/crates/wizer/include/wizer.h b/crates/wizer/include/wizer.h
index b138a5e27b..e74139284d 100644
--- a/crates/wizer/include/wizer.h
+++ b/crates/wizer/include/wizer.h
@@ -87,12 +87,12 @@
__WIZER_EXTERN_C void __wasm_call_dtors(); \
__WIZER_EXTERN_C void __wasi_proc_exit(int); \
__WIZER_EXTERN_C int WIZER_MAIN_VOID(); \
- /* This function's export name `wizer.initialize` is specially */ \
+ /* This function's export name `wizer-initialize` is specially */ \
/* recognized by Wizer. It is the direct entry point for pre-init. */ \
- __attribute__((export_name("wizer.initialize"))) void __wizer_initialize() { \
+ __attribute__((export_name("wizer-initialize"))) void __wizer_initialize() { \
/* `__wasm_call_ctors()` is generated by `wasm-ld` and invokes all */ \
/* of the global constructors. It is safe (and in fact necessary) */ \
- /* to manually invoke it here because `wizer.initialize` is the */ \
+ /* to manually invoke it here because `wizer-initialize` is the */ \
/* direct entry point, and no libc startup (crt1.o or equivalent) */ \
/* is executed before this code does. */ \
__wasm_call_ctors(); \
diff --git a/crates/wizer/src/component.rs b/crates/wizer/src/component.rs
new file mode 100644
index 0000000000..f87a4245bf
--- /dev/null
+++ b/crates/wizer/src/component.rs
@@ -0,0 +1,78 @@
+use crate::Wizer;
+use anyhow::bail;
+
+mod info;
+mod instrument;
+mod parse;
+mod rewrite;
+mod snapshot;
+#[cfg(feature = "wasmtime")]
+mod wasmtime;
+#[cfg(feature = "wasmtime")]
+pub use wasmtime::*;
+
+const WIZER_INSTANCE: &str = "wasmtime:wizer/access";
+
+pub use self::info::ComponentContext;
+
+impl Wizer {
+ /// Same as [`Wizer::instrument`], except for components.
+ pub fn instrument_component<'a>(
+ &self,
+ wasm: &'a [u8],
+ ) -> anyhow::Result<(ComponentContext<'a>, Vec<u8>)> {
+ // Make sure we're given valid Wasm from the get go.
+ self.wasm_validate(&wasm)?;
+
+ let mut cx = parse::parse(wasm)?;
+ let instrumented_wasm = instrument::instrument(&mut cx)?;
+ self.debug_assert_valid_wasm(&instrumented_wasm);
+
+ Ok((cx, instrumented_wasm))
+ }
+
+ /// Same as [`Wizer::snapshot`], except for components.
+ pub async fn snapshot_component(
+ &self,
+ mut cx: ComponentContext<'_>,
+ instance: &mut impl ComponentInstanceState,
+ ) -> anyhow::Result<Vec<u8>> {
+ if !self.func_renames.is_empty() {
+ bail!("components do not support renaming functions");
+ }
+
+ let snapshot = snapshot::snapshot(&cx, instance).await;
+ let rewritten_wasm = self.rewrite_component(&mut cx, &snapshot);
+ self.debug_assert_valid_wasm(&rewritten_wasm);
+
+ Ok(rewritten_wasm)
+ }
+}
+
+/// Trait representing the ability to invoke functions on a component to learn
+/// about its internal state.
+pub trait ComponentInstanceState: Send {
+ /// Looks up the exported `instance` which has `func` as an export, calls
+ /// it, and returns the `list<u8>` return type.
+ fn call_func_ret_list_u8(
+ &mut self,
+ instance: &str,
+ func: &str,
+ ) -> impl Future<Output = Vec<u8>> + Send;
+
+ /// Same as [`Self::call_func_ret_list_u8`], but for the `s32` WIT type.
+ fn call_func_ret_s32(&mut self, instance: &str, func: &str)
+ -> impl Future<Output = i32> + Send;
+
+ /// Same as [`Self::call_func_ret_list_u8`], but for the `s64` WIT type.
+ fn call_func_ret_s64(&mut self, instance: &str, func: &str)
+ -> impl Future<Output = i64> + Send;
+
+ /// Same as [`Self::call_func_ret_list_u8`], but for the `f32` WIT type.
+ fn call_func_ret_f32(&mut self, instance: &str, func: &str)
+ -> impl Future<Output = u32> + Send;
+
+ /// Same as [`Self::call_func_ret_list_u8`], but for the `f64` WIT type.
+ fn call_func_ret_f64(&mut self, instance: &str, func: &str)
+ -> impl Future<Output = u64> + Send;
+}
diff --git a/crates/wizer/src/component/info.rs b/crates/wizer/src/component/info.rs
new file mode 100644
index 0000000000..1c82e10359
--- /dev/null
+++ b/crates/wizer/src/component/info.rs
@@ -0,0 +1,156 @@
+use crate::ModuleContext;
+use std::collections::HashMap;
+
+/// Wizer-specific contextual information about a component, returned from
+/// [`Wizer::instrument_component`].
+///
+/// [`Wizer::instrument_component`]: crate::Wizer::instrument_component
+#[derive(Default)]
+pub struct ComponentContext<'a> {
+ /// Sections of the component, which are either raw bytes or a parsed module
+ /// using `ModuleContext`.
+ pub(crate) sections: Vec<RawSection<'a>>,
+
+ /// Counts of each index space for what this component contains.
+ ///
+ /// Note that these aren't all index spaces in the component, only those
+ /// needed at this time.
+ pub(crate) instances: u32,
+ pub(crate) funcs: u32,
+ pub(crate) types: u32,
+ pub(crate) core_instances: u32,
+ pub(crate) core_memories: u32,
+ pub(crate) core_funcs: u32,
+
+ /// Map of which module index to the core instance index it's instantiated
+ /// as.
+ pub(crate) core_instantiations: HashMap<u32, u32>,
+
+ /// Instrumentation injected to access internal state of globals/memories.
+ pub(crate) accessors: Option<Vec<Accessor>>,
+}
+
+/// Generated accessors during instrumentation and the metadata about them.
+pub(crate) enum Accessor {
+ /// This accessor retrieves the value of a wasm global.
+ Global {
+ /// The module index, within the parent component, that this global
+ /// belongs to.
+ module_index: u32,
+
+ /// The wizer-instrumented name of the global export this is accessing.
+ core_export_name: String,
+
+ /// The component level export name to access this global.
+ accessor_export_name: String,
+
+ /// The content type of this global.
+ ty: wasmparser::ValType,
+ },
+
+ /// This accessor retrieves the value of a wasm linear memory as a
+ /// `list<u8>` in WIT.
+ Memory {
+ /// The module index, within the parent component, that this memory
+ /// belongs to.
+ module_index: u32,
+
+ /// The wizer-instrumented name of the memory export this is accessing.
+ core_export_name: String,
+
+ /// The component level export name to access this memory.
+ accessor_export_name: String,
+ },
+}
+
+/// A section of a component, learned during parsing.
+pub(crate) enum RawSection<'a> {
+ /// A non-module section, whose raw contents are stored here.
+ Raw(wasm_encoder::RawSection<'a>),
+
+ /// A module section, parsed as with Wizer's metadata.
+ Module(ModuleContext<'a>),
+}
+
+impl<'a> ComponentContext<'a> {
+ pub(crate) fn push_raw_section(&mut self, section: wasm_encoder::RawSection<'a>) {
+ self.sections.push(RawSection::Raw(section));
+ }
+
+ pub(crate) fn push_module_section(&mut self, module: ModuleContext<'a>) {
+ self.sections.push(RawSection::Module(module));
+ }
+
+ pub(crate) fn core_modules(&self) -> impl Iterator<Item = (u32, &ModuleContext<'a>)> + '_ {
+ let mut i = 0;
+ self.sections.iter().filter_map(move |s| match s {
+ RawSection::Module(m) => Some((inc(&mut i), m)),
+ RawSection::Raw(_) => None,
+ })
+ }
+
+ pub(crate) fn num_core_modules(&self) -> u32 {
+ u32::try_from(self.core_modules().count()).unwrap()
+ }
+
+ pub(crate) fn inc(&mut self, kind: wasmparser::ComponentExternalKind) {
+ match kind {
+ wasmparser::ComponentExternalKind::Type => {
+ self.inc_types();
+ }
+ wasmparser::ComponentExternalKind::Instance => {
+ self.inc_instances();
+ }
+ wasmparser::ComponentExternalKind::Func => {
+ self.inc_funcs();
+ }
+ wasmparser::ComponentExternalKind::Component
+ | wasmparser::ComponentExternalKind::Module
+ | wasmparser::ComponentExternalKind::Value => {}
+ }
+ }
+
+ pub(crate) fn inc_core(&mut self, kind: wasmparser::ExternalKind) {
+ match kind {
+ wasmparser::ExternalKind::Func => {
+ self.inc_core_funcs();
+ }
+ wasmparser::ExternalKind::Memory => {
+ self.inc_core_memories();
+ }
+ wasmparser::ExternalKind::Table
+ | wasmparser::ExternalKind::Global
+ | wasmparser::ExternalKind::Tag => {}
+ }
+ }
+
+ pub(crate) fn inc_instances(&mut self) -> u32 {
+ inc(&mut self.instances)
+ }
+
+ pub(crate) fn inc_funcs(&mut self) -> u32 {
+ inc(&mut self.funcs)
+ }
+
+ pub(crate) fn inc_core_memories(&mut self) -> u32 {
+ inc(&mut self.core_memories)
+ }
+
+ pub(crate) fn inc_types(&mut self) -> u32 {
+ inc(&mut self.types)
+ }
+
+ pub(crate) fn inc_core_instances(&mut self) -> u32 {
+ inc(&mut self.core_instances)
+ }
+
+ pub(crate) fn inc_core_funcs(&mut self) -> u32 {
+ inc(&mut self.core_funcs)
+ }
+}
+
+fn inc(count: &mut u32) -> u32 {
+ let current = *count;
+ *count += 1;
+ current
+}
diff --git a/crates/wizer/src/component/instrument.rs b/crates/wizer/src/component/instrument.rs
new file mode 100644
index 0000000000..789a9e4cbf
--- /dev/null
+++ b/crates/wizer/src/component/instrument.rs
@@ -0,0 +1,375 @@
+use crate::ModuleContext;
+use crate::component::info::{Accessor, RawSection};
+use crate::component::{ComponentContext, WIZER_INSTANCE};
+use anyhow::{Result, bail};
+use wasm_encoder::reencode::{Reencode, RoundtripReencoder};
+
+/// Instrumentation phase of wizening a component.
+///
+/// This is similar to the core wasm wizening instrumentation but operates at
+/// the component level. This notably handles multiple instances and multiple
+/// globals/memories across these instances. The general idea of the
+/// instrumented component is:
+///
+/// * All core modules are instrumented with typical wizer instrumentation (e.g.
+/// `__wizer_*` exports of all internal state).
+/// * A core module is then generated which accesses all of these exports in the
+/// form of functions.
+/// * This core module is instantiated and lifted in a component instance which
+/// contains exported functions for accessing all of the various pieces of
+/// state of the module.
+/// * The lifted instance is exported under the `WIZER_INSTANCE` name.
+///
+/// The main goal is to reuse the core wasm instrumentation as much as possible
+/// here, and then the only remaining question is how to plumb all the core wasm
+/// state out of the component through WIT.
+pub(crate) fn instrument(component: &mut ComponentContext<'_>) -> Result<Vec<u8>> {
+ let mut encoder = wasm_encoder::Component::new();
+
+ // First pass through all sections as-is, ensuring that module sections are
+ // the instrumented version of the module.
+ for section in component.sections.iter_mut() {
+ match section {
+ RawSection::Raw(raw) => {
+ encoder.section(raw);
+ }
+ RawSection::Module(module) => {
+ let wasm = crate::instrument::instrument(module);
+ encoder.section(&wasm_encoder::RawSection {
+ id: wasm_encoder::ComponentSectionId::CoreModule as u8,
+ data: &wasm,
+ });
+ }
+ }
+ }
+
+ // Build the accessor module and append it with this helper.
+ let mut builder = AccessorBuilder {
+ component,
+ accessor_types: Default::default(),
+ accessor_imports: Default::default(),
+ accessor_functions: Default::default(),
+ accessor_code: Default::default(),
+ accessor_exports: Default::default(),
+ accessor_nglobals: 0,
+ accessor_nmemories: 0,
+ instances_to_instantiate_with: Vec::new(),
+ accessors: Vec::new(),
+ extra_types: Default::default(),
+ extra_aliases: Default::default(),
+ extra_canonicals: Default::default(),
+ accessor_instance_export_items: Vec::new(),
+ extra_core_funcs: 0,
+ };
+ builder.build(&mut encoder)?;
+ component.accessors = Some(builder.accessors);
+
+ Ok(encoder.finish())
+}
+
+struct AccessorBuilder<'a> {
+ component: &'a ComponentContext<'a>,
+
+ // Sections that are used to create the "accessor" module which is a bunch
+ // of functions that reads the internal state of all other instances in this
+ // component.
+ accessor_types: wasm_encoder::TypeSection,
+ accessor_imports: wasm_encoder::ImportSection,
+ accessor_functions: wasm_encoder::FunctionSection,
+ accessor_code: wasm_encoder::CodeSection,
+ accessor_exports: wasm_encoder::ExportSection,
+ accessor_nglobals: u32,
+ accessor_nmemories: u32,
+
+ // Arguments to the instantiation of the accessor module.
+ instances_to_instantiate_with: Vec<(u32, String)>,
+
+ // All accessor functions generated for all instance internal state.
+ accessors: Vec<Accessor>,
+
+ // Sections that are appended to the component as part of the
+ // instrumentation. This is the implementation detail of lifting all the
+ // functions in the "accessor" module.
+ extra_types: wasm_encoder::ComponentTypeSection,
+ extra_aliases: wasm_encoder::ComponentAliasSection,
+ extra_core_funcs: u32,
+ extra_canonicals: wasm_encoder::CanonicalFunctionSection,
+ accessor_instance_export_items: Vec<(String, wasm_encoder::ComponentExportKind, u32)>,
+}
+
+impl AccessorBuilder<'_> {
+ fn build(&mut self, encoder: &mut wasm_encoder::Component) -> Result<()> {
+ for (module_index, module) in self.component.core_modules() {
+ let instance_index = match self.component.core_instantiations.get(&module_index) {
+ Some(i) => *i,
+ None => continue,
+ };
+ self.add_core_instance(module_index, module, instance_index)?;
+ }
+
+ self.finish(encoder);
+ Ok(())
+ }
+
+ fn add_core_instance(
+ &mut self,
+ module_index: u32,
+ module: &ModuleContext<'_>,
+ instance_index: u32,
+ ) -> Result<()> {
+ let instance_import_name = instance_index.to_string();
+
+ for (_, ty, name) in module.defined_globals() {
+ let name = match name {
+ Some(n) => n,
+ None => continue,
+ };
+
+ let accessor_export_name =
+ self.add_core_instance_global(&instance_import_name, name, ty)?;
+ self.accessors.push(Accessor::Global {
+ module_index,
+ accessor_export_name,
+ ty: ty.content_type,
+ core_export_name: name.to_string(),
+ });
+ self.accessor_nglobals += 1;
+ }
+
+ let defined_memory_exports = module.defined_memory_exports.as_ref().unwrap();
+ for ((_, ty), name) in module.defined_memories().zip(defined_memory_exports) {
+ let accessor_export_name =
+ self.add_core_instance_memory(instance_index, &instance_import_name, name, ty);
+ self.accessors.push(Accessor::Memory {
+ module_index,
+ accessor_export_name,
+ core_export_name: name.to_string(),
+ });
+ self.accessor_nmemories += 1;
+ }
+
+ self.instances_to_instantiate_with
+ .push((instance_index, instance_import_name));
+
+ Ok(())
+ }
+
+ fn add_core_instance_global(
+ &mut self,
+ instance_import_name: &str,
+ global_export_name: &str,
+ global_ty: wasmparser::GlobalType,
+ ) -> Result<String> {
+ // Import the global and then define a function which returns the
+ // current value of the global.
+ self.accessor_imports.import(
+ &instance_import_name,
+ global_export_name,
+ RoundtripReencoder.global_type(global_ty).unwrap(),
+ );
+ let type_index = self.accessor_types.len();
+ self.accessor_types.ty().function(
+ [],
+ [RoundtripReencoder.val_type(global_ty.content_type).unwrap()],
+ );
+ self.accessor_functions.function(type_index);
+
+ // Accessing a global is pretty easy, it's just `global.get`
+ let mut function = wasm_encoder::Function::new([]);
+ let mut ins = function.instructions();
+ ins.global_get(self.accessor_nglobals);
+ ins.end();
+ self.accessor_code.function(&function);
+
+ let prim = match global_ty.content_type {
+ wasmparser::ValType::I32 => wasm_encoder::PrimitiveValType::S32,
+ wasmparser::ValType::I64 => wasm_encoder::PrimitiveValType::S64,
+ wasmparser::ValType::F32 => wasm_encoder::PrimitiveValType::F32,
+ wasmparser::ValType::F64 => wasm_encoder::PrimitiveValType::F64,
+ wasmparser::ValType::V128 => bail!("component wizening does not support v128 globals"),
+ wasmparser::ValType::Ref(_) => unreachable!(),
+ };
+ let lift_type_index = self.component.types + self.extra_types.len();
+ self.extra_types
+ .function()
+ .params::<_, wasm_encoder::ComponentValType>([])
+ .result(Some(wasm_encoder::ComponentValType::Primitive(prim)));
+ Ok(self.lift_accessor("global", lift_type_index, &[]))
+ }
+
+ fn add_core_instance_memory(
+ &mut self,
+ instance_index: u32,
+ instance_import_name: &str,
+ memory_export_name: &str,
+ memory_ty: wasmparser::MemoryType,
+ ) -> String {
+ self.accessor_imports.import(
+ &instance_import_name,
+ memory_export_name,
+ RoundtripReencoder.memory_type(memory_ty).unwrap(),
+ );
+ let type_index = self.accessor_types.len();
+ self.accessor_types
+ .ty()
+ .function([], [wasm_encoder::ValType::I32]);
+ self.accessor_functions.function(type_index);
+
+ // Accessing a linear memory is more subtle than a global. We're
+ // returning a `list<u8>` in WIT but to do so we have to store the
+ // ptr/length in memory itself. To work around this the memory is grown
+ // by a single page to ensure we don't tamper with the original image,
+ // and then in this new page the ptr/len are stored. The base pointer is
+ // always 0 and the length is the size of memory prior to the growth.
+ let mut function = wasm_encoder::Function::new([(1, wasm_encoder::ValType::I32)]);
+ let mut ins = function.instructions();
+
+ // Grow memory by 1 page, and trap if the growth failed.
+ let pages_to_grow_by = match memory_ty.page_size_log2 {
+ Some(0) => 8,
+ Some(16) | None => 1,
+ _ => unreachable!(),
+ };
+ ins.i32_const(pages_to_grow_by);
+ ins.memory_grow(self.accessor_nmemories);
+ ins.local_tee(0);
+ ins.i32_const(-1);
+ ins.i32_eq();
+ ins.if_(wasm_encoder::BlockType::Empty);
+ ins.unreachable();
+ ins.end();
+
+ // Update our one local as the full byte length of memory.
+ ins.local_get(0);
+ ins.i32_const(memory_ty.page_size_log2.unwrap_or(16).cast_signed());
+ ins.i32_shl();
+ ins.local_set(0);
+
+ let memarg = |offset| wasm_encoder::MemArg {
+ align: 2,
+ offset,
+ memory_index: self.accessor_nmemories,
+ };
+ // Store the ptr/len into the page that was just allocated
+ ins.local_get(0);
+ ins.i32_const(0);
+ ins.i32_store(memarg(0));
+ ins.local_get(0);
+ ins.local_get(0);
+ ins.i32_store(memarg(4));
+
+ // and return the local as it's the pointer to the ptr/len combo
+ ins.local_get(0);
+
+ ins.end();
+ self.accessor_code.function(&function);
+
+ let list_ty = self.component.types + self.extra_types.len();
+ self.extra_types
+ .defined_type()
+ .list(wasm_encoder::ComponentValType::Primitive(
+ wasm_encoder::PrimitiveValType::U8,
+ ));
+ let lift_type_index = self.component.types + self.extra_types.len();
+ self.extra_types
+ .function()
+ .params::<_, wasm_encoder::ComponentValType>([])
+ .result(Some(wasm_encoder::ComponentValType::Type(list_ty)));
+ self.extra_aliases
+ .alias(wasm_encoder::Alias::CoreInstanceExport {
+ instance: instance_index,
+ kind: wasm_encoder::ExportKind::Memory,
+ name: memory_export_name,
+ });
+ self.lift_accessor(
+ "memory",
+ lift_type_index,
+ &[wasm_encoder::CanonicalOption::Memory(
+ self.component.core_memories + self.accessor_nmemories,
+ )],
+ )
+ }
+
+ fn lift_accessor(
+ &mut self,
+ item: &str,
+ lift_type_index: u32,
+ opts: &[wasm_encoder::CanonicalOption],
+ ) -> String {
+ let accessor_core_export_name = self.accessors.len().to_string();
+ self.accessor_exports.export(
+ &accessor_core_export_name,
+ wasm_encoder::ExportKind::Func,
+ self.accessor_functions.len() - 1,
+ );
+
+ self.extra_aliases
+ .alias(wasm_encoder::Alias::CoreInstanceExport {
+ instance: self.accessor_instance_index(),
+ kind: wasm_encoder::ExportKind::Func,
+ name: &accessor_core_export_name,
+ });
+ self.extra_core_funcs += 1;
+ self.extra_canonicals.lift(
+ self.component.core_funcs + self.extra_core_funcs - 1,
+ lift_type_index,
+ opts.iter().copied(),
+ );
+
+ let accessor_export_name = format!("{item}{}", self.accessors.len());
+ self.accessor_instance_export_items.push((
+ accessor_export_name.clone(),
+ wasm_encoder::ComponentExportKind::Func,
+ self.component.funcs + self.extra_canonicals.len() - 1,
+ ));
+ accessor_export_name
+ }
+
+ fn finish(&mut self, encoder: &mut wasm_encoder::Component) {
+ // Build the `accessor_module` and add it to the component.
+ let mut accessor_module = wasm_encoder::Module::new();
+ accessor_module.section(&self.accessor_types);
+ accessor_module.section(&self.accessor_imports);
+ accessor_module.section(&self.accessor_functions);
+ accessor_module.section(&self.accessor_exports);
+ accessor_module.section(&self.accessor_code);
+ encoder.section(&wasm_encoder::ModuleSection(&accessor_module));
+
+ // Instantiate the `accessor_module` with prior instantiations.
+ let mut extra_instances = wasm_encoder::InstanceSection::new();
+ extra_instances.instantiate(
+ self.component.num_core_modules(),
+ self.instances_to_instantiate_with
+ .iter()
+ .map(|(i, name)| (name.as_str(), wasm_encoder::ModuleArg::Instance(*i))),
+ );
+ encoder.section(&extra_instances);
+
+ // Add instrumentation to the component which extracts names from the
+ // accessor instance, lifts things into the component model, and then
+ // export them.
+ encoder.section(&self.extra_aliases);
+ encoder.section(&self.extra_types);
+ encoder.section(&self.extra_canonicals);
+ let mut extra_component_instances = wasm_encoder::ComponentInstanceSection::new();
+ extra_component_instances.export_items(
+ self.accessor_instance_export_items
+ .iter()
+ .map(|(a, b, c)| (a.as_str(), *b, *c)),
+ );
+ encoder.section(&extra_component_instances);
+
+ let mut extra_exports = wasm_encoder::ComponentExportSection::new();
+ extra_exports.export(
+ WIZER_INSTANCE,
+ wasm_encoder::ComponentExportKind::Instance,
+ self.component.instances,
+ None,
+ );
+ encoder.section(&extra_exports);
+ }
+
+ fn accessor_instance_index(&self) -> u32 {
+ self.component.core_instances
+ }
+}
diff --git a/crates/wizer/src/component/parse.rs b/crates/wizer/src/component/parse.rs
new file mode 100644
index 0000000000..e3892a86fc
--- /dev/null
+++ b/crates/wizer/src/component/parse.rs
@@ -0,0 +1,182 @@
+use crate::component::ComponentContext;
+use anyhow::{Result, bail};
+use std::collections::hash_map::Entry;
+use wasmparser::{
+ CanonicalFunction, ComponentAlias, ComponentExternalKind, ComponentOuterAliasKind, Encoding,
+ Instance, Parser, Payload,
+};
+
+/// Parse the given Wasm bytes into a `ComponentContext` tree.
+///
+/// This will parse the input component and build up metadata that Wizer needs
+/// to know about the component. At this time there are limitations to this
+/// parsing phase which in theory could be lifted in the future but serve as
+/// simplifying assumptions for now:
+///
+/// * Nested components are not supported.
+/// * Imported modules or components are not supported.
+/// * Instantiating a module twice is not supported.
+/// * Component-level start functions are not supported.
+///
+/// Some of these restrictions are likely to be loosened over time, however.
+pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<'a>> {
+ let mut component = ComponentContext::default();
+ let mut parser = Parser::new(0).parse_all(full_wasm);
+
+ while let Some(payload) = parser.next() {
+ let payload = payload?;
+
+ match &payload {
+ // Module sections get parsed with wizer's core wasm support.
+ Payload::ModuleSection { .. } => {
+ let info = crate::parse::parse_with(&full_wasm, &mut parser)?;
+ component.push_module_section(info);
+ }
+
+ // All other sections get pushed raw as-is into the component.
+ _ => {
+ if let Some((id, range)) = payload.as_section() {
+ component.push_raw_section(wasm_encoder::RawSection {
+ id,
+ data: &full_wasm[range],
+ });
+ }
+ }
+ }
+
+ // Further validation/handling of each section, mostly about maintaining
+ // index spaces so we know what indices are used when the instrumented
+ // component is produced.
+ match payload {
+ Payload::Version {
+ encoding: Encoding::Module,
+ ..
+ } => {
+ bail!("expected a component, found a core module");
+ }
+
+ Payload::ComponentSection { .. } => {
+ bail!("wizer does not currently support nested components");
+ }
+
+ Payload::InstanceSection(reader) => {
+ for instance in reader {
+ let instance_index = component.inc_core_instances();
+
+ if let Instance::Instantiate { module_index, .. } = instance? {
+ match component.core_instantiations.entry(module_index) {
+ Entry::Vacant(entry) => {
+ entry.insert(instance_index);
+ }
+ Entry::Occupied(_) => bail!("modules may be instantiated at most once"),
+ }
+ }
+ }
+ }
+ Payload::ComponentInstanceSection(reader) => {
+ for _ in reader {
+ component.inc_instances();
+ }
+ }
+
+ Payload::ComponentAliasSection(reader) => {
+ for alias in reader {
+ match alias? {
+ ComponentAlias::CoreInstanceExport { kind, .. } => {
+ component.inc_core(kind);
+ }
+ ComponentAlias::InstanceExport { kind, .. } => {
+ validate_item_kind(kind, "aliases")?;
+ component.inc(kind);
+ }
+ ComponentAlias::Outer { kind, .. } => match kind {
+ ComponentOuterAliasKind::CoreType => {}
+ ComponentOuterAliasKind::Type => {
+ component.inc_types();
+ }
+ ComponentOuterAliasKind::CoreModule => {
+ bail!("wizer does not currently support module aliases");
+ }
+ ComponentOuterAliasKind::Component => {
+ bail!("wizer does not currently support component aliases");
+ }
+ },
+ }
+ }
+ }
+
+ Payload::ComponentCanonicalSection(reader) => {
+ for function in reader {
+ match function? {
+ CanonicalFunction::Lift { .. } => {
+ component.inc_funcs();
+ }
+ _ => {
+ component.inc_core_funcs();
+ }
+ }
+ }
+ }
+
+ Payload::ComponentImportSection(reader) => {
+ for import in reader {
+ let kind = import?.ty.kind();
+ validate_item_kind(kind, "imports")?;
+ component.inc(kind);
+ }
+ }
+
+ Payload::ComponentExportSection(reader) => {
+ for export in reader {
+ let kind = export?.kind;
+ validate_item_kind(kind, "exports")?;
+ component.inc(kind);
+ }
+ }
+
+ Payload::ComponentTypeSection(reader) => {
+ for _ in reader {
+ component.inc_types();
+ }
+ }
+
+ // The `start` section for components is itself not stable, so not
+ // super urgent to handle. A simplifying assumption is made to just
+ // reject it outright for now if it shows up.
+ Payload::ComponentStartSection { .. } => {
+ bail!("wizer does not currently support component start functions");
+ }
+
+ _ => {}
+ }
+ }
+
+ Ok(component)
+}
+
+fn validate_item_kind(kind: ComponentExternalKind, msg: &str) -> Result<()> {
+ match kind {
+ // Aliasing modules would require keeping track of where a module's
+ // original definition is. There's not much usage of this in the wild
+ // so reject it for now as a simplifying assumption.
+ ComponentExternalKind::Module => {
+ bail!("wizer does not currently support module {msg}");
+ }
+
+ // Aliasing components deals with nested instantiations or similar,
+ // where a simplifying assumption is made to not worry about that for now.
+ ComponentExternalKind::Component => {
+ bail!("wizer does not currently support component {msg}");
+ }
+
+ // Like start sections, support for this is just deferred to some other
+ // time, if ever.
+ ComponentExternalKind::Value => {
+ bail!("wizer does not currently support value {msg}");
+ }
+
+ ComponentExternalKind::Func
+ | ComponentExternalKind::Type
+ | ComponentExternalKind::Instance => Ok(()),
+ }
+}
diff --git a/crates/wizer/src/component/rewrite.rs b/crates/wizer/src/component/rewrite.rs
new file mode 100644
index 0000000000..6a674982be
--- /dev/null
+++ b/crates/wizer/src/component/rewrite.rs
@@ -0,0 +1,61 @@
+use crate::component::ComponentContext;
+use crate::component::info::RawSection;
+use crate::component::snapshot::ComponentSnapshot;
+use crate::{FuncRenames, Wizer};
+
+impl Wizer {
+ /// Helper method which is the equivalent of [`Wizer::rewrite`], but for
+ /// components.
+ ///
+ /// This effectively plumbs through all non-module sections as-is and
+ /// updates module sections with whatever [`Wizer::rewrite`] returns.
+ pub(crate) fn rewrite_component(
+ &self,
+ component: &mut ComponentContext<'_>,
+ snapshot: &ComponentSnapshot,
+ ) -> Vec<u8> {
+ let mut encoder = wasm_encoder::Component::new();
+
+ let mut module_index = 0;
+ for section in component.sections.iter_mut() {
+ match section {
+ RawSection::Module(module) => {
+ let snapshot = snapshot
+ .modules
+ .iter()
+ .find(|(i, _)| *i == module_index)
+ .map(|(_, s)| s);
+ module_index += 1;
+ match snapshot {
+ // This module's snapshot is used for [`Wizer::rewrite`]
+ // and the results of that are spliced into the
+ // component.
+ Some(snapshot) => {
+ let rewritten_wasm =
+ self.rewrite(module, snapshot, &FuncRenames::default());
+ encoder.section(&wasm_encoder::RawSection {
+ id: wasm_encoder::ComponentSectionId::CoreModule as u8,
+ data: &rewritten_wasm,
+ });
+ }
+
+ // This module wasn't instantiated and has no snapshot,
+ // plumb it through as-is.
+ None => {
+ let mut module_encoder = wasm_encoder::Module::new();
+ for section in module.raw_sections() {
+ module_encoder.section(section);
+ }
+ encoder.section(&wasm_encoder::ModuleSection(&module_encoder));
+ }
+ }
+ }
+ RawSection::Raw(s) => {
+ encoder.section(s);
+ }
+ }
+ }
+
+ encoder.finish()
+ }
+}
diff --git a/crates/wizer/src/component/snapshot.rs b/crates/wizer/src/component/snapshot.rs
new file mode 100644
index 0000000000..00f401eaf5
--- /dev/null
+++ b/crates/wizer/src/component/snapshot.rs
@@ -0,0 +1,130 @@
+use crate::component::info::Accessor;
+use crate::component::{ComponentContext, ComponentInstanceState, WIZER_INSTANCE};
+use crate::snapshot::Snapshot;
+use crate::{InstanceState, SnapshotVal};
+
+/// Snapshot result of a component.
+///
+/// Currently this only contains the state for instances found within the
+/// component, and each instance's snapshot is defined as the same core wasm
+/// snapshot state.
+pub struct ComponentSnapshot {
+ /// List of snapshots for corresponding module indices.
+ ///
+ /// Note that modules don't get a snapshot if they aren't instantiated.
+ pub(crate) modules: Vec<(u32, Snapshot)>,
+}
+
+pub async fn snapshot(
+ component: &ComponentContext<'_>,
+ ctx: &mut impl ComponentInstanceState,
+) -> ComponentSnapshot {
+ let mut modules = Vec::new();
+
+ for (module_index, module) in component.core_modules() {
+ // Ignore uninstantiated modules.
+ if !component.core_instantiations.contains_key(&module_index) {
+ continue;
+ }
+
+ // Use the core-level snapshotting routines to collect a
+ // snapshot of the module. Requests for core-level items are
+ // redirected through the component-level `ctx` provided.
+ let snapshot = crate::snapshot::snapshot(
+ module,
+ &mut ViaComponent {
+ ctx,
+ module_index,
+ component,
+ },
+ )
+ .await;
+ modules.push((module_index, snapshot));
+ }
+
+ ComponentSnapshot { modules }
+}
+
+/// Implementation of `InstanceState` via component model primitives.
+struct ViaComponent<'a, 'b, S> {
+ ctx: &'a mut S,
+ component: &'a ComponentContext<'b>,
+ module_index: u32,
+}
+
+impl<'a, S> ViaComponent<'a, '_, S>
+where
+ S: ComponentInstanceState,
+{
+ fn get_accessor(&self, name: &str) -> &'a Accessor {
+ let accessors = self.component.accessors.as_ref().unwrap();
+ accessors
+ .iter()
+ .find(|a| match a {
+ Accessor::Global {
+ module_index,
+ core_export_name,
+ ..
+ }
+ | Accessor::Memory {
+ module_index,
+ core_export_name,
+ ..
+ } => *module_index == self.module_index && core_export_name == name,
+ })
+ .unwrap()
+ }
+}
+
+impl<S> InstanceState for ViaComponent<'_, '_, S>
+where
+ S: ComponentInstanceState,
+{
+ async fn global_get(&mut self, name: &str) -> SnapshotVal {
+ let Accessor::Global {
+ accessor_export_name,
+ ty,
+ ..
+ } = self.get_accessor(name)
+ else {
+ panic!("expected global accessor for {name}");
+ };
+ match ty {
+ wasmparser::ValType::I32 => SnapshotVal::I32(
+ self.ctx
+ .call_func_ret_s32(WIZER_INSTANCE, accessor_export_name)
+ .await,
+ ),
+ wasmparser::ValType::I64 => SnapshotVal::I64(
+ self.ctx
+ .call_func_ret_s64(WIZER_INSTANCE, accessor_export_name)
+ .await,
+ ),
+ wasmparser::ValType::F32 => SnapshotVal::F32(
+ self.ctx
+ .call_func_ret_f32(WIZER_INSTANCE, accessor_export_name)
+ .await,
+ ),
+ wasmparser::ValType::F64 => SnapshotVal::F64(
+ self.ctx
+ .call_func_ret_f64(WIZER_INSTANCE, accessor_export_name)
+ .await,
+ ),
+ wasmparser::ValType::V128 => todo!(),
+ wasmparser::ValType::Ref(_) => unreachable!(),
+ }
+ }
+
+ async fn memory_contents(&mut self, name: &str) -> Vec<u8> {
+ let Accessor::Memory {
+ accessor_export_name,
+ ..
+ } = self.get_accessor(name)
+ else {
+ panic!("expected memory accessor for {name}");
+ };
+ self.ctx
+ .call_func_ret_list_u8(WIZER_INSTANCE, accessor_export_name)
+ .await
+ }
+}
diff --git a/crates/wizer/src/component/wasmtime.rs b/crates/wizer/src/component/wasmtime.rs
new file mode 100644
index 0000000000..83d8370d52
--- /dev/null
+++ b/crates/wizer/src/component/wasmtime.rs
@@ -0,0 +1,130 @@
+use crate::Wizer;
+use crate::component::ComponentInstanceState;
+use anyhow::{Context, anyhow};
+use wasmtime::component::{Component, ComponentExportIndex, Instance, Lift, types::ComponentItem};
+use wasmtime::{Result, Store};
+
+impl Wizer {
+ /// Same as [`Wizer::run`], except for components.
+ pub async fn run_component<T: Send>(
+ &self,
+ store: &mut Store<T>,
+ wasm: &[u8],
+ instantiate: impl AsyncFnOnce(&mut Store<T>, &Component) -> Result<Instance>,
+ ) -> anyhow::Result<Vec<u8>> {
+ let (cx, instrumented_wasm) = self.instrument_component(wasm)?;
+
+ #[cfg(feature = "wasmprinter")]
+ log::debug!(
+ "instrumented wasm: {}",
+ wasmprinter::print_bytes(&instrumented_wasm)?,
+ );
+
+ let engine = store.engine();
+ let component = Component::new(engine, &instrumented_wasm)
+ .context("failed to compile the Wasm component")?;
+ let index = self.validate_component_init_func(&component)?;
+
+ let instance = instantiate(store, &component).await?;
+ self.initialize_component(store, &instance, index).await?;
+ self.snapshot_component(cx, &mut WasmtimeWizerComponent { store, instance })
+ .await
+ }
+
+ fn validate_component_init_func(
+ &self,
+ component: &Component,
+ ) -> anyhow::Result<ComponentExportIndex> {
+ let init_func = self.get_init_func();
+ let (ty, index) = component
+ .get_export(None, init_func)
+ .ok_or_else(|| anyhow!("the component does export the function `{init_func}`"))?;
+
+ let ty = match ty {
+ ComponentItem::ComponentFunc(ty) => ty,
+ _ => anyhow::bail!("the component's `{init_func}` export is not a function",),
+ };
+
+ if ty.params().len() != 0 || ty.results().len() != 0 {
+ anyhow::bail!(
+ "the component's `{init_func}` function export does not have type `[] -> []`",
+ );
+ }
+ Ok(index)
+ }
+
+ async fn initialize_component<T: Send>(
+ &self,
+ store: &mut Store<T>,
+ instance: &Instance,
+ index: ComponentExportIndex,
+ ) -> anyhow::Result<()> {
+ let init_func = instance
+ .get_typed_func::<(), ()>(&mut *store, index)
+ .expect("checked by `validate_init_func`");
+ init_func
+ .call_async(&mut *store, ())
+ .await
+ .with_context(|| format!("the initialization function trapped"))?;
+ init_func
+ .post_return_async(&mut *store)
+ .await
+ .context("failed to call post-return")?;
+
+ Ok(())
+ }
+}
+
+/// Impementation of [`InstanceState`] backed by Wasmtime.
+pub struct WasmtimeWizerComponent<'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: Send> WasmtimeWizerComponent<'_, T> {
+ async fn call_func<R>(&mut self, instance: &str, func: &str) -> R
+ where
+ R: Lift + 'static,
+ {
+ log::debug!("invoking {instance}#{func}");
+ let (_, instance_export) = self
+ .instance
+ .get_export(&mut *self.store, None, instance)
+ .unwrap();
+ let (_, func_export) = self
+ .instance
+ .get_export(&mut *self.store, Some(&instance_export), func)
+ .unwrap();
+ let func = self
+ .instance
+ .get_typed_func::<(), (R,)>(&mut *self.store, func_export)
+ .unwrap();
+ let ret = func.call_async(&mut *self.store, ()).await.unwrap().0;
+ func.post_return_async(&mut *self.store).await.unwrap();
+ ret
+ }
+}
+
+impl<T: Send> ComponentInstanceState for WasmtimeWizerComponent<'_, T> {
+ async fn call_func_ret_list_u8(&mut self, instance: &str, func: &str) -> Vec<u8> {
+ self.call_func(instance, func).await
+ }
+
+ async fn call_func_ret_s32(&mut self, instance: &str, func: &str) -> i32 {
+ self.call_func(instance, func).await
+ }
+
+ async fn call_func_ret_s64(&mut self, instance: &str, func: &str) -> i64 {
+ self.call_func(instance, func).await
+ }
+
+ async fn call_func_ret_f32(&mut self, instance: &str, func: &str) -> u32 {
+ self.call_func::<f32>(instance, func).await.to_bits()
+ }
+
+ async fn call_func_ret_f64(&mut self, instance: &str, func: &str) -> u64 {
+ self.call_func::<f64>(instance, func).await.to_bits()
+ }
+}
diff --git a/crates/wizer/src/info.rs b/crates/wizer/src/info.rs
index 744f666e11..8859ae91eb 100644
--- a/crates/wizer/src/info.rs
+++ b/crates/wizer/src/info.rs
@@ -163,7 +163,15 @@ impl<'a> ModuleContext<'a> {
/// Iterate over the defined globals in this module.
pub(crate) fn defined_globals(
&self,
- ) -> impl Iterator<Item = (u32, wasmparser::GlobalType)> + '_ {
+ ) -> impl Iterator<Item = (u32, wasmparser::GlobalType, Option<&str>)> + '_ {
+ let mut defined_global_exports = self
+ .defined_global_exports
+ .as_ref()
+ .map(|v| v.as_slice())
+ .unwrap_or(&[])
+ .iter()
+ .peekable();
+
self.globals
.iter()
.copied()
@@ -172,7 +180,13 @@ impl<'a> ModuleContext<'a> {
self.defined_globals_index
.map_or(self.globals.len(), |i| usize::try_from(i).unwrap()),
)
- .map(|(i, g)| (u32::try_from(i).unwrap(), g))
+ .map(move |(i, g)| {
+ let i = u32::try_from(i).unwrap();
+ let name = defined_global_exports
+ .next_if(|(j, _)| *j == i)
+ .map(|(_, name)| name.as_str());
+ (i, g, name)
+ })
}
/// Get a slice of this module's original raw sections.
diff --git a/crates/wizer/src/instrument.rs b/crates/wizer/src/instrument.rs
index cc82eed4b6..9208130089 100644
--- a/crates/wizer/src/instrument.rs
+++ b/crates/wizer/src/instrument.rs
@@ -83,7 +83,7 @@ pub(crate) fn instrument(module: &mut ModuleContext<'_>) -> Vec<u8> {
// Now export all of this module's defined globals, memories,
// and instantiations under well-known names so we can inspect
// them after initialization.
- for (i, ty) in module.defined_globals() {
+ for (i, ty, _) in module.defined_globals() {
if !ty.mutable {
continue;
}
diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rs
index cf965d4606..3975f8dcb3 100644
--- a/crates/wizer/src/lib.rs
+++ b/crates/wizer/src/lib.rs
@@ -3,6 +3,7 @@
//! See the [`Wizer`] struct for details.
#![deny(missing_docs)]
+#![cfg_attr(docsrs, feature(doc_cfg))]
mod info;
mod instrument;
@@ -14,6 +15,10 @@ mod snapshot;
mod wasmtime;
#[cfg(feature = "wasmtime")]
pub use wasmtime::*;
+#[cfg(feature = "component-model")]
+mod component;
+#[cfg(feature = "component-model")]
+pub use component::*;
pub use crate::info::ModuleContext;
pub use crate::snapshot::SnapshotVal;
@@ -50,7 +55,7 @@ pub struct Wizer {
/// initialize the Wasm module.
#[cfg_attr(
feature = "clap",
- arg(short = 'f', long, default_value = "wizer.initialize")
+ arg(short = 'f', long, default_value = "wizer-initialize")
)]
init_func: String,
@@ -101,6 +106,7 @@ fn parse_rename(s: &str) -> anyhow::Result<(String, String)> {
Ok((parts[0].into(), parts[1].into()))
}
+#[derive(Default)]
struct FuncRenames {
/// For a given export name that we encounter in the original module, a map
/// to a new name, if any, to emit in the output module.
@@ -139,7 +145,7 @@ impl Wizer {
/// Construct a new `Wizer` builder.
pub fn new() -> Self {
Wizer {
- init_func: "wizer.initialize".into(),
+ init_func: "wizer-initialize".to_string(),
func_renames: vec![],
keep_init_func: None,
}
@@ -147,7 +153,7 @@ impl Wizer {
/// The export name of the initializer function.
///
- /// Defaults to `"wizer.initialize"`.
+ /// Defaults to `"wizer-initialize"`.
pub fn init_func(&mut self, init_func: impl Into<String>) -> &mut Self {
self.init_func = init_func.into();
self
@@ -205,17 +211,7 @@ impl Wizer {
}
let instrumented_wasm = instrument::instrument(&mut cx);
-
- if cfg!(debug_assertions) {
- if let Err(error) = self.wasm_validate(&instrumented_wasm) {
- #[cfg(feature = "wasmprinter")]
- let wat = wasmprinter::print_bytes(&wasm)
- .unwrap_or_else(|e| format!("Disassembling to WAT failed: {}", e));
- #[cfg(not(feature = "wasmprinter"))]
- let wat = "`wasmprinter` cargo feature is not enabled".to_string();
- panic!("instrumented Wasm is not valid: {error:?}\n\nWAT:\n{wat}");
- }
- }
+ self.debug_assert_valid_wasm(&instrumented_wasm);
Ok((cx, instrumented_wasm))
}
@@ -226,31 +222,36 @@ impl Wizer {
///
/// This returns a new WebAssembly binary which has all state
/// pre-initialized.
- pub fn snapshot(
+ pub async fn snapshot(
&self,
mut cx: ModuleContext<'_>,
- instance: &mut dyn InstanceState,
+ instance: &mut impl InstanceState,
) -> anyhow::Result<Vec<u8>> {
// Parse rename spec.
let renames = FuncRenames::parse(&self.func_renames)?;
- let snapshot = snapshot::snapshot(&cx, instance);
+ let snapshot = snapshot::snapshot(&cx, instance).await;
let rewritten_wasm = self.rewrite(&mut cx, &snapshot, &renames);
- if cfg!(debug_assertions) {
- if let Err(error) = self.wasm_validate(&rewritten_wasm) {
- #[cfg(feature = "wasmprinter")]
- let wat = wasmprinter::print_bytes(&rewritten_wasm)
- .unwrap_or_else(|e| format!("Disassembling to WAT failed: {}", e));
- #[cfg(not(feature = "wasmprinter"))]
- let wat = "`wasmprinter` cargo feature is not enabled".to_string();
- panic!("rewritten Wasm is not valid: {error:?}\n\nWAT:\n{wat}");
- }
- }
+ self.debug_assert_valid_wasm(&rewritten_wasm);
Ok(rewritten_wasm)
}
+ fn debug_assert_valid_wasm(&self, wasm: &[u8]) {
+ if !cfg!(debug_assertions) {
+ return;
+ }
+ if let Err(error) = self.wasm_validate(&wasm) {
+ #[cfg(feature = "wasmprinter")]
+ let wat = wasmprinter::print_bytes(&wasm)
+ .unwrap_or_else(|e| format!("Disassembling to WAT failed: {}", e));
+ #[cfg(not(feature = "wasmprinter"))]
+ let wat = "`wasmprinter` cargo feature is not enabled".to_string();
+ panic!("instrumented Wasm is not valid: {error:?}\n\nWAT:\n{wat}");
+ }
+ }
+
fn wasm_validate(&self, wasm: &[u8]) -> anyhow::Result<()> {
log::debug!("Validating input Wasm");
@@ -377,7 +378,7 @@ pub trait InstanceState {
///
/// 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;
+ fn global_get(&mut self, name: &str) -> impl Future<Output = SnapshotVal> + Send;
/// Loads the contents of the memory specified by `name`, returning the
/// entier contents as a `Vec<u8>`.
@@ -385,5 +386,5 @@ pub trait InstanceState {
/// # Panics
///
/// This function panics if `name` isn't an exported memory.
- fn memory_contents(&mut self, name: &str) -> Vec<u8>;
+ fn memory_contents(&mut self, name: &str) -> impl Future<Output = Vec<u8>> + Send;
}
diff --git a/crates/wizer/src/parse.rs b/crates/wizer/src/parse.rs
index dfad0c2bb5..0293d42e5d 100644
--- a/crates/wizer/src/parse.rs
+++ b/crates/wizer/src/parse.rs
@@ -1,14 +1,21 @@
use crate::info::ModuleContext;
-use anyhow::Context;
-use wasmparser::Parser;
+use anyhow::{Context, bail};
+use wasmparser::{Encoding, Parser};
/// Parse the given Wasm bytes into a `ModuleInfo` tree.
pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ModuleContext<'a>> {
+ parse_with(full_wasm, &mut Parser::new(0).parse_all(full_wasm))
+}
+
+pub(crate) fn parse_with<'a>(
+ full_wasm: &'a [u8],
+ payloads: &mut impl Iterator<Item = wasmparser::Result<wasmparser::Payload<'a>>>,
+) -> anyhow::Result<ModuleContext<'a>> {
log::debug!("Parsing the input Wasm");
let mut module = ModuleContext::default();
- for payload in Parser::new(0).parse_all(full_wasm) {
+ while let Some(payload) = payloads.next() {
use wasmparser::Payload::*;
let payload = payload.context("failed to parse Wasm")?;
@@ -18,12 +25,19 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ModuleContext<'a>
}
match payload {
+ Version {
+ encoding: Encoding::Component,
+ ..
+ } => {
+ bail!("expected a core module, found a component");
+ }
ImportSection(imports) => import_section(&mut module, imports)?,
FunctionSection(funcs) => function_section(&mut module, funcs)?,
TableSection(tables) => table_section(&mut module, tables)?,
MemorySection(mems) => memory_section(&mut module, mems)?,
GlobalSection(globals) => global_section(&mut module, globals)?,
ExportSection(exports) => export_section(&mut module, exports)?,
+ End { .. } => break,
_ => {}
}
}
diff --git a/crates/wizer/src/rewrite.rs b/crates/wizer/src/rewrite.rs
index fee0ad6059..df37e6b645 100644
--- a/crates/wizer/src/rewrite.rs
+++ b/crates/wizer/src/rewrite.rs
@@ -86,39 +86,38 @@ impl Wizer {
)
.unwrap();
let mut globals = wasm_encoder::GlobalSection::new();
- let mut snapshot = snapshot.globals.iter().peekable();
- for ((i, glob_ty), global) in module.defined_globals().zip(original_globals) {
+ let mut snapshot = snapshot.globals.iter();
+ for ((_, glob_ty, export_name), global) in
+ module.defined_globals().zip(original_globals)
+ {
let global = global.unwrap();
- match snapshot.next_if(|(j, _)| *j == i) {
+ if export_name.is_some() {
// This is a mutable global and it was present in
// the snapshot, so translate the snapshot value to
// a constant expression and insert it.
- Some((_, val)) => {
- assert!(glob_ty.mutable);
- let init = match val {
- 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))
- }
- SnapshotVal::F64(x) => {
- ConstExpr::f64_const(wasm_encoder::Ieee64::new(*x))
- }
- SnapshotVal::V128(x) => ConstExpr::v128_const(x.cast_signed()),
- };
- let glob_ty = RoundtripReencoder.global_type(glob_ty).unwrap();
- globals.global(glob_ty, &init);
- }
-
+ assert!(glob_ty.mutable);
+ let (_, val) = snapshot.next().unwrap();
+ let init = match val {
+ 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))
+ }
+ SnapshotVal::F64(x) => {
+ ConstExpr::f64_const(wasm_encoder::Ieee64::new(*x))
+ }
+ SnapshotVal::V128(x) => ConstExpr::v128_const(x.cast_signed()),
+ };
+ let glob_ty = RoundtripReencoder.global_type(glob_ty).unwrap();
+ globals.global(glob_ty, &init);
+ } else {
// This global isn't mutable so preserve its value
// as-is.
- None => {
- assert!(!glob_ty.mutable);
- RoundtripReencoder
- .parse_global(&mut globals, global)
- .unwrap();
- }
- }
+ assert!(!glob_ty.mutable);
+ RoundtripReencoder
+ .parse_global(&mut globals, global)
+ .unwrap();
+ };
}
encoder.section(&globals);
}
@@ -129,7 +128,7 @@ impl Wizer {
s if s.id == u8::from(SectionId::Export) => {
let mut exports = wasm_encoder::ExportSection::new();
for export in module.exports() {
- if (export.name == self.init_func && !self.get_keep_init_func())
+ if (export.name == self.get_init_func() && !self.get_keep_init_func())
|| (has_wasi_initialize && export.name == "_initialize")
{
continue;
diff --git a/crates/wizer/src/snapshot.rs b/crates/wizer/src/snapshot.rs
index 1ac112f624..387e856ab3 100644
--- a/crates/wizer/src/snapshot.rs
+++ b/crates/wizer/src/snapshot.rs
@@ -88,11 +88,11 @@ impl DataSegment {
/// defaults.
//
// TODO: when we support reference types, we will have to snapshot tables.
-pub fn snapshot(module: &ModuleContext<'_>, ctx: &mut dyn InstanceState) -> Snapshot {
+pub async fn snapshot(module: &ModuleContext<'_>, ctx: &mut impl InstanceState) -> Snapshot {
log::debug!("Snapshotting the initialized state");
- let globals = snapshot_globals(module, ctx);
- let (memory_mins, data_segments) = snapshot_memories(module, ctx);
+ let globals = snapshot_globals(module, ctx).await;
+ let (memory_mins, data_segments) = snapshot_memories(module, ctx).await;
Snapshot {
globals,
@@ -102,26 +102,25 @@ pub fn snapshot(module: &ModuleContext<'_>, ctx: &mut dyn InstanceState) -> Snap
}
/// Get the initialized values of all globals.
-fn snapshot_globals(
+async fn snapshot_globals(
module: &ModuleContext<'_>,
- ctx: &mut dyn InstanceState,
+ ctx: &mut impl InstanceState,
) -> Vec<(u32, SnapshotVal)> {
log::debug!("Snapshotting global values");
- module
- .defined_global_exports
- .as_ref()
- .unwrap()
- .iter()
- .map(|(i, name)| (*i, ctx.global_get(&name)))
- .collect()
+ let mut ret = Vec::new();
+ for (i, name) in module.defined_global_exports.as_ref().unwrap().iter() {
+ let val = ctx.global_get(&name).await;
+ ret.push((*i, val));
+ }
+ ret
}
/// Find the initialized minimum page size of each memory, as well as all
/// regions of non-zero memory.
-fn snapshot_memories(
+async fn snapshot_memories(
module: &ModuleContext<'_>,
- instance: &mut dyn InstanceState,
+ instance: &mut impl InstanceState,
) -> (Vec<u64>, Vec<DataSegment>) {
log::debug!("Snapshotting memories");
@@ -132,7 +131,7 @@ fn snapshot_memories(
.defined_memories()
.zip(module.defined_memory_exports.as_ref().unwrap());
for ((memory_index, ty), name) in iter {
- let memory = Arc::new(instance.memory_contents(&name));
+ let memory = Arc::new(instance.memory_contents(&name).await);
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);
diff --git a/crates/wizer/src/wasmtime.rs b/crates/wizer/src/wasmtime.rs
index 8225d2190d..9d1f29fac2 100644
--- a/crates/wizer/src/wasmtime.rs
+++ b/crates/wizer/src/wasmtime.rs
@@ -5,11 +5,11 @@ 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>(
+ pub async fn run<T: Send>(
&self,
store: &mut Store<T>,
wasm: &[u8],
- instantiate: impl FnOnce(&mut Store<T>, &Module) -> Result<wasmtime::Instance>,
+ instantiate: impl AsyncFnOnce(&mut Store<T>, &Module) -> Result<wasmtime::Instance>,
) -> anyhow::Result<Vec<u8>> {
let (cx, instrumented_wasm) = self.instrument(wasm)?;
@@ -18,38 +18,39 @@ impl Wizer {
.context("failed to compile the Wasm module")?;
self.validate_init_func(&module)?;
- let instance = instantiate(store, &module)?;
- self.initialize(store, &instance)?;
+ let instance = instantiate(store, &module).await?;
+ self.initialize(store, &instance).await?;
self.snapshot(cx, &mut WasmtimeWizer { store, instance })
+ .await
}
/// 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) {
+ match module.get_export(self.get_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
+ self.get_init_func()
);
}
}
Some(_) => anyhow::bail!(
"the Wasm module's `{}` export is not a function",
- &self.init_func
+ self.get_init_func()
),
None => anyhow::bail!(
"the Wasm module does not have a `{}` export",
- &self.init_func
+ self.get_init_func()
),
}
Ok(())
}
/// Instantiate the module and call its initialization function.
- fn initialize<T>(
+ async fn initialize<T: Send>(
&self,
store: &mut Store<T>,
instance: &wasmtime::Instance,
@@ -58,11 +59,12 @@ impl Wizer {
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, ()))
+ func.typed::<(), ()>(&store)?
+ .call_async(&mut *store, ())
+ .await
.context("calling the Reactor initialization function")?;
- if self.init_func == "_initialize" {
+ if self.get_init_func() == "_initialize" {
// Don't run `_initialize` twice if the it was explicitly
// requested as the init function.
return Ok(());
@@ -71,11 +73,12 @@ impl Wizer {
}
let init_func = instance
- .get_typed_func::<(), ()>(&mut *store, &self.init_func)
+ .get_typed_func::<(), ()>(&mut *store, self.get_init_func())
.expect("checked by `validate_init_func`");
init_func
- .call(&mut *store, ())
- .with_context(|| format!("the `{}` function trapped", self.init_func))?;
+ .call_async(&mut *store, ())
+ .await
+ .with_context(|| format!("the `{}` function trapped", self.get_init_func()))?;
Ok(())
}
@@ -89,8 +92,8 @@ pub struct WasmtimeWizer<'a, T: 'static> {
pub instance: Instance,
}
-impl<T> InstanceState for WasmtimeWizer<'_, T> {
- fn global_get(&mut self, name: &str) -> SnapshotVal {
+impl<T: Send> InstanceState for WasmtimeWizer<'_, T> {
+ async 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),
@@ -102,7 +105,7 @@ impl<T> InstanceState for WasmtimeWizer<'_, T> {
}
}
- fn memory_contents(&mut self, name: &str) -> Vec<u8> {
+ async 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/crates/wizer/test.wasm b/crates/wizer/test.wasm
new file mode 100644
index 0000000000..4abd7bd1ff
Binary files /dev/null and b/crates/wizer/test.wasm differ
diff --git a/crates/wizer/tests/all/component.rs b/crates/wizer/tests/all/component.rs
new file mode 100644
index 0000000000..c844bccaba
--- /dev/null
+++ b/crates/wizer/tests/all/component.rs
@@ -0,0 +1,406 @@
+use anyhow::{Context, Result, bail};
+use wasmtime::component::{Component, Instance, Linker, Val};
+use wasmtime::{Config, Engine, Store};
+use wasmtime_wizer::Wizer;
+
+fn fail_wizening(msg: &str, wasm: &[u8]) -> Result<()> {
+ let _ = env_logger::try_init();
+
+ let wasm = wat::parse_bytes(wasm)?;
+ match Wizer::new().instrument_component(&wasm) {
+ Ok(_) => bail!("expected wizening to fail"),
+ Err(e) => {
+ let err = format!("{e}");
+ if !err.contains(msg) {
+ bail!("unexpected error: {err}");
+ }
+ Ok(())
+ }
+ }
+}
+
+#[test]
+fn unsupported_constructs() -> Result<()> {
+ fail_wizening(
+ "does not currently support component imports",
+ br#"(component
+ (import "x" (component))
+ )"#,
+ )?;
+
+ fail_wizening(
+ "does not currently support nested components",
+ br#"(component
+ (component)
+ )"#,
+ )?;
+
+ fail_wizening(
+ "does not currently support component aliases",
+ br#"(component
+ (import "x" (instance $i (export "x" (component))))
+ (alias export $i "x" (component $c))
+ )"#,
+ )?;
+
+ fail_wizening(
+ "does not currently support module imports",
+ br#"(component
+ (import "x" (core module))
+ )"#,
+ )?;
+
+ fail_wizening(
+ "does not currently support module exports",
+ br#"(component
+ (core module $x)
+ (export "x" (core module $x))
+ )"#,
+ )?;
+
+ fail_wizening(
+ "does not currently support module aliases",
+ br#"(component
+ (import "x" (instance $i (export "x" (core module))))
+ (alias export $i "x" (core module $c))
+ )"#,
+ )?;
+ fail_wizening(
+ "does not currently support component start functions",
+ br#"(component
+ (import "f" (func $f))
+ (start $f)
+ )"#,
+ )?;
+
+ fail_wizening(
+ "modules may be instantiated at most once",
+ br#"(component
+ (core module $a)
+ (core instance $a1 (instantiate $a))
+ (core instance $a2 (instantiate $a))
+ )"#,
+ )?;
+
+ Ok(())
+}
+
+fn store() -> Result<Store<()>> {
+ let mut config = Config::new();
+ config.async_support(true);
+ let engine = Engine::new(&config)?;
+ Ok(Store::new(&engine, ()))
+}
+
+async fn instantiate(store: &mut Store<()>, component: &Component) -> Result<Instance> {
+ Linker::new(store.engine())
+ .instantiate_async(store, component)
+ .await
+}
+
+async fn wizen_and_run_wasm(expected: u32, wat: &str) -> Result<()> {
+ let _ = env_logger::try_init();
+ let wasm = wat::parse_str(wat)?;
+
+ log::debug!(
+ "=== PreWizened Wasm ==========================================================\n\
+ {}\n\
+ ===========================================================================",
+ wasmprinter::print_bytes(&wasm).unwrap()
+ );
+ let mut store = store()?;
+ let wasm = Wizer::new()
+ .run_component(&mut store, &wasm, instantiate)
+ .await?;
+ log::debug!(
+ "=== Wizened Wasm ==========================================================\n\
+ {}\n\
+ ===========================================================================",
+ wasmprinter::print_bytes(&wasm).unwrap()
+ );
+ if log::log_enabled!(log::Level::Debug) {
+ std::fs::write("test.wasm", &wasm).unwrap();
+ }
+
+ let module =
+ Component::new(store.engine(), wasm).context("Wasm test case failed to compile")?;
+
+ let linker = Linker::new(store.engine());
+ let instance = linker.instantiate_async(&mut store, &module).await?;
+
+ let run = instance.get_func(&mut store, "run").ok_or_else(|| {
+ anyhow::anyhow!("the test Wasm component does not export a `run` function")
+ })?;
+
+ let mut actual = [Val::U8(0)];
+ run.call_async(&mut store, &[], &mut actual).await?;
+ let actual = match actual[0] {
+ Val::U32(x) => x,
+ _ => anyhow::bail!("expected an u32 result"),
+ };
+ anyhow::ensure!(
+ expected == actual,
+ "expected `{expected}`, found `{actual}`",
+ );
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn simple() -> Result<()> {
+ wizen_and_run_wasm(
+ 42,
+ r#"(component
+ (core module $m
+ (func (export "init"))
+
+ (func (export "run") (result i32)
+ i32.const 42
+ )
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn snapshot_global_i32() -> Result<()> {
+ wizen_and_run_wasm(
+ 1,
+ r#"(component
+ (core module $m
+ (global $g (mut i32) i32.const 0)
+ (func (export "init") (global.set $g (i32.const 1)))
+ (func (export "run") (result i32) global.get $g)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn snapshot_global_i64() -> Result<()> {
+ wizen_and_run_wasm(
+ 1,
+ r#"(component
+ (core module $m
+ (global $g (mut i64) i64.const 0)
+ (func (export "init") (global.set $g (i64.const 1)))
+ (func (export "run") (result i32)
+ global.get $g
+ i32.wrap_i64
+ )
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn snapshot_global_f32() -> Result<()> {
+ wizen_and_run_wasm(
+ 1,
+ r#"(component
+ (core module $m
+ (global $g (mut f32) f32.const 0)
+ (func (export "init") (global.set $g (f32.const 1)))
+ (func (export "run") (result i32)
+ global.get $g
+ i32.trunc_f32_s)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn snapshot_global_f64() -> Result<()> {
+ wizen_and_run_wasm(
+ 1,
+ r#"(component
+ (core module $m
+ (global $g (mut f64) f64.const 0)
+ (func (export "init") (global.set $g (f64.const 1)))
+ (func (export "run") (result i32)
+ global.get $g
+ i32.trunc_f64_s)
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[test]
+fn v128_globals() -> Result<()> {
+ fail_wizening(
+ "component wizening does not support v128 globals",
+ br#"(component
+ (core module $a
+ (global (export "x") (mut v128) (v128.const i32x4 1 2 3 4))
+ )
+ (core instance (instantiate $a))
+ )"#,
+ )
+}
+
+#[tokio::test]
+async fn snapshot_memory() -> Result<()> {
+ wizen_and_run_wasm(
+ 201,
+ r#"(component
+ (core module $m
+ (memory 1)
+ (func (export "init")
+ i32.const 200
+ i32.const 100
+ i32.store
+ i32.const 300
+ i32.const 101
+ i32.store
+ )
+ (func (export "run") (result i32)
+ i32.const 200
+ i32.load
+ i32.const 300
+ i32.load
+ i32.add
+ )
+ )
+ (core instance $i (instantiate $m))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn multiple_modules() -> Result<()> {
+ wizen_and_run_wasm(
+ 100 + 101 + 200 + 201 + 7 + 112,
+ r#"(component
+ (core module $a
+ (memory 1)
+ (global $g (export "g") (mut i32) (i32.const 0))
+
+ (func (export "init")
+ i32.const 200
+ i32.const 100
+ i32.store
+ i32.const 300
+ i32.const 101
+ i32.store
+ )
+
+ (func (export "run") (result i32)
+ i32.const 200
+ i32.load
+ i32.const 300
+ i32.load
+ i32.add
+ global.get $g
+ i32.add
+ )
+ )
+ (core instance $a (instantiate $a))
+
+ (core module $b
+ (import "a" "g" (global $g (mut i32)))
+ (import "a" "init" (func $init))
+ (import "a" "run" (func $run (result i32)))
+ (memory (export "mem") 1)
+ (func (export "init")
+ call $init
+ i32.const 400
+ i32.const 200
+ i32.store
+ i32.const 500
+ i32.const 201
+ i32.store
+
+ i32.const 111
+ global.set $g
+ )
+ (func (export "run") (result i32)
+ i32.const 400
+ i32.load
+ i32.const 500
+ i32.load
+ i32.add
+ call $run
+ i32.add
+ )
+ )
+ (core instance $b (instantiate $b (with "a" (instance $a))))
+
+ (core module $c
+ (import "a" "g" (global $g (mut i32)))
+ (import "b" "init" (func $init))
+ (import "b" "run" (func $run (result i32)))
+ (import "b" "mem" (memory 1))
+
+ (func (export "init")
+ call $init
+
+ i32.const 1
+ memory.grow
+ i32.const -1
+ i32.eq
+ if unreachable end
+
+ i32.const 65536
+ i32.const 7
+ i32.store
+
+ ;; overwrite a#init with a different value, make sure this
+ ;; one is snapshot
+ i32.const 112
+ global.set $g
+ )
+ (func (export "run") (result i32)
+ i32.const 65536
+ i32.load
+ call $run
+ i32.add
+ )
+ )
+ (core instance $c (instantiate $c
+ (with "a" (instance $a))
+ (with "b" (instance $b))
+ ))
+
+ (func (export "run") (result u32) (canon lift (core func $c "run")))
+ (func (export "wizer-initialize") (canon lift (core func $c "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}
diff --git a/crates/wizer/tests/all/main.rs b/crates/wizer/tests/all/main.rs
index bb277495bf..cf8d03873d 100644
--- a/crates/wizer/tests/all/main.rs
+++ b/crates/wizer/tests/all/main.rs
@@ -1,3 +1,5 @@
+#[cfg(feature = "component-model")]
+mod component;
mod make_linker;
mod preloads;
mod tests;
diff --git a/crates/wizer/tests/all/make_linker.rs b/crates/wizer/tests/all/make_linker.rs
index 05ab49b473..27685d21e3 100644
--- a/crates/wizer/tests/all/make_linker.rs
+++ b/crates/wizer/tests/all/make_linker.rs
@@ -3,10 +3,11 @@ use wasmtime_wasi::WasiCtxBuilder;
use wasmtime_wizer::Wizer;
use wat::parse_str as wat_to_wasm;
-fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
+async fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
let _ = env_logger::try_init();
let mut config = wasmtime::Config::new();
+ config.async_support(true);
wasmtime::Cache::from_file(None)
.map(|cache| config.cache(Some(cache)))
.unwrap();
@@ -16,11 +17,13 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
let engine = wasmtime::Engine::new(&config)?;
let wasi_ctx = WasiCtxBuilder::new().build_p1();
let mut store = wasmtime::Store::new(&engine, wasi_ctx);
- let wasm = Wizer::new().run(&mut store, &wasm, |store, module| {
- let mut linker = wasmtime::Linker::new(module.engine());
- linker.func_wrap("foo", "bar", |x: i32| x + 1)?;
- linker.instantiate(store, module)
- })?;
+ let wasm = Wizer::new()
+ .run(&mut store, &wasm, async |store, module| {
+ let mut linker = wasmtime::Linker::new(module.engine());
+ linker.func_wrap("foo", "bar", |x: i32| x + 1)?;
+ linker.instantiate_async(store, module).await
+ })
+ .await?;
log::debug!(
"=== Wizened Wasm ==========================================================\n\
{}\n\
@@ -41,14 +44,14 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
Err(anyhow!("shouldn't be called"))
})?;
- let instance = linker.instantiate(&mut store, &module)?;
+ let instance = linker.instantiate_async(&mut store, &module).await?;
let run = instance
.get_func(&mut store, "run")
.ok_or_else(|| anyhow::anyhow!("the test Wasm module does not export a `run` function"))?;
let mut actual = vec![wasmtime::Val::I32(0)];
- run.call(&mut store, args, &mut actual)?;
+ run.call_async(&mut store, args, &mut actual).await?;
anyhow::ensure!(actual.len() == 1, "expected one result");
let actual = match actual[0] {
wasmtime::Val::I32(x) => x,
@@ -62,14 +65,14 @@ fn run_wasm(args: &[wasmtime::Val], expected: i32, wasm: &[u8]) -> Result<()> {
Ok(())
}
-fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
+async fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(wat)?;
- run_wasm(args, expected, &wasm)
+ run_wasm(args, expected, &wasm).await
}
-#[test]
-fn custom_linker() -> Result<()> {
+#[tokio::test]
+async fn custom_linker() -> Result<()> {
run_wat(
&[],
1,
@@ -78,7 +81,7 @@ fn custom_linker() -> Result<()> {
(type (func (param i32) (result i32)))
(import "foo" "bar" (func (type 0)))
(global $g (mut i32) (i32.const 0))
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
global.get $g
call 0
global.set $g
@@ -88,4 +91,5 @@ fn custom_linker() -> Result<()> {
)
)"#,
)
+ .await
}
diff --git a/crates/wizer/tests/all/preloads.rs b/crates/wizer/tests/all/preloads.rs
index cd838bfc06..0ddc2fcd8b 100644
--- a/crates/wizer/tests/all/preloads.rs
+++ b/crates/wizer/tests/all/preloads.rs
@@ -19,48 +19,52 @@ const PRELOAD2: &'static str = r#"
i32.add))
"#;
-fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val> {
+async fn run_with_preloads(args: &[wasmtime::Val], wat: &str) -> Result<wasmtime::Val> {
let wasm = wat_to_wasm(wat)?;
- let engine = wasmtime::Engine::default();
+ let mut config = wasmtime::Config::new();
+ config.async_support(true);
+ let engine = wasmtime::Engine::new(&config)?;
let mut store = wasmtime::Store::new(&engine, ());
let mod1 = Module::new(store.engine(), PRELOAD1)?;
let mod2 = Module::new(store.engine(), PRELOAD2)?;
- let processed = Wizer::new().run(&mut store, &wasm, |store, module| {
- let i1 = Instance::new(&mut *store, &mod1, &[])?;
- let i2 = Instance::new(&mut *store, &mod2, &[])?;
- let mut linker = Linker::new(store.engine());
- linker.instance(&mut *store, "mod1", i1)?;
- linker.instance(&mut *store, "mod2", i2)?;
- linker.instantiate(store, module)
- })?;
+ let processed = Wizer::new()
+ .run(&mut store, &wasm, async |store, module| {
+ let i1 = Instance::new_async(&mut *store, &mod1, &[]).await?;
+ let i2 = Instance::new_async(&mut *store, &mod2, &[]).await?;
+ let mut linker = Linker::new(store.engine());
+ linker.instance(&mut *store, "mod1", i1)?;
+ linker.instance(&mut *store, "mod2", i2)?;
+ linker.instantiate_async(store, module).await
+ })
+ .await?;
let testmod = wasmtime::Module::new(&engine, &processed[..])?;
- let mod1_inst = wasmtime::Instance::new(&mut store, &mod1, &[])?;
- let mod2_inst = wasmtime::Instance::new(&mut store, &mod2, &[])?;
+ let mod1_inst = wasmtime::Instance::new_async(&mut store, &mod1, &[]).await?;
+ let mod2_inst = wasmtime::Instance::new_async(&mut store, &mod2, &[]).await?;
let mut linker = wasmtime::Linker::new(&engine);
linker.instance(&mut store, "mod1", mod1_inst)?;
linker.instance(&mut store, "mod2", mod2_inst)?;
- let inst = linker.instantiate(&mut store, &testmod)?;
+ let inst = linker.instantiate_async(&mut store, &testmod).await?;
let run = inst
.get_func(&mut store, "run")
.ok_or_else(|| anyhow::anyhow!("no `run` function on test module"))?;
let mut returned = vec![wasmtime::Val::I32(0)];
- run.call(&mut store, args, &mut returned)?;
+ run.call_async(&mut store, args, &mut returned).await?;
Ok(returned[0])
}
-#[test]
-fn test_preloads() {
+#[tokio::test]
+async fn test_preloads() {
const WAT: &'static str = r#"
(module
(import "mod1" "f" (func $mod1f (param i32) (result i32)))
(import "mod2" "f" (func $mod2f (param i32) (result i32)))
(global $g1 (mut i32) (i32.const 0))
(global $g2 (mut i32) (i32.const 0))
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 100
call $mod1f
global.set $g1
@@ -79,7 +83,8 @@ fn test_preloads() {
i32.add))
"#;
- let result =
- run_with_preloads(&[wasmtime::Val::I32(200), wasmtime::Val::I32(201)], WAT).unwrap();
+ let result = run_with_preloads(&[wasmtime::Val::I32(200), wasmtime::Val::I32(201)], WAT)
+ .await
+ .unwrap();
assert!(matches!(result, wasmtime::Val::I32(607)));
}
diff --git a/crates/wizer/tests/all/tests.rs b/crates/wizer/tests/all/tests.rs
index fe597fd15b..895b9e6945 100644
--- a/crates/wizer/tests/all/tests.rs
+++ b/crates/wizer/tests/all/tests.rs
@@ -6,10 +6,10 @@ use wasmtime_wasi::{WasiCtxBuilder, p1};
use wasmtime_wizer::Wizer;
use wat::parse_str as wat_to_wasm;
-fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
+async fn run_wat(args: &[wasmtime::Val], expected: i32, wat: &str) -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(wat)?;
- wizen_and_run_wasm(args, expected, &wasm, get_wizer())
+ wizen_and_run_wasm(args, expected, &wasm, get_wizer()).await
}
fn get_wizer() -> Wizer {
@@ -19,19 +19,20 @@ fn get_wizer() -> Wizer {
fn store() -> Result<Store<p1::WasiP1Ctx>> {
let mut wasi = WasiCtxBuilder::new();
let mut config = Config::new();
+ config.async_support(true);
config.relaxed_simd_deterministic(true);
let engine = Engine::new(&config)?;
Ok(Store::new(&engine, wasi.build_p1()))
}
-fn instantiate(store: &mut Store<p1::WasiP1Ctx>, module: &Module) -> Result<Instance> {
+async fn instantiate(store: &mut Store<p1::WasiP1Ctx>, module: &Module) -> Result<Instance> {
let mut linker = Linker::new(store.engine());
- p1::add_to_linker_sync(&mut linker, |x| x)?;
+ p1::add_to_linker_async(&mut linker, |x| x)?;
linker.define_unknown_imports_as_traps(module)?;
- linker.instantiate(store, module)
+ linker.instantiate_async(store, module).await
}
-fn wizen_and_run_wasm(
+async fn wizen_and_run_wasm(
args: &[wasmtime::Val],
expected: i32,
wasm: &[u8],
@@ -46,7 +47,7 @@ fn wizen_and_run_wasm(
wasmprinter::print_bytes(&wasm).unwrap()
);
let mut store = store()?;
- let wasm = wizer.run(&mut store, &wasm, instantiate)?;
+ let wasm = wizer.run(&mut store, &wasm, instantiate).await?;
log::debug!(
"=== Wizened Wasm ==========================================================\n\
{}\n\
@@ -68,16 +69,16 @@ fn wizen_and_run_wasm(
.define_name(&mut store, "f", thunk)?
.define(&mut store, "x", "f", thunk)?;
- p1::add_to_linker_sync(&mut linker, |wasi| wasi)?;
+ p1::add_to_linker_async(&mut linker, |wasi| wasi)?;
- let instance = linker.instantiate(&mut store, &module)?;
+ let instance = linker.instantiate_async(&mut store, &module).await?;
let run = instance
.get_func(&mut store, "run")
.ok_or_else(|| anyhow::anyhow!("the test Wasm module does not export a `run` function"))?;
let mut actual = vec![wasmtime::Val::I32(0)];
- run.call(&mut store, args, &mut actual)?;
+ run.call_async(&mut store, args, &mut actual).await?;
anyhow::ensure!(actual.len() == 1, "expected one result");
let actual = match actual[0] {
wasmtime::Val::I32(x) => x,
@@ -91,7 +92,7 @@ fn wizen_and_run_wasm(
Ok(())
}
-fn fails_wizening(wat: &str) -> Result<()> {
+async fn fails_wizening(wat: &str) -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(wat)?;
@@ -105,38 +106,42 @@ fn fails_wizening(wat: &str) -> Result<()> {
.context("initial Wasm should be valid")?;
anyhow::ensure!(
- get_wizer().run(&mut store()?, &wasm, instantiate).is_err(),
+ get_wizer()
+ .run(&mut store()?, &wasm, instantiate)
+ .await
+ .is_err(),
"Expected an error when wizening, but didn't get one"
);
Ok(())
}
-#[test]
-fn basic_global() -> Result<()> {
+#[tokio::test]
+async fn basic_global() -> Result<()> {
run_wat(
&[],
42,
r#"
(module
(global $g (mut i32) i32.const 0)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 42
global.set $g)
(func (export "run") (result i32)
global.get $g))
"#,
)
+ .await
}
-#[test]
-fn basic_memory() -> Result<()> {
+#[tokio::test]
+async fn basic_memory() -> Result<()> {
run_wat(
&[],
42,
r#"
(module
(memory 1)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 0
i32.const 42
i32.store offset=1337)
@@ -145,10 +150,11 @@ fn basic_memory() -> Result<()> {
i32.load offset=1337))
"#,
)
+ .await
}
-#[test]
-fn multi_memory() -> Result<()> {
+#[tokio::test]
+async fn multi_memory() -> Result<()> {
run_wat(
&[],
42,
@@ -156,7 +162,7 @@ fn multi_memory() -> Result<()> {
(module
(memory $m1 1)
(memory $m2 1)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 0
i32.const 41
i32.store $m1 offset=1337
@@ -171,39 +177,43 @@ fn multi_memory() -> Result<()> {
i32.add))
"#,
)
+ .await
}
-#[test]
-fn reject_imported_memory() -> Result<()> {
+#[tokio::test]
+async fn reject_imported_memory() -> Result<()> {
fails_wizening(
r#"
(module
(import "" "" (memory 1)))
"#,
)
+ .await
}
-#[test]
-fn reject_imported_global() -> Result<()> {
+#[tokio::test]
+async fn reject_imported_global() -> Result<()> {
fails_wizening(
r#"
(module
(import "" "" (global i32)))
"#,
)
+ .await
}
-#[test]
-fn reject_imported_table() -> Result<()> {
+#[tokio::test]
+async fn reject_imported_table() -> Result<()> {
fails_wizening(
r#"
(module
(import "" "" (table 0 externref)))
"#,
)
+ .await
}
-#[test]
-fn reject_table_copy() -> Result<()> {
+#[tokio::test]
+async fn reject_table_copy() -> Result<()> {
let result = run_wat(
&[],
42,
@@ -224,7 +234,8 @@ fn reject_table_copy() -> Result<()> {
(elem (i32.const 0) $f $g $h)
)
"#,
- );
+ )
+ .await;
assert!(result.is_err());
let err = result.unwrap_err();
@@ -236,8 +247,8 @@ fn reject_table_copy() -> Result<()> {
Ok(())
}
-#[test]
-fn reject_table_get_set() -> Result<()> {
+#[tokio::test]
+async fn reject_table_get_set() -> Result<()> {
let wat = r#"
(module
(table 3 funcref)
@@ -259,7 +270,7 @@ fn reject_table_get_set() -> Result<()> {
let _ = env_logger::try_init();
let wizer = Wizer::new();
let wasm = wat_to_wasm(wat)?;
- let result = wizen_and_run_wasm(&[], 42, &wasm, wizer);
+ let result = wizen_and_run_wasm(&[], 42, &wasm, wizer).await;
assert!(result.is_err());
@@ -273,8 +284,8 @@ fn reject_table_get_set() -> Result<()> {
Ok(())
}
-#[test]
-fn reject_table_get_set_with_reference_types_enabled() -> Result<()> {
+#[tokio::test]
+async fn reject_table_get_set_with_reference_types_enabled() -> Result<()> {
let result = run_wat(
&[],
42,
@@ -294,7 +305,8 @@ fn reject_table_get_set_with_reference_types_enabled() -> Result<()> {
(elem (i32.const 0) $f $g $h)
)"#,
- );
+ )
+ .await;
assert!(result.is_err());
let err = result.unwrap_err();
@@ -306,8 +318,8 @@ fn reject_table_get_set_with_reference_types_enabled() -> Result<()> {
Ok(())
}
-#[test]
-fn reject_table_grow_with_reference_types_enabled() -> anyhow::Result<()> {
+#[tokio::test]
+async fn reject_table_grow_with_reference_types_enabled() -> anyhow::Result<()> {
let wat = r#"
(module
(elem declare func $f)
@@ -323,7 +335,7 @@ fn reject_table_grow_with_reference_types_enabled() -> anyhow::Result<()> {
let _ = env_logger::try_init();
let wizer = Wizer::new();
let wasm = wat_to_wasm(wat)?;
- let result = wizen_and_run_wasm(&[], 42, &wasm, wizer);
+ let result = wizen_and_run_wasm(&[], 42, &wasm, wizer).await;
assert!(result.is_err());
@@ -336,8 +348,8 @@ fn reject_table_grow_with_reference_types_enabled() -> anyhow::Result<()> {
Ok(())
}
-#[test]
-fn indirect_call_with_reference_types() -> anyhow::Result<()> {
+#[tokio::test]
+async fn indirect_call_with_reference_types() -> anyhow::Result<()> {
let wat = r#"
(module
(type $sig (func (result i32)))
@@ -347,7 +359,7 @@ fn indirect_call_with_reference_types() -> anyhow::Result<()> {
(func $f (type $sig)
i32.const 42
)
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
(func (export "run") (result i32)
i32.const 0
call_indirect $table1 (type $sig)
@@ -357,11 +369,11 @@ fn indirect_call_with_reference_types() -> anyhow::Result<()> {
let _ = env_logger::try_init();
let wizer = Wizer::new();
let wasm = wat_to_wasm(wat)?;
- wizen_and_run_wasm(&[], 42, &wasm, wizer)
+ wizen_and_run_wasm(&[], 42, &wasm, wizer).await
}
-#[test]
-fn reject_table_init() -> Result<()> {
+#[tokio::test]
+async fn reject_table_init() -> Result<()> {
let result = run_wat(
&[],
42,
@@ -382,7 +394,8 @@ fn reject_table_init() -> Result<()> {
table.init $elem)
)
"#,
- );
+ )
+ .await;
assert!(result.is_err());
let err = result.unwrap_err();
@@ -394,8 +407,8 @@ fn reject_table_init() -> Result<()> {
Ok(())
}
-#[test]
-fn reject_elem_drop() -> Result<()> {
+#[tokio::test]
+async fn reject_elem_drop() -> Result<()> {
let result = run_wat(
&[],
42,
@@ -413,7 +426,8 @@ fn reject_elem_drop() -> Result<()> {
elem.drop $elem)
)
"#,
- );
+ )
+ .await;
assert!(result.is_err());
let err = result.unwrap_err();
@@ -425,8 +439,8 @@ fn reject_elem_drop() -> Result<()> {
Ok(())
}
-#[test]
-fn reject_data_drop() -> Result<()> {
+#[tokio::test]
+async fn reject_data_drop() -> Result<()> {
let result = run_wat(
&[],
42,
@@ -439,7 +453,8 @@ fn reject_data_drop() -> Result<()> {
data.drop $data)
)
"#,
- );
+ )
+ .await;
assert!(result.is_err());
let err = result.unwrap_err();
@@ -451,8 +466,8 @@ fn reject_data_drop() -> Result<()> {
Ok(())
}
-#[test]
-fn rust_regex() -> Result<()> {
+#[tokio::test]
+async fn rust_regex() -> Result<()> {
let status = Command::new("cargo")
.args(&["build", "--target=wasm32-wasip1", "-q"])
.current_dir("./tests/regex-test")
@@ -468,10 +483,11 @@ fn rust_regex() -> Result<()> {
.expect("failed to read regex test case"),
get_wizer(),
)
+ .await
}
-#[test]
-fn data_segment_at_end_of_memory() -> Result<()> {
+#[tokio::test]
+async fn data_segment_at_end_of_memory() -> Result<()> {
// Test that we properly synthesize data segments for data at the end of
// memory.
run_wat(
@@ -480,7 +496,7 @@ fn data_segment_at_end_of_memory() -> Result<()> {
r#"
(module
(memory 1)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
;; Store 42 to the last byte in memory.
i32.const 0
i32.const 42
@@ -493,10 +509,11 @@ fn data_segment_at_end_of_memory() -> Result<()> {
)
"#,
)
+ .await
}
-#[test]
-fn too_many_data_segments_for_engines() -> Result<()> {
+#[tokio::test]
+async fn too_many_data_segments_for_engines() -> Result<()> {
run_wat(
&[],
42,
@@ -518,7 +535,7 @@ fn too_many_data_segments_for_engines() -> Result<()> {
;; let num_pages = round_up(max_segments * wizer_min_gap / wasm_page_size);
(memory 10)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
(local i32)
loop
(i32.ge_u (local.get 0) (i32.const 655360)) ;; 10 * wasm_page_size
@@ -538,13 +555,14 @@ fn too_many_data_segments_for_engines() -> Result<()> {
)
"#,
)
+ .await
}
-#[test]
-fn rename_functions() -> Result<()> {
+#[tokio::test]
+async fn rename_functions() -> Result<()> {
let wat = r#"
(module
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
(func (export "func_a") (result i32)
i32.const 1)
(func (export "func_b") (result i32)
@@ -557,7 +575,7 @@ fn rename_functions() -> Result<()> {
let mut wizer = Wizer::new();
wizer.func_rename("func_a", "func_b");
wizer.func_rename("func_b", "func_c");
- let wasm = wizer.run(&mut store()?, &wasm, instantiate)?;
+ let wasm = wizer.run(&mut store()?, &wasm, instantiate).await?;
let wat = wasmprinter::print_bytes(&wasm)?;
let expected_wat = r#"
@@ -583,8 +601,8 @@ fn rename_functions() -> Result<()> {
Ok(())
}
-#[test]
-fn wasi_reactor() -> anyhow::Result<()> {
+#[tokio::test]
+async fn wasi_reactor() -> anyhow::Result<()> {
run_wat(
&[],
42,
@@ -595,7 +613,7 @@ fn wasi_reactor() -> anyhow::Result<()> {
i32.const 6
global.set $g
)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
global.get $g
i32.const 7
i32.mul
@@ -606,10 +624,11 @@ fn wasi_reactor() -> anyhow::Result<()> {
)
"#,
)
+ .await
}
-#[test]
-fn wasi_reactor_initializer_as_init_func() -> anyhow::Result<()> {
+#[tokio::test]
+async fn wasi_reactor_initializer_as_init_func() -> anyhow::Result<()> {
let wat = r#"
(module
(global $g (mut i32) i32.const 0)
@@ -629,11 +648,11 @@ fn wasi_reactor_initializer_as_init_func() -> anyhow::Result<()> {
wizer.init_func("_initialize");
let wasm = wat_to_wasm(wat)?;
// we expect `_initialize` to be called _exactly_ once
- wizen_and_run_wasm(&[], 1, &wasm, wizer)
+ wizen_and_run_wasm(&[], 1, &wasm, wizer).await
}
-#[test]
-fn wasi_reactor_initializer_with_keep_init() -> anyhow::Result<()> {
+#[tokio::test]
+async fn wasi_reactor_initializer_with_keep_init() -> anyhow::Result<()> {
let wat = r#"
(module
(global $g (mut i32) i32.const 0)
@@ -641,7 +660,7 @@ fn wasi_reactor_initializer_with_keep_init() -> anyhow::Result<()> {
i32.const 1
global.set $g
)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 2
global.set $g)
(func (export "run") (result i32)
@@ -654,49 +673,51 @@ fn wasi_reactor_initializer_with_keep_init() -> anyhow::Result<()> {
wizer.keep_init_func(true);
let wasm = wat_to_wasm(wat)?;
// we expect `_initialize` to be un-exported and not called at run
- wizen_and_run_wasm(&[], 2, &wasm, wizer)
+ wizen_and_run_wasm(&[], 2, &wasm, wizer).await
}
-#[test]
-fn call_undefined_import_function_during_init() -> Result<()> {
+#[tokio::test]
+async fn call_undefined_import_function_during_init() -> Result<()> {
fails_wizening(
r#"
(module
(import "x" "f" (func $import))
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
(call $import)
)
)
"#,
)
+ .await
}
-#[test]
-fn allow_undefined_import_function() -> Result<()> {
+#[tokio::test]
+async fn allow_undefined_import_function() -> Result<()> {
run_wat(
&[],
42,
r#"
(module
(import "x" "f" (func $import))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
(func (export "run") (result i32)
i32.const 42
)
)
"#,
)
+ .await
}
-#[test]
-fn accept_bulk_memory_copy() -> Result<()> {
+#[tokio::test]
+async fn accept_bulk_memory_copy() -> Result<()> {
run_wat(
&[],
('h' as i32) + ('w' as i32),
r#"
(module
(memory $memory (data "hello, wizer!"))
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 42 ;; dst
i32.const 0 ;; src
i32.const 13 ;; size
@@ -709,10 +730,11 @@ fn accept_bulk_memory_copy() -> Result<()> {
i32.add))
"#,
)
+ .await
}
-#[test]
-fn accept_bulk_memory_data_count() -> Result<()> {
+#[tokio::test]
+async fn accept_bulk_memory_data_count() -> Result<()> {
let mut module = wasm_encoder::Module::new();
let mut types = wasm_encoder::TypeSection::new();
types.ty().func_type(&wasm_encoder::FuncType::new(
@@ -741,7 +763,7 @@ fn accept_bulk_memory_data_count() -> Result<()> {
let mut exports = wasm_encoder::ExportSection::new();
exports.export("run", wasm_encoder::ExportKind::Func, 0);
- exports.export("wizer.initialize", wasm_encoder::ExportKind::Func, 1);
+ exports.export("wizer-initialize", wasm_encoder::ExportKind::Func, 1);
module.section(&exports);
module.section(&wasm_encoder::DataCountSection { count: 2 });
@@ -765,19 +787,21 @@ fn accept_bulk_memory_data_count() -> Result<()> {
data.active(0, &ConstExpr::i32_const(4), vec![5, 6, 7, 8]);
module.section(&data);
- wizen_and_run_wasm(&[], 42, &module.finish(), get_wizer()).unwrap();
+ wizen_and_run_wasm(&[], 42, &module.finish(), get_wizer())
+ .await
+ .unwrap();
Ok(())
}
-#[test]
-fn accept_bulk_memory_fill() -> Result<()> {
+#[tokio::test]
+async fn accept_bulk_memory_fill() -> Result<()> {
run_wat(
&[],
77 + 77,
r#"
(module
(memory 1)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 42 ;; dst
i32.const 77 ;; value
i32.const 13 ;; size
@@ -790,10 +814,11 @@ fn accept_bulk_memory_fill() -> Result<()> {
i32.add))
"#,
)
+ .await
}
-#[test]
-fn accept_bulk_memory_init() -> Result<()> {
+#[tokio::test]
+async fn accept_bulk_memory_init() -> Result<()> {
run_wat(
&[],
('h' as i32) + ('w' as i32),
@@ -801,7 +826,7 @@ fn accept_bulk_memory_init() -> Result<()> {
(module
(memory 1)
(data $data "hello, wizer!")
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 42 ;; dst
i32.const 0 ;; offset
i32.const 13 ;; size
@@ -814,17 +839,18 @@ fn accept_bulk_memory_init() -> Result<()> {
i32.add))
"#,
)
+ .await
}
-#[test]
-fn accept_simd128() -> Result<()> {
+#[tokio::test]
+async fn accept_simd128() -> Result<()> {
run_wat(
&[],
49,
r#"
(module
(global $g (mut v128) (v128.const i32x4 2 3 5 7))
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
global.get $g
global.get $g
i32x4.mul
@@ -834,16 +860,17 @@ fn accept_simd128() -> Result<()> {
i32x4.extract_lane 3))
"#,
)
+ .await
}
-#[test]
-fn relaxed_simd_deterministic() -> Result<()> {
+#[tokio::test]
+async fn relaxed_simd_deterministic() -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(
r#"
(module
(global $g (mut i32) i32.const 0)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
(v128.const f32x4 2796203.5 0.0 0.0 0.0)
(v128.const f32x4 3.0 0.0 0.0 0.0)
(v128.const f32x4 8388611.0 0.0 0.0 0.0)
@@ -861,11 +888,11 @@ fn relaxed_simd_deterministic() -> Result<()> {
// We'll get 0x4b000003 if we have the deterministic `relaxed_madd`
// semantics. We might get 0x4b000002 if we don't.
- wizen_and_run_wasm(&[], 0x4b800003, &wasm, wizer)
+ wizen_and_run_wasm(&[], 0x4b800003, &wasm, wizer).await
}
-#[test]
-fn reject_mutable_globals_of_reference_types() -> Result<()> {
+#[tokio::test]
+async fn reject_mutable_globals_of_reference_types() -> Result<()> {
// Non-mutable globals are fine
run_wat(
&[],
@@ -873,27 +900,29 @@ fn reject_mutable_globals_of_reference_types() -> Result<()> {
r#"
(module
(global funcref (ref.null func))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
(func (export "run") (result i32) i32.const 42)
)
"#,
- )?;
+ )
+ .await?;
// Mutable globals are not fine
fails_wizening(
r#"
(module
(global (mut funcref) (ref.null func))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
(func (export "run") (result i32) i32.const 42)
)
"#,
- )?;
+ )
+ .await?;
Ok(())
}
-#[test]
-fn mixture_of_globals() -> Result<()> {
+#[tokio::test]
+async fn mixture_of_globals() -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(
r#"
@@ -902,7 +931,7 @@ fn mixture_of_globals() -> Result<()> {
(global $g2 i32 i32.const 2)
(global $g3 (mut i32) i32.const 3)
(global $g4 i32 i32.const 4)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
(global.set $g1 (i32.const 42))
(global.set $g3 (i32.const 43))
)
@@ -919,18 +948,18 @@ fn mixture_of_globals() -> Result<()> {
"#,
)?;
let wizer = get_wizer();
- wizen_and_run_wasm(&[], 42 + 2 + 43 + 4, &wasm, wizer)
+ wizen_and_run_wasm(&[], 42 + 2 + 43 + 4, &wasm, wizer).await
}
-#[test]
-fn memory_init_and_data_segments() -> Result<()> {
+#[tokio::test]
+async fn memory_init_and_data_segments() -> Result<()> {
let _ = env_logger::try_init();
let wasm = wat_to_wasm(
r#"
(module
(memory 1)
- (func (export "wizer.initialize")
+ (func (export "wizer-initialize")
i32.const 2
i32.const 0
i32.const 2
@@ -961,5 +990,5 @@ fn memory_init_and_data_segments() -> Result<()> {
"#,
)?;
let wizer = get_wizer();
- wizen_and_run_wasm(&[], 0x02010403 + 0x06050201, &wasm, wizer)
+ wizen_and_run_wasm(&[], 0x02010403 + 0x06050201, &wasm, wizer).await
}
diff --git a/crates/wizer/tests/regex-test/src/lib.rs b/crates/wizer/tests/regex-test/src/lib.rs
index 6446f15594..4329389825 100644
--- a/crates/wizer/tests/regex-test/src/lib.rs
+++ b/crates/wizer/tests/regex-test/src/lib.rs
@@ -4,7 +4,7 @@ use std::sync::LazyLock;
/// A regex that matches numbers that start with "1".
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^1\d*$").unwrap());
-#[unsafe(export_name = "wizer.initialize")]
+#[unsafe(export_name = "wizer-initialize")]
pub fn init() {
LazyLock::force(&REGEX);
}
diff --git a/src/commands/run.rs b/src/commands/run.rs
index 6c1f411b92..3fb885ae02 100644
--- a/src/commands/run.rs
+++ b/src/commands/run.rs
@@ -105,16 +105,24 @@ impl RunCommand {
/// Executes the command.
#[cfg(feature = "run")]
pub fn execute(mut self) -> Result<()> {
- self.run.common.init_logging()?;
+ let runtime = tokio::runtime::Builder::new_multi_thread()
+ .enable_time()
+ .enable_io()
+ .build()?;
- let engine = self.new_engine()?;
- let main = self
- .run
- .load_module(&engine, self.module_and_args[0].as_ref())?;
- let (mut store, mut linker) = self.new_store_and_linker(&engine, &main)?;
+ runtime.block_on(async {
+ self.run.common.init_logging()?;
- self.instantiate_and_run(&engine, &mut linker, &main, &mut store)?;
- Ok(())
+ let engine = self.new_engine()?;
+ let main = self
+ .run
+ .load_module(&engine, self.module_and_args[0].as_ref())?;
+ let (mut store, mut linker) = self.new_store_and_linker(&engine, &main)?;
+
+ self.instantiate_and_run(&engine, &mut linker, &main, &mut store)
+ .await?;
+ Ok(())
+ })
}
/// Creates a new `Engine` with the configuration for this command.
@@ -207,80 +215,72 @@ impl RunCommand {
/// This applies all configuration within `self`, such as timeouts and
/// profiling, and performs the execution. The resulting instance is
/// returned.
- pub fn instantiate_and_run(
+ pub async fn instantiate_and_run(
&self,
engine: &Engine,
linker: &mut CliLinker,
main: &RunTarget,
store: &mut Store<Host>,
) -> Result<CliInstance> {
- // Always run the module asynchronously to ensure that the module can be
- // interrupted, even if it is blocking on I/O or a timeout or something.
- let runtime = tokio::runtime::Builder::new_multi_thread()
- .enable_time()
- .enable_io()
- .build()?;
let dur = self
.run
.common
.wasm
.timeout
.unwrap_or(std::time::Duration::MAX);
- let result = runtime.block_on(async {
- tokio::time::timeout(dur, async {
- let mut profiled_modules: Vec<(String, Module)> = Vec::new();
- if let RunTarget::Core(m) = &main {
- profiled_modules.push(("".to_string(), m.clone()));
- }
+ let result = tokio::time::timeout(dur, async {
+ let mut profiled_modules: Vec<(String, Module)> = Vec::new();
+ if let RunTarget::Core(m) = &main {
+ profiled_modules.push(("".to_string(), m.clone()));
+ }
- // Load the preload wasm modules.
- for (name, path) in self.preloads.modules.iter() {
- // Read the wasm module binary either as `*.wat` or a raw binary
- let preload_target = self.run.load_module(&engine, path)?;
- let preload_module = match preload_target {
- RunTarget::Core(m) => m,
- #[cfg(feature = "component-model")]
- RunTarget::Component(_) => {
- bail!("components cannot be loaded with `--preload`")
- }
- };
- profiled_modules.push((name.to_string(), preload_module.clone()));
-
- // Add the module's functions to the linker.
- match linker {
- #[cfg(feature = "cranelift")]
- CliLinker::Core(linker) => {
- linker
- .module_async(&mut *store, name, &preload_module)
- .await
- .context(format!(
- "failed to process preload `{}` at `{}`",
- name,
- path.display()
- ))?;
- }
- #[cfg(not(feature = "cranelift"))]
- CliLinker::Core(_) => {
- bail!("support for --preload disabled at compile time");
- }
- #[cfg(feature = "component-model")]
- CliLinker::Component(_) => {
- bail!("--preload cannot be used with components");
- }
+ // Load the preload wasm modules.
+ for (name, path) in self.preloads.modules.iter() {
+ // Read the wasm module binary either as `*.wat` or a raw binary
+ let preload_target = self.run.load_module(&engine, path)?;
+ let preload_module = match preload_target {
+ RunTarget::Core(m) => m,
+ #[cfg(feature = "component-model")]
+ RunTarget::Component(_) => {
+ bail!("components cannot be loaded with `--preload`")
+ }
+ };
+ profiled_modules.push((name.to_string(), preload_module.clone()));
+
+ // Add the module's functions to the linker.
+ match linker {
+ #[cfg(feature = "cranelift")]
+ CliLinker::Core(linker) => {
+ linker
+ .module_async(&mut *store, name, &preload_module)
+ .await
+ .context(format!(
+ "failed to process preload `{}` at `{}`",
+ name,
+ path.display()
+ ))?;
+ }
+ #[cfg(not(feature = "cranelift"))]
+ CliLinker::Core(_) => {
+ bail!("support for --preload disabled at compile time");
+ }
+ #[cfg(feature = "component-model")]
+ CliLinker::Component(_) => {
+ bail!("--preload cannot be used with components");
}
}
+ }
- self.load_main_module(store, linker, &main, profiled_modules)
- .await
- .with_context(|| {
- format!(
- "failed to run main module `{}`",
- self.module_and_args[0].to_string_lossy()
- )
- })
- })
- .await
- });
+ self.load_main_module(store, linker, &main, profiled_modules)
+ .await
+ .with_context(|| {
+ format!(
+ "failed to run main module `{}`",
+ self.module_and_args[0].to_string_lossy()
+ )
+ })
+ })
+ .await;
// Load the main wasm module.
let instance = match result.unwrap_or_else(|elapsed| {
@@ -615,6 +615,7 @@ impl RunCommand {
let mut results = vec![Val::Bool(false); func_type.results().len()];
func.call_async(&mut *store, &params, &mut results).await?;
+ func.post_return_async(&mut *store).await?;
println!("{}", DisplayFuncResults(&results));
diff --git a/src/commands/wizer.rs b/src/commands/wizer.rs
index 4d47bfa2b4..0664ee314b 100644
--- a/src/commands/wizer.rs
+++ b/src/commands/wizer.rs
@@ -29,11 +29,24 @@ pub struct WizerCommand {
output: Option<PathBuf>,
}
+enum WizerInfo<'a> {
+ Core(wasmtime_wizer::ModuleContext<'a>),
+ #[cfg(feature = "component-model")]
+ Component(wasmtime_wizer::ComponentContext<'a>),
+}
+
impl WizerCommand {
/// Runs the command.
pub fn execute(mut self) -> Result<()> {
self.run.common.init_logging()?;
+ let runtime = tokio::runtime::Builder::new_multi_thread()
+ .enable_time()
+ .enable_io()
+ .build()?;
+ runtime.block_on(self.execute_async())
+ }
+ async fn execute_async(mut self) -> Result<()> {
// By default use deterministic relaxed simd operations to guarantee
// that if relaxed simd operations are used in a module that they always
// produce the same result.
@@ -60,41 +73,79 @@ impl WizerCommand {
#[cfg(feature = "wat")]
let wasm = wat::parse_bytes(&wasm)?;
+ let is_component = wasmparser::Parser::is_component(&wasm);
- // Instrument the input wasm with wizer.
- let (cx, instrumented_wasm) = self.wizer.instrument(&wasm)?;
-
- // Execute a rough equivalent of
- // `wasmtime run --invoke <..> <instrumented-wasm>`
let mut run = RunCommand {
run: self.run,
argv0: None,
- invoke: Some(self.wizer.get_init_func().to_string()),
+ invoke: Some(if is_component {
+ format!("{}()", self.wizer.get_init_func())
+ } else {
+ self.wizer.get_init_func().to_string()
+ }),
module_and_args: vec![self.input.clone().into()],
preloads: self.preloads.clone(),
};
let engine = run.new_engine()?;
- let main = RunTarget::Core(Module::new(&engine, &instrumented_wasm)?);
- let (mut store, mut linker) = run.new_store_and_linker(&engine, &main)?;
- #[allow(
- irrefutable_let_patterns,
- reason = "infallible when components are disabled"
- )]
- let CliInstance::Core(instance) =
- run.instantiate_and_run(&engine, &mut linker, &main, &mut store)?
- else {
- unreachable!()
+
+ // Instrument the input wasm with wizer.
+ let (cx, main) = if is_component {
+ #[cfg(feature = "component-model")]
+ {
+ let (cx, wasm) = self.wizer.instrument_component(&wasm)?;
+ (
+ WizerInfo::Component(cx),
+ RunTarget::Component(wasmtime::component::Component::new(&engine, &wasm)?),
+ )
+ }
+ #[cfg(not(feature = "component-model"))]
+ unreachable!();
+ } else {
+ let (cx, wasm) = self.wizer.instrument(&wasm)?;
+ (
+ WizerInfo::Core(cx),
+ RunTarget::Core(Module::new(&engine, &wasm)?),
+ )
};
+ // Execute a rough equivalent of
+ // `wasmtime run --invoke <..> <instrumented-wasm>`
+ let (mut store, mut linker) = run.new_store_and_linker(&engine, &main)?;
+ let instance = run
+ .instantiate_and_run(&engine, &mut linker, &main, &mut store)
+ .await?;
+
// Use our state to capture a snapshot with Wizer and then serialize
// that.
- let final_wasm = self.wizer.snapshot(
- cx,
- &mut wasmtime_wizer::WasmtimeWizer {
- store: &mut store,
- instance,
- },
- )?;
+ let final_wasm = match (cx, instance) {
+ (WizerInfo::Core(cx), CliInstance::Core(instance)) => {
+ self.wizer
+ .snapshot(
+ cx,
+ &mut wasmtime_wizer::WasmtimeWizer {
+ store: &mut store,
+ instance,
+ },
+ )
+ .await?
+ }
+
+ #[cfg(feature = "component-model")]
+ (WizerInfo::Component(cx), CliInstance::Component(instance)) => {
+ self.wizer
+ .snapshot_component(
+ cx,
+ &mut wasmtime_wizer::WasmtimeWizerComponent {
+ store: &mut store,
+ instance,
+ },
+ )
+ .await?
+ }
+
+ #[cfg(feature = "component-model")]
+ (WizerInfo::Core(_) | WizerInfo::Component(_), _) => unreachable!(),
+ };
match &self.output {
Some(file) => fs::write(file, &final_wasm).context("failed to write output file")?,
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index a84bf616ec..768204f917 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -2855,7 +2855,7 @@ fn wizer_no_imports_by_default() -> Result<()> {
let result = wizen(
&[],
r#"(module
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
)"#,
)?;
assert!(result.status.success());
@@ -2864,7 +2864,7 @@ fn wizer_no_imports_by_default() -> Result<()> {
&[],
r#"(module
(import "foo" "bar" (func))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
)"#,
)?;
assert!(!result.status.success());
@@ -2873,7 +2873,7 @@ fn wizer_no_imports_by_default() -> Result<()> {
&[],
r#"(module
(import "wasi_snapshot_preview1" "fd_write" (func (param i32 i32 i32 i32) (result i32)))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
)"#,
)?;
assert!(!result.status.success());
@@ -2882,10 +2882,53 @@ fn wizer_no_imports_by_default() -> Result<()> {
&["-Scli"],
r#"(module
(import "wasi_snapshot_preview1" "fd_write" (func (param i32 i32 i32 i32) (result i32)))
- (func (export "wizer.initialize"))
+ (func (export "wizer-initialize"))
)"#,
)?;
assert!(result.status.success());
Ok(())
}
+
+#[test]
+fn wizer_components() -> Result<()> {
+ let result = wizen(
+ &[],
+ r#"
+(component
+ (core module $a
+ (global (mut i32) (i32.const 0))
+ (func (export "init")
+ i32.const 100
+ global.set 0)
+ )
+ (core instance $a (instantiate $a))
+ (func (export "wizer-initialize") (canon lift (core func $a "init")))
+)
+ "#,
+ )?;
+ assert!(result.status.success());
+
+ let component_with_wasi = r#"
+(component
+ (import "wasi:cli/[email protected]" (instance
+ (export "get-arguments" (func (result (list string))))
+ ))
+ (core module $a
+ (global (mut i32) (i32.const 0))
+ (func (export "init")
+ i32.const 100
+ global.set 0)
+ )
+ (core instance $a (instantiate $a))
+ (func (export "wizer-initialize") (canon lift (core func $a "init")))
+)
+ "#;
+
+ let result = wizen(&[], component_with_wasi)?;
+ assert!(!result.status.success());
+ let result = wizen(&["-Scli"], component_with_wasi)?;
+ assert!(result.status.success());
+
+ Ok(())
+}