Skip to content

Commit b6eb4f9

Browse files
committed
use #[naked] for __rust_probestack
1 parent f3af860 commit b6eb4f9

File tree

3 files changed

+36
-99
lines changed

3 files changed

+36
-99
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::def_id::DefId;
55
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, PatchableFunctionEntry};
66
use rustc_middle::ty::{self, TyCtxt};
77
use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
8+
use rustc_symbol_mangling::mangle_internal_symbol;
89
use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
910
use smallvec::SmallVec;
1011

@@ -256,11 +257,11 @@ fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
256257
StackProbeType::Inline => "inline-asm",
257258
// Flag our internal `__rust_probestack` function as the stack probe symbol.
258259
// This is defined in the `compiler-builtins` crate for each architecture.
259-
StackProbeType::Call => "__rust_probestack",
260+
StackProbeType::Call => &mangle_internal_symbol(cx.tcx, "__rust_probestack"),
260261
// Pick from the two above based on the LLVM version.
261262
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
262263
if llvm_util::get_version() < min_llvm_version_for_inline {
263-
"__rust_probestack"
264+
&mangle_internal_symbol(cx.tcx, "__rust_probestack")
264265
} else {
265266
"inline-asm"
266267
}

library/compiler-builtins/compiler-builtins/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(linkage)]
99
#![feature(naked_functions)]
1010
#![feature(repr_simd)]
11+
#![feature(rustc_attrs)]
1112
#![cfg_attr(f16_enabled, feature(f16))]
1213
#![cfg_attr(f128_enabled, feature(f128))]
1314
#![no_builtins]

library/compiler-builtins/compiler-builtins/src/probestack.rs

Lines changed: 32 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -49,79 +49,6 @@
4949
// We only define stack probing for these architectures today.
5050
#![cfg(any(target_arch = "x86_64", target_arch = "x86"))]
5151

52-
// SAFETY: defined in this module.
53-
// FIXME(extern_custom): the ABI is not correct.
54-
unsafe extern "C" {
55-
pub fn __rust_probestack();
56-
}
57-
58-
// A wrapper for our implementation of __rust_probestack, which allows us to
59-
// keep the assembly inline while controlling all CFI directives in the assembly
60-
// emitted for the function.
61-
//
62-
// This is the ELF version.
63-
#[cfg(not(any(target_vendor = "apple", target_os = "uefi")))]
64-
macro_rules! define_rust_probestack {
65-
($body: expr) => {
66-
concat!(
67-
"
68-
.pushsection .text.__rust_probestack
69-
.globl __rust_probestack
70-
.type __rust_probestack, @function
71-
.hidden __rust_probestack
72-
__rust_probestack:
73-
",
74-
$body,
75-
"
76-
.size __rust_probestack, . - __rust_probestack
77-
.popsection
78-
"
79-
)
80-
};
81-
}
82-
83-
#[cfg(all(target_os = "uefi", target_arch = "x86_64"))]
84-
macro_rules! define_rust_probestack {
85-
($body: expr) => {
86-
concat!(
87-
"
88-
.globl __rust_probestack
89-
__rust_probestack:
90-
",
91-
$body
92-
)
93-
};
94-
}
95-
96-
// Same as above, but for Mach-O. Note that the triple underscore
97-
// is deliberate
98-
#[cfg(target_vendor = "apple")]
99-
macro_rules! define_rust_probestack {
100-
($body: expr) => {
101-
concat!(
102-
"
103-
.globl ___rust_probestack
104-
___rust_probestack:
105-
",
106-
$body
107-
)
108-
};
109-
}
110-
111-
// In UEFI x86 arch, triple underscore is deliberate.
112-
#[cfg(all(target_os = "uefi", target_arch = "x86"))]
113-
macro_rules! define_rust_probestack {
114-
($body: expr) => {
115-
concat!(
116-
"
117-
.globl ___rust_probestack
118-
___rust_probestack:
119-
",
120-
$body
121-
)
122-
};
123-
}
124-
12552
// Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
12653
// ensuring that if any pages are unmapped we'll make a page fault.
12754
//
@@ -136,8 +63,10 @@ macro_rules! define_rust_probestack {
13663
target_arch = "x86_64",
13764
not(all(target_env = "sgx", target_vendor = "fortanix"))
13865
))]
139-
core::arch::global_asm!(
140-
define_rust_probestack!(
66+
#[unsafe(naked)]
67+
#[rustc_std_internal_symbol]
68+
pub unsafe extern "C" fn __rust_probestack() {
69+
core::arch::naked_asm!(
14170
"
14271
.cfi_startproc
14372
pushq %rbp
@@ -187,10 +116,10 @@ core::arch::global_asm!(
187116
.cfi_adjust_cfa_offset -8
188117
ret
189118
.cfi_endproc
190-
"
191-
),
192-
options(att_syntax)
193-
);
119+
",
120+
options(att_syntax)
121+
)
122+
}
194123

195124
// This function is the same as above, except that some instructions are
196125
// [manually patched for LVI].
@@ -200,8 +129,10 @@ core::arch::global_asm!(
200129
target_arch = "x86_64",
201130
all(target_env = "sgx", target_vendor = "fortanix")
202131
))]
203-
core::arch::global_asm!(
204-
define_rust_probestack!(
132+
#[unsafe(naked)]
133+
#[no_mangle]
134+
pub unsafe extern "C" fn __rust_probestack() {
135+
core::arch::naked_asm!(
205136
"
206137
.cfi_startproc
207138
pushq %rbp
@@ -253,10 +184,10 @@ core::arch::global_asm!(
253184
lfence
254185
jmp *%r11
255186
.cfi_endproc
256-
"
257-
),
258-
options(att_syntax)
259-
);
187+
",
188+
options(att_syntax)
189+
)
190+
}
260191

261192
#[cfg(all(target_arch = "x86", not(target_os = "uefi")))]
262193
// This is the same as x86_64 above, only translated for 32-bit sizes. Note
@@ -267,8 +198,10 @@ core::arch::global_asm!(
267198
// it does not actually match `extern "C"`.
268199
//
269200
// The ABI here is the same as x86_64, except everything is 32-bits large.
270-
core::arch::global_asm!(
271-
define_rust_probestack!(
201+
#[unsafe(naked)]
202+
#[rustc_std_internal_symbol]
203+
pub unsafe extern "C" fn __rust_probestack() {
204+
core::arch::naked_asm!(
272205
"
273206
.cfi_startproc
274207
push %ebp
@@ -299,10 +232,10 @@ core::arch::global_asm!(
299232
.cfi_adjust_cfa_offset -4
300233
ret
301234
.cfi_endproc
302-
"
303-
),
304-
options(att_syntax)
305-
);
235+
",
236+
options(att_syntax)
237+
)
238+
}
306239

307240
#[cfg(all(target_arch = "x86", target_os = "uefi"))]
308241
// UEFI target is windows like target. LLVM will do _chkstk things like windows.
@@ -318,8 +251,10 @@ core::arch::global_asm!(
318251
// MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves.
319252
// MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
320253
// themselves.
321-
core::arch::global_asm!(
322-
define_rust_probestack!(
254+
#[unsafe(naked)]
255+
#[rustc_std_internal_symbol]
256+
pub unsafe extern "C" fn __rust_probestack() {
257+
core::arch::naked_asm!(
323258
"
324259
.cfi_startproc
325260
push %ebp
@@ -355,7 +290,7 @@ core::arch::global_asm!(
355290
.cfi_adjust_cfa_offset -4
356291
ret
357292
.cfi_endproc
358-
"
359-
),
360-
options(att_syntax)
361-
);
293+
",
294+
options(att_syntax)
295+
)
296+
}

0 commit comments

Comments
 (0)