Stream: wasmtime

Topic: ✔ propogate error by invoking wasm func


view this post on Zulip Mossaka (Joe) (Jun 29 2022 at 20:38):

Hey there, I have a wasm module that is written in Rust and it's main function returns a anyhow::Err(). I noticed that if wasmtime API calls the main() function directly, it won't return the correct status code. I then tried to use linker.get_default() and call the function, and I can see the status code returned is 1. Why does instance.call() won't return the same status code?

Calling instance.call()

let instance = linker.instantiate(&mut store, &module)?;
instance
    .get_typed_func::<(i32, i32), i32, _>(&mut store, "main")?
    .call(&mut store, (0, 0))?;
Error: ErrorWithDescription("failed")
(base) ➜  wasi-error git:(main)echo $?
0

The above program does not return the expected status code.

Calling linker.get_default().call()

linker.module(&mut store, "", &module)?;
linker
    .get_default(&mut store, "")?
    .typed::<(), (), _>(&store)?
    .call(&mut store, ())?;
Error: ErrorWithDescription("failed")
Error: Exited with i32 exit status 1
wasm backtrace:
    ...

This program returns the correct status code 1.

view this post on Zulip bjorn3 (Jun 29 2022 at 21:08):

You need to call _start, not main. Otherwise constructors and destructors don't run. In addition main returns an exit code. _start calls exit() if the exit code of main is non-zero. The act of calling exit() results in the error you show.

view this post on Zulip Notification Bot (Jun 29 2022 at 23:31):

mossaka has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC