std\sys\pal\windows/
stack_overflow.rs

1#![cfg_attr(test, allow(dead_code))]
2
3use crate::sys::c;
4use crate::thread;
5
6/// Reserve stack space for use in stack overflow exceptions.
7pub fn reserve_stack() {
8    let result = unsafe { c::SetThreadStackGuarantee(&mut 0x5000) };
9    // Reserving stack space is not critical so we allow it to fail in the released build of libstd.
10    // We still use debug assert here so that CI will test that we haven't made a mistake calling the function.
11    debug_assert_ne!(result, 0, "failed to reserve stack space for exception handling");
12}
13
14unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -> i32 {
15    // SAFETY: It's up to the caller (which in this case is the OS) to ensure that `ExceptionInfo` is valid.
16    unsafe {
17        let rec = &(*(*ExceptionInfo).ExceptionRecord);
18        let code = rec.ExceptionCode;
19
20        if code == c::EXCEPTION_STACK_OVERFLOW {
21            thread::with_current_name(|name| {
22                let name = name.unwrap_or("<unknown>");
23                rtprintpanic!("\nthread '{name}' has overflowed its stack\n");
24            });
25        }
26        c::EXCEPTION_CONTINUE_SEARCH
27    }
28}
29
30pub fn init() {
31    // SAFETY: `vectored_handler` has the correct ABI and is safe to call during exception handling.
32    unsafe {
33        let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
34        // Similar to the above, adding the stack overflow handler is allowed to fail
35        // but a debug assert is used so CI will still test that it normally works.
36        debug_assert!(!result.is_null(), "failed to install exception handler");
37    }
38    // Set the thread stack guarantee for the main thread.
39    reserve_stack();
40}