]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/stack_overflow.rs
Auto merge of #74802 - Mark-Simulacrum:reland-74069, r=nnethercote
[rust.git] / library / std / src / sys / windows / stack_overflow.rs
1 #![cfg_attr(test, allow(dead_code))]
2
3 use crate::sys::c;
4 use crate::sys_common::util::report_overflow;
5
6 pub struct Handler;
7
8 impl Handler {
9     pub unsafe fn new() -> Handler {
10         // This API isn't available on XP, so don't panic in that case and just
11         // pray it works out ok.
12         if c::SetThreadStackGuarantee(&mut 0x5000) == 0 {
13             if c::GetLastError() as u32 != c::ERROR_CALL_NOT_IMPLEMENTED as u32 {
14                 panic!("failed to reserve stack space for exception handling");
15             }
16         }
17         Handler
18     }
19 }
20
21 extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POINTERS) -> c::LONG {
22     unsafe {
23         let rec = &(*(*ExceptionInfo).ExceptionRecord);
24         let code = rec.ExceptionCode;
25
26         if code == c::EXCEPTION_STACK_OVERFLOW {
27             report_overflow();
28         }
29         c::EXCEPTION_CONTINUE_SEARCH
30     }
31 }
32
33 pub unsafe fn init() {
34     if c::AddVectoredExceptionHandler(0, vectored_handler).is_null() {
35         panic!("failed to install exception handler");
36     }
37     // Set the thread stack guarantee for the main thread.
38     let _h = Handler::new();
39 }
40
41 pub unsafe fn cleanup() {}