[NTDLL_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / ntdll / exception.c
1 /*
2 * Unit test suite for ntdll exceptions
3 *
4 * Copyright 2005 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #ifndef _WIN32_WINNT
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
26 #endif
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winnt.h"
35 #include "winreg.h"
36 #include "wine/winternl.h"
37 #include "wine/exception.h"
38 #include "wine/test.h"
39
40 static void *code_mem;
41
42 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
43 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
44 static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
45 static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
46 static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
47 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
48 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
49 static PVOID (WINAPI *pRtlAddVectoredContinueHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
50 static ULONG (WINAPI *pRtlRemoveVectoredContinueHandler)(PVOID handler);
51 static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
52 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
53 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
54 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
55 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
56 static NTSTATUS (WINAPI *pNtClose)(HANDLE);
57
58 #if defined(__x86_64__)
59 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
60 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
61 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
62 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
63 #endif
64
65 #ifdef __i386__
66
67 #ifndef __WINE_WINTRNL_H
68 #define ProcessExecuteFlags 0x22
69 #define MEM_EXECUTE_OPTION_DISABLE 0x01
70 #define MEM_EXECUTE_OPTION_ENABLE 0x02
71 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
72 #endif
73
74 static int my_argc;
75 static char** my_argv;
76 static int test_stage;
77
78 static BOOL is_wow64;
79
80 /* Test various instruction combinations that cause a protection fault on the i386,
81 * and check what the resulting exception looks like.
82 */
83
84 static const struct exception
85 {
86 BYTE code[18]; /* asm code */
87 BYTE offset; /* offset of faulting instruction */
88 BYTE length; /* length of faulting instruction */
89 BOOL wow64_broken; /* broken on Wow64, should be skipped */
90 NTSTATUS status; /* expected status code */
91 DWORD nb_params; /* expected number of parameters */
92 DWORD params[4]; /* expected parameters */
93 NTSTATUS alt_status; /* alternative status code */
94 DWORD alt_nb_params; /* alternative number of parameters */
95 DWORD alt_params[4]; /* alternative parameters */
96 } exceptions[] =
97 {
98 /* 0 */
99 /* test some privileged instructions */
100 { { 0xfb, 0xc3 }, /* 0: sti; ret */
101 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
102 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
103 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
104 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
105 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
106 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
107 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
108 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
109 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
110 /* 5 */
111 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
112 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
113 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
114 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
115 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
116 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
117 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
118 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
119 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
120 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
121 /* 10 */
122 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
123 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
124 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
125 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
126 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
127 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
128 { { 0xfa, 0xc3 }, /* 13: cli; ret */
129 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
130
131 /* test long jump to invalid selector */
132 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
133 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
134
135 /* 15 */
136 /* test iret to invalid selector */
137 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
138 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
139 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
140
141 /* test loading an invalid selector */
142 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
143 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
144
145 /* test accessing a zero selector (%es broken on Wow64) */
146 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
147 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
148 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
149 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
150 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
151 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
152
153 /* test moving %cs -> %ss */
154 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
155 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
156
157 /* 20 */
158 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
159 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
160 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
161 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
162 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
163 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
164
165 /* test invalid interrupt */
166 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
167 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
168
169 /* test moves to/from Crx */
170 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
171 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
172 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
173 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
174 /* 25 */
175 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
176 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
177 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
178 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
179
180 /* test moves to/from Drx */
181 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
182 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
183 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
184 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
185 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
186 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
187 /* 30 */
188 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
189 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
190 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
191 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
192 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
193 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
194
195 /* test memory reads */
196 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
197 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
198 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
199 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
200 /* 35 */
201 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
202 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
203 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
204 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
205
206 /* test memory writes */
207 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
208 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
209 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
210 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
211 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
212 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
213 /* 40 */
214 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
215 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
216
217 /* test exception with cleared %ds and %es (broken on Wow64) */
218 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
219 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
220 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
221
222 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
223 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
224 };
225
226 static int got_exception;
227 static BOOL have_vectored_api;
228
229 static void run_exception_test(void *handler, const void* context,
230 const void *code, unsigned int code_size,
231 DWORD access)
232 {
233 struct {
234 EXCEPTION_REGISTRATION_RECORD frame;
235 const void *context;
236 } exc_frame;
237 void (*func)(void) = code_mem;
238 DWORD oldaccess, oldaccess2;
239
240 exc_frame.frame.Handler = handler;
241 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
242 exc_frame.context = context;
243
244 memcpy(code_mem, code, code_size);
245 if(access)
246 VirtualProtect(code_mem, code_size, access, &oldaccess);
247
248 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
249 func();
250 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
251
252 if(access)
253 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
254 }
255
256 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
257 {
258 PCONTEXT context = ExceptionInfo->ContextRecord;
259 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
260 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
261 rec->ExceptionAddress, context->Eip);
262
263 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
264 rec->ExceptionAddress, (char *)code_mem + 0xb);
265
266 if (pNtCurrentTeb()->Peb->BeingDebugged)
267 ok((void *)context->Eax == pRtlRaiseException ||
268 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
269 "debugger managed to modify Eax to %x should be %p\n",
270 context->Eax, pRtlRaiseException);
271
272 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
273 * even if raised by RtlRaiseException
274 */
275 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
276 {
277 ok(context->Eip == (DWORD)code_mem + 0xa ||
278 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
279 "Eip at %x instead of %x or %x\n", context->Eip,
280 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
281 }
282 else
283 {
284 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
285 context->Eip, (DWORD)code_mem + 0xb);
286 }
287
288 /* test if context change is preserved from vectored handler to stack handlers */
289 context->Eax = 0xf00f00f0;
290 return EXCEPTION_CONTINUE_SEARCH;
291 }
292
293 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
294 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
295 {
296 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
297 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
298
299 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
300 rec->ExceptionAddress, (char *)code_mem + 0xb);
301
302 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
303 * even if raised by RtlRaiseException
304 */
305 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
306 {
307 ok(context->Eip == (DWORD)code_mem + 0xa ||
308 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
309 "Eip at %x instead of %x or %x\n", context->Eip,
310 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
311 }
312 else
313 {
314 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
315 context->Eip, (DWORD)code_mem + 0xb);
316 }
317
318 if(have_vectored_api)
319 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
320 context->Eax);
321
322 /* give the debugger a chance to examine the state a second time */
323 /* without the exception handler changing Eip */
324 if (test_stage == 2)
325 return ExceptionContinueSearch;
326
327 /* Eip in context is decreased by 1
328 * Increase it again, else execution will continue in the middle of an instruction */
329 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
330 context->Eip += 1;
331 return ExceptionContinueExecution;
332 }
333
334
335 static const BYTE call_one_arg_code[] = {
336 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
337 0x50, /* push %eax */
338 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
339 0xff, 0xd0, /* call *%eax */
340 0x90, /* nop */
341 0x90, /* nop */
342 0x90, /* nop */
343 0x90, /* nop */
344 0xc3, /* ret */
345 };
346
347
348 static void run_rtlraiseexception_test(DWORD exceptioncode)
349 {
350 EXCEPTION_REGISTRATION_RECORD frame;
351 EXCEPTION_RECORD record;
352 PVOID vectored_handler = NULL;
353
354 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
355
356 record.ExceptionCode = exceptioncode;
357 record.ExceptionFlags = 0;
358 record.ExceptionRecord = NULL;
359 record.ExceptionAddress = NULL; /* does not matter, copied return address */
360 record.NumberParameters = 0;
361
362 frame.Handler = rtlraiseexception_handler;
363 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
364
365 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
366
367 pNtCurrentTeb()->Tib.ExceptionList = &frame;
368 if (have_vectored_api)
369 {
370 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
371 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
372 }
373
374 func(pRtlRaiseException, &record);
375 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
376 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
377
378 if (have_vectored_api)
379 pRtlRemoveVectoredExceptionHandler(vectored_handler);
380 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
381 }
382
383 static void test_rtlraiseexception(void)
384 {
385 if (!pRtlRaiseException)
386 {
387 skip("RtlRaiseException not found\n");
388 return;
389 }
390
391 /* test without debugger */
392 run_rtlraiseexception_test(0x12345);
393 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
394 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
395 }
396
397 static DWORD unwind_expected_eax;
398
399 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
400 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
401 {
402 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
403 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
404
405 ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
406 rec->ExceptionCode, STATUS_UNWIND);
407 ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
408 rec->ExceptionAddress, (char *)code_mem + 0x22);
409 ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
410 context->Eax, unwind_expected_eax);
411
412 context->Eax += 1;
413 return ExceptionContinueSearch;
414 }
415
416 static const BYTE call_unwind_code[] = {
417 0x55, /* push %ebp */
418 0x53, /* push %ebx */
419 0x56, /* push %esi */
420 0x57, /* push %edi */
421 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
422 0x58, /* 0: pop %eax */
423 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
424 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
425 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
426 0x50, /* push %eax */
427 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
428 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
429 0xff, 0xd0, /* call *%eax */
430 0x5f, /* pop %edi */
431 0x5e, /* pop %esi */
432 0x5b, /* pop %ebx */
433 0x5d, /* pop %ebp */
434 0xc3, /* ret */
435 0xcc, /* int $3 */
436 };
437
438 static void test_unwind(void)
439 {
440 EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
441 DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
442 DWORD retval;
443
444 memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
445
446 /* add first unwind handler */
447 frame1->Handler = unwind_handler;
448 frame1->Prev = pNtCurrentTeb()->Tib.ExceptionList;
449 pNtCurrentTeb()->Tib.ExceptionList = frame1;
450
451 /* add second unwind handler */
452 frame2->Handler = unwind_handler;
453 frame2->Prev = pNtCurrentTeb()->Tib.ExceptionList;
454 pNtCurrentTeb()->Tib.ExceptionList = frame2;
455
456 /* test unwind to current frame */
457 unwind_expected_eax = 0xDEAD0000;
458 retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
459 ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
460 ok(pNtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
461 pNtCurrentTeb()->Tib.ExceptionList, frame2);
462
463 /* unwind to frame1 */
464 unwind_expected_eax = 0xDEAD0000;
465 retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
466 ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
467 ok(pNtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
468 pNtCurrentTeb()->Tib.ExceptionList, frame1);
469
470 /* restore original handler */
471 pNtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
472 }
473
474 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
475 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
476 {
477 const struct exception *except = *(const struct exception **)(frame + 1);
478 unsigned int i, entry = except - exceptions;
479
480 got_exception++;
481 trace( "exception %u: %x flags:%x addr:%p\n",
482 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
483
484 ok( rec->ExceptionCode == except->status ||
485 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
486 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
487 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
488 "%u: Wrong exception address %p/%p\n", entry,
489 rec->ExceptionAddress, (char*)code_mem + except->offset );
490
491 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
492 {
493 ok( rec->NumberParameters == except->nb_params,
494 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
495 }
496 else
497 {
498 ok( rec->NumberParameters == except->alt_nb_params,
499 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
500 }
501
502 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
503 /* instead of page faults for accesses beyond 0xffffffff */
504 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
505 {
506 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
507 goto skip_params;
508 }
509
510 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
511 if (except->nb_params == 2 && rec->NumberParameters == 2
512 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
513 && except->params[0] == rec->ExceptionInformation[0])
514 {
515 goto skip_params;
516 }
517
518 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
519 {
520 for (i = 0; i < rec->NumberParameters; i++)
521 ok( rec->ExceptionInformation[i] == except->params[i],
522 "%u: Wrong parameter %d: %lx/%x\n",
523 entry, i, rec->ExceptionInformation[i], except->params[i] );
524 }
525 else
526 {
527 for (i = 0; i < rec->NumberParameters; i++)
528 ok( rec->ExceptionInformation[i] == except->alt_params[i],
529 "%u: Wrong parameter %d: %lx/%x\n",
530 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
531 }
532
533 skip_params:
534 /* don't handle exception if it's not the address we expected */
535 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
536
537 context->Eip += except->length;
538 return ExceptionContinueExecution;
539 }
540
541 static void test_prot_fault(void)
542 {
543 unsigned int i;
544
545 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
546 {
547 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
548 {
549 skip( "Exception %u broken on Wow64\n", i );
550 continue;
551 }
552 got_exception = 0;
553 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
554 sizeof(exceptions[i].code), 0);
555 if (!i && !got_exception)
556 {
557 trace( "No exception, assuming win9x, no point in testing further\n" );
558 break;
559 }
560 ok( got_exception == (exceptions[i].status != 0),
561 "%u: bad exception count %d\n", i, got_exception );
562 }
563 }
564
565 struct dbgreg_test {
566 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
567 };
568
569 /* test handling of debug registers */
570 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
571 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
572 {
573 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
574
575 context->Eip += 2; /* Skips the popl (%eax) */
576 context->Dr0 = test->dr0;
577 context->Dr1 = test->dr1;
578 context->Dr2 = test->dr2;
579 context->Dr3 = test->dr3;
580 context->Dr6 = test->dr6;
581 context->Dr7 = test->dr7;
582 return ExceptionContinueExecution;
583 }
584
585 #define CHECK_DEBUG_REG(n, m) \
586 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
587 test_num, test->dr##n, ctx.Dr##n)
588
589 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
590 {
591 CONTEXT ctx;
592 NTSTATUS status;
593
594 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
595 status = pNtGetContextThread(GetCurrentThread(), &ctx);
596 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
597
598 CHECK_DEBUG_REG(0, ~0);
599 CHECK_DEBUG_REG(1, ~0);
600 CHECK_DEBUG_REG(2, ~0);
601 CHECK_DEBUG_REG(3, ~0);
602 CHECK_DEBUG_REG(6, 0x0f);
603 CHECK_DEBUG_REG(7, ~0xdc00);
604 }
605
606 static const BYTE segfault_code[5] = {
607 0x31, 0xc0, /* xor %eax,%eax */
608 0x8f, 0x00, /* popl (%eax) - cause exception */
609 0xc3 /* ret */
610 };
611
612 /* test the single step exception behaviour */
613 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
614 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
615 {
616 got_exception++;
617 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
618
619 if( got_exception < 3)
620 context->EFlags |= 0x100; /* single step until popf instruction */
621 else {
622 /* show that the last single step exception on the popf instruction
623 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
624 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
625 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
626 }
627
628 return ExceptionContinueExecution;
629 }
630
631 static const BYTE single_stepcode[] = {
632 0x9c, /* pushf */
633 0x58, /* pop %eax */
634 0x0d,0,1,0,0, /* or $0x100,%eax */
635 0x50, /* push %eax */
636 0x9d, /* popf */
637 0x35,0,1,0,0, /* xor $0x100,%eax */
638 0x50, /* push %eax */
639 0x9d, /* popf */
640 0xc3
641 };
642
643 /* Test the alignment check (AC) flag handling. */
644 static const BYTE align_check_code[] = {
645 0x55, /* push %ebp */
646 0x89,0xe5, /* mov %esp,%ebp */
647 0x9c, /* pushf */
648 0x58, /* pop %eax */
649 0x0d,0,0,4,0, /* or $0x40000,%eax */
650 0x50, /* push %eax */
651 0x9d, /* popf */
652 0x89,0xe0, /* mov %esp, %eax */
653 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
654 0x9c, /* pushf */
655 0x58, /* pop %eax */
656 0x35,0,0,4,0, /* xor $0x40000,%eax */
657 0x50, /* push %eax */
658 0x9d, /* popf */
659 0x5d, /* pop %ebp */
660 0xc3, /* ret */
661 };
662
663 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
664 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
665 {
666 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
667 got_exception++;
668 return ExceptionContinueExecution;
669 }
670
671 /* Test the direction flag handling. */
672 static const BYTE direction_flag_code[] = {
673 0x55, /* push %ebp */
674 0x89,0xe5, /* mov %esp,%ebp */
675 0xfd, /* std */
676 0xfa, /* cli - cause exception */
677 0x5d, /* pop %ebp */
678 0xc3, /* ret */
679 };
680
681 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
682 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
683 {
684 #ifdef __GNUC__
685 unsigned int flags;
686 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
687 /* older windows versions don't clear DF properly so don't test */
688 if (flags & 0x400) trace( "eflags has DF bit set\n" );
689 #endif
690 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
691 got_exception++;
692 context->Eip++; /* skip cli */
693 context->EFlags &= ~0x400; /* make sure it is cleared on return */
694 return ExceptionContinueExecution;
695 }
696
697 /* test single stepping over hardware breakpoint */
698 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
699 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
700 {
701 got_exception++;
702 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
703 "wrong exception code: %x\n", rec->ExceptionCode);
704
705 if(got_exception == 1) {
706 /* hw bp exception on first nop */
707 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
708 context->Eip, (DWORD)code_mem);
709 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
710 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
711 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
712 context->EFlags |= 0x100; /* enable single stepping */
713 } else if( got_exception == 2) {
714 /* single step exception on second nop */
715 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
716 context->Eip, (DWORD)code_mem + 1);
717 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
718 /* depending on the win version the B0 bit is already set here as well
719 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
720 context->EFlags |= 0x100;
721 } else if( got_exception == 3) {
722 /* hw bp exception on second nop */
723 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
724 context->Eip, (DWORD)code_mem + 1);
725 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
726 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
727 context->Dr0 = 0; /* clear breakpoint */
728 context->EFlags |= 0x100;
729 } else {
730 /* single step exception on ret */
731 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
732 context->Eip, (DWORD)code_mem + 2);
733 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
734 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
735 }
736
737 context->Dr6 = 0; /* clear status register */
738 return ExceptionContinueExecution;
739 }
740
741 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
742
743 /* test int3 handling */
744 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
745 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
746 {
747 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
748 code_mem, rec->ExceptionAddress);
749 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
750 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
751
752 return ExceptionContinueExecution;
753 }
754
755 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
756
757
758 static void test_exceptions(void)
759 {
760 CONTEXT ctx;
761 NTSTATUS res;
762 struct dbgreg_test dreg_test;
763
764 if (!pNtGetContextThread || !pNtSetContextThread)
765 {
766 skip( "NtGetContextThread/NtSetContextThread not found\n" );
767 return;
768 }
769
770 /* test handling of debug registers */
771 memset(&dreg_test, 0, sizeof(dreg_test));
772
773 dreg_test.dr0 = 0x42424240;
774 dreg_test.dr2 = 0x126bb070;
775 dreg_test.dr3 = 0x0badbad0;
776 dreg_test.dr7 = 0xffff0115;
777 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
778 check_debug_registers(1, &dreg_test);
779
780 dreg_test.dr0 = 0x42424242;
781 dreg_test.dr2 = 0x100f0fe7;
782 dreg_test.dr3 = 0x0abebabe;
783 dreg_test.dr7 = 0x115;
784 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
785 check_debug_registers(2, &dreg_test);
786
787 /* test single stepping behavior */
788 got_exception = 0;
789 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
790 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
791
792 /* test alignment exceptions */
793 got_exception = 0;
794 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
795 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
796
797 /* test direction flag */
798 got_exception = 0;
799 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
800 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
801
802 /* test single stepping over hardware breakpoint */
803 memset(&ctx, 0, sizeof(ctx));
804 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
805 ctx.Dr7 = 3;
806 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
807 res = pNtSetContextThread( GetCurrentThread(), &ctx);
808 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
809
810 got_exception = 0;
811 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
812 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
813
814 /* test int3 handling */
815 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
816 }
817
818 static void test_debugger(void)
819 {
820 char cmdline[MAX_PATH];
821 PROCESS_INFORMATION pi;
822 STARTUPINFOA si = { 0 };
823 DEBUG_EVENT de;
824 DWORD continuestatus;
825 PVOID code_mem_address = NULL;
826 NTSTATUS status;
827 SIZE_T size_read;
828 BOOL ret;
829 int counter = 0;
830 si.cb = sizeof(si);
831
832 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
833 {
834 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
835 return;
836 }
837
838 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
839 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
840 ok(ret, "could not create child process error: %u\n", GetLastError());
841 if (!ret)
842 return;
843
844 do
845 {
846 continuestatus = DBG_CONTINUE;
847 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
848
849 if (de.dwThreadId != pi.dwThreadId)
850 {
851 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
852 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
853 continue;
854 }
855
856 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
857 {
858 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
859 {
860 skip("child process loaded at different address, terminating it\n");
861 pNtTerminateProcess(pi.hProcess, 0);
862 }
863 }
864 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
865 {
866 CONTEXT ctx;
867 int stage;
868
869 counter++;
870 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
871 sizeof(code_mem_address), &size_read);
872 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
873 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
874 sizeof(stage), &size_read);
875 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
876
877 ctx.ContextFlags = CONTEXT_FULL;
878 status = pNtGetContextThread(pi.hThread, &ctx);
879 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
880
881 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
882 de.u.Exception.ExceptionRecord.ExceptionCode,
883 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
884
885 if (counter > 100)
886 {
887 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
888 pNtTerminateProcess(pi.hProcess, 1);
889 }
890 else if (counter >= 2) /* skip startup breakpoint */
891 {
892 if (stage == 1)
893 {
894 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
895 ctx.Eip, (char *)code_mem_address + 0xb);
896 /* setting the context from debugger does not affect the context, the exception handlers gets */
897 /* uncomment once wine is fixed */
898 /* ctx.Eip = 0x12345; */
899 ctx.Eax = 0xf00f00f1;
900
901 /* let the debuggee handle the exception */
902 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
903 }
904 else if (stage == 2)
905 {
906 if (de.u.Exception.dwFirstChance)
907 {
908 /* debugger gets first chance exception with unmodified ctx.Eip */
909 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
910 ctx.Eip, (char *)code_mem_address + 0xb);
911
912 /* setting the context from debugger does not affect the context, the exception handlers gets */
913 /* uncomment once wine is fixed */
914 /* ctx.Eip = 0x12345; */
915 ctx.Eax = 0xf00f00f1;
916
917 /* pass exception to debuggee
918 * exception will not be handled and
919 * a second chance exception will be raised */
920 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
921 }
922 else
923 {
924 /* debugger gets context after exception handler has played with it */
925 /* ctx.Eip is the same value the exception handler got */
926 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
927 {
928 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
929 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
930 "Eip at 0x%x instead of %p\n",
931 ctx.Eip, (char *)code_mem_address + 0xa);
932 /* need to fixup Eip for debuggee */
933 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
934 ctx.Eip += 1;
935 }
936 else
937 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
938 ctx.Eip, (char *)code_mem_address + 0xb);
939 /* here we handle exception */
940 }
941 }
942 else if (stage == 7 || stage == 8)
943 {
944 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_INVALID_HANDLE,
945 "unexpected exception code %08x, expected %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode,
946 EXCEPTION_INVALID_HANDLE);
947 ok(de.u.Exception.ExceptionRecord.NumberParameters == 0,
948 "unexpected number of parameters %d, expected 0\n", de.u.Exception.ExceptionRecord.NumberParameters);
949
950 if (stage == 8) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
951 }
952 else
953 ok(FALSE, "unexpected stage %x\n", stage);
954
955 status = pNtSetContextThread(pi.hThread, &ctx);
956 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
957 }
958 }
959 else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
960 {
961 int stage;
962 #ifdef __REACTOS__
963 /* This will catch our DPRINTs, such as
964 * "WARNING: RtlpDphTargetDllsLogicInitialize at ..\..\lib\rtl\heappage.c:1283 is UNIMPLEMENTED!"
965 * so we need a full-size buffer to avoid a stack overflow
966 */
967 char buffer[513];
968 #else
969 char buffer[64];
970 #endif
971
972 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
973 sizeof(stage), &size_read);
974 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
975
976 ok(!de.u.DebugString.fUnicode, "unexpected unicode debug string event\n");
977 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
978 de.u.DebugString.nDebugStringLength);
979
980 memset(buffer, 0, sizeof(buffer));
981 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
982 de.u.DebugString.nDebugStringLength, &size_read);
983 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
984
985 if (stage == 3 || stage == 4)
986 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
987 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
988 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
989
990 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
991 }
992 else if (de.dwDebugEventCode == RIP_EVENT)
993 {
994 int stage;
995
996 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
997 sizeof(stage), &size_read);
998 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
999
1000 if (stage == 5 || stage == 6)
1001 {
1002 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
1003 de.u.RipInfo.dwError, 0x11223344);
1004 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
1005 de.u.RipInfo.dwType, 0x55667788);
1006 }
1007 else
1008 ok(FALSE, "unexpected stage %x\n", stage);
1009
1010 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1011 }
1012
1013 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
1014
1015 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1016
1017 winetest_wait_child_process( pi.hProcess );
1018 ret = CloseHandle(pi.hThread);
1019 ok(ret, "error %u\n", GetLastError());
1020 ret = CloseHandle(pi.hProcess);
1021 ok(ret, "error %u\n", GetLastError());
1022
1023 return;
1024 }
1025
1026 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1027 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1028 {
1029 int *stage = *(int **)(frame + 1);
1030
1031 got_exception++;
1032
1033 if( *stage == 1) {
1034 /* fault while executing sse instruction */
1035 context->Eip += 3; /* skip addps */
1036 return ExceptionContinueExecution;
1037 }
1038
1039 /* stage 2 - divide by zero fault */
1040 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1041 skip("system doesn't support SIMD exceptions\n");
1042 else {
1043 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1044 "exception code: %#x, should be %#x\n",
1045 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1046 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1047 "# of params: %i, should be 1\n",
1048 rec->NumberParameters);
1049 if( rec->NumberParameters == 1 )
1050 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1051 }
1052
1053 context->Eip += 3; /* skip divps */
1054
1055 return ExceptionContinueExecution;
1056 }
1057
1058 static const BYTE simd_exception_test[] = {
1059 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1060 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1061 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1062 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1063 0x6a, 0x01, /* push $0x1 */
1064 0x6a, 0x01, /* push $0x1 */
1065 0x6a, 0x01, /* push $0x1 */
1066 0x6a, 0x01, /* push $0x1 */
1067 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1068 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1069 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1070 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1071 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
1072 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1073 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1074 0xc3, /* ret */
1075 };
1076
1077 static const BYTE sse_check[] = {
1078 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1079 0xc3, /* ret */
1080 };
1081
1082 static void test_simd_exceptions(void)
1083 {
1084 int stage;
1085
1086 /* test if CPU & OS can do sse */
1087 stage = 1;
1088 got_exception = 0;
1089 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1090 if(got_exception) {
1091 skip("system doesn't support SSE\n");
1092 return;
1093 }
1094
1095 /* generate a SIMD exception */
1096 stage = 2;
1097 got_exception = 0;
1098 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1099 sizeof(simd_exception_test), 0);
1100 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1101 }
1102
1103 struct fpu_exception_info
1104 {
1105 DWORD exception_code;
1106 DWORD exception_offset;
1107 DWORD eip_offset;
1108 };
1109
1110 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1111 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1112 {
1113 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1114
1115 info->exception_code = rec->ExceptionCode;
1116 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1117 info->eip_offset = context->Eip - (DWORD)code_mem;
1118
1119 ++context->Eip;
1120 return ExceptionContinueExecution;
1121 }
1122
1123 static void test_fpu_exceptions(void)
1124 {
1125 static const BYTE fpu_exception_test_ie[] =
1126 {
1127 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1128 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1129 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1130 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1131 0xd9, 0xee, /* fldz */
1132 0xd9, 0xe8, /* fld1 */
1133 0xde, 0xf1, /* fdivp */
1134 0xdd, 0xd8, /* fstp %st(0) */
1135 0xdd, 0xd8, /* fstp %st(0) */
1136 0x9b, /* fwait */
1137 0xdb, 0xe2, /* fnclex */
1138 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1139 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1140 0xc3, /* ret */
1141 };
1142
1143 static const BYTE fpu_exception_test_de[] =
1144 {
1145 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1146 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1147 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1148 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1149 0xdd, 0xd8, /* fstp %st(0) */
1150 0xd9, 0xee, /* fldz */
1151 0xd9, 0xe8, /* fld1 */
1152 0xde, 0xf1, /* fdivp */
1153 0x9b, /* fwait */
1154 0xdb, 0xe2, /* fnclex */
1155 0xdd, 0xd8, /* fstp %st(0) */
1156 0xdd, 0xd8, /* fstp %st(0) */
1157 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1158 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1159 0xc3, /* ret */
1160 };
1161
1162 struct fpu_exception_info info;
1163
1164 memset(&info, 0, sizeof(info));
1165 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1166 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1167 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1168 ok(info.exception_offset == 0x19 ||
1169 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1170 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1171 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1172
1173 memset(&info, 0, sizeof(info));
1174 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1175 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1176 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1177 ok(info.exception_offset == 0x17 ||
1178 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1179 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1180 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1181 }
1182
1183 struct dpe_exception_info {
1184 BOOL exception_caught;
1185 DWORD exception_info;
1186 };
1187
1188 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1189 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1190 {
1191 DWORD old_prot;
1192 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1193
1194 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1195 "Exception code %08x\n", rec->ExceptionCode);
1196 ok(rec->NumberParameters == 2,
1197 "Parameter count: %d\n", rec->NumberParameters);
1198 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1199 "Exception address: %p, expected %p\n",
1200 (LPVOID)rec->ExceptionInformation[1], code_mem);
1201
1202 info->exception_info = rec->ExceptionInformation[0];
1203 info->exception_caught = TRUE;
1204
1205 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1206 return ExceptionContinueExecution;
1207 }
1208
1209 static void test_dpe_exceptions(void)
1210 {
1211 static const BYTE single_ret[] = {0xC3};
1212 struct dpe_exception_info info;
1213 NTSTATUS stat;
1214 BOOL has_hw_support;
1215 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1216 DWORD val;
1217 ULONG len;
1218
1219 /* Query DEP with len too small */
1220 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1221 if(stat == STATUS_INVALID_INFO_CLASS)
1222 {
1223 skip("This software platform does not support DEP\n");
1224 return;
1225 }
1226 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1227
1228 /* Query DEP */
1229 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1230 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1231 if(stat == STATUS_SUCCESS)
1232 {
1233 ok(len == sizeof val, "returned length: %d\n", len);
1234 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1235 {
1236 skip("toggling DEP impossible - status locked\n");
1237 is_permanent = TRUE;
1238 if(val & MEM_EXECUTE_OPTION_DISABLE)
1239 can_test_without = FALSE;
1240 else
1241 can_test_with = FALSE;
1242 }
1243 }
1244
1245 if(!is_permanent)
1246 {
1247 /* Enable DEP */
1248 val = MEM_EXECUTE_OPTION_DISABLE;
1249 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1250 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1251 }
1252
1253 if(can_test_with)
1254 {
1255 /* Try access to locked page with DEP on*/
1256 info.exception_caught = FALSE;
1257 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1258 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1259 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1260 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1261 "Access violation type: %08x\n", (unsigned)info.exception_info);
1262 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1263 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1264
1265 /* Try execution of data with DEP on*/
1266 info.exception_caught = FALSE;
1267 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1268 if(has_hw_support)
1269 {
1270 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1271 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1272 "Access violation type: %08x\n", (unsigned)info.exception_info);
1273 }
1274 else
1275 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1276 }
1277 else
1278 skip("DEP is in AlwaysOff state\n");
1279
1280 if(!is_permanent)
1281 {
1282 /* Disable DEP */
1283 val = MEM_EXECUTE_OPTION_ENABLE;
1284 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1285 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1286 }
1287
1288 /* page is read without exec here */
1289 if(can_test_without)
1290 {
1291 /* Try execution of data with DEP off */
1292 info.exception_caught = FALSE;
1293 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1294 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1295
1296 /* Try access to locked page with DEP off - error code is different than
1297 with hardware DEP on */
1298 info.exception_caught = FALSE;
1299 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1300 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1301 ok(info.exception_info == EXCEPTION_READ_FAULT,
1302 "Access violation type: %08x\n", (unsigned)info.exception_info);
1303 }
1304 else
1305 skip("DEP is in AlwaysOn state\n");
1306
1307 if(!is_permanent)
1308 {
1309 /* Turn off DEP permanently */
1310 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1311 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1312 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1313 }
1314
1315 /* Try to turn off DEP */
1316 val = MEM_EXECUTE_OPTION_ENABLE;
1317 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1318 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1319
1320 /* Try to turn on DEP */
1321 val = MEM_EXECUTE_OPTION_DISABLE;
1322 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1323 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1324 }
1325
1326 #elif defined(__x86_64__)
1327
1328 #define UNW_FLAG_NHANDLER 0
1329 #define UNW_FLAG_EHANDLER 1
1330 #define UNW_FLAG_UHANDLER 2
1331 #define UNW_FLAG_CHAININFO 4
1332
1333 #define UWOP_PUSH_NONVOL 0
1334 #define UWOP_ALLOC_LARGE 1
1335 #define UWOP_ALLOC_SMALL 2
1336 #define UWOP_SET_FPREG 3
1337 #define UWOP_SAVE_NONVOL 4
1338 #define UWOP_SAVE_NONVOL_FAR 5
1339 #define UWOP_SAVE_XMM128 8
1340 #define UWOP_SAVE_XMM128_FAR 9
1341 #define UWOP_PUSH_MACHFRAME 10
1342
1343 struct results
1344 {
1345 int rip_offset; /* rip offset from code start */
1346 int rbp_offset; /* rbp offset from stack pointer */
1347 int handler; /* expect handler to be set? */
1348 int rip; /* expected final rip value */
1349 int frame; /* expected frame return value */
1350 int regs[8][2]; /* expected values for registers */
1351 };
1352
1353 struct unwind_test
1354 {
1355 const BYTE *function;
1356 size_t function_size;
1357 const BYTE *unwind_info;
1358 const struct results *results;
1359 unsigned int nb_results;
1360 };
1361
1362 enum regs
1363 {
1364 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1365 r8, r9, r10, r11, r12, r13, r14, r15
1366 };
1367
1368 static const char * const reg_names[16] =
1369 {
1370 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1371 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1372 };
1373
1374 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1375
1376 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1377 {
1378 static const int code_offset = 1024;
1379 static const int unwind_offset = 2048;
1380 void *handler, *data;
1381 CONTEXT context;
1382 RUNTIME_FUNCTION runtime_func;
1383 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1384 UINT i, j, k;
1385 ULONG64 fake_stack[256];
1386 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1387 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1388
1389 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1390 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1391
1392 runtime_func.BeginAddress = code_offset;
1393 runtime_func.EndAddress = code_offset + test->function_size;
1394 runtime_func.UnwindData = unwind_offset;
1395
1396 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1397
1398 for (i = 0; i < test->nb_results; i++)
1399 {
1400 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1401 memset( &context, 0x55, sizeof(context) );
1402 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1403 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1404
1405 context.Rsp = (ULONG_PTR)fake_stack;
1406 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1407 orig_rbp = context.Rbp;
1408 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1409
1410 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1411 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1412
1413 data = (void *)0xdeadbeef;
1414 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1415 &runtime_func, &context, &data, &frame, &ctx_ptr );
1416 if (test->results[i].handler)
1417 {
1418 ok( (char *)handler == (char *)code_mem + 0x200,
1419 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1420 if (handler) ok( *(DWORD *)data == 0x08070605,
1421 "%u/%u: wrong handler data %p\n", testnum, i, data );
1422 }
1423 else
1424 {
1425 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1426 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1427 }
1428
1429 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1430 testnum, i, (void *)context.Rip, test->results[i].rip );
1431 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1432 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1433
1434 for (j = 0; j < 16; j++)
1435 {
1436 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1437
1438 for (k = 0; k < nb_regs; k++)
1439 {
1440 if (test->results[i].regs[k][0] == -1)
1441 {
1442 k = nb_regs;
1443 break;
1444 }
1445 if (test->results[i].regs[k][0] == j) break;
1446 }
1447
1448 if (j == rsp) /* rsp is special */
1449 {
1450 ok( !ctx_ptr.u2.IntegerContext[j],
1451 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1452 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1453 "%u/%u: register rsp wrong %p/%p\n",
1454 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1455 continue;
1456 }
1457
1458 if (ctx_ptr.u2.IntegerContext[j])
1459 {
1460 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1461 testnum, i, reg_names[j], *(&context.Rax + j) );
1462 if (k < nb_regs)
1463 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1464 "%u/%u: register %s wrong %p/%x\n",
1465 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1466 }
1467 else
1468 {
1469 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1470 if (j == rbp)
1471 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1472 testnum, i, (void *)context.Rbp );
1473 else
1474 ok( *(&context.Rax + j) == unset_reg,
1475 "%u/%u: register %s wrong %p/unset\n",
1476 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1477 }
1478 }
1479 }
1480 }
1481
1482 static void test_virtual_unwind(void)
1483 {
1484 static const BYTE function_0[] =
1485 {
1486 0xff, 0xf5, /* 00: push %rbp */
1487 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1488 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1489 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1490 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1491 0x90, /* 1c: nop */
1492 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1493 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1494 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1495 0x5d, /* 32: pop %rbp */
1496 0xc3 /* 33: ret */
1497 };
1498
1499 static const BYTE unwind_info_0[] =
1500 {
1501 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1502 0x1c, /* prolog size */
1503 8, /* opcode count */
1504 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1505
1506 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1507 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1508 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1509 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1510 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1511
1512 0x00, 0x02, 0x00, 0x00, /* handler */
1513 0x05, 0x06, 0x07, 0x08, /* data */
1514 };
1515
1516 static const struct results results_0[] =
1517 {
1518 /* offset rbp handler rip frame registers */
1519 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1520 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1521 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1522 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1523 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1524 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1525 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1526 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1527 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1528 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1529 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1530 };
1531
1532
1533 static const BYTE function_1[] =
1534 {
1535 0x53, /* 00: push %rbx */
1536 0x55, /* 01: push %rbp */
1537 0x56, /* 02: push %rsi */
1538 0x57, /* 03: push %rdi */
1539 0x41, 0x54, /* 04: push %r12 */
1540 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1541 0x90, 0x90, /* 0a: nop; nop */
1542 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1543 0x41, 0x5c, /* 10: pop %r12 */
1544 0x5f, /* 12: pop %rdi */
1545 0x5e, /* 13: pop %rsi */
1546 0x5d, /* 14: pop %rbp */
1547 0x5b, /* 15: pop %rbx */
1548 0xc3 /* 16: ret */
1549 };
1550
1551 static const BYTE unwind_info_1[] =
1552 {
1553 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1554 0x0a, /* prolog size */
1555 6, /* opcode count */
1556 0, /* frame reg */
1557
1558 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1559 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1560 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1561 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1562 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1563 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1564
1565 0x00, 0x02, 0x00, 0x00, /* handler */
1566 0x05, 0x06, 0x07, 0x08, /* data */
1567 };
1568
1569 static const struct results results_1[] =
1570 {
1571 /* offset rbp handler rip frame registers */
1572 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1573 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1574 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1575 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1576 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1577 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1578 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1579 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1580 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1581 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1582 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1583 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1584 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1585 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1586 };
1587
1588 static const struct unwind_test tests[] =
1589 {
1590 { function_0, sizeof(function_0), unwind_info_0,
1591 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1592 { function_1, sizeof(function_1), unwind_info_1,
1593 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1594 };
1595 unsigned int i;
1596
1597 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1598 call_virtual_unwind( i, &tests[i] );
1599 }
1600
1601 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1602 {
1603 static const int code_offset = 1024;
1604 static RUNTIME_FUNCTION runtime_func;
1605 (*(DWORD *)context)++;
1606
1607 runtime_func.BeginAddress = code_offset + 16;
1608 runtime_func.EndAddress = code_offset + 32;
1609 runtime_func.UnwindData = 0;
1610 return &runtime_func;
1611 }
1612
1613 static void test_dynamic_unwind(void)
1614 {
1615 static const int code_offset = 1024;
1616 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1617 RUNTIME_FUNCTION *runtime_func, *func;
1618 ULONG_PTR table, base;
1619 DWORD count;
1620
1621 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1622 runtime_func = (RUNTIME_FUNCTION *)buf;
1623 runtime_func->BeginAddress = code_offset;
1624 runtime_func->EndAddress = code_offset + 16;
1625 runtime_func->UnwindData = 0;
1626 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1627 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1628
1629 /* Lookup function outside of any function table */
1630 base = 0xdeadbeef;
1631 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1632 ok( func == NULL,
1633 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1634 ok( !base || broken(base == 0xdeadbeef),
1635 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1636
1637 /* Test with pointer inside of our function */
1638 base = 0xdeadbeef;
1639 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1640 ok( func == runtime_func,
1641 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1642 ok( base == (ULONG_PTR)code_mem,
1643 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1644
1645 /* Test RtlDeleteFunctionTable */
1646 ok( pRtlDeleteFunctionTable( runtime_func ),
1647 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1648 ok( !pRtlDeleteFunctionTable( runtime_func ),
1649 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1650
1651 /* Unaligned RUNTIME_FUNCTION pointer */
1652 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1653 runtime_func->BeginAddress = code_offset;
1654 runtime_func->EndAddress = code_offset + 16;
1655 runtime_func->UnwindData = 0;
1656 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1657 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1658 ok( pRtlDeleteFunctionTable( runtime_func ),
1659 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1660
1661 /* Attempt to insert the same entry twice */
1662 runtime_func = (RUNTIME_FUNCTION *)buf;
1663 runtime_func->BeginAddress = code_offset;
1664 runtime_func->EndAddress = code_offset + 16;
1665 runtime_func->UnwindData = 0;
1666 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1667 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1668 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1669 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1670 ok( pRtlDeleteFunctionTable( runtime_func ),
1671 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1672 ok( pRtlDeleteFunctionTable( runtime_func ),
1673 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1674 ok( !pRtlDeleteFunctionTable( runtime_func ),
1675 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1676
1677 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1678 table = (ULONG_PTR)code_mem;
1679 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1680 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1681
1682 /* Test RtlInstallFunctionTableCallback with both low bits set */
1683 table = (ULONG_PTR)code_mem | 0x3;
1684 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1685 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1686
1687 /* Lookup function outside of any function table */
1688 count = 0;
1689 base = 0xdeadbeef;
1690 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1691 ok( func == NULL,
1692 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1693 ok( !base || broken(base == 0xdeadbeef),
1694 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1695 ok( !count,
1696 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1697
1698 /* Test with pointer inside of our function */
1699 count = 0;
1700 base = 0xdeadbeef;
1701 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1702 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1703 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1704 ok( base == (ULONG_PTR)code_mem,
1705 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1706 ok( count == 1,
1707 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1708
1709 /* Clean up again */
1710 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1711 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1712 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1713 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1714
1715 }
1716
1717 #endif /* __x86_64__ */
1718
1719 #if defined(__i386__) || defined(__x86_64__)
1720 static DWORD outputdebugstring_exceptions;
1721
1722 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1723 {
1724 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1725 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1726
1727 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
1728 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
1729 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1730 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
1731 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
1732 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
1733
1734 outputdebugstring_exceptions++;
1735 return EXCEPTION_CONTINUE_SEARCH;
1736 }
1737
1738 static void test_outputdebugstring(DWORD numexc)
1739 {
1740 PVOID vectored_handler;
1741
1742 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
1743 {
1744 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1745 return;
1746 }
1747
1748 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
1749 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1750
1751 outputdebugstring_exceptions = 0;
1752 OutputDebugStringA("Hello World");
1753 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1754 outputdebugstring_exceptions, numexc);
1755
1756 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1757 }
1758
1759 static DWORD ripevent_exceptions;
1760
1761 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1762 {
1763 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1764 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1765
1766 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
1767 rec->ExceptionCode, DBG_RIPEXCEPTION);
1768 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1769 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
1770 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
1771 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
1772 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
1773
1774 ripevent_exceptions++;
1775 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1776 }
1777
1778 static void test_ripevent(DWORD numexc)
1779 {
1780 EXCEPTION_RECORD record;
1781 PVOID vectored_handler;
1782
1783 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1784 {
1785 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1786 return;
1787 }
1788
1789 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
1790 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1791
1792 record.ExceptionCode = DBG_RIPEXCEPTION;
1793 record.ExceptionFlags = 0;
1794 record.ExceptionRecord = NULL;
1795 record.ExceptionAddress = NULL;
1796 record.NumberParameters = 2;
1797 record.ExceptionInformation[0] = 0x11223344;
1798 record.ExceptionInformation[1] = 0x55667788;
1799
1800 ripevent_exceptions = 0;
1801 pRtlRaiseException(&record);
1802 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
1803 ripevent_exceptions, numexc);
1804
1805 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1806 }
1807
1808 static DWORD invalid_handle_exceptions;
1809
1810 static LONG CALLBACK invalid_handle_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1811 {
1812 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1813 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1814
1815 ok(rec->ExceptionCode == EXCEPTION_INVALID_HANDLE, "ExceptionCode is %08x instead of %08x\n",
1816 rec->ExceptionCode, EXCEPTION_INVALID_HANDLE);
1817 ok(rec->NumberParameters == 0, "ExceptionParameters is %d instead of 0\n", rec->NumberParameters);
1818
1819 invalid_handle_exceptions++;
1820 return (rec->ExceptionCode == EXCEPTION_INVALID_HANDLE) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1821 }
1822
1823 static void test_closehandle(DWORD numexc)
1824 {
1825 PVOID vectored_handler;
1826 NTSTATUS status;
1827 DWORD res;
1828
1829 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1830 {
1831 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1832 return;
1833 }
1834
1835 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &invalid_handle_vectored_handler);
1836 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1837
1838 invalid_handle_exceptions = 0;
1839 res = CloseHandle((HANDLE)0xdeadbeef);
1840 ok(!res, "CloseHandle(0xdeadbeef) unexpectedly succeeded\n");
1841 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error code %d instead of %d\n",
1842 GetLastError(), ERROR_INVALID_HANDLE);
1843 ok(invalid_handle_exceptions == numexc, "CloseHandle generated %d exceptions, expected %d\n",
1844 invalid_handle_exceptions, numexc);
1845
1846 invalid_handle_exceptions = 0;
1847 status = pNtClose((HANDLE)0xdeadbeef);
1848 ok(status == STATUS_INVALID_HANDLE, "NtClose(0xdeadbeef) returned status %08x\n", status);
1849 ok(invalid_handle_exceptions == numexc, "NtClose generated %d exceptions, expected %d\n",
1850 invalid_handle_exceptions, numexc);
1851
1852 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1853 }
1854
1855 static void test_vectored_continue_handler(void)
1856 {
1857 PVOID handler1, handler2;
1858 ULONG ret;
1859
1860 if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
1861 {
1862 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
1863 return;
1864 }
1865
1866 handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1867 ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
1868
1869 handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1870 ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
1871 ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
1872
1873 if (pRtlRemoveVectoredExceptionHandler)
1874 {
1875 ret = pRtlRemoveVectoredExceptionHandler(handler1);
1876 ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
1877 }
1878
1879 ret = pRtlRemoveVectoredContinueHandler(handler1);
1880 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1881
1882 ret = pRtlRemoveVectoredContinueHandler(handler2);
1883 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1884
1885 ret = pRtlRemoveVectoredContinueHandler(handler1);
1886 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1887
1888 ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
1889 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1890 }
1891 #endif /* defined(__i386__) || defined(__x86_64__) */
1892
1893 START_TEST(exception)
1894 {
1895 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1896
1897 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1898 if(!code_mem) {
1899 trace("VirtualAlloc failed\n");
1900 return;
1901 }
1902
1903 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1904 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1905 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1906 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1907 pNtClose = (void *)GetProcAddress( hntdll, "NtClose" );
1908 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
1909 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1910 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1911 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1912 "RtlAddVectoredExceptionHandler" );
1913 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1914 "RtlRemoveVectoredExceptionHandler" );
1915 pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1916 "RtlAddVectoredContinueHandler" );
1917 pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1918 "RtlRemoveVectoredContinueHandler" );
1919 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1920 "NtQueryInformationProcess" );
1921 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1922 "NtSetInformationProcess" );
1923 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
1924
1925 #ifdef __i386__
1926 if (!pNtCurrentTeb)
1927 {
1928 skip( "NtCurrentTeb not found\n" );
1929 return;
1930 }
1931 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1932
1933 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1934 have_vectored_api = TRUE;
1935 else
1936 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1937
1938 my_argc = winetest_get_mainargs( &my_argv );
1939 if (my_argc >= 4)
1940 {
1941 void *addr;
1942 sscanf( my_argv[3], "%p", &addr );
1943
1944 if (addr != &test_stage)
1945 {
1946 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1947 return;
1948 }
1949
1950 /* child must be run under a debugger */
1951 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1952 {
1953 ok(FALSE, "child process not being debugged?\n");
1954 return;
1955 }
1956
1957 if (pRtlRaiseException)
1958 {
1959 test_stage = 1;
1960 run_rtlraiseexception_test(0x12345);
1961 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1962 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1963 test_stage = 2;
1964 run_rtlraiseexception_test(0x12345);
1965 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1966 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1967 test_stage = 3;
1968 test_outputdebugstring(0);
1969 test_stage = 4;
1970 test_outputdebugstring(2);
1971 test_stage = 5;
1972 test_ripevent(0);
1973 test_stage = 6;
1974 test_ripevent(1);
1975 test_stage = 7;
1976 test_closehandle(0);
1977 test_stage = 8;
1978 test_closehandle(1);
1979 }
1980 else
1981 skip( "RtlRaiseException not found\n" );
1982
1983 /* rest of tests only run in parent */
1984 return;
1985 }
1986
1987 test_unwind();
1988 test_exceptions();
1989 test_rtlraiseexception();
1990 test_outputdebugstring(1);
1991 test_ripevent(1);
1992 test_closehandle(0);
1993 test_vectored_continue_handler();
1994 test_debugger();
1995 test_simd_exceptions();
1996 test_fpu_exceptions();
1997 test_dpe_exceptions();
1998 test_prot_fault();
1999
2000 #elif defined(__x86_64__)
2001 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
2002 "RtlAddFunctionTable" );
2003 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
2004 "RtlDeleteFunctionTable" );
2005 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
2006 "RtlInstallFunctionTableCallback" );
2007 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
2008 "RtlLookupFunctionEntry" );
2009
2010 test_outputdebugstring(1);
2011 test_ripevent(1);
2012 test_closehandle(0);
2013 test_vectored_continue_handler();
2014 test_virtual_unwind();
2015
2016 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
2017 test_dynamic_unwind();
2018 else
2019 skip( "Dynamic unwind functions not found\n" );
2020
2021 #endif
2022
2023 VirtualFree(code_mem, 0, MEM_FREE);
2024 }