Skip to content

Commit 902403f

Browse files
Fix issues with stubbing WASI imports (#185)
Specifically, without this patch WASIp2 imports where stubbed even if they are part of the target world, because of a bug in how the target world imports were checked. Additionally, many WASIp1 imports where unconditionally stubbed, making it impossible to use them after resuming a wizer snapshot. With this commit, they're only stubbed if the respective WASIp2 functionality is stubbed as well. Finally, ComponentizeJS now emits a warning for any imports that exist, but end up being stubbed. Without this, it's exceedingly difficult to debug why something doesn't work at runtime.
1 parent 6bddd01 commit 902403f

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

crates/spidermonkey-embedding-splicer/src/stub_wasi.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ where
4848
let mut builder = FunctionBuilder::new(params.as_slice(), results.as_slice());
4949
let _args = stub(&mut builder)?;
5050

51+
println!("Warning: the component import '{full_import}#{name}' isn't listed in the target WIT world, and will abort execution when called.");
5152
builder.replace_import_in_module(module, iid);
5253

5354
return Ok(Some(fid));
@@ -117,7 +118,7 @@ pub fn stub_wasi(
117118
let mut target_world_imports = HashSet::new();
118119

119120
for (key, _) in &target_world.imports {
120-
target_world_imports.insert(resolve.name_world_key(key));
121+
target_world_imports.insert(resolve.name_canonicalized_world_key(key));
121122
}
122123

123124
let mut module = Module::parse(wasm.as_slice(), false).unwrap();
@@ -174,22 +175,6 @@ fn target_world_requires_io(target_world_imports: &HashSet<String>) -> bool {
174175

175176
const PREVIEW1: &str = "wasi_snapshot_preview1";
176177
fn stub_preview1(module: &mut Module) -> Result<()> {
177-
stub_import(module, PREVIEW1, "environ_get", unreachable_stub)?;
178-
stub_import(module, PREVIEW1, "environ_sizes_get", unreachable_stub)?;
179-
stub_import(module, PREVIEW1, "fd_close", unreachable_stub)?;
180-
stub_import(module, PREVIEW1, "fd_fdstat_set_flags", unreachable_stub)?;
181-
stub_import(module, PREVIEW1, "fd_prestat_get", unreachable_stub)?;
182-
stub_import(module, PREVIEW1, "fd_readdir", unreachable_stub)?;
183-
stub_import(module, PREVIEW1, "args_get", unreachable_stub)?;
184-
stub_import(module, PREVIEW1, "args_sizes_get", unreachable_stub)?;
185-
stub_import(module, PREVIEW1, "path_filestat_get", unreachable_stub)?;
186-
stub_import(module, PREVIEW1, "fd_prestat_dir_name", unreachable_stub)?;
187-
stub_import(module, PREVIEW1, "fd_read", unreachable_stub)?;
188-
stub_import(module, PREVIEW1, "fd_seek", unreachable_stub)?;
189-
stub_import(module, PREVIEW1, "path_open", unreachable_stub)?;
190-
stub_import(module, PREVIEW1, "path_remove_directory", unreachable_stub)?;
191-
stub_import(module, PREVIEW1, "path_unlink_file", unreachable_stub)?;
192-
stub_import(module, PREVIEW1, "proc_exit", unreachable_stub)?;
193178
// random comes from prevew2 only in StarlingMonkey
194179
stub_import(module, PREVIEW1, "random_get", unreachable_stub)?;
195180
Ok(())
@@ -1478,6 +1463,18 @@ fn stub_filesystem(module: &mut Module, world_imports: &HashSet<String>) -> Resu
14781463
"[resource-drop]directory-entry-stream",
14791464
unreachable_stub,
14801465
)?;
1466+
1467+
stub_import(module, PREVIEW1, "fd_close", unreachable_stub)?;
1468+
stub_import(module, PREVIEW1, "fd_fdstat_set_flags", unreachable_stub)?;
1469+
stub_import(module, PREVIEW1, "fd_prestat_get", unreachable_stub)?;
1470+
stub_import(module, PREVIEW1, "fd_readdir", unreachable_stub)?;
1471+
stub_import(module, PREVIEW1, "fd_prestat_dir_name", unreachable_stub)?;
1472+
stub_import(module, PREVIEW1, "fd_read", unreachable_stub)?;
1473+
stub_import(module, PREVIEW1, "fd_seek", unreachable_stub)?;
1474+
stub_import(module, PREVIEW1, "path_open", unreachable_stub)?;
1475+
stub_import(module, PREVIEW1, "path_filestat_get", unreachable_stub)?;
1476+
stub_import(module, PREVIEW1, "path_remove_directory", unreachable_stub)?;
1477+
stub_import(module, PREVIEW1, "path_unlink_file", unreachable_stub)?;
14811478
}
14821479

14831480
if !world_imports.contains("wasi:filesystem/preopens@0.2") {
@@ -1513,10 +1510,16 @@ fn stub_cli(module: &mut Module, world_imports: &HashSet<String>) -> Result<()>
15131510
"initial-cwd",
15141511
unreachable_stub,
15151512
)?;
1513+
1514+
stub_import(module, PREVIEW1, "args_get", unreachable_stub)?;
1515+
stub_import(module, PREVIEW1, "args_sizes_get", unreachable_stub)?;
1516+
stub_import(module, PREVIEW1, "environ_get", unreachable_stub)?;
1517+
stub_import(module, PREVIEW1, "environ_sizes_get", unreachable_stub)?;
15161518
}
15171519

15181520
if !world_imports.contains("wasi:cli/exit@0.2") {
15191521
stub_wasi_imports(module, "wasi:cli/exit", "exit", unreachable_stub)?;
1522+
stub_import(module, PREVIEW1, "proc_exit", unreachable_stub)?;
15201523
}
15211524

15221525
if !world_imports.contains("wasi:cli/terminal-stdin@0.2") {

0 commit comments

Comments
 (0)