ntdll/tests: Add tests for DBG_RIPEXCEPTION debugger events.
[wine.git] / dlls / ntdll / tests / exception.c
blob2074f0a58f9971f90a18185a93daee072eb5c7fe
1 /*
2 * Unit test suite for ntdll exceptions
4 * Copyright 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #ifndef _WIN32_WINNT
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
26 #endif
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winnt.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "excpt.h"
38 #include "wine/test.h"
40 static void *code_mem;
42 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
43 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
44 static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
45 static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
46 static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
47 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
48 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
49 static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
50 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
51 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
52 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
53 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
55 #if defined(__x86_64__)
56 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
57 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
58 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
59 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
60 #endif
62 #ifdef __i386__
64 #ifndef __WINE_WINTRNL_H
65 #define ProcessExecuteFlags 0x22
66 #define MEM_EXECUTE_OPTION_DISABLE 0x01
67 #define MEM_EXECUTE_OPTION_ENABLE 0x02
68 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
69 #endif
71 static int my_argc;
72 static char** my_argv;
73 static int test_stage;
75 static BOOL is_wow64;
77 /* Test various instruction combinations that cause a protection fault on the i386,
78 * and check what the resulting exception looks like.
81 static const struct exception
83 BYTE code[18]; /* asm code */
84 BYTE offset; /* offset of faulting instruction */
85 BYTE length; /* length of faulting instruction */
86 BOOL wow64_broken; /* broken on Wow64, should be skipped */
87 NTSTATUS status; /* expected status code */
88 DWORD nb_params; /* expected number of parameters */
89 DWORD params[4]; /* expected parameters */
90 NTSTATUS alt_status; /* alternative status code */
91 DWORD alt_nb_params; /* alternative number of parameters */
92 DWORD alt_params[4]; /* alternative parameters */
93 } exceptions[] =
95 /* 0 */
96 /* test some privileged instructions */
97 { { 0xfb, 0xc3 }, /* 0: sti; ret */
98 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
99 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
100 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
101 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
102 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
103 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
104 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
105 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
106 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
107 /* 5 */
108 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
109 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
110 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
111 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
112 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
113 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
114 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
115 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
116 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
117 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
118 /* 10 */
119 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
120 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
121 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
122 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
123 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
124 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
125 { { 0xfa, 0xc3 }, /* 13: cli; ret */
126 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
128 /* test long jump to invalid selector */
129 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
130 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
132 /* 15 */
133 /* test iret to invalid selector */
134 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
135 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
136 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
138 /* test loading an invalid selector */
139 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
140 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
142 /* test accessing a zero selector (%es broken on Wow64) */
143 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
144 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
145 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
146 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
147 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
148 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
150 /* test moving %cs -> %ss */
151 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
152 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
154 /* 20 */
155 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
156 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
157 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
158 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
159 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
160 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
162 /* test invalid interrupt */
163 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
164 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
166 /* test moves to/from Crx */
167 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
168 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
169 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
170 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
171 /* 25 */
172 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
173 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
174 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
175 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
177 /* test moves to/from Drx */
178 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
179 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
180 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
181 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
182 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
183 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
184 /* 30 */
185 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
186 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
187 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
188 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
189 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
190 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
192 /* test memory reads */
193 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
194 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
195 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
196 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
197 /* 35 */
198 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
199 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
200 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
201 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
203 /* test memory writes */
204 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
205 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
206 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
207 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
208 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
209 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
210 /* 40 */
211 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
212 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
214 /* test exception with cleared %ds and %es (broken on Wow64) */
215 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
216 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
217 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
219 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
220 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
223 static int got_exception;
224 static BOOL have_vectored_api;
226 static void run_exception_test(void *handler, const void* context,
227 const void *code, unsigned int code_size,
228 DWORD access)
230 struct {
231 EXCEPTION_REGISTRATION_RECORD frame;
232 const void *context;
233 } exc_frame;
234 void (*func)(void) = code_mem;
235 DWORD oldaccess, oldaccess2;
237 exc_frame.frame.Handler = handler;
238 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
239 exc_frame.context = context;
241 memcpy(code_mem, code, code_size);
242 if(access)
243 VirtualProtect(code_mem, code_size, access, &oldaccess);
245 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
246 func();
247 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
249 if(access)
250 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
253 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
255 PCONTEXT context = ExceptionInfo->ContextRecord;
256 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
257 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
258 rec->ExceptionAddress, context->Eip);
260 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
261 rec->ExceptionAddress, (char *)code_mem + 0xb);
263 if (pNtCurrentTeb()->Peb->BeingDebugged)
264 ok((void *)context->Eax == pRtlRaiseException ||
265 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
266 "debugger managed to modify Eax to %x should be %p\n",
267 context->Eax, pRtlRaiseException);
269 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
270 * even if raised by RtlRaiseException
272 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
274 ok(context->Eip == (DWORD)code_mem + 0xa ||
275 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
276 "Eip at %x instead of %x or %x\n", context->Eip,
277 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
279 else
281 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
282 context->Eip, (DWORD)code_mem + 0xb);
285 /* test if context change is preserved from vectored handler to stack handlers */
286 context->Eax = 0xf00f00f0;
287 return EXCEPTION_CONTINUE_SEARCH;
290 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
291 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
293 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
294 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
296 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
297 rec->ExceptionAddress, (char *)code_mem + 0xb);
299 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
300 * even if raised by RtlRaiseException
302 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
304 ok(context->Eip == (DWORD)code_mem + 0xa ||
305 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
306 "Eip at %x instead of %x or %x\n", context->Eip,
307 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
309 else
311 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
312 context->Eip, (DWORD)code_mem + 0xb);
315 if(have_vectored_api)
316 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
317 context->Eax);
319 /* give the debugger a chance to examine the state a second time */
320 /* without the exception handler changing Eip */
321 if (test_stage == 2)
322 return ExceptionContinueSearch;
324 /* Eip in context is decreased by 1
325 * Increase it again, else execution will continue in the middle of an instruction */
326 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
327 context->Eip += 1;
328 return ExceptionContinueExecution;
332 static const BYTE call_one_arg_code[] = {
333 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
334 0x50, /* push %eax */
335 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
336 0xff, 0xd0, /* call *%eax */
337 0x90, /* nop */
338 0x90, /* nop */
339 0x90, /* nop */
340 0x90, /* nop */
341 0xc3, /* ret */
345 static void run_rtlraiseexception_test(DWORD exceptioncode)
347 EXCEPTION_REGISTRATION_RECORD frame;
348 EXCEPTION_RECORD record;
349 PVOID vectored_handler = NULL;
351 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
353 record.ExceptionCode = exceptioncode;
354 record.ExceptionFlags = 0;
355 record.ExceptionRecord = NULL;
356 record.ExceptionAddress = NULL; /* does not matter, copied return address */
357 record.NumberParameters = 0;
359 frame.Handler = rtlraiseexception_handler;
360 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
362 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
364 pNtCurrentTeb()->Tib.ExceptionList = &frame;
365 if (have_vectored_api)
367 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
368 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
371 func(pRtlRaiseException, &record);
372 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
373 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
375 if (have_vectored_api)
376 pRtlRemoveVectoredExceptionHandler(vectored_handler);
377 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
380 static void test_rtlraiseexception(void)
382 if (!pRtlRaiseException)
384 skip("RtlRaiseException not found\n");
385 return;
388 /* test without debugger */
389 run_rtlraiseexception_test(0x12345);
390 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
391 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
394 static DWORD unwind_expected_eax;
396 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
397 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
399 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
400 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
402 ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
403 rec->ExceptionCode, STATUS_UNWIND);
404 ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
405 rec->ExceptionAddress, (char *)code_mem + 0x22);
406 ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
407 context->Eax, unwind_expected_eax);
409 context->Eax += 1;
410 return ExceptionContinueSearch;
413 static const BYTE call_unwind_code[] = {
414 0x55, /* push %ebp */
415 0x53, /* push %ebx */
416 0x56, /* push %esi */
417 0x57, /* push %edi */
418 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
419 0x58, /* 0: pop %eax */
420 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
421 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
422 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
423 0x50, /* push %eax */
424 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
425 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
426 0xff, 0xd0, /* call *%eax */
427 0x5f, /* pop %edi */
428 0x5e, /* pop %esi */
429 0x5b, /* pop %ebx */
430 0x5d, /* pop %ebp */
431 0xc3, /* ret */
432 0xcc, /* int $3 */
435 static void test_unwind(void)
437 EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
438 DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
439 DWORD retval;
441 memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
443 /* add first unwind handler */
444 frame1->Handler = unwind_handler;
445 frame1->Prev = pNtCurrentTeb()->Tib.ExceptionList;
446 pNtCurrentTeb()->Tib.ExceptionList = frame1;
448 /* add second unwind handler */
449 frame2->Handler = unwind_handler;
450 frame2->Prev = pNtCurrentTeb()->Tib.ExceptionList;
451 pNtCurrentTeb()->Tib.ExceptionList = frame2;
453 /* test unwind to current frame */
454 unwind_expected_eax = 0xDEAD0000;
455 retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
456 ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
457 ok(pNtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
458 pNtCurrentTeb()->Tib.ExceptionList, frame2);
460 /* unwind to frame1 */
461 unwind_expected_eax = 0xDEAD0000;
462 retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
463 ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
464 ok(pNtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
465 pNtCurrentTeb()->Tib.ExceptionList, frame1);
467 /* restore original handler */
468 pNtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
471 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
472 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
474 const struct exception *except = *(const struct exception **)(frame + 1);
475 unsigned int i, entry = except - exceptions;
477 got_exception++;
478 trace( "exception %u: %x flags:%x addr:%p\n",
479 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
481 ok( rec->ExceptionCode == except->status ||
482 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
483 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
484 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
485 "%u: Wrong exception address %p/%p\n", entry,
486 rec->ExceptionAddress, (char*)code_mem + except->offset );
488 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
490 ok( rec->NumberParameters == except->nb_params,
491 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
493 else
495 ok( rec->NumberParameters == except->alt_nb_params,
496 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
499 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
500 /* instead of page faults for accesses beyond 0xffffffff */
501 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
503 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
504 goto skip_params;
507 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
508 if (except->nb_params == 2 && rec->NumberParameters == 2
509 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
510 && except->params[0] == rec->ExceptionInformation[0])
512 goto skip_params;
515 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
517 for (i = 0; i < rec->NumberParameters; i++)
518 ok( rec->ExceptionInformation[i] == except->params[i],
519 "%u: Wrong parameter %d: %lx/%x\n",
520 entry, i, rec->ExceptionInformation[i], except->params[i] );
522 else
524 for (i = 0; i < rec->NumberParameters; i++)
525 ok( rec->ExceptionInformation[i] == except->alt_params[i],
526 "%u: Wrong parameter %d: %lx/%x\n",
527 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
530 skip_params:
531 /* don't handle exception if it's not the address we expected */
532 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
534 context->Eip += except->length;
535 return ExceptionContinueExecution;
538 static void test_prot_fault(void)
540 unsigned int i;
542 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
544 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
546 skip( "Exception %u broken on Wow64\n", i );
547 continue;
549 got_exception = 0;
550 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
551 sizeof(exceptions[i].code), 0);
552 if (!i && !got_exception)
554 trace( "No exception, assuming win9x, no point in testing further\n" );
555 break;
557 ok( got_exception == (exceptions[i].status != 0),
558 "%u: bad exception count %d\n", i, got_exception );
562 struct dbgreg_test {
563 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
566 /* test handling of debug registers */
567 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
568 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
570 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
572 context->Eip += 2; /* Skips the popl (%eax) */
573 context->Dr0 = test->dr0;
574 context->Dr1 = test->dr1;
575 context->Dr2 = test->dr2;
576 context->Dr3 = test->dr3;
577 context->Dr6 = test->dr6;
578 context->Dr7 = test->dr7;
579 return ExceptionContinueExecution;
582 #define CHECK_DEBUG_REG(n, m) \
583 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
584 test_num, test->dr##n, ctx.Dr##n)
586 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
588 CONTEXT ctx;
589 NTSTATUS status;
591 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
592 status = pNtGetContextThread(GetCurrentThread(), &ctx);
593 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
595 CHECK_DEBUG_REG(0, ~0);
596 CHECK_DEBUG_REG(1, ~0);
597 CHECK_DEBUG_REG(2, ~0);
598 CHECK_DEBUG_REG(3, ~0);
599 CHECK_DEBUG_REG(6, 0x0f);
600 CHECK_DEBUG_REG(7, ~0xdc00);
603 static const BYTE segfault_code[5] = {
604 0x31, 0xc0, /* xor %eax,%eax */
605 0x8f, 0x00, /* popl (%eax) - cause exception */
606 0xc3 /* ret */
609 /* test the single step exception behaviour */
610 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
611 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
613 got_exception++;
614 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
616 if( got_exception < 3)
617 context->EFlags |= 0x100; /* single step until popf instruction */
618 else {
619 /* show that the last single step exception on the popf instruction
620 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
621 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
622 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
625 return ExceptionContinueExecution;
628 static const BYTE single_stepcode[] = {
629 0x9c, /* pushf */
630 0x58, /* pop %eax */
631 0x0d,0,1,0,0, /* or $0x100,%eax */
632 0x50, /* push %eax */
633 0x9d, /* popf */
634 0x35,0,1,0,0, /* xor $0x100,%eax */
635 0x50, /* push %eax */
636 0x9d, /* popf */
637 0xc3
640 /* Test the alignment check (AC) flag handling. */
641 static const BYTE align_check_code[] = {
642 0x55, /* push %ebp */
643 0x89,0xe5, /* mov %esp,%ebp */
644 0x9c, /* pushf */
645 0x58, /* pop %eax */
646 0x0d,0,0,4,0, /* or $0x40000,%eax */
647 0x50, /* push %eax */
648 0x9d, /* popf */
649 0x89,0xe0, /* mov %esp, %eax */
650 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
651 0x9c, /* pushf */
652 0x58, /* pop %eax */
653 0x35,0,0,4,0, /* xor $0x40000,%eax */
654 0x50, /* push %eax */
655 0x9d, /* popf */
656 0x5d, /* pop %ebp */
657 0xc3, /* ret */
660 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
661 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
663 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
664 got_exception++;
665 return ExceptionContinueExecution;
668 /* Test the direction flag handling. */
669 static const BYTE direction_flag_code[] = {
670 0x55, /* push %ebp */
671 0x89,0xe5, /* mov %esp,%ebp */
672 0xfd, /* std */
673 0xfa, /* cli - cause exception */
674 0x5d, /* pop %ebp */
675 0xc3, /* ret */
678 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
679 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
681 #ifdef __GNUC__
682 unsigned int flags;
683 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
684 /* older windows versions don't clear DF properly so don't test */
685 if (flags & 0x400) trace( "eflags has DF bit set\n" );
686 #endif
687 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
688 got_exception++;
689 context->Eip++; /* skip cli */
690 context->EFlags &= ~0x400; /* make sure it is cleared on return */
691 return ExceptionContinueExecution;
694 /* test single stepping over hardware breakpoint */
695 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
696 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
698 got_exception++;
699 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
700 "wrong exception code: %x\n", rec->ExceptionCode);
702 if(got_exception == 1) {
703 /* hw bp exception on first nop */
704 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
705 context->Eip, (DWORD)code_mem);
706 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
707 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
708 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
709 context->EFlags |= 0x100; /* enable single stepping */
710 } else if( got_exception == 2) {
711 /* single step exception on second nop */
712 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
713 context->Eip, (DWORD)code_mem + 1);
714 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
715 /* depending on the win version the B0 bit is already set here as well
716 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
717 context->EFlags |= 0x100;
718 } else if( got_exception == 3) {
719 /* hw bp exception on second nop */
720 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
721 context->Eip, (DWORD)code_mem + 1);
722 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
723 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
724 context->Dr0 = 0; /* clear breakpoint */
725 context->EFlags |= 0x100;
726 } else {
727 /* single step exception on ret */
728 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
729 context->Eip, (DWORD)code_mem + 2);
730 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
731 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
734 context->Dr6 = 0; /* clear status register */
735 return ExceptionContinueExecution;
738 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
740 /* test int3 handling */
741 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
742 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
744 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
745 code_mem, rec->ExceptionAddress);
746 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
747 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
749 return ExceptionContinueExecution;
752 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
755 static void test_exceptions(void)
757 CONTEXT ctx;
758 NTSTATUS res;
759 struct dbgreg_test dreg_test;
761 if (!pNtGetContextThread || !pNtSetContextThread)
763 skip( "NtGetContextThread/NtSetContextThread not found\n" );
764 return;
767 /* test handling of debug registers */
768 memset(&dreg_test, 0, sizeof(dreg_test));
770 dreg_test.dr0 = 0x42424240;
771 dreg_test.dr2 = 0x126bb070;
772 dreg_test.dr3 = 0x0badbad0;
773 dreg_test.dr7 = 0xffff0115;
774 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
775 check_debug_registers(1, &dreg_test);
777 dreg_test.dr0 = 0x42424242;
778 dreg_test.dr2 = 0x100f0fe7;
779 dreg_test.dr3 = 0x0abebabe;
780 dreg_test.dr7 = 0x115;
781 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
782 check_debug_registers(2, &dreg_test);
784 /* test single stepping behavior */
785 got_exception = 0;
786 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
787 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
789 /* test alignment exceptions */
790 got_exception = 0;
791 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
792 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
794 /* test direction flag */
795 got_exception = 0;
796 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
797 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
799 /* test single stepping over hardware breakpoint */
800 memset(&ctx, 0, sizeof(ctx));
801 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
802 ctx.Dr7 = 3;
803 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
804 res = pNtSetContextThread( GetCurrentThread(), &ctx);
805 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
807 got_exception = 0;
808 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
809 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
811 /* test int3 handling */
812 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
815 static void test_debugger(void)
817 char cmdline[MAX_PATH];
818 PROCESS_INFORMATION pi;
819 STARTUPINFOA si = { 0 };
820 DEBUG_EVENT de;
821 DWORD continuestatus;
822 PVOID code_mem_address = NULL;
823 NTSTATUS status;
824 SIZE_T size_read;
825 BOOL ret;
826 int counter = 0;
827 si.cb = sizeof(si);
829 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
831 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
832 return;
835 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
836 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
837 ok(ret, "could not create child process error: %u\n", GetLastError());
838 if (!ret)
839 return;
843 continuestatus = DBG_CONTINUE;
844 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
846 if (de.dwThreadId != pi.dwThreadId)
848 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
849 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
850 continue;
853 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
855 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
857 skip("child process loaded at different address, terminating it\n");
858 pNtTerminateProcess(pi.hProcess, 0);
861 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
863 CONTEXT ctx;
864 int stage;
866 counter++;
867 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
868 sizeof(code_mem_address), &size_read);
869 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
870 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
871 sizeof(stage), &size_read);
872 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
874 ctx.ContextFlags = CONTEXT_FULL;
875 status = pNtGetContextThread(pi.hThread, &ctx);
876 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
878 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
879 de.u.Exception.ExceptionRecord.ExceptionCode,
880 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
882 if (counter > 100)
884 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
885 pNtTerminateProcess(pi.hProcess, 1);
887 else if (counter >= 2) /* skip startup breakpoint */
889 if (stage == 1)
891 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
892 ctx.Eip, (char *)code_mem_address + 0xb);
893 /* setting the context from debugger does not affect the context, the exception handlers gets */
894 /* uncomment once wine is fixed */
895 /* ctx.Eip = 0x12345; */
896 ctx.Eax = 0xf00f00f1;
898 /* let the debuggee handle the exception */
899 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
901 else if (stage == 2)
903 if (de.u.Exception.dwFirstChance)
905 /* debugger gets first chance exception with unmodified ctx.Eip */
906 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
907 ctx.Eip, (char *)code_mem_address + 0xb);
909 /* setting the context from debugger does not affect the context, the exception handlers gets */
910 /* uncomment once wine is fixed */
911 /* ctx.Eip = 0x12345; */
912 ctx.Eax = 0xf00f00f1;
914 /* pass exception to debuggee
915 * exception will not be handled and
916 * a second chance exception will be raised */
917 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
919 else
921 /* debugger gets context after exception handler has played with it */
922 /* ctx.Eip is the same value the exception handler got */
923 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
925 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
926 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
927 "Eip at 0x%x instead of %p\n",
928 ctx.Eip, (char *)code_mem_address + 0xa);
929 /* need to fixup Eip for debuggee */
930 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
931 ctx.Eip += 1;
933 else
934 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
935 ctx.Eip, (char *)code_mem_address + 0xb);
936 /* here we handle exception */
939 else
940 ok(FALSE, "unexpected stage %x\n", stage);
942 status = pNtSetContextThread(pi.hThread, &ctx);
943 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
946 else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
948 int stage;
949 char buffer[64];
951 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
952 sizeof(stage), &size_read);
953 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
955 ok(!de.u.DebugString.fUnicode, "unepxected unicode debug string event\n");
956 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
957 de.u.DebugString.nDebugStringLength);
959 memset(buffer, 0, sizeof(buffer));
960 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
961 de.u.DebugString.nDebugStringLength, &size_read);
962 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
964 if (stage == 3 || stage == 4)
965 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
966 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
967 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
969 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
971 else if (de.dwDebugEventCode == RIP_EVENT)
973 int stage;
975 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
976 sizeof(stage), &size_read);
977 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
979 if (stage == 5 || stage == 6)
981 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
982 de.u.RipInfo.dwError, 0x11223344);
983 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
984 de.u.RipInfo.dwType, 0x55667788);
986 else
987 ok(FALSE, "unexpected stage %x\n", stage);
989 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
992 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
994 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
996 winetest_wait_child_process( pi.hProcess );
997 ret = CloseHandle(pi.hThread);
998 ok(ret, "error %u\n", GetLastError());
999 ret = CloseHandle(pi.hProcess);
1000 ok(ret, "error %u\n", GetLastError());
1002 return;
1005 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1006 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1008 int *stage = *(int **)(frame + 1);
1010 got_exception++;
1012 if( *stage == 1) {
1013 /* fault while executing sse instruction */
1014 context->Eip += 3; /* skip addps */
1015 return ExceptionContinueExecution;
1018 /* stage 2 - divide by zero fault */
1019 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1020 skip("system doesn't support SIMD exceptions\n");
1021 else {
1022 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1023 "exception code: %#x, should be %#x\n",
1024 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1025 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1026 "# of params: %i, should be 1\n",
1027 rec->NumberParameters);
1028 if( rec->NumberParameters == 1 )
1029 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1032 context->Eip += 3; /* skip divps */
1034 return ExceptionContinueExecution;
1037 static const BYTE simd_exception_test[] = {
1038 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1039 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1040 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1041 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1042 0x6a, 0x01, /* push $0x1 */
1043 0x6a, 0x01, /* push $0x1 */
1044 0x6a, 0x01, /* push $0x1 */
1045 0x6a, 0x01, /* push $0x1 */
1046 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1047 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1048 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1049 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1050 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
1051 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1052 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1053 0xc3, /* ret */
1056 static const BYTE sse_check[] = {
1057 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1058 0xc3, /* ret */
1061 static void test_simd_exceptions(void)
1063 int stage;
1065 /* test if CPU & OS can do sse */
1066 stage = 1;
1067 got_exception = 0;
1068 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1069 if(got_exception) {
1070 skip("system doesn't support SSE\n");
1071 return;
1074 /* generate a SIMD exception */
1075 stage = 2;
1076 got_exception = 0;
1077 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1078 sizeof(simd_exception_test), 0);
1079 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1082 struct fpu_exception_info
1084 DWORD exception_code;
1085 DWORD exception_offset;
1086 DWORD eip_offset;
1089 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1090 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1092 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1094 info->exception_code = rec->ExceptionCode;
1095 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1096 info->eip_offset = context->Eip - (DWORD)code_mem;
1098 ++context->Eip;
1099 return ExceptionContinueExecution;
1102 static void test_fpu_exceptions(void)
1104 static const BYTE fpu_exception_test_ie[] =
1106 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1107 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1108 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1109 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1110 0xd9, 0xee, /* fldz */
1111 0xd9, 0xe8, /* fld1 */
1112 0xde, 0xf1, /* fdivp */
1113 0xdd, 0xd8, /* fstp %st(0) */
1114 0xdd, 0xd8, /* fstp %st(0) */
1115 0x9b, /* fwait */
1116 0xdb, 0xe2, /* fnclex */
1117 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1118 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1119 0xc3, /* ret */
1122 static const BYTE fpu_exception_test_de[] =
1124 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1125 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1126 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1127 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1128 0xdd, 0xd8, /* fstp %st(0) */
1129 0xd9, 0xee, /* fldz */
1130 0xd9, 0xe8, /* fld1 */
1131 0xde, 0xf1, /* fdivp */
1132 0x9b, /* fwait */
1133 0xdb, 0xe2, /* fnclex */
1134 0xdd, 0xd8, /* fstp %st(0) */
1135 0xdd, 0xd8, /* fstp %st(0) */
1136 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1137 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1138 0xc3, /* ret */
1141 struct fpu_exception_info info;
1143 memset(&info, 0, sizeof(info));
1144 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1145 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1146 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1147 ok(info.exception_offset == 0x19 ||
1148 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1149 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1150 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1152 memset(&info, 0, sizeof(info));
1153 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1154 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1155 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1156 ok(info.exception_offset == 0x17 ||
1157 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1158 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1159 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1162 struct dpe_exception_info {
1163 BOOL exception_caught;
1164 DWORD exception_info;
1167 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1168 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1170 DWORD old_prot;
1171 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1173 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1174 "Exception code %08x\n", rec->ExceptionCode);
1175 ok(rec->NumberParameters == 2,
1176 "Parameter count: %d\n", rec->NumberParameters);
1177 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1178 "Exception address: %p, expected %p\n",
1179 (LPVOID)rec->ExceptionInformation[1], code_mem);
1181 info->exception_info = rec->ExceptionInformation[0];
1182 info->exception_caught = TRUE;
1184 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1185 return ExceptionContinueExecution;
1188 static void test_dpe_exceptions(void)
1190 static const BYTE single_ret[] = {0xC3};
1191 struct dpe_exception_info info;
1192 NTSTATUS stat;
1193 BOOL has_hw_support;
1194 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1195 DWORD val;
1196 ULONG len;
1198 /* Query DEP with len too small */
1199 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1200 if(stat == STATUS_INVALID_INFO_CLASS)
1202 skip("This software platform does not support DEP\n");
1203 return;
1205 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1207 /* Query DEP */
1208 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1209 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1210 if(stat == STATUS_SUCCESS)
1212 ok(len == sizeof val, "returned length: %d\n", len);
1213 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1215 skip("toggling DEP impossible - status locked\n");
1216 is_permanent = TRUE;
1217 if(val & MEM_EXECUTE_OPTION_DISABLE)
1218 can_test_without = FALSE;
1219 else
1220 can_test_with = FALSE;
1224 if(!is_permanent)
1226 /* Enable DEP */
1227 val = MEM_EXECUTE_OPTION_DISABLE;
1228 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1229 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1232 if(can_test_with)
1234 /* Try access to locked page with DEP on*/
1235 info.exception_caught = FALSE;
1236 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1237 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1238 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1239 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1240 "Access violation type: %08x\n", (unsigned)info.exception_info);
1241 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1242 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1244 /* Try execution of data with DEP on*/
1245 info.exception_caught = FALSE;
1246 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1247 if(has_hw_support)
1249 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1250 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1251 "Access violation type: %08x\n", (unsigned)info.exception_info);
1253 else
1254 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1256 else
1257 skip("DEP is in AlwaysOff state\n");
1259 if(!is_permanent)
1261 /* Disable DEP */
1262 val = MEM_EXECUTE_OPTION_ENABLE;
1263 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1264 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1267 /* page is read without exec here */
1268 if(can_test_without)
1270 /* Try execution of data with DEP off */
1271 info.exception_caught = FALSE;
1272 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1273 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1275 /* Try access to locked page with DEP off - error code is different than
1276 with hardware DEP on */
1277 info.exception_caught = FALSE;
1278 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1279 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1280 ok(info.exception_info == EXCEPTION_READ_FAULT,
1281 "Access violation type: %08x\n", (unsigned)info.exception_info);
1283 else
1284 skip("DEP is in AlwaysOn state\n");
1286 if(!is_permanent)
1288 /* Turn off DEP permanently */
1289 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1290 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1291 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1294 /* Try to turn off DEP */
1295 val = MEM_EXECUTE_OPTION_ENABLE;
1296 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1297 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1299 /* Try to turn on DEP */
1300 val = MEM_EXECUTE_OPTION_DISABLE;
1301 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1302 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1305 #elif defined(__x86_64__)
1307 #define UNW_FLAG_NHANDLER 0
1308 #define UNW_FLAG_EHANDLER 1
1309 #define UNW_FLAG_UHANDLER 2
1310 #define UNW_FLAG_CHAININFO 4
1312 #define UWOP_PUSH_NONVOL 0
1313 #define UWOP_ALLOC_LARGE 1
1314 #define UWOP_ALLOC_SMALL 2
1315 #define UWOP_SET_FPREG 3
1316 #define UWOP_SAVE_NONVOL 4
1317 #define UWOP_SAVE_NONVOL_FAR 5
1318 #define UWOP_SAVE_XMM128 8
1319 #define UWOP_SAVE_XMM128_FAR 9
1320 #define UWOP_PUSH_MACHFRAME 10
1322 struct results
1324 int rip_offset; /* rip offset from code start */
1325 int rbp_offset; /* rbp offset from stack pointer */
1326 int handler; /* expect handler to be set? */
1327 int rip; /* expected final rip value */
1328 int frame; /* expected frame return value */
1329 int regs[8][2]; /* expected values for registers */
1332 struct unwind_test
1334 const BYTE *function;
1335 size_t function_size;
1336 const BYTE *unwind_info;
1337 const struct results *results;
1338 unsigned int nb_results;
1341 enum regs
1343 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1344 r8, r9, r10, r11, r12, r13, r14, r15
1347 static const char * const reg_names[16] =
1349 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1350 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1353 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1355 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1357 static const int code_offset = 1024;
1358 static const int unwind_offset = 2048;
1359 void *handler, *data;
1360 CONTEXT context;
1361 RUNTIME_FUNCTION runtime_func;
1362 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1363 UINT i, j, k;
1364 ULONG64 fake_stack[256];
1365 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1366 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1368 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1369 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1371 runtime_func.BeginAddress = code_offset;
1372 runtime_func.EndAddress = code_offset + test->function_size;
1373 runtime_func.UnwindData = unwind_offset;
1375 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1377 for (i = 0; i < test->nb_results; i++)
1379 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1380 memset( &context, 0x55, sizeof(context) );
1381 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1382 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1384 context.Rsp = (ULONG_PTR)fake_stack;
1385 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1386 orig_rbp = context.Rbp;
1387 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1389 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1390 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1392 data = (void *)0xdeadbeef;
1393 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1394 &runtime_func, &context, &data, &frame, &ctx_ptr );
1395 if (test->results[i].handler)
1397 ok( (char *)handler == (char *)code_mem + 0x200,
1398 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1399 if (handler) ok( *(DWORD *)data == 0x08070605,
1400 "%u/%u: wrong handler data %p\n", testnum, i, data );
1402 else
1404 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1405 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1408 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1409 testnum, i, (void *)context.Rip, test->results[i].rip );
1410 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1411 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1413 for (j = 0; j < 16; j++)
1415 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1417 for (k = 0; k < nb_regs; k++)
1419 if (test->results[i].regs[k][0] == -1)
1421 k = nb_regs;
1422 break;
1424 if (test->results[i].regs[k][0] == j) break;
1427 if (j == rsp) /* rsp is special */
1429 ok( !ctx_ptr.u2.IntegerContext[j],
1430 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1431 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1432 "%u/%u: register rsp wrong %p/%p\n",
1433 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1434 continue;
1437 if (ctx_ptr.u2.IntegerContext[j])
1439 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1440 testnum, i, reg_names[j], *(&context.Rax + j) );
1441 if (k < nb_regs)
1442 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1443 "%u/%u: register %s wrong %p/%x\n",
1444 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1446 else
1448 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1449 if (j == rbp)
1450 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1451 testnum, i, (void *)context.Rbp );
1452 else
1453 ok( *(&context.Rax + j) == unset_reg,
1454 "%u/%u: register %s wrong %p/unset\n",
1455 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1461 static void test_virtual_unwind(void)
1463 static const BYTE function_0[] =
1465 0xff, 0xf5, /* 00: push %rbp */
1466 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1467 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1468 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1469 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1470 0x90, /* 1c: nop */
1471 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1472 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1473 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1474 0x5d, /* 32: pop %rbp */
1475 0xc3 /* 33: ret */
1478 static const BYTE unwind_info_0[] =
1480 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1481 0x1c, /* prolog size */
1482 8, /* opcode count */
1483 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1485 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1486 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1487 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1488 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1489 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1491 0x00, 0x02, 0x00, 0x00, /* handler */
1492 0x05, 0x06, 0x07, 0x08, /* data */
1495 static const struct results results_0[] =
1497 /* offset rbp handler rip frame registers */
1498 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1499 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1500 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1501 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1502 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1503 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1504 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1505 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1506 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1507 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1508 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1512 static const BYTE function_1[] =
1514 0x53, /* 00: push %rbx */
1515 0x55, /* 01: push %rbp */
1516 0x56, /* 02: push %rsi */
1517 0x57, /* 03: push %rdi */
1518 0x41, 0x54, /* 04: push %r12 */
1519 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1520 0x90, 0x90, /* 0a: nop; nop */
1521 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1522 0x41, 0x5c, /* 10: pop %r12 */
1523 0x5f, /* 12: pop %rdi */
1524 0x5e, /* 13: pop %rsi */
1525 0x5d, /* 14: pop %rbp */
1526 0x5b, /* 15: pop %rbx */
1527 0xc3 /* 16: ret */
1530 static const BYTE unwind_info_1[] =
1532 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1533 0x0a, /* prolog size */
1534 6, /* opcode count */
1535 0, /* frame reg */
1537 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1538 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1539 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1540 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1541 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1542 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1544 0x00, 0x02, 0x00, 0x00, /* handler */
1545 0x05, 0x06, 0x07, 0x08, /* data */
1548 static const struct results results_1[] =
1550 /* offset rbp handler rip frame registers */
1551 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1552 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1553 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1554 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1555 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1556 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1557 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1558 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1559 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1560 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1561 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1562 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1563 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1564 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1567 static const struct unwind_test tests[] =
1569 { function_0, sizeof(function_0), unwind_info_0,
1570 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1571 { function_1, sizeof(function_1), unwind_info_1,
1572 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1574 unsigned int i;
1576 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1577 call_virtual_unwind( i, &tests[i] );
1580 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1582 static const int code_offset = 1024;
1583 static RUNTIME_FUNCTION runtime_func;
1584 (*(DWORD *)context)++;
1586 runtime_func.BeginAddress = code_offset + 16;
1587 runtime_func.EndAddress = code_offset + 32;
1588 runtime_func.UnwindData = 0;
1589 return &runtime_func;
1592 static void test_dynamic_unwind(void)
1594 static const int code_offset = 1024;
1595 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1596 RUNTIME_FUNCTION *runtime_func, *func;
1597 ULONG_PTR table, base;
1598 DWORD count;
1600 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1601 runtime_func = (RUNTIME_FUNCTION *)buf;
1602 runtime_func->BeginAddress = code_offset;
1603 runtime_func->EndAddress = code_offset + 16;
1604 runtime_func->UnwindData = 0;
1605 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1606 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1608 /* Lookup function outside of any function table */
1609 base = 0xdeadbeef;
1610 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1611 ok( func == NULL,
1612 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1613 ok( !base || broken(base == 0xdeadbeef),
1614 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1616 /* Test with pointer inside of our function */
1617 base = 0xdeadbeef;
1618 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1619 ok( func == runtime_func,
1620 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1621 ok( base == (ULONG_PTR)code_mem,
1622 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1624 /* Test RtlDeleteFunctionTable */
1625 ok( pRtlDeleteFunctionTable( runtime_func ),
1626 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1627 ok( !pRtlDeleteFunctionTable( runtime_func ),
1628 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1630 /* Unaligned RUNTIME_FUNCTION pointer */
1631 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1632 runtime_func->BeginAddress = code_offset;
1633 runtime_func->EndAddress = code_offset + 16;
1634 runtime_func->UnwindData = 0;
1635 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1636 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1637 ok( pRtlDeleteFunctionTable( runtime_func ),
1638 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1640 /* Attempt to insert the same entry twice */
1641 runtime_func = (RUNTIME_FUNCTION *)buf;
1642 runtime_func->BeginAddress = code_offset;
1643 runtime_func->EndAddress = code_offset + 16;
1644 runtime_func->UnwindData = 0;
1645 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1646 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1647 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1648 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1649 ok( pRtlDeleteFunctionTable( runtime_func ),
1650 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1651 ok( pRtlDeleteFunctionTable( runtime_func ),
1652 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1653 ok( !pRtlDeleteFunctionTable( runtime_func ),
1654 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1656 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1657 table = (ULONG_PTR)code_mem;
1658 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1659 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1661 /* Test RtlInstallFunctionTableCallback with both low bits set */
1662 table = (ULONG_PTR)code_mem | 0x3;
1663 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1664 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1666 /* Lookup function outside of any function table */
1667 count = 0;
1668 base = 0xdeadbeef;
1669 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1670 ok( func == NULL,
1671 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1672 ok( !base || broken(base == 0xdeadbeef),
1673 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1674 ok( !count,
1675 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1677 /* Test with pointer inside of our function */
1678 count = 0;
1679 base = 0xdeadbeef;
1680 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1681 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1682 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1683 ok( base == (ULONG_PTR)code_mem,
1684 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1685 ok( count == 1,
1686 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1688 /* Clean up again */
1689 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1690 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1691 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1692 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1696 #endif /* __x86_64__ */
1698 static DWORD outputdebugstring_exceptions;
1700 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1702 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1703 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1705 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
1706 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
1707 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1708 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
1709 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
1710 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
1712 outputdebugstring_exceptions++;
1713 return EXCEPTION_CONTINUE_SEARCH;
1716 static void test_outputdebugstring(DWORD numexc, BOOL todo)
1718 PVOID vectored_handler;
1720 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
1722 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1723 return;
1726 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
1727 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1729 outputdebugstring_exceptions = 0;
1730 OutputDebugStringA("Hello World");
1731 if (todo)
1732 todo_wine
1733 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1734 outputdebugstring_exceptions, numexc);
1735 else
1736 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1737 outputdebugstring_exceptions, numexc);
1739 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1742 static DWORD ripevent_exceptions;
1744 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1746 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1747 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1749 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
1750 rec->ExceptionCode, DBG_RIPEXCEPTION);
1751 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1752 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
1753 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
1754 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
1755 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
1757 ripevent_exceptions++;
1758 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1761 static void test_ripevent(DWORD numexc)
1763 EXCEPTION_RECORD record;
1764 PVOID vectored_handler;
1766 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1768 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1769 return;
1772 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
1773 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1775 record.ExceptionCode = DBG_RIPEXCEPTION;
1776 record.ExceptionFlags = 0;
1777 record.ExceptionRecord = NULL;
1778 record.ExceptionAddress = NULL;
1779 record.NumberParameters = 2;
1780 record.ExceptionInformation[0] = 0x11223344;
1781 record.ExceptionInformation[1] = 0x55667788;
1783 ripevent_exceptions = 0;
1784 pRtlRaiseException(&record);
1785 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
1786 ripevent_exceptions, numexc);
1788 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1791 START_TEST(exception)
1793 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1795 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1796 if(!code_mem) {
1797 trace("VirtualAlloc failed\n");
1798 return;
1801 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1802 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1803 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1804 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1805 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
1806 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1807 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1808 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1809 "RtlAddVectoredExceptionHandler" );
1810 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1811 "RtlRemoveVectoredExceptionHandler" );
1812 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1813 "NtQueryInformationProcess" );
1814 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1815 "NtSetInformationProcess" );
1816 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
1818 #ifdef __i386__
1819 if (!pNtCurrentTeb)
1821 skip( "NtCurrentTeb not found\n" );
1822 return;
1824 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1826 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1827 have_vectored_api = TRUE;
1828 else
1829 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1831 my_argc = winetest_get_mainargs( &my_argv );
1832 if (my_argc >= 4)
1834 void *addr;
1835 sscanf( my_argv[3], "%p", &addr );
1837 if (addr != &test_stage)
1839 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1840 return;
1843 /* child must be run under a debugger */
1844 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1846 ok(FALSE, "child process not being debugged?\n");
1847 return;
1850 if (pRtlRaiseException)
1852 test_stage = 1;
1853 run_rtlraiseexception_test(0x12345);
1854 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1855 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1856 test_stage = 2;
1857 run_rtlraiseexception_test(0x12345);
1858 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1859 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1860 test_stage = 3;
1861 test_outputdebugstring(0, FALSE);
1862 test_stage = 4;
1863 test_outputdebugstring(2, TRUE); /* is this a Windows bug? */
1864 test_stage = 5;
1865 test_ripevent(0);
1866 test_stage = 6;
1867 test_ripevent(1);
1869 else
1870 skip( "RtlRaiseException not found\n" );
1872 /* rest of tests only run in parent */
1873 return;
1876 test_unwind();
1877 test_exceptions();
1878 test_rtlraiseexception();
1879 test_outputdebugstring(1, FALSE);
1880 test_ripevent(1);
1881 test_debugger();
1882 test_simd_exceptions();
1883 test_fpu_exceptions();
1884 test_dpe_exceptions();
1885 test_prot_fault();
1887 #elif defined(__x86_64__)
1888 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
1889 "RtlAddFunctionTable" );
1890 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
1891 "RtlDeleteFunctionTable" );
1892 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
1893 "RtlInstallFunctionTableCallback" );
1894 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
1895 "RtlLookupFunctionEntry" );
1897 test_outputdebugstring(1, FALSE);
1898 test_ripevent(1);
1899 test_virtual_unwind();
1901 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
1902 test_dynamic_unwind();
1903 else
1904 skip( "Dynamic unwind functions not found\n" );
1906 #endif
1908 VirtualFree(code_mem, 0, MEM_FREE);