Skip to content

Commit

Permalink
add wasmtime trap information on function call errors, if available (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tessi authored Dec 1, 2024
1 parent f0df3e8 commit a8a3bc5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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 @@ -267,7 +267,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 @@ -287,7 +287,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 @@ -323,7 +323,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

0 comments on commit a8a3bc5

Please sign in to comment.