Skip to content

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

wizer: Use utilties from wasm-encoder for conversion (#11853)

Alex Crichton819587f02c77
diff --git a/crates/wizer/Cargo.toml b/crates/wizer/Cargo.toml
index a1bd246be3..b6fd3c7d08 100644
--- a/crates/wizer/Cargo.toml
+++ b/crates/wizer/Cargo.toml
@@ -29,7 +29,7 @@ anyhow = { workspace = true }
log = { workspace = true }
rayon = { workspace = true }
clap = { workspace = true, optional = true }
-wasm-encoder = { workspace = true }
+wasm-encoder = { workspace = true, features = ['wasmparser'] }
wasmparser = { workspace = true }
wasmprinter = { workspace = true, optional = true }
wasmtime = { workspace = true, features = ['std', 'runtime', 'cranelift', 'cache', 'threads', 'gc', 'gc-null'] }
diff --git a/crates/wizer/src/instrument.rs b/crates/wizer/src/instrument.rs
index 0e69945190..b63b14bf57 100644
--- a/crates/wizer/src/instrument.rs
+++ b/crates/wizer/src/instrument.rs
@@ -1,8 +1,8 @@
//! The initial instrumentation pass.
-use crate::info::{Module, ModuleContext};
-use crate::stack_ext::StackExt;
+use crate::info::ModuleContext;
use wasm_encoder::SectionId;
+use wasm_encoder::reencode::{Reencode, RoundtripReencoder};
/// Instrument the input Wasm so that it exports its memories and globals,
/// allowing us to inspect their state after the module is instantiated and
@@ -61,84 +61,46 @@ use wasm_encoder::SectionId;
pub(crate) fn instrument(cx: &ModuleContext<'_>) -> Vec<u8> {
log::debug!("Instrumenting the input Wasm");
- struct StackEntry<'a> {
- /// This entry's module.
- module: Module,
+ let module = cx.root();
+ let mut encoder = wasm_encoder::Module::new();
- /// The work-in-progress encoding of the new, instrumented module.
- encoder: wasm_encoder::Module,
-
- /// Sections in this module info that we are iterating over.
- sections: std::slice::Iter<'a, wasm_encoder::RawSection<'a>>,
- }
-
- let root = cx.root();
- let mut stack = vec![StackEntry {
- module: root,
- encoder: wasm_encoder::Module::new(),
- sections: root.raw_sections(cx).iter(),
- }];
-
- loop {
- assert!(!stack.is_empty());
-
- match stack.top_mut().sections.next() {
+ for section in module.raw_sections(cx) {
+ match section.id {
// For the exports section, we need to transitively export internal
// state so that we can read the initialized state after we call the
// initialization function.
- Some(section) if section.id == u8::from(SectionId::Export) => {
- let entry = stack.top_mut();
+ id if id == u8::from(SectionId::Export) => {
let mut exports = wasm_encoder::ExportSection::new();
// First, copy over all the original exports.
- for export in entry.module.exports(cx) {
- exports.export(
- export.name,
- match export.kind {
- wasmparser::ExternalKind::Func => wasm_encoder::ExportKind::Func,
- wasmparser::ExternalKind::Table => wasm_encoder::ExportKind::Table,
- wasmparser::ExternalKind::Memory => wasm_encoder::ExportKind::Memory,
- wasmparser::ExternalKind::Global => wasm_encoder::ExportKind::Global,
- wasmparser::ExternalKind::Tag => {
- unreachable!("should have been rejected in validation/parsing")
- }
- },
- export.index,
- );
+ for export in module.exports(cx) {
+ RoundtripReencoder
+ .parse_export(&mut exports, *export)
+ .unwrap();
}
// 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, (j, _)) in entry.module.defined_globals(cx).enumerate() {
+ for (i, (j, _)) in module.defined_globals(cx).enumerate() {
let name = format!("__wizer_global_{i}");
exports.export(&name, wasm_encoder::ExportKind::Global, j);
}
- for (i, (j, _)) in entry.module.defined_memories(cx).enumerate() {
+ for (i, (j, _)) in module.defined_memories(cx).enumerate() {
let name = format!("__wizer_memory_{i}");
exports.export(&name, wasm_encoder::ExportKind::Memory, j);
}
- entry.encoder.section(&exports);
- }
-
- // End of the current module: if this is the root, return the
- // instrumented module, otherwise add it as an entry in its parent's
- // module section.
- None => {
- let entry = stack.pop().unwrap();
-
- if entry.module.is_root() {
- assert!(stack.is_empty());
- return entry.encoder.finish();
- }
+ encoder.section(&exports);
}
// All other sections don't need instrumentation and can be copied
// over directly.
- Some(section) => {
- stack.top_mut().encoder.section(section);
+ _other => {
+ encoder.section(section);
}
}
}
+
+ encoder.finish()
}
diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rs
index d39fe61dc3..dd8733932e 100644
--- a/crates/wizer/src/lib.rs
+++ b/crates/wizer/src/lib.rs
@@ -10,7 +10,6 @@ mod parse;
mod rewrite;
mod snapshot;
mod stack_ext;
-mod translate;
/// Re-export wasmtime so users can align with our version. This is
/// especially useful when providing a custom Linker.
diff --git a/crates/wizer/src/rewrite.rs b/crates/wizer/src/rewrite.rs
index 7abf04e1ad..bc5cad66d9 100644
--- a/crates/wizer/src/rewrite.rs
+++ b/crates/wizer/src/rewrite.rs
@@ -1,7 +1,8 @@
//! Final rewrite pass.
-use crate::{FuncRenames, Wizer, info::ModuleContext, snapshot::Snapshot, translate};
+use crate::{FuncRenames, Wizer, info::ModuleContext, snapshot::Snapshot};
use std::convert::TryFrom;
+use wasm_encoder::reencode::{Reencode, RoundtripReencoder};
use wasm_encoder::{ConstExpr, SectionId};
impl Wizer {
@@ -69,7 +70,7 @@ impl Wizer {
.defined_memories(cx)
.zip(snapshot.memory_mins.iter().copied())
{
- let mut mem = translate::memory_type(mem);
+ let mut mem = RoundtripReencoder.memory_type(mem).unwrap();
mem.minimum = new_min;
memories.memory(mem);
}
@@ -83,7 +84,7 @@ impl Wizer {
for ((_, glob_ty), val) in
module.defined_globals(cx).zip(snapshot.globals.iter())
{
- let glob_ty = translate::global_type(glob_ty);
+ let glob_ty = RoundtripReencoder.global_type(glob_ty).unwrap();
globals.global(
glob_ty,
&match val {
@@ -130,7 +131,7 @@ impl Wizer {
.get(export.name)
.map_or(export.name, |f| f.as_str());
- let kind = translate::export(export.kind);
+ let kind = RoundtripReencoder.export_kind(export.kind).unwrap();
exports.export(field, kind, export.index);
}
encoder.section(&exports);
diff --git a/crates/wizer/src/translate.rs b/crates/wizer/src/translate.rs
deleted file mode 100644
index 697c0108e0..0000000000
--- a/crates/wizer/src/translate.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-//! Type translator functions from `wasmparser` to `wasm_encoder`.
-
-pub(crate) fn val_type(ty: wasmparser::ValType) -> wasm_encoder::ValType {
- use wasm_encoder::ValType;
- use wasmparser::ValType::*;
- match ty {
- I32 => ValType::I32,
- I64 => ValType::I64,
- F32 => ValType::F32,
- F64 => ValType::F64,
- V128 => ValType::V128,
- wasmparser::ValType::FUNCREF => ValType::FUNCREF,
- Ref(_) => panic!("not supported"),
- }
-}
-
-pub(crate) fn global_type(ty: wasmparser::GlobalType) -> wasm_encoder::GlobalType {
- wasm_encoder::GlobalType {
- val_type: val_type(ty.content_type),
- mutable: ty.mutable,
- shared: ty.shared,
- }
-}
-
-pub(crate) fn memory_type(ty: wasmparser::MemoryType) -> wasm_encoder::MemoryType {
- wasm_encoder::MemoryType {
- minimum: ty.initial,
- maximum: ty.maximum.map(|val| val),
- memory64: ty.memory64,
- shared: ty.shared,
- page_size_log2: None,
- }
-}
-
-pub(crate) fn export(kind: wasmparser::ExternalKind) -> wasm_encoder::ExportKind {
- match kind {
- wasmparser::ExternalKind::Func => wasm_encoder::ExportKind::Func,
- wasmparser::ExternalKind::Global => wasm_encoder::ExportKind::Global,
- wasmparser::ExternalKind::Table => wasm_encoder::ExportKind::Table,
- wasmparser::ExternalKind::Memory => wasm_encoder::ExportKind::Memory,
- wasmparser::ExternalKind::Tag => unreachable!(),
- }
-}