probestack.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //! This module defines the `__rust_probestack` intrinsic which is used in the
  11. //! implementation of "stack probes" on certain platforms.
  12. //!
  13. //! The purpose of a stack probe is to provide a static guarantee that if a
  14. //! thread has a guard page then a stack overflow is guaranteed to hit that
  15. //! guard page. If a function did not have a stack probe then there's a risk of
  16. //! having a stack frame *larger* than the guard page, so a function call could
  17. //! skip over the guard page entirely and then later hit maybe the heap or
  18. //! another thread, possibly leading to security vulnerabilities such as [The
  19. //! Stack Clash], for example.
  20. //!
  21. //! [The Stack Clash]: https://blog.qualys.com/securitylabs/2017/06/19/the-stack-clash
  22. //!
  23. //! The `__rust_probestack` is called in the prologue of functions whose stack
  24. //! size is larger than the guard page, for example larger than 4096 bytes on
  25. //! x86. This function is then responsible for "touching" all pages relevant to
  26. //! the stack to ensure that that if any of them are the guard page we'll hit
  27. //! them guaranteed.
  28. //!
  29. //! The precise ABI for how this function operates is defined by LLVM. There's
  30. //! no real documentation as to what this is, so you'd basically need to read
  31. //! the LLVM source code for reference. Often though the test cases can be
  32. //! illuminating as to the ABI that's generated, or just looking at the output
  33. //! of `llc`.
  34. //!
  35. //! Note that `#[naked]` is typically used here for the stack probe because the
  36. //! ABI corresponds to no actual ABI.
  37. //!
  38. //! Finally it's worth noting that at the time of this writing LLVM only has
  39. //! support for stack probes on x86 and x86_64. There's no support for stack
  40. //! probes on any other architecture like ARM or PowerPC64. LLVM I'm sure would
  41. //! be more than welcome to accept such a change!
  42. #![cfg(not(windows))] // Windows already has builtins to do this
  43. #[naked]
  44. #[no_mangle]
  45. #[cfg(all(target_arch = "x86_64", not(feature = "mangled-names")))]
  46. pub unsafe extern "C" fn __rust_probestack() {
  47. // Our goal here is to touch each page between %rsp+8 and %rsp+8-%rax,
  48. // ensuring that if any pages are unmapped we'll make a page fault.
  49. //
  50. // The ABI here is that the stack frame size is located in `%eax`. Upon
  51. // return we're not supposed to modify `%esp` or `%eax`.
  52. asm!("
  53. pushq %rbp
  54. movq %rsp, %rbp
  55. mov %rax,%r11 // duplicate %rax as we're clobbering %r11
  56. // Main loop, taken in one page increments. We're decrementing rsp by
  57. // a page each time until there's less than a page remaining. We're
  58. // guaranteed that this function isn't called unless there's more than a
  59. // page needed.
  60. //
  61. // Note that we're also testing against `8(%rsp)` to account for the 8
  62. // bytes pushed on the stack orginally with our return address. Using
  63. // `8(%rsp)` simulates us testing the stack pointer in the caller's
  64. // context.
  65. // It's usually called when %rax >= 0x1000, but that's not always true.
  66. // Dynamic stack allocation, which is needed to implement unsized
  67. // rvalues, triggers stackprobe even if %rax < 0x1000.
  68. // Thus we have to check %r11 first to avoid segfault.
  69. cmp $$0x1000,%r11
  70. jna 3f
  71. 2:
  72. sub $$0x1000,%rsp
  73. test %rsp,8(%rsp)
  74. sub $$0x1000,%r11
  75. cmp $$0x1000,%r11
  76. ja 2b
  77. 3:
  78. // Finish up the last remaining stack space requested, getting the last
  79. // bits out of r11
  80. sub %r11,%rsp
  81. test %rsp,8(%rsp)
  82. // Restore the stack pointer to what it previously was when entering
  83. // this function. The caller will readjust the stack pointer after we
  84. // return.
  85. add %rax,%rsp
  86. leave
  87. ret
  88. " ::: "memory" : "volatile");
  89. ::core::intrinsics::unreachable();
  90. }
  91. #[naked]
  92. #[no_mangle]
  93. #[cfg(all(target_arch = "x86", not(feature = "mangled-names")))]
  94. pub unsafe extern "C" fn __rust_probestack() {
  95. // This is the same as x86_64 above, only translated for 32-bit sizes. Note
  96. // that on Unix we're expected to restore everything as it was, this
  97. // function basically can't tamper with anything.
  98. //
  99. // The ABI here is the same as x86_64, except everything is 32-bits large.
  100. asm!("
  101. push %ebp
  102. mov %esp, %ebp
  103. push %ecx
  104. mov %eax,%ecx
  105. cmp $$0x1000,%ecx
  106. jna 3f
  107. 2:
  108. sub $$0x1000,%esp
  109. test %esp,8(%esp)
  110. sub $$0x1000,%ecx
  111. cmp $$0x1000,%ecx
  112. ja 2b
  113. 3:
  114. sub %ecx,%esp
  115. test %esp,8(%esp)
  116. add %eax,%esp
  117. pop %ecx
  118. leave
  119. ret
  120. " ::: "memory" : "volatile");
  121. ::core::intrinsics::unreachable();
  122. }