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 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
);
47 __security_init_cookie (void)
51 LARGE_INTEGER perfctr
;
53 if (__security_cookie
!= DEFAULT_SECURITY_COOKIE
)
55 __security_cookie_complement
= ~__security_cookie
;
59 GetSystemTimeAsFileTime (&systime
.ft_struct
);
61 cookie
= systime
.ft_scalar
;
63 cookie
= systime
.ft_struct
.dwLowDateTime
;
64 cookie
^= systime
.ft_struct
.dwHighDateTime
;
67 cookie
^= GetCurrentProcessId ();
68 cookie
^= GetCurrentThreadId ();
69 cookie
^= GetTickCount ();
71 QueryPerformanceCounter (&perfctr
);
73 cookie
^= perfctr
.QuadPart
;
75 cookie
^= perfctr
.LowPart
;
76 cookie
^= perfctr
.HighPart
;
80 cookie
&= 0x0000ffffffffffffll
;
83 if (cookie
== DEFAULT_SECURITY_COOKIE
)
84 cookie
= DEFAULT_SECURITY_COOKIE
+ 1;
85 __security_cookie
= cookie
;
86 __security_cookie_complement
= ~cookie
;
89 __declspec(noreturn
) void __cdecl
90 __report_gsfailure (ULONGLONG StackCookie
)
92 volatile UINT_PTR cookie
[2];
94 ULONG64 controlPC
, imgBase
, establisherFrame
;
95 PRUNTIME_FUNCTION fctEntry
;
100 RtlCaptureContext (&GS_ContextRecord
);
101 controlPC
= GS_ContextRecord
.Rip
;
102 fctEntry
= RtlLookupFunctionEntry (controlPC
, &imgBase
, NULL
);
103 if (fctEntry
!= NULL
)
105 RtlVirtualUnwind (UNW_FLAG_NHANDLER
, imgBase
, controlPC
, fctEntry
,
106 &GS_ContextRecord
, &hndData
, &establisherFrame
, NULL
);
113 GS_ContextRecord
.Rip
= (ULONGLONG
) __builtin_return_address (0);
114 GS_ContextRecord
.Rsp
= (ULONGLONG
) __builtin_frame_address (0) + 8;
116 GS_ContextRecord
.Eip
= (DWORD
) __builtin_return_address (0);
117 GS_ContextRecord
.Esp
= (DWORD
) __builtin_frame_address (0) + 4;
121 GS_ContextRecord
.Rip
= (ULONGLONG
) _ReturnAddress();
122 GS_ContextRecord
.Rsp
= (ULONGLONG
) _AddressOfReturnAddress();
124 GS_ContextRecord
.Eip
= (DWORD
) _ReturnAddress();
125 GS_ContextRecord
.Esp
= (DWORD
) _AddressOfReturnAddress();
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
);