Skip to content

Commit d1c591b

Browse files
author
hyd-dev
committed
Allow using -C force-unwind-tables=no when panic=unwind
1 parent 6e17a5c commit d1c591b

File tree

7 files changed

+74
-29
lines changed

7 files changed

+74
-29
lines changed

Diff for: compiler/rustc_session/src/session.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,11 @@ impl Session {
844844
// This is used to control the emission of the `uwtable` attribute on
845845
// LLVM functions.
846846
//
847-
// At the very least, unwind tables are needed when compiling with
848-
// `-C panic=unwind`.
847+
// Unwind tables are needed when compiling with `-C panic=unwind`, but
848+
// LLVM won't omit unwind tables unless the function is also marked as
849+
// `nounwind`, so users are allowed to disable `uwtable` emission.
850+
// Historically rustc always emits `uwtable` attributes by default, so
851+
// even they can be disabled, they're still emitted by default.
849852
//
850853
// On some targets (including windows), however, exceptions include
851854
// other events such as illegal instructions, segfaults, etc. This means
@@ -858,13 +861,10 @@ impl Session {
858861
// If a target requires unwind tables, then they must be emitted.
859862
// Otherwise, we can defer to the `-C force-unwind-tables=<yes/no>`
860863
// value, if it is provided, or disable them, if not.
861-
if self.panic_strategy() == PanicStrategy::Unwind {
862-
true
863-
} else if self.target.requires_uwtable {
864-
true
865-
} else {
866-
self.opts.cg.force_unwind_tables.unwrap_or(self.target.default_uwtable)
867-
}
864+
self.target.requires_uwtable
865+
|| self.opts.cg.force_unwind_tables.unwrap_or(
866+
self.panic_strategy() == PanicStrategy::Unwind || self.target.default_uwtable,
867+
)
868868
}
869869

870870
/// Returns the symbol name for the registrar function,
@@ -1536,13 +1536,6 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
15361536

15371537
// Unwind tables cannot be disabled if the target requires them.
15381538
if let Some(include_uwtables) = sess.opts.cg.force_unwind_tables {
1539-
if sess.panic_strategy() == PanicStrategy::Unwind && !include_uwtables {
1540-
sess.err(
1541-
"panic=unwind requires unwind tables, they cannot be disabled \
1542-
with `-C force-unwind-tables=no`.",
1543-
);
1544-
}
1545-
15461539
if sess.target.requires_uwtable && !include_uwtables {
15471540
sess.err(
15481541
"target requires unwind tables, they cannot be disabled with \

Diff for: src/test/assembly/panic-no-unwind-no-uwtable.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// assembly-output: emit-asm
2+
// only-x86_64-unknown-linux-gnu
3+
// compile-flags: -C panic=unwind -C force-unwind-tables=n
4+
5+
#![crate_type = "lib"]
6+
7+
// CHECK-NOT: .cfi_startproc
8+
pub fn foo() {}

Diff for: src/test/assembly/panic-unwind-no-uwtable.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// assembly-output: emit-asm
2+
// only-x86_64-unknown-linux-gnu
3+
// compile-flags: -C panic=unwind -C force-unwind-tables=n
4+
5+
#![crate_type = "lib"]
6+
7+
// CHECK-LABEL: foo:
8+
// CHECK: .cfi_startproc
9+
#[no_mangle]
10+
fn foo() {
11+
panic!();
12+
}

Diff for: src/test/codegen/force-no-unwind-tables.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
// compile-flags: -C no-prepopulate-passes -C panic=abort -C force-unwind-tables=n
1+
// compile-flags: -C no-prepopulate-passes -C force-unwind-tables=n
22
// ignore-windows
33

44
#![crate_type="lib"]
55

6+
// CHECK-LABEL: define void @foo
67
// CHECK-NOT: attributes #{{.*}} uwtable
7-
pub fn foo() {}
8+
#[no_mangle]
9+
fn foo() {
10+
panic!();
11+
}

Diff for: src/test/codegen/panic-unwind-default-uwtable.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags: -C no-prepopulate-passes
2+
3+
#![crate_type = "lib"]
4+
5+
// CHECK: attributes #{{.*}} uwtable
6+
pub fn foo() {}

Diff for: src/test/ui/panic-runtime/unwind-tables-panic-required.rs

-11
This file was deleted.

Diff for: src/test/ui/unwind-no-uwtable.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// run-pass
2+
// ignore-windows target requires uwtable
3+
// compile-flags: -C panic=unwind -C force-unwind-tables=n
4+
5+
use std::panic::{self, AssertUnwindSafe};
6+
7+
struct Increase<'a>(&'a mut u8);
8+
9+
impl Drop for Increase<'_> {
10+
fn drop(&mut self) {
11+
*self.0 += 1;
12+
}
13+
}
14+
15+
#[inline(never)]
16+
fn unwind() {
17+
panic!();
18+
}
19+
20+
#[inline(never)]
21+
fn increase(count: &mut u8) {
22+
let _increase = Increase(count);
23+
unwind();
24+
}
25+
26+
fn main() {
27+
let mut count = 0;
28+
assert!(panic::catch_unwind(AssertUnwindSafe(
29+
#[inline(never)]
30+
|| increase(&mut count)
31+
)).is_err());
32+
assert_eq!(count, 1);
33+
}

0 commit comments

Comments
 (0)