TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / ntdll / tests / exception.c
bloba368997bbe2f737ccb7ae05f175ccc3c37d7bf3c
1 /*
2 * Unit test suite for ntdll exceptions
4 * Copyright 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #ifndef _WIN32_WINNT
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
26 #endif
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnt.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "excpt.h"
37 #include "wine/test.h"
39 static void *code_mem;
41 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
42 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
43 static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
44 static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
45 static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
46 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
47 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
48 static PVOID (WINAPI *pRtlAddVectoredContinueHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
49 static ULONG (WINAPI *pRtlRemoveVectoredContinueHandler)(PVOID handler);
50 static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
51 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
52 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
53 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
54 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
56 #if defined(__x86_64__)
57 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
58 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
59 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
60 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
61 #endif
63 #ifdef __i386__
65 #ifndef __WINE_WINTRNL_H
66 #define ProcessExecuteFlags 0x22
67 #define MEM_EXECUTE_OPTION_DISABLE 0x01
68 #define MEM_EXECUTE_OPTION_ENABLE 0x02
69 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
70 #endif
72 static int my_argc;
73 static char** my_argv;
74 static int test_stage;
76 static BOOL is_wow64;
78 /* Test various instruction combinations that cause a protection fault on the i386,
79 * and check what the resulting exception looks like.
82 static const struct exception
84 BYTE code[18]; /* asm code */
85 BYTE offset; /* offset of faulting instruction */
86 BYTE length; /* length of faulting instruction */
87 BOOL wow64_broken; /* broken on Wow64, should be skipped */
88 NTSTATUS status; /* expected status code */
89 DWORD nb_params; /* expected number of parameters */
90 DWORD params[4]; /* expected parameters */
91 NTSTATUS alt_status; /* alternative status code */
92 DWORD alt_nb_params; /* alternative number of parameters */
93 DWORD alt_params[4]; /* alternative parameters */
94 } exceptions[] =
96 /* 0 */
97 /* test some privileged instructions */
98 { { 0xfb, 0xc3 }, /* 0: sti; ret */
99 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
100 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
101 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
102 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
103 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
104 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
105 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
106 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
107 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
108 /* 5 */
109 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
110 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
111 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
112 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
113 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
114 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
115 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
116 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
117 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
118 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
119 /* 10 */
120 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
121 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
122 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
123 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
124 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
125 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
126 { { 0xfa, 0xc3 }, /* 13: cli; ret */
127 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
129 /* test long jump to invalid selector */
130 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
131 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
133 /* 15 */
134 /* test iret to invalid selector */
135 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
136 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
137 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
139 /* test loading an invalid selector */
140 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
141 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
143 /* test accessing a zero selector (%es broken on Wow64) */
144 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
145 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
146 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
147 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
148 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
149 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
151 /* test moving %cs -> %ss */
152 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
153 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
155 /* 20 */
156 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
157 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
158 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
159 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
160 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
161 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
163 /* test invalid interrupt */
164 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
165 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
167 /* test moves to/from Crx */
168 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
169 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
170 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
171 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
172 /* 25 */
173 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
174 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
175 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
176 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
178 /* test moves to/from Drx */
179 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
180 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
181 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
182 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
183 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
184 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
185 /* 30 */
186 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
187 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
188 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
189 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
190 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
191 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
193 /* test memory reads */
194 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
195 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
196 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
197 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
198 /* 35 */
199 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
200 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
201 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
202 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
204 /* test memory writes */
205 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
206 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
207 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
208 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
209 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
210 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
211 /* 40 */
212 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
213 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
215 /* test exception with cleared %ds and %es (broken on Wow64) */
216 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
217 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
218 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
220 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
221 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
224 static int got_exception;
225 static BOOL have_vectored_api;
227 static void run_exception_test(void *handler, const void* context,
228 const void *code, unsigned int code_size,
229 DWORD access)
231 struct {
232 EXCEPTION_REGISTRATION_RECORD frame;
233 const void *context;
234 } exc_frame;
235 void (*func)(void) = code_mem;
236 DWORD oldaccess, oldaccess2;
238 exc_frame.frame.Handler = handler;
239 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
240 exc_frame.context = context;
242 memcpy(code_mem, code, code_size);
243 if(access)
244 VirtualProtect(code_mem, code_size, access, &oldaccess);
246 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
247 func();
248 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
250 if(access)
251 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
254 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
256 PCONTEXT context = ExceptionInfo->ContextRecord;
257 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
258 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
259 rec->ExceptionAddress, context->Eip);
261 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
262 rec->ExceptionAddress, (char *)code_mem + 0xb);
264 if (pNtCurrentTeb()->Peb->BeingDebugged)
265 ok((void *)context->Eax == pRtlRaiseException ||
266 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
267 "debugger managed to modify Eax to %x should be %p\n",
268 context->Eax, pRtlRaiseException);
270 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
271 * even if raised by RtlRaiseException
273 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
275 ok(context->Eip == (DWORD)code_mem + 0xa ||
276 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
277 "Eip at %x instead of %x or %x\n", context->Eip,
278 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
280 else
282 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
283 context->Eip, (DWORD)code_mem + 0xb);
286 /* test if context change is preserved from vectored handler to stack handlers */
287 context->Eax = 0xf00f00f0;
288 return EXCEPTION_CONTINUE_SEARCH;
291 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
292 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
294 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
295 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
297 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
298 rec->ExceptionAddress, (char *)code_mem + 0xb);
300 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
301 * even if raised by RtlRaiseException
303 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
305 ok(context->Eip == (DWORD)code_mem + 0xa ||
306 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
307 "Eip at %x instead of %x or %x\n", context->Eip,
308 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
310 else
312 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
313 context->Eip, (DWORD)code_mem + 0xb);
316 if(have_vectored_api)
317 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
318 context->Eax);
320 /* give the debugger a chance to examine the state a second time */
321 /* without the exception handler changing Eip */
322 if (test_stage == 2)
323 return ExceptionContinueSearch;
325 /* Eip in context is decreased by 1
326 * Increase it again, else execution will continue in the middle of an instruction */
327 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
328 context->Eip += 1;
329 return ExceptionContinueExecution;
333 static const BYTE call_one_arg_code[] = {
334 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
335 0x50, /* push %eax */
336 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
337 0xff, 0xd0, /* call *%eax */
338 0x90, /* nop */
339 0x90, /* nop */
340 0x90, /* nop */
341 0x90, /* nop */
342 0xc3, /* ret */
346 static void run_rtlraiseexception_test(DWORD exceptioncode)
348 EXCEPTION_REGISTRATION_RECORD frame;
349 EXCEPTION_RECORD record;
350 PVOID vectored_handler = NULL;
352 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
354 record.ExceptionCode = exceptioncode;
355 record.ExceptionFlags = 0;
356 record.ExceptionRecord = NULL;
357 record.ExceptionAddress = NULL; /* does not matter, copied return address */
358 record.NumberParameters = 0;
360 frame.Handler = rtlraiseexception_handler;
361 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
363 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
365 pNtCurrentTeb()->Tib.ExceptionList = &frame;
366 if (have_vectored_api)
368 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
369 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
372 func(pRtlRaiseException, &record);
373 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
374 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
376 if (have_vectored_api)
377 pRtlRemoveVectoredExceptionHandler(vectored_handler);
378 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
381 static void test_rtlraiseexception(void)
383 if (!pRtlRaiseException)
385 skip("RtlRaiseException not found\n");
386 return;
389 /* test without debugger */
390 run_rtlraiseexception_test(0x12345);
391 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
392 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
395 static DWORD unwind_expected_eax;
397 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
398 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
400 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
401 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
403 ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
404 rec->ExceptionCode, STATUS_UNWIND);
405 ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
406 rec->ExceptionAddress, (char *)code_mem + 0x22);
407 ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
408 context->Eax, unwind_expected_eax);
410 context->Eax += 1;
411 return ExceptionContinueSearch;
414 static const BYTE call_unwind_code[] = {
415 0x55, /* push %ebp */
416 0x53, /* push %ebx */
417 0x56, /* push %esi */
418 0x57, /* push %edi */
419 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
420 0x58, /* 0: pop %eax */
421 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
422 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
423 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
424 0x50, /* push %eax */
425 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
426 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
427 0xff, 0xd0, /* call *%eax */
428 0x5f, /* pop %edi */
429 0x5e, /* pop %esi */
430 0x5b, /* pop %ebx */
431 0x5d, /* pop %ebp */
432 0xc3, /* ret */
433 0xcc, /* int $3 */
436 static void test_unwind(void)
438 EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
439 DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
440 DWORD retval;
442 memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
444 /* add first unwind handler */
445 frame1->Handler = unwind_handler;
446 frame1->Prev = pNtCurrentTeb()->Tib.ExceptionList;
447 pNtCurrentTeb()->Tib.ExceptionList = frame1;
449 /* add second unwind handler */
450 frame2->Handler = unwind_handler;
451 frame2->Prev = pNtCurrentTeb()->Tib.ExceptionList;
452 pNtCurrentTeb()->Tib.ExceptionList = frame2;
454 /* test unwind to current frame */
455 unwind_expected_eax = 0xDEAD0000;
456 retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
457 ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
458 ok(pNtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
459 pNtCurrentTeb()->Tib.ExceptionList, frame2);
461 /* unwind to frame1 */
462 unwind_expected_eax = 0xDEAD0000;
463 retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
464 ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
465 ok(pNtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
466 pNtCurrentTeb()->Tib.ExceptionList, frame1);
468 /* restore original handler */
469 pNtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
472 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
473 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
475 const struct exception *except = *(const struct exception **)(frame + 1);
476 unsigned int i, entry = except - exceptions;
478 got_exception++;
479 trace( "exception %u: %x flags:%x addr:%p\n",
480 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
482 ok( rec->ExceptionCode == except->status ||
483 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
484 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
485 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
486 "%u: Wrong exception address %p/%p\n", entry,
487 rec->ExceptionAddress, (char*)code_mem + except->offset );
489 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
491 ok( rec->NumberParameters == except->nb_params,
492 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
494 else
496 ok( rec->NumberParameters == except->alt_nb_params,
497 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
500 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
501 /* instead of page faults for accesses beyond 0xffffffff */
502 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
504 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
505 goto skip_params;
508 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
509 if (except->nb_params == 2 && rec->NumberParameters == 2
510 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
511 && except->params[0] == rec->ExceptionInformation[0])
513 goto skip_params;
516 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
518 for (i = 0; i < rec->NumberParameters; i++)
519 ok( rec->ExceptionInformation[i] == except->params[i],
520 "%u: Wrong parameter %d: %lx/%x\n",
521 entry, i, rec->ExceptionInformation[i], except->params[i] );
523 else
525 for (i = 0; i < rec->NumberParameters; i++)
526 ok( rec->ExceptionInformation[i] == except->alt_params[i],
527 "%u: Wrong parameter %d: %lx/%x\n",
528 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
531 skip_params:
532 /* don't handle exception if it's not the address we expected */
533 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
535 context->Eip += except->length;
536 return ExceptionContinueExecution;
539 static void test_prot_fault(void)
541 unsigned int i;
543 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
545 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
547 skip( "Exception %u broken on Wow64\n", i );
548 continue;
550 got_exception = 0;
551 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
552 sizeof(exceptions[i].code), 0);
553 if (!i && !got_exception)
555 trace( "No exception, assuming win9x, no point in testing further\n" );
556 break;
558 ok( got_exception == (exceptions[i].status != 0),
559 "%u: bad exception count %d\n", i, got_exception );
563 struct dbgreg_test {
564 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
567 /* test handling of debug registers */
568 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
569 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
571 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
573 context->Eip += 2; /* Skips the popl (%eax) */
574 context->Dr0 = test->dr0;
575 context->Dr1 = test->dr1;
576 context->Dr2 = test->dr2;
577 context->Dr3 = test->dr3;
578 context->Dr6 = test->dr6;
579 context->Dr7 = test->dr7;
580 return ExceptionContinueExecution;
583 #define CHECK_DEBUG_REG(n, m) \
584 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
585 test_num, test->dr##n, ctx.Dr##n)
587 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
589 CONTEXT ctx;
590 NTSTATUS status;
592 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
593 status = pNtGetContextThread(GetCurrentThread(), &ctx);
594 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
596 CHECK_DEBUG_REG(0, ~0);
597 CHECK_DEBUG_REG(1, ~0);
598 CHECK_DEBUG_REG(2, ~0);
599 CHECK_DEBUG_REG(3, ~0);
600 CHECK_DEBUG_REG(6, 0x0f);
601 CHECK_DEBUG_REG(7, ~0xdc00);
604 static const BYTE segfault_code[5] = {
605 0x31, 0xc0, /* xor %eax,%eax */
606 0x8f, 0x00, /* popl (%eax) - cause exception */
607 0xc3 /* ret */
610 /* test the single step exception behaviour */
611 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
612 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
614 got_exception++;
615 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
617 if( got_exception < 3)
618 context->EFlags |= 0x100; /* single step until popf instruction */
619 else {
620 /* show that the last single step exception on the popf instruction
621 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
622 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
623 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
626 return ExceptionContinueExecution;
629 static const BYTE single_stepcode[] = {
630 0x9c, /* pushf */
631 0x58, /* pop %eax */
632 0x0d,0,1,0,0, /* or $0x100,%eax */
633 0x50, /* push %eax */
634 0x9d, /* popf */
635 0x35,0,1,0,0, /* xor $0x100,%eax */
636 0x50, /* push %eax */
637 0x9d, /* popf */
638 0xc3
641 /* Test the alignment check (AC) flag handling. */
642 static const BYTE align_check_code[] = {
643 0x55, /* push %ebp */
644 0x89,0xe5, /* mov %esp,%ebp */
645 0x9c, /* pushf */
646 0x58, /* pop %eax */
647 0x0d,0,0,4,0, /* or $0x40000,%eax */
648 0x50, /* push %eax */
649 0x9d, /* popf */
650 0x89,0xe0, /* mov %esp, %eax */
651 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
652 0x9c, /* pushf */
653 0x58, /* pop %eax */
654 0x35,0,0,4,0, /* xor $0x40000,%eax */
655 0x50, /* push %eax */
656 0x9d, /* popf */
657 0x5d, /* pop %ebp */
658 0xc3, /* ret */
661 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
662 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
664 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
665 got_exception++;
666 return ExceptionContinueExecution;
669 /* Test the direction flag handling. */
670 static const BYTE direction_flag_code[] = {
671 0x55, /* push %ebp */
672 0x89,0xe5, /* mov %esp,%ebp */
673 0xfd, /* std */
674 0xfa, /* cli - cause exception */
675 0x5d, /* pop %ebp */
676 0xc3, /* ret */
679 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
680 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
682 #ifdef __GNUC__
683 unsigned int flags;
684 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
685 /* older windows versions don't clear DF properly so don't test */
686 if (flags & 0x400) trace( "eflags has DF bit set\n" );
687 #endif
688 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
689 got_exception++;
690 context->Eip++; /* skip cli */
691 context->EFlags &= ~0x400; /* make sure it is cleared on return */
692 return ExceptionContinueExecution;
695 /* test single stepping over hardware breakpoint */
696 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
697 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
699 got_exception++;
700 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
701 "wrong exception code: %x\n", rec->ExceptionCode);
703 if(got_exception == 1) {
704 /* hw bp exception on first nop */
705 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
706 context->Eip, (DWORD)code_mem);
707 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
708 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
709 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
710 context->EFlags |= 0x100; /* enable single stepping */
711 } else if( got_exception == 2) {
712 /* single step exception on second nop */
713 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
714 context->Eip, (DWORD)code_mem + 1);
715 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
716 /* depending on the win version the B0 bit is already set here as well
717 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
718 context->EFlags |= 0x100;
719 } else if( got_exception == 3) {
720 /* hw bp exception on second nop */
721 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
722 context->Eip, (DWORD)code_mem + 1);
723 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
724 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
725 context->Dr0 = 0; /* clear breakpoint */
726 context->EFlags |= 0x100;
727 } else {
728 /* single step exception on ret */
729 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
730 context->Eip, (DWORD)code_mem + 2);
731 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
732 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
735 context->Dr6 = 0; /* clear status register */
736 return ExceptionContinueExecution;
739 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
741 /* test int3 handling */
742 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
743 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
745 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
746 code_mem, rec->ExceptionAddress);
747 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
748 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
750 return ExceptionContinueExecution;
753 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
756 static void test_exceptions(void)
758 CONTEXT ctx;
759 NTSTATUS res;
760 struct dbgreg_test dreg_test;
762 if (!pNtGetContextThread || !pNtSetContextThread)
764 skip( "NtGetContextThread/NtSetContextThread not found\n" );
765 return;
768 /* test handling of debug registers */
769 memset(&dreg_test, 0, sizeof(dreg_test));
771 dreg_test.dr0 = 0x42424240;
772 dreg_test.dr2 = 0x126bb070;
773 dreg_test.dr3 = 0x0badbad0;
774 dreg_test.dr7 = 0xffff0115;
775 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
776 check_debug_registers(1, &dreg_test);
778 dreg_test.dr0 = 0x42424242;
779 dreg_test.dr2 = 0x100f0fe7;
780 dreg_test.dr3 = 0x0abebabe;
781 dreg_test.dr7 = 0x115;
782 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
783 check_debug_registers(2, &dreg_test);
785 /* test single stepping behavior */
786 got_exception = 0;
787 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
788 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
790 /* test alignment exceptions */
791 got_exception = 0;
792 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
793 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
795 /* test direction flag */
796 got_exception = 0;
797 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
798 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
800 /* test single stepping over hardware breakpoint */
801 memset(&ctx, 0, sizeof(ctx));
802 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
803 ctx.Dr7 = 3;
804 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
805 res = pNtSetContextThread( GetCurrentThread(), &ctx);
806 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
808 got_exception = 0;
809 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
810 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
812 /* test int3 handling */
813 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
816 static void test_debugger(void)
818 char cmdline[MAX_PATH];
819 PROCESS_INFORMATION pi;
820 STARTUPINFOA si = { 0 };
821 DEBUG_EVENT de;
822 DWORD continuestatus;
823 PVOID code_mem_address = NULL;
824 NTSTATUS status;
825 SIZE_T size_read;
826 BOOL ret;
827 int counter = 0;
828 si.cb = sizeof(si);
830 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
832 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
833 return;
836 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
837 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
838 ok(ret, "could not create child process error: %u\n", GetLastError());
839 if (!ret)
840 return;
844 continuestatus = DBG_CONTINUE;
845 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
847 if (de.dwThreadId != pi.dwThreadId)
849 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
850 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
851 continue;
854 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
856 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
858 skip("child process loaded at different address, terminating it\n");
859 pNtTerminateProcess(pi.hProcess, 0);
862 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
864 CONTEXT ctx;
865 int stage;
867 counter++;
868 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
869 sizeof(code_mem_address), &size_read);
870 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
871 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
872 sizeof(stage), &size_read);
873 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
875 ctx.ContextFlags = CONTEXT_FULL;
876 status = pNtGetContextThread(pi.hThread, &ctx);
877 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
879 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
880 de.u.Exception.ExceptionRecord.ExceptionCode,
881 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
883 if (counter > 100)
885 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
886 pNtTerminateProcess(pi.hProcess, 1);
888 else if (counter >= 2) /* skip startup breakpoint */
890 if (stage == 1)
892 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
893 ctx.Eip, (char *)code_mem_address + 0xb);
894 /* setting the context from debugger does not affect the context, the exception handlers gets */
895 /* uncomment once wine is fixed */
896 /* ctx.Eip = 0x12345; */
897 ctx.Eax = 0xf00f00f1;
899 /* let the debuggee handle the exception */
900 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
902 else if (stage == 2)
904 if (de.u.Exception.dwFirstChance)
906 /* debugger gets first chance exception with unmodified ctx.Eip */
907 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
908 ctx.Eip, (char *)code_mem_address + 0xb);
910 /* setting the context from debugger does not affect the context, the exception handlers gets */
911 /* uncomment once wine is fixed */
912 /* ctx.Eip = 0x12345; */
913 ctx.Eax = 0xf00f00f1;
915 /* pass exception to debuggee
916 * exception will not be handled and
917 * a second chance exception will be raised */
918 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
920 else
922 /* debugger gets context after exception handler has played with it */
923 /* ctx.Eip is the same value the exception handler got */
924 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
926 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
927 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
928 "Eip at 0x%x instead of %p\n",
929 ctx.Eip, (char *)code_mem_address + 0xa);
930 /* need to fixup Eip for debuggee */
931 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
932 ctx.Eip += 1;
934 else
935 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
936 ctx.Eip, (char *)code_mem_address + 0xb);
937 /* here we handle exception */
940 else
941 ok(FALSE, "unexpected stage %x\n", stage);
943 status = pNtSetContextThread(pi.hThread, &ctx);
944 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
947 else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
949 int stage;
950 char buffer[64];
952 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
953 sizeof(stage), &size_read);
954 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
956 ok(!de.u.DebugString.fUnicode, "unexpected unicode debug string event\n");
957 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
958 de.u.DebugString.nDebugStringLength);
960 memset(buffer, 0, sizeof(buffer));
961 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
962 de.u.DebugString.nDebugStringLength, &size_read);
963 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
965 if (stage == 3 || stage == 4)
966 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
967 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
968 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
970 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
972 else if (de.dwDebugEventCode == RIP_EVENT)
974 int stage;
976 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
977 sizeof(stage), &size_read);
978 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
980 if (stage == 5 || stage == 6)
982 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
983 de.u.RipInfo.dwError, 0x11223344);
984 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
985 de.u.RipInfo.dwType, 0x55667788);
987 else
988 ok(FALSE, "unexpected stage %x\n", stage);
990 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
993 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
995 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
997 winetest_wait_child_process( pi.hProcess );
998 ret = CloseHandle(pi.hThread);
999 ok(ret, "error %u\n", GetLastError());
1000 ret = CloseHandle(pi.hProcess);
1001 ok(ret, "error %u\n", GetLastError());
1003 return;
1006 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1007 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1009 int *stage = *(int **)(frame + 1);
1011 got_exception++;
1013 if( *stage == 1) {
1014 /* fault while executing sse instruction */
1015 context->Eip += 3; /* skip addps */
1016 return ExceptionContinueExecution;
1018 else if ( *stage == 2 || *stage == 3 ) {
1019 /* stage 2 - divide by zero fault */
1020 /* stage 3 - invalid operation fault */
1021 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1022 skip("system doesn't support SIMD exceptions\n");
1023 else {
1024 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1025 "exception code: %#x, should be %#x\n",
1026 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1027 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1028 "# of params: %i, should be 1\n",
1029 rec->NumberParameters);
1030 if( rec->NumberParameters == 1 )
1031 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1033 context->Eip += 3; /* skip divps */
1035 else
1036 ok(FALSE, "unexpected stage %x\n", *stage);
1038 return ExceptionContinueExecution;
1041 static const BYTE simd_exception_test[] = {
1042 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1043 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1044 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1045 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1046 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1047 0x6a, 0x01, /* push $0x1 */
1048 0x6a, 0x01, /* push $0x1 */
1049 0x6a, 0x01, /* push $0x1 */
1050 0x6a, 0x01, /* push $0x1 */
1051 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1052 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1053 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1054 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1055 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1056 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1057 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1058 0xc3, /* ret */
1061 static const BYTE simd_exception_test2[] = {
1062 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1063 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1064 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1065 0x66, 0x81, 0x24, 0x24, 0x7f, 0xff, /* andw $0xff7f,(%esp) * enable invalid */
1066 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * operation exceptions */
1067 0x0f, 0x57, 0xc9, /* xorps %xmm1,%xmm1 * clear dividend */
1068 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1069 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1070 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1071 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1072 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1073 0xc3, /* ret */
1076 static const BYTE sse_check[] = {
1077 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1078 0xc3, /* ret */
1081 static void test_simd_exceptions(void)
1083 int stage;
1085 /* test if CPU & OS can do sse */
1086 stage = 1;
1087 got_exception = 0;
1088 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1089 if(got_exception) {
1090 skip("system doesn't support SSE\n");
1091 return;
1094 /* generate a SIMD exception */
1095 stage = 2;
1096 got_exception = 0;
1097 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1098 sizeof(simd_exception_test), 0);
1099 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1101 /* generate a SIMD exception, test FPE_FLTINV */
1102 stage = 3;
1103 got_exception = 0;
1104 run_exception_test(simd_fault_handler, &stage, simd_exception_test2,
1105 sizeof(simd_exception_test2), 0);
1106 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1109 struct fpu_exception_info
1111 DWORD exception_code;
1112 DWORD exception_offset;
1113 DWORD eip_offset;
1116 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1117 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1119 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1121 info->exception_code = rec->ExceptionCode;
1122 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1123 info->eip_offset = context->Eip - (DWORD)code_mem;
1125 ++context->Eip;
1126 return ExceptionContinueExecution;
1129 static void test_fpu_exceptions(void)
1131 static const BYTE fpu_exception_test_ie[] =
1133 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1134 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1135 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1136 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1137 0xd9, 0xee, /* fldz */
1138 0xd9, 0xe8, /* fld1 */
1139 0xde, 0xf1, /* fdivp */
1140 0xdd, 0xd8, /* fstp %st(0) */
1141 0xdd, 0xd8, /* fstp %st(0) */
1142 0x9b, /* fwait */
1143 0xdb, 0xe2, /* fnclex */
1144 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1145 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1146 0xc3, /* ret */
1149 static const BYTE fpu_exception_test_de[] =
1151 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1152 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1153 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1154 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1155 0xdd, 0xd8, /* fstp %st(0) */
1156 0xd9, 0xee, /* fldz */
1157 0xd9, 0xe8, /* fld1 */
1158 0xde, 0xf1, /* fdivp */
1159 0x9b, /* fwait */
1160 0xdb, 0xe2, /* fnclex */
1161 0xdd, 0xd8, /* fstp %st(0) */
1162 0xdd, 0xd8, /* fstp %st(0) */
1163 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1164 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1165 0xc3, /* ret */
1168 struct fpu_exception_info info;
1170 memset(&info, 0, sizeof(info));
1171 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1172 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1173 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1174 ok(info.exception_offset == 0x19 ||
1175 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1176 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1177 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1179 memset(&info, 0, sizeof(info));
1180 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1181 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1182 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1183 ok(info.exception_offset == 0x17 ||
1184 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1185 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1186 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1189 struct dpe_exception_info {
1190 BOOL exception_caught;
1191 DWORD exception_info;
1194 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1195 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1197 DWORD old_prot;
1198 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1200 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1201 "Exception code %08x\n", rec->ExceptionCode);
1202 ok(rec->NumberParameters == 2,
1203 "Parameter count: %d\n", rec->NumberParameters);
1204 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1205 "Exception address: %p, expected %p\n",
1206 (LPVOID)rec->ExceptionInformation[1], code_mem);
1208 info->exception_info = rec->ExceptionInformation[0];
1209 info->exception_caught = TRUE;
1211 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1212 return ExceptionContinueExecution;
1215 static void test_dpe_exceptions(void)
1217 static const BYTE single_ret[] = {0xC3};
1218 struct dpe_exception_info info;
1219 NTSTATUS stat;
1220 BOOL has_hw_support;
1221 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1222 DWORD val;
1223 ULONG len;
1225 /* Query DEP with len too small */
1226 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1227 if(stat == STATUS_INVALID_INFO_CLASS)
1229 skip("This software platform does not support DEP\n");
1230 return;
1232 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1234 /* Query DEP */
1235 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1236 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1237 if(stat == STATUS_SUCCESS)
1239 ok(len == sizeof val, "returned length: %d\n", len);
1240 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1242 skip("toggling DEP impossible - status locked\n");
1243 is_permanent = TRUE;
1244 if(val & MEM_EXECUTE_OPTION_DISABLE)
1245 can_test_without = FALSE;
1246 else
1247 can_test_with = FALSE;
1251 if(!is_permanent)
1253 /* Enable DEP */
1254 val = MEM_EXECUTE_OPTION_DISABLE;
1255 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1256 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1259 if(can_test_with)
1261 /* Try access to locked page with DEP on*/
1262 info.exception_caught = FALSE;
1263 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1264 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1265 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1266 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1267 "Access violation type: %08x\n", (unsigned)info.exception_info);
1268 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1269 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1271 /* Try execution of data with DEP on*/
1272 info.exception_caught = FALSE;
1273 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1274 if(has_hw_support)
1276 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1277 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1278 "Access violation type: %08x\n", (unsigned)info.exception_info);
1280 else
1281 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1283 else
1284 skip("DEP is in AlwaysOff state\n");
1286 if(!is_permanent)
1288 /* Disable DEP */
1289 val = MEM_EXECUTE_OPTION_ENABLE;
1290 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1291 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1294 /* page is read without exec here */
1295 if(can_test_without)
1297 /* Try execution of data with DEP off */
1298 info.exception_caught = FALSE;
1299 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1300 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1302 /* Try access to locked page with DEP off - error code is different than
1303 with hardware DEP on */
1304 info.exception_caught = FALSE;
1305 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1306 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1307 ok(info.exception_info == EXCEPTION_READ_FAULT,
1308 "Access violation type: %08x\n", (unsigned)info.exception_info);
1310 else
1311 skip("DEP is in AlwaysOn state\n");
1313 if(!is_permanent)
1315 /* Turn off DEP permanently */
1316 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1317 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1318 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1321 /* Try to turn off DEP */
1322 val = MEM_EXECUTE_OPTION_ENABLE;
1323 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1324 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1326 /* Try to turn on DEP */
1327 val = MEM_EXECUTE_OPTION_DISABLE;
1328 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1329 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1332 #elif defined(__x86_64__)
1334 #define UNW_FLAG_NHANDLER 0
1335 #define UNW_FLAG_EHANDLER 1
1336 #define UNW_FLAG_UHANDLER 2
1337 #define UNW_FLAG_CHAININFO 4
1339 #define UWOP_PUSH_NONVOL 0
1340 #define UWOP_ALLOC_LARGE 1
1341 #define UWOP_ALLOC_SMALL 2
1342 #define UWOP_SET_FPREG 3
1343 #define UWOP_SAVE_NONVOL 4
1344 #define UWOP_SAVE_NONVOL_FAR 5
1345 #define UWOP_SAVE_XMM128 8
1346 #define UWOP_SAVE_XMM128_FAR 9
1347 #define UWOP_PUSH_MACHFRAME 10
1349 struct results
1351 int rip_offset; /* rip offset from code start */
1352 int rbp_offset; /* rbp offset from stack pointer */
1353 int handler; /* expect handler to be set? */
1354 int rip; /* expected final rip value */
1355 int frame; /* expected frame return value */
1356 int regs[8][2]; /* expected values for registers */
1359 struct unwind_test
1361 const BYTE *function;
1362 size_t function_size;
1363 const BYTE *unwind_info;
1364 const struct results *results;
1365 unsigned int nb_results;
1368 enum regs
1370 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1371 r8, r9, r10, r11, r12, r13, r14, r15
1374 static const char * const reg_names[16] =
1376 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1377 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1380 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1382 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1384 static const int code_offset = 1024;
1385 static const int unwind_offset = 2048;
1386 void *handler, *data;
1387 CONTEXT context;
1388 RUNTIME_FUNCTION runtime_func;
1389 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1390 UINT i, j, k;
1391 ULONG64 fake_stack[256];
1392 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1393 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1395 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1396 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1398 runtime_func.BeginAddress = code_offset;
1399 runtime_func.EndAddress = code_offset + test->function_size;
1400 runtime_func.UnwindData = unwind_offset;
1402 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1404 for (i = 0; i < test->nb_results; i++)
1406 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1407 memset( &context, 0x55, sizeof(context) );
1408 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1409 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1411 context.Rsp = (ULONG_PTR)fake_stack;
1412 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1413 orig_rbp = context.Rbp;
1414 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1416 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1417 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1419 data = (void *)0xdeadbeef;
1420 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1421 &runtime_func, &context, &data, &frame, &ctx_ptr );
1422 if (test->results[i].handler)
1424 ok( (char *)handler == (char *)code_mem + 0x200,
1425 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1426 if (handler) ok( *(DWORD *)data == 0x08070605,
1427 "%u/%u: wrong handler data %p\n", testnum, i, data );
1429 else
1431 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1432 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1435 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1436 testnum, i, (void *)context.Rip, test->results[i].rip );
1437 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1438 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1440 for (j = 0; j < 16; j++)
1442 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1444 for (k = 0; k < nb_regs; k++)
1446 if (test->results[i].regs[k][0] == -1)
1448 k = nb_regs;
1449 break;
1451 if (test->results[i].regs[k][0] == j) break;
1454 if (j == rsp) /* rsp is special */
1456 ok( !ctx_ptr.u2.IntegerContext[j],
1457 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1458 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1459 "%u/%u: register rsp wrong %p/%p\n",
1460 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1461 continue;
1464 if (ctx_ptr.u2.IntegerContext[j])
1466 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1467 testnum, i, reg_names[j], *(&context.Rax + j) );
1468 if (k < nb_regs)
1469 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1470 "%u/%u: register %s wrong %p/%x\n",
1471 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1473 else
1475 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1476 if (j == rbp)
1477 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1478 testnum, i, (void *)context.Rbp );
1479 else
1480 ok( *(&context.Rax + j) == unset_reg,
1481 "%u/%u: register %s wrong %p/unset\n",
1482 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1488 static void test_virtual_unwind(void)
1490 static const BYTE function_0[] =
1492 0xff, 0xf5, /* 00: push %rbp */
1493 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1494 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1495 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1496 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1497 0x90, /* 1c: nop */
1498 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1499 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1500 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1501 0x5d, /* 32: pop %rbp */
1502 0xc3 /* 33: ret */
1505 static const BYTE unwind_info_0[] =
1507 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1508 0x1c, /* prolog size */
1509 8, /* opcode count */
1510 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1512 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1513 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1514 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1515 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1516 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1518 0x00, 0x02, 0x00, 0x00, /* handler */
1519 0x05, 0x06, 0x07, 0x08, /* data */
1522 static const struct results results_0[] =
1524 /* offset rbp handler rip frame registers */
1525 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1526 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1527 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1528 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1529 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1530 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1531 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1532 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1533 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1534 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1535 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1539 static const BYTE function_1[] =
1541 0x53, /* 00: push %rbx */
1542 0x55, /* 01: push %rbp */
1543 0x56, /* 02: push %rsi */
1544 0x57, /* 03: push %rdi */
1545 0x41, 0x54, /* 04: push %r12 */
1546 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1547 0x90, 0x90, /* 0a: nop; nop */
1548 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1549 0x41, 0x5c, /* 10: pop %r12 */
1550 0x5f, /* 12: pop %rdi */
1551 0x5e, /* 13: pop %rsi */
1552 0x5d, /* 14: pop %rbp */
1553 0x5b, /* 15: pop %rbx */
1554 0xc3 /* 16: ret */
1557 static const BYTE unwind_info_1[] =
1559 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1560 0x0a, /* prolog size */
1561 6, /* opcode count */
1562 0, /* frame reg */
1564 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1565 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1566 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1567 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1568 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1569 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1571 0x00, 0x02, 0x00, 0x00, /* handler */
1572 0x05, 0x06, 0x07, 0x08, /* data */
1575 static const struct results results_1[] =
1577 /* offset rbp handler rip frame registers */
1578 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1579 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1580 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1581 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1582 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1583 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1584 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1585 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1586 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1587 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1588 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1589 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1590 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1591 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1594 static const struct unwind_test tests[] =
1596 { function_0, sizeof(function_0), unwind_info_0,
1597 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1598 { function_1, sizeof(function_1), unwind_info_1,
1599 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1601 unsigned int i;
1603 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1604 call_virtual_unwind( i, &tests[i] );
1607 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1609 static const int code_offset = 1024;
1610 static RUNTIME_FUNCTION runtime_func;
1611 (*(DWORD *)context)++;
1613 runtime_func.BeginAddress = code_offset + 16;
1614 runtime_func.EndAddress = code_offset + 32;
1615 runtime_func.UnwindData = 0;
1616 return &runtime_func;
1619 static void test_dynamic_unwind(void)
1621 static const int code_offset = 1024;
1622 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1623 RUNTIME_FUNCTION *runtime_func, *func;
1624 ULONG_PTR table, base;
1625 DWORD count;
1627 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1628 runtime_func = (RUNTIME_FUNCTION *)buf;
1629 runtime_func->BeginAddress = code_offset;
1630 runtime_func->EndAddress = code_offset + 16;
1631 runtime_func->UnwindData = 0;
1632 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1633 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1635 /* Lookup function outside of any function table */
1636 base = 0xdeadbeef;
1637 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1638 ok( func == NULL,
1639 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1640 ok( !base || broken(base == 0xdeadbeef),
1641 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1643 /* Test with pointer inside of our function */
1644 base = 0xdeadbeef;
1645 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1646 ok( func == runtime_func,
1647 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1648 ok( base == (ULONG_PTR)code_mem,
1649 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1651 /* Test RtlDeleteFunctionTable */
1652 ok( pRtlDeleteFunctionTable( runtime_func ),
1653 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1654 ok( !pRtlDeleteFunctionTable( runtime_func ),
1655 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1657 /* Unaligned RUNTIME_FUNCTION pointer */
1658 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1659 runtime_func->BeginAddress = code_offset;
1660 runtime_func->EndAddress = code_offset + 16;
1661 runtime_func->UnwindData = 0;
1662 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1663 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1664 ok( pRtlDeleteFunctionTable( runtime_func ),
1665 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1667 /* Attempt to insert the same entry twice */
1668 runtime_func = (RUNTIME_FUNCTION *)buf;
1669 runtime_func->BeginAddress = code_offset;
1670 runtime_func->EndAddress = code_offset + 16;
1671 runtime_func->UnwindData = 0;
1672 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1673 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1674 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1675 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1676 ok( pRtlDeleteFunctionTable( runtime_func ),
1677 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1678 ok( pRtlDeleteFunctionTable( runtime_func ),
1679 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1680 ok( !pRtlDeleteFunctionTable( runtime_func ),
1681 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1683 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1684 table = (ULONG_PTR)code_mem;
1685 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1686 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1688 /* Test RtlInstallFunctionTableCallback with both low bits set */
1689 table = (ULONG_PTR)code_mem | 0x3;
1690 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1691 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1693 /* Lookup function outside of any function table */
1694 count = 0;
1695 base = 0xdeadbeef;
1696 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1697 ok( func == NULL,
1698 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1699 ok( !base || broken(base == 0xdeadbeef),
1700 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
1701 ok( !count,
1702 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1704 /* Test with pointer inside of our function */
1705 count = 0;
1706 base = 0xdeadbeef;
1707 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1708 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1709 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1710 ok( base == (ULONG_PTR)code_mem,
1711 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1712 ok( count == 1,
1713 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1715 /* Clean up again */
1716 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1717 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1718 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1719 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1723 #endif /* __x86_64__ */
1725 #if defined(__i386__) || defined(__x86_64__)
1726 static DWORD outputdebugstring_exceptions;
1728 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1730 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1731 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1733 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
1734 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
1735 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1736 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
1737 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
1738 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
1740 outputdebugstring_exceptions++;
1741 return EXCEPTION_CONTINUE_SEARCH;
1744 static void test_outputdebugstring(DWORD numexc, BOOL todo)
1746 PVOID vectored_handler;
1748 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
1750 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1751 return;
1754 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
1755 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1757 outputdebugstring_exceptions = 0;
1758 OutputDebugStringA("Hello World");
1759 if (todo)
1760 todo_wine
1761 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1762 outputdebugstring_exceptions, numexc);
1763 else
1764 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
1765 outputdebugstring_exceptions, numexc);
1767 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1770 static DWORD ripevent_exceptions;
1772 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
1774 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
1775 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
1777 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
1778 rec->ExceptionCode, DBG_RIPEXCEPTION);
1779 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
1780 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
1781 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
1782 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
1783 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
1785 ripevent_exceptions++;
1786 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
1789 static void test_ripevent(DWORD numexc)
1791 EXCEPTION_RECORD record;
1792 PVOID vectored_handler;
1794 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
1796 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1797 return;
1800 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
1801 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
1803 record.ExceptionCode = DBG_RIPEXCEPTION;
1804 record.ExceptionFlags = 0;
1805 record.ExceptionRecord = NULL;
1806 record.ExceptionAddress = NULL;
1807 record.NumberParameters = 2;
1808 record.ExceptionInformation[0] = 0x11223344;
1809 record.ExceptionInformation[1] = 0x55667788;
1811 ripevent_exceptions = 0;
1812 pRtlRaiseException(&record);
1813 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
1814 ripevent_exceptions, numexc);
1816 pRtlRemoveVectoredExceptionHandler(vectored_handler);
1819 static void test_vectored_continue_handler(void)
1821 PVOID handler1, handler2;
1822 ULONG ret;
1824 if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
1826 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
1827 return;
1830 handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1831 ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
1833 handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
1834 ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
1835 ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
1837 if (pRtlRemoveVectoredExceptionHandler)
1839 ret = pRtlRemoveVectoredExceptionHandler(handler1);
1840 ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
1843 ret = pRtlRemoveVectoredContinueHandler(handler1);
1844 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1846 ret = pRtlRemoveVectoredContinueHandler(handler2);
1847 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
1849 ret = pRtlRemoveVectoredContinueHandler(handler1);
1850 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1852 ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
1853 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
1855 #endif /* defined(__i386__) || defined(__x86_64__) */
1857 START_TEST(exception)
1859 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1861 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1862 if(!code_mem) {
1863 trace("VirtualAlloc failed\n");
1864 return;
1867 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1868 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1869 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1870 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1871 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
1872 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1873 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1874 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1875 "RtlAddVectoredExceptionHandler" );
1876 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1877 "RtlRemoveVectoredExceptionHandler" );
1878 pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1879 "RtlAddVectoredContinueHandler" );
1880 pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
1881 "RtlRemoveVectoredContinueHandler" );
1882 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1883 "NtQueryInformationProcess" );
1884 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1885 "NtSetInformationProcess" );
1886 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
1888 #ifdef __i386__
1889 if (!pNtCurrentTeb)
1891 skip( "NtCurrentTeb not found\n" );
1892 return;
1894 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1896 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1897 have_vectored_api = TRUE;
1898 else
1899 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1901 my_argc = winetest_get_mainargs( &my_argv );
1902 if (my_argc >= 4)
1904 void *addr;
1905 sscanf( my_argv[3], "%p", &addr );
1907 if (addr != &test_stage)
1909 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1910 return;
1913 /* child must be run under a debugger */
1914 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1916 ok(FALSE, "child process not being debugged?\n");
1917 return;
1920 if (pRtlRaiseException)
1922 test_stage = 1;
1923 run_rtlraiseexception_test(0x12345);
1924 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1925 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1926 test_stage = 2;
1927 run_rtlraiseexception_test(0x12345);
1928 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1929 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1930 test_stage = 3;
1931 test_outputdebugstring(0, FALSE);
1932 test_stage = 4;
1933 test_outputdebugstring(2, TRUE); /* is this a Windows bug? */
1934 test_stage = 5;
1935 test_ripevent(0);
1936 test_stage = 6;
1937 test_ripevent(1);
1939 else
1940 skip( "RtlRaiseException not found\n" );
1942 /* rest of tests only run in parent */
1943 return;
1946 test_unwind();
1947 test_exceptions();
1948 test_rtlraiseexception();
1949 test_outputdebugstring(1, FALSE);
1950 test_ripevent(1);
1951 test_vectored_continue_handler();
1952 test_debugger();
1953 test_simd_exceptions();
1954 test_fpu_exceptions();
1955 test_dpe_exceptions();
1956 test_prot_fault();
1958 #elif defined(__x86_64__)
1959 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
1960 "RtlAddFunctionTable" );
1961 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
1962 "RtlDeleteFunctionTable" );
1963 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
1964 "RtlInstallFunctionTableCallback" );
1965 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
1966 "RtlLookupFunctionEntry" );
1968 test_outputdebugstring(1, FALSE);
1969 test_ripevent(1);
1970 test_vectored_continue_handler();
1971 test_virtual_unwind();
1973 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
1974 test_dynamic_unwind();
1975 else
1976 skip( "Dynamic unwind functions not found\n" );
1978 #endif
1980 VirtualFree(code_mem, 0, MEM_FREE);