Auto merge of #194 - mattico:i386, r=alexcrichton
Implement x86 chkstk in "rust" cc #183 Basically the same as the x86_64 ones, except `__alloca` doesn't need to fix the parameter register. I've manually verified that the disassembly is the same, and that these work in a compiled rust program. The second commit disables compiling probestack functions for `feature = mangled-names`. They aren't needed during testing because they aren't comparison tested and the unmangled versions are the ones that actually get used. r? @alexcrichton
This commit is contained in:
commit
4d9df62fb7
@ -159,8 +159,8 @@ features = ["c"]
|
|||||||
- [x] floatunsisf.c
|
- [x] floatunsisf.c
|
||||||
- [ ] i386/ashldi3.S
|
- [ ] i386/ashldi3.S
|
||||||
- [ ] i386/ashrdi3.S
|
- [ ] i386/ashrdi3.S
|
||||||
- [ ] i386/chkstk.S
|
- [x] i386/chkstk.S
|
||||||
- [ ] i386/chkstk2.S
|
- [x] i386/chkstk2.S
|
||||||
- [ ] i386/divdi3.S
|
- [ ] i386/divdi3.S
|
||||||
- [ ] i386/lshrdi3.S
|
- [ ] i386/lshrdi3.S
|
||||||
- [ ] i386/moddi3.S
|
- [ ] i386/moddi3.S
|
||||||
|
2
build.rs
2
build.rs
@ -4176,8 +4176,6 @@ mod c {
|
|||||||
&[
|
&[
|
||||||
"i386/ashldi3.S",
|
"i386/ashldi3.S",
|
||||||
"i386/ashrdi3.S",
|
"i386/ashrdi3.S",
|
||||||
"i386/chkstk.S",
|
|
||||||
"i386/chkstk2.S",
|
|
||||||
"i386/divdi3.S",
|
"i386/divdi3.S",
|
||||||
"i386/floatdidf.S",
|
"i386/floatdidf.S",
|
||||||
"i386/floatdisf.S",
|
"i386/floatdisf.S",
|
||||||
|
@ -51,6 +51,9 @@ pub mod mem;
|
|||||||
#[cfg(target_arch = "arm")]
|
#[cfg(target_arch = "arm")]
|
||||||
pub mod arm;
|
pub mod arm;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "x86")]
|
||||||
|
pub mod x86;
|
||||||
|
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
pub mod x86_64;
|
pub mod x86_64;
|
||||||
|
|
||||||
|
@ -44,8 +44,8 @@
|
|||||||
#![cfg(not(windows))] // Windows already has builtins to do this
|
#![cfg(not(windows))] // Windows already has builtins to do this
|
||||||
|
|
||||||
#[naked]
|
#[naked]
|
||||||
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
|
#[no_mangle]
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(all(target_arch = "x86_64", not(feature = "mangled-names")))]
|
||||||
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,
|
||||||
// ensuring that if any pages are unmapped we'll make a page fault.
|
// ensuring that if any pages are unmapped we'll make a page fault.
|
||||||
@ -87,8 +87,8 @@ pub unsafe extern fn __rust_probestack() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[naked]
|
#[naked]
|
||||||
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
|
#[no_mangle]
|
||||||
#[cfg(target_arch = "x86")]
|
#[cfg(all(target_arch = "x86", not(feature = "mangled-names")))]
|
||||||
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
|
||||||
// that on Unix we're expected to restore everything as it was, this
|
// that on Unix we're expected to restore everything as it was, this
|
||||||
|
71
src/x86.rs
Normal file
71
src/x86.rs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
use core::intrinsics;
|
||||||
|
|
||||||
|
// NOTE These functions are implemented using assembly because they using a custom
|
||||||
|
// calling convention which can't be implemented using a normal Rust function
|
||||||
|
|
||||||
|
// NOTE These functions are never mangled as they are not tested against compiler-rt
|
||||||
|
// and mangling ___chkstk would break the `jmp ___chkstk` instruction in __alloca
|
||||||
|
|
||||||
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
|
#[naked]
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe fn ___chkstk_ms() {
|
||||||
|
asm!("
|
||||||
|
push %ecx
|
||||||
|
push %eax
|
||||||
|
cmp $$0x1000,%eax
|
||||||
|
lea 12(%esp),%ecx
|
||||||
|
jb 1f
|
||||||
|
2:
|
||||||
|
sub $$0x1000,%ecx
|
||||||
|
test %ecx,(%ecx)
|
||||||
|
sub $$0x1000,%eax
|
||||||
|
cmp $$0x1000,%eax
|
||||||
|
ja 2b
|
||||||
|
1:
|
||||||
|
sub %eax,%ecx
|
||||||
|
test %ecx,(%ecx)
|
||||||
|
pop %eax
|
||||||
|
pop %ecx
|
||||||
|
ret");
|
||||||
|
intrinsics::unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: __alloca should be an alias to __chkstk
|
||||||
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
|
#[naked]
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe fn __alloca() {
|
||||||
|
asm!("jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
|
||||||
|
intrinsics::unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
|
#[naked]
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe fn ___chkstk() {
|
||||||
|
asm!("
|
||||||
|
push %ecx
|
||||||
|
cmp $$0x1000,%eax
|
||||||
|
lea 8(%esp),%ecx // esp before calling this routine -> ecx
|
||||||
|
jb 1f
|
||||||
|
2:
|
||||||
|
sub $$0x1000,%ecx
|
||||||
|
test %ecx,(%ecx)
|
||||||
|
sub $$0x1000,%eax
|
||||||
|
cmp $$0x1000,%eax
|
||||||
|
ja 2b
|
||||||
|
1:
|
||||||
|
sub %eax,%ecx
|
||||||
|
test %ecx,(%ecx)
|
||||||
|
|
||||||
|
lea 4(%esp),%eax // load pointer to the return address into eax
|
||||||
|
mov %ecx,%esp // install the new top of stack pointer into esp
|
||||||
|
mov -4(%eax),%ecx // restore ecx
|
||||||
|
push (%eax) // push return address onto the stack
|
||||||
|
sub %esp,%eax // restore the original value in eax
|
||||||
|
ret");
|
||||||
|
intrinsics::unreachable();
|
||||||
|
}
|
@ -8,62 +8,64 @@ use core::intrinsics;
|
|||||||
// NOTE These functions are never mangled as they are not tested against compiler-rt
|
// NOTE These functions are never mangled as they are not tested against compiler-rt
|
||||||
// and mangling ___chkstk would break the `jmp ___chkstk` instruction in __alloca
|
// and mangling ___chkstk would break the `jmp ___chkstk` instruction in __alloca
|
||||||
|
|
||||||
#[cfg(all(windows, target_env = "gnu"))]
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
#[naked]
|
#[naked]
|
||||||
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
|
#[no_mangle]
|
||||||
pub unsafe fn ___chkstk_ms() {
|
pub unsafe fn ___chkstk_ms() {
|
||||||
asm!("push %rcx
|
asm!("
|
||||||
push %rax
|
push %rcx
|
||||||
cmp $$0x1000,%rax
|
push %rax
|
||||||
lea 24(%rsp),%rcx
|
cmp $$0x1000,%rax
|
||||||
jb 1f
|
lea 24(%rsp),%rcx
|
||||||
2:
|
jb 1f
|
||||||
sub $$0x1000,%rcx
|
2:
|
||||||
test %rcx,(%rcx)
|
sub $$0x1000,%rcx
|
||||||
sub $$0x1000,%rax
|
test %rcx,(%rcx)
|
||||||
cmp $$0x1000,%rax
|
sub $$0x1000,%rax
|
||||||
ja 2b
|
cmp $$0x1000,%rax
|
||||||
1:
|
ja 2b
|
||||||
sub %rax,%rcx
|
1:
|
||||||
test %rcx,(%rcx)
|
sub %rax,%rcx
|
||||||
pop %rax
|
test %rcx,(%rcx)
|
||||||
pop %rcx
|
pop %rax
|
||||||
ret");
|
pop %rcx
|
||||||
|
ret");
|
||||||
intrinsics::unreachable();
|
intrinsics::unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(windows, target_env = "gnu"))]
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
#[naked]
|
#[naked]
|
||||||
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
|
#[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");
|
||||||
intrinsics::unreachable();
|
intrinsics::unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(windows, target_env = "gnu"))]
|
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
|
||||||
#[naked]
|
#[naked]
|
||||||
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
|
#[no_mangle]
|
||||||
pub unsafe fn ___chkstk() {
|
pub unsafe fn ___chkstk() {
|
||||||
asm!("push %rcx
|
asm!("
|
||||||
cmp $$0x1000,%rax
|
push %rcx
|
||||||
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
|
cmp $$0x1000,%rax
|
||||||
jb 1f
|
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
|
||||||
2:
|
jb 1f
|
||||||
sub $$0x1000,%rcx
|
2:
|
||||||
test %rcx,(%rcx)
|
sub $$0x1000,%rcx
|
||||||
sub $$0x1000,%rax
|
test %rcx,(%rcx)
|
||||||
cmp $$0x1000,%rax
|
sub $$0x1000,%rax
|
||||||
ja 2b
|
cmp $$0x1000,%rax
|
||||||
1:
|
ja 2b
|
||||||
sub %rax,%rcx
|
1:
|
||||||
test %rcx,(%rcx)
|
sub %rax,%rcx
|
||||||
|
test %rcx,(%rcx)
|
||||||
|
|
||||||
lea 8(%rsp),%rax // load pointer to the return address into rax
|
lea 8(%rsp),%rax // load pointer to the return address into rax
|
||||||
mov %rcx,%rsp // install the new top of stack pointer into rsp
|
mov %rcx,%rsp // install the new top of stack pointer into rsp
|
||||||
mov -8(%rax),%rcx // restore rcx
|
mov -8(%rax),%rcx // restore rcx
|
||||||
push (%rax) // push return address onto the stack
|
push (%rax) // push return address onto the stack
|
||||||
sub %rsp,%rax // restore the original value in rax
|
sub %rsp,%rax // restore the original value in rax
|
||||||
ret");
|
ret");
|
||||||
intrinsics::unreachable();
|
intrinsics::unreachable();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user