donovano opened Issue #1772:
Hi,
Wasi::new
takes aWasiCtx
parameter that can be created with environment args as shown in your examples on WASI and linking. With the wasmtime binary you can pass the--dir
parameter to preopen a directory where the wasm module can read a file from. I did not so far get this to work when instantiating a WASI module using your API. It seems the args passed into theWasiCtx
creation does not end up with directories being pre-opened. The end result is that file IO from a WASI module does not seem to work unless you are calling it from the wasmtime binary. Maybe I'm missing something? Thanks.
sunfishcode commented on Issue #1772:
Can you describe how you're setting up the
WasiCtx
? Are you callingpreopened_dir
to add the preopens?
donovano commented on Issue #1772:
Sure, the following is based on some code of wasmtime in run.rs. I suspect the File::open might be not correct but not sure. The wasm module tries to open a file in the current directory of the executable. Anyway...
... let mut linker = Linker::new(&store); let mut cx1 = wasi_common::WasiCtxBuilder::new(); let mut path = Path::new("."); let mut file = match File::open(&path) { Err(why) => panic!("couldn't open file: {}", why.description()), Ok(file) => {println!("could open ."); file}, }; cx1.preopened_dir(file.try_clone()?, "."); let cx1 = cx1.build()?; let wasi = Wasi::new(&store, cx1); //NOTE the following also did not work when passing the --dir=. parameter to the executable //let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?); wasi.add_to_linker(&mut linker)?; ...
sunfishcode commented on Issue #1772:
Offhand I don't see anything wrong there. Do you know what string the application code is attempting to
open
that isn't working?
donovano commented on Issue #1772:
Here is the simple code section from the wasm module. Note that this works when called from wasmtime (with --dir=.). The file is in the current directory:
... let path = Path::new("somefile.txt"); let mut file = match File::open(path) { ...
I'll fish around for something else that might be causing this if you say that it all looks sound.
donovano commented on Issue #1772:
Solved the problem. start wasn't being called. Thanks for the help.
linouxis9 commented on Issue #1772:
I'm having a similar issue @donovano, may I ask which start was not being called?
Thank you!
Last updated: Dec 23 2024 at 13:07 UTC