Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit ca1837b

Browse files
test: impprove panic reporting
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent c0dbf46 commit ca1837b

File tree

7 files changed

+46
-15
lines changed

7 files changed

+46
-15
lines changed

crates/cli/src/bin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn run(module: Module, func: Option<String>, args: Vec<WasmValue>) -> Result<()>
117117
let instance = module.instantiate(&mut store)?;
118118

119119
if let Some(func) = func {
120-
let func = instance.get_func(&store, &func)?;
120+
let func = instance.exported_func_by_name(&store, &func)?;
121121
let res = func.call(&mut store, &args)?;
122122
info!("{res:?}");
123123
}

crates/tinywasm/src/instance.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,17 @@ impl ModuleInstance {
5858
&self.0.types[addr as usize]
5959
}
6060

61+
// resolve a function address to the index of the function in the store
62+
pub(crate) fn func_addr(&self, addr: FuncAddr) -> FuncAddr {
63+
self.0.func_addrs[addr as usize]
64+
}
65+
66+
pub(crate) fn func_addrs(&self) -> &[FuncAddr] {
67+
&self.0.func_addrs
68+
}
69+
6170
/// Get an exported function by name
62-
pub fn get_func(&self, store: &Store, name: &str) -> Result<FuncHandle> {
71+
pub fn exported_func_by_name(&self, store: &Store, name: &str) -> Result<FuncHandle> {
6372
if self.0.store_id != store.id() {
6473
return Err(Error::InvalidStore);
6574
}
@@ -90,7 +99,7 @@ impl ModuleInstance {
9099
P: IntoWasmValueTuple,
91100
R: FromWasmValueTuple,
92101
{
93-
let func = self.get_func(store, name)?;
102+
let func = self.exported_func_by_name(store, name)?;
94103
Ok(TypedFuncHandle {
95104
func,
96105
marker: core::marker::PhantomData,

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use traits::*;
1818

1919
impl DefaultRuntime {
2020
pub(crate) fn exec(&self, store: &mut Store, stack: &mut Stack, module: ModuleInstance) -> Result<()> {
21+
log::info!("exports: {:?}", module.exports());
22+
log::info!("func_addrs: {:?}", module.func_addrs());
23+
log::info!("store funcs: {:?}", store.data.funcs.len());
24+
2125
// The current call frame, gets updated inside of exec_one
2226
let mut cf = stack.call_stack.pop()?;
2327

@@ -106,8 +110,9 @@ fn exec_one(
106110
Call(v) => {
107111
debug!("start call");
108112
// prepare the call frame
109-
let func = store.get_func(*v as usize)?;
110-
let func_ty = module.func_ty(*v);
113+
let func_idx = module.func_addr(*v);
114+
let func = store.get_func(func_idx as usize)?;
115+
let func_ty = module.func_ty(func_idx);
111116

112117
debug!("params: {:?}", func_ty.params);
113118
debug!("stack: {:?}", stack.values);

crates/tinywasm/src/store.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,21 @@ impl Store {
124124
Ok(())
125125
}
126126

127+
/// Add functions to the store, returning their addresses in the store
127128
pub(crate) fn add_funcs(&mut self, funcs: Vec<Function>, idx: ModuleInstanceAddr) -> Vec<FuncAddr> {
128-
let mut func_addrs = Vec::with_capacity(funcs.len());
129+
let func_count = self.data.funcs.len();
130+
let mut func_addrs = Vec::with_capacity(func_count);
129131
for (i, func) in funcs.into_iter().enumerate() {
130132
self.data.funcs.push(Rc::new(FunctionInstance {
131133
func,
132134
_module_instance: idx,
133135
}));
134-
func_addrs.push(i as FuncAddr);
136+
func_addrs.push((i + func_count) as FuncAddr);
135137
}
136138
func_addrs
137139
}
138140

141+
/// Get the function at the actual index in the store
139142
pub(crate) fn get_func(&self, addr: usize) -> Result<&Rc<FunctionInstance>> {
140143
self.data
141144
.funcs

crates/tinywasm/tests/testsuite/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ impl TestGroup {
161161
}
162162

163163
fn stats(&self) -> (usize, usize) {
164-
log::error!("stats: {:?}", self.tests);
165-
166164
let mut passed_count = 0;
167165
let mut failed_count = 0;
168166

crates/tinywasm/tests/testsuite/run.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl TestSuite {
5151
debug!("got wat module");
5252

5353
let result = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap()))
54-
.map_err(|e| eyre!("failed to parse module: {:?}", e))
54+
.map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
5555
.and_then(|res| res);
5656

5757
match &result {
@@ -77,7 +77,7 @@ impl TestSuite {
7777
};
7878

7979
let res = catch_unwind_silent(|| parse_module_bytes(&module))
80-
.map_err(|e| eyre!("failed to parse module: {:?}", e))
80+
.map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
8181
.and_then(|res| res);
8282

8383
test_group.add_result(
@@ -96,7 +96,7 @@ impl TestSuite {
9696
message: _,
9797
} => {
9898
let res = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap()))
99-
.map_err(|e| eyre!("failed to parse module: {:?}", e))
99+
.map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
100100
.and_then(|res| res);
101101

102102
test_group.add_result(
@@ -133,7 +133,7 @@ impl TestSuite {
133133
Err(err) => test_group.add_result(
134134
&format!("AssertTrap({})", i),
135135
span.linecol_in(wast),
136-
Err(eyre!("test panicked: {:?}", err)),
136+
Err(eyre!("test panicked: {:?}", try_downcast_panic(err))),
137137
),
138138
Ok(Err(tinywasm::Error::Trap(_))) => {
139139
test_group.add_result(&format!("AssertTrap({})", i), span.linecol_in(wast), Ok(()))
@@ -222,7 +222,7 @@ impl TestSuite {
222222
});
223223

224224
let res = res
225-
.map_err(|e| eyre!("test panicked: {:?}", e.downcast_ref::<&str>()))
225+
.map_err(|e| eyre!("test panicked: {:?}", try_downcast_panic(e)))
226226
.and_then(|r| r);
227227

228228
test_group.add_result(

crates/tinywasm/tests/testsuite/util.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ use std::panic;
33
use eyre::{eyre, Result};
44
use tinywasm_types::{TinyWasmModule, WasmValue};
55

6+
pub fn try_downcast_panic(panic: Box<dyn std::any::Any + Send>) -> String {
7+
let info = panic
8+
.downcast_ref::<panic::PanicInfo>()
9+
.or(None)
10+
.map(|p| p.to_string())
11+
.clone();
12+
let info_string = panic.downcast_ref::<String>().cloned();
13+
let info_str = panic.downcast::<&str>().ok().map(|s| *s);
14+
15+
info.unwrap_or(
16+
info_str
17+
.unwrap_or(&info_string.unwrap_or("unknown panic".to_owned()))
18+
.to_string(),
19+
)
20+
}
21+
622
pub fn exec_fn(
723
module: Option<&TinyWasmModule>,
824
name: &str,
@@ -15,7 +31,7 @@ pub fn exec_fn(
1531
let mut store = tinywasm::Store::new();
1632
let module = tinywasm::Module::from(module);
1733
let instance = module.instantiate(&mut store)?;
18-
instance.get_func(&store, name)?.call(&mut store, args)
34+
instance.exported_func_by_name(&store, name)?.call(&mut store, args)
1935
}
2036

2137
pub fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {

0 commit comments

Comments
 (0)