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 rtprintpanic!("\nthread '{name}' has overflowed its stack\n");
24 });
25 }
26 c::EXCEPTION_CONTINUE_SEARCH
27 }
28}
29
30pub fn init() {
31 unsafe {
33 let result = c::AddVectoredExceptionHandler(0, Some(vectored_handler));
34 debug_assert!(!result.is_null(), "failed to install exception handler");
37 }
38 reserve_stack();
40}