ReactOS  0.4.13-dev-563-g0561610
gs_support.c
Go to the documentation of this file.
1 
7 #ifndef WIN32_LEAN_AND_MEAN
8 #define WIN32_LEAN_AND_MEAN
9 #endif
10 #define WIN32_NO_STATUS
11 #include <stdlib.h> /* abort () */
12 //#include <windows.h>
13 #include <stdarg.h>
14 #include <windef.h>
15 #include <winbase.h>
16 #undef WIN32_NO_STATUS
17 #include <ntstatus.h> /* STATUS macros */
18 #ifdef _WIN64
19 #include <intrin.h>
20 #endif
21 
22 #ifdef _WIN64
23 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
24 #else
25 #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
26 #endif
27 
28 /* Externals. */
29 
30 typedef LONG NTSTATUS; /* same as in ntdef.h / winternl.h */
31 
32 #define UNW_FLAG_NHANDLER 0x00
33 
34 typedef union
35 {
36  unsigned __int64 ft_scalar;
38 } FT;
39 
40 #ifndef _MSC_VER
43 
46 };
47 #endif
48 
51 
53 
54 void __cdecl
56 {
57  UINT_PTR cookie;
58  FT systime = { 0, };
59  LARGE_INTEGER perfctr;
60 
62  {
64  return;
65  }
66 
68 #ifdef _WIN64
69  cookie = systime.ft_scalar;
70 #else
71  cookie = systime.ft_struct.dwLowDateTime;
72  cookie ^= systime.ft_struct.dwHighDateTime;
73 #endif
74 
75  cookie ^= GetCurrentProcessId ();
76  cookie ^= GetCurrentThreadId ();
77  cookie ^= GetTickCount ();
78 
79  QueryPerformanceCounter (&perfctr);
80 #ifdef _WIN64
81  cookie ^= perfctr.QuadPart;
82 #else
83  cookie ^= perfctr.LowPart;
84  cookie ^= perfctr.HighPart;
85 #endif
86 
87 #ifdef _WIN64
88  cookie &= 0x0000ffffffffffffll;
89 #endif
90 
91  if (cookie == DEFAULT_SECURITY_COOKIE)
92  cookie = DEFAULT_SECURITY_COOKIE + 1;
93  __security_cookie = cookie;
95 }
96 
97 
98 #if defined(__GNUC__) /* wrap msvc intrinsics onto gcc builtins */
99 #undef _ReturnAddress
100 #undef _AddressOfReturnAddress
101 #define _ReturnAddress() __builtin_return_address(0)
102 #define _AddressOfReturnAddress() __builtin_frame_address (0)
103 #endif /* __GNUC__ */
104 
105 __declspec(noreturn) void __cdecl __report_gsfailure (ULONG_PTR);
106 
107 #ifndef _MSC_VER
108 __declspec(noreturn) void __cdecl
109 __report_gsfailure (ULONG_PTR StackCookie)
110 {
111  volatile UINT_PTR cookie[2] __MINGW_ATTRIB_UNUSED;
112 #ifdef _WIN64
113  ULONG64 controlPC, imgBase, establisherFrame;
114  PRUNTIME_FUNCTION fctEntry;
115  PVOID hndData;
116 
118  controlPC = GS_ContextRecord.Rip;
119  fctEntry = RtlLookupFunctionEntry (controlPC, &imgBase, NULL);
120  if (fctEntry != NULL)
121  {
122  RtlVirtualUnwind (UNW_FLAG_NHANDLER, imgBase, controlPC, fctEntry,
123  &GS_ContextRecord, &hndData, &establisherFrame, NULL);
124  }
125  else
126 #endif /* _WIN64 */
127  {
128 #ifdef _WIN64
131 #else
134 #endif /* _WIN64 */
135  }
136 
137 #ifdef _WIN64
139  GS_ContextRecord.Rcx = StackCookie;
140 #else
142  GS_ContextRecord.Ecx = StackCookie;
143 #endif /* _WIN64 */
146  cookie[0] = __security_cookie;
147  cookie[1] = __security_cookie_complement;
151  abort();
152 }
153 #endif /* !_MSC_VER */
VOID WINAPI GetSystemTimeAsFileTime(OUT PFILETIME lpFileTime)
Definition: time.c:128
ULONG Esp
Definition: nt_native.h:1479
ULONG Eip
Definition: nt_native.h:1476
#define __cdecl
Definition: accygwin.h:79
FILETIME ft_struct
Definition: gs_support.c:37
ULONG Ecx
Definition: nt_native.h:1467
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
#define EXCEPTION_NONCONTINUABLE
Definition: rtltypes.h:150
void * _AddressOfReturnAddress(void)
#define DEFAULT_SECURITY_COOKIE
Definition: gs_support.c:25
PEXCEPTION_ROUTINE NTAPI RtlVirtualUnwind(_In_ ULONG HandlerType, _In_ ULONG64 ImageBase, _In_ ULONG64 ControlPc, _In_ PRUNTIME_FUNCTION FunctionEntry, _Inout_ PCONTEXT Context, _Outptr_ PVOID *HandlerData, _Out_ PULONG64 EstablisherFrame, _Inout_ PKNONVOLATILE_CONTEXT_POINTERS ContextPointers)
Definition: unwind.c:349
#define DECLSPEC_SELECTANY
Definition: guiddef.h:40
NTSYSAPI VOID NTAPI RtlCaptureContext(_Out_ PCONTEXT ContextRecord)
#define DWORD
Definition: nt_native.h:44
PVOID ExceptionAddress
Definition: compat.h:199
void __cdecl __security_init_cookie(void)
Definition: gs_support.c:55
uint32_t ULONG_PTR
Definition: typedefs.h:63
DWORD ExceptionCode
Definition: compat.h:196
DWORD dwHighDateTime
Definition: mapidefs.h:66
long LONG
Definition: pedump.c:60
#define STATUS_STACK_BUFFER_OVERRUN
Definition: ntstatus.h:932
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI DECLSPEC_HOTPATCH SetUnhandledExceptionFilter(IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
Definition: except.c:792
LONG WINAPI UnhandledExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo)
Definition: except.c:262
static const EXCEPTION_POINTERS GS_ExceptionPointers
Definition: gs_support.c:44
static EXCEPTION_RECORD GS_ExceptionRecord
Definition: gs_support.c:41
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
DWORD WINAPI GetCurrentThreadId(VOID)
Definition: thread.c:420
PRUNTIME_FUNCTION WINAPI RtlLookupFunctionEntry(ULONG_PTR, DWORD *, UNWIND_HISTORY_TABLE *)
uint64_t ULONGLONG
Definition: typedefs.h:65
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
#define __MINGW_ATTRIB_UNUSED
Definition: _mingw.h:122
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
unsigned __int64 ULONG64
Definition: imports.h:198
BOOL WINAPI QueryPerformanceCounter(OUT PLARGE_INTEGER lpPerformanceCount)
Definition: perfcnt.c:23
DECLSPEC_SELECTANY UINT_PTR __security_cookie_complement
Definition: gs_support.c:50
ULONG LowPart
Definition: typedefs.h:104
__declspec(noreturn)
Definition: gs_support.c:105
Definition: gs_support.c:34
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1562
#define abort()
Definition: i386-dis.c:35
w ll
Definition: byte_order.h:166
unsigned __int64 ft_scalar
Definition: gs_support.c:36
static CONTEXT GS_ContextRecord
Definition: gs_support.c:42
void * _ReturnAddress(void)
DWORD ExceptionFlags
Definition: compat.h:197
LONG NTSTATUS
Definition: gs_support.c:30
DWORD dwLowDateTime
Definition: mapidefs.h:65
#define __int64
Definition: basetyps.h:16
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1188
LONGLONG QuadPart
Definition: typedefs.h:112
DECLSPEC_SELECTANY UINT_PTR __security_cookie
Definition: gs_support.c:49
#define UNW_FLAG_NHANDLER
Definition: gs_support.c:32