]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/stack_overflow.rs
Merge commit 'd3a2366ee877075c59b38bd8ced55f224fc7ef51' into sync_cg_clif-2022-07-26
[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::thread;
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             && c::GetLastError() as u32 != c::ERROR_CALL_NOT_IMPLEMENTED as u32
14         {
15             panic!("failed to reserve stack space for exception handling");
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             rtprintpanic!(
28                 "\nthread '{}' has overflowed its stack\n",
29                 thread::current().name().unwrap_or("<unknown>")
30             );
31         }
32         c::EXCEPTION_CONTINUE_SEARCH
33     }
34 }
35
36 pub unsafe fn init() {
37     if c::AddVectoredExceptionHandler(0, vectored_handler).is_null() {
38         panic!("failed to install exception handler");
39     }
40     // Set the thread stack guarantee for the main thread.
41     let _h = Handler::new();
42 }