Skip to content

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

only remove `_initialize` function from module snapshots (#12540)

Joel Dicee69370509fc1
diff --git a/crates/wizer/src/component/rewrite.rs b/crates/wizer/src/component/rewrite.rs
index e20fd1116d..4f7c5fc631 100644
--- a/crates/wizer/src/component/rewrite.rs
+++ b/crates/wizer/src/component/rewrite.rs
@@ -38,7 +38,7 @@ impl Wizer {
// component.
Some(snapshot) => {
let rewritten_wasm =
- self.rewrite(module, snapshot, &FuncRenames::default());
+ self.rewrite(module, snapshot, &FuncRenames::default(), false);
encoder.section(&wasm_encoder::RawSection {
id: wasm_encoder::ComponentSectionId::CoreModule as u8,
data: &rewritten_wasm,
diff --git a/crates/wizer/src/lib.rs b/crates/wizer/src/lib.rs
index b1a6028091..9dfe5f9800 100644
--- a/crates/wizer/src/lib.rs
+++ b/crates/wizer/src/lib.rs
@@ -235,7 +235,7 @@ impl Wizer {
let renames = FuncRenames::parse(&self.func_renames)?;
let snapshot = snapshot::snapshot(&cx, instance).await;
- let rewritten_wasm = self.rewrite(&mut cx, &snapshot, &renames);
+ let rewritten_wasm = self.rewrite(&mut cx, &snapshot, &renames, true);
self.debug_assert_valid_wasm(&rewritten_wasm);
diff --git a/crates/wizer/src/rewrite.rs b/crates/wizer/src/rewrite.rs
index 5f854af485..33083b5467 100644
--- a/crates/wizer/src/rewrite.rs
+++ b/crates/wizer/src/rewrite.rs
@@ -15,6 +15,7 @@ impl Wizer {
module: &mut ModuleContext<'_>,
snapshot: &Snapshot,
renames: &FuncRenames,
+ remove_wasi_initialize: bool,
) -> Vec<u8> {
log::debug!("Rewriting input Wasm to pre-initialized state");
@@ -130,7 +131,9 @@ impl Wizer {
let mut exports = wasm_encoder::ExportSection::new();
for export in module.exports() {
if (export.name == self.get_init_func() && !self.get_keep_init_func())
- || (has_wasi_initialize && export.name == "_initialize")
+ || (remove_wasi_initialize
+ && has_wasi_initialize
+ && export.name == "_initialize")
{
continue;
}
diff --git a/crates/wizer/tests/all/component.rs b/crates/wizer/tests/all/component.rs
index 4c41c83bfc..d4dc744a1a 100644
--- a/crates/wizer/tests/all/component.rs
+++ b/crates/wizer/tests/all/component.rs
@@ -609,3 +609,42 @@ async fn export_is_removed() -> Result<()> {
.collect()
}
}
+
+// For the time being, Wizer should _not_ remove the `_initialize` export if
+// present, even though it will become unreachable by virtual of `start`
+// functions being removed. That's because `wit-component` creates an alias to
+// the `_initialize` function, which would require additional surgery to remove.
+// Ideally, we'd have a general purpose, component-level dead-code-elimination
+// tool to remove all unreachable code, but for now we accept a bit of
+// redundancy.
+#[tokio::test]
+async fn leave_wasip1_initialize() -> Result<()> {
+ wizen_and_run_wasm(
+ 42,
+ r#"(component
+ (core module $m
+ (func (export "init"))
+
+ (func (export "run") (result i32)
+ i32.const 42
+ )
+
+ (func (export "_initialize"))
+ )
+ (core instance $i (instantiate $m))
+ (alias core export $i "_initialize" (core func $initialize))
+ (core module $shim
+ (import "" "_initialize" (func $initialize))
+ (start $initialize)
+ )
+ (core instance $shim (instantiate $shim (with "" (instance
+ (export "_initialize" (func $initialize))
+ ))))
+ (func (export "run") (result u32) (canon lift (core func $i "run")))
+ (func (export "wizer-initialize") (canon lift (core func $i "init")))
+ )"#,
+ )
+ .await?;
+
+ Ok(())
+}