std\sys\pal\windows/
stack_overflow.rs1#![cfg_attr(test, allow(dead_code))]
2
3use crate::sys::c;
4use crate::thread;
5
6pub fn reserve_stack() {
8 let result = unsafe { c::SetThreadStackGuarantee(&mut 0x5000) };
9 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 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 unsafe {
34 let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
35 debug_assert!(!result.is_null(), "failed to install exception handler");
38 }
39 reserve_stack();
41}