Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Trigger a trap from the context of an import #1426

Closed
reuvenpo opened this issue May 4, 2020 · 7 comments · Fixed by #1657
Closed

How to Trigger a trap from the context of an import #1426

reuvenpo opened this issue May 4, 2020 · 7 comments · Fixed by #1657
Labels
🧞 needs example The issue needs an example on how to do it ❓ question I've a question!

Comments

@reuvenpo
Copy link

reuvenpo commented May 4, 2020

Summary

This isn't clear to me from the documentation, although i believe the answer would be simple. How does one trigger a trap during an import? The imported functions that are provided to the import macro must return simple types (i32, u32, ...) and can't return a Result of either a Value or a Trap.

Additional details

I would like to basically "throw" a custom error enumeration during import execution which would abort the module, and catch it outside of that context, where i can analyze the thrown error.

@reuvenpo reuvenpo added the ❓ question I've a question! label May 4, 2020
@syrusakbary
Copy link
Member

I think @MarkMcCaskey might be able to help you with that :)

@MarkMcCaskey
Copy link
Contributor

Good question! This is something that should be documented more clearly, in Rust you can do it by returning a Result from a host function:

You can look at our WASI implementation for an example of this: https://github.com/wasmerio/wasmer/blob/master/lib/wasi/src/syscalls/mod.rs#L2506-L2509

We special cased it so that the failure value of a Result will trap with that value!

@reuvenpo
Copy link
Author

reuvenpo commented May 5, 2020

Oh, that's cool :D Thanks for the answer!
Can you also show me how you use the proc_exit function in the import!() macro, and how you catch the error later?

@MarkMcCaskey
Copy link
Contributor

In Wasmer 0.16.* it'll be returned through RuntimeError and can be accessed by doing error.0.downcast::<Type>(...) or using other APIs that work on Box<dyn Any>; this is actually changing in the next release of Wasmer and will be stored in RuntimeError::User() (which contains a Box<dyn Any> that you can try to downcast to your type!

The runtime error will be returned at the site where you called into Wasm.

proc_exit should be a normal import, just func!() (https://github.com/wasmerio/wasmer/blob/master/lib/wasi/src/lib.rs#L218) nothing special needed to make this work

@reuvenpo
Copy link
Author

Thanks for the help on this issue!
This worked in the project I'm working on, so i think it's safe to close the issue now :)

@syrusakbary
Copy link
Member

The API changed a bit preparing to Wasmer 1.0. We will create an example soon

@syrusakbary syrusakbary added the 🧞 needs example The issue needs an example on how to do it label Sep 1, 2020
@reuvenpo
Copy link
Author

reuvenpo commented Sep 1, 2020

CC: @ethanfrey @webmaster128

@bors bors bot closed this as completed in #1657 Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧞 needs example The issue needs an example on how to do it ❓ question I've a question!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants