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                let tid = thread::current_os_id();
24                rtprintpanic!("\nthread '{name}' ({tid}) has overflowed its stack\n");
25            });
26        }
27        c::EXCEPTION_CONTINUE_SEARCH
28    }
29}
30
31pub fn init() {
32    // SAFETY: `vectored_handler` has the correct ABI and is safe to call during exception handling.
33    unsafe {
34        let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
35        // Similar to the above, adding the stack overflow handler is allowed to fail
36        // but a debug assert is used so CI will still test that it normally works.
37        debug_assert!(!result.is_null(), "failed to install exception handler");
38    }
39    // Set the thread stack guarantee for the main thread.
40    reserve_stack();
41}