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.
7
#ifndef WIN32_LEAN_AND_MEAN
8
#define WIN32_LEAN_AND_MEAN
10
#define WIN32_NO_STATUS
11
#include <stdlib.h> /* abort () */
13
#undef WIN32_NO_STATUS
14
#include <ntstatus.h> /* STATUS macros */
20
#define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
22
#define DEFAULT_SECURITY_COOKIE 0xBB40E64E
27
PRUNTIME_FUNCTION RtlLookupFunctionEntry (ULONG64, PULONG64, PVOID);
28
PVOID RtlVirtualUnwind (ULONG HandlerType, ULONG64, ULONG64, PRUNTIME_FUNCTION,
29
PCONTEXT, PVOID *, PULONG64, PVOID);
32
typedef LONG NTSTATUS; /* same as in ntdef.h / winternl.h */
34
#define UNW_FLAG_NHANDLER 0x00
38
unsigned __int64 ft_scalar;
42
static EXCEPTION_RECORD GS_ExceptionRecord;
43
static CONTEXT GS_ContextRecord;
45
static const EXCEPTION_POINTERS GS_ExceptionPointers = {
46
&GS_ExceptionRecord,&GS_ContextRecord
49
DECLSPEC_SELECTANY UINT_PTR __security_cookie = DEFAULT_SECURITY_COOKIE;
50
DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement = ~(DEFAULT_SECURITY_COOKIE);
52
void __cdecl __security_init_cookie (void);
55
__security_init_cookie (void)
59
LARGE_INTEGER perfctr;
61
if (__security_cookie != DEFAULT_SECURITY_COOKIE)
63
__security_cookie_complement = ~__security_cookie;
67
GetSystemTimeAsFileTime (&systime.ft_struct);
69
cookie = systime.ft_scalar;
71
cookie = systime.ft_struct.dwLowDateTime;
72
cookie ^= systime.ft_struct.dwHighDateTime;
75
cookie ^= GetCurrentProcessId ();
76
cookie ^= GetCurrentThreadId ();
77
cookie ^= GetTickCount ();
79
QueryPerformanceCounter (&perfctr);
81
cookie ^= perfctr.QuadPart;
83
cookie ^= perfctr.LowPart;
84
cookie ^= perfctr.HighPart;
88
cookie &= 0x0000ffffffffffffll;
91
if (cookie == DEFAULT_SECURITY_COOKIE)
92
cookie = DEFAULT_SECURITY_COOKIE + 1;
93
__security_cookie = cookie;
94
__security_cookie_complement = ~cookie;
98
#if defined(__GNUC__) /* wrap msvc intrinsics onto gcc builtins */
100
#undef _AddressOfReturnAddress
101
#define _ReturnAddress() __builtin_return_address(0)
102
#define _AddressOfReturnAddress() __builtin_frame_address (0)
103
#endif /* __GNUC__ */
105
__declspec(noreturn) void __cdecl __report_gsfailure (ULONGLONG);
107
__declspec(noreturn) void __cdecl
108
__report_gsfailure (ULONGLONG StackCookie)
110
volatile UINT_PTR cookie[2] __MINGW_ATTRIB_UNUSED;
112
ULONG64 controlPC, imgBase, establisherFrame;
113
PRUNTIME_FUNCTION fctEntry;
116
RtlCaptureContext (&GS_ContextRecord);
117
controlPC = GS_ContextRecord.Rip;
118
fctEntry = RtlLookupFunctionEntry (controlPC, &imgBase, NULL);
119
if (fctEntry != NULL)
121
RtlVirtualUnwind (UNW_FLAG_NHANDLER, imgBase, controlPC, fctEntry,
122
&GS_ContextRecord, &hndData, &establisherFrame, NULL);
128
GS_ContextRecord.Rip = (ULONGLONG) _ReturnAddress();
129
GS_ContextRecord.Rsp = (ULONGLONG) _AddressOfReturnAddress() + 8;
131
GS_ContextRecord.Eip = (DWORD) _ReturnAddress();
132
GS_ContextRecord.Esp = (DWORD) _AddressOfReturnAddress() + 4;
137
GS_ExceptionRecord.ExceptionAddress = (PVOID) GS_ContextRecord.Rip;
138
GS_ContextRecord.Rcx = StackCookie;
140
GS_ExceptionRecord.ExceptionAddress = (PVOID) GS_ContextRecord.Eip;
141
GS_ContextRecord.Ecx = StackCookie;
143
GS_ExceptionRecord.ExceptionCode = STATUS_STACK_BUFFER_OVERRUN;
144
GS_ExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
145
cookie[0] = __security_cookie;
146
cookie[1] = __security_cookie_complement;
147
SetUnhandledExceptionFilter (NULL);
148
UnhandledExceptionFilter ((EXCEPTION_POINTERS *) &GS_ExceptionPointers);
149
TerminateProcess (GetCurrentProcess (), STATUS_STACK_BUFFER_OVERRUN);