A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wizer: Refactor snapshotting of globals/memories (#11857)
Alex Crichton01d9cb812b72
diff --git a/crates/wizer/src/info.rs b/crates/wizer/src/info.rsindex 05df36a9da..f0e778de3d 100644--- a/crates/wizer/src/info.rs+++ b/crates/wizer/src/info.rs@@ -50,6 +50,12 @@ pub struct ModuleContext<'a> {////// If this is `None`, then there are no locally defined memories.defined_memories_index: Option<u32>,++ /// Export names of defined globals injected by the instrumentation pass.+ pub(crate) defined_global_exports: Option<Vec<String>>,++ /// Export names of defined memories injected by the instrumentation pass.+ pub(crate) defined_memory_exports: Option<Vec<String>>,}impl<'a> ModuleContext<'a> {diff --git a/crates/wizer/src/instrument.rs b/crates/wizer/src/instrument.rsindex de6aab24de..2407fe14ae 100644--- a/crates/wizer/src/instrument.rs+++ b/crates/wizer/src/instrument.rs@@ -58,10 +58,12 @@ use wasm_encoder::reencode::{Reencode, RoundtripReencoder};/// export their memories and globals individually, that would disturb the/// modules locally defined memoryies' and globals' indices, which would require/// rewriting the code section, which would break debug info offsets.-pub(crate) fn instrument(module: &ModuleContext<'_>) -> Vec<u8> {+pub(crate) fn instrument(module: &mut ModuleContext<'_>) -> Vec<u8> {log::debug!("Instrumenting the input Wasm");let mut encoder = wasm_encoder::Module::new();+ let mut defined_global_exports = Vec::new();+ let mut defined_memory_exports = Vec::new();for section in module.raw_sections() {match section.id {@@ -84,10 +86,12 @@ pub(crate) fn instrument(module: &ModuleContext<'_>) -> Vec<u8> {for (i, (j, _)) in module.defined_globals().enumerate() {let name = format!("__wizer_global_{i}");exports.export(&name, wasm_encoder::ExportKind::Global, j);+ defined_global_exports.push(name);}for (i, (j, _)) in module.defined_memories().enumerate() {let name = format!("__wizer_memory_{i}");exports.export(&name, wasm_encoder::ExportKind::Memory, j);+ defined_memory_exports.push(name);}encoder.section(&exports);@@ -101,5 +105,8 @@ pub(crate) fn instrument(module: &ModuleContext<'_>) -> Vec<u8> {}}+ module.defined_global_exports = Some(defined_global_exports);+ module.defined_memory_exports = Some(defined_memory_exports);+encoder.finish()}diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rsindex 3db74939db..96224e8428 100644--- a/crates/wizer/src/lib.rs+++ b/crates/wizer/src/lib.rs@@ -203,7 +203,7 @@ impl Wizer {// Make sure we're given valid Wasm from the get go.self.wasm_validate(&wasm)?;- let cx = parse::parse(wasm)?;+ let mut cx = parse::parse(wasm)?;// When wizening core modules directly some imports aren't supported,// so check for those here.@@ -223,7 +223,7 @@ impl Wizer {}}- let instrumented_wasm = instrument::instrument(&cx);+ let instrumented_wasm = instrument::instrument(&mut cx);if cfg!(debug_assertions) {if let Err(error) = self.wasm_validate(&instrumented_wasm) {@@ -254,7 +254,7 @@ impl Wizer {// Parse rename spec.let renames = FuncRenames::parse(&self.func_renames)?;- let snapshot = snapshot::snapshot(&mut *store, &instance);+ let snapshot = snapshot::snapshot(&cx, &mut *store, &instance);let rewritten_wasm = self.rewrite(&mut cx, store, &snapshot, &renames);if cfg!(debug_assertions) {diff --git a/crates/wizer/src/snapshot.rs b/crates/wizer/src/snapshot.rsindex b6d14dd003..1c081cbf13 100644--- a/crates/wizer/src/snapshot.rs+++ b/crates/wizer/src/snapshot.rs@@ -1,3 +1,4 @@+use crate::info::ModuleContext;use rayon::iter::{IntoParallelIterator, ParallelExtend, ParallelIterator};use std::convert::TryFrom;use wasmtime::{AsContext, AsContextMut};@@ -75,11 +76,15 @@ impl DataSegment {/// defaults.//// TODO: when we support reference types, we will have to snapshot tables.-pub fn snapshot(ctx: &mut impl AsContextMut, instance: &wasmtime::Instance) -> Snapshot {+pub fn snapshot(+ module: &ModuleContext<'_>,+ ctx: &mut impl AsContextMut,+ instance: &wasmtime::Instance,+) -> Snapshot {log::debug!("Snapshotting the initialized state");- let globals = snapshot_globals(&mut *ctx, instance);- let (memory_mins, data_segments) = snapshot_memories(&mut *ctx, instance);+ let globals = snapshot_globals(module, &mut *ctx, instance);+ let (memory_mins, data_segments) = snapshot_memories(module, &mut *ctx, instance);Snapshot {globals,@@ -90,28 +95,30 @@ pub fn snapshot(ctx: &mut impl AsContextMut, instance: &wasmtime::Instance) -> S/// Get the initialized values of all globals.fn snapshot_globals(+ module: &ModuleContext<'_>,ctx: &mut impl AsContextMut,instance: &wasmtime::Instance,) -> Vec<wasmtime::Val> {log::debug!("Snapshotting global values");- let mut globals = vec![];- let mut index = 0;- loop {- let name = format!("__wizer_global_{index}");- match instance.get_global(&mut *ctx, &name) {- None => break,- Some(global) => {- globals.push(global.get(&mut *ctx));- index += 1;- }- }- }- globals++ module+ .defined_global_exports+ .as_ref()+ .unwrap()+ .iter()+ .map(|name| {+ let global = instance+ .get_global(&mut *ctx, &name)+ .expect("defined global missing");+ global.get(&mut *ctx)+ })+ .collect()}/// Find the initialized minimum page size of each memory, as well as all/// regions of non-zero memory.fn snapshot_memories(+ module: &ModuleContext<'_>,ctx: &mut impl AsContextMut,instance: &wasmtime::Instance,) -> (Vec<u64>, Vec<DataSegment>) {@@ -120,13 +127,11 @@ fn snapshot_memories(// Find and record non-zero regions of memory (in parallel).let mut memory_mins = vec![];let mut data_segments = vec![];- let mut memory_index = 0;- loop {- let name = format!("__wizer_memory_{memory_index}");- let memory = match instance.get_memory(&mut *ctx, &name) {- None => break,- Some(memory) => memory,- };+ let iter = module+ .defined_memories()+ .zip(module.defined_memory_exports.as_ref().unwrap());+ for ((memory_index, _), name) in iter {+ let memory = instance.get_memory(&mut *ctx, &name).unwrap();memory_mins.push(memory.size(&*ctx));let num_wasm_pages = memory.size(&*ctx);@@ -162,8 +167,6 @@ fn snapshot_memories(}segments}));-- memory_index += 1;}if data_segments.is_empty() {