Stream: git-wasmtime

Topic: wasmtime / Issue #1772 WasiCtx and preopen


view this post on Zulip Wasmtime GitHub notifications bot (May 27 2020 at 19:20):

donovano opened Issue #1772:

Hi,

Wasi::new takes a WasiCtx 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 the WasiCtx 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.

view this post on Zulip Wasmtime GitHub notifications bot (May 27 2020 at 19:47):

sunfishcode commented on Issue #1772:

Can you describe how you're setting up the WasiCtx? Are you calling preopened_dir to add the preopens?

view this post on Zulip Wasmtime GitHub notifications bot (May 27 2020 at 20:29):

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)?;
   ...

view this post on Zulip Wasmtime GitHub notifications bot (May 27 2020 at 22:28):

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?

view this post on Zulip Wasmtime GitHub notifications bot (May 27 2020 at 22:43):

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.

view this post on Zulip Wasmtime GitHub notifications bot (May 28 2020 at 12:24):

donovano commented on Issue #1772:

Solved the problem. start wasn't being called. Thanks for the help.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 06 2020 at 23:05):

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: Nov 22 2024 at 16:03 UTC