Browse Source

Implement x86 chkstk in "rust"

Matt Ickstadt 7 years ago
parent
commit
b3679bcfba
5 changed files with 114 additions and 40 deletions
  1. 2 2
      README.md
  2. 0 2
      build.rs
  3. 3 0
      src/lib.rs
  4. 71 0
      src/x86.rs
  5. 38 36
      src/x86_64.rs

+ 2 - 2
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

+ 0 - 2
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",

+ 3 - 0
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;
 

+ 71 - 0
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();
+}

+ 38 - 36
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();
 }