Skip to content

Commit 634cc43

Browse files
seritoolsmbilker
authored andcommitted
Only enable stack overflow exception detection if the system supports it
1 parent 4cee186 commit 634cc43

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

library/std/src/sys/windows/c.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,27 @@ pub unsafe fn NtWriteFile(
319319
compat_fn_with_fallback! {
320320
pub static KERNEL32: &CStr = ansi_str!("kernel32");
321321

322+
// >= XP
323+
// https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-addvectoredexceptionhandler
324+
pub fn AddVectoredExceptionHandler(
325+
FirstHandler: ULONG,
326+
VectoredHandler: PVECTORED_EXCEPTION_HANDLER,
327+
) -> LPVOID {
328+
panic!("unavailable")
329+
}
330+
322331
// >= Win10 1607
323332
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
324333
pub fn SetThreadDescription(hthread: HANDLE, lpthreaddescription: PCWSTR) -> HRESULT {
325334
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
326335
}
327336

337+
// >= Vista / Server 2003
338+
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadstackguarantee
339+
pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
340+
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
341+
}
342+
328343
// >= Win8 / Server 2012
329344
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
330345
pub fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime: *mut FILETIME) -> () {

library/std/src/sys/windows/c/windows_sys.lst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,6 @@ Windows.Win32.System.Console.STD_HANDLE
24182418
Windows.Win32.System.Console.STD_INPUT_HANDLE
24192419
Windows.Win32.System.Console.STD_OUTPUT_HANDLE
24202420
Windows.Win32.System.Console.WriteConsoleW
2421-
Windows.Win32.System.Diagnostics.Debug.AddVectoredExceptionHandler
24222421
Windows.Win32.System.Diagnostics.Debug.ARM64_NT_NEON128
24232422
Windows.Win32.System.Diagnostics.Debug.CONTEXT
24242423
Windows.Win32.System.Diagnostics.Debug.CONTEXT
@@ -2545,7 +2544,6 @@ Windows.Win32.System.Threading.ReleaseSRWLockShared
25452544
Windows.Win32.System.Threading.RTL_CONDITION_VARIABLE
25462545
Windows.Win32.System.Threading.RTL_RUN_ONCE
25472546
Windows.Win32.System.Threading.RTL_SRWLOCK
2548-
Windows.Win32.System.Threading.SetThreadStackGuarantee
25492547
Windows.Win32.System.Threading.Sleep
25502548
Windows.Win32.System.Threading.SleepConditionVariableSRW
25512549
Windows.Win32.System.Threading.SleepEx

library/std/src/sys/windows/c/windows_sys.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ extern "system" {
3939
pub fn AcquireSRWLockShared(srwlock: *mut RTL_SRWLOCK) -> ();
4040
}
4141
#[link(name = "kernel32")]
42-
extern "system" {
43-
pub fn AddVectoredExceptionHandler(
44-
first: u32,
45-
handler: PVECTORED_EXCEPTION_HANDLER,
46-
) -> *mut ::core::ffi::c_void;
47-
}
48-
#[link(name = "kernel32")]
4942
extern "system" {
5043
pub fn CancelIo(hfile: HANDLE) -> BOOL;
5144
}
@@ -497,10 +490,6 @@ extern "system" {
497490
pub fn SetLastError(dwerrcode: WIN32_ERROR) -> ();
498491
}
499492
#[link(name = "kernel32")]
500-
extern "system" {
501-
pub fn SetThreadStackGuarantee(stacksizeinbytes: *mut u32) -> BOOL;
502-
}
503-
#[link(name = "kernel32")]
504493
extern "system" {
505494
pub fn Sleep(dwmilliseconds: u32) -> ();
506495
}

library/std/src/sys/windows/stack_overflow.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ unsafe extern "system" fn vectored_handler(ExceptionInfo: *mut c::EXCEPTION_POIN
3434
}
3535

3636
pub unsafe fn init() {
37+
// This API was added in XP
38+
if !c::AddVectoredExceptionHandler::available() {
39+
return;
40+
}
41+
3742
if c::AddVectoredExceptionHandler(0, Some(vectored_handler)).is_null() {
3843
panic!("failed to install exception handler");
3944
}

0 commit comments

Comments
 (0)