[NTDLL_WINETEST] Sync with Wine Staging 1.7.37. CORE-9246
[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 "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 char buffer[64];
963
964 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
965 sizeof(stage), &size_read);
966 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
967
968 ok(!de.u.DebugString.fUnicode, "unepxected unicode debug string event\n");
969 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
970 de.u.DebugString.nDebugStringLength);
971
972 memset(buffer, 0, sizeof(buffer));
973 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
974 de.u.DebugString.nDebugStringLength, &size_read);
975 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
976
977 if (stage == 3 || stage == 4)
978 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
979 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
980 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
981
982 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
983 }
984 else if (de.dwDebugEventCode == RIP_EVENT)
985 {
986 int stage;
987
988 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
989 sizeof(stage), &size_read);
990 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
991
992 if (stage == 5 || stage == 6)
993 {
994 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
995 de.u.RipInfo.dwError, 0x11223344);
996 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
997 de.u.RipInfo.dwType, 0x55667788);
998 }
999 else
1000 ok(FALSE, "unexpected stage %x\n", stage);
1001
1002 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1003 }
1004
1005 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
1006
1007 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1008
1009 winetest_wait_child_process( pi.hProcess );
1010 ret = CloseHandle(pi.hThread);
1011 ok(ret, "error %u\n", GetLastError());
1012 ret = CloseHandle(pi.hProcess);
1013 ok(ret, "error %u\n", GetLastError());
1014
1015 return;
1016 }
1017
1018 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1019 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1020 {
1021 int *stage = *(int **)(frame + 1);
1022
1023 got_exception++;
1024
1025 if( *stage == 1) {
1026 /* fault while executing sse instruction */
1027 context->Eip += 3; /* skip addps */
1028 return ExceptionContinueExecution;
1029 }
1030
1031 /* stage 2 - divide by zero fault */
1032 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1033 skip("system doesn't support SIMD exceptions\n");
1034 else {
1035 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1036 "exception code: %#x, should be %#x\n",
1037 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1038 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1039 "# of params: %i, should be 1\n",
1040 rec->NumberParameters);
1041 if( rec->NumberParameters == 1 )
1042 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1043 }
1044
1045 context->Eip += 3; /* skip divps */
1046
1047 return ExceptionContinueExecution;
1048 }
1049
1050 static const BYTE simd_exception_test[] = {
1051 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1052 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1053 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1054 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1055 0x6a, 0x01, /* push $0x1 */
1056 0x6a, 0x01, /* push $0x1 */
1057 0x6a, 0x01, /* push $0x1 */
1058 0x6a, 0x01, /* push $0x1 */
1059 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1060 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1061 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1062 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1063 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
1064 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1065 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1066 0xc3, /* ret */
1067 };
1068
1069 static const BYTE sse_check[] = {
1070 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1071 0xc3, /* ret */
1072 };
1073
1074 static void test_simd_exceptions(void)
1075 {
1076 int stage;
1077
1078 /* test if CPU & OS can do sse */
1079 stage = 1;
1080 got_exception = 0;
1081 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1082 if(got_exception) {
1083 skip("system doesn't support SSE\n");
1084 return;
1085 }
1086
1087 /* generate a SIMD exception */
1088 stage = 2;
1089 got_exception = 0;
1090 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1091 sizeof(simd_exception_test), 0);
1092 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1093 }
1094
1095 struct fpu_exception_info
1096 {
1097 DWORD exception_code;
1098 DWORD exception_offset;
1099 DWORD eip_offset;
1100 };
1101
1102 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1103 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1104 {
1105 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1106
1107 info->exception_code = rec->ExceptionCode;
1108 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1109 info->eip_offset = context->Eip - (DWORD)code_mem;
1110
1111 ++context->Eip;
1112 return ExceptionContinueExecution;
1113 }
1114
1115 static void test_fpu_exceptions(void)
1116 {
1117 static const BYTE fpu_exception_test_ie[] =
1118 {
1119 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1120 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1121 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1122 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1123 0xd9, 0xee, /* fldz */
1124 0xd9, 0xe8, /* fld1 */
1125 0xde, 0xf1, /* fdivp */
1126 0xdd, 0xd8, /* fstp %st(0) */
1127 0xdd, 0xd8, /* fstp %st(0) */
1128 0x9b, /* fwait */
1129 0xdb, 0xe2, /* fnclex */
1130 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1131 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1132 0xc3, /* ret */
1133 };
1134
1135 static const BYTE fpu_exception_test_de[] =
1136 {
1137 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1138 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1139 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1140 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1141 0xdd, 0xd8, /* fstp %st(0) */
1142 0xd9, 0xee, /* fldz */
1143 0xd9, 0xe8, /* fld1 */
1144 0xde, 0xf1, /* fdivp */
1145 0x9b, /* fwait */
1146 0xdb, 0xe2, /* fnclex */
1147 0xdd, 0xd8, /* fstp %st(0) */
1148 0xdd, 0xd8, /* fstp %st(0) */
1149 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1150 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1151 0xc3, /* ret */
1152 };
1153
1154 struct fpu_exception_info info;
1155
1156 memset(&info, 0, sizeof(info));
1157 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1158 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1159 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1160 ok(info.exception_offset == 0x19 ||
1161 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1162 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1163 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1164
1165 memset(&info, 0, sizeof(info));
1166 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1167 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1168 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1169 ok(info.exception_offset == 0x17 ||
1170 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1171 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1172 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1173 }
1174
1175 struct dpe_exception_info {
1176 BOOL exception_caught;
1177 DWORD exception_info;
1178 };
1179
1180 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1181 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1182 {
1183 DWORD old_prot;
1184 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1185
1186 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1187 "Exception code %08x\n", rec->ExceptionCode);
1188 ok(rec->NumberParameters == 2,
1189 "Parameter count: %d\n", rec->NumberParameters);
1190 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1191 "Exception address: %p, expected %p\n",
1192 (LPVOID)rec->ExceptionInformation[1], code_mem);
1193
1194 info->exception_info = rec->ExceptionInformation[0];
1195 info->exception_caught = TRUE;
1196
1197 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1198 return ExceptionContinueExecution;
1199 }
1200
1201 static void test_dpe_exceptions(void)
1202 {
1203 static const BYTE single_ret[] = {0xC3};
1204 struct dpe_exception_info info;
1205 NTSTATUS stat;
1206 BOOL has_hw_support;
1207 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1208 DWORD val;
1209 ULONG len;
1210
1211 /* Query DEP with len too small */
1212 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1213 if(stat == STATUS_INVALID_INFO_CLASS)
1214 {
1215 skip("This software platform does not support DEP\n");
1216 return;
1217 }
1218 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1219
1220 /* Query DEP */
1221 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1222 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1223 if(stat == STATUS_SUCCESS)
1224 {
1225 ok(len == sizeof val, "returned length: %d\n", len);
1226 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1227 {
1228 skip("toggling DEP impossible - status locked\n");
1229 is_permanent = TRUE;
1230 if(val & MEM_EXECUTE_OPTION_DISABLE)
1231 can_test_without = FALSE;
1232 else
1233 can_test_with = FALSE;
1234 }
1235 }
1236
1237 if(!is_permanent)
1238 {
1239 /* Enable DEP */
1240 val = MEM_EXECUTE_OPTION_DISABLE;
1241 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1242 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1243 }
1244
1245 if(can_test_with)
1246 {
1247 /* Try access to locked page with DEP on*/
1248 info.exception_caught = FALSE;
1249 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1250 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1251 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1252 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1253 "Access violation type: %08x\n", (unsigned)info.exception_info);
1254 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1255 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1256
1257 /* Try execution of data with DEP on*/
1258 info.exception_caught = FALSE;
1259 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1260 if(has_hw_support)
1261 {
1262 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1263 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1264 "Access violation type: %08x\n", (unsigned)info.exception_info);
1265 }
1266 else
1267 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1268 }
1269 else
1270 skip("DEP is in AlwaysOff state\n");
1271
1272 if(!is_permanent)
1273 {
1274 /* Disable DEP */
1275 val = MEM_EXECUTE_OPTION_ENABLE;
1276 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1277 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1278 }
1279
1280 /* page is read without exec here */
1281 if(can_test_without)
1282 {
1283 /* Try execution of data with DEP off */
1284 info.exception_caught = FALSE;
1285 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1286 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1287
1288 /* Try access to locked page with DEP off - error code is different than
1289 with hardware DEP on */
1290 info.exception_caught = FALSE;
1291 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1292 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1293 ok(info.exception_info == EXCEPTION_READ_FAULT,
1294 "Access violation type: %08x\n", (unsigned)info.exception_info);
1295 }
1296 else
1297 skip("DEP is in AlwaysOn state\n");
1298
1299 if(!is_permanent)
1300 {
1301 /* Turn off DEP permanently */
1302 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1303 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1304 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1305 }
1306
1307 /* Try to turn off DEP */
1308 val = MEM_EXECUTE_OPTION_ENABLE;
1309 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1310 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1311
1312 /* Try to turn on DEP */
1313 val = MEM_EXECUTE_OPTION_DISABLE;
1314 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1315 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1316 }
1317
1318 #elif defined(__x86_64__)
1319
1320 #define UNW_FLAG_NHANDLER 0
1321 #define UNW_FLAG_EHANDLER 1
1322 #define UNW_FLAG_UHANDLER 2
1323 #define UNW_FLAG_CHAININFO 4
1324
1325 #define UWOP_PUSH_NONVOL 0
1326 #define UWOP_ALLOC_LARGE 1
1327 #define UWOP_ALLOC_SMALL 2
1328 #define UWOP_SET_FPREG 3
1329 #define UWOP_SAVE_NONVOL 4
1330 #define UWOP_SAVE_NONVOL_FAR 5
1331 #define UWOP_SAVE_XMM128 8
1332 #define UWOP_SAVE_XMM128_FAR 9
1333 #define UWOP_PUSH_MACHFRAME 10
1334
1335 struct results
1336 {
1337 int rip_offset; /* rip offset from code start */
1338 int rbp_offset; /* rbp offset from stack pointer */
1339 int handler; /* expect handler to be set? */
1340 int rip; /* expected final rip value */
1341 int frame; /* expected frame return value */
1342 int regs[8][2]; /* expected values for registers */
1343 };
1344
1345 struct unwind_test
1346 {
1347 const BYTE *function;
1348 size_t function_size;
1349 const BYTE *unwind_info;
1350 const struct results *results;
1351 unsigned int nb_results;
1352 };
1353
1354 enum regs
1355 {
1356 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1357 r8, r9, r10, r11, r12, r13, r14, r15
1358 };
1359
1360 static const char * const reg_names[16] =
1361 {
1362 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1363 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1364 };
1365
1366 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1367
1368 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1369 {
1370 static const int code_offset = 1024;
1371 static const int unwind_offset = 2048;
1372 void *handler, *data;
1373 CONTEXT context;
1374 RUNTIME_FUNCTION runtime_func;
1375 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1376 UINT i, j, k;
1377 ULONG64 fake_stack[256];
1378 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1379 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1380
1381 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1382 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1383
1384 runtime_func.BeginAddress = code_offset;
1385 runtime_func.EndAddress = code_offset + test->function_size;
1386 runtime_func.UnwindData = unwind_offset;
1387
1388 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1389
1390 for (i = 0; i < test->nb_results; i++)
1391 {
1392 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1393 memset( &context, 0x55, sizeof(context) );
1394 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1395 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1396
1397 context.Rsp = (ULONG_PTR)fake_stack;
1398 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1399 orig_rbp = context.Rbp;
1400 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1401
1402 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1403 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1404
1405 data = (void *)0xdeadbeef;
1406 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1407 &runtime_func, &context, &data, &frame, &ctx_ptr );
1408 if (test->results[i].handler)
1409 {
1410 ok( (char *)handler == (char *)code_mem + 0x200,
1411 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1412 if (handler) ok( *(DWORD *)data == 0x08070605,
1413 "%u/%u: wrong handler data %p\n", testnum, i, data );
1414 }
1415 else
1416 {
1417 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1418 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1419 }
1420
1421 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1422 testnum, i, (void *)context.Rip, test->results[i].rip );
1423 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1424 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1425
1426 for (j = 0; j < 16; j++)
1427 {
1428 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1429
1430 for (k = 0; k < nb_regs; k++)
1431 {
1432 if (test->results[i].regs[k][0] == -1)
1433 {
1434 k = nb_regs;
1435 break;
1436 }
1437 if (test->results[i].regs[k][0] == j) break;
1438 }
1439
1440 if (j == rsp) /* rsp is special */
1441 {
1442 ok( !ctx_ptr.u2.IntegerContext[j],
1443 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1444 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1445 "%u/%u: register rsp wrong %p/%p\n",
1446 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1447 continue;
1448 }
1449
1450 if (ctx_ptr.u2.IntegerContext[j])
1451 {
1452 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1453 testnum, i, reg_names[j], *(&context.Rax + j) );
1454 if (k < nb_regs)
1455 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1456 "%u/%u: register %s wrong %p/%x\n",
1457 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1458 }
1459 else
1460 {
1461 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1462 if (j == rbp)
1463 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1464 testnum, i, (void *)context.Rbp );
1465 else
1466 ok( *(&context.Rax + j) == unset_reg,
1467 "%u/%u: register %s wrong %p/unset\n",
1468 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1469 }
1470 }
1471 }
1472 }
1473
1474 static void test_virtual_unwind(void)
1475 {
1476 static const BYTE function_0[] =
1477 {
1478 0xff, 0xf5, /* 00: push %rbp */
1479 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1480 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1481 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1482 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1483 0x90, /* 1c: nop */
1484 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1485 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1486 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1487 0x5d, /* 32: pop %rbp */
1488 0xc3 /* 33: ret */
1489 };
1490
1491 static const BYTE unwind_info_0[] =
1492 {
1493 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1494 0x1c, /* prolog size */
1495 8, /* opcode count */
1496 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1497
1498 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1499 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1500 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1501 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1502 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1503
1504 0x00, 0x02, 0x00, 0x00, /* handler */
1505 0x05, 0x06, 0x07, 0x08, /* data */
1506 };
1507
1508 static const struct results results_0[] =
1509 {
1510 /* offset rbp handler rip frame registers */
1511 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1512 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1513 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1514 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1515 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1516 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1517 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1518 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1519 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1520 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1521 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1522 };
1523
1524
1525 static const BYTE function_1[] =
1526 {
1527 0x53, /* 00: push %rbx */
1528 0x55, /* 01: push %rbp */
1529 0x56, /* 02: push %rsi */
1530 0x57, /* 03: push %rdi */
1531 0x41, 0x54, /* 04: push %r12 */
1532 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1533 0x90, 0x90, /* 0a: nop; nop */
1534 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1535 0x41, 0x5c, /* 10: pop %r12 */
1536 0x5f, /* 12: pop %rdi */
1537 0x5e, /* 13: pop %rsi */
1538 0x5d, /* 14: pop %rbp */
1539 0x5b, /* 15: pop %rbx */
1540 0xc3 /* 16: ret */
1541 };
1542
1543 static const BYTE unwind_info_1[] =
1544 {
1545 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1546 0x0a, /* prolog size */
1547 6, /* opcode count */
1548 0, /* frame reg */
1549
1550 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1551 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1552 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1553 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1554 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1555 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1556
1557 0x00, 0x02, 0x00, 0x00, /* handler */
1558 0x05, 0x06, 0x07, 0x08, /* data */
1559 };
1560
1561 static const struct results results_1[] =
1562 {
1563 /* offset rbp handler rip frame registers */
1564 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1565 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1566 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1567 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1568 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1569 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1570 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1571 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1572 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1573 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1574 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1575 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1576 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1577 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1578 };
1579
1580 static const struct unwind_test tests[] =
1581 {
1582 { function_0, sizeof(function_0), unwind_info_0,
1583 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1584 { function_1, sizeof(function_1), unwind_info_1,
1585 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1586 };
1587 unsigned int i;
1588
1589 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1590 call_virtual_unwind( i, &tests[i] );
1591 }
1592
1593 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1594 {
1595 static const int code_offset = 1024;
1596 static RUNTIME_FUNCTION runtime_func;
1597 (*(DWORD *)context)++;
1598
1599 runtime_func.BeginAddress = code_offset + 16;
1600 runtime_func.EndAddress = code_offset + 32;
1601 runtime_func.UnwindData = 0;
1602 return &runtime_func;
1603 }
1604
1605 static void test_dynamic_unwind(void)
1606 {
1607 static const int code_offset = 1024;
1608 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1609 RUNTIME_FUNCTION *runtime_func, *func;
1610 ULONG_PTR table, base;
1611 DWORD count;
1612
1613 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1614 runtime_func = (RUNTIME_FUNCTION *)buf;
1615 runtime_func->BeginAddress = code_offset;
1616 runtime_func->EndAddress = code_offset + 16;
1617 runtime_func->UnwindData = 0;
1618 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1619 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1620
1621 /* Lookup function outside of any function table */
1622 base = 0xdeadbeef;
1623 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1624 ok( func == NULL,
1625 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1626 ok( !base || broken(base == 0xdeadbeef),
1627 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1628
1629 /* Test with pointer inside of our function */
1630 base = 0xdeadbeef;
1631 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1632 ok( func == runtime_func,
1633 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1634 ok( base == (ULONG_PTR)code_mem,
1635 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1636
1637 /* Test RtlDeleteFunctionTable */
1638 ok( pRtlDeleteFunctionTable( runtime_func ),
1639 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1640 ok( !pRtlDeleteFunctionTable( runtime_func ),
1641 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1642
1643 /* Unaligned RUNTIME_FUNCTION pointer */
1644 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1645 runtime_func->BeginAddress = code_offset;
1646 runtime_func->EndAddress = code_offset + 16;
1647 runtime_func->UnwindData = 0;
1648 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1649 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1650 ok( pRtlDeleteFunctionTable( runtime_func ),
1651 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1652
1653 /* Attempt to insert the same entry twice */
1654 runtime_func = (RUNTIME_FUNCTION *)buf;
1655 runtime_func->BeginAddress = code_offset;
1656 runtime_func->EndAddress = code_offset + 16;
1657 runtime_func->UnwindData = 0;
1658 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1659 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1660 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1661 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1662 ok( pRtlDeleteFunctionTable( runtime_func ),
1663 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1664 ok( pRtlDeleteFunctionTable( runtime_func ),
1665 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1666 ok( !pRtlDeleteFunctionTable( runtime_func ),
1667 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1668
1669 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1670 table = (ULONG_PTR)code_mem;
1671 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1672 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1673
1674 /* Test RtlInstallFunctionTableCallback with both low bits set */
1675 table = (ULONG_PTR)code_mem | 0x3;
1676 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1677 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1678
1679 /* Lookup function outside of any function table */
1680 count = 0;
1681 base = 0xdeadbeef;
1682 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1683 ok( func == NULL,
1684 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1685 ok( !base || broken(base == 0xdeadbeef),
1686 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1687 ok( !count,
1688 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1689
1690 /* Test with pointer inside of our function */
1691 count = 0;
1692 base = 0xdeadbeef;
1693 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1694 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1695 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1696 ok( base == (ULONG_PTR)code_mem,
1697 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1698 ok( count == 1,
1699 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1700
1701 /* Clean up again */
1702 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1703 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1704 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1705 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1706
1707 }
1708
1709 #endif /* __x86_64__ */
1710
1711 #if defined(__i386__) || defined(__x86_64__)
1712 static DWORD outputdebugstring_exceptions;
1713
1714 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1715 {
1716 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1717 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1718
1719 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
1720 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
1721 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1722 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
1723 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
1724 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
1725
1726 outputdebugstring_exceptions++;
1727 return EXCEPTION_CONTINUE_SEARCH;
1728 }
1729
1730 static void test_outputdebugstring(DWORD numexc)
1731 {
1732 PVOID vectored_handler;
1733
1734 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
1735 {
1736 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1737 return;
1738 }
1739
1740 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
1741 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1742
1743 outputdebugstring_exceptions = 0;
1744 OutputDebugStringA("Hello World");
1745 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1746 outputdebugstring_exceptions, numexc);
1747
1748 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1749 }
1750
1751 static DWORD ripevent_exceptions;
1752
1753 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1754 {
1755 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1756 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1757
1758 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
1759 rec->ExceptionCode, DBG_RIPEXCEPTION);
1760 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1761 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
1762 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
1763 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
1764 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
1765
1766 ripevent_exceptions++;
1767 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1768 }
1769
1770 static void test_ripevent(DWORD numexc)
1771 {
1772 EXCEPTION_RECORD record;
1773 PVOID vectored_handler;
1774
1775 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1776 {
1777 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1778 return;
1779 }
1780
1781 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
1782 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1783
1784 record.ExceptionCode = DBG_RIPEXCEPTION;
1785 record.ExceptionFlags = 0;
1786 record.ExceptionRecord = NULL;
1787 record.ExceptionAddress = NULL;
1788 record.NumberParameters = 2;
1789 record.ExceptionInformation[0] = 0x11223344;
1790 record.ExceptionInformation[1] = 0x55667788;
1791
1792 ripevent_exceptions = 0;
1793 pRtlRaiseException(&record);
1794 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
1795 ripevent_exceptions, numexc);
1796
1797 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1798 }
1799
1800 static DWORD invalid_handle_exceptions;
1801
1802 static LONG CALLBACK invalid_handle_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1803 {
1804 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1805 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1806
1807 ok(rec->ExceptionCode == EXCEPTION_INVALID_HANDLE, "ExceptionCode is %08x instead of %08x\n",
1808 rec->ExceptionCode, EXCEPTION_INVALID_HANDLE);
1809 ok(rec->NumberParameters == 0, "ExceptionParameters is %d instead of 0\n", rec->NumberParameters);
1810
1811 invalid_handle_exceptions++;
1812 return (rec->ExceptionCode == EXCEPTION_INVALID_HANDLE) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1813 }
1814
1815 static void test_closehandle(DWORD numexc)
1816 {
1817 PVOID vectored_handler;
1818 NTSTATUS status;
1819 DWORD res;
1820
1821 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1822 {
1823 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1824 return;
1825 }
1826
1827 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &invalid_handle_vectored_handler);
1828 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1829
1830 invalid_handle_exceptions = 0;
1831 res = CloseHandle((HANDLE)0xdeadbeef);
1832 ok(!res, "CloseHandle(0xdeadbeef) unexpectedly succeeded\n");
1833 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error code %d instead of %d",
1834 GetLastError(), ERROR_INVALID_HANDLE);
1835 ok(invalid_handle_exceptions == numexc, "CloseHandle generated %d exceptions, expected %d\n",
1836 invalid_handle_exceptions, numexc);
1837
1838 invalid_handle_exceptions = 0;
1839 status = pNtClose((HANDLE)0xdeadbeef);
1840 ok(status == STATUS_INVALID_HANDLE, "NtClose(0xdeadbeef) returned status %08x\n", status);
1841 ok(invalid_handle_exceptions == numexc, "NtClose generated %d exceptions, expected %d\n",
1842 invalid_handle_exceptions, numexc);
1843
1844 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1845 }
1846
1847 static void test_vectored_continue_handler(void)
1848 {
1849 PVOID handler1, handler2;
1850 ULONG ret;
1851
1852 if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
1853 {
1854 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
1855 return;
1856 }
1857
1858 handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1859 ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
1860
1861 handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1862 ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
1863 ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
1864
1865 if (pRtlRemoveVectoredExceptionHandler)
1866 {
1867 ret = pRtlRemoveVectoredExceptionHandler(handler1);
1868 ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
1869 }
1870
1871 ret = pRtlRemoveVectoredContinueHandler(handler1);
1872 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1873
1874 ret = pRtlRemoveVectoredContinueHandler(handler2);
1875 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1876
1877 ret = pRtlRemoveVectoredContinueHandler(handler1);
1878 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1879
1880 ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
1881 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1882 }
1883 #endif /* defined(__i386__) || defined(__x86_64__) */
1884
1885 START_TEST(exception)
1886 {
1887 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1888
1889 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1890 if(!code_mem) {
1891 trace("VirtualAlloc failed\n");
1892 return;
1893 }
1894
1895 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1896 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1897 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1898 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1899 pNtClose = (void *)GetProcAddress( hntdll, "NtClose" );
1900 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
1901 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1902 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1903 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1904 "RtlAddVectoredExceptionHandler" );
1905 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1906 "RtlRemoveVectoredExceptionHandler" );
1907 pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1908 "RtlAddVectoredContinueHandler" );
1909 pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1910 "RtlRemoveVectoredContinueHandler" );
1911 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1912 "NtQueryInformationProcess" );
1913 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1914 "NtSetInformationProcess" );
1915 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
1916
1917 #ifdef __i386__
1918 if (!pNtCurrentTeb)
1919 {
1920 skip( "NtCurrentTeb not found\n" );
1921 return;
1922 }
1923 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1924
1925 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1926 have_vectored_api = TRUE;
1927 else
1928 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1929
1930 my_argc = winetest_get_mainargs( &my_argv );
1931 if (my_argc >= 4)
1932 {
1933 void *addr;
1934 sscanf( my_argv[3], "%p", &addr );
1935
1936 if (addr != &test_stage)
1937 {
1938 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1939 return;
1940 }
1941
1942 /* child must be run under a debugger */
1943 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1944 {
1945 ok(FALSE, "child process not being debugged?\n");
1946 return;
1947 }
1948
1949 if (pRtlRaiseException)
1950 {
1951 test_stage = 1;
1952 run_rtlraiseexception_test(0x12345);
1953 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1954 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1955 test_stage = 2;
1956 run_rtlraiseexception_test(0x12345);
1957 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1958 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1959 test_stage = 3;
1960 test_outputdebugstring(0);
1961 test_stage = 4;
1962 test_outputdebugstring(2);
1963 test_stage = 5;
1964 test_ripevent(0);
1965 test_stage = 6;
1966 test_ripevent(1);
1967 test_stage = 7;
1968 test_closehandle(0);
1969 test_stage = 8;
1970 test_closehandle(1);
1971 }
1972 else
1973 skip( "RtlRaiseException not found\n" );
1974
1975 /* rest of tests only run in parent */
1976 return;
1977 }
1978
1979 test_unwind();
1980 test_exceptions();
1981 test_rtlraiseexception();
1982 test_outputdebugstring(1);
1983 test_ripevent(1);
1984 test_closehandle(0);
1985 test_vectored_continue_handler();
1986 test_debugger();
1987 test_simd_exceptions();
1988 test_fpu_exceptions();
1989 test_dpe_exceptions();
1990 test_prot_fault();
1991
1992 #elif defined(__x86_64__)
1993 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
1994 "RtlAddFunctionTable" );
1995 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
1996 "RtlDeleteFunctionTable" );
1997 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
1998 "RtlInstallFunctionTableCallback" );
1999 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
2000 "RtlLookupFunctionEntry" );
2001
2002 test_outputdebugstring(1);
2003 test_ripevent(1);
2004 test_closehandle(0);
2005 test_vectored_continue_handler();
2006 test_virtual_unwind();
2007
2008 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
2009 test_dynamic_unwind();
2010 else
2011 skip( "Dynamic unwind functions not found\n" );
2012
2013 #endif
2014
2015 VirtualFree(code_mem, 0, MEM_FREE);
2016 }