Skip to content

Commit 08574db

Browse files
committed
Fixes safe Rust part of soundness bug 52652 by aborting on safe extern "C" functions
1 parent 1e86913 commit 08574db

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/librustc_mir/build/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: DefId, abi: Abi) -> bool {
491491

492492
// Validate `#[unwind]` syntax regardless of platform-specific panic strategy
493493
let attrs = &tcx.get_attrs(fn_def_id);
494+
let unsafety = &tcx.fn_sig(fn_def_id).skip_binder().unsafety.clone();
494495
let unwind_attr = attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs);
495496

496497
// We never unwind, so it's not relevant to stop an unwind
@@ -502,9 +503,11 @@ fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: DefId, abi: Abi) -> bool {
502503
// This is a special case: some functions have a C abi but are meant to
503504
// unwind anyway. Don't stop them.
504505
match unwind_attr {
505-
None => false, // FIXME(#58794)
506506
Some(UnwindAttr::Allowed) => false,
507507
Some(UnwindAttr::Aborts) => true,
508+
// If no `#[unwind]` attribute present unsafe function definitions
509+
// are temporarily allowed to unwind:
510+
None => unsafety == &rustc::hir::Unsafety::Normal,
508511
}
509512
}
510513

src/test/ui/abi/abort-on-c-abi.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ use std::io::prelude::*;
1414
use std::io;
1515
use std::process::{Command, Stdio};
1616

17-
#[unwind(aborts)] // FIXME(#58794)
17+
unsafe extern "C" fn unsafe_panic_in_ffi() {
18+
panic!("Test");
19+
}
20+
1821
extern "C" fn panic_in_ffi() {
1922
panic!("Test");
2023
}
2124

2225
fn test() {
23-
let _ = panic::catch_unwind(|| { panic_in_ffi(); });
24-
// The process should have aborted by now.
26+
// A safe extern "C" function that panics should abort the process:
27+
let _ = panic::catch_unwind(|| panic_in_ffi() );
28+
29+
// If the process did not abort, the panic escaped FFI:
2530
io::stdout().write(b"This should never be printed.\n");
2631
let _ = io::stdout().flush();
2732
}
@@ -37,4 +42,7 @@ fn main() {
3742
.stdin(Stdio::piped())
3843
.arg("test").spawn().unwrap();
3944
assert!(!p.wait().unwrap().success());
45+
46+
// An unsafe extern "C" function that panics should let the panic escape:
47+
assert!(panic::catch_unwind(|| unsafe { unsafe_panic_in_ffi() }).is_err());
4048
}

0 commit comments

Comments
 (0)