probestack.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. 2:
  64. sub $$0x1000,%rsp
  65. test %rsp,8(%rsp)
  66. sub $$0x1000,%r11
  67. cmp $$0x1000,%r11
  68. ja 2b
  69. // Finish up the last remaining stack space requested, getting the last
  70. // bits out of r11
  71. sub %r11,%rsp
  72. test %rsp,8(%rsp)
  73. // Restore the stack pointer to what it previously was when entering
  74. // this function. The caller will readjust the stack pointer after we
  75. // return.
  76. add %rax,%rsp
  77. ret
  78. " ::: "memory" : "volatile");
  79. ::core::intrinsics::unreachable();
  80. }
  81. #[naked]
  82. #[no_mangle]
  83. #[cfg(all(target_arch = "x86", not(feature = "mangled-names")))]
  84. pub unsafe extern fn __rust_probestack() {
  85. // This is the same as x86_64 above, only translated for 32-bit sizes. Note
  86. // that on Unix we're expected to restore everything as it was, this
  87. // function basically can't tamper with anything.
  88. //
  89. // The ABI here is the same as x86_64, except everything is 32-bits large.
  90. asm!("
  91. push %ecx
  92. mov %eax,%ecx
  93. 2:
  94. sub $$0x1000,%esp
  95. test %esp,8(%esp)
  96. sub $$0x1000,%ecx
  97. cmp $$0x1000,%ecx
  98. ja 2b
  99. sub %ecx,%esp
  100. test %esp,8(%esp)
  101. add %eax,%esp
  102. pop %ecx
  103. ret
  104. " ::: "memory" : "volatile");
  105. ::core::intrinsics::unreachable();
  106. }