Skip to content

Commit 64b37a8

Browse files
authored
use extern "custom" on naked functions with a custom calling convention
1 parent 1e2ebeb commit 64b37a8

File tree

7 files changed

+19
-28
lines changed

7 files changed

+19
-28
lines changed

compiler-builtins/src/aarch64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::intrinsics;
55
intrinsics! {
66
#[unsafe(naked)]
77
#[cfg(all(target_os = "uefi", not(feature = "no-asm")))]
8-
pub unsafe extern "C" fn __chkstk() {
8+
pub unsafe extern "custom" fn __chkstk() {
99
core::arch::naked_asm!(
1010
".p2align 2",
1111
"lsl x16, x15, #4",

compiler-builtins/src/arm.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ unsafe extern "C" {
99
}
1010

1111
// SAFETY: these are defined in compiler-builtins
12-
// FIXME(extern_custom), this isn't always the correct ABI
13-
unsafe extern "aapcs" {
12+
unsafe extern "custom" {
1413
// AAPCS is not always the correct ABI for these intrinsics, but we only use this to
1514
// forward another `__aeabi_` call so it doesn't matter.
16-
fn __aeabi_idiv(a: i32, b: i32) -> i32;
15+
fn __aeabi_idiv();
1716
}
1817

1918
intrinsics! {
2019
// NOTE This function and the ones below are implemented using assembly because they are using a
2120
// custom calling convention which can't be implemented using a normal Rust function.
2221
#[unsafe(naked)]
2322
#[cfg(not(target_env = "msvc"))]
24-
pub unsafe extern "C" fn __aeabi_uidivmod() {
23+
pub unsafe extern "custom" fn __aeabi_uidivmod() {
2524
core::arch::naked_asm!(
2625
"push {{lr}}",
2726
"sub sp, sp, #4",
@@ -35,7 +34,7 @@ intrinsics! {
3534
}
3635

3736
#[unsafe(naked)]
38-
pub unsafe extern "C" fn __aeabi_uldivmod() {
37+
pub unsafe extern "custom" fn __aeabi_uldivmod() {
3938
core::arch::naked_asm!(
4039
"push {{r4, lr}}",
4140
"sub sp, sp, #16",
@@ -51,7 +50,7 @@ intrinsics! {
5150
}
5251

5352
#[unsafe(naked)]
54-
pub unsafe extern "C" fn __aeabi_idivmod() {
53+
pub unsafe extern "custom" fn __aeabi_idivmod() {
5554
core::arch::naked_asm!(
5655
"push {{r0, r1, r4, lr}}",
5756
"bl {trampoline}",
@@ -64,7 +63,7 @@ intrinsics! {
6463
}
6564

6665
#[unsafe(naked)]
67-
pub unsafe extern "C" fn __aeabi_ldivmod() {
66+
pub unsafe extern "custom" fn __aeabi_ldivmod() {
6867
core::arch::naked_asm!(
6968
"push {{r4, lr}}",
7069
"sub sp, sp, #16",

compiler-builtins/src/int/udiv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ intrinsics! {
4444
}
4545

4646
#[unsafe(naked)]
47-
pub unsafe extern "C" fn __udivmodqi4() {
47+
pub unsafe extern "custom" fn __udivmodqi4() {
4848
// compute unsigned 8-bit `n / d` and `n % d`.
4949
//
5050
// Note: GCC implements a [non-standard calling convention](https://gcc.gnu.org/wiki/avr-gcc#Exceptions_to_the_Calling_Convention) for this function.

compiler-builtins/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg_attr(feature = "compiler-builtins", compiler_builtins)]
22
#![cfg_attr(all(target_family = "wasm"), feature(wasm_numeric_instr))]
3+
#![feature(abi_custom)]
34
#![feature(abi_unadjusted)]
45
#![feature(asm_experimental_arch)]
56
#![feature(cfg_target_has_atomic)]

compiler-builtins/src/probestack.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,12 @@
5252
// Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
5353
// ensuring that if any pages are unmapped we'll make a page fault.
5454
//
55-
// FIXME(abi_custom): This function is unsafe because it uses a custom ABI,
56-
// it does not actually match `extern "C"`.
57-
//
5855
// The ABI here is that the stack frame size is located in `%rax`. Upon
5956
// return we're not supposed to modify `%rsp` or `%rax`.
6057
#[cfg(target_arch = "x86_64")]
6158
#[unsafe(naked)]
6259
#[rustc_std_internal_symbol]
63-
pub unsafe extern "C" fn __rust_probestack() {
60+
pub unsafe extern "custom" fn __rust_probestack() {
6461
#[cfg(not(all(target_env = "sgx", target_vendor = "fortanix")))]
6562
macro_rules! ret {
6663
() => {
@@ -144,13 +141,10 @@ pub unsafe extern "C" fn __rust_probestack() {
144141
// that on Unix we're expected to restore everything as it was, this
145142
// function basically can't tamper with anything.
146143
//
147-
// FIXME(abi_custom): This function is unsafe because it uses a custom ABI,
148-
// it does not actually match `extern "C"`.
149-
//
150144
// The ABI here is the same as x86_64, except everything is 32-bits large.
151145
#[unsafe(naked)]
152146
#[rustc_std_internal_symbol]
153-
pub unsafe extern "C" fn __rust_probestack() {
147+
pub unsafe extern "custom" fn __rust_probestack() {
154148
core::arch::naked_asm!(
155149
"
156150
.cfi_startproc
@@ -192,9 +186,6 @@ pub unsafe extern "C" fn __rust_probestack() {
192186
// probestack function will also do things like _chkstk in MSVC.
193187
// So we need to sub %ax %sp in probestack when arch is x86.
194188
//
195-
// FIXME(abi_custom): This function is unsafe because it uses a custom ABI,
196-
// it does not actually match `extern "C"`.
197-
//
198189
// REF: Rust commit(74e80468347)
199190
// rust\src\llvm-project\llvm\lib\Target\X86\X86FrameLowering.cpp: 805
200191
// Comments in LLVM:
@@ -203,7 +194,7 @@ pub unsafe extern "C" fn __rust_probestack() {
203194
// themselves.
204195
#[unsafe(naked)]
205196
#[rustc_std_internal_symbol]
206-
pub unsafe extern "C" fn __rust_probestack() {
197+
pub unsafe extern "custom" fn __rust_probestack() {
207198
core::arch::naked_asm!(
208199
"
209200
.cfi_startproc

compiler-builtins/src/x86.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use core::intrinsics;
44

5-
// NOTE These functions are implemented using assembly because they using a custom
5+
// NOTE These functions are implemented using assembly because they use a custom
66
// calling convention which can't be implemented using a normal Rust function
77

88
// NOTE These functions are never mangled as they are not tested against compiler-rt
@@ -13,10 +13,10 @@ intrinsics! {
1313
any(all(windows, target_env = "gnu"), target_os = "uefi"),
1414
not(feature = "no-asm")
1515
))]
16-
pub unsafe extern "C" fn __chkstk() {
16+
pub unsafe extern "custom" fn __chkstk() {
1717
core::arch::naked_asm!(
18-
"jmp __alloca", // Jump to __alloca since fallthrough may be unreliable"
19-
options(att_syntax)
18+
"jmp {}", // Jump to __alloca since fallthrough may be unreliable"
19+
sym crate::x86::_alloca::_alloca,
2020
);
2121
}
2222

@@ -25,7 +25,7 @@ intrinsics! {
2525
any(all(windows, target_env = "gnu"), target_os = "uefi"),
2626
not(feature = "no-asm")
2727
))]
28-
pub unsafe extern "C" fn _alloca() {
28+
pub unsafe extern "custom" fn _alloca() {
2929
// __chkstk and _alloca are the same function
3030
core::arch::naked_asm!(
3131
"push %ecx",

compiler-builtins/src/x86_64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use core::intrinsics;
44

5-
// NOTE These functions are implemented using assembly because they using a custom
5+
// NOTE These functions are implemented using assembly because they use a custom
66
// calling convention which can't be implemented using a normal Rust function
77

88
// NOTE These functions are never mangled as they are not tested against compiler-rt
@@ -17,7 +17,7 @@ intrinsics! {
1717
),
1818
not(feature = "no-asm")
1919
))]
20-
pub unsafe extern "C" fn ___chkstk_ms() {
20+
pub unsafe extern "custom" fn ___chkstk_ms() {
2121
core::arch::naked_asm!(
2222
"push %rcx",
2323
"push %rax",

0 commit comments

Comments
 (0)