Auto merge of #176 - alexcrichton:probestack2, r=alexcrichton

Tweak definition of probestack functions

It looks like the old `__rust_probestack` routine is incompatible with newer
linux kernels. My best guess for this is that the kernel's auto-growth logic is
failing to trigger, causing what looks like a legitimate segfault to get
delivered. My best guess for why *that's* happening is that the faulting address
is below `%rsp`, whereas previously all faulting stack addresses were above
`%rsp`. The probestack routine does not modify `%rsp` as it's probing the stack,
and presumably newer kernels are interpreting this as a legitimate violation.

This commit tweaks the probestack routine to instead update `%rsp` incrementally
as probing happens. The ABI of the function, however, requires that `%rsp`
isn't changed as part of the function so it's restored at the end to the
previous value.
This commit is contained in:
bors 2017-07-08 03:36:04 +00:00
commit 5e49856003
6 changed files with 51 additions and 36 deletions

View File

@ -21,9 +21,14 @@ fn main() {
#[cfg(feature = "gen-tests")] #[cfg(feature = "gen-tests")]
tests::generate(); tests::generate();
// Build missing intrinsics from compiler-rt C source code // Build missing intrinsics from compiler-rt C source code. If we're
#[cfg(feature = "c")] // mangling names though we assume that we're also in test mode so we don't
c::compile(&llvm_target); // build anything and we rely on the upstream implementation of compiler-rt
// functions
if !cfg!(feature = "mangled-names") {
#[cfg(feature = "c")]
c::compile(&llvm_target);
}
// To compile intrinsics.rs for thumb targets, where there is no libc // To compile intrinsics.rs for thumb targets, where there is no libc
if llvm_target[0].starts_with("thumb") { if llvm_target[0].starts_with("thumb") {
@ -4099,11 +4104,9 @@ mod c {
// also needs to satisfy intrinsics that jemalloc or C in general may // also needs to satisfy intrinsics that jemalloc or C in general may
// need, so include a few more that aren't typically needed by // need, so include a few more that aren't typically needed by
// LLVM/Rust. // LLVM/Rust.
if env::var_os("CARGO_FEATURE_RUSTBUILD").is_some() { sources.extend(&[
sources.extend(&[ "ffsdi2.c",
"ffsdi2.c", ]);
]);
}
if target_os != "ios" { if target_os != "ios" {
sources.extend( sources.extend(

View File

@ -93,6 +93,7 @@ for rlib in $(echo $path); do
uniq -d | \ uniq -d | \
grep -v __x86.get_pc_thunk | \ grep -v __x86.get_pc_thunk | \
grep -v __builtin_cl | \ grep -v __builtin_cl | \
grep -v __builtin_ctz | \
grep 'T __' grep 'T __'
if test $? = 0; then if test $? = 0; then

View File

@ -13,6 +13,8 @@
#![feature(lang_items)] #![feature(lang_items)]
#![feature(start)] #![feature(start)]
#![feature(i128_type)] #![feature(i128_type)]
#![feature(global_allocator)]
#![feature(allocator_api)]
#![cfg_attr(windows, feature(panic_unwind))] #![cfg_attr(windows, feature(panic_unwind))]
#![no_std] #![no_std]
@ -22,6 +24,10 @@ extern crate compiler_builtins;
#[cfg(windows)] #[cfg(windows)]
extern crate panic_unwind; extern crate panic_unwind;
#[cfg(not(thumb))]
#[global_allocator]
static A: alloc_system::System = alloc_system::System;
// NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even // NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even
// compiler-rt provides a C/assembly implementation. // compiler-rt provides a C/assembly implementation.

View File

@ -112,8 +112,9 @@ intrinsics! {
int_to_float!(i, u32, f64) int_to_float!(i, u32, f64)
} }
#[use_c_shim_if(all(any(target_arch = "x86", target_arch = "x86_64"), #[use_c_shim_if(all(not(target_env = "msvc"),
not(windows)))] any(target_arch = "x86",
all(not(windows), target_arch = "x86_64"))))]
#[arm_aeabi_alias = __aeabi_ul2d] #[arm_aeabi_alias = __aeabi_ul2d]
pub extern "C" fn __floatundidf(i: u64) -> f64 { pub extern "C" fn __floatundidf(i: u64) -> f64 {
int_to_float!(i, u64, f64) int_to_float!(i, u64, f64)

View File

@ -44,7 +44,7 @@
#![cfg(not(windows))] // Windows already has builtins to do this #![cfg(not(windows))] // Windows already has builtins to do this
#[naked] #[naked]
#[no_mangle] #[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub unsafe extern fn __rust_probestack() { pub unsafe extern fn __rust_probestack() {
// Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax, // Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
@ -53,28 +53,33 @@ pub unsafe extern fn __rust_probestack() {
// The ABI here is that the stack frame size is located in `%eax`. Upon // The ABI here is that the stack frame size is located in `%eax`. Upon
// return we're not supposed to modify `%esp` or `%eax`. // return we're not supposed to modify `%esp` or `%eax`.
asm!(" asm!("
lea 8(%rsp),%r11 // rsp before calling this routine -> r11 mov %rax,%r11 // duplicate %rax as we're clobbering %r11
// Main loop, taken in one page increments. We're decrementing r11 by // Main loop, taken in one page increments. We're decrementing rsp by
// a page each time until there's less than a page remaining. We're // a page each time until there's less than a page remaining. We're
// guaranteed that this function isn't called unless there's more than a // guaranteed that this function isn't called unless there's more than a
// page needed // page needed.
//
// Note that we're also testing against `8(%rsp)` to account for the 8
// bytes pushed on the stack orginally with our return address. Using
// `8(%rsp)` simulates us testing the stack pointer in the caller's
// context.
2: 2:
sub $$0x1000,%rsp
test %rsp,8(%rsp)
sub $$0x1000,%r11 sub $$0x1000,%r11
test %r11,(%r11) cmp $$0x1000,%r11
sub $$0x1000,%rax
cmp $$0x1000,%rax
ja 2b ja 2b
// Finish up the last remaining stack space requested, getting the last // Finish up the last remaining stack space requested, getting the last
// bits out of rax // bits out of r11
sub %rax,%r11 sub %r11,%rsp
test %r11,(%r11) test %rsp,8(%rsp)
// We now know that %r11 is (%rsp + 8 - %rax) so to recover rax // Restore the stack pointer to what it previously was when entering
// we calculate (%rsp + 8) - %r11 which will give us %rax // this function. The caller will readjust the stack pointer after we
lea 8(%rsp),%rax // return.
sub %r11,%rax add %rax,%rsp
ret ret
"); ");
@ -82,7 +87,7 @@ pub unsafe extern fn __rust_probestack() {
} }
#[naked] #[naked]
#[no_mangle] #[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
pub unsafe extern fn __rust_probestack() { pub unsafe extern fn __rust_probestack() {
// This is the same as x86_64 above, only translated for 32-bit sizes. Note // This is the same as x86_64 above, only translated for 32-bit sizes. Note
@ -92,19 +97,18 @@ pub unsafe extern fn __rust_probestack() {
// The ABI here is the same as x86_64, except everything is 32-bits large. // The ABI here is the same as x86_64, except everything is 32-bits large.
asm!(" asm!("
push %ecx push %ecx
lea 8(%esp),%ecx mov %eax,%ecx
2: 2:
sub $$0x1000,%esp
test %esp,8(%esp)
sub $$0x1000,%ecx sub $$0x1000,%ecx
test %ecx,(%ecx) cmp $$0x1000,%ecx
sub $$0x1000,%eax
cmp $$0x1000,%eax
ja 2b ja 2b
sub %eax,%ecx sub %ecx,%esp
test %ecx,(%ecx) test %esp,8(%esp)
lea 8(%esp),%eax add %eax,%esp
sub %ecx,%eax
pop %ecx pop %ecx
ret ret
"); ");

View File

@ -10,7 +10,7 @@ use core::intrinsics;
#[cfg(windows)] #[cfg(windows)]
#[naked] #[naked]
#[no_mangle] #[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe fn ___chkstk_ms() { pub unsafe fn ___chkstk_ms() {
asm!("push %rcx asm!("push %rcx
push %rax push %rax
@ -34,7 +34,7 @@ pub unsafe fn ___chkstk_ms() {
#[cfg(windows)] #[cfg(windows)]
#[naked] #[naked]
#[no_mangle] #[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe fn __alloca() { pub unsafe fn __alloca() {
asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"); jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
@ -43,7 +43,7 @@ pub unsafe fn __alloca() {
#[cfg(windows)] #[cfg(windows)]
#[naked] #[naked]
#[no_mangle] #[cfg_attr(not(feature = "mangled-names"), no_mangle)]
pub unsafe fn ___chkstk() { pub unsafe fn ___chkstk() {
asm!("push %rcx asm!("push %rcx
cmp $$0x1000,%rax cmp $$0x1000,%rax