Skip to content

Implement x86 chkstk in "rust" #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ features = ["c"]
- [x] floatunsisf.c
- [ ] i386/ashldi3.S
- [ ] i386/ashrdi3.S
- [ ] i386/chkstk.S
- [ ] i386/chkstk2.S
- [x] i386/chkstk.S
- [x] i386/chkstk2.S
- [ ] i386/divdi3.S
- [ ] i386/lshrdi3.S
- [ ] i386/moddi3.S
Expand Down
2 changes: 0 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4176,8 +4176,6 @@ mod c {
&[
"i386/ashldi3.S",
"i386/ashrdi3.S",
"i386/chkstk.S",
"i386/chkstk2.S",
"i386/divdi3.S",
"i386/floatdidf.S",
"i386/floatdisf.S",
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub mod mem;
#[cfg(target_arch = "arm")]
pub mod arm;

#[cfg(target_arch = "x86")]
pub mod x86;

#[cfg(target_arch = "x86_64")]
pub mod x86_64;

Expand Down
8 changes: 4 additions & 4 deletions src/probestack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
#![cfg(not(windows))] // Windows already has builtins to do this

#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg(target_arch = "x86_64")]
#[no_mangle]
#[cfg(all(target_arch = "x86_64", not(feature = "mangled-names")))]
pub unsafe extern fn __rust_probestack() {
// 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.
Expand Down Expand Up @@ -87,8 +87,8 @@ pub unsafe extern fn __rust_probestack() {
}

#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[cfg(target_arch = "x86")]
#[no_mangle]
#[cfg(all(target_arch = "x86", not(feature = "mangled-names")))]
pub unsafe extern fn __rust_probestack() {
// 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
Expand Down
71 changes: 71 additions & 0 deletions src/x86.rs
Original file line number Diff line number Diff line change
@@ -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();
}
86 changes: 44 additions & 42 deletions src/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,64 @@ use core::intrinsics;
// 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"))]
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[no_mangle]
pub unsafe fn ___chkstk_ms() {
asm!("push %rcx
push %rax
cmp $$0x1000,%rax
lea 24(%rsp),%rcx
jb 1f
2:
sub $$0x1000,%rcx
test %rcx,(%rcx)
sub $$0x1000,%rax
cmp $$0x1000,%rax
ja 2b
1:
sub %rax,%rcx
test %rcx,(%rcx)
pop %rax
pop %rcx
ret");
asm!("
push %rcx
push %rax
cmp $$0x1000,%rax
lea 24(%rsp),%rcx
jb 1f
2:
sub $$0x1000,%rcx
test %rcx,(%rcx)
sub $$0x1000,%rax
cmp $$0x1000,%rax
ja 2b
1:
sub %rax,%rcx
test %rcx,(%rcx)
pop %rax
pop %rcx
ret");
intrinsics::unreachable();
}

#[cfg(all(windows, target_env = "gnu"))]
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[no_mangle]
pub unsafe fn __alloca() {
asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
intrinsics::unreachable();
}

#[cfg(all(windows, target_env = "gnu"))]
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
#[naked]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[no_mangle]
pub unsafe fn ___chkstk() {
asm!("push %rcx
cmp $$0x1000,%rax
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
jb 1f
2:
sub $$0x1000,%rcx
test %rcx,(%rcx)
sub $$0x1000,%rax
cmp $$0x1000,%rax
ja 2b
1:
sub %rax,%rcx
test %rcx,(%rcx)
asm!("
push %rcx
cmp $$0x1000,%rax
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
jb 1f
2:
sub $$0x1000,%rcx
test %rcx,(%rcx)
sub $$0x1000,%rax
cmp $$0x1000,%rax
ja 2b
1:
sub %rax,%rcx
test %rcx,(%rcx)

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 -8(%rax),%rcx // restore rcx
push (%rax) // push return address onto the stack
sub %rsp,%rax // restore the original value in rax
ret");
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 -8(%rax),%rcx // restore rcx
push (%rax) // push return address onto the stack
sub %rsp,%rax // restore the original value in rax
ret");
intrinsics::unreachable();
}