Skip to content

Commit 7ccf840

Browse files
committed
Add __rust_probestack intrinsic
Will be required for rust-lang/rust#42816
1 parent 238647a commit 7ccf840

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ pub mod arm;
5353

5454
#[cfg(target_arch = "x86_64")]
5555
pub mod x86_64;
56+
57+
pub mod probestack;

src/probestack.rs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! This module defines the `__rust_probestack` intrinsic which is used in the
12+
//! implementation of "stack probes" on certain platforms.
13+
//!
14+
//! The purpose of a stack probe is to provide a static guarantee that if a
15+
//! thread has a guard page then a stack overflow is guaranteed to hit that
16+
//! guard page. If a function did not have a stack probe then there's a risk of
17+
//! having a stack frame *larger* than the guard page, so a function call could
18+
//! skip over the guard page entirely and then later hit maybe the heap or
19+
//! another thread, possibly leading to security vulnerabilities such as [The
20+
//! Stack Clash], for example.
21+
//!
22+
//! [The Stack Clash]: https://blog.qualys.com/securitylabs/2017/06/19/the-stack-clash
23+
//!
24+
//! The `__rust_probestack` is called in the prologue of functions whose stack
25+
//! size is larger than the guard page, for example larger than 4096 bytes on
26+
//! x86. This function is then responsible for "touching" all pages relevant to
27+
//! the stack to ensure that that if any of them are the guard page we'll hit
28+
//! them guaranteed.
29+
//!
30+
//! The precise ABI for how this function operates is defined by LLVM. There's
31+
//! no real documentation as to what this is, so you'd basically need to read
32+
//! the LLVM source code for reference. Often though the test cases can be
33+
//! illuminating as to the ABI that's generated, or just looking at the output
34+
//! of `llc`.
35+
//!
36+
//! Note that `#[naked]` is typically used here for the stack probe because the
37+
//! ABI corresponds to no actual ABI.
38+
//!
39+
//! Finally it's worth noting that at the time of this writing LLVM only has
40+
//! support for stack probes on x86 and x86_64. There's no support for stack
41+
//! probes on any other architecture like ARM or PowerPC64. LLVM I'm sure would
42+
//! be more than welcome to accept such a change!
43+
44+
#![cfg(not(windows))] // Windows already has builtins to do this
45+
46+
#[naked]
47+
#[no_mangle]
48+
#[cfg(target_arch = "x86_64")]
49+
pub unsafe extern fn __rust_probestack() {
50+
// Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
51+
// ensuring that if any pages are unmapped we'll make a page fault.
52+
//
53+
// The ABI here is that the stack frame size is located in `%eax`. Upon
54+
// return we're not supposed to modify `%esp` or `%eax`.
55+
asm!("
56+
lea 8(%rsp),%r11 // rsp before calling this routine -> r11
57+
58+
// Main loop, taken in one page increments. We're decrementing r11 by
59+
// a page each time until there's less than a page remaining. We're
60+
// guaranteed that this function isn't called unless there's more than a
61+
// page needed
62+
2:
63+
sub $$0x1000,%r11
64+
test %r11,(%r11)
65+
sub $$0x1000,%rax
66+
cmp $$0x1000,%rax
67+
ja 2b
68+
69+
// Finish up the last remaining stack space requested, getting the last
70+
// bits out of rax
71+
sub %rax,%r11
72+
test %r11,(%r11)
73+
74+
// We now know that %r11 is (%rsp + 8 - %rax) so to recover rax
75+
// we calculate (%rsp + 8) - %r11 which will give us %rax
76+
lea 8(%rsp),%rax
77+
sub %r11,%rax
78+
79+
ret
80+
");
81+
::core::intrinsics::unreachable();
82+
}
83+
84+
#[naked]
85+
#[no_mangle]
86+
#[cfg(target_arch = "x86")]
87+
pub unsafe extern fn __rust_probestack() {
88+
// This is the same as x86_64 above, only translated for 32-bit sizes. Note
89+
// that on Unix we're expected to restore everything as it was, this
90+
// function basically can't tamper with anything.
91+
//
92+
// The ABI here is the same as x86_64, except everything is 32-bits large.
93+
asm!("
94+
push %ecx
95+
lea 8(%esp),%ecx
96+
2:
97+
sub $$0x1000,%ecx
98+
test %ecx,(%ecx)
99+
sub $$0x1000,%eax
100+
cmp $$0x1000,%eax
101+
ja 2b
102+
103+
sub %eax,%ecx
104+
test %ecx,(%ecx)
105+
106+
lea 8(%esp),%eax
107+
sub %ecx,%eax
108+
pop %ecx
109+
ret
110+
");
111+
::core::intrinsics::unreachable();
112+
}

0 commit comments

Comments
 (0)