Skip to content

Commit 73b95b6

Browse files
authored
Merge pull request #640 from CosmWasm/aw/improve-panics
Improve panic messages when VM panicks
2 parents c3faa7b + d78ccbd commit 73b95b6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

libwasmvm/src/handle_vm_panic.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ use std::any::Any;
55
/// We want to provide as much debug information as possible
66
/// as those cases are not expected to happen during healthy operations.
77
pub fn handle_vm_panic(what: &str, err: Box<dyn Any + Send + 'static>) {
8+
let err = match (err.downcast_ref::<&str>(), err.downcast_ref::<String>()) {
9+
(Some(str), ..) => *str,
10+
(.., Some(str)) => str,
11+
(None, None) => "[unusable panic payload]",
12+
};
13+
814
eprintln!("Panic in {what}:");
9-
eprintln!("{err:?}"); // Does not show useful information, see https://users.rust-lang.org/t/return-value-from-catch-unwind-is-a-useless-any/89134/6
15+
eprintln!("{err:?}");
1016
eprintln!(
1117
"This indicates a panic in during the operations of libwasmvm/cosmwasm-vm.
1218
Such panics must not happen and are considered bugs. If you see this in any real-world or
@@ -31,4 +37,13 @@ mod tests {
3137
let err = catch_unwind(nice_try).unwrap_err();
3238
handle_vm_panic("nice_try", err);
3339
}
40+
41+
#[test]
42+
fn can_handle_random_payloads() {
43+
fn nice_try() {
44+
std::panic::panic_any(());
45+
}
46+
let err = catch_unwind(nice_try).unwrap_err();
47+
handle_vm_panic("nice_try", err);
48+
}
3449
}

0 commit comments

Comments
 (0)