-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathsentry_wer.c
More file actions
260 lines (228 loc) · 7.43 KB
/
Copy pathsentry_wer.c
File metadata and controls
260 lines (228 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "sentry_crash_context.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <werapi.h>
#include <windows.h>
#ifndef STATUS_FAIL_FAST_EXCEPTION
# define STATUS_FAIL_FAST_EXCEPTION ((DWORD)0xC0000602)
#endif
#ifndef STATUS_STACK_BUFFER_OVERRUN
# define STATUS_STACK_BUFFER_OVERRUN ((DWORD)0xC0000409)
#endif
typedef struct {
DWORD dwSize;
HANDLE hProcess;
HANDLE hThread;
EXCEPTION_RECORD exceptionRecord;
CONTEXT context;
PCWSTR pwszReportId;
BOOL bIsFatal;
DWORD dwReserved;
} WER_RUNTIME_EXCEPTION_INFORMATION_19041;
static BOOL
is_fatal_wer_exception(const WER_RUNTIME_EXCEPTION_INFORMATION *info)
{
// bIsFatal is missing in older SDKs; guard access with dwSize.
if (!info
|| info->dwSize
<= offsetof(WER_RUNTIME_EXCEPTION_INFORMATION_19041, bIsFatal)) {
return FALSE;
}
return ((const WER_RUNTIME_EXCEPTION_INFORMATION_19041 *)info)->bIsFatal;
}
static PCWSTR
get_report_id(const WER_RUNTIME_EXCEPTION_INFORMATION *info)
{
// pwszReportId is missing in older SDKs; guard access with dwSize.
if (!info
|| info->dwSize <= offsetof(
WER_RUNTIME_EXCEPTION_INFORMATION_19041, pwszReportId)) {
return NULL;
}
return ((const WER_RUNTIME_EXCEPTION_INFORMATION_19041 *)info)
->pwszReportId;
}
static BOOL
is_native_wer_exception(DWORD code)
{
return code == STATUS_FAIL_FAST_EXCEPTION
|| code == STATUS_STACK_BUFFER_OVERRUN;
}
static BOOL
read_registration(
HANDLE process, PVOID context, sentry_wer_registration_t *registration)
{
if (!process || !context || !registration) {
return FALSE;
}
if (!ReadProcessMemory(
process, context, registration, sizeof(*registration), NULL)) {
return FALSE;
}
return registration->version == 1 && registration->app_pid != 0;
}
static BOOL
open_native_crash_objects(const sentry_wer_registration_t *registration,
HANDLE *mapping, HANDLE *event, sentry_crash_context_t **ctx)
{
wchar_t shm_name[SENTRY_CRASH_IPC_NAME_SIZE];
wchar_t event_name[SENTRY_CRASH_IPC_NAME_SIZE];
swprintf(shm_name, SENTRY_CRASH_IPC_NAME_SIZE,
L"Local\\SentryCrash-%lu-%llx", (unsigned long)registration->app_pid,
(unsigned long long)registration->app_tid);
swprintf(event_name, SENTRY_CRASH_IPC_NAME_SIZE,
L"Local\\SentryCrashEvent-%lu-%llx",
(unsigned long)registration->app_pid,
(unsigned long long)registration->app_tid);
*mapping = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, shm_name);
if (!*mapping) {
return FALSE;
}
*ctx = MapViewOfFile(
*mapping, FILE_MAP_ALL_ACCESS, 0, 0, SENTRY_CRASH_SHM_SIZE);
if (!*ctx) {
CloseHandle(*mapping);
*mapping = NULL;
return FALSE;
}
if ((*ctx)->magic != SENTRY_CRASH_MAGIC) {
UnmapViewOfFile(*ctx);
CloseHandle(*mapping);
*ctx = NULL;
*mapping = NULL;
return FALSE;
}
*event = OpenEventW(EVENT_MODIFY_STATE, FALSE, event_name);
if (!*event) {
UnmapViewOfFile(*ctx);
CloseHandle(*mapping);
*ctx = NULL;
*mapping = NULL;
return FALSE;
}
return TRUE;
}
static BOOL
process_wer_exception(
PVOID context, const WER_RUNTIME_EXCEPTION_INFORMATION *exception_info)
{
sentry_wer_registration_t registration = { 0 };
if (!exception_info
|| !read_registration(
exception_info->hProcess, context, ®istration)) {
return FALSE;
}
BOOL claimed = FALSE;
HANDLE mapping = NULL;
HANDLE event = NULL;
sentry_crash_context_t *ctx = NULL;
if (!open_native_crash_objects(®istration, &mapping, &event, &ctx)) {
return FALSE;
}
PCWSTR report_id = get_report_id(exception_info);
if (report_id) {
WideCharToMultiByte(CP_UTF8, 0, report_id, -1,
ctx->platform.wer_report_id,
(int)sizeof(ctx->platform.wer_report_id), NULL, NULL);
}
// advance POSTPROCESSING -> POSTPROCESSED
if (InterlockedCompareExchange(&ctx->state,
SENTRY_CRASH_STATE_POSTPROCESSED, SENTRY_CRASH_STATE_POSTPROCESSING)
== SENTRY_CRASH_STATE_POSTPROCESSING) {
goto done;
}
if (!is_fatal_wer_exception(exception_info)
|| !is_native_wer_exception(
exception_info->exceptionRecord.ExceptionCode)) {
goto done;
}
if (InterlockedCompareExchange(&ctx->state, SENTRY_CRASH_STATE_PROCESSING,
SENTRY_CRASH_STATE_READY)
== SENTRY_CRASH_STATE_READY) {
ctx->crashed_pid = GetProcessId(exception_info->hProcess);
ctx->crashed_tid = GetThreadId(exception_info->hThread);
ctx->platform.exception_code
= exception_info->exceptionRecord.ExceptionCode;
ctx->platform.exception_record = exception_info->exceptionRecord;
ctx->platform.context = exception_info->context;
ctx->platform.exception_pointers = NULL;
ctx->platform.num_threads = 1;
ctx->platform.threads[0].thread_id = ctx->crashed_tid;
ctx->platform.threads[0].context = exception_info->context;
InterlockedExchange(&ctx->state, SENTRY_CRASH_STATE_CRASHED);
if (SetEvent(event)) {
claimed = TRUE;
uint64_t timeout_ms = ctx->shutdown_timeout
? ctx->shutdown_timeout
: SENTRY_CRASH_HANDLER_WAIT_TIMEOUT_MS;
for (uint64_t waited_ms = 0; waited_ms < timeout_ms;
waited_ms += SENTRY_CRASH_HANDLER_POLL_INTERVAL_MS) {
if (InterlockedCompareExchange(&ctx->state,
SENTRY_CRASH_STATE_DONE, SENTRY_CRASH_STATE_DONE)
>= SENTRY_CRASH_STATE_CAPTURED) {
break;
}
Sleep(SENTRY_CRASH_HANDLER_POLL_INTERVAL_MS);
}
TerminateProcess(exception_info->hProcess,
exception_info->exceptionRecord.ExceptionCode);
}
}
done:
CloseHandle(event);
UnmapViewOfFile(ctx);
CloseHandle(mapping);
return claimed;
}
BOOL WINAPI
DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
(void)instance;
(void)reason;
(void)reserved;
return TRUE;
}
HRESULT WINAPI
OutOfProcessExceptionEventCallback(PVOID context,
const PWER_RUNTIME_EXCEPTION_INFORMATION exception_info,
BOOL *ownership_claimed, PWSTR event_name, PDWORD event_name_size,
PDWORD signature_count)
{
(void)event_name;
(void)event_name_size;
(void)signature_count;
*ownership_claimed = FALSE;
if (process_wer_exception(context, exception_info)) {
*ownership_claimed = TRUE;
}
return S_OK;
}
HRESULT WINAPI
OutOfProcessExceptionEventSignatureCallback(PVOID context,
const PWER_RUNTIME_EXCEPTION_INFORMATION exception_info, DWORD index,
PWSTR name, PDWORD name_size, PWSTR value, PDWORD value_size)
{
(void)context;
(void)exception_info;
(void)index;
(void)name;
(void)name_size;
(void)value;
(void)value_size;
return E_FAIL;
}
HRESULT WINAPI
OutOfProcessExceptionEventDebuggerLaunchCallback(PVOID context,
const PWER_RUNTIME_EXCEPTION_INFORMATION exception_info,
PBOOL is_custom_debugger, PWSTR debugger_launch,
PDWORD debugger_launch_size, PBOOL is_debugger_autolaunch)
{
(void)context;
(void)exception_info;
(void)is_custom_debugger;
(void)debugger_launch;
(void)debugger_launch_size;
(void)is_debugger_autolaunch;
return E_FAIL;
}