Skip to content

Commit

Permalink
Fix a panic when a module is missing component metadata (#1378)
Browse files Browse the repository at this point in the history
Closes #1375
  • Loading branch information
alexcrichton authored Jan 18, 2024
1 parent 702d4c5 commit c998356
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
30 changes: 21 additions & 9 deletions crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ impl<'a> EncodingState<'a> {
let info = &self.info.info;

// Encode a shim instantiation if needed
let shims = self.encode_shim_instantiation();
let shims = self.encode_shim_instantiation()?;

// For each instance import into the main module create a
// pseudo-core-wasm-module via a bag-of-exports.
Expand Down Expand Up @@ -1117,7 +1117,7 @@ impl<'a> EncodingState<'a> {
Ok(func_index)
}

fn encode_shim_instantiation(&mut self) -> Shims<'a> {
fn encode_shim_instantiation(&mut self) -> Result<Shims<'a>> {
let mut signatures = Vec::new();
let mut ret = Shims::default();
let info = &self.info.info;
Expand All @@ -1138,7 +1138,8 @@ impl<'a> EncodingState<'a> {
&required.funcs,
info.metadata,
&mut signatures,
);
)
.context("failed to register indirect shims for main module")?;
}

// For all required adapter modules a shim is created for each required
Expand All @@ -1159,7 +1160,10 @@ impl<'a> EncodingState<'a> {
&required.funcs,
adapter.info.metadata,
&mut signatures,
);
)
.with_context(|| {
format!("failed to register indirect shims for adapter {adapter_name}")
})?;
}

self.encode_resource_dtors(
Expand Down Expand Up @@ -1205,7 +1209,7 @@ impl<'a> EncodingState<'a> {
);

if ret.list.is_empty() {
return ret;
return Ok(ret);
}

for shim in ret.list.iter() {
Expand Down Expand Up @@ -1302,7 +1306,7 @@ impl<'a> EncodingState<'a> {
self.fixups_module_index = Some(self.component.core_module(&fixups));
self.shim_instance_index = Some(self.component.core_instantiate(shim_module_index, []));

return ret;
return Ok(ret);

fn to_wasm_type(ty: &wasmparser::ValType) -> WasmType {
match ty {
Expand Down Expand Up @@ -1792,7 +1796,7 @@ impl<'a> Shims<'a> {
required: &IndexSet<String>,
metadata: &ModuleMetadata,
sigs: &mut Vec<WasmSignature>,
) {
) -> Result<()> {
let interface = if core_wasm_module == BARE_FUNC_MODULE_NAME {
None
} else {
Expand All @@ -1811,8 +1815,15 @@ impl<'a> Shims<'a> {

Lowering::Indirect { sig, options } => {
sigs.push(sig.clone());
let encoding =
metadata.import_encodings[&(core_wasm_module.to_string(), name.clone())];
let encoding = *metadata
.import_encodings
.get(&(core_wasm_module.to_string(), name.clone()))
.ok_or_else(|| {
anyhow::anyhow!(
"missing component metadata for import of \
`{core_wasm_module}::{name}`"
)
})?;
self.list.push(Shim {
name: shim_name,
debug_name: format!("indirect-{core_wasm_module}-{name}"),
Expand All @@ -1827,6 +1838,7 @@ impl<'a> Shims<'a> {
}
}
}
Ok(())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(import "new" "log" (func $log (param i32 i32)))
(export "log" (func $log))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
world adapt-old {
import new: interface {
log: func(s: string);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
failed to register indirect shims for main module

Caused by:
missing component metadata for import of `new::log`
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(import "new" "log" (func $log (param i32 i32)))
(import "old" "log" (func (param i32 i32)))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package foo:foo;

world module {
}

0 comments on commit c998356

Please sign in to comment.