@@ -5,8 +5,14 @@ use std::any::Any;
5
5
/// We want to provide as much debug information as possible
6
6
/// as those cases are not expected to happen during healthy operations.
7
7
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
+
8
14
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:?}" ) ;
10
16
eprintln ! (
11
17
"This indicates a panic in during the operations of libwasmvm/cosmwasm-vm.
12
18
Such panics must not happen and are considered bugs. If you see this in any real-world or
@@ -31,4 +37,13 @@ mod tests {
31
37
let err = catch_unwind ( nice_try) . unwrap_err ( ) ;
32
38
handle_vm_panic ( "nice_try" , err) ;
33
39
}
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
+ }
34
49
}
0 commit comments