wasmer_vm/probestack/mod.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// Based on `compiler-builtins` crate with changes in `#[cfg(...)]`:
18// https://raw.githubusercontent.com/rust-lang/compiler-builtins/319637f544d9dda8fc3dd482d9979e0da135a258/compiler-builtins/src/probestack.rs
19mod compiler_builtins;
20
21// A declaration for the stack probe function in Rust's standard library, for
22// catching callstack overflow.
23cfg_select! {
24 all(
25 target_os = "windows",
26 target_env = "msvc",
27 target_pointer_width = "64"
28 ) => {
29 unsafe extern "C" {
30 pub fn __chkstk();
31 }
32 /// The probestack for 64bit Windows when compiled with MSVC (note the double underscore)
33 pub const PROBESTACK: unsafe extern "C" fn() = __chkstk;
34 }
35 all(
36 target_os = "windows",
37 target_env = "msvc",
38 target_pointer_width = "32"
39 ) => {
40 unsafe extern "C" {
41 pub fn _chkstk();
42 }
43 /// The probestack for 32bit Windows when compiled with MSVC (note the singular underscore)
44 pub const PROBESTACK: unsafe extern "C" fn() = _chkstk;
45 }
46 all(target_os = "windows", target_env = "gnu") => {
47 unsafe extern "C" {
48 // ___chkstk (note the triple underscore) is implemented in compiler-builtins/src/x86_64.rs
49 // by the Rust compiler for the MinGW target
50 #[cfg(all(target_os = "windows", target_env = "gnu"))]
51 pub fn ___chkstk_ms();
52 }
53 /// The probestack for Windows when compiled with GNU
54 pub const PROBESTACK: unsafe extern "C" fn() = ___chkstk_ms;
55 }
56 not(any(target_arch = "x86_64", target_arch = "x86")) => {
57 // As per
58 // https://github.com/rust-lang/compiler-builtins/blob/cae3e6ea23739166504f9f9fb50ec070097979d4/src/probestack.rs#L39,
59 // LLVM only has stack-probe support on x86-64 and x86. Thus, on any other CPU
60 // architecture, we simply use an empty stack-probe function.
61 extern "C" fn empty_probestack() {}
62 /// A default probestack for other architectures
63 pub const PROBESTACK: unsafe extern "C" fn() = empty_probestack;
64 }
65 _ => {
66 /// The probestack based on the Rust probestack
67 pub static PROBESTACK: unsafe extern "C" fn() = compiler_builtins::__rust_probestack;
68 }
69}