wasmer_vm/probestack.rs
1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4//! This section defines the `PROBESTACK` intrinsic which is used in the
5//! implementation of "stack probes" on certain platforms.
6//!
7//! The purpose of a stack probe is to provide a static guarantee that if a
8//! thread has a guard page then a stack overflow is guaranteed to hit that
9//! guard page. If a function did not have a stack probe then there's a risk of
10//! having a stack frame *larger* than the guard page, so a function call could
11//! skip over the guard page entirely and then later hit maybe the heap or
12//! another thread, possibly leading to security vulnerabilities such as [The
13//! Stack Clash], for example.
14//!
15//! [The Stack Clash]: https://blog.qualys.com/securitylabs/2017/06/19/the-stack-clash
16
17// A declaration for the stack probe function in Rust's standard library, for
18// catching callstack overflow.
19cfg_if::cfg_if! {
20 if #[cfg(all(
21 target_os = "windows",
22 target_env = "msvc",
23 target_pointer_width = "64"
24 ))] {
25 extern "C" {
26 pub fn __chkstk();
27 }
28 /// The probestack for 64bit Windows when compiled with MSVC (note the double underscore)
29 pub const PROBESTACK: unsafe extern "C" fn() = __chkstk;
30 } else if #[cfg(all(
31 target_os = "windows",
32 target_env = "msvc",
33 target_pointer_width = "32"
34 ))] {
35 extern "C" {
36 pub fn _chkstk();
37 }
38 /// The probestack for 32bit Windows when compiled with MSVC (note the singular underscore)
39 pub const PROBESTACK: unsafe extern "C" fn() = _chkstk;
40 } else if #[cfg(all(target_os = "windows", target_env = "gnu"))] {
41 extern "C" {
42 // ___chkstk (note the triple underscore) is implemented in compiler-builtins/src/x86_64.rs
43 // by the Rust compiler for the MinGW target
44 #[cfg(all(target_os = "windows", target_env = "gnu"))]
45 pub fn ___chkstk_ms();
46 }
47 /// The probestack for Windows when compiled with GNU
48 pub const PROBESTACK: unsafe extern "C" fn() = ___chkstk_ms;
49 } else if #[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] {
50 // As per
51 // https://github.com/rust-lang/compiler-builtins/blob/cae3e6ea23739166504f9f9fb50ec070097979d4/src/probestack.rs#L39,
52 // LLVM only has stack-probe support on x86-64 and x86. Thus, on any other CPU
53 // architecture, we simply use an empty stack-probe function.
54 extern "C" fn empty_probestack() {}
55 /// A default probestack for other architectures
56 pub const PROBESTACK: unsafe extern "C" fn() = empty_probestack;
57 } else {
58 extern "C" {
59 pub fn __rust_probestack();
60 }
61 /// The probestack based on the Rust probestack
62 pub static PROBESTACK: unsafe extern "C" fn() = __rust_probestack;
63 }
64}