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! {
443
443
// Where possible, these definitions should be kept in sync with https://docs.rs/windows-sys
444
444
cfg_if:: cfg_if! {
445
445
if #[ 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
448
453
pub fn AddVectoredExceptionHandler (
449
454
first: u32 ,
450
455
handler: PVECTORED_EXCEPTION_HANDLER ,
451
456
) -> * mut c_void;
452
457
}
458
+
453
459
pub type PVECTORED_EXCEPTION_HANDLER = Option <
454
460
unsafe extern "system" fn ( exceptioninfo: * mut EXCEPTION_POINTERS ) -> i32 ,
455
461
>;
Original file line number Diff line number Diff line change @@ -65,6 +65,8 @@ unsafe extern "C" fn init() {
65
65
66
66
// Attempt to preload the synch functions.
67
67
load_synch_functions ( ) ;
68
+ #[ cfg( not( target_vendor = "uwp" ) ) ]
69
+ load_stack_overflow_functions ( ) ;
68
70
}
69
71
70
72
/// Helper macro for creating CStrs from literals and symbol names.
@@ -360,7 +362,10 @@ macro_rules! static_load {
360
362
[ $( $symbol: ident) ,* $( , ) ?]
361
363
) => {
362
364
$(
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
+ } ;
364
369
) *
365
370
$(
366
371
c:: $symbol:: PTR . store( $symbol. as_ptr( ) , Ordering :: Relaxed ) ;
@@ -383,3 +388,20 @@ pub(super) fn load_synch_functions() {
383
388
384
389
try_load ( ) ;
385
390
}
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;
9
9
10
10
impl Handler {
11
11
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
+
19
18
Handler
20
19
}
21
20
}
@@ -36,7 +35,11 @@ unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POIN
36
35
}
37
36
38
37
pub 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 ( ) {
40
43
panic ! ( "failed to install exception handler" ) ;
41
44
}
42
45
// Set the thread stack guarantee for the main thread.
You can’t perform that action at this time.
0 commit comments