Skip to content

Commit

Permalink
Prevent recursive panics from happening
Browse files Browse the repository at this point in the history
Previously if a panic occured while processing a panic the logic would
get stuck in a loop trying to handle the panic.
Now if a panic happens while processing a panic, a hard abort is
immediately executed.
  • Loading branch information
nick-mobilecoin committed Jan 30, 2023
1 parent cdc44ad commit d97b920
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
5 changes: 3 additions & 2 deletions panic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ keywords = ["sgx", "no-std", "panic"]
log = ["dep:mc-sgx-io", "dep:mc-sgx-sync"]

[dependencies]
mc-sgx-io = { path = "../io", version = "0.1.0", optional = true }
mc-sgx-sync = { path = "../sync", version = "0.1.0", optional = true }
mc-sgx-io = { path = "../io", version = "=0.1.0", optional = true }
mc-sgx-panic-sys = { path = "sys", version = "=0.1.0" }
mc-sgx-sync = { path = "../sync", version = "=0.1.0", optional = true }
17 changes: 14 additions & 3 deletions panic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@

#[cfg(not(test))]
use core::panic::PanicInfo;
#[cfg(not(test))]
use mc_sgx_panic_sys::panic_count;

#[cfg(feature = "log")]
mod log;

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
#[cfg(feature = "log")]
log::log_panic_info(_info);

extern "C" {
fn abort() -> !;
}

let panics = panic_count::increase();

// If we entered the panic handler more than once then we must have panicked
// while trying to handle the panic. Fail hard in these instances, nothing
// more we can do.
if panics > 1 {
unsafe { abort() }
}

#[cfg(feature = "log")]
log::log_panic_info(_info);

unsafe { abort() }
}
1 change: 1 addition & 0 deletions panic/sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

mod panicking;
pub mod thread;
pub use panicking::panic_count;
2 changes: 1 addition & 1 deletion panic/sys/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub(crate) fn panicking() -> bool {
!panic_count::count_is_zero()
}

pub(crate) mod panic_count {
pub mod panic_count {
//! Number of panics that are currently being handled on the current thread
//!
//! This deviates from
Expand Down

0 comments on commit d97b920

Please sign in to comment.