hlink/tests: Sign compare fix.
[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 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winnt.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "excpt.h"
36 #include "wine/test.h"
37
38 #ifdef __i386__
39 static int      my_argc;
40 static char**   my_argv;
41 static int      test_stage;
42
43 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
44 static NTSTATUS  (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
45 static NTSTATUS  (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
46 static NTSTATUS  (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
47 static PVOID     (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
48 static ULONG     (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
49 static NTSTATUS  (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
50 static NTSTATUS  (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
51 static void *code_mem;
52
53 /* Test various instruction combinations that cause a protection fault on the i386,
54  * and check what the resulting exception looks like.
55  */
56
57 static const struct exception
58 {
59     BYTE     code[18];   /* asm code */
60     BYTE     offset;     /* offset of faulting instruction */
61     BYTE     length;     /* length of faulting instruction */
62     NTSTATUS status;     /* expected status code */
63     DWORD    nb_params;  /* expected number of parameters */
64     DWORD    params[4];  /* expected parameters */
65 } exceptions[] =
66 {
67     /* test some privileged instructions */
68     { { 0xfb, 0xc3 },  /* 0: sti; ret */
69       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
70     { { 0x6c, 0xc3 },  /* 1: insb (%dx); ret */
71       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
72     { { 0x6d, 0xc3 },  /* 2: insl (%dx); ret */
73       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
74     { { 0x6e, 0xc3 },  /* 3: outsb (%dx); ret */
75       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
76     { { 0x6f, 0xc3 },  /* 4: outsl (%dx); ret */
77       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
78     { { 0xe4, 0x11, 0xc3 },  /* 5: inb $0x11,%al; ret */
79       0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
80     { { 0xe5, 0x11, 0xc3 },  /* 6: inl $0x11,%eax; ret */
81       0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
82     { { 0xe6, 0x11, 0xc3 },  /* 7: outb %al,$0x11; ret */
83       0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
84     { { 0xe7, 0x11, 0xc3 },  /* 8: outl %eax,$0x11; ret */
85       0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
86     { { 0xed, 0xc3 },  /* 9: inl (%dx),%eax; ret */
87       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
88     { { 0xee, 0xc3 },  /* 10: outb %al,(%dx); ret */
89       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
90     { { 0xef, 0xc3 },  /* 11: outl %eax,(%dx); ret */
91       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
92     { { 0xf4, 0xc3 },  /* 12: hlt; ret */
93       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
94     { { 0xfa, 0xc3 },  /* 13: cli; ret */
95       0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
96
97     /* test long jump to invalid selector */
98     { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 },  /* 14: ljmp $0,$0; ret */
99       0, 7, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
100
101     /* test iret to invalid selector */
102     { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
103       /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
104       6, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
105
106     /* test loading an invalid selector */
107     { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 },  /* 16: mov $beef,%ax; mov %ax,%gs; ret */
108       5, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
109
110     /* test accessing a zero selector */
111     { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
112       /* 17: push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
113       5, 6, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
114
115     /* test moving %cs -> %ss */
116     { { 0x0e, 0x17, 0x58, 0xc3 },  /* 18: pushl %cs; popl %ss; popl %eax; ret */
117       1, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
118
119     /* 19: test overlong instruction (limit is 16 bytes) */
120     { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
121       0, 16, STATUS_ILLEGAL_INSTRUCTION, 0 },
122     { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
123       0, 15, STATUS_PRIVILEGED_INSTRUCTION, 0 },
124
125     /* test invalid interrupt */
126     { { 0xcd, 0xff, 0xc3 },   /* 21: int $0xff; ret */
127       0, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
128
129     /* test moves to/from Crx */
130     { { 0x0f, 0x20, 0xc0, 0xc3 },  /* 22: movl %cr0,%eax; ret */
131       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
132     { { 0x0f, 0x20, 0xe0, 0xc3 },  /* 23: movl %cr4,%eax; ret */
133       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
134     { { 0x0f, 0x22, 0xc0, 0xc3 },  /* 24: movl %eax,%cr0; ret */
135       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
136     { { 0x0f, 0x22, 0xe0, 0xc3 },  /* 25: movl %eax,%cr4; ret */
137       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
138
139     /* test moves to/from Drx */
140     { { 0x0f, 0x21, 0xc0, 0xc3 },  /* 26: movl %dr0,%eax; ret */
141       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
142     { { 0x0f, 0x21, 0xc8, 0xc3 },  /* 27: movl %dr1,%eax; ret */
143       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
144     { { 0x0f, 0x21, 0xf8, 0xc3 },  /* 28: movl %dr7,%eax; ret */
145       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
146     { { 0x0f, 0x23, 0xc0, 0xc3 },  /* 29: movl %eax,%dr0; ret */
147       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
148     { { 0x0f, 0x23, 0xc8, 0xc3 },  /* 30: movl %eax,%dr1; ret */
149       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
150     { { 0x0f, 0x23, 0xf8, 0xc3 },  /* 31: movl %eax,%dr7; ret */
151       0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
152
153     /* test memory reads */
154     { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 },  /* 32: movl 0xfffffffc,%eax; ret */
155       0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
156     { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 },  /* 33: movl 0xfffffffd,%eax; ret */
157       0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
158     { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 },  /* 34: movl 0xfffffffe,%eax; ret */
159       0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
160     { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 },  /* 35: movl 0xffffffff,%eax; ret */
161       0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
162
163     /* test memory writes */
164     { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 },  /* 36: movl %eax,0xfffffffc; ret */
165       0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
166     { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 },  /* 37: movl %eax,0xfffffffd; ret */
167       0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
168     { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 },  /* 38: movl %eax,0xfffffffe; ret */
169       0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
170     { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 },  /* 39: movl %eax,0xffffffff; ret */
171       0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
172
173     /* 40: test exception with cleared %ds and %es */
174     { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
175           /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
176       8, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
177 };
178
179 static int got_exception;
180 static BOOL have_vectored_api;
181
182 static void run_exception_test(void *handler, const void* context,
183                                const void *code, unsigned int code_size)
184 {
185     struct {
186         EXCEPTION_REGISTRATION_RECORD frame;
187         const void *context;
188     } exc_frame;
189     void (*func)(void) = code_mem;
190
191     exc_frame.frame.Handler = handler;
192     exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
193     exc_frame.context = context;
194
195     memcpy(code_mem, code, code_size);
196
197     pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
198     func();
199     pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
200 }
201
202 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
203 {
204     PCONTEXT context = ExceptionInfo->ContextRecord;
205     PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
206     trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
207           rec->ExceptionAddress, context->Eip);
208
209     ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
210        rec->ExceptionAddress, (char *)code_mem + 0xb);
211
212     if (pNtCurrentTeb()->Peb->BeingDebugged)
213         ok((void *)context->Eax == pRtlRaiseException, "debugger managed to modify Eax to %x should be %p\n",
214            context->Eax, pRtlRaiseException);
215
216     /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
217      * even if raised by RtlRaiseException
218      */
219     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
220     {
221         ok(context->Eip == (DWORD)code_mem + 0xa ||
222            broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
223            "Eip at %x instead of %x or %x\n", context->Eip,
224            (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
225     }
226     else
227     {
228         ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
229            context->Eip, (DWORD)code_mem + 0xb);
230     }
231
232     /* test if context change is preserved from vectored handler to stack handlers */
233     context->Eax = 0xf00f00f0;
234     return EXCEPTION_CONTINUE_SEARCH;
235 }
236
237 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
238                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
239 {
240     trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
241            rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
242
243     ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
244        rec->ExceptionAddress, (char *)code_mem + 0xb);
245
246     /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
247      * even if raised by RtlRaiseException
248      */
249     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
250     {
251         ok(context->Eip == (DWORD)code_mem + 0xa ||
252            broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
253            "Eip at %x instead of %x or %x\n", context->Eip,
254            (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
255     }
256     else
257     {
258         ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
259            context->Eip, (DWORD)code_mem + 0xb);
260     }
261
262     if(have_vectored_api)
263         ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
264            context->Eax);
265
266     /* give the debugger a chance to examine the state a second time */
267     /* without the exception handler changing Eip */
268     if (test_stage == 2)
269         return ExceptionContinueSearch;
270
271     /* Eip in context is decreased by 1
272      * Increase it again, else execution will continue in the middle of a instruction */
273     if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
274         context->Eip += 1;
275     return ExceptionContinueExecution;
276 }
277
278
279 static const BYTE call_one_arg_code[] = {
280         0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
281         0x50,                   /* push %eax */
282         0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
283         0xff, 0xd0,             /* call *%eax */
284         0x90,                   /* nop */
285         0x90,                   /* nop */
286         0x90,                   /* nop */
287         0x90,                   /* nop */
288         0xc3,                   /* ret */
289 };
290
291
292 static void run_rtlraiseexception_test(DWORD exceptioncode)
293 {
294     EXCEPTION_REGISTRATION_RECORD frame;
295     EXCEPTION_RECORD record;
296     PVOID vectored_handler = NULL;
297
298     void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
299
300     record.ExceptionCode = exceptioncode;
301     record.ExceptionFlags = 0;
302     record.ExceptionRecord = NULL;
303     record.ExceptionAddress = NULL; /* does not matter, copied return address */
304     record.NumberParameters = 0;
305
306     frame.Handler = rtlraiseexception_handler;
307     frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
308
309     memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
310
311     pNtCurrentTeb()->Tib.ExceptionList = &frame;
312     if (have_vectored_api)
313     {
314         vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
315         ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
316     }
317
318     func(pRtlRaiseException, &record);
319     ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
320         "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
321
322     if (have_vectored_api)
323         pRtlRemoveVectoredExceptionHandler(vectored_handler);
324     pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
325 }
326
327 static void test_rtlraiseexception(void)
328 {
329     if (!pRtlRaiseException)
330     {
331         skip("RtlRaiseException not found\n");
332         return;
333     }
334
335     /* test without debugger */
336     run_rtlraiseexception_test(0x12345);
337     run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
338     run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
339 }
340
341 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
342                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
343 {
344     const struct exception *except = *(const struct exception **)(frame + 1);
345     unsigned int i, entry = except - exceptions;
346
347     got_exception++;
348     trace( "exception: %x flags:%x addr:%p\n",
349            rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
350
351     ok( rec->ExceptionCode == except->status,
352         "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
353     ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
354         "%u: Wrong exception address %p/%p\n", entry,
355         rec->ExceptionAddress, (char*)code_mem + except->offset );
356
357     ok( rec->NumberParameters == except->nb_params,
358         "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
359
360     /* Most CPUs (except Intel Core apparently) report a segment limit violation */
361     /* instead of page faults for accesses beyond 0xffffffff */
362     if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
363     {
364         if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
365             goto skip_params;
366     }
367
368     /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
369     if (except->nb_params == 2 && rec->NumberParameters == 2
370         && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
371         && except->params[0] == rec->ExceptionInformation[0])
372     {
373         goto skip_params;
374     }
375
376     for (i = 0; i < rec->NumberParameters; i++)
377         ok( rec->ExceptionInformation[i] == except->params[i],
378             "%u: Wrong parameter %d: %lx/%x\n",
379             entry, i, rec->ExceptionInformation[i], except->params[i] );
380
381 skip_params:
382     /* don't handle exception if it's not the address we expected */
383     if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
384
385     context->Eip += except->length;
386     return ExceptionContinueExecution;
387 }
388
389 static void test_prot_fault(void)
390 {
391     unsigned int i;
392
393     for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
394     {
395         got_exception = 0;
396         run_exception_test(handler, &exceptions[i], &exceptions[i].code,
397                            sizeof(exceptions[i].code));
398         if (!i && !got_exception)
399         {
400             trace( "No exception, assuming win9x, no point in testing further\n" );
401             break;
402         }
403         ok( got_exception == (exceptions[i].status != 0),
404             "%u: bad exception count %d\n", i, got_exception );
405     }
406 }
407
408 /* test handling of debug registers */
409 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
410                       CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
411 {
412     context->Eip += 2;  /* Skips the popl (%eax) */
413     context->Dr0 = 0x42424242;
414     context->Dr1 = 0;
415     context->Dr2 = 0;
416     context->Dr3 = 0;
417     context->Dr6 = 0;
418     context->Dr7 = 0x155;
419     return ExceptionContinueExecution;
420 }
421
422 static const BYTE segfault_code[5] = {
423         0x31, 0xc0, /* xor    %eax,%eax */
424         0x8f, 0x00, /* popl   (%eax) - cause exception */
425         0xc3        /* ret */
426 };
427
428 /* test the single step exception behaviour */
429 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
430                                   CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
431 {
432     got_exception++;
433     ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
434
435     if( got_exception < 3)
436         context->EFlags |= 0x100;  /* single step until popf instruction */
437     else {
438         /* show that the last single step exception on the popf instruction
439          * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
440         ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
441             "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
442     }
443
444     return ExceptionContinueExecution;
445 }
446
447 static const BYTE single_stepcode[] = {
448     0x9c,               /* pushf */
449     0x58,               /* pop   %eax */
450     0x0d,0,1,0,0,       /* or    $0x100,%eax */
451     0x50,               /* push   %eax */
452     0x9d,               /* popf    */
453     0x35,0,1,0,0,       /* xor    $0x100,%eax */
454     0x50,               /* push   %eax */
455     0x9d,               /* popf    */
456     0xc3
457 };
458
459 /* Test the alignment check (AC) flag handling. */
460 static const BYTE align_check_code[] = {
461     0x55,                       /* push   %ebp */
462     0x89,0xe5,                  /* mov    %esp,%ebp */
463     0x9c,                       /* pushf   */
464     0x58,                       /* pop    %eax */
465     0x0d,0,0,4,0,               /* or     $0x40000,%eax */
466     0x50,                       /* push   %eax */
467     0x9d,                       /* popf    */
468     0x89,0xe0,                  /* mov %esp, %eax */
469     0x8b,0x40,0x1,              /* mov 0x1(%eax), %eax - cause exception */
470     0x9c,                       /* pushf   */
471     0x58,                       /* pop    %eax */
472     0x35,0,0,4,0,               /* xor    $0x40000,%eax */
473     0x50,                       /* push   %eax */
474     0x9d,                       /* popf    */
475     0x5d,                       /* pop    %ebp */
476     0xc3,                       /* ret     */
477 };
478
479 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
480                                   CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
481 {
482     ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
483     got_exception++;
484     return ExceptionContinueExecution;
485 }
486
487 /* Test the direction flag handling. */
488 static const BYTE direction_flag_code[] = {
489     0x55,                       /* push   %ebp */
490     0x89,0xe5,                  /* mov    %esp,%ebp */
491     0xfd,                       /* std */
492     0xfa,                       /* cli - cause exception */
493     0x5d,                       /* pop    %ebp */
494     0xc3,                       /* ret     */
495 };
496
497 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
498                                      CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
499 {
500 #ifdef __GNUC__
501     unsigned int flags;
502     __asm__("pushfl; popl %0; cld" : "=r" (flags) );
503     /* older windows versions don't clear DF properly so don't test */
504     if (flags & 0x400) trace( "eflags has DF bit set\n" );
505 #endif
506     ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
507     got_exception++;
508     context->Eip++;  /* skip cli */
509     context->EFlags &= ~0x400;  /* make sure it is cleared on return */
510     return ExceptionContinueExecution;
511 }
512
513 /* test single stepping over hardware breakpoint */
514 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
515                           CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
516 {
517     got_exception++;
518     ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
519         "wrong exception code: %x\n", rec->ExceptionCode);
520
521     if(got_exception == 1) {
522         /* hw bp exception on first nop */
523         ok( context->Eip == (DWORD)code_mem, "eip is wrong:  %x instead of %x\n",
524                                              context->Eip, (DWORD)code_mem);
525         ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
526         ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
527         context->Dr0 = context->Dr0 + 1;  /* set hw bp again on next instruction */
528         context->EFlags |= 0x100;       /* enable single stepping */
529     } else if(  got_exception == 2) {
530         /* single step exception on second nop */
531         ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
532                                                  context->Eip, (DWORD)code_mem + 1);
533         ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
534        /* depending on the win version the B0 bit is already set here as well
535         ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
536         context->EFlags |= 0x100;
537     } else if( got_exception == 3) {
538         /* hw bp exception on second nop */
539         ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
540                                                  context->Eip, (DWORD)code_mem + 1);
541         ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
542         ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
543         context->Dr0 = 0;       /* clear breakpoint */
544         context->EFlags |= 0x100;
545     } else {
546         /* single step exception on ret */
547         ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
548                                                  context->Eip, (DWORD)code_mem + 2);
549         ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
550         ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
551     }
552
553     context->Dr6 = 0;  /* clear status register */
554     return ExceptionContinueExecution;
555 }
556
557 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 };  /* nop, nop, ret */
558
559 /* test int3 handling */
560 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
561                            CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
562 {
563     ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
564                                            code_mem,  rec->ExceptionAddress);
565     ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
566     if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
567
568     return ExceptionContinueExecution;
569 }
570
571 static const BYTE int3_code[] = { 0xCC, 0xc3 };  /* int 3, ret */
572
573
574 static void test_exceptions(void)
575 {
576     CONTEXT ctx;
577     NTSTATUS res;
578
579     if (!pNtGetContextThread || !pNtSetContextThread)
580     {
581         skip( "NtGetContextThread/NtSetContextThread not found\n" );
582         return;
583     }
584
585     /* test handling of debug registers */
586     run_exception_test(dreg_handler, NULL, &segfault_code, sizeof(segfault_code));
587
588     ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
589     res = pNtGetContextThread(GetCurrentThread(), &ctx);
590     ok (res == STATUS_SUCCESS,"NtGetContextThread failed with %x\n", res);
591     ok(ctx.Dr0 == 0x42424242,"failed to set debugregister 0 to 0x42424242, got %x\n", ctx.Dr0);
592     ok((ctx.Dr7 & ~0xdc00) == 0x155,"failed to set debugregister 7 to 0x155, got %x\n", ctx.Dr7);
593
594     /* test single stepping behavior */
595     got_exception = 0;
596     run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode));
597     ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
598
599     /* test alignment exceptions */
600     got_exception = 0;
601     run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code));
602     ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
603
604     /* test direction flag */
605     got_exception = 0;
606     run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code));
607     ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
608
609     /* test single stepping over hardware breakpoint */
610     memset(&ctx, 0, sizeof(ctx));
611     ctx.Dr0 = (DWORD) code_mem;  /* set hw bp on first nop */
612     ctx.Dr7 = 3;
613     ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
614     res = pNtSetContextThread( GetCurrentThread(), &ctx);
615     ok( res == STATUS_SUCCESS, "NtSetContextThread faild with %x\n", res);
616
617     got_exception = 0;
618     run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code));
619     ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
620
621     /* test int3 handling */
622     run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code));
623 }
624
625 static void test_debugger(void)
626 {
627     char cmdline[MAX_PATH];
628     PROCESS_INFORMATION pi;
629     STARTUPINFO si = { 0 };
630     DEBUG_EVENT de;
631     DWORD continuestatus;
632     PVOID code_mem_address = NULL;
633     NTSTATUS status;
634     SIZE_T size_read;
635     BOOL ret;
636     int counter = 0;
637     si.cb = sizeof(si);
638
639     if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
640     {
641         skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n)");
642         return;
643     }
644
645     sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
646     ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
647     ok(ret, "could not create child process error: %u\n", GetLastError());
648     if (!ret)
649         return;
650
651     do
652     {
653         continuestatus = DBG_CONTINUE;
654         ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
655
656         if (de.dwThreadId != pi.dwThreadId)
657         {
658             trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
659             ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
660             continue;
661         }
662
663         if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
664         {
665             if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
666             {
667                 skip("child process loaded at different address, terminating it\n");
668                 pNtTerminateProcess(pi.hProcess, 0);
669             }
670         }
671         else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
672         {
673             CONTEXT ctx;
674             int stage;
675
676             counter++;
677             status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
678                                           sizeof(code_mem_address), &size_read);
679             ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
680             status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
681                                           sizeof(stage), &size_read);
682             ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
683
684             ctx.ContextFlags = CONTEXT_FULL;
685             status = pNtGetContextThread(pi.hThread, &ctx);
686             ok(!status, "NtGetContextThread failed with 0x%x\n", status);
687
688             trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
689                   de.u.Exception.ExceptionRecord.ExceptionCode,
690                   de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
691
692             if (counter > 100)
693             {
694                 ok(FALSE, "got way too many exceptions, probaby caught in a infinite loop, terminating child\n");
695                 pNtTerminateProcess(pi.hProcess, 1);
696             }
697             else if (counter >= 2) /* skip startup breakpoint */
698             {
699                 if (stage == 1)
700                 {
701                     ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
702                        ctx.Eip, (char *)code_mem_address + 0xb);
703                     /* setting the context from debugger does not affect the context, the exception handlers gets */
704                     /* uncomment once wine is fixed */
705                     /* ctx.Eip = 0x12345; */
706                     ctx.Eax = 0xf00f00f1;
707
708                     /* let the debuggee handle the exception */
709                     continuestatus = DBG_EXCEPTION_NOT_HANDLED;
710                 }
711                 else if (stage == 2)
712                 {
713                     if (de.u.Exception.dwFirstChance)
714                     {
715                         /* debugger gets first chance exception with unmodified ctx.Eip */
716                         ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
717                             ctx.Eip, (char *)code_mem_address + 0xb);
718
719                         /* setting the context from debugger does not affect the context, the exception handlers gets */
720                         /* uncomment once wine is fixed */
721                         /* ctx.Eip = 0x12345; */
722                         ctx.Eax = 0xf00f00f1;
723
724                         /* pass exception to debuggee
725                          * exception will not be handled and
726                          * a second chance exception will be raised */
727                         continuestatus = DBG_EXCEPTION_NOT_HANDLED;
728                     }
729                     else
730                     {
731                         /* debugger gets context after exception handler has played with it */
732                         /* ctx.Eip is the same value the exception handler got */
733                         if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
734                         {
735                             ok((char *)ctx.Eip == (char *)code_mem_address + 0xa, "Eip at 0x%x instead of %p\n",
736                                 ctx.Eip, (char *)code_mem_address + 0xa);
737                             /* need to fixup Eip for debuggee */
738                             if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
739                                 ctx.Eip += 1;
740                         }
741                         else
742                             ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
743                                 ctx.Eip, (char *)code_mem_address + 0xb);
744                         /* here we handle exception */
745                     }
746                 }
747                 else
748                     ok(FALSE, "unexpected stage %x\n", stage);
749
750                 status = pNtSetContextThread(pi.hThread, &ctx);
751                 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
752             }
753         }
754
755         ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
756
757     } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
758
759     winetest_wait_child_process( pi.hProcess );
760     ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
761     ok(CloseHandle(pi.hProcess) != 0, "error %u\n", GetLastError());
762
763     return;
764 }
765
766 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
767                                  CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
768 {
769     int *stage = *(int **)(frame + 1);
770
771     got_exception++;
772
773     if( *stage == 1) {
774         /* fault while executing sse instruction */
775         context->Eip += 3; /* skip addps */
776         return ExceptionContinueExecution;
777     }
778
779     /* stage 2 - divide by zero fault */
780     if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
781         skip("system doesn't support SIMD exceptions\n");
782     else {
783         ok( rec->ExceptionCode ==  STATUS_FLOAT_MULTIPLE_TRAPS,
784             "exception code: %#x, should be %#x\n",
785             rec->ExceptionCode,  STATUS_FLOAT_MULTIPLE_TRAPS);
786         ok( rec->NumberParameters == 1, "# of params: %i, should be 1\n",
787             rec->NumberParameters);
788         if( rec->NumberParameters == 1 )
789             ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
790     }
791
792     context->Eip += 3; /* skip divps */
793
794     return ExceptionContinueExecution;
795 }
796
797 static const BYTE simd_exception_test[] = {
798     0x83, 0xec, 0x4,                     /* sub $0x4, %esp       */
799     0x0f, 0xae, 0x1c, 0x24,              /* stmxcsr (%esp)       */
800     0x66, 0x81, 0x24, 0x24, 0xff, 0xfd,  /* andw $0xfdff,(%esp)  * enable divide by */
801     0x0f, 0xae, 0x14, 0x24,              /* ldmxcsr (%esp)       * zero exceptions  */
802     0x6a, 0x01,                          /* push   $0x1          */
803     0x6a, 0x01,                          /* push   $0x1          */
804     0x6a, 0x01,                          /* push   $0x1          */
805     0x6a, 0x01,                          /* push   $0x1          */
806     0x0f, 0x10, 0x0c, 0x24,              /* movups (%esp),%xmm1  * fill dividend  */
807     0x0f, 0x57, 0xc0,                    /* xorps  %xmm0,%xmm0   * clear divisor  */
808     0x0f, 0x5e, 0xc8,                    /* divps  %xmm0,%xmm1   * generate fault */
809     0x83, 0xc4, 0x10,                    /* add    $0x10,%esp    */
810     0x66, 0x81, 0x0c, 0x24, 0x00, 0x02,  /* orw    $0x200,(%esp) * disable exceptions */
811     0x0f, 0xae, 0x14, 0x24,              /* ldmxcsr (%esp)       */
812     0x83, 0xc4, 0x04,                    /* add    $0x4,%esp     */
813     0xc3,                                /* ret */
814 };
815
816 static const BYTE sse_check[] = {
817     0x0f, 0x58, 0xc8,                    /* addps  %xmm0,%xmm1 */
818     0xc3,                                /* ret */
819 };
820
821 static void test_simd_exceptions(void)
822 {
823     int stage;
824
825     /* test if CPU & OS can do sse */
826     stage = 1;
827     got_exception = 0;
828     run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check));
829     if(got_exception) {
830         skip("system doesn't support SSE\n");
831         return;
832     }
833
834     /* generate a SIMD exception */
835     stage = 2;
836     got_exception = 0;
837     run_exception_test(simd_fault_handler, &stage, simd_exception_test,
838                        sizeof(simd_exception_test));
839     ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
840 }
841
842 struct fpu_exception_info
843 {
844     DWORD exception_code;
845     DWORD exception_offset;
846     DWORD eip_offset;
847 };
848
849 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
850         CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
851 {
852     struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
853
854     info->exception_code = rec->ExceptionCode;
855     info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
856     info->eip_offset = context->Eip - (DWORD)code_mem;
857
858     ++context->Eip;
859     return ExceptionContinueExecution;
860 }
861
862 static void test_fpu_exceptions(void)
863 {
864     static const BYTE fpu_exception_test_ie[] =
865     {
866         0x83, 0xec, 0x04,                   /* sub $0x4,%esp        */
867         0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp)   */
868         0x9b, 0xd9, 0x7c, 0x24, 0x02,       /* fstcw 0x2(%esp)      */
869         0xd9, 0x2c, 0x24,                   /* fldcw (%esp)         */
870         0xd9, 0xee,                         /* fldz                 */
871         0xd9, 0xe8,                         /* fld1                 */
872         0xde, 0xf1,                         /* fdivp                */
873         0xdd, 0xd8,                         /* fstp %st(0)          */
874         0xdd, 0xd8,                         /* fstp %st(0)          */
875         0x9b,                               /* fwait                */
876         0xdb, 0xe2,                         /* fnclex               */
877         0xd9, 0x6c, 0x24, 0x02,             /* fldcw 0x2(%esp)      */
878         0x83, 0xc4, 0x04,                   /* add $0x4,%esp        */
879         0xc3,                               /* ret                  */
880     };
881
882     static const BYTE fpu_exception_test_de[] =
883     {
884         0x83, 0xec, 0x04,                   /* sub $0x4,%esp        */
885         0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp)   */
886         0x9b, 0xd9, 0x7c, 0x24, 0x02,       /* fstcw 0x2(%esp)      */
887         0xd9, 0x2c, 0x24,                   /* fldcw (%esp)         */
888         0xdd, 0xd8,                         /* fstp %st(0)          */
889         0xd9, 0xee,                         /* fldz                 */
890         0xd9, 0xe8,                         /* fld1                 */
891         0xde, 0xf1,                         /* fdivp                */
892         0x9b,                               /* fwait                */
893         0xdb, 0xe2,                         /* fnclex               */
894         0xdd, 0xd8,                         /* fstp %st(0)          */
895         0xdd, 0xd8,                         /* fstp %st(0)          */
896         0xd9, 0x6c, 0x24, 0x02,             /* fldcw 0x2(%esp)      */
897         0x83, 0xc4, 0x04,                   /* add $0x4,%esp        */
898         0xc3,                               /* ret                  */
899     };
900
901     struct fpu_exception_info info;
902
903     memset(&info, 0, sizeof(info));
904     run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie));
905     ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
906             "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
907     ok(info.exception_offset == 0x19, "Got exception offset %#x, expected 0x19\n", info.exception_offset);
908     ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
909
910     memset(&info, 0, sizeof(info));
911     run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de));
912     ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
913             "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
914     ok(info.exception_offset == 0x17, "Got exception offset %#x, expected 0x17\n", info.exception_offset);
915     ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
916 }
917
918 #endif  /* __i386__ */
919
920 START_TEST(exception)
921 {
922 #ifdef __i386__
923     HMODULE hntdll = GetModuleHandleA("ntdll.dll");
924
925     pNtCurrentTeb        = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
926     pNtGetContextThread  = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
927     pNtSetContextThread  = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
928     pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
929     pRtlRaiseException   = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
930     pNtTerminateProcess  = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
931     pRtlAddVectoredExceptionHandler    = (void *)GetProcAddress( hntdll,
932                                                                  "RtlAddVectoredExceptionHandler" );
933     pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
934                                                                  "RtlRemoveVectoredExceptionHandler" );
935     if (!pNtCurrentTeb)
936     {
937         skip( "NtCurrentTeb not found\n" );
938         return;
939     }
940
941     if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
942         have_vectored_api = TRUE;
943     else
944         skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
945
946     /* 1024 byte should be sufficient */
947     code_mem = VirtualAlloc(NULL, 1024, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
948     if(!code_mem) {
949         trace("VirtualAlloc failed\n");
950         return;
951     }
952
953     my_argc = winetest_get_mainargs( &my_argv );
954     if (my_argc >= 4)
955     {
956         void *addr;
957         sscanf( my_argv[3], "%p", &addr );
958
959         if (addr != &test_stage)
960         {
961             skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
962             return;
963         }
964
965         /* child must be run under a debugger */
966         if (!pNtCurrentTeb()->Peb->BeingDebugged)
967         {
968             ok(FALSE, "child process not being debugged?\n");
969             return;
970         }
971
972         if (pRtlRaiseException)
973         {
974             test_stage = 1;
975             run_rtlraiseexception_test(0x12345);
976             run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
977             run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
978             test_stage = 2;
979             run_rtlraiseexception_test(0x12345);
980             run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
981             run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
982         }
983         else
984             skip( "RtlRaiseException not found\n" );
985
986         /* rest of tests only run in parent */
987         return;
988     }
989
990     test_prot_fault();
991     test_exceptions();
992     test_rtlraiseexception();
993     test_debugger();
994     test_simd_exceptions();
995     test_fpu_exceptions();
996
997     VirtualFree(code_mem, 1024, MEM_RELEASE);
998 #endif
999 }