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