Skip to content

Commit d70607d

Browse files
committed
use #[naked] for __rust_probestack
1 parent ee8f257 commit d70607d

File tree

1 file changed

+32
-95
lines changed

1 file changed

+32
-95
lines changed

compiler-builtins/src/probestack.rs

+32-95
Original file line numberDiff line numberDiff line change
@@ -49,77 +49,6 @@
4949
// We only define stack probing for these architectures today.
5050
#![cfg(any(target_arch = "x86_64", target_arch = "x86"))]
5151

52-
extern "C" {
53-
pub fn __rust_probestack();
54-
}
55-
56-
// A wrapper for our implementation of __rust_probestack, which allows us to
57-
// keep the assembly inline while controlling all CFI directives in the assembly
58-
// emitted for the function.
59-
//
60-
// This is the ELF version.
61-
#[cfg(not(any(target_vendor = "apple", target_os = "uefi")))]
62-
macro_rules! define_rust_probestack {
63-
($body: expr) => {
64-
concat!(
65-
"
66-
.pushsection .text.__rust_probestack
67-
.globl __rust_probestack
68-
.type __rust_probestack, @function
69-
.hidden __rust_probestack
70-
__rust_probestack:
71-
",
72-
$body,
73-
"
74-
.size __rust_probestack, . - __rust_probestack
75-
.popsection
76-
"
77-
)
78-
};
79-
}
80-
81-
#[cfg(all(target_os = "uefi", target_arch = "x86_64"))]
82-
macro_rules! define_rust_probestack {
83-
($body: expr) => {
84-
concat!(
85-
"
86-
.globl __rust_probestack
87-
__rust_probestack:
88-
",
89-
$body
90-
)
91-
};
92-
}
93-
94-
// Same as above, but for Mach-O. Note that the triple underscore
95-
// is deliberate
96-
#[cfg(target_vendor = "apple")]
97-
macro_rules! define_rust_probestack {
98-
($body: expr) => {
99-
concat!(
100-
"
101-
.globl ___rust_probestack
102-
___rust_probestack:
103-
",
104-
$body
105-
)
106-
};
107-
}
108-
109-
// In UEFI x86 arch, triple underscore is deliberate.
110-
#[cfg(all(target_os = "uefi", target_arch = "x86"))]
111-
macro_rules! define_rust_probestack {
112-
($body: expr) => {
113-
concat!(
114-
"
115-
.globl ___rust_probestack
116-
___rust_probestack:
117-
",
118-
$body
119-
)
120-
};
121-
}
122-
12352
// Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
12453
// ensuring that if any pages are unmapped we'll make a page fault.
12554
//
@@ -131,8 +60,10 @@ macro_rules! define_rust_probestack {
13160
target_arch = "x86_64",
13261
not(all(target_env = "sgx", target_vendor = "fortanix"))
13362
))]
134-
core::arch::global_asm!(
135-
define_rust_probestack!(
63+
#[unsafe(naked)]
64+
#[no_mangle]
65+
pub unsafe extern "C" fn __rust_probestack() {
66+
core::arch::naked_asm!(
13667
"
13768
.cfi_startproc
13869
pushq %rbp
@@ -182,10 +113,10 @@ core::arch::global_asm!(
182113
.cfi_adjust_cfa_offset -8
183114
ret
184115
.cfi_endproc
185-
"
186-
),
187-
options(att_syntax)
188-
);
116+
",
117+
options(att_syntax)
118+
)
119+
}
189120

190121
// This function is the same as above, except that some instructions are
191122
// [manually patched for LVI].
@@ -195,8 +126,10 @@ core::arch::global_asm!(
195126
target_arch = "x86_64",
196127
all(target_env = "sgx", target_vendor = "fortanix")
197128
))]
198-
core::arch::global_asm!(
199-
define_rust_probestack!(
129+
#[unsafe(naked)]
130+
#[no_mangle]
131+
pub unsafe extern "C" fn __rust_probestack() {
132+
core::arch::naked_asm!(
200133
"
201134
.cfi_startproc
202135
pushq %rbp
@@ -248,19 +181,21 @@ core::arch::global_asm!(
248181
lfence
249182
jmp *%r11
250183
.cfi_endproc
251-
"
252-
),
253-
options(att_syntax)
254-
);
184+
",
185+
options(att_syntax)
186+
)
187+
}
255188

256189
#[cfg(all(target_arch = "x86", not(target_os = "uefi")))]
257190
// This is the same as x86_64 above, only translated for 32-bit sizes. Note
258191
// that on Unix we're expected to restore everything as it was, this
259192
// function basically can't tamper with anything.
260193
//
261194
// The ABI here is the same as x86_64, except everything is 32-bits large.
262-
core::arch::global_asm!(
263-
define_rust_probestack!(
195+
#[unsafe(naked)]
196+
#[no_mangle]
197+
pub unsafe extern "C" fn __rust_probestack() {
198+
core::arch::naked_asm!(
264199
"
265200
.cfi_startproc
266201
push %ebp
@@ -291,10 +226,10 @@ core::arch::global_asm!(
291226
.cfi_adjust_cfa_offset -4
292227
ret
293228
.cfi_endproc
294-
"
295-
),
296-
options(att_syntax)
297-
);
229+
",
230+
options(att_syntax)
231+
)
232+
}
298233

299234
#[cfg(all(target_arch = "x86", target_os = "uefi"))]
300235
// UEFI target is windows like target. LLVM will do _chkstk things like windows.
@@ -307,8 +242,10 @@ core::arch::global_asm!(
307242
// MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves.
308243
// MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp
309244
// themselves.
310-
core::arch::global_asm!(
311-
define_rust_probestack!(
245+
#[unsafe(naked)]
246+
#[no_mangle]
247+
pub unsafe extern "C" fn __rust_probestack() {
248+
core::arch::naked_asm!(
312249
"
313250
.cfi_startproc
314251
push %ebp
@@ -344,7 +281,7 @@ core::arch::global_asm!(
344281
.cfi_adjust_cfa_offset -4
345282
ret
346283
.cfi_endproc
347-
"
348-
),
349-
options(att_syntax)
350-
);
284+
",
285+
options(att_syntax)
286+
)
287+
}

0 commit comments

Comments
 (0)