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

add wasmtime trap information on function call errors, if available #620

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions native/wasmex/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustler::{
use std::ops::Deref;
use std::sync::Mutex;
use std::thread;
use wasmtime::{Instance, Linker, Module, Val, ValType};
use wasmtime::{Instance, Linker, Module, Trap, Val, ValType};

#[derive(NifMap)]
pub struct LinkedModule {
Expand Down Expand Up @@ -289,8 +289,20 @@ fn execute_function(
match call_result {
Ok(_) => (),
Err(err) => {
let reason = format!("Error during function excecution: `{err}`.");
return make_error_tuple(&thread_env, &reason, from);
let reason = format!("{err}");
if let Ok(trap) = err.downcast::<Trap>() {
return make_error_tuple(
&thread_env,
&format!("Error during function excecution ({trap}): {reason}"),
from,
);
} else {
return make_error_tuple(
&thread_env,
&format!("Error during function excecution: {reason}"),
from,
);
}
}
};
let mut return_values: Vec<Term> = Vec::with_capacity(results_count);
Expand Down
6 changes: 3 additions & 3 deletions test/wasmex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ defmodule WasmexTest do
assert {:error, reason} = Wasmex.call_function(instance, "using_imported_sum3", [1, 2, 3])

assert reason =~
~r/Error during function excecution: `error while executing at wasm backtrace:\n\s*0:\s*0x.* - .*\!using_imported_sum3`\./
~r/Error during function excecution: error while executing at wasm backtrace:\n\s*0:\s*0x.* - .*\!using_imported_sum3/
end
end

Expand All @@ -296,7 +296,7 @@ defmodule WasmexTest do

assert String.starts_with?(
err_msg,
"Error during function excecution: `error while executing at wasm backtrace:"
"Error during function excecution (wasm trap: wasm `unreachable` instruction executed): error while executing at wasm backtrace:"
)
end

Expand Down Expand Up @@ -332,7 +332,7 @@ defmodule WasmexTest do
assert {:error, err_msg} = Wasmex.call_function(pid, :void, [])

assert err_msg =~
~r/Error during function excecution: `error while executing at wasm backtrace:\n.+0:.+0x.+ - .*\!void`\./
~r/Error during function excecution \(wasm trap: all fuel consumed by WebAssembly\): error while executing at wasm backtrace:\n.+0:.+0x.+ - .*\!void/
end
end

Expand Down