Skip to content

Commit 6cf9185

Browse files
committed
sync adding nounwind attribute with generating abort-on-panic shim
1 parent 2201adc commit 6cf9185

File tree

5 files changed

+33
-45
lines changed

5 files changed

+33
-45
lines changed

src/librustc/hir/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2646,9 +2646,6 @@ bitflags! {
26462646
/// `#[rustc_allocator]`: a hint to LLVM that the pointer returned from this
26472647
/// function is never null.
26482648
const ALLOCATOR = 1 << 1;
2649-
/// `#[unwind]`: an indicator that this function may unwind despite what
2650-
/// its ABI signature may otherwise imply.
2651-
const UNWIND = 1 << 2;
26522649
/// `#[rust_allocator_nounwind]`, an indicator that an imported FFI
26532650
/// function will never unwind. Probably obsolete by recent changes with
26542651
/// #[unwind], but hasn't been removed/migrated yet

src/librustc/ty/context.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use std::ops::{Deref, Bound};
6565
use std::iter;
6666
use std::sync::mpsc;
6767
use std::sync::Arc;
68-
use rustc_target::spec::abi;
68+
use rustc_target::spec::{abi, PanicStrategy};
6969
use rustc_macros::HashStable;
7070
use syntax::ast;
7171
use syntax::attr;
@@ -1581,6 +1581,28 @@ impl<'tcx> TyCtxt<'tcx> {
15811581
pub fn has_strict_asm_symbol_naming(&self) -> bool {
15821582
self.gcx.sess.target.target.arch.contains("nvptx")
15831583
}
1584+
1585+
/// Determine if this function gets a shim to catch panics and abort.
1586+
pub fn abort_on_panic_shim(&self, fn_def_id: DefId, _abi: abi::Abi) -> bool {
1587+
// Validate `#[unwind]` syntax regardless of platform-specific panic strategy
1588+
let attrs = &self.get_attrs(fn_def_id);
1589+
let unwind_attr = attr::find_unwind_attr(Some(self.sess.diagnostic()), attrs);
1590+
1591+
// We never unwind, so it's not relevant to stop an unwind
1592+
if self.sess.panic_strategy() != PanicStrategy::Unwind { return false; }
1593+
1594+
// We cannot add landing pads, so don't add one
1595+
if self.sess.no_landing_pads() { return false; }
1596+
1597+
// For the common case, check the attribute.
1598+
match unwind_attr {
1599+
Some(attr::UnwindAttr::Allowed) => false,
1600+
Some(attr::UnwindAttr::Aborts) => true,
1601+
None =>
1602+
// Default: don't catch panics.
1603+
false, // FIXME(#58794), should be `abi != Abi::Rust && abi != Abi::RustCall`
1604+
}
1605+
}
15841606
}
15851607

15861608
impl<'tcx> TyCtxt<'tcx> {

src/librustc_codegen_llvm/attributes.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_data_structures::fx::FxHashMap;
1414
use rustc_target::spec::PanicStrategy;
1515
use rustc_codegen_ssa::traits::*;
1616

17-
use crate::abi::Abi;
1817
use crate::attributes;
1918
use crate::llvm::{self, Attribute};
2019
use crate::llvm::AttributePlace::Function;
@@ -267,27 +266,24 @@ pub fn from_fn_attrs(
267266
// In panic=abort mode we assume nothing can unwind anywhere, so
268267
// optimize based on this!
269268
false
270-
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::UNWIND) {
271-
// If a specific #[unwind] attribute is present, use that.
272-
// FIXME: We currently assume it can unwind even with `#[unwind(aborts)]`.
273-
true
274269
} else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
275-
// Special attribute for allocator functions, which can't unwind
270+
// Special attribute for allocator functions, which can't unwind.
276271
false
277272
} else if let Some(id) = id {
278273
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
279-
if cx.tcx.is_foreign_item(id) && sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
280-
// Foreign non-Rust items like `extern "C" { fn foo(); }` are assumed not to
281-
// unwind
274+
if cx.tcx.is_foreign_item(id) {
275+
// Foreign items like `extern "C" { fn foo(); }` and `extern "Rust" { fn bar(); }`
276+
// are assumed not to unwind.
277+
false
278+
} else if cx.tcx.abort_on_panic_shim(id, sig.abi) {
279+
// Since we are adding a shim to abort on panic, this cannot unwind.
282280
false
283281
} else {
284-
// Anything else defined in Rust is assumed that it can possibly
285-
// unwind
282+
// Anything else defined in Rust and can possibly unwind.
286283
true
287284
}
288285
} else {
289-
// assume this can possibly unwind, avoiding the application of a
290-
// `nounwind` attribute below.
286+
// Assume this can possibly unwind.
291287
true
292288
});
293289

src/librustc_mir/build/mod.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ use rustc::middle::region;
1111
use rustc::mir::*;
1212
use rustc::ty::{self, Ty, TyCtxt};
1313
use rustc::util::nodemap::HirIdMap;
14-
use rustc_target::spec::PanicStrategy;
1514
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1615
use std::u32;
1716
use rustc_target::spec::abi::Abi;
18-
use syntax::attr::{self, UnwindAttr};
1917
use syntax::symbol::kw;
2018
use syntax_pos::Span;
2119

@@ -485,29 +483,6 @@ macro_rules! unpack {
485483
};
486484
}
487485

488-
fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: DefId, abi: Abi) -> bool {
489-
// Not callable from C, so we can safely unwind through these
490-
if abi == Abi::Rust || abi == Abi::RustCall { return false; }
491-
492-
// Validate `#[unwind]` syntax regardless of platform-specific panic strategy
493-
let attrs = &tcx.get_attrs(fn_def_id);
494-
let unwind_attr = attr::find_unwind_attr(Some(tcx.sess.diagnostic()), attrs);
495-
496-
// We never unwind, so it's not relevant to stop an unwind
497-
if tcx.sess.panic_strategy() != PanicStrategy::Unwind { return false; }
498-
499-
// We cannot add landing pads, so don't add one
500-
if tcx.sess.no_landing_pads() { return false; }
501-
502-
// This is a special case: some functions have a C abi but are meant to
503-
// unwind anyway. Don't stop them.
504-
match unwind_attr {
505-
None => false, // FIXME(#58794)
506-
Some(UnwindAttr::Allowed) => false,
507-
Some(UnwindAttr::Aborts) => true,
508-
}
509-
}
510-
511486
///////////////////////////////////////////////////////////////////////////
512487
/// the main entry point for building MIR for a function
513488
@@ -599,7 +574,7 @@ where
599574
let source_info = builder.source_info(span);
600575
let call_site_s = (call_site_scope, source_info);
601576
unpack!(block = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
602-
if should_abort_on_panic(tcx, fn_def_id, abi) {
577+
if tcx.abort_on_panic_shim(fn_def_id, abi) {
603578
builder.schedule_abort();
604579
}
605580

src/librustc_typeck/collect.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2527,8 +2527,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25272527
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
25282528
} else if attr.check_name(sym::rustc_allocator) {
25292529
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
2530-
} else if attr.check_name(sym::unwind) {
2531-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::UNWIND;
25322530
} else if attr.check_name(sym::ffi_returns_twice) {
25332531
if tcx.is_foreign_item(id) {
25342532
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;

0 commit comments

Comments
 (0)