1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rt::util::report_overflow;
16 use libc::types::os::arch::extra::{LPVOID, DWORD, LONG, BOOL};
17 use sys_common::stack;
20 _data: *mut libc::c_void
24 pub unsafe fn new() -> Handler {
29 impl Drop for Handler {
33 // This is initialized in init() and only read from after
34 static mut PAGE_SIZE: uint = 0;
37 extern "system" fn vectored_handler(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG {
39 let rec = &(*(*ExceptionInfo).ExceptionRecord);
40 let code = rec.ExceptionCode;
42 if code != EXCEPTION_STACK_OVERFLOW {
43 return EXCEPTION_CONTINUE_SEARCH;
46 // We're calling into functions with stack checks,
47 // however stack checks by limit should be disabled on Windows
48 stack::record_sp_limit(0);
52 EXCEPTION_CONTINUE_SEARCH
56 pub unsafe fn init() {
57 let mut info = mem::zeroed();
58 libc::GetSystemInfo(&mut info);
59 PAGE_SIZE = info.dwPageSize as uint;
61 if AddVectoredExceptionHandler(0, vectored_handler) == ptr::null_mut() {
62 panic!("failed to install exception handler");
65 mem::forget(make_handler());
68 pub unsafe fn cleanup() {
71 pub unsafe fn make_handler() -> Handler {
72 if SetThreadStackGuarantee(&mut 0x5000) == 0 {
73 panic!("failed to reserve stack space for exception handling");
76 Handler { _data: 0i as *mut libc::c_void }
79 pub struct EXCEPTION_RECORD {
80 pub ExceptionCode: DWORD,
81 pub ExceptionFlags: DWORD,
82 pub ExceptionRecord: *mut EXCEPTION_RECORD,
83 pub ExceptionAddress: LPVOID,
84 pub NumberParameters: DWORD,
85 pub ExceptionInformation: [LPVOID, ..EXCEPTION_MAXIMUM_PARAMETERS]
88 pub struct EXCEPTION_POINTERS {
89 pub ExceptionRecord: *mut EXCEPTION_RECORD,
90 pub ContextRecord: LPVOID
93 pub type PVECTORED_EXCEPTION_HANDLER = extern "system"
94 fn(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG;
96 pub type ULONG = libc::c_ulong;
98 const EXCEPTION_CONTINUE_SEARCH: LONG = 0;
99 const EXCEPTION_MAXIMUM_PARAMETERS: uint = 15;
100 const EXCEPTION_STACK_OVERFLOW: DWORD = 0xc00000fd;
103 fn AddVectoredExceptionHandler(FirstHandler: ULONG,
104 VectoredHandler: PVECTORED_EXCEPTION_HANDLER)
106 fn SetThreadStackGuarantee(StackSizeInBytes: *mut ULONG) -> BOOL;