Skip to content

Commit 5258981

Browse files
committed
merge the sgx/fortanix __rust_probestack into the general x86_64 one
1 parent 8f9de82 commit 5258981

File tree

1 file changed

+23
-73
lines changed

1 file changed

+23
-73
lines changed

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

Lines changed: 23 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -132,81 +132,31 @@ macro_rules! define_rust_probestack {
132132
//
133133
// The ABI here is that the stack frame size is located in `%rax`. Upon
134134
// return we're not supposed to modify `%rsp` or `%rax`.
135-
//
136-
// Any changes to this function should be replicated to the SGX version below.
137-
#[cfg(all(
138-
target_arch = "x86_64",
139-
not(all(target_env = "sgx", target_vendor = "fortanix"))
140-
))]
135+
#[cfg(target_arch = "x86_64")]
141136
#[unsafe(naked)]
142137
#[rustc_std_internal_symbol]
143138
pub unsafe extern "C" fn __rust_probestack() {
144-
core::arch::naked_asm!(
145-
"
146-
.cfi_startproc
147-
pushq %rbp
148-
.cfi_adjust_cfa_offset 8
149-
.cfi_offset %rbp, -16
150-
movq %rsp, %rbp
151-
.cfi_def_cfa_register %rbp
152-
153-
mov %rax,%r11 // duplicate %rax as we're clobbering %r11
139+
#[cfg(not(all(target_env = "sgx", target_vendor = "fortanix")))]
140+
macro_rules! ret {
141+
() => {
142+
"ret"
143+
};
144+
}
154145

155-
// Main loop, taken in one page increments. We're decrementing rsp by
156-
// a page each time until there's less than a page remaining. We're
157-
// guaranteed that this function isn't called unless there's more than a
158-
// page needed.
159-
//
160-
// Note that we're also testing against `8(%rsp)` to account for the 8
161-
// bytes pushed on the stack orginally with our return address. Using
162-
// `8(%rsp)` simulates us testing the stack pointer in the caller's
163-
// context.
164-
165-
// It's usually called when %rax >= 0x1000, but that's not always true.
166-
// Dynamic stack allocation, which is needed to implement unsized
167-
// rvalues, triggers stackprobe even if %rax < 0x1000.
168-
// Thus we have to check %r11 first to avoid segfault.
169-
cmp $0x1000,%r11
170-
jna 3f
171-
2:
172-
sub $0x1000,%rsp
173-
test %rsp,8(%rsp)
174-
sub $0x1000,%r11
175-
cmp $0x1000,%r11
176-
ja 2b
177-
178-
3:
179-
// Finish up the last remaining stack space requested, getting the last
180-
// bits out of r11
181-
sub %r11,%rsp
182-
test %rsp,8(%rsp)
183-
184-
// Restore the stack pointer to what it previously was when entering
185-
// this function. The caller will readjust the stack pointer after we
186-
// return.
187-
add %rax,%rsp
188-
189-
leave
190-
.cfi_def_cfa_register %rsp
191-
.cfi_adjust_cfa_offset -8
192-
ret
193-
.cfi_endproc
194-
",
195-
options(att_syntax)
196-
)
197-
}
146+
#[cfg(all(target_env = "sgx", target_vendor = "fortanix"))]
147+
macro_rules! ret {
148+
// for this target, [manually patch for LVI].
149+
//
150+
// [manually patch for LVI]: https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions
151+
() => {
152+
"
153+
pop %r11
154+
lfence
155+
jmp *%r11
156+
"
157+
};
158+
}
198159

199-
// This function is the same as above, except that some instructions are
200-
// [manually patched for LVI].
201-
//
202-
// [manually patched for LVI]: https://software.intel.com/security-software-guidance/insights/deep-dive-load-value-injection#specialinstructions
203-
#[cfg(all(
204-
target_arch = "x86_64",
205-
all(target_env = "sgx", target_vendor = "fortanix")
206-
))]
207-
#[unsafe(naked)]
208-
#[no_mangle]
209-
pub unsafe extern "C" fn __rust_probestack() {
210160
core::arch::naked_asm!(
211161
"
212162
.cfi_startproc
@@ -255,9 +205,9 @@ pub unsafe extern "C" fn __rust_probestack() {
255205
leave
256206
.cfi_def_cfa_register %rsp
257207
.cfi_adjust_cfa_offset -8
258-
pop %r11
259-
lfence
260-
jmp *%r11
208+
",
209+
ret!(),
210+
"
261211
.cfi_endproc
262212
",
263213
options(att_syntax)

0 commit comments

Comments
 (0)