Skip to content

Commit

Permalink
Move called function and error into handle_vm_panic
Browse files Browse the repository at this point in the history
webmaster128 committed Oct 30, 2024
1 parent 7dd9fa5 commit b785826
Showing 3 changed files with 23 additions and 24 deletions.
27 changes: 9 additions & 18 deletions libwasmvm/src/cache.rs
Original file line number Diff line number Diff line change
@@ -32,8 +32,7 @@ pub extern "C" fn init_cache(
error_msg: Option<&mut UnmanagedVector>,
) -> *mut cache_t {
let r = catch_unwind(|| do_init_cache(config)).unwrap_or_else(|err| {
eprintln!("Panic in do_init_cache: {err:?}");
handle_vm_panic();
handle_vm_panic("do_init_cache", err);
Err(Error::panic())
});
handle_c_error_ptr(r, error_msg) as *mut cache_t
@@ -58,8 +57,7 @@ pub extern "C" fn save_wasm(
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_save_wasm(c, wasm, unchecked)))
.unwrap_or_else(|err| {
eprintln!("Panic in do_save_wasm: {err:?}");
handle_vm_panic();
handle_vm_panic("do_save_wasm", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -91,8 +89,7 @@ pub extern "C" fn remove_wasm(
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_remove_wasm(c, checksum)))
.unwrap_or_else(|err| {
eprintln!("Panic in do_remove_wasm: {err:?}");
handle_vm_panic();
handle_vm_panic("do_remove_wasm", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -121,8 +118,7 @@ pub extern "C" fn load_wasm(
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_load_wasm(c, checksum)))
.unwrap_or_else(|err| {
eprintln!("Panic in do_load_wasm: {err:?}");
handle_vm_panic();
handle_vm_panic("do_load_wasm", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -152,8 +148,7 @@ pub extern "C" fn pin(
let r = match to_cache(cache) {
Some(c) => {
catch_unwind(AssertUnwindSafe(move || do_pin(c, checksum))).unwrap_or_else(|err| {
eprintln!("Panic in do_pin: {err:?}");
handle_vm_panic();
handle_vm_panic("do_pin", err);
Err(Error::panic())
})
}
@@ -183,8 +178,7 @@ pub extern "C" fn unpin(
let r = match to_cache(cache) {
Some(c) => {
catch_unwind(AssertUnwindSafe(move || do_unpin(c, checksum))).unwrap_or_else(|err| {
eprintln!("Panic in do_unpin: {err:?}");
handle_vm_panic();
handle_vm_panic("do_unpin", err);
Err(Error::panic())
})
}
@@ -292,8 +286,7 @@ pub extern "C" fn analyze_code(
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_analyze_code(c, checksum)))
.unwrap_or_else(|err| {
eprintln!("Panic in do_analyze_code: {err:?}");
handle_vm_panic();
handle_vm_panic("do_analyze_code", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -371,8 +364,7 @@ pub extern "C" fn get_metrics(
let r = match to_cache(cache) {
Some(c) => {
catch_unwind(AssertUnwindSafe(move || do_get_metrics(c))).unwrap_or_else(|err| {
eprintln!("Panic in do_get_metrics: {err:?}");
handle_vm_panic();
handle_vm_panic("do_get_metrics", err);
Err(Error::panic())
})
}
@@ -427,8 +419,7 @@ pub extern "C" fn get_pinned_metrics(
let r = match to_cache(cache) {
Some(c) => {
catch_unwind(AssertUnwindSafe(move || do_get_pinned_metrics(c))).unwrap_or_else(|err| {
eprintln!("Panic in do_get_pinned_metrics: {err:?}");
handle_vm_panic();
handle_vm_panic("do_get_pinned_metrics", err);
Err(Error::panic())
})
}
6 changes: 2 additions & 4 deletions libwasmvm/src/calls.rs
Original file line number Diff line number Diff line change
@@ -529,8 +529,7 @@ fn call_2_args(
)
}))
.unwrap_or_else(|err| {
eprintln!("Panic in do_call_2_args: {err:?}");
handle_vm_panic();
handle_vm_panic("do_call_2_args", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -624,8 +623,7 @@ fn call_3_args(
)
}))
.unwrap_or_else(|err| {
eprintln!("Panic in do_call_3_args: {err:?}");
handle_vm_panic();
handle_vm_panic("do_call_3_args", err);
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
14 changes: 12 additions & 2 deletions libwasmvm/src/handle_vm_panic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::any::Any;

/// A function to process cases in which the VM panics.
///
/// We want to provide as much debug information as possible
/// as those cases are not expated to happen during healthy operations.
pub fn handle_vm_panic() {
pub fn handle_vm_panic(what: &str, err: Box<dyn Any + Send + 'static>) {
eprintln!("Panic in {what}:");
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
eprintln!(
"This indicates a panic in during the operations of libwasmvm/cosmwasm-vm.
Such panics must not happen and are considered bugs. If you see this in any real-world or
@@ -17,8 +21,14 @@ Thank you for your help keeping CosmWasm safe and secure 💚"
mod tests {
use super::*;

use std::panic::catch_unwind;

#[test]
fn handle_vm_panic_works() {
handle_vm_panic();
fn nice_try() {
panic!("oh no!");
}
let err = catch_unwind(nice_try).unwrap_err();
handle_vm_panic("nice_try", err);
}
}

0 comments on commit b785826

Please sign in to comment.