ntdll/tests: Fix some exception tests for Wow64.
[wine] / dlls / ntdll / tests / 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 "excpt.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 *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
47 static ULONG     (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
48 static NTSTATUS  (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
49 static NTSTATUS  (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
50 static NTSTATUS  (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
51 static NTSTATUS  (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
52 static BOOL      (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
53
54 #ifdef __i386__
55
56 #ifndef __WINE_WINTRNL_H
57 #define ProcessExecuteFlags 0x22
58 #define MEM_EXECUTE_OPTION_DISABLE   0x01
59 #define MEM_EXECUTE_OPTION_ENABLE    0x02
60 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
61 #endif
62
63 static int      my_argc;
64 static char**   my_argv;
65 static int      test_stage;
66
67 static BOOL is_wow64;
68
69 /* Test various instruction combinations that cause a protection fault on the i386,
70  * and check what the resulting exception looks like.
71  */
72
73 static const struct exception
74 {
75     BYTE     code[18];   /* asm code */
76     BYTE     offset;     /* offset of faulting instruction */
77     BYTE     length;     /* length of faulting instruction */
78     BOOL     wow64_broken; /* broken on Wow64, should be skipped */
79     NTSTATUS status;     /* expected status code */
80     DWORD    nb_params;  /* expected number of parameters */
81     DWORD    params[4];  /* expected parameters */
82 } exceptions[] =
83 {
84 /* 0 */
85     /* test some privileged instructions */
86     { { 0xfb, 0xc3 },  /* 0: sti; ret */
87       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
88     { { 0x6c, 0xc3 },  /* 1: insb (%dx); ret */
89       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
90     { { 0x6d, 0xc3 },  /* 2: insl (%dx); ret */
91       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
92     { { 0x6e, 0xc3 },  /* 3: outsb (%dx); ret */
93       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
94     { { 0x6f, 0xc3 },  /* 4: outsl (%dx); ret */
95       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
96 /* 5 */
97     { { 0xe4, 0x11, 0xc3 },  /* 5: inb $0x11,%al; ret */
98       0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
99     { { 0xe5, 0x11, 0xc3 },  /* 6: inl $0x11,%eax; ret */
100       0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
101     { { 0xe6, 0x11, 0xc3 },  /* 7: outb %al,$0x11; ret */
102       0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
103     { { 0xe7, 0x11, 0xc3 },  /* 8: outl %eax,$0x11; ret */
104       0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
105     { { 0xed, 0xc3 },  /* 9: inl (%dx),%eax; ret */
106       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
107 /* 10 */
108     { { 0xee, 0xc3 },  /* 10: outb %al,(%dx); ret */
109       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
110     { { 0xef, 0xc3 },  /* 11: outl %eax,(%dx); ret */
111       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
112     { { 0xf4, 0xc3 },  /* 12: hlt; ret */
113       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
114     { { 0xfa, 0xc3 },  /* 13: cli; ret */
115       0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
116
117     /* test long jump to invalid selector */
118     { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 },  /* 14: ljmp $0,$0; ret */
119       0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
120
121 /* 15 */
122     /* test iret to invalid selector */
123     { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
124       /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
125       6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
126
127     /* test loading an invalid selector */
128     { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 },  /* 16: mov $beef,%ax; mov %ax,%gs; ret */
129       5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
130
131     /* test accessing a zero selector (%es broken on Wow64) */
132     { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
133        /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
134       5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
135     { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
136       /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
137       6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
138
139     /* test moving %cs -> %ss */
140     { { 0x0e, 0x17, 0x58, 0xc3 },  /* 18: pushl %cs; popl %ss; popl %eax; ret */
141       1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
142
143 /* 20 */
144     /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
145     { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
146       0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0 },
147     { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
148       0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
149
150     /* test invalid interrupt */
151     { { 0xcd, 0xff, 0xc3 },   /* 21: int $0xff; ret */
152       0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
153
154     /* test moves to/from Crx */
155     { { 0x0f, 0x20, 0xc0, 0xc3 },  /* 22: movl %cr0,%eax; ret */
156       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
157     { { 0x0f, 0x20, 0xe0, 0xc3 },  /* 23: movl %cr4,%eax; ret */
158       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
159 /* 25 */
160     { { 0x0f, 0x22, 0xc0, 0xc3 },  /* 24: movl %eax,%cr0; ret */
161       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
162     { { 0x0f, 0x22, 0xe0, 0xc3 },  /* 25: movl %eax,%cr4; ret */
163       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
164
165     /* test moves to/from Drx */
166     { { 0x0f, 0x21, 0xc0, 0xc3 },  /* 26: movl %dr0,%eax; ret */
167       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
168     { { 0x0f, 0x21, 0xc8, 0xc3 },  /* 27: movl %dr1,%eax; ret */
169       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
170     { { 0x0f, 0x21, 0xf8, 0xc3 },  /* 28: movl %dr7,%eax; ret */
171       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
172 /* 30 */
173     { { 0x0f, 0x23, 0xc0, 0xc3 },  /* 29: movl %eax,%dr0; ret */
174       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
175     { { 0x0f, 0x23, 0xc8, 0xc3 },  /* 30: movl %eax,%dr1; ret */
176       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
177     { { 0x0f, 0x23, 0xf8, 0xc3 },  /* 31: movl %eax,%dr7; ret */
178       0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
179
180     /* test memory reads */
181     { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 },  /* 32: movl 0xfffffffc,%eax; ret */
182       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
183     { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 },  /* 33: movl 0xfffffffd,%eax; ret */
184       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
185 /* 35 */
186     { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 },  /* 34: movl 0xfffffffe,%eax; ret */
187       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
188     { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 },  /* 35: movl 0xffffffff,%eax; ret */
189       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
190
191     /* test memory writes */
192     { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 },  /* 36: movl %eax,0xfffffffc; ret */
193       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
194     { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 },  /* 37: movl %eax,0xfffffffd; ret */
195       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
196     { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 },  /* 38: movl %eax,0xfffffffe; ret */
197       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
198 /* 40 */
199     { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 },  /* 39: movl %eax,0xffffffff; ret */
200       0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
201
202     /* test exception with cleared %ds and %es (broken on Wow64) */
203     { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
204           /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
205       8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
206 };
207
208 static int got_exception;
209 static BOOL have_vectored_api;
210
211 static void run_exception_test(void *handler, const void* context,
212                                const void *code, unsigned int code_size,
213                                DWORD access)
214 {
215     struct {
216         EXCEPTION_REGISTRATION_RECORD frame;
217         const void *context;
218     } exc_frame;
219     void (*func)(void) = code_mem;
220     DWORD oldaccess, oldaccess2;
221
222     exc_frame.frame.Handler = handler;
223     exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
224     exc_frame.context = context;
225
226     memcpy(code_mem, code, code_size);
227     if(access)
228         VirtualProtect(code_mem, code_size, access, &oldaccess);
229
230     pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
231     func();
232     pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
233
234     if(access)
235         VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
236 }
237
238 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
239 {
240     PCONTEXT context = ExceptionInfo->ContextRecord;
241     PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
242     trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
243           rec->ExceptionAddress, context->Eip);
244
245     ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
246        rec->ExceptionAddress, (char *)code_mem + 0xb);
247
248     if (pNtCurrentTeb()->Peb->BeingDebugged)
249         ok((void *)context->Eax == pRtlRaiseException ||
250            broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
251            "debugger managed to modify Eax to %x should be %p\n",
252            context->Eax, pRtlRaiseException);
253
254     /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
255      * even if raised by RtlRaiseException
256      */
257     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
258     {
259         ok(context->Eip == (DWORD)code_mem + 0xa ||
260            broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
261            "Eip at %x instead of %x or %x\n", context->Eip,
262            (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
263     }
264     else
265     {
266         ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
267            context->Eip, (DWORD)code_mem + 0xb);
268     }
269
270     /* test if context change is preserved from vectored handler to stack handlers */
271     context->Eax = 0xf00f00f0;
272     return EXCEPTION_CONTINUE_SEARCH;
273 }
274
275 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
276                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
277 {
278     trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
279            rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
280
281     ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
282        rec->ExceptionAddress, (char *)code_mem + 0xb);
283
284     /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
285      * even if raised by RtlRaiseException
286      */
287     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
288     {
289         ok(context->Eip == (DWORD)code_mem + 0xa ||
290            broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
291            "Eip at %x instead of %x or %x\n", context->Eip,
292            (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
293     }
294     else
295     {
296         ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
297            context->Eip, (DWORD)code_mem + 0xb);
298     }
299
300     if(have_vectored_api)
301         ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
302            context->Eax);
303
304     /* give the debugger a chance to examine the state a second time */
305     /* without the exception handler changing Eip */
306     if (test_stage == 2)
307         return ExceptionContinueSearch;
308
309     /* Eip in context is decreased by 1
310      * Increase it again, else execution will continue in the middle of a instruction */
311     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
312         context->Eip += 1;
313     return ExceptionContinueExecution;
314 }
315
316
317 static const BYTE call_one_arg_code[] = {
318         0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
319         0x50,                   /* push %eax */
320         0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
321         0xff, 0xd0,             /* call *%eax */
322         0x90,                   /* nop */
323         0x90,                   /* nop */
324         0x90,                   /* nop */
325         0x90,                   /* nop */
326         0xc3,                   /* ret */
327 };
328
329
330 static void run_rtlraiseexception_test(DWORD exceptioncode)
331 {
332     EXCEPTION_REGISTRATION_RECORD frame;
333     EXCEPTION_RECORD record;
334     PVOID vectored_handler = NULL;
335
336     void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
337
338     record.ExceptionCode = exceptioncode;
339     record.ExceptionFlags = 0;
340     record.ExceptionRecord = NULL;
341     record.ExceptionAddress = NULL; /* does not matter, copied return address */
342     record.NumberParameters = 0;
343
344     frame.Handler = rtlraiseexception_handler;
345     frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
346
347     memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
348
349     pNtCurrentTeb()->Tib.ExceptionList = &frame;
350     if (have_vectored_api)
351     {
352         vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
353         ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
354     }
355
356     func(pRtlRaiseException, &record);
357     ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
358         "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
359
360     if (have_vectored_api)
361         pRtlRemoveVectoredExceptionHandler(vectored_handler);
362     pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
363 }
364
365 static void test_rtlraiseexception(void)
366 {
367     if (!pRtlRaiseException)
368     {
369         skip("RtlRaiseException not found\n");
370         return;
371     }
372
373     /* test without debugger */
374     run_rtlraiseexception_test(0x12345);
375     run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
376     run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
377 }
378
379 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
380                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
381 {
382     const struct exception *except = *(const struct exception **)(frame + 1);
383     unsigned int i, entry = except - exceptions;
384
385     got_exception++;
386     trace( "exception %u: %x flags:%x addr:%p\n",
387            entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
388
389     ok( rec->ExceptionCode == except->status,
390         "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
391     ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
392         "%u: Wrong exception address %p/%p\n", entry,
393         rec->ExceptionAddress, (char*)code_mem + except->offset );
394
395     ok( rec->NumberParameters == except->nb_params,
396         "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
397
398     /* Most CPUs (except Intel Core apparently) report a segment limit violation */
399     /* instead of page faults for accesses beyond 0xffffffff */
400     if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
401     {
402         if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
403             goto skip_params;
404     }
405
406     /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
407     if (except->nb_params == 2 && rec->NumberParameters == 2
408         && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
409         && except->params[0] == rec->ExceptionInformation[0])
410     {
411         goto skip_params;
412     }
413
414     for (i = 0; i < rec->NumberParameters; i++)
415         ok( rec->ExceptionInformation[i] == except->params[i],
416             "%u: Wrong parameter %d: %lx/%x\n",
417             entry, i, rec->ExceptionInformation[i], except->params[i] );
418
419 skip_params:
420     /* don't handle exception if it's not the address we expected */
421     if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
422
423     context->Eip += except->length;
424     return ExceptionContinueExecution;
425 }
426
427 static void test_prot_fault(void)
428 {
429     unsigned int i;
430
431     for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
432     {
433         if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
434         {
435             skip( "Exception %u broken on Wow64\n", i );
436             continue;
437         }
438         got_exception = 0;
439         run_exception_test(handler, &exceptions[i], &exceptions[i].code,
440                            sizeof(exceptions[i].code), 0);
441         if (!i && !got_exception)
442         {
443             trace( "No exception, assuming win9x, no point in testing further\n" );
444             break;
445         }
446         ok( got_exception == (exceptions[i].status != 0),
447             "%u: bad exception count %d\n", i, got_exception );
448     }
449 }
450
451 /* test handling of debug registers */
452 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
453                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
454 {
455     context->Eip += 2;  /* Skips the popl (%eax) */
456     context->Dr0 = 0x42424242;
457     context->Dr1 = 0;
458     context->Dr2 = 0;
459     context->Dr3 = 0;
460     context->Dr6 = 0;
461     context->Dr7 = 0x155;
462     return ExceptionContinueExecution;
463 }
464
465 static const BYTE segfault_code[5] = {
466         0x31, 0xc0, /* xor    %eax,%eax */
467         0x8f, 0x00, /* popl   (%eax) - cause exception */
468         0xc3        /* ret */
469 };
470
471 /* test the single step exception behaviour */
472 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
473                                   CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
474 {
475     got_exception++;
476     ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
477
478     if( got_exception < 3)
479         context->EFlags |= 0x100;  /* single step until popf instruction */
480     else {
481         /* show that the last single step exception on the popf instruction
482          * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
483         ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
484             "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
485     }
486
487     return ExceptionContinueExecution;
488 }
489
490 static const BYTE single_stepcode[] = {
491     0x9c,               /* pushf */
492     0x58,               /* pop   %eax */
493     0x0d,0,1,0,0,       /* or    $0x100,%eax */
494     0x50,               /* push   %eax */
495     0x9d,               /* popf    */
496     0x35,0,1,0,0,       /* xor    $0x100,%eax */
497     0x50,               /* push   %eax */
498     0x9d,               /* popf    */
499     0xc3
500 };
501
502 /* Test the alignment check (AC) flag handling. */
503 static const BYTE align_check_code[] = {
504     0x55,                       /* push   %ebp */
505     0x89,0xe5,                  /* mov    %esp,%ebp */
506     0x9c,                       /* pushf   */
507     0x58,                       /* pop    %eax */
508     0x0d,0,0,4,0,               /* or     $0x40000,%eax */
509     0x50,                       /* push   %eax */
510     0x9d,                       /* popf    */
511     0x89,0xe0,                  /* mov %esp, %eax */
512     0x8b,0x40,0x1,              /* mov 0x1(%eax), %eax - cause exception */
513     0x9c,                       /* pushf   */
514     0x58,                       /* pop    %eax */
515     0x35,0,0,4,0,               /* xor    $0x40000,%eax */
516     0x50,                       /* push   %eax */
517     0x9d,                       /* popf    */
518     0x5d,                       /* pop    %ebp */
519     0xc3,                       /* ret     */
520 };
521
522 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
523                                   CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
524 {
525     ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
526     got_exception++;
527     return ExceptionContinueExecution;
528 }
529
530 /* Test the direction flag handling. */
531 static const BYTE direction_flag_code[] = {
532     0x55,                       /* push   %ebp */
533     0x89,0xe5,                  /* mov    %esp,%ebp */
534     0xfd,                       /* std */
535     0xfa,                       /* cli - cause exception */
536     0x5d,                       /* pop    %ebp */
537     0xc3,                       /* ret     */
538 };
539
540 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
541                                      CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
542 {
543 #ifdef __GNUC__
544     unsigned int flags;
545     __asm__("pushfl; popl %0; cld" : "=r" (flags) );
546     /* older windows versions don't clear DF properly so don't test */
547     if (flags & 0x400) trace( "eflags has DF bit set\n" );
548 #endif
549     ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
550     got_exception++;
551     context->Eip++;  /* skip cli */
552     context->EFlags &= ~0x400;  /* make sure it is cleared on return */
553     return ExceptionContinueExecution;
554 }
555
556 /* test single stepping over hardware breakpoint */
557 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
558                           CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
559 {
560     got_exception++;
561     ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
562         "wrong exception code: %x\n", rec->ExceptionCode);
563
564     if(got_exception == 1) {
565         /* hw bp exception on first nop */
566         ok( context->Eip == (DWORD)code_mem, "eip is wrong:  %x instead of %x\n",
567                                              context->Eip, (DWORD)code_mem);
568         ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
569         ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
570         context->Dr0 = context->Dr0 + 1;  /* set hw bp again on next instruction */
571         context->EFlags |= 0x100;       /* enable single stepping */
572     } else if(  got_exception == 2) {
573         /* single step exception on second nop */
574         ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
575                                                  context->Eip, (DWORD)code_mem + 1);
576         ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
577        /* depending on the win version the B0 bit is already set here as well
578         ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
579         context->EFlags |= 0x100;
580     } else if( got_exception == 3) {
581         /* hw bp exception on second nop */
582         ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
583                                                  context->Eip, (DWORD)code_mem + 1);
584         ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
585         ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
586         context->Dr0 = 0;       /* clear breakpoint */
587         context->EFlags |= 0x100;
588     } else {
589         /* single step exception on ret */
590         ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
591                                                  context->Eip, (DWORD)code_mem + 2);
592         ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
593         ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
594     }
595
596     context->Dr6 = 0;  /* clear status register */
597     return ExceptionContinueExecution;
598 }
599
600 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 };  /* nop, nop, ret */
601
602 /* test int3 handling */
603 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
604                            CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
605 {
606     ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
607                                            code_mem,  rec->ExceptionAddress);
608     ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
609     if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
610
611     return ExceptionContinueExecution;
612 }
613
614 static const BYTE int3_code[] = { 0xCC, 0xc3 };  /* int 3, ret */
615
616
617 static void test_exceptions(void)
618 {
619     CONTEXT ctx;
620     NTSTATUS res;
621
622     if (!pNtGetContextThread || !pNtSetContextThread)
623     {
624         skip( "NtGetContextThread/NtSetContextThread not found\n" );
625         return;
626     }
627
628     /* test handling of debug registers */
629     run_exception_test(dreg_handler, NULL, &segfault_code, sizeof(segfault_code), 0);
630
631     ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
632     res = pNtGetContextThread(GetCurrentThread(), &ctx);
633     ok (res == STATUS_SUCCESS,"NtGetContextThread failed with %x\n", res);
634     ok(ctx.Dr0 == 0x42424242,"failed to set debugregister 0 to 0x42424242, got %x\n", ctx.Dr0);
635     ok((ctx.Dr7 & ~0xdc00) == 0x155,"failed to set debugregister 7 to 0x155, got %x\n", ctx.Dr7);
636
637     /* test single stepping behavior */
638     got_exception = 0;
639     run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
640     ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
641
642     /* test alignment exceptions */
643     got_exception = 0;
644     run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
645     ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
646
647     /* test direction flag */
648     got_exception = 0;
649     run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
650     ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
651
652     /* test single stepping over hardware breakpoint */
653     memset(&ctx, 0, sizeof(ctx));
654     ctx.Dr0 = (DWORD) code_mem;  /* set hw bp on first nop */
655     ctx.Dr7 = 3;
656     ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
657     res = pNtSetContextThread( GetCurrentThread(), &ctx);
658     ok( res == STATUS_SUCCESS, "NtSetContextThread faild with %x\n", res);
659
660     got_exception = 0;
661     run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
662     ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
663
664     /* test int3 handling */
665     run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
666 }
667
668 static void test_debugger(void)
669 {
670     char cmdline[MAX_PATH];
671     PROCESS_INFORMATION pi;
672     STARTUPINFO si = { 0 };
673     DEBUG_EVENT de;
674     DWORD continuestatus;
675     PVOID code_mem_address = NULL;
676     NTSTATUS status;
677     SIZE_T size_read;
678     BOOL ret;
679     int counter = 0;
680     si.cb = sizeof(si);
681
682     if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
683     {
684         skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
685         return;
686     }
687
688     sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
689     ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
690     ok(ret, "could not create child process error: %u\n", GetLastError());
691     if (!ret)
692         return;
693
694     do
695     {
696         continuestatus = DBG_CONTINUE;
697         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
698
699         if (de.dwThreadId != pi.dwThreadId)
700         {
701             trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
702             ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
703             continue;
704         }
705
706         if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
707         {
708             if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
709             {
710                 skip("child process loaded at different address, terminating it\n");
711                 pNtTerminateProcess(pi.hProcess, 0);
712             }
713         }
714         else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
715         {
716             CONTEXT ctx;
717             int stage;
718
719             counter++;
720             status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
721                                           sizeof(code_mem_address), &size_read);
722             ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
723             status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
724                                           sizeof(stage), &size_read);
725             ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
726
727             ctx.ContextFlags = CONTEXT_FULL;
728             status = pNtGetContextThread(pi.hThread, &ctx);
729             ok(!status, "NtGetContextThread failed with 0x%x\n", status);
730
731             trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
732                   de.u.Exception.ExceptionRecord.ExceptionCode,
733                   de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
734
735             if (counter > 100)
736             {
737                 ok(FALSE, "got way too many exceptions, probaby caught in a infinite loop, terminating child\n");
738                 pNtTerminateProcess(pi.hProcess, 1);
739             }
740             else if (counter >= 2) /* skip startup breakpoint */
741             {
742                 if (stage == 1)
743                 {
744                     ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
745                        ctx.Eip, (char *)code_mem_address + 0xb);
746                     /* setting the context from debugger does not affect the context, the exception handlers gets */
747                     /* uncomment once wine is fixed */
748                     /* ctx.Eip = 0x12345; */
749                     ctx.Eax = 0xf00f00f1;
750
751                     /* let the debuggee handle the exception */
752                     continuestatus = DBG_EXCEPTION_NOT_HANDLED;
753                 }
754                 else if (stage == 2)
755                 {
756                     if (de.u.Exception.dwFirstChance)
757                     {
758                         /* debugger gets first chance exception with unmodified ctx.Eip */
759                         ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
760                             ctx.Eip, (char *)code_mem_address + 0xb);
761
762                         /* setting the context from debugger does not affect the context, the exception handlers gets */
763                         /* uncomment once wine is fixed */
764                         /* ctx.Eip = 0x12345; */
765                         ctx.Eax = 0xf00f00f1;
766
767                         /* pass exception to debuggee
768                          * exception will not be handled and
769                          * a second chance exception will be raised */
770                         continuestatus = DBG_EXCEPTION_NOT_HANDLED;
771                     }
772                     else
773                     {
774                         /* debugger gets context after exception handler has played with it */
775                         /* ctx.Eip is the same value the exception handler got */
776                         if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
777                         {
778                             ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
779                                broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
780                                "Eip at 0x%x instead of %p\n",
781                                 ctx.Eip, (char *)code_mem_address + 0xa);
782                             /* need to fixup Eip for debuggee */
783                             if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
784                                 ctx.Eip += 1;
785                         }
786                         else
787                             ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
788                                 ctx.Eip, (char *)code_mem_address + 0xb);
789                         /* here we handle exception */
790                     }
791                 }
792                 else
793                     ok(FALSE, "unexpected stage %x\n", stage);
794
795                 status = pNtSetContextThread(pi.hThread, &ctx);
796                 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
797             }
798         }
799
800         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
801
802     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
803
804     winetest_wait_child_process( pi.hProcess );
805     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
806     ok(CloseHandle(pi.hProcess) != 0, "error %u\n", GetLastError());
807
808     return;
809 }
810
811 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
812                                  CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
813 {
814     int *stage = *(int **)(frame + 1);
815
816     got_exception++;
817
818     if( *stage == 1) {
819         /* fault while executing sse instruction */
820         context->Eip += 3; /* skip addps */
821         return ExceptionContinueExecution;
822     }
823
824     /* stage 2 - divide by zero fault */
825     if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
826         skip("system doesn't support SIMD exceptions\n");
827     else {
828         ok( rec->ExceptionCode ==  STATUS_FLOAT_MULTIPLE_TRAPS,
829             "exception code: %#x, should be %#x\n",
830             rec->ExceptionCode,  STATUS_FLOAT_MULTIPLE_TRAPS);
831         ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
832             "# of params: %i, should be 1\n",
833             rec->NumberParameters);
834         if( rec->NumberParameters == 1 )
835             ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
836     }
837
838     context->Eip += 3; /* skip divps */
839
840     return ExceptionContinueExecution;
841 }
842
843 static const BYTE simd_exception_test[] = {
844     0x83, 0xec, 0x4,                     /* sub $0x4, %esp       */
845     0x0f, 0xae, 0x1c, 0x24,              /* stmxcsr (%esp)       */
846     0x66, 0x81, 0x24, 0x24, 0xff, 0xfd,  /* andw $0xfdff,(%esp)  * enable divide by */
847     0x0f, 0xae, 0x14, 0x24,              /* ldmxcsr (%esp)       * zero exceptions  */
848     0x6a, 0x01,                          /* push   $0x1          */
849     0x6a, 0x01,                          /* push   $0x1          */
850     0x6a, 0x01,                          /* push   $0x1          */
851     0x6a, 0x01,                          /* push   $0x1          */
852     0x0f, 0x10, 0x0c, 0x24,              /* movups (%esp),%xmm1  * fill dividend  */
853     0x0f, 0x57, 0xc0,                    /* xorps  %xmm0,%xmm0   * clear divisor  */
854     0x0f, 0x5e, 0xc8,                    /* divps  %xmm0,%xmm1   * generate fault */
855     0x83, 0xc4, 0x10,                    /* add    $0x10,%esp    */
856     0x66, 0x81, 0x0c, 0x24, 0x00, 0x02,  /* orw    $0x200,(%esp) * disable exceptions */
857     0x0f, 0xae, 0x14, 0x24,              /* ldmxcsr (%esp)       */
858     0x83, 0xc4, 0x04,                    /* add    $0x4,%esp     */
859     0xc3,                                /* ret */
860 };
861
862 static const BYTE sse_check[] = {
863     0x0f, 0x58, 0xc8,                    /* addps  %xmm0,%xmm1 */
864     0xc3,                                /* ret */
865 };
866
867 static void test_simd_exceptions(void)
868 {
869     int stage;
870
871     /* test if CPU & OS can do sse */
872     stage = 1;
873     got_exception = 0;
874     run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
875     if(got_exception) {
876         skip("system doesn't support SSE\n");
877         return;
878     }
879
880     /* generate a SIMD exception */
881     stage = 2;
882     got_exception = 0;
883     run_exception_test(simd_fault_handler, &stage, simd_exception_test,
884                        sizeof(simd_exception_test), 0);
885     ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
886 }
887
888 struct fpu_exception_info
889 {
890     DWORD exception_code;
891     DWORD exception_offset;
892     DWORD eip_offset;
893 };
894
895 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
896         CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
897 {
898     struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
899
900     info->exception_code = rec->ExceptionCode;
901     info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
902     info->eip_offset = context->Eip - (DWORD)code_mem;
903
904     ++context->Eip;
905     return ExceptionContinueExecution;
906 }
907
908 static void test_fpu_exceptions(void)
909 {
910     static const BYTE fpu_exception_test_ie[] =
911     {
912         0x83, 0xec, 0x04,                   /* sub $0x4,%esp        */
913         0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp)   */
914         0x9b, 0xd9, 0x7c, 0x24, 0x02,       /* fstcw 0x2(%esp)      */
915         0xd9, 0x2c, 0x24,                   /* fldcw (%esp)         */
916         0xd9, 0xee,                         /* fldz                 */
917         0xd9, 0xe8,                         /* fld1                 */
918         0xde, 0xf1,                         /* fdivp                */
919         0xdd, 0xd8,                         /* fstp %st(0)          */
920         0xdd, 0xd8,                         /* fstp %st(0)          */
921         0x9b,                               /* fwait                */
922         0xdb, 0xe2,                         /* fnclex               */
923         0xd9, 0x6c, 0x24, 0x02,             /* fldcw 0x2(%esp)      */
924         0x83, 0xc4, 0x04,                   /* add $0x4,%esp        */
925         0xc3,                               /* ret                  */
926     };
927
928     static const BYTE fpu_exception_test_de[] =
929     {
930         0x83, 0xec, 0x04,                   /* sub $0x4,%esp        */
931         0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp)   */
932         0x9b, 0xd9, 0x7c, 0x24, 0x02,       /* fstcw 0x2(%esp)      */
933         0xd9, 0x2c, 0x24,                   /* fldcw (%esp)         */
934         0xdd, 0xd8,                         /* fstp %st(0)          */
935         0xd9, 0xee,                         /* fldz                 */
936         0xd9, 0xe8,                         /* fld1                 */
937         0xde, 0xf1,                         /* fdivp                */
938         0x9b,                               /* fwait                */
939         0xdb, 0xe2,                         /* fnclex               */
940         0xdd, 0xd8,                         /* fstp %st(0)          */
941         0xdd, 0xd8,                         /* fstp %st(0)          */
942         0xd9, 0x6c, 0x24, 0x02,             /* fldcw 0x2(%esp)      */
943         0x83, 0xc4, 0x04,                   /* add $0x4,%esp        */
944         0xc3,                               /* ret                  */
945     };
946
947     struct fpu_exception_info info;
948
949     memset(&info, 0, sizeof(info));
950     run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
951     ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
952             "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
953     ok(info.exception_offset == 0x19 ||
954        broken( is_wow64 && info.exception_offset == info.eip_offset ),
955        "Got exception offset %#x, expected 0x19\n", info.exception_offset);
956     ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
957
958     memset(&info, 0, sizeof(info));
959     run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
960     ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
961             "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
962     ok(info.exception_offset == 0x17 ||
963        broken( is_wow64 && info.exception_offset == info.eip_offset ),
964        "Got exception offset %#x, expected 0x17\n", info.exception_offset);
965     ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
966 }
967
968 struct dpe_exception_info {
969     BOOL exception_caught;
970     DWORD exception_info;
971 };
972
973 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
974         CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
975 {
976     DWORD old_prot;
977     struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
978
979     ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
980        "Exception code %08x\n", rec->ExceptionCode);
981     ok(rec->NumberParameters == 2,
982        "Parameter count: %d\n", rec->NumberParameters);
983     ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
984        "Exception address: %p, expected %p\n",
985        (LPVOID)rec->ExceptionInformation[1], code_mem);
986
987     info->exception_info = rec->ExceptionInformation[0];
988     info->exception_caught = TRUE;
989
990     VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
991     return ExceptionContinueExecution;
992 }
993
994 static void test_dpe_exceptions(void)
995 {
996     static char single_ret[] = {0xC3};
997     struct dpe_exception_info info;
998     NTSTATUS stat;
999     BOOL has_hw_support;
1000     BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1001     DWORD val;
1002     ULONG len;
1003
1004     /* Query DEP with len to small */
1005     stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1006     if(stat == STATUS_INVALID_INFO_CLASS)
1007     {
1008         skip("This software platform does not support DEP\n");
1009         return;
1010     }
1011     ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1012
1013     /* Query DEP */
1014     stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1015     ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1016     if(stat == STATUS_SUCCESS)
1017     {
1018         ok(len == sizeof val, "returned length: %d\n", len);
1019         if(val & MEM_EXECUTE_OPTION_PERMANENT)
1020         {
1021             skip("toggling DEP impossible - status locked\n");
1022             is_permanent = TRUE;
1023             if(val & MEM_EXECUTE_OPTION_DISABLE)
1024                 can_test_without = FALSE;
1025             else
1026                 can_test_with = FALSE;
1027         }
1028     }
1029
1030     if(!is_permanent)
1031     {
1032         /* Enable DEP */
1033         val = MEM_EXECUTE_OPTION_DISABLE;
1034         stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1035         ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1036     }
1037
1038     if(can_test_with)
1039     {
1040         /* Try access to locked page with DEP on*/
1041         info.exception_caught = FALSE;
1042         run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1043         ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1044         ok(info.exception_info == EXCEPTION_READ_FAULT ||
1045            info.exception_info == EXCEPTION_EXECUTE_FAULT,
1046               "Access violation type: %08x\n", (unsigned)info.exception_info);
1047         has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1048         trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1049
1050         /* Try execution of data with DEP on*/
1051         info.exception_caught = FALSE;
1052         run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1053         if(has_hw_support)
1054         {
1055             ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1056             ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1057                   "Access violation type: %08x\n", (unsigned)info.exception_info);
1058         }
1059         else
1060             ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1061     }
1062     else
1063         skip("DEP is in AlwaysOff state\n");
1064
1065     if(!is_permanent)
1066     {
1067         /* Disable DEP */
1068         val = MEM_EXECUTE_OPTION_ENABLE;
1069         stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1070         ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1071     }
1072
1073     /* page is read without exec here */
1074     if(can_test_without)
1075     {
1076         /* Try execution of data with DEP off */
1077         info.exception_caught = FALSE;
1078         run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1079         ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1080
1081         /* Try access to locked page with DEP off - error code is different than
1082            with hardware DEP on */
1083         info.exception_caught = FALSE;
1084         run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1085         ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1086         ok(info.exception_info == EXCEPTION_READ_FAULT,
1087               "Access violation type: %08x\n", (unsigned)info.exception_info);
1088     }
1089     else
1090         skip("DEP is in AlwaysOn state\n");
1091
1092     if(!is_permanent)
1093     {
1094         /* Turn off DEP permanently */
1095         val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1096         stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1097         ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1098     }
1099
1100     /* Try to turn off DEP */
1101     val = MEM_EXECUTE_OPTION_ENABLE;
1102     stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1103     ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1104
1105     /* Try to turn on DEP */
1106     val = MEM_EXECUTE_OPTION_DISABLE;
1107     stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1108     ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1109 }
1110
1111 #elif defined(__x86_64__)
1112
1113 #define UNW_FLAG_NHANDLER  0
1114 #define UNW_FLAG_EHANDLER  1
1115 #define UNW_FLAG_UHANDLER  2
1116 #define UNW_FLAG_CHAININFO 4
1117
1118 #define UWOP_PUSH_NONVOL     0
1119 #define UWOP_ALLOC_LARGE     1
1120 #define UWOP_ALLOC_SMALL     2
1121 #define UWOP_SET_FPREG       3
1122 #define UWOP_SAVE_NONVOL     4
1123 #define UWOP_SAVE_NONVOL_FAR 5
1124 #define UWOP_SAVE_XMM128     8
1125 #define UWOP_SAVE_XMM128_FAR 9
1126 #define UWOP_PUSH_MACHFRAME  10
1127
1128 struct results
1129 {
1130     int rip_offset;   /* rip offset from code start */
1131     int rbp_offset;   /* rbp offset from stack pointer */
1132     int handler;      /* expect handler to be set? */
1133     int rip;          /* expected final rip value */
1134     int frame;        /* expected frame return value */
1135     int regs[8][2];   /* expected values for registers */
1136 };
1137
1138 struct unwind_test
1139 {
1140     const BYTE *function;
1141     size_t function_size;
1142     const BYTE *unwind_info;
1143     const struct results *results;
1144     unsigned int nb_results;
1145 };
1146
1147 enum regs
1148 {
1149     rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1150     r8,  r9,  r10, r11, r12, r13, r14, r15
1151 };
1152
1153 static const char * const reg_names[16] =
1154 {
1155     "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1156     "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15"
1157 };
1158
1159 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1160
1161 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1162 {
1163     static const int code_offset = 1024;
1164     static const int unwind_offset = 2048;
1165     void *handler, *data;
1166     CONTEXT context;
1167     RUNTIME_FUNCTION runtime_func;
1168     KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1169     UINT i, j, k;
1170     ULONG64 fake_stack[256];
1171     ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1172     UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1173
1174     memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1175     memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1176
1177     runtime_func.BeginAddress = code_offset;
1178     runtime_func.EndAddress = code_offset + test->function_size;
1179     runtime_func.UnwindData = unwind_offset;
1180
1181     trace( "code: %p stack: %p\n", code_mem, fake_stack );
1182
1183     for (i = 0; i < test->nb_results; i++)
1184     {
1185         memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1186         memset( &context, 0x55, sizeof(context) );
1187         memset( &unset_reg, 0x55, sizeof(unset_reg) );
1188         for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1189
1190         context.Rsp = (ULONG_PTR)fake_stack;
1191         context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1192         orig_rbp = context.Rbp;
1193         orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1194
1195         trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1196                (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1197
1198         data = (void *)0xdeadbeef;
1199         handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1200                                     &runtime_func, &context, &data, &frame, &ctx_ptr );
1201         if (test->results[i].handler)
1202         {
1203             ok( (char *)handler == (char *)code_mem + 0x200,
1204                 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1205             if (handler) ok( *(DWORD *)data == 0x08070605,
1206                              "%u/%u: wrong handler data %p\n", testnum, i, data );
1207         }
1208         else
1209         {
1210             ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1211             ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1212         }
1213
1214         ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1215             testnum, i, (void *)context.Rip, test->results[i].rip );
1216         ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1217             testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1218
1219         for (j = 0; j < 16; j++)
1220         {
1221             static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1222
1223             for (k = 0; k < nb_regs; k++)
1224             {
1225                 if (test->results[i].regs[k][0] == -1)
1226                 {
1227                     k = nb_regs;
1228                     break;
1229                 }
1230                 if (test->results[i].regs[k][0] == j) break;
1231             }
1232
1233             if (j == rsp)  /* rsp is special */
1234             {
1235                 ok( !ctx_ptr.u2.IntegerContext[j],
1236                     "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1237                 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1238                     "%u/%u: register rsp wrong %p/%p\n",
1239                     testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1240                 continue;
1241             }
1242
1243             if (ctx_ptr.u2.IntegerContext[j])
1244             {
1245                 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1246                     testnum, i, reg_names[j], *(&context.Rax + j) );
1247                 if (k < nb_regs)
1248                     ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1249                         "%u/%u: register %s wrong %p/%x\n",
1250                         testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1251             }
1252             else
1253             {
1254                 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1255                 if (j == rbp)
1256                     ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1257                         testnum, i, (void *)context.Rbp );
1258                 else
1259                     ok( *(&context.Rax + j) == unset_reg,
1260                         "%u/%u: register %s wrong %p/unset\n",
1261                         testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1262             }
1263         }
1264     }
1265 }
1266
1267 static void test_virtual_unwind(void)
1268 {
1269     static const BYTE function_0[] =
1270     {
1271         0xff, 0xf5,                                  /* 00: push %rbp */
1272         0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00,    /* 02: sub $0x110,%rsp */
1273         0x48, 0x8d, 0x6c, 0x24, 0x30,                /* 09: lea 0x30(%rsp),%rbp */
1274         0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00,    /* 0e: mov %rbx,0xf0(%rbp) */
1275         0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00,    /* 15: mov %rsi,0xf8(%rbp) */
1276         0x90,                                        /* 1c: nop */
1277         0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00,    /* 1d: mov 0xf0(%rbp),%rbx */
1278         0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00,    /* 24: mov 0xf8(%rbp),%rsi */
1279         0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00,    /* 2b: lea 0xe0(%rbp),%rsp */
1280         0x5d,                                        /* 32: pop %rbp */
1281         0xc3                                         /* 33: ret */
1282     };
1283
1284     static const BYTE unwind_info_0[] =
1285     {
1286         1 | (UNW_FLAG_EHANDLER << 3),  /* version + flags */
1287         0x1c,                          /* prolog size */
1288         8,                             /* opcode count */
1289         (0x03 << 4) | rbp,             /* frame reg rbp offset 0x30 */
1290
1291         0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1292         0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1293         0x0e, UWOP(SET_FPREG, rbp),            /* 0e: lea 0x30(%rsp),rbp */
1294         0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0,   /* 09: sub $0x110,%rsp */
1295         0x02, UWOP(PUSH_NONVOL, rbp),          /* 02: push %rbp */
1296
1297         0x00, 0x02, 0x00, 0x00,  /* handler */
1298         0x05, 0x06, 0x07, 0x08,  /* data */
1299     };
1300
1301     static const struct results results_0[] =
1302     {
1303       /* offset  rbp   handler  rip   frame   registers */
1304         { 0x00,  0x40,  FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1305         { 0x02,  0x40,  FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1306         { 0x09,  0x40,  FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1307         { 0x0e,  0x40,  FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1308         { 0x15,  0x40,  FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1309         { 0x1c,  0x40,  TRUE,  0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1310         { 0x1d,  0x40,  TRUE,  0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1311         { 0x24,  0x40,  TRUE,  0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1312         { 0x2b,  0x40,  FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1313         { 0x32,  0x40,  FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1314         { 0x33,  0x40,  FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1315     };
1316
1317
1318     static const BYTE function_1[] =
1319     {
1320         0x53,                     /* 00: push %rbx */
1321         0x55,                     /* 01: push %rbp */
1322         0x56,                     /* 02: push %rsi */
1323         0x57,                     /* 03: push %rdi */
1324         0x41, 0x54,               /* 04: push %r12 */
1325         0x48, 0x83, 0xec, 0x30,   /* 06: sub $0x30,%rsp */
1326         0x90, 0x90,               /* 0a: nop; nop */
1327         0x48, 0x83, 0xc4, 0x30,   /* 0c: add $0x30,%rsp */
1328         0x41, 0x5c,               /* 10: pop %r12 */
1329         0x5f,                     /* 12: pop %rdi */
1330         0x5e,                     /* 13: pop %rsi */
1331         0x5d,                     /* 14: pop %rbp */
1332         0x5b,                     /* 15: pop %rbx */
1333         0xc3                      /* 16: ret */
1334      };
1335
1336     static const BYTE unwind_info_1[] =
1337     {
1338         1 | (UNW_FLAG_EHANDLER << 3),  /* version + flags */
1339         0x0a,                          /* prolog size */
1340         6,                             /* opcode count */
1341         0,                             /* frame reg */
1342
1343         0x0a, UWOP(ALLOC_SMALL, 5),   /* 0a: sub $0x30,%rsp */
1344         0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1345         0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1346         0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1347         0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1348         0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1349
1350         0x00, 0x02, 0x00, 0x00,  /* handler */
1351         0x05, 0x06, 0x07, 0x08,  /* data */
1352     };
1353
1354     static const struct results results_1[] =
1355     {
1356       /* offset  rbp   handler  rip   frame   registers */
1357         { 0x00,  0x50,  FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1358         { 0x01,  0x50,  FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1359         { 0x02,  0x50,  FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1360         { 0x03,  0x50,  FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1361         { 0x04,  0x50,  FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1362         { 0x06,  0x50,  FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1363         { 0x0a,  0x50,  TRUE,  0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1364         { 0x0c,  0x50,  FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1365         { 0x10,  0x50,  FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1366         { 0x12,  0x50,  FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1367         { 0x13,  0x50,  FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1368         { 0x14,  0x50,  FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1369         { 0x15,  0x50,  FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1370         { 0x16,  0x50,  FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1371     };
1372
1373     static const struct unwind_test tests[] =
1374     {
1375         { function_0, sizeof(function_0), unwind_info_0,
1376           results_0, sizeof(results_0)/sizeof(results_0[0]) },
1377         { function_1, sizeof(function_1), unwind_info_1,
1378           results_1, sizeof(results_1)/sizeof(results_1[0]) }
1379     };
1380     unsigned int i;
1381
1382     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1383         call_virtual_unwind( i, &tests[i] );
1384 }
1385
1386 #endif  /* __x86_64__ */
1387
1388 START_TEST(exception)
1389 {
1390     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1391
1392     code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1393     if(!code_mem) {
1394         trace("VirtualAlloc failed\n");
1395         return;
1396     }
1397
1398     pNtCurrentTeb        = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1399     pNtGetContextThread  = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1400     pNtSetContextThread  = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1401     pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1402     pRtlRaiseException   = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1403     pNtTerminateProcess  = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1404     pRtlAddVectoredExceptionHandler    = (void *)GetProcAddress( hntdll,
1405                                                                  "RtlAddVectoredExceptionHandler" );
1406     pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1407                                                                  "RtlRemoveVectoredExceptionHandler" );
1408     pNtQueryInformationProcess         = (void*)GetProcAddress( hntdll,
1409                                                                  "NtQueryInformationProcess" );
1410     pNtSetInformationProcess           = (void*)GetProcAddress( hntdll,
1411                                                                  "NtSetInformationProcess" );
1412     pIsWow64Process = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process");
1413
1414 #ifdef __i386__
1415     if (!pNtCurrentTeb)
1416     {
1417         skip( "NtCurrentTeb not found\n" );
1418         return;
1419     }
1420     if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1421
1422     if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1423         have_vectored_api = TRUE;
1424     else
1425         skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1426
1427     my_argc = winetest_get_mainargs( &my_argv );
1428     if (my_argc >= 4)
1429     {
1430         void *addr;
1431         sscanf( my_argv[3], "%p", &addr );
1432
1433         if (addr != &test_stage)
1434         {
1435             skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1436             return;
1437         }
1438
1439         /* child must be run under a debugger */
1440         if (!pNtCurrentTeb()->Peb->BeingDebugged)
1441         {
1442             ok(FALSE, "child process not being debugged?\n");
1443             return;
1444         }
1445
1446         if (pRtlRaiseException)
1447         {
1448             test_stage = 1;
1449             run_rtlraiseexception_test(0x12345);
1450             run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1451             run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1452             test_stage = 2;
1453             run_rtlraiseexception_test(0x12345);
1454             run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1455             run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1456         }
1457         else
1458             skip( "RtlRaiseException not found\n" );
1459
1460         /* rest of tests only run in parent */
1461         return;
1462     }
1463
1464     test_prot_fault();
1465     test_exceptions();
1466     test_rtlraiseexception();
1467     test_debugger();
1468     test_simd_exceptions();
1469     test_fpu_exceptions();
1470     test_dpe_exceptions();
1471
1472 #elif defined(__x86_64__)
1473
1474     test_virtual_unwind();
1475
1476 #endif
1477
1478     VirtualFree(code_mem, 0, MEM_FREE);
1479 }