From b3679bcfba64e423972af9ec872baa17207bb1c7 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Fri, 15 Sep 2017 17:18:54 -0500 Subject: [PATCH 1/2] Implement x86 chkstk in "rust" --- README.md | 4 +-- build.rs | 2 -- src/lib.rs | 3 +++ src/x86.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/x86_64.rs | 74 ++++++++++++++++++++++++++------------------------- 5 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 src/x86.rs diff --git a/README.md b/README.md index 3571200..6356996 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.rs b/build.rs index 8b5695b..74e506e 100644 --- a/build.rs +++ b/build.rs @@ -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", diff --git a/src/lib.rs b/src/lib.rs index b8cf83e..d30a218 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/x86.rs b/src/x86.rs new file mode 100644 index 0000000..554b08f --- /dev/null +++ b/src/x86.rs @@ -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"))] +#[naked] +#[cfg_attr(not(feature = "mangled-names"), 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"))] +#[naked] +#[cfg_attr(not(feature = "mangled-names"), 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"))] +#[naked] +#[cfg_attr(not(feature = "mangled-names"), 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(); +} diff --git a/src/x86_64.rs b/src/x86_64.rs index bc3c4ee..221161f 100644 --- a/src/x86_64.rs +++ b/src/x86_64.rs @@ -12,23 +12,24 @@ use core::intrinsics; #[naked] #[cfg_attr(not(feature = "mangled-names"), 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(); } @@ -45,25 +46,26 @@ pub unsafe fn __alloca() { #[naked] #[cfg_attr(not(feature = "mangled-names"), 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(); } From a40cc6a12df6e17b3e785af142c3c4d92b25800e Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Fri, 15 Sep 2017 18:04:11 -0500 Subject: [PATCH 2/2] Don't compile probestack functions during testing --- src/probestack.rs | 8 ++++---- src/x86.rs | 12 ++++++------ src/x86_64.rs | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/probestack.rs b/src/probestack.rs index 329e272..13ea5a5 100644 --- a/src/probestack.rs +++ b/src/probestack.rs @@ -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. @@ -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 diff --git a/src/x86.rs b/src/x86.rs index 554b08f..3e540e2 100644 --- a/src/x86.rs +++ b/src/x86.rs @@ -8,9 +8,9 @@ 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 %ecx @@ -34,17 +34,17 @@ pub unsafe fn ___chkstk_ms() { } // FIXME: __alloca should be an alias to __chkstk -#[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!("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 %ecx diff --git a/src/x86_64.rs b/src/x86_64.rs index 221161f..09b0081 100644 --- a/src/x86_64.rs +++ b/src/x86_64.rs @@ -8,9 +8,9 @@ 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 @@ -33,18 +33,18 @@ pub unsafe fn ___chkstk_ms() { 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