2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
13 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
15 #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
20 PRUNTIME_FUNCTION
RtlLookupFunctionEntry (ULONG64
, PULONG64
, PVOID
);
21 PVOID
RtlVirtualUnwind (ULONG HandlerType
, ULONG64
, ULONG64
, PRUNTIME_FUNCTION
,
22 PCONTEXT
, PVOID
*, PULONG64
, PVOID
);
25 typedef LONG NTSTATUS
;
27 #define UNW_FLAG_NHANDLER 0x00
28 #define STATUS_STACK_BUFFER_OVERRUN ((NTSTATUS)0xC0000409L)
32 unsigned __int64 ft_scalar
;
36 static EXCEPTION_RECORD GS_ExceptionRecord
;
37 static CONTEXT GS_ContextRecord
;
39 static const EXCEPTION_POINTERS GS_ExceptionPointers
= {
40 &GS_ExceptionRecord
,&GS_ContextRecord
43 DECLSPEC_SELECTANY UINT_PTR __security_cookie
= DEFAULT_SECURITY_COOKIE
;
44 DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement
= ~(DEFAULT_SECURITY_COOKIE
);
46 void __cdecl
__security_init_cookie (void);
49 __security_init_cookie (void)
53 LARGE_INTEGER perfctr
;
55 if (__security_cookie
!= DEFAULT_SECURITY_COOKIE
)
57 __security_cookie_complement
= ~__security_cookie
;
61 GetSystemTimeAsFileTime (&systime
.ft_struct
);
63 cookie
= systime
.ft_scalar
;
65 cookie
= systime
.ft_struct
.dwLowDateTime
;
66 cookie
^= systime
.ft_struct
.dwHighDateTime
;
69 cookie
^= GetCurrentProcessId ();
70 cookie
^= GetCurrentThreadId ();
71 cookie
^= GetTickCount ();
73 QueryPerformanceCounter (&perfctr
);
75 cookie
^= perfctr
.QuadPart
;
77 cookie
^= perfctr
.LowPart
;
78 cookie
^= perfctr
.HighPart
;
82 cookie
&= 0x0000ffffffffffffll
;
85 if (cookie
== DEFAULT_SECURITY_COOKIE
)
86 cookie
= DEFAULT_SECURITY_COOKIE
+ 1;
87 __security_cookie
= cookie
;
88 __security_cookie_complement
= ~cookie
;
92 #if defined(__GNUC__) /* wrap msvc intrinsics onto gcc builtins */
94 #undef _AddressOfReturnAddress
95 #define _ReturnAddress() __builtin_return_address(0)
96 #define _AddressOfReturnAddress() __builtin_frame_address (0)
99 __declspec(noreturn
) void __cdecl
__report_gsfailure (ULONGLONG
);
101 __declspec(noreturn
) void __cdecl
102 __report_gsfailure (ULONGLONG StackCookie
)
104 volatile UINT_PTR
__UNUSED_PARAM(cookie
[2]);
106 ULONG64 controlPC
, imgBase
, establisherFrame
;
107 PRUNTIME_FUNCTION fctEntry
;
110 RtlCaptureContext (&GS_ContextRecord
);
111 controlPC
= GS_ContextRecord
.Rip
;
112 fctEntry
= RtlLookupFunctionEntry (controlPC
, &imgBase
, NULL
);
113 if (fctEntry
!= NULL
)
115 RtlVirtualUnwind (UNW_FLAG_NHANDLER
, imgBase
, controlPC
, fctEntry
,
116 &GS_ContextRecord
, &hndData
, &establisherFrame
, NULL
);
122 GS_ContextRecord
.Rip
= (ULONGLONG
) _ReturnAddress();
123 GS_ContextRecord
.Rsp
= (ULONGLONG
) _AddressOfReturnAddress() + 8;
125 GS_ContextRecord
.Eip
= (DWORD
) _ReturnAddress();
126 GS_ContextRecord
.Esp
= (DWORD
) _AddressOfReturnAddress() + 4;
131 GS_ExceptionRecord
.ExceptionAddress
= (PVOID
) GS_ContextRecord
.Rip
;
132 GS_ContextRecord
.Rcx
= StackCookie
;
134 GS_ExceptionRecord
.ExceptionAddress
= (PVOID
) GS_ContextRecord
.Eip
;
135 GS_ContextRecord
.Ecx
= StackCookie
;
137 GS_ExceptionRecord
.ExceptionCode
= STATUS_STACK_BUFFER_OVERRUN
;
138 GS_ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
139 cookie
[0] = __security_cookie
;
140 cookie
[1] = __security_cookie_complement
;
141 SetUnhandledExceptionFilter (NULL
);
142 UnhandledExceptionFilter ((EXCEPTION_POINTERS
*) &GS_ExceptionPointers
);
143 TerminateProcess (GetCurrentProcess (), STATUS_STACK_BUFFER_OVERRUN
);