probestack.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 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. mov %rax,%r11 // duplicate %rax as we're clobbering %r11
  54. // Main loop, taken in one page increments. We're decrementing rsp by
  55. // a page each time until there's less than a page remaining. We're
  56. // guaranteed that this function isn't called unless there's more than a
  57. // page needed.
  58. //
  59. // Note that we're also testing against `8(%rsp)` to account for the 8
  60. // bytes pushed on the stack orginally with our return address. Using
  61. // `8(%rsp)` simulates us testing the stack pointer in the caller's
  62. // context.
  63. // It's usually called when %rax >= 0x1000, but that's not always true.
  64. // Dynamic stack allocation, which is needed to implement unsized
  65. // rvalues, triggers stackprobe even if %rax < 0x1000.
  66. // Thus we have to check %r11 first to avoid segfault.
  67. cmp $$0x1000,%r11
  68. jna 3f
  69. 2:
  70. sub $$0x1000,%rsp
  71. test %rsp,8(%rsp)
  72. sub $$0x1000,%r11
  73. cmp $$0x1000,%r11
  74. ja 2b
  75. 3:
  76. // Finish up the last remaining stack space requested, getting the last
  77. // bits out of r11
  78. sub %r11,%rsp
  79. test %rsp,8(%rsp)
  80. // Restore the stack pointer to what it previously was when entering
  81. // this function. The caller will readjust the stack pointer after we
  82. // return.
  83. add %rax,%rsp
  84. ret
  85. " ::: "memory" : "volatile");
  86. ::core::intrinsics::unreachable();
  87. }
  88. #[naked]
  89. #[no_mangle]
  90. #[cfg(all(target_arch = "x86", not(feature = "mangled-names")))]
  91. pub unsafe extern fn __rust_probestack() {
  92. // This is the same as x86_64 above, only translated for 32-bit sizes. Note
  93. // that on Unix we're expected to restore everything as it was, this
  94. // function basically can't tamper with anything.
  95. //
  96. // The ABI here is the same as x86_64, except everything is 32-bits large.
  97. asm!("
  98. push %ecx
  99. mov %eax,%ecx
  100. cmp $$0x1000,%ecx
  101. jna 3f
  102. 2:
  103. sub $$0x1000,%esp
  104. test %esp,8(%esp)
  105. sub $$0x1000,%ecx
  106. cmp $$0x1000,%ecx
  107. ja 2b
  108. 3:
  109. sub %ecx,%esp
  110. test %esp,8(%esp)
  111. add %eax,%esp
  112. pop %ecx
  113. ret
  114. " ::: "memory" : "volatile");
  115. ::core::intrinsics::unreachable();
  116. }