Skip to content

Commit 6fdcfb6

Browse files
committed
fix: ensure ResourceTable contains valid resource upon creation
1 parent efcb604 commit 6fdcfb6

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

host/src/component.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ use crate::{
2424
state::WasmStateImpl, vfs::VfsState,
2525
};
2626

27+
/// Sentinel resource for handle 0 in the resource table. This is needed since
28+
/// wasip2 treats handle 0 as invalid, but wasmtime's resource table starts at
29+
/// 0. We need to reserve handle 0 to be able to use the resource table as a
30+
/// source of truth for handle validity in the WASI p2 implementation. The
31+
/// actual value of the sentinel is not important since we will never access it
32+
/// (and if we do, it's a bug).
33+
struct ResourceTableSentinel;
34+
35+
/// Create new resource table with sentinel.
36+
fn new_resource_table() -> ResourceTable {
37+
let mut table = ResourceTable::new();
38+
// Reserve handle 0 since wasip2 treats it as invalid.
39+
table
40+
.push(ResourceTableSentinel)
41+
.expect("resource table sentinel insert");
42+
table
43+
}
44+
2745
/// Create WASM engine.
2846
fn create_engine<F>(flags: &F) -> DataFusionResult<Engine>
2947
where
@@ -321,7 +339,7 @@ impl WasmComponentInstance {
321339
stderr,
322340
wasi_ctx: wasi_ctx_builder.build().into(),
323341
wasi_http_ctx: WasiHttpCtx::new(),
324-
resource_table: ResourceTable::new(),
342+
resource_table: new_resource_table(),
325343
http_validator: Arc::clone(&permissions.http),
326344
io_rt,
327345
};

0 commit comments

Comments
 (0)