I am trying to use wasmtime from my own program to run a module that opens a file for reading. The file is in same folder with the runtime.
I have tried setting up WASI and the linker in the following manner after which I run the Wasm module.
let wasi = Wasi::new(&store, WasiCtx::new(std::env::args()).expect("Wasi Error"));
wasi.add_to_linker(&mut linker)?;
let main_module = Module::from_file(store.engine(), "./example_main.wasm")?;
let main_instance = linker.instantiate(&main_module)?;
linker.instance("example_main", &main_instance)?;
linker.alias("example_main", "env")?;
However, calling fopen in the module returns NULL and perror prints a message saying "Operation not permitted".
The same code in C works fine, so file permissions are unlikely the cause. The file also has 777 permissions.
I would assume that I have some permissions missing from WASI to allow it to access files.
I tried passing different switches to the program ./example --dir=. --dir=absolute/path/to/example/folder ./example -- --dir=. --dir=absolute/path/to/example/folder, but none change the error message.
Pointers to where the issue could be are appreciated.
Tried to use fopen with wasmtime-cli too, but it has the same error, so the problem isnt specific to my code either.
You need to use WasiCtxBuilder and call preopened_dir I think. WasiCtx::new takes the arguments to be passed to the wasm module, not arguments to customize WasiCtx.
It seems that the issue might be with the module itself. It was built with emscripten.
Running the examples from here worked fine, unless compiled with emscripten https://github.com/bytecodealliance/wasmtime/blob/main/docs/WASI-tutorial.md#executing-in-wasmtime-runtime
Posted an issue about it here: https://github.com/emscripten-core/emscripten/issues/12094
Last updated: Dec 06 2025 at 06:05 UTC