Skip to content

Commit 4b42e91

Browse files
committed
Auto merge of #65020 - pnkfelix:targetted-fix-for-always-marking-rust-abi-unwind-issue-64655, r=alexcrichton
Always mark rust and rust-call abi's as unwind PR #63909 identified a bug that had been injected by PR #55982. As discussed on #64655 (comment) , we started marking extern items as nounwind, *even* extern items that said they were using "Rust" or "rust-call" ABI. This is a more targeted variant of PR #63909 that fixes the above bug. Fix #64655 ---- I personally suspect we will want PR #63909 to land in the long-term But: * it is not certain that PR #63909 *will* land, * more importantly, PR #63909 almost certainly will not be backported to beta/stable. The identified bug was more severe than I think anyone realized (apart from perhaps @gnzlbg, as noted [here](#63909 (comment))). Thus, I was motivated to write this PR, which fixes *just* the issue with extern rust/rust-call functions, and deliberately avoids injecting further deviation from current behavior (you can see further notes on this in the comments of the code added here).
2 parents 6767d9b + 028f53e commit 4b42e91

File tree

3 files changed

+187
-13
lines changed

3 files changed

+187
-13
lines changed

src/librustc_codegen_llvm/attributes.rs

+39-13
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,51 @@ pub fn from_fn_attrs(
275275
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
276276
// Special attribute for allocator functions, which can't unwind
277277
false
278-
} else if let Some(id) = id {
278+
} else if let Some(_) = id {
279+
// rust-lang/rust#64655, rust-lang/rust#63909: to minimize
280+
// risk associated with changing cases where nounwind
281+
// attribute is attached, this code is deliberately mimicking
282+
// old control flow based on whether `id` is `Some` or `None`.
283+
//
284+
// However, in the long term we should either:
285+
// - fold this into final else (i.e. stop inspecting `id`)
286+
// - or, adopt Rust PR #63909.
287+
//
288+
// see also Rust RFC 2753.
289+
279290
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
280-
if cx.tcx.is_foreign_item(id) {
281-
// Foreign items like `extern "C" { fn foo(); }` are assumed not to
282-
// unwind
283-
false
284-
} else if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
285-
// Any items defined in Rust that *don't* have the `extern` ABI are
286-
// defined to not unwind. We insert shims to abort if an unwind
287-
// happens to enforce this.
288-
false
289-
} else {
290-
// Anything else defined in Rust is assumed that it can possibly
291-
// unwind
291+
if sig.abi == Abi::Rust || sig.abi == Abi::RustCall {
292+
// Any Rust method (or `extern "Rust" fn` or `extern
293+
// "rust-call" fn`) is explicitly allowed to unwind
294+
// (unless it has no-unwind attribute, handled above).
292295
true
296+
} else {
297+
// Anything else is either:
298+
//
299+
// 1. A foreign item using a non-Rust ABI (like `extern "C" { fn foo(); }`), or
300+
//
301+
// 2. A Rust item using a non-Rust ABI (like `extern "C" fn foo() { ... }`).
302+
//
303+
// Foreign items (case 1) are assumed to not unwind; it is
304+
// UB otherwise. (At least for now; see also
305+
// rust-lang/rust#63909 and Rust RFC 2753.)
306+
//
307+
// Items defined in Rust with non-Rust ABIs (case 2) are also
308+
// not supposed to unwind. Whether this should be enforced
309+
// (versus stating it is UB) and *how* it would be enforced
310+
// is currently under discussion; see rust-lang/rust#58794.
311+
//
312+
// In either case, we mark item as explicitly nounwind.
313+
false
293314
}
294315
} else {
295316
// assume this can possibly unwind, avoiding the application of a
296317
// `nounwind` attribute below.
318+
//
319+
// (But: See comments in previous branch. Specifically, it is
320+
// unclear whether there is real value in the assumption this
321+
// can unwind. The conservatism here may just be papering over
322+
// a real problem by making some UB a bit harder to hit.)
297323
true
298324
});
299325

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// run-pass
2+
// ignore-wasm32-bare compiled with panic=abort by default
3+
// ignore-emscripten no threads support
4+
5+
// rust-lang/rust#64655: with panic=unwind, a panic from a subroutine
6+
// should still run destructors as it unwinds the stack. However,
7+
// bugs with how the nounwind LLVM attribute was applied led to this
8+
// simple case being mishandled *if* you had fat LTO turned on.
9+
10+
// Unlike issue-64655-extern-rust-must-allow-unwind.rs, the issue
11+
// embodied in this test cropped up regardless of optimization level.
12+
// Therefore it seemed worthy of being enshrined as a dedicated unit
13+
// test.
14+
15+
// LTO settings cannot be combined with -C prefer-dynamic
16+
// no-prefer-dynamic
17+
18+
// The revisions just enumerate lto settings (the opt-level appeared irrelevant in practice)
19+
20+
// revisions: no thin fat
21+
//[no]compile-flags: -C lto=no
22+
//[thin]compile-flags: -C lto=thin
23+
//[fat]compile-flags: -C lto=fat
24+
25+
#![feature(core_panic)]
26+
27+
// (For some reason, reproducing the LTO issue requires pulling in std
28+
// explicitly this way.)
29+
#![no_std]
30+
extern crate std;
31+
32+
fn main() {
33+
use std::sync::atomic::{AtomicUsize, Ordering};
34+
use std::boxed::Box;
35+
36+
static SHARED: AtomicUsize = AtomicUsize::new(0);
37+
38+
assert_eq!(SHARED.fetch_add(0, Ordering::SeqCst), 0);
39+
40+
let old_hook = std::panic::take_hook();
41+
42+
std::panic::set_hook(Box::new(|_| { } )); // no-op on panic.
43+
44+
let handle = std::thread::spawn(|| {
45+
struct Droppable;
46+
impl Drop for Droppable {
47+
fn drop(&mut self) {
48+
SHARED.fetch_add(1, Ordering::SeqCst);
49+
}
50+
}
51+
52+
let _guard = Droppable;
53+
let s = "issue-64655-allow-unwind-when-calling-panic-directly.rs";
54+
core::panicking::panic(&("???", s, 17, 4));
55+
});
56+
57+
let wait = handle.join();
58+
59+
// Reinstate handler to ease observation of assertion failures.
60+
std::panic::set_hook(old_hook);
61+
62+
assert!(wait.is_err());
63+
64+
assert_eq!(SHARED.fetch_add(0, Ordering::SeqCst), 1);
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// run-pass
2+
// ignore-wasm32-bare compiled with panic=abort by default
3+
// ignore-emscripten no threads support
4+
5+
// rust-lang/rust#64655: with panic=unwind, a panic from a subroutine
6+
// should still run destructors as it unwinds the stack. However,
7+
// bugs with how the nounwind LLVM attribute was applied led to this
8+
// simple case being mishandled *if* you had optimization *and* fat
9+
// LTO turned on.
10+
11+
// This test is the closest thing to a "regression test" we can do
12+
// without actually spawning subprocesses and comparing stderr
13+
// results.
14+
//
15+
// This test takes the code from the above issue and adapts it to
16+
// better fit our test infrastructure:
17+
//
18+
// * Instead of relying on `println!` to observe whether the destructor
19+
// is run, we instead run the code in a spawned thread and
20+
// communicate the destructor's operation via a synchronous atomic
21+
// in static memory.
22+
//
23+
// * To keep the output from confusing a casual user, we override the
24+
// panic hook to be a no-op (rather than printing a message to
25+
// stderr).
26+
//
27+
// (pnkfelix has confirmed by hand that these additions do not mask
28+
// the underlying bug.)
29+
30+
// LTO settings cannot be combined with -C prefer-dynamic
31+
// no-prefer-dynamic
32+
33+
// The revisions combine each lto setting with each optimization
34+
// setting; pnkfelix observed three differing behaviors at opt-levels
35+
// 0/1/2+3 for this test, so it seems prudent to be thorough.
36+
37+
// revisions: no0 no1 no2 no3 thin0 thin1 thin2 thin3 fat0 fat1 fat2 fat3
38+
39+
//[no0]compile-flags: -C opt-level=0 -C lto=no
40+
//[no1]compile-flags: -C opt-level=1 -C lto=no
41+
//[no2]compile-flags: -C opt-level=2 -C lto=no
42+
//[no3]compile-flags: -C opt-level=3 -C lto=no
43+
//[thin0]compile-flags: -C opt-level=0 -C lto=thin
44+
//[thin1]compile-flags: -C opt-level=1 -C lto=thin
45+
//[thin2]compile-flags: -C opt-level=2 -C lto=thin
46+
//[thin3]compile-flags: -C opt-level=3 -C lto=thin
47+
//[fat0]compile-flags: -C opt-level=0 -C lto=fat
48+
//[fat1]compile-flags: -C opt-level=1 -C lto=fat
49+
//[fat2]compile-flags: -C opt-level=2 -C lto=fat
50+
//[fat3]compile-flags: -C opt-level=3 -C lto=fat
51+
52+
fn main() {
53+
use std::sync::atomic::{AtomicUsize, Ordering};
54+
55+
static SHARED: AtomicUsize = AtomicUsize::new(0);
56+
57+
assert_eq!(SHARED.fetch_add(0, Ordering::SeqCst), 0);
58+
59+
let old_hook = std::panic::take_hook();
60+
61+
std::panic::set_hook(Box::new(|_| { } )); // no-op on panic.
62+
63+
let handle = std::thread::spawn(|| {
64+
struct Droppable;
65+
impl Drop for Droppable {
66+
fn drop(&mut self) {
67+
SHARED.fetch_add(1, Ordering::SeqCst);
68+
}
69+
}
70+
71+
let _guard = Droppable;
72+
None::<()>.expect("???");
73+
});
74+
75+
let wait = handle.join();
76+
77+
// reinstate handler to ease observation of assertion failures.
78+
std::panic::set_hook(old_hook);
79+
80+
assert!(wait.is_err());
81+
82+
assert_eq!(SHARED.fetch_add(0, Ordering::SeqCst), 1);
83+
}

0 commit comments

Comments
 (0)