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 () */
12 //#include <windows.h>
16 #undef WIN32_NO_STATUS
17 #include <ntstatus.h> /* STATUS macros */
23 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
25 #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
30 typedef LONG NTSTATUS
; /* same as in ntdef.h / winternl.h */
32 #define UNW_FLAG_NHANDLER 0x00
36 unsigned __int64 ft_scalar
;
40 static EXCEPTION_RECORD GS_ExceptionRecord
;
41 static CONTEXT GS_ContextRecord
;
43 static const EXCEPTION_POINTERS GS_ExceptionPointers
= {
44 &GS_ExceptionRecord
,&GS_ContextRecord
47 DECLSPEC_SELECTANY UINT_PTR __security_cookie
= DEFAULT_SECURITY_COOKIE
;
48 DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement
= ~(DEFAULT_SECURITY_COOKIE
);
50 void __cdecl
__security_init_cookie (void);
53 __security_init_cookie (void)
57 LARGE_INTEGER perfctr
;
59 if (__security_cookie
!= DEFAULT_SECURITY_COOKIE
)
61 __security_cookie_complement
= ~__security_cookie
;
65 GetSystemTimeAsFileTime (&systime
.ft_struct
);
67 cookie
= systime
.ft_scalar
;
69 cookie
= systime
.ft_struct
.dwLowDateTime
;
70 cookie
^= systime
.ft_struct
.dwHighDateTime
;
73 cookie
^= GetCurrentProcessId ();
74 cookie
^= GetCurrentThreadId ();
75 cookie
^= GetTickCount ();
77 QueryPerformanceCounter (&perfctr
);
79 cookie
^= perfctr
.QuadPart
;
81 cookie
^= perfctr
.LowPart
;
82 cookie
^= perfctr
.HighPart
;
86 cookie
&= 0x0000ffffffffffffll
;
89 if (cookie
== DEFAULT_SECURITY_COOKIE
)
90 cookie
= DEFAULT_SECURITY_COOKIE
+ 1;
91 __security_cookie
= cookie
;
92 __security_cookie_complement
= ~cookie
;
96 #if defined(__GNUC__) /* wrap msvc intrinsics onto gcc builtins */
98 #undef _AddressOfReturnAddress
99 #define _ReturnAddress() __builtin_return_address(0)
100 #define _AddressOfReturnAddress() __builtin_frame_address (0)
101 #endif /* __GNUC__ */
103 __declspec(noreturn
) void __cdecl
__report_gsfailure (ULONG_PTR
);
106 __declspec(noreturn
) void __cdecl
107 __report_gsfailure (ULONG_PTR StackCookie
)
109 volatile UINT_PTR cookie
[2] __MINGW_ATTRIB_UNUSED
;
111 ULONG64 controlPC
, imgBase
, establisherFrame
;
112 PRUNTIME_FUNCTION fctEntry
;
115 RtlCaptureContext (&GS_ContextRecord
);
116 controlPC
= GS_ContextRecord
.Rip
;
117 fctEntry
= RtlLookupFunctionEntry (controlPC
, &imgBase
, NULL
);
118 if (fctEntry
!= NULL
)
120 RtlVirtualUnwind (UNW_FLAG_NHANDLER
, imgBase
, controlPC
, fctEntry
,
121 &GS_ContextRecord
, &hndData
, &establisherFrame
, NULL
);
127 GS_ContextRecord
.Rip
= (ULONGLONG
) _ReturnAddress();
128 GS_ContextRecord
.Rsp
= (ULONGLONG
) _AddressOfReturnAddress() + 8;
130 GS_ContextRecord
.Eip
= (DWORD
) _ReturnAddress();
131 GS_ContextRecord
.Esp
= (DWORD
) _AddressOfReturnAddress() + 4;
136 GS_ExceptionRecord
.ExceptionAddress
= (PVOID
) GS_ContextRecord
.Rip
;
137 GS_ContextRecord
.Rcx
= StackCookie
;
139 GS_ExceptionRecord
.ExceptionAddress
= (PVOID
) GS_ContextRecord
.Eip
;
140 GS_ContextRecord
.Ecx
= StackCookie
;
142 GS_ExceptionRecord
.ExceptionCode
= STATUS_STACK_BUFFER_OVERRUN
;
143 GS_ExceptionRecord
.ExceptionFlags
= EXCEPTION_NONCONTINUABLE
;
144 cookie
[0] = __security_cookie
;
145 cookie
[1] = __security_cookie_complement
;
146 SetUnhandledExceptionFilter (NULL
);
147 UnhandledExceptionFilter ((EXCEPTION_POINTERS
*) &GS_ExceptionPointers
);
148 TerminateProcess (GetCurrentProcess (), STATUS_STACK_BUFFER_OVERRUN
);
151 #endif /* !_MSC_VER */