A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wizer: Partially support nested components (#11898)
Alex Crichton155f82762d8f
diff --git a/crates/wizer/.gitignore b/crates/wizer/.gitignoreindex 196c957bbb..47276c273c 100644--- a/crates/wizer/.gitignore+++ b/crates/wizer/.gitignore@@ -5,4 +5,5 @@ npm/wizer-darwin-x64/npm/wizer-linux-x64/npm/wizer-win32-x64/npm/wizer-linux-s390x/-npm/wizer-linux-arm64/\ No newline at end of file+npm/wizer-linux-arm64/+test.wasmdiff --git a/crates/wizer/src/component/parse.rs b/crates/wizer/src/component/parse.rsindex e3892a86fc..3be39aba79 100644--- a/crates/wizer/src/component/parse.rs+++ b/crates/wizer/src/component/parse.rs@@ -13,7 +13,7 @@ use wasmparser::{/// parsing phase which in theory could be lifted in the future but serve as/// simplifying assumptions for now:///-/// * Nested components are not supported.+/// * Nested components with modules are not supported./// * Imported modules or components are not supported./// * Instantiating a module twice is not supported./// * Component-level start functions are not supported.@@ -21,21 +21,37 @@ use wasmparser::{/// 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);+ let parser = Parser::new(0).parse_all(full_wasm);+ parse_into(Some(&mut component), full_wasm, parser)?;+ Ok(component)+}- while let Some(payload) = parser.next() {+fn parse_into<'a>(+ mut cx: Option<&mut ComponentContext<'a>>,+ full_wasm: &'a [u8],+ mut iter: impl Iterator<Item = wasmparser::Result<Payload<'a>>>,+) -> anyhow::Result<()> {+ let mut stack = Vec::new();+ while let Some(payload) = iter.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);- }+ Payload::ModuleSection { .. } => match &mut cx {+ Some(component) => {+ let info = crate::parse::parse_with(&full_wasm, &mut iter)?;+ component.push_module_section(info);+ }+ None => {+ bail!("nested components with modules not currently supported");+ }+ },// All other sections get pushed raw as-is into the component._ => {- if let Some((id, range)) = payload.as_section() {+ if let Some((id, range)) = payload.as_section()+ && let Some(component) = &mut cx+ {component.push_raw_section(wasm_encoder::RawSection {id,data: &full_wasm[range],@@ -56,26 +72,38 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<}Payload::ComponentSection { .. } => {- bail!("wizer does not currently support nested components");+ stack.push(cx.take());}- Payload::InstanceSection(reader) => {- for instance in reader {- let instance_index = component.inc_core_instances();+ Payload::End(_) => {+ if stack.len() > 0 {+ cx = stack.pop().unwrap();+ }+ }- if let Instance::Instantiate { module_index, .. } = instance? {- match component.core_instantiations.entry(module_index) {- Entry::Vacant(entry) => {- entry.insert(instance_index);+ Payload::InstanceSection(reader) => {+ if let Some(component) = &mut cx {+ 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")+ }}- Entry::Occupied(_) => bail!("modules may be instantiated at most once"),}}}}Payload::ComponentInstanceSection(reader) => {- for _ in reader {- component.inc_instances();+ if let Some(component) = &mut cx {+ for _ in reader {+ component.inc_instances();+ }}}@@ -83,16 +111,22 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<for alias in reader {match alias? {ComponentAlias::CoreInstanceExport { kind, .. } => {- component.inc_core(kind);+ if let Some(component) = &mut cx {+ component.inc_core(kind);+ }}ComponentAlias::InstanceExport { kind, .. } => {validate_item_kind(kind, "aliases")?;- component.inc(kind);+ if let Some(component) = &mut cx {+ component.inc(kind);+ }}ComponentAlias::Outer { kind, .. } => match kind {ComponentOuterAliasKind::CoreType => {}ComponentOuterAliasKind::Type => {- component.inc_types();+ if let Some(component) = &mut cx {+ component.inc_types();+ }}ComponentOuterAliasKind::CoreModule => {bail!("wizer does not currently support module aliases");@@ -109,10 +143,14 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<for function in reader {match function? {CanonicalFunction::Lift { .. } => {- component.inc_funcs();+ if let Some(component) = &mut cx {+ component.inc_funcs();+ }}_ => {- component.inc_core_funcs();+ if let Some(component) = &mut cx {+ component.inc_core_funcs();+ }}}}@@ -122,7 +160,9 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<for import in reader {let kind = import?.ty.kind();validate_item_kind(kind, "imports")?;- component.inc(kind);+ if let Some(component) = &mut cx {+ component.inc(kind);+ }}}@@ -130,13 +170,17 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<for export in reader {let kind = export?.kind;validate_item_kind(kind, "exports")?;- component.inc(kind);+ if let Some(component) = &mut cx {+ component.inc(kind);+ }}}Payload::ComponentTypeSection(reader) => {for _ in reader {- component.inc_types();+ if let Some(component) = &mut cx {+ component.inc_types();+ }}}@@ -151,7 +195,7 @@ pub(crate) fn parse<'a>(full_wasm: &'a [u8]) -> anyhow::Result<ComponentContext<}}- Ok(component)+ Ok(())}fn validate_item_kind(kind: ComponentExternalKind, msg: &str) -> Result<()> {diff --git a/crates/wizer/src/component/rewrite.rs b/crates/wizer/src/component/rewrite.rsindex 2666218a41..e20fd1116d 100644--- a/crates/wizer/src/component/rewrite.rs+++ b/crates/wizer/src/component/rewrite.rs@@ -107,7 +107,11 @@ impl Reencoder<'_> {// indices in case the index is higher than the one that we're// removing.id if id == wasm_encoder::ComponentSectionId::Instance as u8 => {- self.rewrite(encoder, section.data, Self::parse_instance_section);+ self.rewrite(+ encoder,+ section.data,+ Self::parse_component_instance_section,+ );}id if id == wasm_encoder::ComponentSectionId::Alias as u8 => {self.rewrite(encoder, section.data, Self::parse_component_alias_section);diff --git a/crates/wizer/test.wasm b/crates/wizer/test.wasmdeleted file mode 100644index 6635aa9328..0000000000Binary files a/crates/wizer/test.wasm and /dev/null differdiff --git a/crates/wizer/tests/all/component.rs b/crates/wizer/tests/all/component.rsindex c793dff2fc..8fe9b22f8d 100644--- a/crates/wizer/tests/all/component.rs+++ b/crates/wizer/tests/all/component.rs@@ -7,6 +7,10 @@ fn fail_wizening(msg: &str, wasm: &[u8]) -> Result<()> {let _ = env_logger::try_init();let wasm = wat::parse_bytes(wasm)?;+ log::debug!(+ "testing wizening failure for wasm:\n{}",+ wasmprinter::print_bytes(&wasm)?+ );match Wizer::new().instrument_component(&wasm) {Ok(_) => bail!("expected wizening to fail"),Err(e) => {@@ -29,9 +33,40 @@ fn unsupported_constructs() -> Result<()> {)?;fail_wizening(- "does not currently support nested components",+ "nested components with modules not currently supported",+ br#"(component+ (component (core module))+ )"#,+ )?;+ fail_wizening(+ "nested components with modules not currently supported",br#"(component(component)+ (component (core module))+ )"#,+ )?;+ fail_wizening(+ "wizer does not currently support module imports",+ br#"(component+ (component (import "x" (core module)))+ )"#,+ )?;+ fail_wizening(+ "wizer does not currently support module aliases",+ br#"(component+ (core module $a)+ (component+ (core instance (instantiate $a))+ )+ )"#,+ )?;+ fail_wizening(+ "wizer does not currently support component aliases",+ br#"(component+ (component $a)+ (component+ (instance (instantiate $a))+ ))"#,)?;@@ -308,6 +343,44 @@ async fn snapshot_memory() -> Result<()> {Ok(())}+#[tokio::test]+async fn nested_components() -> Result<()> {+ wizen_and_run_wasm(+ 42,+ r#"(component+ (component $a)+ (instance (instantiate $a))+ (instance (export "hi") (instantiate $a))++ (component $b+ (type $t string)+ (import "x" (type (eq $t)))+ (component $a)+ (instance (instantiate $a))+ (instance (export "hi") (instantiate $a))+ )+ (type $x string)+ (instance (instantiate $b+ (with "x" (type $x))+ ))+ (instance (export "hi2") (instantiate $b+ (with "x" (type $x))+ ))++ (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 multiple_modules() -> Result<()> {wizen_and_run_wasm(