bytecodealliance / bytecodealliance/wasmtime

Using wasmtime_wasi `Dir` struct can cause panic of `TryFromIntError`

Open
#6,978 7 comments 0 reactions 1 assignee Claimed by @sunfishcode View on GitHub
bug
Dominant language
Rust
Stars
18.6k
Forks
1.8k
Avg merge
1d 16h
Merged PRs (30d)
135

Description

Thanks for filing a bug report! Please fill out the TODOs below.

**Note: if you want to report a security issue, please read our [security policy](https://bytecodealliance.org/security)!**

### Test Case

Here's the WASM code required to reproduce this bug:

```rs
fn main() {
let dir = std::fs::read_dir("/").unwrap();
println!("{:?}", dir.collect::>());
}
```

```
cargo build --target wasm32-wasi --release
```

Compiled in release mode with `debug = true`:
[test.wasm.tgz](https://github.com/bytecodealliance/wasmtime/files/12554668/test.wasm.tgz)

Here's the Rust host code required, modified from the [WASI example](https://docs.wasmtime.dev/examples-rust-wasi.html):

```rs
use wasmtime::*;
use wasmtime_wasi::{ambient_authority, sync::WasiCtxBuilder, Dir};

fn main() -> Result<()> {
// Define the WASI functions globally on the `Config`.
let engine = Engine::default();
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;

let wasm_fs = Dir::open_ambient_dir("/", ambient_authority())
.expect("Filesystem root should be accessible");

// Create a WASI context and put it in a Store; all instances in the store
// share this context. `WasiCtxBuilder` provides a number of ways to
// configure what the target program will have access to.
let wasi = WasiCtxBuilder::new()
.preopened_dir(wasm_fs, "/")
.unwrap()
.inherit_stdio()
.inherit_args()?
.build();
let mut store = Store::new(&engine, wasi);

// Instantiate our module with the imports we've created, and run it.
let module = Module::from_file(&engine, "./test.wasm")?;
linker.module(&mut store, "", &module)?;
// --> Panic occurs here
linker
.get_default(&mut store, "")?
.typed::<(), ()>(&store)?
.call(&mut store, ())?;

Ok(())
}

```

### Steps to Reproduce

* Use `wasmtime_wasi::Dir` to allow access to the root directory
* Attempt to read the root directory from WASM

### Expected Results

The program will print the files in the root directory: `bin`, `dev`, `usr`, etc.

### Actual Results

Program panics:
```
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TryFromIntError(())', <...>/cap-primitives-2.0.0/src/rustix/fs/metadata_ext.rs:167:49
```

### Versions and Environment

Wasmtime version or commit: v12.0.1

Operating system: macOS Ventura 13.5.1

Architecture: arm64 Apple Silicon

### Extra Info

I'm not familiar with ambient authority.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.