Skip to content

Commit 7dd9fa5

Browse files
committed
Add panic handler function
1 parent 2b79acd commit 7dd9fa5

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

libwasmvm/src/cache.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::Serialize;
99
use crate::api::GoApi;
1010
use crate::args::{CACHE_ARG, CHECKSUM_ARG, CONFIG_ARG, WASM_ARG};
1111
use crate::error::{handle_c_error_binary, handle_c_error_default, handle_c_error_ptr, Error};
12+
use crate::handle_vm_panic::handle_vm_panic;
1213
use crate::memory::{ByteSliceView, UnmanagedVector};
1314
use crate::querier::GoQuerier;
1415
use crate::storage::GoStorage;
@@ -32,6 +33,7 @@ pub extern "C" fn init_cache(
3233
) -> *mut cache_t {
3334
let r = catch_unwind(|| do_init_cache(config)).unwrap_or_else(|err| {
3435
eprintln!("Panic in do_init_cache: {err:?}");
36+
handle_vm_panic();
3537
Err(Error::panic())
3638
});
3739
handle_c_error_ptr(r, error_msg) as *mut cache_t
@@ -57,6 +59,7 @@ pub extern "C" fn save_wasm(
5759
Some(c) => catch_unwind(AssertUnwindSafe(move || do_save_wasm(c, wasm, unchecked)))
5860
.unwrap_or_else(|err| {
5961
eprintln!("Panic in do_save_wasm: {err:?}");
62+
handle_vm_panic();
6063
Err(Error::panic())
6164
}),
6265
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -89,6 +92,7 @@ pub extern "C" fn remove_wasm(
8992
Some(c) => catch_unwind(AssertUnwindSafe(move || do_remove_wasm(c, checksum)))
9093
.unwrap_or_else(|err| {
9194
eprintln!("Panic in do_remove_wasm: {err:?}");
95+
handle_vm_panic();
9296
Err(Error::panic())
9397
}),
9498
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -118,6 +122,7 @@ pub extern "C" fn load_wasm(
118122
Some(c) => catch_unwind(AssertUnwindSafe(move || do_load_wasm(c, checksum)))
119123
.unwrap_or_else(|err| {
120124
eprintln!("Panic in do_load_wasm: {err:?}");
125+
handle_vm_panic();
121126
Err(Error::panic())
122127
}),
123128
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -148,6 +153,7 @@ pub extern "C" fn pin(
148153
Some(c) => {
149154
catch_unwind(AssertUnwindSafe(move || do_pin(c, checksum))).unwrap_or_else(|err| {
150155
eprintln!("Panic in do_pin: {err:?}");
156+
handle_vm_panic();
151157
Err(Error::panic())
152158
})
153159
}
@@ -178,6 +184,7 @@ pub extern "C" fn unpin(
178184
Some(c) => {
179185
catch_unwind(AssertUnwindSafe(move || do_unpin(c, checksum))).unwrap_or_else(|err| {
180186
eprintln!("Panic in do_unpin: {err:?}");
187+
handle_vm_panic();
181188
Err(Error::panic())
182189
})
183190
}
@@ -286,6 +293,7 @@ pub extern "C" fn analyze_code(
286293
Some(c) => catch_unwind(AssertUnwindSafe(move || do_analyze_code(c, checksum)))
287294
.unwrap_or_else(|err| {
288295
eprintln!("Panic in do_analyze_code: {err:?}");
296+
handle_vm_panic();
289297
Err(Error::panic())
290298
}),
291299
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -364,6 +372,7 @@ pub extern "C" fn get_metrics(
364372
Some(c) => {
365373
catch_unwind(AssertUnwindSafe(move || do_get_metrics(c))).unwrap_or_else(|err| {
366374
eprintln!("Panic in do_get_metrics: {err:?}");
375+
handle_vm_panic();
367376
Err(Error::panic())
368377
})
369378
}
@@ -419,6 +428,7 @@ pub extern "C" fn get_pinned_metrics(
419428
Some(c) => {
420429
catch_unwind(AssertUnwindSafe(move || do_get_pinned_metrics(c))).unwrap_or_else(|err| {
421430
eprintln!("Panic in do_get_pinned_metrics: {err:?}");
431+
handle_vm_panic();
422432
Err(Error::panic())
423433
})
424434
}

libwasmvm/src/calls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::args::{ARG1, ARG2, ARG3, CACHE_ARG, CHECKSUM_ARG, GAS_REPORT_ARG};
1919
use crate::cache::{cache_t, to_cache};
2020
use crate::db::Db;
2121
use crate::error::{handle_c_error_binary, Error};
22+
use crate::handle_vm_panic::handle_vm_panic;
2223
use crate::memory::{ByteSliceView, UnmanagedVector};
2324
use crate::querier::GoQuerier;
2425
use crate::storage::GoStorage;
@@ -529,6 +530,7 @@ fn call_2_args(
529530
}))
530531
.unwrap_or_else(|err| {
531532
eprintln!("Panic in do_call_2_args: {err:?}");
533+
handle_vm_panic();
532534
Err(Error::panic())
533535
}),
534536
None => Err(Error::unset_arg(CACHE_ARG)),
@@ -623,6 +625,7 @@ fn call_3_args(
623625
}))
624626
.unwrap_or_else(|err| {
625627
eprintln!("Panic in do_call_3_args: {err:?}");
628+
handle_vm_panic();
626629
Err(Error::panic())
627630
}),
628631
None => Err(Error::unset_arg(CACHE_ARG)),

libwasmvm/src/handle_vm_panic.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// A function to process cases in which the VM panics.
2+
///
3+
/// We want to provide as much debug information as possible
4+
/// as those cases are not expated to happen during healthy operations.
5+
pub fn handle_vm_panic() {
6+
eprintln!(
7+
"This indicates a panic in during the operations of libwasmvm/cosmwasm-vm.
8+
Such panics must not happen and are considered bugs. If you see this in any real-world or
9+
close-to-real-world usage of wasmvm, please consider filing a security report,
10+
no matter if it can be abused or not:
11+
(https://github.com/CosmWasm/advisories/blob/main/SECURITY.md#reporting-a-vulnerability).
12+
Thank you for your help keeping CosmWasm safe and secure 💚"
13+
);
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn handle_vm_panic_works() {
22+
handle_vm_panic();
23+
}
24+
}

libwasmvm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod db;
99
mod error;
1010
mod gas_meter;
1111
mod gas_report;
12+
mod handle_vm_panic;
1213
mod iterator;
1314
mod memory;
1415
mod querier;

0 commit comments

Comments
 (0)