Skip to content

Commit

Permalink
rustc_target: add "unwind" payloads to Abi
Browse files Browse the repository at this point in the history
 ### Overview

    This commit begins the implementation work for RFC 2945. For more
    information, see the rendered RFC [1] and tracking issue [2].

    A boolean `unwind` payload is added to the `C`, `System`, `Stdcall`,
    and `Thiscall` variants, marking whether unwinding across FFI
    boundaries is acceptable. The cases where each of these variants'
    `unwind` member is true correspond with the `C-unwind`,
    `system-unwind`, `stdcall-unwind`, and `thiscall-unwind` ABI strings
    introduced in RFC 2945 [3].

 ### Feature Gate and Unstable Book

    This commit adds a `c_unwind` feature gate for the new ABI strings.
    Tests for this feature gate are included in `src/test/ui/c-unwind/`,
    which ensure that this feature gate works correctly for each of the
    new ABIs.

    A new language features entry in the unstable book is added as well.

 ### Further Work To Be Done

    This commit does not proceed to implement the new unwinding ABIs,
    and is intentionally scoped specifically to *defining* the ABIs and
    their feature flag.

 ### One Note on Test Churn

    This will lead to some test churn, in re-blessing hash tests, as the
    deleted comment in `src/librustc_target/spec/abi.rs` mentioned,
    because we can no longer guarantee the ordering of the `Abi`
    variants.

    While this is a downside, this decision was made bearing in mind
    that RFC 2945 states the following, in the "Other `unwind` Strings"
    section [3]:

    >  More unwind variants of existing ABI strings may be introduced,
    >  with the same semantics, without an additional RFC.

    Adding a new variant for each of these cases, rather than specifying
    a payload for a given ABI, would quickly become untenable, and make
    working with the `Abi` enum prone to mistakes.

    This approach encodes the unwinding information *into* a given ABI,
    to account for the future possibility of other `-unwind` ABI
    strings.

 ### Footnotes

[1]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
[2]: rust-lang#74990
[3]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md#other-unwind-abi-strings
  • Loading branch information
katelyn a. martin committed Jan 26, 2021
1 parent d1aed50 commit d9bee1c
Show file tree
Hide file tree
Showing 29 changed files with 270 additions and 60 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
ItemKind::ForeignMod(ref fm) => {
if fm.abi.is_none() {
self.maybe_lint_missing_abi(span, id, abi::Abi::C);
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
}
hir::ItemKind::ForeignMod {
abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)),
abi: fm.abi.map_or(abi::Abi::C { unwind: false }, |abi| self.lower_abi(abi)),
items: self
.arena
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
Expand Down Expand Up @@ -1315,8 +1315,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
match ext {
Extern::None => abi::Abi::Rust,
Extern::Implicit => {
self.maybe_lint_missing_abi(span, id, abi::Abi::C);
abi::Abi::C
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
abi::Abi::C { unwind: false }
}
Extern::Explicit(abi) => self.lower_abi(abi),
}
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,39 @@ impl<'a> PostExpansionVisitor<'a> {
"efiapi ABI is experimental and subject to change"
);
}
// FFI Unwinding ABI's
"C-unwind" => {
gate_feature_post!(
&self,
c_unwind,
span,
"C-unwind ABI is experimental and subject to change"
);
}
"stdcall-unwind" => {
gate_feature_post!(
&self,
c_unwind,
span,
"stdcall-unwind ABI is experimental and subject to change"
);
}
"system-unwind" => {
gate_feature_post!(
&self,
c_unwind,
span,
"system-unwind ABI is experimental and subject to change"
);
}
"thiscall-unwind" => {
gate_feature_post!(
&self,
c_unwind,
span,
"thiscall-unwind ABI is experimental and subject to change"
);
}
abi => self
.sess
.parse_sess
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_cranelift/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn clif_sig_from_fn_sig<'tcx>(
requires_caller_location: bool,
) -> Signature {
let abi = match sig.abi {
Abi::System => Abi::C,
Abi::System { unwind: false } => Abi::C { unwind: false },
abi => abi,
};
let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
Expand All @@ -110,7 +110,7 @@ fn clif_sig_from_fn_sig<'tcx>(
sig.inputs().to_vec(),
sig.output(),
),
Abi::C | Abi::Unadjusted => (
Abi::C { unwind: false } | Abi::Unadjusted => (
CallConv::triple_default(triple),
sig.inputs().to_vec(),
sig.output(),
Expand All @@ -126,7 +126,7 @@ fn clif_sig_from_fn_sig<'tcx>(
inputs.extend(extra_args.types());
(CallConv::triple_default(triple), inputs, sig.output())
}
Abi::System => unreachable!(),
Abi::System { .. } => unreachable!(),
Abi::RustIntrinsic => (
CallConv::triple_default(triple),
sig.inputs().to_vec(),
Expand Down Expand Up @@ -664,7 +664,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(

// FIXME find a cleaner way to support varargs
if fn_sig.c_variadic {
if fn_sig.abi != Abi::C {
if !matches!(fn_sig.abi, Abi::C { .. }) {
fx.tcx.sess.span_fatal(
span,
&format!("Variadic call for non-C abi {:?}", fn_sig.abi),
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ declare_features! (

/// Allows using `pointer` and `reference` in intra-doc links
(active, intra_doc_pointers, "1.51.0", Some(80896), None),

/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries.
(active, c_unwind, "1.51.0", Some(74990), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2615,14 +2615,14 @@ where
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => Conv::Rust,

// It's the ABI's job to select this, not ours.
System => bug!("system abi should be selected elsewhere"),
System { .. } => bug!("system abi should be selected elsewhere"),
EfiApi => bug!("eficall abi should be selected elsewhere"),

Stdcall => Conv::X86Stdcall,
Stdcall { .. } => Conv::X86Stdcall,
Fastcall => Conv::X86Fastcall,
Vectorcall => Conv::X86VectorCall,
Thiscall => Conv::X86ThisCall,
C => Conv::C,
Thiscall { .. } => Conv::X86ThisCall,
C { .. } => Conv::C,
Unadjusted => Conv::C,
Win64 => Conv::X86_64Win64,
SysV64 => Conv::X86_64SysV,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir/src/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
};
if normalize_abi(caller_abi) != normalize_abi(callee_abi) {
throw_ub_format!(
"calling a function with ABI {:?} using caller ABI {:?}",
callee_abi,
caller_abi
"calling a function with ABI {} using caller ABI {}",
callee_abi.name(),
caller_abi.name()
)
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ symbols! {
bridge,
bswap,
c_str,
c_unwind,
c_variadic,
call,
call_mut,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
}
match sig.abi {
Abi::Rust => {}
Abi::C => cx.push("KC"),
Abi::C { unwind: false } => cx.push("KC"),
abi => {
cx.push("K");
let name = abi.name();
Expand Down
69 changes: 50 additions & 19 deletions compiler/rustc_target/src/spec/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@ mod tests;
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
pub enum Abi {
// N.B., this ordering MUST match the AbiDatas array below.
// (This is ensured by the test indices_are_correct().)

// Multiplatform / generic ABIs
//
// These ABIs come first because every time we add a new ABI, we
// have to re-bless all the hashing tests. These are used in many
// places, so giving them stable values reduces test churn. The
// specific values are meaningless.
Rust = 0,
C = 1,
Rust,
C { unwind: bool },

// Single platform ABIs
Cdecl,
Stdcall,
Stdcall { unwind: bool },
Fastcall,
Vectorcall,
Thiscall,
Thiscall { unwind: bool },
Aapcs,
Win64,
SysV64,
Expand All @@ -38,7 +30,7 @@ pub enum Abi {
AvrNonBlockingInterrupt,

// Multiplatform / generic ABIs
System,
System { unwind: bool },
RustIntrinsic,
RustCall,
PlatformIntrinsic,
Expand All @@ -60,13 +52,16 @@ pub struct AbiData {
const AbiDatas: &[AbiData] = &[
// Cross-platform ABIs
AbiData { abi: Abi::Rust, name: "Rust", generic: true },
AbiData { abi: Abi::C, name: "C", generic: true },
AbiData { abi: Abi::C { unwind: false }, name: "C", generic: true },
AbiData { abi: Abi::C { unwind: true }, name: "C-unwind", generic: true },
// Platform-specific ABIs
AbiData { abi: Abi::Cdecl, name: "cdecl", generic: false },
AbiData { abi: Abi::Stdcall, name: "stdcall", generic: false },
AbiData { abi: Abi::Stdcall { unwind: false }, name: "stdcall", generic: false },
AbiData { abi: Abi::Stdcall { unwind: true }, name: "stdcall-unwind", generic: false },
AbiData { abi: Abi::Fastcall, name: "fastcall", generic: false },
AbiData { abi: Abi::Vectorcall, name: "vectorcall", generic: false },
AbiData { abi: Abi::Thiscall, name: "thiscall", generic: false },
AbiData { abi: Abi::Thiscall { unwind: false }, name: "thiscall", generic: false },
AbiData { abi: Abi::Thiscall { unwind: true }, name: "thiscall-unwind", generic: false },
AbiData { abi: Abi::Aapcs, name: "aapcs", generic: false },
AbiData { abi: Abi::Win64, name: "win64", generic: false },
AbiData { abi: Abi::SysV64, name: "sysv64", generic: false },
Expand All @@ -82,7 +77,8 @@ const AbiDatas: &[AbiData] = &[
generic: false,
},
// Cross-platform ABIs
AbiData { abi: Abi::System, name: "system", generic: true },
AbiData { abi: Abi::System { unwind: false }, name: "system", generic: true },
AbiData { abi: Abi::System { unwind: true }, name: "system-unwind", generic: true },
AbiData { abi: Abi::RustIntrinsic, name: "rust-intrinsic", generic: true },
AbiData { abi: Abi::RustCall, name: "rust-call", generic: true },
AbiData { abi: Abi::PlatformIntrinsic, name: "platform-intrinsic", generic: true },
Expand All @@ -101,7 +97,40 @@ pub fn all_names() -> Vec<&'static str> {
impl Abi {
#[inline]
pub fn index(self) -> usize {
self as usize
// N.B., this ordering MUST match the AbiDatas array above.
// (This is ensured by the test indices_are_correct().)
use Abi::*;
match self {
// Cross-platform ABIs
Rust => 0,
C { unwind: false } => 1,
C { unwind: true } => 2,
// Platform-specific ABIs
Cdecl => 3,
Stdcall { unwind: false } => 4,
Stdcall { unwind: true } => 5,
Fastcall => 6,
Vectorcall => 7,
Thiscall { unwind: false } => 8,
Thiscall { unwind: true } => 9,
Aapcs => 10,
Win64 => 11,
SysV64 => 12,
PtxKernel => 13,
Msp430Interrupt => 14,
X86Interrupt => 15,
AmdGpuKernel => 16,
EfiApi => 17,
AvrInterrupt => 18,
AvrNonBlockingInterrupt => 19,
// Cross-platform ABIs
System { unwind: false } => 20,
System { unwind: true } => 21,
RustIntrinsic => 22,
RustCall => 23,
PlatformIntrinsic => 24,
Unadjusted => 25,
}
}

#[inline]
Expand All @@ -120,6 +149,8 @@ impl Abi {

impl fmt::Display for Abi {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"{}\"", self.name())
match self {
abi => write!(f, "\"{}\"", abi.name()),
}
}
}
11 changes: 10 additions & 1 deletion compiler/rustc_target/src/spec/arm_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ use crate::spec::abi::Abi;

// All the calling conventions trigger an assertion(Unsupported calling convention) in llvm on arm
pub fn unsupported_abis() -> Vec<Abi> {
vec![Abi::Stdcall, Abi::Fastcall, Abi::Vectorcall, Abi::Thiscall, Abi::Win64, Abi::SysV64]
vec![
Abi::Stdcall { unwind: false },
Abi::Stdcall { unwind: true },
Abi::Fastcall,
Abi::Vectorcall,
Abi::Thiscall { unwind: false },
Abi::Thiscall { unwind: true },
Abi::Win64,
Abi::SysV64,
]
}
6 changes: 4 additions & 2 deletions compiler/rustc_target/src/spec/mipsel_unknown_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ pub fn target() -> Target {
panic_strategy: PanicStrategy::Abort,
relocation_model: RelocModel::Static,
unsupported_abis: vec![
Abi::Stdcall,
Abi::Stdcall { unwind: false },
Abi::Stdcall { unwind: true },
Abi::Fastcall,
Abi::Vectorcall,
Abi::Thiscall,
Abi::Thiscall { unwind: false },
Abi::Thiscall { unwind: true },
Abi::Win64,
Abi::SysV64,
],
Expand Down
19 changes: 13 additions & 6 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,24 +1208,31 @@ impl Target {
/// Given a function ABI, turn it into the correct ABI for this target.
pub fn adjust_abi(&self, abi: Abi) -> Abi {
match abi {
Abi::System => {
Abi::System { unwind } => {
if self.is_like_windows && self.arch == "x86" {
Abi::Stdcall
Abi::Stdcall { unwind }
} else {
Abi::C
Abi::C { unwind }
}
}
// These ABI kinds are ignored on non-x86 Windows targets.
// See https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions
// and the individual pages for __stdcall et al.
Abi::Stdcall | Abi::Fastcall | Abi::Vectorcall | Abi::Thiscall => {
if self.is_like_windows && self.arch != "x86" { Abi::C } else { abi }
Abi::Stdcall { unwind } | Abi::Thiscall { unwind } => {
if self.is_like_windows && self.arch != "x86" { Abi::C { unwind } } else { abi }
}
Abi::Fastcall | Abi::Vectorcall => {
if self.is_like_windows && self.arch != "x86" {
Abi::C { unwind: false }
} else {
abi
}
}
Abi::EfiApi => {
if self.arch == "x86_64" {
Abi::Win64
} else {
Abi::C
Abi::C { unwind: false }
}
}
abi => abi,
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_target/src/spec/nvptx64_nvidia_cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ pub fn target() -> Target {
// create the tests for this.
unsupported_abis: vec![
Abi::Cdecl,
Abi::Stdcall,
Abi::Stdcall { unwind: false },
Abi::Stdcall { unwind: true },
Abi::Fastcall,
Abi::Vectorcall,
Abi::Thiscall,
Abi::Thiscall { unwind: false },
Abi::Thiscall { unwind: true },
Abi::Aapcs,
Abi::Win64,
Abi::SysV64,
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_target/src/spec/riscv_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use crate::spec::abi::Abi;
pub fn unsupported_abis() -> Vec<Abi> {
vec![
Abi::Cdecl,
Abi::Stdcall,
Abi::Stdcall { unwind: false },
Abi::Stdcall { unwind: true },
Abi::Fastcall,
Abi::Vectorcall,
Abi::Thiscall,
Abi::Thiscall { unwind: false },
Abi::Thiscall { unwind: true },
Abi::Aapcs,
Abi::Win64,
Abi::SysV64,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2569,7 +2569,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
} else if tcx.sess.check_name(attr, sym::used) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
} else if tcx.sess.check_name(attr, sym::cmse_nonsecure_entry) {
if tcx.fn_sig(id).abi() != abi::Abi::C {
if !matches!(tcx.fn_sig(id).abi(), abi::Abi::C { .. }) {
struct_span_err!(
tcx.sess,
attr.span,
Expand Down
Loading

0 comments on commit d9bee1c

Please sign in to comment.