File tree Expand file tree Collapse file tree 3 files changed +42
-11
lines changed
library/std/src/sys/windows Expand file tree Collapse file tree 3 files changed +42
-11
lines changed Original file line number Diff line number Diff line change @@ -443,13 +443,19 @@ compat_fn_with_fallback! {
443443// Where possible, these definitions should be kept in sync with https://docs.rs/windows-sys
444444cfg_if:: cfg_if! {
445445if #[ cfg( not( target_vendor = "uwp" ) ) ] {
446- #[ link( name = "kernel32" ) ]
447- extern "system" {
446+ compat_fn_optional! {
447+ crate :: sys:: compat:: load_stack_overflow_functions( ) ;
448+ // >= Vista / Server 2003
449+ // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadstackguarantee
450+ pub fn SetThreadStackGuarantee ( stacksizeinbytes: * mut u32 ) -> BOOL ;
451+ // >= XP
452+ // https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-addvectoredexceptionhandler
448453 pub fn AddVectoredExceptionHandler (
449454 first: u32 ,
450455 handler: PVECTORED_EXCEPTION_HANDLER ,
451456 ) -> * mut c_void;
452457 }
458+
453459 pub type PVECTORED_EXCEPTION_HANDLER = Option <
454460 unsafe extern "system" fn ( exceptioninfo: * mut EXCEPTION_POINTERS ) -> i32 ,
455461 >;
Original file line number Diff line number Diff line change @@ -65,6 +65,8 @@ unsafe extern "C" fn init() {
6565
6666 // Attempt to preload the synch functions.
6767 load_synch_functions ( ) ;
68+ #[ cfg( not( target_vendor = "uwp" ) ) ]
69+ load_stack_overflow_functions ( ) ;
6870}
6971
7072/// Helper macro for creating CStrs from literals and symbol names.
@@ -360,7 +362,10 @@ macro_rules! static_load {
360362 [ $( $symbol: ident) ,* $( , ) ?]
361363 ) => {
362364 $(
363- let $symbol = $library. proc_address( ansi_str!( sym $symbol) ) ?;
365+ let $symbol = {
366+ const $symbol: & CStr = ansi_str!( sym $symbol) ;
367+ $library. proc_address( $symbol) ?
368+ } ;
364369 ) *
365370 $(
366371 c:: $symbol:: PTR . store( $symbol. as_ptr( ) , Ordering :: Relaxed ) ;
@@ -383,3 +388,20 @@ pub(super) fn load_synch_functions() {
383388
384389 try_load ( ) ;
385390}
391+
392+ #[ cfg( not( target_vendor = "uwp" ) ) ]
393+ pub ( super ) fn load_stack_overflow_functions ( ) {
394+ fn try_load ( ) -> Option < ( ) > {
395+ const MODULE_NAME : & CStr = c"kernel32" ;
396+
397+ // Try loading the library and all the required functions.
398+ // If any step fails, then they all fail.
399+ let library = unsafe { Module :: new ( MODULE_NAME ) } ?;
400+
401+ static_load ! ( library, [ SetThreadStackGuarantee , AddVectoredExceptionHandler ] ) ;
402+
403+ Some ( ( ) )
404+ }
405+
406+ try_load ( ) ;
407+ }
Original file line number Diff line number Diff line change @@ -9,13 +9,12 @@ pub struct Handler;
99
1010impl Handler {
1111 pub unsafe fn new ( ) -> Handler {
12- // This API isn't available on XP, so don't panic in that case and just
13- // pray it works out ok.
14- if c:: SetThreadStackGuarantee ( & mut 0x5000 ) == 0
15- && api:: get_last_error ( ) . code != c:: ERROR_CALL_NOT_IMPLEMENTED
16- {
17- panic ! ( "failed to reserve stack space for exception handling" ) ;
18- }
12+ if let Some ( f) = c:: SetThreadStackGuarantee :: option ( ) {
13+ if f ( & mut 0x5000 ) == 0 && api:: get_last_error ( ) . code != c:: ERROR_CALL_NOT_IMPLEMENTED {
14+ panic ! ( "failed to reserve stack space for exception handling" ) ;
15+ }
16+ } ;
17+
1918 Handler
2019 }
2120}
@@ -36,7 +35,11 @@ unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POIN
3635}
3736
3837pub unsafe fn init ( ) {
39- if c:: AddVectoredExceptionHandler ( 0 , Some ( vectored_handler) ) . is_null ( ) {
38+ let Some ( f) = c:: AddVectoredExceptionHandler :: option ( ) else {
39+ return ;
40+ } ;
41+
42+ if f ( 0 , Some ( vectored_handler) ) . is_null ( ) {
4043 panic ! ( "failed to install exception handler" ) ;
4144 }
4245 // Set the thread stack guarantee for the main thread.
You can’t perform that action at this time.
0 commit comments