Skip to content

Commit

Permalink
Add UnwindSafe bound to catch_unwind
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Jan 18, 2024
1 parent 42569be commit 3c4e11b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

use core::fmt;
use core::panic::UnwindSafe;

use serde::Deserialize;

Expand Down Expand Up @@ -45,7 +46,7 @@ where
}
}

pub fn wrap(f: impl FnOnce()) {
pub fn wrap(f: impl FnOnce() + UnwindSafe) {
let _ = catch_unwind(|| {
f();
});
Expand Down
3 changes: 2 additions & 1 deletion crates/private/support/sel4-simple-task/threading/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl From<Endpoint> for StaticThread {
#[cfg(feature = "alloc")]
mod when_alloc {
use alloc::boxed::Box;
use core::panic::UnwindSafe;

use sel4::Word;
use sel4_panicking::catch_unwind;
Expand All @@ -62,7 +63,7 @@ mod when_alloc {
}

extern "C" fn entry(f_arg: Word) {
let f = unsafe { Box::from_raw(f_arg as *mut Box<dyn FnOnce()>) };
let f = unsafe { Box::from_raw(f_arg as *mut Box<dyn FnOnce() + UnwindSafe>) };
let _ = catch_unwind(f);
}
}
4 changes: 3 additions & 1 deletion crates/sel4-microkit/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// SPDX-License-Identifier: BSD-2-Clause
//

use core::panic::UnwindSafe;

pub use sel4_panicking::catch_unwind;
pub use sel4_panicking_env::abort;

Expand Down Expand Up @@ -59,7 +61,7 @@ macro_rules! declare_init {
}

#[allow(clippy::missing_safety_doc)]
pub fn run_main<T: Handler>(init: impl FnOnce() -> T) {
pub fn run_main<T: Handler>(init: impl FnOnce() -> T + UnwindSafe) {
let result = catch_unwind(|| match run_handler(init()) {
Ok(absurdity) => match absurdity {},
Err(err) => err,
Expand Down
4 changes: 2 additions & 2 deletions crates/sel4-panicking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern crate alloc;
use core::fmt;
use core::mem::ManuallyDrop;
use core::panic::Location;
use core::panic::PanicInfo;
use core::panic::{PanicInfo, UnwindSafe};

use sel4_panicking_env::abort;

Expand Down Expand Up @@ -108,7 +108,7 @@ fn do_panic(info: ExternalPanicInfo) -> ! {
}
}

pub fn catch_unwind<R, F: FnOnce() -> R>(f: F) -> Result<R, Payload> {
pub fn catch_unwind<R, F: FnOnce() -> R + UnwindSafe>(f: F) -> Result<R, Payload> {
union Data<F, R> {
f: ManuallyDrop<F>,
r: ManuallyDrop<R>,
Expand Down
5 changes: 3 additions & 2 deletions crates/sel4-root-task/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(never_type)]

use core::fmt;
use core::panic::UnwindSafe;

pub use sel4_panicking_env::{abort, debug_print, debug_println};
pub use sel4_root_task_macros::root_task;
Expand Down Expand Up @@ -70,12 +71,12 @@ macro_rules! declare_main {

#[doc(hidden)]
#[allow(clippy::missing_safety_doc)]
pub fn run_main<T>(f: impl Fn(&sel4::BootInfo) -> T, bootinfo: &sel4::BootInfo)
pub fn run_main<T>(f: impl FnOnce(&sel4::BootInfo) -> T + UnwindSafe, bootinfo: &sel4::BootInfo)
where
T: Termination,
T::Error: fmt::Debug,
{
let result = panicking::catch_unwind(|| f(bootinfo).report());
let result = panicking::catch_unwind(move || f(bootinfo).report());
match result {
Ok(err) => abort!("main thread terminated with error: {err:?}"),
Err(_) => abort!("uncaught panic in main thread"),
Expand Down
3 changes: 2 additions & 1 deletion crates/sel4-test-harness/src/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use alloc::string::String;
use core::fmt;
use core::panic::AssertUnwindSafe;

use sel4_panicking::catch_unwind;
use sel4_panicking_env::{debug_print, debug_println};
Expand Down Expand Up @@ -111,7 +112,7 @@ impl fmt::Display for TestResult {
}

fn wrap_run(should_panic: ShouldPanic, f: impl FnOnce() -> Result<(), String>) -> TestResult {
match catch_unwind(f) {
match catch_unwind(AssertUnwindSafe(f)) {
Err(_) => TestResult::from(should_panic.should_panic()),
Ok(Ok(())) => TestResult::from(!should_panic.should_panic()),
Ok(Err(msg)) => {
Expand Down

0 comments on commit 3c4e11b

Please sign in to comment.