ntdll/tests: Dump entire VM_COUNTERS structure.
[wine.git] / dlls / ntdll / tests / exception.c
blob27eac2748cb7fab1a4161530500dddf2f5949c5a
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 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnt.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "excpt.h"
37 #include "wine/test.h"
39 static void *code_mem;
41 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
42 static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
43 static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
44 static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
45 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
46 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
47 static PVOID (WINAPI *pRtlAddVectoredContinueHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
48 static ULONG (WINAPI *pRtlRemoveVectoredContinueHandler)(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 typedef struct
58 ULONG Count;
59 struct
61 ULONG BeginAddress;
62 ULONG EndAddress;
63 ULONG HandlerAddress;
64 ULONG JumpTarget;
65 } ScopeRecord[1];
66 } SCOPE_TABLE;
68 typedef struct
70 ULONG64 ControlPc;
71 ULONG64 ImageBase;
72 PRUNTIME_FUNCTION FunctionEntry;
73 ULONG64 EstablisherFrame;
74 ULONG64 TargetIp;
75 PCONTEXT ContextRecord;
76 void* /*PEXCEPTION_ROUTINE*/ LanguageHandler;
77 PVOID HandlerData;
78 PUNWIND_HISTORY_TABLE HistoryTable;
79 ULONG ScopeIndex;
80 } DISPATCHER_CONTEXT;
82 typedef struct _SETJMP_FLOAT128
84 unsigned __int64 DECLSPEC_ALIGN(16) Part[2];
85 } SETJMP_FLOAT128;
87 typedef struct _JUMP_BUFFER
89 unsigned __int64 Frame;
90 unsigned __int64 Rbx;
91 unsigned __int64 Rsp;
92 unsigned __int64 Rbp;
93 unsigned __int64 Rsi;
94 unsigned __int64 Rdi;
95 unsigned __int64 R12;
96 unsigned __int64 R13;
97 unsigned __int64 R14;
98 unsigned __int64 R15;
99 unsigned __int64 Rip;
100 unsigned __int64 Spare;
101 SETJMP_FLOAT128 Xmm6;
102 SETJMP_FLOAT128 Xmm7;
103 SETJMP_FLOAT128 Xmm8;
104 SETJMP_FLOAT128 Xmm9;
105 SETJMP_FLOAT128 Xmm10;
106 SETJMP_FLOAT128 Xmm11;
107 SETJMP_FLOAT128 Xmm12;
108 SETJMP_FLOAT128 Xmm13;
109 SETJMP_FLOAT128 Xmm14;
110 SETJMP_FLOAT128 Xmm15;
111 } _JUMP_BUFFER;
113 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
114 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
115 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
116 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
117 static EXCEPTION_DISPOSITION (WINAPI *p__C_specific_handler)(EXCEPTION_RECORD*, ULONG64, CONTEXT*, DISPATCHER_CONTEXT*);
118 static VOID (WINAPI *pRtlCaptureContext)(CONTEXT*);
119 static VOID (CDECL *pRtlRestoreContext)(CONTEXT*, EXCEPTION_RECORD*);
120 static VOID (CDECL *pRtlUnwindEx)(VOID*, VOID*, EXCEPTION_RECORD*, VOID*, CONTEXT*, UNWIND_HISTORY_TABLE*);
121 static int (CDECL *p_setjmp)(_JUMP_BUFFER*);
122 #endif
124 #ifdef __i386__
126 #ifndef __WINE_WINTRNL_H
127 #define ProcessExecuteFlags 0x22
128 #define MEM_EXECUTE_OPTION_DISABLE 0x01
129 #define MEM_EXECUTE_OPTION_ENABLE 0x02
130 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
131 #endif
133 static int my_argc;
134 static char** my_argv;
135 static int test_stage;
137 static BOOL is_wow64;
139 /* Test various instruction combinations that cause a protection fault on the i386,
140 * and check what the resulting exception looks like.
143 static const struct exception
145 BYTE code[18]; /* asm code */
146 BYTE offset; /* offset of faulting instruction */
147 BYTE length; /* length of faulting instruction */
148 BOOL wow64_broken; /* broken on Wow64, should be skipped */
149 NTSTATUS status; /* expected status code */
150 DWORD nb_params; /* expected number of parameters */
151 DWORD params[4]; /* expected parameters */
152 NTSTATUS alt_status; /* alternative status code */
153 DWORD alt_nb_params; /* alternative number of parameters */
154 DWORD alt_params[4]; /* alternative parameters */
155 } exceptions[] =
157 /* 0 */
158 /* test some privileged instructions */
159 { { 0xfb, 0xc3 }, /* 0: sti; ret */
160 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
161 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
162 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
163 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
164 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
165 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
166 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
167 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
168 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
169 /* 5 */
170 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
171 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
172 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
173 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
174 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
175 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
176 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
177 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
178 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
179 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
180 /* 10 */
181 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
182 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
183 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
184 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
185 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
186 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
187 { { 0xfa, 0xc3 }, /* 13: cli; ret */
188 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
190 /* test long jump to invalid selector */
191 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
192 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
194 /* 15 */
195 /* test iret to invalid selector */
196 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
197 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
198 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
200 /* test loading an invalid selector */
201 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
202 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
204 /* test accessing a zero selector (%es broken on Wow64) */
205 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
206 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
207 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
208 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
209 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
210 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
212 /* test moving %cs -> %ss */
213 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
214 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
216 /* 20 */
217 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
218 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
219 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
220 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
221 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
222 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
224 /* test invalid interrupt */
225 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
226 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
228 /* test moves to/from Crx */
229 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
230 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
231 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
232 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
233 /* 25 */
234 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
235 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
236 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
237 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
239 /* test moves to/from Drx */
240 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
241 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
242 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
243 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
244 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
245 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
246 /* 30 */
247 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
248 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
249 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
250 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
251 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
252 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
254 /* test memory reads */
255 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
256 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
257 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
258 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
259 /* 35 */
260 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
261 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
262 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
263 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
265 /* test memory writes */
266 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
267 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
268 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
269 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
270 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
271 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
272 /* 40 */
273 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
274 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
276 /* test exception with cleared %ds and %es (broken on Wow64) */
277 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
278 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
279 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
281 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
282 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
283 { { 0xb8, 0xb8, 0xb8, 0xb8, 0xb8, /* mov $0xb8b8b8b8, %eax */
284 0xb9, 0xb9, 0xb9, 0xb9, 0xb9, /* mov $0xb9b9b9b9, %ecx */
285 0xba, 0xba, 0xba, 0xba, 0xba, /* mov $0xbabababa, %edx */
286 0xcd, 0x2d, 0xc3 }, /* int $0x2d; ret */
287 17, 0, FALSE, STATUS_BREAKPOINT, 3, { 0xb8b8b8b8, 0xb9b9b9b9, 0xbabababa } },
290 static int got_exception;
291 static BOOL have_vectored_api;
293 static void run_exception_test(void *handler, const void* context,
294 const void *code, unsigned int code_size,
295 DWORD access)
297 struct {
298 EXCEPTION_REGISTRATION_RECORD frame;
299 const void *context;
300 } exc_frame;
301 void (*func)(void) = code_mem;
302 DWORD oldaccess, oldaccess2;
304 exc_frame.frame.Handler = handler;
305 exc_frame.frame.Prev = NtCurrentTeb()->Tib.ExceptionList;
306 exc_frame.context = context;
308 memcpy(code_mem, code, code_size);
309 if(access)
310 VirtualProtect(code_mem, code_size, access, &oldaccess);
312 NtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
313 func();
314 NtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
316 if(access)
317 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
320 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
322 PCONTEXT context = ExceptionInfo->ContextRecord;
323 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
324 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
325 rec->ExceptionAddress, context->Eip);
327 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
328 rec->ExceptionAddress, (char *)code_mem + 0xb);
330 if (NtCurrentTeb()->Peb->BeingDebugged)
331 ok((void *)context->Eax == pRtlRaiseException ||
332 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
333 "debugger managed to modify Eax to %x should be %p\n",
334 context->Eax, pRtlRaiseException);
336 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
337 * even if raised by RtlRaiseException
339 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
341 ok(context->Eip == (DWORD)code_mem + 0xa ||
342 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
343 "Eip at %x instead of %x or %x\n", context->Eip,
344 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
346 else
348 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
349 context->Eip, (DWORD)code_mem + 0xb);
352 /* test if context change is preserved from vectored handler to stack handlers */
353 context->Eax = 0xf00f00f0;
354 return EXCEPTION_CONTINUE_SEARCH;
357 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
358 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
360 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
361 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
363 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
364 rec->ExceptionAddress, (char *)code_mem + 0xb);
366 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
367 * even if raised by RtlRaiseException
369 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
371 ok(context->Eip == (DWORD)code_mem + 0xa ||
372 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
373 "Eip at %x instead of %x or %x\n", context->Eip,
374 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
376 else
378 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
379 context->Eip, (DWORD)code_mem + 0xb);
382 if(have_vectored_api)
383 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
384 context->Eax);
386 /* give the debugger a chance to examine the state a second time */
387 /* without the exception handler changing Eip */
388 if (test_stage == 2)
389 return ExceptionContinueSearch;
391 /* Eip in context is decreased by 1
392 * Increase it again, else execution will continue in the middle of an instruction */
393 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
394 context->Eip += 1;
395 return ExceptionContinueExecution;
399 static const BYTE call_one_arg_code[] = {
400 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
401 0x50, /* push %eax */
402 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
403 0xff, 0xd0, /* call *%eax */
404 0x90, /* nop */
405 0x90, /* nop */
406 0x90, /* nop */
407 0x90, /* nop */
408 0xc3, /* ret */
412 static void run_rtlraiseexception_test(DWORD exceptioncode)
414 EXCEPTION_REGISTRATION_RECORD frame;
415 EXCEPTION_RECORD record;
416 PVOID vectored_handler = NULL;
418 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
420 record.ExceptionCode = exceptioncode;
421 record.ExceptionFlags = 0;
422 record.ExceptionRecord = NULL;
423 record.ExceptionAddress = NULL; /* does not matter, copied return address */
424 record.NumberParameters = 0;
426 frame.Handler = rtlraiseexception_handler;
427 frame.Prev = NtCurrentTeb()->Tib.ExceptionList;
429 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
431 NtCurrentTeb()->Tib.ExceptionList = &frame;
432 if (have_vectored_api)
434 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
435 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
438 func(pRtlRaiseException, &record);
439 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
440 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
442 if (have_vectored_api)
443 pRtlRemoveVectoredExceptionHandler(vectored_handler);
444 NtCurrentTeb()->Tib.ExceptionList = frame.Prev;
447 static void test_rtlraiseexception(void)
449 if (!pRtlRaiseException)
451 skip("RtlRaiseException not found\n");
452 return;
455 /* test without debugger */
456 run_rtlraiseexception_test(0x12345);
457 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
458 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
461 static DWORD unwind_expected_eax;
463 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
464 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
466 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
467 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
469 ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
470 rec->ExceptionCode, STATUS_UNWIND);
471 ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
472 rec->ExceptionAddress, (char *)code_mem + 0x22);
473 ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
474 context->Eax, unwind_expected_eax);
476 context->Eax += 1;
477 return ExceptionContinueSearch;
480 static const BYTE call_unwind_code[] = {
481 0x55, /* push %ebp */
482 0x53, /* push %ebx */
483 0x56, /* push %esi */
484 0x57, /* push %edi */
485 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
486 0x58, /* 0: pop %eax */
487 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
488 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
489 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
490 0x50, /* push %eax */
491 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
492 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
493 0xff, 0xd0, /* call *%eax */
494 0x5f, /* pop %edi */
495 0x5e, /* pop %esi */
496 0x5b, /* pop %ebx */
497 0x5d, /* pop %ebp */
498 0xc3, /* ret */
499 0xcc, /* int $3 */
502 static void test_unwind(void)
504 EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
505 DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
506 DWORD retval;
508 memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
510 /* add first unwind handler */
511 frame1->Handler = unwind_handler;
512 frame1->Prev = NtCurrentTeb()->Tib.ExceptionList;
513 NtCurrentTeb()->Tib.ExceptionList = frame1;
515 /* add second unwind handler */
516 frame2->Handler = unwind_handler;
517 frame2->Prev = NtCurrentTeb()->Tib.ExceptionList;
518 NtCurrentTeb()->Tib.ExceptionList = frame2;
520 /* test unwind to current frame */
521 unwind_expected_eax = 0xDEAD0000;
522 retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
523 ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
524 ok(NtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
525 NtCurrentTeb()->Tib.ExceptionList, frame2);
527 /* unwind to frame1 */
528 unwind_expected_eax = 0xDEAD0000;
529 retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
530 ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
531 ok(NtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
532 NtCurrentTeb()->Tib.ExceptionList, frame1);
534 /* restore original handler */
535 NtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
538 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
539 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
541 const struct exception *except = *(const struct exception **)(frame + 1);
542 unsigned int i, parameter_count, entry = except - exceptions;
544 got_exception++;
545 trace( "exception %u: %x flags:%x addr:%p\n",
546 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
548 ok( rec->ExceptionCode == except->status ||
549 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
550 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
551 ok( context->Eip == (DWORD_PTR)code_mem + except->offset,
552 "%u: Unexpected eip %#x/%#lx\n", entry,
553 context->Eip, (DWORD_PTR)code_mem + except->offset );
554 ok( rec->ExceptionAddress == (char*)context->Eip ||
555 (rec->ExceptionCode == STATUS_BREAKPOINT && rec->ExceptionAddress == (char*)context->Eip + 1),
556 "%u: Unexpected exception address %p/%p\n", entry,
557 rec->ExceptionAddress, (char*)context->Eip );
559 if (except->status == STATUS_BREAKPOINT && is_wow64)
560 parameter_count = 1;
561 else if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
562 parameter_count = except->nb_params;
563 else
564 parameter_count = except->alt_nb_params;
566 ok( rec->NumberParameters == parameter_count,
567 "%u: Unexpected parameter count %u/%u\n", entry, rec->NumberParameters, parameter_count );
569 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
570 /* instead of page faults for accesses beyond 0xffffffff */
571 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
573 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
574 goto skip_params;
577 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
578 if (except->nb_params == 2 && rec->NumberParameters == 2
579 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
580 && except->params[0] == rec->ExceptionInformation[0])
582 goto skip_params;
585 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
587 for (i = 0; i < rec->NumberParameters; i++)
588 ok( rec->ExceptionInformation[i] == except->params[i],
589 "%u: Wrong parameter %d: %lx/%x\n",
590 entry, i, rec->ExceptionInformation[i], except->params[i] );
592 else
594 for (i = 0; i < rec->NumberParameters; i++)
595 ok( rec->ExceptionInformation[i] == except->alt_params[i],
596 "%u: Wrong parameter %d: %lx/%x\n",
597 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
600 skip_params:
601 /* don't handle exception if it's not the address we expected */
602 if (context->Eip != (DWORD_PTR)code_mem + except->offset) return ExceptionContinueSearch;
604 context->Eip += except->length;
605 return ExceptionContinueExecution;
608 static void test_prot_fault(void)
610 unsigned int i;
612 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
614 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
616 skip( "Exception %u broken on Wow64\n", i );
617 continue;
619 got_exception = 0;
620 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
621 sizeof(exceptions[i].code), 0);
622 if (!i && !got_exception)
624 trace( "No exception, assuming win9x, no point in testing further\n" );
625 break;
627 ok( got_exception == (exceptions[i].status != 0),
628 "%u: bad exception count %d\n", i, got_exception );
632 struct dbgreg_test {
633 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
636 /* test handling of debug registers */
637 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
638 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
640 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
642 context->Eip += 2; /* Skips the popl (%eax) */
643 context->Dr0 = test->dr0;
644 context->Dr1 = test->dr1;
645 context->Dr2 = test->dr2;
646 context->Dr3 = test->dr3;
647 context->Dr6 = test->dr6;
648 context->Dr7 = test->dr7;
649 return ExceptionContinueExecution;
652 #define CHECK_DEBUG_REG(n, m) \
653 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
654 test_num, test->dr##n, ctx.Dr##n)
656 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
658 CONTEXT ctx;
659 NTSTATUS status;
661 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
662 status = pNtGetContextThread(GetCurrentThread(), &ctx);
663 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
665 CHECK_DEBUG_REG(0, ~0);
666 CHECK_DEBUG_REG(1, ~0);
667 CHECK_DEBUG_REG(2, ~0);
668 CHECK_DEBUG_REG(3, ~0);
669 CHECK_DEBUG_REG(6, 0x0f);
670 CHECK_DEBUG_REG(7, ~0xdc00);
673 static const BYTE segfault_code[5] = {
674 0x31, 0xc0, /* xor %eax,%eax */
675 0x8f, 0x00, /* popl (%eax) - cause exception */
676 0xc3 /* ret */
679 /* test the single step exception behaviour */
680 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
681 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
683 got_exception++;
684 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
686 if( got_exception < 3)
687 context->EFlags |= 0x100; /* single step until popf instruction */
688 else {
689 /* show that the last single step exception on the popf instruction
690 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
691 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
692 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
695 return ExceptionContinueExecution;
698 static const BYTE single_stepcode[] = {
699 0x9c, /* pushf */
700 0x58, /* pop %eax */
701 0x0d,0,1,0,0, /* or $0x100,%eax */
702 0x50, /* push %eax */
703 0x9d, /* popf */
704 0x35,0,1,0,0, /* xor $0x100,%eax */
705 0x50, /* push %eax */
706 0x9d, /* popf */
707 0xc3
710 /* Test the alignment check (AC) flag handling. */
711 static const BYTE align_check_code[] = {
712 0x55, /* push %ebp */
713 0x89,0xe5, /* mov %esp,%ebp */
714 0x9c, /* pushf */
715 0x58, /* pop %eax */
716 0x0d,0,0,4,0, /* or $0x40000,%eax */
717 0x50, /* push %eax */
718 0x9d, /* popf */
719 0x89,0xe0, /* mov %esp, %eax */
720 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
721 0x9c, /* pushf */
722 0x58, /* pop %eax */
723 0x35,0,0,4,0, /* xor $0x40000,%eax */
724 0x50, /* push %eax */
725 0x9d, /* popf */
726 0x5d, /* pop %ebp */
727 0xc3, /* ret */
730 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
731 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
733 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
734 got_exception++;
735 return ExceptionContinueExecution;
738 /* Test the direction flag handling. */
739 static const BYTE direction_flag_code[] = {
740 0x55, /* push %ebp */
741 0x89,0xe5, /* mov %esp,%ebp */
742 0xfd, /* std */
743 0xfa, /* cli - cause exception */
744 0x5d, /* pop %ebp */
745 0xc3, /* ret */
748 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
749 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
751 #ifdef __GNUC__
752 unsigned int flags;
753 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
754 /* older windows versions don't clear DF properly so don't test */
755 if (flags & 0x400) trace( "eflags has DF bit set\n" );
756 #endif
757 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
758 got_exception++;
759 context->Eip++; /* skip cli */
760 context->EFlags &= ~0x400; /* make sure it is cleared on return */
761 return ExceptionContinueExecution;
764 /* test single stepping over hardware breakpoint */
765 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
766 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
768 got_exception++;
769 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
770 "wrong exception code: %x\n", rec->ExceptionCode);
772 if(got_exception == 1) {
773 /* hw bp exception on first nop */
774 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
775 context->Eip, (DWORD)code_mem);
776 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
777 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
778 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
779 context->EFlags |= 0x100; /* enable single stepping */
780 } else if( got_exception == 2) {
781 /* single step exception on second nop */
782 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
783 context->Eip, (DWORD)code_mem + 1);
784 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
785 /* depending on the win version the B0 bit is already set here as well
786 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
787 context->EFlags |= 0x100;
788 } else if( got_exception == 3) {
789 /* hw bp exception on second nop */
790 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
791 context->Eip, (DWORD)code_mem + 1);
792 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
793 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
794 context->Dr0 = 0; /* clear breakpoint */
795 context->EFlags |= 0x100;
796 } else {
797 /* single step exception on ret */
798 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
799 context->Eip, (DWORD)code_mem + 2);
800 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
801 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
804 context->Dr6 = 0; /* clear status register */
805 return ExceptionContinueExecution;
808 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
810 /* test int3 handling */
811 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
812 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
814 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
815 code_mem, rec->ExceptionAddress);
816 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
817 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
819 return ExceptionContinueExecution;
822 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
825 static void test_exceptions(void)
827 CONTEXT ctx;
828 NTSTATUS res;
829 struct dbgreg_test dreg_test;
831 if (!pNtGetContextThread || !pNtSetContextThread)
833 skip( "NtGetContextThread/NtSetContextThread not found\n" );
834 return;
837 /* test handling of debug registers */
838 memset(&dreg_test, 0, sizeof(dreg_test));
840 dreg_test.dr0 = 0x42424240;
841 dreg_test.dr2 = 0x126bb070;
842 dreg_test.dr3 = 0x0badbad0;
843 dreg_test.dr7 = 0xffff0115;
844 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
845 check_debug_registers(1, &dreg_test);
847 dreg_test.dr0 = 0x42424242;
848 dreg_test.dr2 = 0x100f0fe7;
849 dreg_test.dr3 = 0x0abebabe;
850 dreg_test.dr7 = 0x115;
851 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
852 check_debug_registers(2, &dreg_test);
854 /* test single stepping behavior */
855 got_exception = 0;
856 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
857 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
859 /* test alignment exceptions */
860 got_exception = 0;
861 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
862 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
864 /* test direction flag */
865 got_exception = 0;
866 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
867 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
869 /* test single stepping over hardware breakpoint */
870 memset(&ctx, 0, sizeof(ctx));
871 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
872 ctx.Dr7 = 3;
873 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
874 res = pNtSetContextThread( GetCurrentThread(), &ctx);
875 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
877 got_exception = 0;
878 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
879 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
881 /* test int3 handling */
882 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
885 static void test_debugger(void)
887 char cmdline[MAX_PATH];
888 PROCESS_INFORMATION pi;
889 STARTUPINFOA si = { 0 };
890 DEBUG_EVENT de;
891 DWORD continuestatus;
892 PVOID code_mem_address = NULL;
893 NTSTATUS status;
894 SIZE_T size_read;
895 BOOL ret;
896 int counter = 0;
897 si.cb = sizeof(si);
899 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
901 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
902 return;
905 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
906 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
907 ok(ret, "could not create child process error: %u\n", GetLastError());
908 if (!ret)
909 return;
913 continuestatus = DBG_CONTINUE;
914 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
916 if (de.dwThreadId != pi.dwThreadId)
918 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
919 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
920 continue;
923 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
925 if(de.u.CreateProcessInfo.lpBaseOfImage != NtCurrentTeb()->Peb->ImageBaseAddress)
927 skip("child process loaded at different address, terminating it\n");
928 pNtTerminateProcess(pi.hProcess, 0);
931 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
933 CONTEXT ctx;
934 int stage;
936 counter++;
937 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
938 sizeof(code_mem_address), &size_read);
939 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
940 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
941 sizeof(stage), &size_read);
942 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
944 ctx.ContextFlags = CONTEXT_FULL;
945 status = pNtGetContextThread(pi.hThread, &ctx);
946 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
948 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
949 de.u.Exception.ExceptionRecord.ExceptionCode,
950 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
952 if (counter > 100)
954 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
955 pNtTerminateProcess(pi.hProcess, 1);
957 else if (counter >= 2) /* skip startup breakpoint */
959 if (stage == 1)
961 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
962 ctx.Eip, (char *)code_mem_address + 0xb);
963 /* setting the context from debugger does not affect the context, the exception handlers gets */
964 /* uncomment once wine is fixed */
965 /* ctx.Eip = 0x12345; */
966 ctx.Eax = 0xf00f00f1;
968 /* let the debuggee handle the exception */
969 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
971 else if (stage == 2)
973 if (de.u.Exception.dwFirstChance)
975 /* debugger gets first chance exception with unmodified ctx.Eip */
976 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
977 ctx.Eip, (char *)code_mem_address + 0xb);
979 /* setting the context from debugger does not affect the context, the exception handlers gets */
980 /* uncomment once wine is fixed */
981 /* ctx.Eip = 0x12345; */
982 ctx.Eax = 0xf00f00f1;
984 /* pass exception to debuggee
985 * exception will not be handled and
986 * a second chance exception will be raised */
987 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
989 else
991 /* debugger gets context after exception handler has played with it */
992 /* ctx.Eip is the same value the exception handler got */
993 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
995 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
996 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
997 "Eip at 0x%x instead of %p\n",
998 ctx.Eip, (char *)code_mem_address + 0xa);
999 /* need to fixup Eip for debuggee */
1000 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
1001 ctx.Eip += 1;
1003 else
1004 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
1005 ctx.Eip, (char *)code_mem_address + 0xb);
1006 /* here we handle exception */
1009 else if (stage == 7 || stage == 8)
1011 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT,
1012 "expected EXCEPTION_BREAKPOINT, got %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode);
1013 ok((char *)ctx.Eip == (char *)code_mem_address + 0x1d,
1014 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address + 0x1d, ctx.Eip);
1016 if (stage == 8) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1018 else if (stage == 9 || stage == 10)
1020 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT,
1021 "expected EXCEPTION_BREAKPOINT, got %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode);
1022 ok((char *)ctx.Eip == (char *)code_mem_address + 2,
1023 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address + 2, ctx.Eip);
1025 if (stage == 10) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1027 else
1028 ok(FALSE, "unexpected stage %x\n", stage);
1030 status = pNtSetContextThread(pi.hThread, &ctx);
1031 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
1034 else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
1036 int stage;
1037 char buffer[64];
1039 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
1040 sizeof(stage), &size_read);
1041 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1043 ok(!de.u.DebugString.fUnicode, "unexpected unicode debug string event\n");
1044 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
1045 de.u.DebugString.nDebugStringLength);
1047 memset(buffer, 0, sizeof(buffer));
1048 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
1049 de.u.DebugString.nDebugStringLength, &size_read);
1050 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1052 if (stage == 3 || stage == 4)
1053 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
1054 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
1055 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
1057 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1059 else if (de.dwDebugEventCode == RIP_EVENT)
1061 int stage;
1063 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
1064 sizeof(stage), &size_read);
1065 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1067 if (stage == 5 || stage == 6)
1069 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
1070 de.u.RipInfo.dwError, 0x11223344);
1071 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
1072 de.u.RipInfo.dwType, 0x55667788);
1074 else
1075 ok(FALSE, "unexpected stage %x\n", stage);
1077 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1080 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
1082 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1084 winetest_wait_child_process( pi.hProcess );
1085 ret = CloseHandle(pi.hThread);
1086 ok(ret, "error %u\n", GetLastError());
1087 ret = CloseHandle(pi.hProcess);
1088 ok(ret, "error %u\n", GetLastError());
1090 return;
1093 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1094 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1096 int *stage = *(int **)(frame + 1);
1098 got_exception++;
1100 if( *stage == 1) {
1101 /* fault while executing sse instruction */
1102 context->Eip += 3; /* skip addps */
1103 return ExceptionContinueExecution;
1105 else if ( *stage == 2 || *stage == 3 ) {
1106 /* stage 2 - divide by zero fault */
1107 /* stage 3 - invalid operation fault */
1108 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1109 skip("system doesn't support SIMD exceptions\n");
1110 else {
1111 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1112 "exception code: %#x, should be %#x\n",
1113 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1114 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1115 "# of params: %i, should be 1\n",
1116 rec->NumberParameters);
1117 if( rec->NumberParameters == 1 )
1118 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1120 context->Eip += 3; /* skip divps */
1122 else
1123 ok(FALSE, "unexpected stage %x\n", *stage);
1125 return ExceptionContinueExecution;
1128 static const BYTE simd_exception_test[] = {
1129 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1130 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1131 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1132 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1133 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1134 0x6a, 0x01, /* push $0x1 */
1135 0x6a, 0x01, /* push $0x1 */
1136 0x6a, 0x01, /* push $0x1 */
1137 0x6a, 0x01, /* push $0x1 */
1138 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1139 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1140 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1141 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1142 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1143 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1144 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1145 0xc3, /* ret */
1148 static const BYTE simd_exception_test2[] = {
1149 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1150 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1151 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1152 0x66, 0x81, 0x24, 0x24, 0x7f, 0xff, /* andw $0xff7f,(%esp) * enable invalid */
1153 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * operation exceptions */
1154 0x0f, 0x57, 0xc9, /* xorps %xmm1,%xmm1 * clear dividend */
1155 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1156 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1157 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1158 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1159 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1160 0xc3, /* ret */
1163 static const BYTE sse_check[] = {
1164 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1165 0xc3, /* ret */
1168 static void test_simd_exceptions(void)
1170 int stage;
1172 /* test if CPU & OS can do sse */
1173 stage = 1;
1174 got_exception = 0;
1175 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1176 if(got_exception) {
1177 skip("system doesn't support SSE\n");
1178 return;
1181 /* generate a SIMD exception */
1182 stage = 2;
1183 got_exception = 0;
1184 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1185 sizeof(simd_exception_test), 0);
1186 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1188 /* generate a SIMD exception, test FPE_FLTINV */
1189 stage = 3;
1190 got_exception = 0;
1191 run_exception_test(simd_fault_handler, &stage, simd_exception_test2,
1192 sizeof(simd_exception_test2), 0);
1193 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1196 struct fpu_exception_info
1198 DWORD exception_code;
1199 DWORD exception_offset;
1200 DWORD eip_offset;
1203 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1204 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1206 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1208 info->exception_code = rec->ExceptionCode;
1209 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1210 info->eip_offset = context->Eip - (DWORD)code_mem;
1212 ++context->Eip;
1213 return ExceptionContinueExecution;
1216 static void test_fpu_exceptions(void)
1218 static const BYTE fpu_exception_test_ie[] =
1220 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1221 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1222 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1223 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1224 0xd9, 0xee, /* fldz */
1225 0xd9, 0xe8, /* fld1 */
1226 0xde, 0xf1, /* fdivp */
1227 0xdd, 0xd8, /* fstp %st(0) */
1228 0xdd, 0xd8, /* fstp %st(0) */
1229 0x9b, /* fwait */
1230 0xdb, 0xe2, /* fnclex */
1231 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1232 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1233 0xc3, /* ret */
1236 static const BYTE fpu_exception_test_de[] =
1238 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1239 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1240 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1241 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1242 0xdd, 0xd8, /* fstp %st(0) */
1243 0xd9, 0xee, /* fldz */
1244 0xd9, 0xe8, /* fld1 */
1245 0xde, 0xf1, /* fdivp */
1246 0x9b, /* fwait */
1247 0xdb, 0xe2, /* fnclex */
1248 0xdd, 0xd8, /* fstp %st(0) */
1249 0xdd, 0xd8, /* fstp %st(0) */
1250 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1251 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1252 0xc3, /* ret */
1255 struct fpu_exception_info info;
1257 memset(&info, 0, sizeof(info));
1258 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1259 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1260 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1261 ok(info.exception_offset == 0x19 ||
1262 broken( info.exception_offset == info.eip_offset ),
1263 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1264 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1266 memset(&info, 0, sizeof(info));
1267 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1268 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1269 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1270 ok(info.exception_offset == 0x17 ||
1271 broken( info.exception_offset == info.eip_offset ),
1272 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1273 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1276 struct dpe_exception_info {
1277 BOOL exception_caught;
1278 DWORD exception_info;
1281 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1282 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1284 DWORD old_prot;
1285 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1287 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1288 "Exception code %08x\n", rec->ExceptionCode);
1289 ok(rec->NumberParameters == 2,
1290 "Parameter count: %d\n", rec->NumberParameters);
1291 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1292 "Exception address: %p, expected %p\n",
1293 (LPVOID)rec->ExceptionInformation[1], code_mem);
1295 info->exception_info = rec->ExceptionInformation[0];
1296 info->exception_caught = TRUE;
1298 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1299 return ExceptionContinueExecution;
1302 static void test_dpe_exceptions(void)
1304 static const BYTE single_ret[] = {0xC3};
1305 struct dpe_exception_info info;
1306 NTSTATUS stat;
1307 BOOL has_hw_support;
1308 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1309 DWORD val;
1310 ULONG len;
1312 /* Query DEP with len too small */
1313 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1314 if(stat == STATUS_INVALID_INFO_CLASS)
1316 skip("This software platform does not support DEP\n");
1317 return;
1319 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1321 /* Query DEP */
1322 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1323 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1324 if(stat == STATUS_SUCCESS)
1326 ok(len == sizeof val, "returned length: %d\n", len);
1327 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1329 skip("toggling DEP impossible - status locked\n");
1330 is_permanent = TRUE;
1331 if(val & MEM_EXECUTE_OPTION_DISABLE)
1332 can_test_without = FALSE;
1333 else
1334 can_test_with = FALSE;
1338 if(!is_permanent)
1340 /* Enable DEP */
1341 val = MEM_EXECUTE_OPTION_DISABLE;
1342 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1343 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1346 if(can_test_with)
1348 /* Try access to locked page with DEP on*/
1349 info.exception_caught = FALSE;
1350 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1351 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1352 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1353 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1354 "Access violation type: %08x\n", (unsigned)info.exception_info);
1355 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1356 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1358 /* Try execution of data with DEP on*/
1359 info.exception_caught = FALSE;
1360 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1361 if(has_hw_support)
1363 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1364 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1365 "Access violation type: %08x\n", (unsigned)info.exception_info);
1367 else
1368 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1370 else
1371 skip("DEP is in AlwaysOff state\n");
1373 if(!is_permanent)
1375 /* Disable DEP */
1376 val = MEM_EXECUTE_OPTION_ENABLE;
1377 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1378 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1381 /* page is read without exec here */
1382 if(can_test_without)
1384 /* Try execution of data with DEP off */
1385 info.exception_caught = FALSE;
1386 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1387 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1389 /* Try access to locked page with DEP off - error code is different than
1390 with hardware DEP on */
1391 info.exception_caught = FALSE;
1392 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1393 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1394 ok(info.exception_info == EXCEPTION_READ_FAULT,
1395 "Access violation type: %08x\n", (unsigned)info.exception_info);
1397 else
1398 skip("DEP is in AlwaysOn state\n");
1400 if(!is_permanent)
1402 /* Turn off DEP permanently */
1403 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1404 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1405 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1408 /* Try to turn off DEP */
1409 val = MEM_EXECUTE_OPTION_ENABLE;
1410 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1411 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1413 /* Try to turn on DEP */
1414 val = MEM_EXECUTE_OPTION_DISABLE;
1415 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1416 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1419 #elif defined(__x86_64__)
1421 #define is_wow64 0
1423 #define UNW_FLAG_NHANDLER 0
1424 #define UNW_FLAG_EHANDLER 1
1425 #define UNW_FLAG_UHANDLER 2
1426 #define UNW_FLAG_CHAININFO 4
1428 #define UWOP_PUSH_NONVOL 0
1429 #define UWOP_ALLOC_LARGE 1
1430 #define UWOP_ALLOC_SMALL 2
1431 #define UWOP_SET_FPREG 3
1432 #define UWOP_SAVE_NONVOL 4
1433 #define UWOP_SAVE_NONVOL_FAR 5
1434 #define UWOP_SAVE_XMM128 8
1435 #define UWOP_SAVE_XMM128_FAR 9
1436 #define UWOP_PUSH_MACHFRAME 10
1438 struct results
1440 int rip_offset; /* rip offset from code start */
1441 int rbp_offset; /* rbp offset from stack pointer */
1442 int handler; /* expect handler to be set? */
1443 int rip; /* expected final rip value */
1444 int frame; /* expected frame return value */
1445 int regs[8][2]; /* expected values for registers */
1448 struct unwind_test
1450 const BYTE *function;
1451 size_t function_size;
1452 const BYTE *unwind_info;
1453 const struct results *results;
1454 unsigned int nb_results;
1457 enum regs
1459 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1460 r8, r9, r10, r11, r12, r13, r14, r15
1463 static const char * const reg_names[16] =
1465 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1466 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1469 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1471 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1473 static const int code_offset = 1024;
1474 static const int unwind_offset = 2048;
1475 void *handler, *data;
1476 CONTEXT context;
1477 RUNTIME_FUNCTION runtime_func;
1478 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1479 UINT i, j, k;
1480 ULONG64 fake_stack[256];
1481 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1482 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1484 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1485 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1487 runtime_func.BeginAddress = code_offset;
1488 runtime_func.EndAddress = code_offset + test->function_size;
1489 runtime_func.UnwindData = unwind_offset;
1491 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1493 for (i = 0; i < test->nb_results; i++)
1495 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1496 memset( &context, 0x55, sizeof(context) );
1497 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1498 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1500 context.Rsp = (ULONG_PTR)fake_stack;
1501 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1502 orig_rbp = context.Rbp;
1503 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1505 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1506 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1508 data = (void *)0xdeadbeef;
1509 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1510 &runtime_func, &context, &data, &frame, &ctx_ptr );
1511 if (test->results[i].handler)
1513 ok( (char *)handler == (char *)code_mem + 0x200,
1514 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1515 if (handler) ok( *(DWORD *)data == 0x08070605,
1516 "%u/%u: wrong handler data %p\n", testnum, i, data );
1518 else
1520 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1521 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1524 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1525 testnum, i, (void *)context.Rip, test->results[i].rip );
1526 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1527 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1529 for (j = 0; j < 16; j++)
1531 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1533 for (k = 0; k < nb_regs; k++)
1535 if (test->results[i].regs[k][0] == -1)
1537 k = nb_regs;
1538 break;
1540 if (test->results[i].regs[k][0] == j) break;
1543 if (j == rsp) /* rsp is special */
1545 ok( !ctx_ptr.u2.IntegerContext[j],
1546 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1547 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1548 "%u/%u: register rsp wrong %p/%p\n",
1549 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1550 continue;
1553 if (ctx_ptr.u2.IntegerContext[j])
1555 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1556 testnum, i, reg_names[j], *(&context.Rax + j) );
1557 if (k < nb_regs)
1558 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1559 "%u/%u: register %s wrong %p/%x\n",
1560 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1562 else
1564 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1565 if (j == rbp)
1566 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1567 testnum, i, (void *)context.Rbp );
1568 else
1569 ok( *(&context.Rax + j) == unset_reg,
1570 "%u/%u: register %s wrong %p/unset\n",
1571 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1577 static void test_virtual_unwind(void)
1579 static const BYTE function_0[] =
1581 0xff, 0xf5, /* 00: push %rbp */
1582 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1583 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1584 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1585 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1586 0x90, /* 1c: nop */
1587 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1588 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1589 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1590 0x5d, /* 32: pop %rbp */
1591 0xc3 /* 33: ret */
1594 static const BYTE unwind_info_0[] =
1596 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1597 0x1c, /* prolog size */
1598 8, /* opcode count */
1599 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1601 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1602 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1603 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1604 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1605 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1607 0x00, 0x02, 0x00, 0x00, /* handler */
1608 0x05, 0x06, 0x07, 0x08, /* data */
1611 static const struct results results_0[] =
1613 /* offset rbp handler rip frame registers */
1614 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1615 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1616 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1617 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1618 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1619 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1620 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1621 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1622 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1623 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1624 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1628 static const BYTE function_1[] =
1630 0x53, /* 00: push %rbx */
1631 0x55, /* 01: push %rbp */
1632 0x56, /* 02: push %rsi */
1633 0x57, /* 03: push %rdi */
1634 0x41, 0x54, /* 04: push %r12 */
1635 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1636 0x90, 0x90, /* 0a: nop; nop */
1637 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1638 0x41, 0x5c, /* 10: pop %r12 */
1639 0x5f, /* 12: pop %rdi */
1640 0x5e, /* 13: pop %rsi */
1641 0x5d, /* 14: pop %rbp */
1642 0x5b, /* 15: pop %rbx */
1643 0xc3 /* 16: ret */
1646 static const BYTE unwind_info_1[] =
1648 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1649 0x0a, /* prolog size */
1650 6, /* opcode count */
1651 0, /* frame reg */
1653 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1654 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1655 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1656 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1657 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1658 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1660 0x00, 0x02, 0x00, 0x00, /* handler */
1661 0x05, 0x06, 0x07, 0x08, /* data */
1664 static const struct results results_1[] =
1666 /* offset rbp handler rip frame registers */
1667 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1668 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1669 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1670 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1671 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1672 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1673 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1674 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1675 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1676 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1677 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1678 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1679 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1680 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1683 static const struct unwind_test tests[] =
1685 { function_0, sizeof(function_0), unwind_info_0,
1686 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1687 { function_1, sizeof(function_1), unwind_info_1,
1688 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1690 unsigned int i;
1692 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1693 call_virtual_unwind( i, &tests[i] );
1696 static int consolidate_dummy_called;
1697 static PVOID CALLBACK test_consolidate_dummy(EXCEPTION_RECORD *rec)
1699 CONTEXT *ctx = (CONTEXT *)rec->ExceptionInformation[1];
1700 consolidate_dummy_called = 1;
1701 ok(ctx->Rip == 0xdeadbeef, "test_consolidate_dummy failed for Rip, expected: 0xdeadbeef, got: %lx\n", ctx->Rip);
1702 return (PVOID)rec->ExceptionInformation[2];
1705 static void test_restore_context(void)
1707 SETJMP_FLOAT128 *fltsave;
1708 EXCEPTION_RECORD rec;
1709 _JUMP_BUFFER buf;
1710 CONTEXT ctx;
1711 int i, pass;
1713 if (!pRtlUnwindEx || !pRtlRestoreContext || !pRtlCaptureContext || !p_setjmp)
1715 skip("RtlUnwindEx/RtlCaptureContext/RtlRestoreContext/_setjmp not found\n");
1716 return;
1719 /* RtlRestoreContext(NULL, NULL); crashes on Windows */
1721 /* test simple case of capture and restore context */
1722 pass = 0;
1723 InterlockedIncrement(&pass); /* interlocked to prevent compiler from moving after capture */
1724 pRtlCaptureContext(&ctx);
1725 if (InterlockedIncrement(&pass) == 2) /* interlocked to prevent compiler from moving before capture */
1727 pRtlRestoreContext(&ctx, NULL);
1728 ok(0, "shouldn't be reached\n");
1730 else
1731 ok(pass < 4, "unexpected pass %d\n", pass);
1733 /* test with jmp using RltRestoreContext */
1734 pass = 0;
1735 InterlockedIncrement(&pass);
1736 RtlCaptureContext(&ctx);
1737 InterlockedIncrement(&pass); /* only called once */
1738 p_setjmp(&buf);
1739 InterlockedIncrement(&pass);
1740 if (pass == 3)
1742 rec.ExceptionCode = STATUS_LONGJUMP;
1743 rec.NumberParameters = 1;
1744 rec.ExceptionInformation[0] = (DWORD64)&buf;
1746 /* uses buf.Rip instead of ctx.Rip */
1747 pRtlRestoreContext(&ctx, &rec);
1748 ok(0, "shouldn't be reached\n");
1750 else if (pass == 4)
1752 ok(buf.Rbx == ctx.Rbx, "longjmp failed for Rbx, expected: %lx, got: %lx\n", buf.Rbx, ctx.Rbx);
1753 ok(buf.Rsp == ctx.Rsp, "longjmp failed for Rsp, expected: %lx, got: %lx\n", buf.Rsp, ctx.Rsp);
1754 ok(buf.Rbp == ctx.Rbp, "longjmp failed for Rbp, expected: %lx, got: %lx\n", buf.Rbp, ctx.Rbp);
1755 ok(buf.Rsi == ctx.Rsi, "longjmp failed for Rsi, expected: %lx, got: %lx\n", buf.Rsi, ctx.Rsi);
1756 ok(buf.Rdi == ctx.Rdi, "longjmp failed for Rdi, expected: %lx, got: %lx\n", buf.Rdi, ctx.Rdi);
1757 ok(buf.R12 == ctx.R12, "longjmp failed for R12, expected: %lx, got: %lx\n", buf.R12, ctx.R12);
1758 ok(buf.R13 == ctx.R13, "longjmp failed for R13, expected: %lx, got: %lx\n", buf.R13, ctx.R13);
1759 ok(buf.R14 == ctx.R14, "longjmp failed for R14, expected: %lx, got: %lx\n", buf.R14, ctx.R14);
1760 ok(buf.R15 == ctx.R15, "longjmp failed for R15, expected: %lx, got: %lx\n", buf.R15, ctx.R15);
1762 fltsave = &buf.Xmm6;
1763 for (i = 0; i < 10; i++)
1765 ok(fltsave[i].Part[0] == ctx.u.FltSave.XmmRegisters[i + 6].Low,
1766 "longjmp failed for Xmm%d, expected %lx, got %lx\n", i + 6,
1767 fltsave[i].Part[0], ctx.u.FltSave.XmmRegisters[i + 6].Low);
1769 ok(fltsave[i].Part[1] == ctx.u.FltSave.XmmRegisters[i + 6].High,
1770 "longjmp failed for Xmm%d, expected %lx, got %lx\n", i + 6,
1771 fltsave[i].Part[1], ctx.u.FltSave.XmmRegisters[i + 6].High);
1774 else
1775 ok(0, "unexpected pass %d\n", pass);
1777 /* test with jmp through RtlUnwindEx */
1778 pass = 0;
1779 InterlockedIncrement(&pass);
1780 pRtlCaptureContext(&ctx);
1781 InterlockedIncrement(&pass); /* only called once */
1782 p_setjmp(&buf);
1783 InterlockedIncrement(&pass);
1784 if (pass == 3)
1786 rec.ExceptionCode = STATUS_LONGJUMP;
1787 rec.NumberParameters = 1;
1788 rec.ExceptionInformation[0] = (DWORD64)&buf;
1790 /* uses buf.Rip instead of bogus 0xdeadbeef */
1791 pRtlUnwindEx((void*)buf.Rsp, (void*)0xdeadbeef, &rec, NULL, &ctx, NULL);
1792 ok(0, "shouldn't be reached\n");
1794 else
1795 ok(pass == 4, "unexpected pass %d\n", pass);
1798 /* test with consolidate */
1799 pass = 0;
1800 InterlockedIncrement(&pass);
1801 RtlCaptureContext(&ctx);
1802 InterlockedIncrement(&pass);
1803 if (pass == 2)
1805 rec.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
1806 rec.NumberParameters = 3;
1807 rec.ExceptionInformation[0] = (DWORD64)test_consolidate_dummy;
1808 rec.ExceptionInformation[1] = (DWORD64)&ctx;
1809 rec.ExceptionInformation[2] = ctx.Rip;
1810 ctx.Rip = 0xdeadbeef;
1812 pRtlRestoreContext(&ctx, &rec);
1813 ok(0, "shouldn't be reached\n");
1815 else if (pass == 3)
1816 ok(consolidate_dummy_called, "test_consolidate_dummy not called\n");
1817 else
1818 ok(0, "unexpected pass %d\n", pass);
1821 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1823 static const int code_offset = 1024;
1824 static RUNTIME_FUNCTION runtime_func;
1825 (*(DWORD *)context)++;
1827 runtime_func.BeginAddress = code_offset + 16;
1828 runtime_func.EndAddress = code_offset + 32;
1829 runtime_func.UnwindData = 0;
1830 return &runtime_func;
1833 static void test_dynamic_unwind(void)
1835 static const int code_offset = 1024;
1836 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1837 RUNTIME_FUNCTION *runtime_func, *func;
1838 ULONG_PTR table, base;
1839 DWORD count;
1841 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1842 runtime_func = (RUNTIME_FUNCTION *)buf;
1843 runtime_func->BeginAddress = code_offset;
1844 runtime_func->EndAddress = code_offset + 16;
1845 runtime_func->UnwindData = 0;
1846 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1847 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1849 /* Lookup function outside of any function table */
1850 base = 0xdeadbeef;
1851 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1852 ok( func == NULL,
1853 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1854 ok( !base || broken(base == 0xdeadbeef),
1855 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1857 /* Test with pointer inside of our function */
1858 base = 0xdeadbeef;
1859 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1860 ok( func == runtime_func,
1861 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1862 ok( base == (ULONG_PTR)code_mem,
1863 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1865 /* Test RtlDeleteFunctionTable */
1866 ok( pRtlDeleteFunctionTable( runtime_func ),
1867 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1868 ok( !pRtlDeleteFunctionTable( runtime_func ),
1869 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1871 /* Unaligned RUNTIME_FUNCTION pointer */
1872 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1873 runtime_func->BeginAddress = code_offset;
1874 runtime_func->EndAddress = code_offset + 16;
1875 runtime_func->UnwindData = 0;
1876 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1877 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1878 ok( pRtlDeleteFunctionTable( runtime_func ),
1879 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1881 /* Attempt to insert the same entry twice */
1882 runtime_func = (RUNTIME_FUNCTION *)buf;
1883 runtime_func->BeginAddress = code_offset;
1884 runtime_func->EndAddress = code_offset + 16;
1885 runtime_func->UnwindData = 0;
1886 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1887 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1888 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1889 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1890 ok( pRtlDeleteFunctionTable( runtime_func ),
1891 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1892 ok( pRtlDeleteFunctionTable( runtime_func ),
1893 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1894 ok( !pRtlDeleteFunctionTable( runtime_func ),
1895 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1897 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1898 table = (ULONG_PTR)code_mem;
1899 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1900 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1902 /* Test RtlInstallFunctionTableCallback with both low bits set */
1903 table = (ULONG_PTR)code_mem | 0x3;
1904 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1905 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1907 /* Lookup function outside of any function table */
1908 count = 0;
1909 base = 0xdeadbeef;
1910 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1911 ok( func == NULL,
1912 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1913 ok( !base || broken(base == 0xdeadbeef),
1914 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1915 ok( !count,
1916 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1918 /* Test with pointer inside of our function */
1919 count = 0;
1920 base = 0xdeadbeef;
1921 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1922 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1923 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1924 ok( base == (ULONG_PTR)code_mem,
1925 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1926 ok( count == 1,
1927 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1929 /* Clean up again */
1930 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1931 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1932 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1933 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1937 static int termination_handler_called;
1938 static void WINAPI termination_handler(ULONG flags, ULONG64 frame)
1940 termination_handler_called++;
1942 ok(flags == 1 || broken(flags == 0x401), "flags = %x\n", flags);
1943 ok(frame == 0x1234, "frame = %p\n", (void*)frame);
1946 static void test___C_specific_handler(void)
1948 DISPATCHER_CONTEXT dispatch;
1949 EXCEPTION_RECORD rec;
1950 CONTEXT context;
1951 ULONG64 frame;
1952 EXCEPTION_DISPOSITION ret;
1953 SCOPE_TABLE scope_table;
1955 if (!p__C_specific_handler)
1957 win_skip("__C_specific_handler not available\n");
1958 return;
1961 memset(&rec, 0, sizeof(rec));
1962 rec.ExceptionFlags = 2; /* EH_UNWINDING */
1963 frame = 0x1234;
1964 memset(&dispatch, 0, sizeof(dispatch));
1965 dispatch.ImageBase = (ULONG_PTR)GetModuleHandleA(NULL);
1966 dispatch.ControlPc = dispatch.ImageBase + 0x200;
1967 dispatch.HandlerData = &scope_table;
1968 dispatch.ContextRecord = &context;
1969 scope_table.Count = 1;
1970 scope_table.ScopeRecord[0].BeginAddress = 0x200;
1971 scope_table.ScopeRecord[0].EndAddress = 0x400;
1972 scope_table.ScopeRecord[0].HandlerAddress = (ULONG_PTR)termination_handler-dispatch.ImageBase;
1973 scope_table.ScopeRecord[0].JumpTarget = 0;
1974 memset(&context, 0, sizeof(context));
1976 termination_handler_called = 0;
1977 ret = p__C_specific_handler(&rec, frame, &context, &dispatch);
1978 ok(ret == ExceptionContinueSearch, "__C_specific_handler returned %x\n", ret);
1979 ok(termination_handler_called == 1, "termination_handler_called = %d\n",
1980 termination_handler_called);
1981 ok(dispatch.ScopeIndex == 1, "dispatch.ScopeIndex = %d\n", dispatch.ScopeIndex);
1983 ret = p__C_specific_handler(&rec, frame, &context, &dispatch);
1984 ok(ret == ExceptionContinueSearch, "__C_specific_handler returned %x\n", ret);
1985 ok(termination_handler_called == 1, "termination_handler_called = %d\n",
1986 termination_handler_called);
1987 ok(dispatch.ScopeIndex == 1, "dispatch.ScopeIndex = %d\n", dispatch.ScopeIndex);
1990 #endif /* __x86_64__ */
1992 #if defined(__i386__) || defined(__x86_64__)
1994 static DWORD WINAPI dummy_thread(void *arg)
1996 return 0;
1999 static void test_debug_registers(void)
2001 static const struct
2003 ULONG_PTR dr0, dr1, dr2, dr3, dr6, dr7;
2005 tests[] =
2007 { 0x42424240, 0, 0x126bb070, 0x0badbad0, 0, 0xffff0115 },
2008 { 0x42424242, 0, 0x100f0fe7, 0x0abebabe, 0, 0x115 },
2010 NTSTATUS status;
2011 CONTEXT ctx;
2012 HANDLE thread;
2013 int i;
2015 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
2017 memset(&ctx, 0, sizeof(ctx));
2018 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2019 ctx.Dr0 = tests[i].dr0;
2020 ctx.Dr1 = tests[i].dr1;
2021 ctx.Dr2 = tests[i].dr2;
2022 ctx.Dr3 = tests[i].dr3;
2023 ctx.Dr6 = tests[i].dr6;
2024 ctx.Dr7 = tests[i].dr7;
2026 status = pNtSetContextThread(GetCurrentThread(), &ctx);
2027 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %08x\n", status);
2029 memset(&ctx, 0, sizeof(ctx));
2030 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2032 status = pNtGetContextThread(GetCurrentThread(), &ctx);
2033 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %08x\n", status);
2034 ok(ctx.Dr0 == tests[i].dr0, "test %d: expected %lx, got %lx\n", i, tests[i].dr0, (DWORD_PTR)ctx.Dr0);
2035 ok(ctx.Dr1 == tests[i].dr1, "test %d: expected %lx, got %lx\n", i, tests[i].dr1, (DWORD_PTR)ctx.Dr1);
2036 ok(ctx.Dr2 == tests[i].dr2, "test %d: expected %lx, got %lx\n", i, tests[i].dr2, (DWORD_PTR)ctx.Dr2);
2037 ok(ctx.Dr3 == tests[i].dr3, "test %d: expected %lx, got %lx\n", i, tests[i].dr3, (DWORD_PTR)ctx.Dr3);
2038 ok((ctx.Dr6 & 0xf00f) == tests[i].dr6, "test %d: expected %lx, got %lx\n", i, tests[i].dr6, (DWORD_PTR)ctx.Dr6);
2039 ok((ctx.Dr7 & ~0xdc00) == tests[i].dr7, "test %d: expected %lx, got %lx\n", i, tests[i].dr7, (DWORD_PTR)ctx.Dr7);
2042 memset(&ctx, 0, sizeof(ctx));
2043 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2044 ctx.Dr0 = 0xffffffff;
2045 ctx.Dr1 = 0xffffffff;
2046 ctx.Dr2 = 0xffffffff;
2047 ctx.Dr3 = 0xffffffff;
2048 ctx.Dr6 = 0xffffffff;
2049 ctx.Dr7 = 0x00000400;
2050 status = pNtSetContextThread(GetCurrentThread(), &ctx);
2051 ok(status == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", status);
2052 thread = CreateThread(NULL, 0, dummy_thread, NULL, CREATE_SUSPENDED, NULL);
2053 ok(thread != INVALID_HANDLE_VALUE, "CreateThread failed with %d\n", GetLastError());
2054 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2055 status = pNtGetContextThread(thread, &ctx);
2056 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
2057 ok(!ctx.Dr0, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr0);
2058 ok(!ctx.Dr1, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr1);
2059 ok(!ctx.Dr2, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr2);
2060 ok(!ctx.Dr3, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr3);
2061 todo_wine ok(!ctx.Dr6, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr6);
2062 todo_wine ok(!ctx.Dr7, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr7);
2063 TerminateThread(thread, 0);
2064 CloseHandle(thread);
2067 static DWORD outputdebugstring_exceptions;
2069 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
2071 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2072 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2074 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
2075 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
2076 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
2077 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
2078 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
2079 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
2081 outputdebugstring_exceptions++;
2082 return EXCEPTION_CONTINUE_SEARCH;
2085 static void test_outputdebugstring(DWORD numexc, BOOL todo)
2087 PVOID vectored_handler;
2089 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
2091 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
2092 return;
2095 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
2096 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2098 outputdebugstring_exceptions = 0;
2099 OutputDebugStringA("Hello World");
2101 todo_wine_if(todo)
2102 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
2103 outputdebugstring_exceptions, numexc);
2105 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2108 static DWORD ripevent_exceptions;
2110 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
2112 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2113 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2115 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
2116 rec->ExceptionCode, DBG_RIPEXCEPTION);
2117 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
2118 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
2119 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
2120 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
2121 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
2123 ripevent_exceptions++;
2124 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2127 static void test_ripevent(DWORD numexc)
2129 EXCEPTION_RECORD record;
2130 PVOID vectored_handler;
2132 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
2134 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
2135 return;
2138 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
2139 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2141 record.ExceptionCode = DBG_RIPEXCEPTION;
2142 record.ExceptionFlags = 0;
2143 record.ExceptionRecord = NULL;
2144 record.ExceptionAddress = NULL;
2145 record.NumberParameters = 2;
2146 record.ExceptionInformation[0] = 0x11223344;
2147 record.ExceptionInformation[1] = 0x55667788;
2149 ripevent_exceptions = 0;
2150 pRtlRaiseException(&record);
2151 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
2152 ripevent_exceptions, numexc);
2154 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2157 static DWORD debug_service_exceptions;
2159 static LONG CALLBACK debug_service_handler(EXCEPTION_POINTERS *ExceptionInfo)
2161 EXCEPTION_RECORD *rec = ExceptionInfo->ExceptionRecord;
2162 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2164 ok(rec->ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode is %08x instead of %08x\n",
2165 rec->ExceptionCode, EXCEPTION_BREAKPOINT);
2167 #ifdef __i386__
2168 ok(ExceptionInfo->ContextRecord->Eip == (DWORD)code_mem + 0x1c,
2169 "expected Eip = %x, got %x\n", (DWORD)code_mem + 0x1c, ExceptionInfo->ContextRecord->Eip);
2170 ok(rec->NumberParameters == (is_wow64 ? 1 : 3),
2171 "ExceptionParameters is %d instead of %d\n", rec->NumberParameters, is_wow64 ? 1 : 3);
2172 ok(rec->ExceptionInformation[0] == ExceptionInfo->ContextRecord->Eax,
2173 "expected ExceptionInformation[0] = %x, got %lx\n",
2174 ExceptionInfo->ContextRecord->Eax, rec->ExceptionInformation[0]);
2175 if (!is_wow64)
2177 ok(rec->ExceptionInformation[1] == 0x11111111,
2178 "got ExceptionInformation[1] = %lx\n", rec->ExceptionInformation[1]);
2179 ok(rec->ExceptionInformation[2] == 0x22222222,
2180 "got ExceptionInformation[2] = %lx\n", rec->ExceptionInformation[2]);
2182 #else
2183 ok(ExceptionInfo->ContextRecord->Rip == (DWORD_PTR)code_mem + 0x2f,
2184 "expected Rip = %lx, got %lx\n", (DWORD_PTR)code_mem + 0x2f, ExceptionInfo->ContextRecord->Rip);
2185 ok(rec->NumberParameters == 1,
2186 "ExceptionParameters is %d instead of 1\n", rec->NumberParameters);
2187 ok(rec->ExceptionInformation[0] == ExceptionInfo->ContextRecord->Rax,
2188 "expected ExceptionInformation[0] = %lx, got %lx\n",
2189 ExceptionInfo->ContextRecord->Rax, rec->ExceptionInformation[0]);
2190 #endif
2192 debug_service_exceptions++;
2193 return (rec->ExceptionCode == EXCEPTION_BREAKPOINT) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2196 #ifdef __i386__
2198 static const BYTE call_debug_service_code[] = {
2199 0x53, /* pushl %ebx */
2200 0x57, /* pushl %edi */
2201 0x8b, 0x44, 0x24, 0x0c, /* movl 12(%esp),%eax */
2202 0xb9, 0x11, 0x11, 0x11, 0x11, /* movl $0x11111111,%ecx */
2203 0xba, 0x22, 0x22, 0x22, 0x22, /* movl $0x22222222,%edx */
2204 0xbb, 0x33, 0x33, 0x33, 0x33, /* movl $0x33333333,%ebx */
2205 0xbf, 0x44, 0x44, 0x44, 0x44, /* movl $0x44444444,%edi */
2206 0xcd, 0x2d, /* int $0x2d */
2207 0xeb, /* jmp $+17 */
2208 0x0f, 0x1f, 0x00, /* nop */
2209 0x31, 0xc0, /* xorl %eax,%eax */
2210 0xeb, 0x0c, /* jmp $+14 */
2211 0x90, 0x90, 0x90, 0x90, /* nop */
2212 0x90, 0x90, 0x90, 0x90,
2213 0x90,
2214 0x31, 0xc0, /* xorl %eax,%eax */
2215 0x40, /* incl %eax */
2216 0x5f, /* popl %edi */
2217 0x5b, /* popl %ebx */
2218 0xc3, /* ret */
2221 #else
2223 static const BYTE call_debug_service_code[] = {
2224 0x53, /* push %rbx */
2225 0x57, /* push %rdi */
2226 0x48, 0x89, 0xc8, /* movl %rcx,%rax */
2227 0x48, 0xb9, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, /* movabs $0x1111111111111111,%rcx */
2228 0x48, 0xba, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* movabs $0x2222222222222222,%rdx */
2229 0x48, 0xbb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, /* movabs $0x3333333333333333,%rbx */
2230 0x48, 0xbf, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, /* movabs $0x4444444444444444,%rdi */
2231 0xcd, 0x2d, /* int $0x2d */
2232 0xeb, /* jmp $+17 */
2233 0x0f, 0x1f, 0x00, /* nop */
2234 0x48, 0x31, 0xc0, /* xor %rax,%rax */
2235 0xeb, 0x0e, /* jmp $+16 */
2236 0x90, 0x90, 0x90, 0x90, /* nop */
2237 0x90, 0x90, 0x90, 0x90,
2238 0x48, 0x31, 0xc0, /* xor %rax,%rax */
2239 0x48, 0xff, 0xc0, /* inc %rax */
2240 0x5f, /* pop %rdi */
2241 0x5b, /* pop %rbx */
2242 0xc3, /* ret */
2245 #endif
2247 static void test_debug_service(DWORD numexc)
2249 DWORD (CDECL *func)(DWORD_PTR) = code_mem;
2250 DWORD expected_exc, expected_ret;
2251 void *vectored_handler;
2252 DWORD ret;
2254 /* code will return 0 if execution resumes immediately after "int $0x2d", otherwise 1 */
2255 memcpy(code_mem, call_debug_service_code, sizeof(call_debug_service_code));
2257 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &debug_service_handler);
2258 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2260 expected_exc = numexc;
2261 expected_ret = (numexc != 0);
2263 /* BREAKPOINT_BREAK */
2264 debug_service_exceptions = 0;
2265 ret = func(0);
2266 ok(debug_service_exceptions == expected_exc,
2267 "BREAKPOINT_BREAK generated %u exceptions, expected %u\n",
2268 debug_service_exceptions, expected_exc);
2269 ok(ret == expected_ret,
2270 "BREAKPOINT_BREAK returned %u, expected %u\n", ret, expected_ret);
2272 /* BREAKPOINT_PROMPT */
2273 debug_service_exceptions = 0;
2274 ret = func(2);
2275 ok(debug_service_exceptions == expected_exc,
2276 "BREAKPOINT_PROMPT generated %u exceptions, expected %u\n",
2277 debug_service_exceptions, expected_exc);
2278 ok(ret == expected_ret,
2279 "BREAKPOINT_PROMPT returned %u, expected %u\n", ret, expected_ret);
2281 /* invalid debug service */
2282 debug_service_exceptions = 0;
2283 ret = func(6);
2284 ok(debug_service_exceptions == expected_exc,
2285 "invalid debug service generated %u exceptions, expected %u\n",
2286 debug_service_exceptions, expected_exc);
2287 ok(ret == expected_ret,
2288 "invalid debug service returned %u, expected %u\n", ret, expected_ret);
2290 expected_exc = (is_wow64 ? numexc : 0);
2291 expected_ret = (is_wow64 && numexc);
2293 /* BREAKPOINT_PRINT */
2294 debug_service_exceptions = 0;
2295 ret = func(1);
2296 ok(debug_service_exceptions == expected_exc,
2297 "BREAKPOINT_PRINT generated %u exceptions, expected %u\n",
2298 debug_service_exceptions, expected_exc);
2299 ok(ret == expected_ret,
2300 "BREAKPOINT_PRINT returned %u, expected %u\n", ret, expected_ret);
2302 /* BREAKPOINT_LOAD_SYMBOLS */
2303 debug_service_exceptions = 0;
2304 ret = func(3);
2305 ok(debug_service_exceptions == expected_exc,
2306 "BREAKPOINT_LOAD_SYMBOLS generated %u exceptions, expected %u\n",
2307 debug_service_exceptions, expected_exc);
2308 ok(ret == expected_ret,
2309 "BREAKPOINT_LOAD_SYMBOLS returned %u, expected %u\n", ret, expected_ret);
2311 /* BREAKPOINT_UNLOAD_SYMBOLS */
2312 debug_service_exceptions = 0;
2313 ret = func(4);
2314 ok(debug_service_exceptions == expected_exc,
2315 "BREAKPOINT_UNLOAD_SYMBOLS generated %u exceptions, expected %u\n",
2316 debug_service_exceptions, expected_exc);
2317 ok(ret == expected_ret,
2318 "BREAKPOINT_UNLOAD_SYMBOLS returned %u, expected %u\n", ret, expected_ret);
2320 /* BREAKPOINT_COMMAND_STRING */
2321 debug_service_exceptions = 0;
2322 ret = func(5);
2323 ok(debug_service_exceptions == expected_exc || broken(debug_service_exceptions == numexc),
2324 "BREAKPOINT_COMMAND_STRING generated %u exceptions, expected %u\n",
2325 debug_service_exceptions, expected_exc);
2326 ok(ret == expected_ret || broken(ret == (numexc != 0)),
2327 "BREAKPOINT_COMMAND_STRING returned %u, expected %u\n", ret, expected_ret);
2329 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2332 static DWORD breakpoint_exceptions;
2334 static LONG CALLBACK breakpoint_handler(EXCEPTION_POINTERS *ExceptionInfo)
2336 EXCEPTION_RECORD *rec = ExceptionInfo->ExceptionRecord;
2337 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2339 ok(rec->ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode is %08x instead of %08x\n",
2340 rec->ExceptionCode, EXCEPTION_BREAKPOINT);
2342 #ifdef __i386__
2343 ok(ExceptionInfo->ContextRecord->Eip == (DWORD)code_mem + 1,
2344 "expected Eip = %x, got %x\n", (DWORD)code_mem + 1, ExceptionInfo->ContextRecord->Eip);
2345 ok(rec->NumberParameters == (is_wow64 ? 1 : 3),
2346 "ExceptionParameters is %d instead of %d\n", rec->NumberParameters, is_wow64 ? 1 : 3);
2347 ok(rec->ExceptionInformation[0] == 0,
2348 "got ExceptionInformation[0] = %lx\n", rec->ExceptionInformation[0]);
2349 ExceptionInfo->ContextRecord->Eip = (DWORD)code_mem + 2;
2350 #else
2351 ok(ExceptionInfo->ContextRecord->Rip == (DWORD_PTR)code_mem + 1,
2352 "expected Rip = %lx, got %lx\n", (DWORD_PTR)code_mem + 1, ExceptionInfo->ContextRecord->Rip);
2353 ok(rec->NumberParameters == 1,
2354 "ExceptionParameters is %d instead of 1\n", rec->NumberParameters);
2355 ok(rec->ExceptionInformation[0] == 0,
2356 "got ExceptionInformation[0] = %lx\n", rec->ExceptionInformation[0]);
2357 ExceptionInfo->ContextRecord->Rip = (DWORD_PTR)code_mem + 2;
2358 #endif
2360 breakpoint_exceptions++;
2361 return (rec->ExceptionCode == EXCEPTION_BREAKPOINT) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2364 static const BYTE breakpoint_code[] = {
2365 0xcd, 0x03, /* int $0x3 */
2366 0xc3, /* ret */
2369 static void test_breakpoint(DWORD numexc)
2371 DWORD (CDECL *func)(void) = code_mem;
2372 void *vectored_handler;
2374 memcpy(code_mem, breakpoint_code, sizeof(breakpoint_code));
2376 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &breakpoint_handler);
2377 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2379 breakpoint_exceptions = 0;
2380 func();
2381 ok(breakpoint_exceptions == numexc, "int $0x3 generated %u exceptions, expected %u\n",
2382 breakpoint_exceptions, numexc);
2384 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2387 static void test_vectored_continue_handler(void)
2389 PVOID handler1, handler2;
2390 ULONG ret;
2392 if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
2394 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
2395 return;
2398 handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
2399 ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
2401 handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
2402 ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
2403 ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
2405 if (pRtlRemoveVectoredExceptionHandler)
2407 ret = pRtlRemoveVectoredExceptionHandler(handler1);
2408 ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
2411 ret = pRtlRemoveVectoredContinueHandler(handler1);
2412 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
2414 ret = pRtlRemoveVectoredContinueHandler(handler2);
2415 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
2417 ret = pRtlRemoveVectoredContinueHandler(handler1);
2418 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
2420 ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
2421 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
2423 #endif /* defined(__i386__) || defined(__x86_64__) */
2425 START_TEST(exception)
2427 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
2428 #if defined(__x86_64__)
2429 HMODULE hmsvcrt = LoadLibraryA("msvcrt.dll");
2430 #endif
2432 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2433 if(!code_mem) {
2434 trace("VirtualAlloc failed\n");
2435 return;
2438 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
2439 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
2440 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
2441 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
2442 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
2443 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
2444 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
2445 "RtlAddVectoredExceptionHandler" );
2446 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
2447 "RtlRemoveVectoredExceptionHandler" );
2448 pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
2449 "RtlAddVectoredContinueHandler" );
2450 pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
2451 "RtlRemoveVectoredContinueHandler" );
2452 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
2453 "NtQueryInformationProcess" );
2454 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
2455 "NtSetInformationProcess" );
2456 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
2458 #ifdef __i386__
2459 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
2461 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
2462 have_vectored_api = TRUE;
2463 else
2464 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
2466 my_argc = winetest_get_mainargs( &my_argv );
2467 if (my_argc >= 4)
2469 void *addr;
2470 sscanf( my_argv[3], "%p", &addr );
2472 if (addr != &test_stage)
2474 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
2475 return;
2478 /* child must be run under a debugger */
2479 if (!NtCurrentTeb()->Peb->BeingDebugged)
2481 ok(FALSE, "child process not being debugged?\n");
2482 return;
2485 if (pRtlRaiseException)
2487 test_stage = 1;
2488 run_rtlraiseexception_test(0x12345);
2489 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
2490 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
2491 test_stage = 2;
2492 run_rtlraiseexception_test(0x12345);
2493 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
2494 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
2495 test_stage = 3;
2496 test_outputdebugstring(0, FALSE);
2497 test_stage = 4;
2498 test_outputdebugstring(2, TRUE); /* is this a Windows bug? */
2499 test_stage = 5;
2500 test_ripevent(0);
2501 test_stage = 6;
2502 test_ripevent(1);
2503 test_stage = 7;
2504 test_debug_service(0);
2505 test_stage = 8;
2506 test_debug_service(1);
2507 test_stage = 9;
2508 test_breakpoint(0);
2509 test_stage = 10;
2510 test_breakpoint(1);
2512 else
2513 skip( "RtlRaiseException not found\n" );
2515 /* rest of tests only run in parent */
2516 return;
2519 test_unwind();
2520 test_exceptions();
2521 test_rtlraiseexception();
2522 test_debug_registers();
2523 test_outputdebugstring(1, FALSE);
2524 test_ripevent(1);
2525 test_debug_service(1);
2526 test_breakpoint(1);
2527 test_vectored_continue_handler();
2528 test_debugger();
2529 test_simd_exceptions();
2530 test_fpu_exceptions();
2531 test_dpe_exceptions();
2532 test_prot_fault();
2534 #elif defined(__x86_64__)
2535 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
2536 "RtlAddFunctionTable" );
2537 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
2538 "RtlDeleteFunctionTable" );
2539 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
2540 "RtlInstallFunctionTableCallback" );
2541 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
2542 "RtlLookupFunctionEntry" );
2543 p__C_specific_handler = (void *)GetProcAddress( hntdll,
2544 "__C_specific_handler" );
2545 pRtlCaptureContext = (void *)GetProcAddress( hntdll,
2546 "RtlCaptureContext" );
2547 pRtlRestoreContext = (void *)GetProcAddress( hntdll,
2548 "RtlRestoreContext" );
2549 pRtlUnwindEx = (void *)GetProcAddress( hntdll,
2550 "RtlUnwindEx" );
2551 p_setjmp = (void *)GetProcAddress( hmsvcrt,
2552 "_setjmp" );
2554 test_debug_registers();
2555 test_outputdebugstring(1, FALSE);
2556 test_ripevent(1);
2557 test_debug_service(1);
2558 test_breakpoint(1);
2559 test_vectored_continue_handler();
2560 test_virtual_unwind();
2561 test___C_specific_handler();
2562 test_restore_context();
2564 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
2565 test_dynamic_unwind();
2566 else
2567 skip( "Dynamic unwind functions not found\n" );
2569 #endif
2571 VirtualFree(code_mem, 0, MEM_RELEASE);