Skip to content

Commit b723b3f

Browse files
committed
Add default exception handler for win32
1 parent 3b71833 commit b723b3f

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

lib/std/os/win32/exception.c3

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
module std::os::win32 @if(env::WIN32);
2+
3+
import std::io;
4+
5+
const EXCEPTION_MAXIMUM_PARAMETERS = 15;
6+
7+
struct ExceptionRecord
8+
{
9+
Win32_DWORD exception_code;
10+
Win32_DWORD exception_flags;
11+
ExceptionRecord* exception_record;
12+
void* exception_address;
13+
Win32_DWORD number_parameters;
14+
Win32_ULONG_PTR[EXCEPTION_MAXIMUM_PARAMETERS] exception_information;
15+
}
16+
17+
struct ExceptionContext @align(16)
18+
{
19+
Win32_DWORD64 p1_home;
20+
Win32_DWORD64 p2_home;
21+
Win32_DWORD64 p3_home;
22+
Win32_DWORD64 p4_home;
23+
Win32_DWORD64 p5_home;
24+
Win32_DWORD64 p6_home;
25+
26+
Win32_DWORD context_flags;
27+
Win32_DWORD mx_csr;
28+
29+
Win32_WORD seg_cs;
30+
Win32_WORD seg_ds;
31+
Win32_WORD seg_es;
32+
Win32_WORD seg_fs;
33+
Win32_WORD seg_gs;
34+
Win32_WORD seg_ss;
35+
Win32_DWORD eflags;
36+
37+
Win32_DWORD64 dr0;
38+
Win32_DWORD64 dr1;
39+
Win32_DWORD64 dr2;
40+
Win32_DWORD64 dr3;
41+
Win32_DWORD64 dr6;
42+
Win32_DWORD64 dr7;
43+
44+
Win32_DWORD64 rax;
45+
Win32_DWORD64 rcx;
46+
Win32_DWORD64 rdx;
47+
Win32_DWORD64 rbx;
48+
Win32_DWORD64 rsp;
49+
Win32_DWORD64 rbp;
50+
Win32_DWORD64 rsi;
51+
Win32_DWORD64 rdi;
52+
Win32_DWORD64 r8;
53+
Win32_DWORD64 r9;
54+
Win32_DWORD64 r10;
55+
Win32_DWORD64 r11;
56+
Win32_DWORD64 r12;
57+
Win32_DWORD64 r13;
58+
Win32_DWORD64 r14;
59+
Win32_DWORD64 r15;
60+
61+
Win32_DWORD64 rip;
62+
63+
union
64+
{
65+
struct flt_save @align(16)
66+
{
67+
Win32_WORD control_word;
68+
Win32_WORD status_word;
69+
Win32_BYTE tag_word;
70+
Win32_BYTE reserved1;
71+
Win32_WORD error_opcode;
72+
Win32_DWORD error_offset;
73+
Win32_WORD error_selector;
74+
Win32_WORD reserved2;
75+
Win32_DWORD data_offset;
76+
Win32_WORD data_selector;
77+
Win32_WORD reserved3;
78+
Win32_DWORD mx_csr;
79+
Win32_DWORD mx_csr_mask;
80+
81+
Win32_M128A[16] xmm_registers;
82+
Win32_BYTE[96] reserved4;
83+
}
84+
struct
85+
{
86+
Win32_M128A[2] header;
87+
Win32_M128A[8] legacy;
88+
Win32_M128A xmm0;
89+
Win32_M128A xmm1;
90+
Win32_M128A xmm2;
91+
Win32_M128A xmm3;
92+
Win32_M128A xmm4;
93+
Win32_M128A xmm5;
94+
Win32_M128A xmm6;
95+
Win32_M128A xmm7;
96+
Win32_M128A xmm8;
97+
Win32_M128A xmm9;
98+
Win32_M128A xmm10;
99+
Win32_M128A xmm11;
100+
Win32_M128A xmm12;
101+
Win32_M128A xmm13;
102+
Win32_M128A xmm14;
103+
Win32_M128A xmm15;
104+
}
105+
}
106+
107+
Win32_M128A[26] vector_registers;
108+
Win32_DWORD64 vector_control;
109+
110+
Win32_DWORD64 debug_control;
111+
Win32_DWORD64 last_branch_to_rip;
112+
Win32_DWORD64 last_branch_from_rip;
113+
Win32_DWORD64 last_exception_to_rip;
114+
Win32_DWORD64 last_exception_from_rip;
115+
}
116+
117+
struct ExceptionPointers
118+
{
119+
ExceptionRecord* exception_record;
120+
ExceptionContext* context_record;
121+
}
122+
123+
alias UnhandledExceptionFilter = fn Win32_LONG (ExceptionPointers* exception_info);
124+
const EXCEPTION_EXECUTE_HANDLER = 1;
125+
126+
extern fn void debugBreak() @extern("DebugBreak") @if(env::WIN32);
127+
extern fn bool isDebuggerPresent() @extern("IsDebuggerPresent") @if(env::WIN32);
128+
extern fn UnhandledExceptionFilter setUnhandledExceptionFilter(UnhandledExceptionFilter filter) @extern("SetUnhandledExceptionFilter") @if(env::WIN32);
129+
130+
UnhandledExceptionFilter previous_filter;
131+
PanicFn previous_panic;
132+
bool has_panicked;
133+
134+
fn Win32_LONG exceptionHandler(ExceptionPointers* exception_info)
135+
{
136+
if (!has_panicked)
137+
{
138+
@stack_mem(512; Allocator allocator)
139+
{
140+
DString s;
141+
s.init(allocator: allocator);
142+
Win32_DWORD code = exception_info.exception_record.exception_code;
143+
void* addr = exception_info.exception_record.exception_address;
144+
switch (code)
145+
{
146+
case 0x80000001: s.appendf("Guard page violation at address %p", addr);
147+
case 0x80000002: s.appendf("Datatype misalignment at address %p", addr);
148+
case 0xC0000005: s.appendf("Access Violation at address %p", addr);
149+
case 0xC0000006: s.appendf("In page error at address %p", addr);
150+
case 0xC000001D: s.appendf("Illegal instruction at address %p", addr);
151+
case 0xC000008C: s.appendf("Array bounds exceeded at address %p", addr);
152+
case 0xC000008D: s.appendf("Flt denormal operand at address %p", addr);
153+
case 0xC000008E: s.appendf("Flt divide by zero at address %p", addr);
154+
case 0xC0000090: s.appendf("Flt invalid operation at address %p", addr);
155+
case 0xC0000094: s.appendf("Integer divide by zero at address %p", addr);
156+
case 0xC00000FD: s.appendf("Stack overflow at address %p", addr);
157+
case 0xC0000096: s.appendf("Privileged instruction at address %p", addr);
158+
case 0xC0000374: s.appendf("Heap corruption detected at address %p", addr);
159+
case 0xC0000409: s.appendf("Stack buffer overflow at address %p", addr);
160+
case 0xC00004A2: s.appendf("Enclave violation at address %p", addr);
161+
default:
162+
s.appendf("Unhandled exception (%X) at %p", code, addr);
163+
}
164+
if (!builtin::print_backtrace(s.str_view(), 8))
165+
{
166+
io::eprintfn("\nERROR: %s", s.str_view());
167+
}
168+
};
169+
}
170+
if (previous_filter)
171+
{
172+
return previous_filter(exception_info);
173+
}
174+
return EXCEPTION_EXECUTE_HANDLER;
175+
}
176+
177+
fn void panicTracker(String message, String file, String function, uint line)
178+
{
179+
has_panicked = true;
180+
previous_panic(message, file, function, line);
181+
}
182+
183+
fn void initExceptionHandler() @init
184+
{
185+
previous_filter = setUnhandledExceptionFilter(&exceptionHandler);
186+
previous_panic = builtin::panic;
187+
builtin::panic = &panicTracker;
188+
}

0 commit comments

Comments
 (0)