gdi32: Add a test for outline text metrics.
[wine.git] / dlls / ntdll / tests / exception.c
blob811650365a5796a41586eecb3ffd34220f458d65
1 /*
2 * Unit test suite for ntdll exceptions
4 * Copyright 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #ifndef _WIN32_WINNT
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
26 #endif
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #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"
38 #ifdef __i386__
39 static int my_argc;
40 static char** my_argv;
41 static int test_stage;
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;
53 /* Test various instruction combinations that cause a protection fault on the i386,
54 * and check what the resulting exception looks like.
57 static const struct exception
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[] =
67 /* test some privileged instructions */
68 { { 0xfb, 0xc3 }, /* sti; ret */
69 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
70 { { 0x6c, 0xc3 }, /* insb (%dx); ret */
71 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
72 { { 0x6d, 0xc3 }, /* insl (%dx); ret */
73 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
74 { { 0x6e, 0xc3 }, /* outsb (%dx); ret */
75 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
76 { { 0x6f, 0xc3 }, /* outsl (%dx); ret */
77 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
78 { { 0xe4, 0x11, 0xc3 }, /* inb $0x11,%al; ret */
79 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
80 { { 0xe5, 0x11, 0xc3 }, /* inl $0x11,%eax; ret */
81 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
82 { { 0xe6, 0x11, 0xc3 }, /* outb %al,$0x11; ret */
83 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
84 { { 0xe7, 0x11, 0xc3 }, /* outl %eax,$0x11; ret */
85 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
86 { { 0xed, 0xc3 }, /* inl (%dx),%eax; ret */
87 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
88 { { 0xee, 0xc3 }, /* outb %al,(%dx); ret */
89 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
90 { { 0xef, 0xc3 }, /* outl %eax,(%dx); ret */
91 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
92 { { 0xf4, 0xc3 }, /* hlt; ret */
93 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
94 { { 0xfa, 0xc3 }, /* cli; ret */
95 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
97 /* test long jump to invalid selector */
98 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* ljmp $0,$0; ret */
99 0, 7, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
101 /* test iret to invalid selector */
102 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
103 /* pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
104 6, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
106 /* test loading an invalid selector */
107 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* mov $beef,%ax; mov %ax,%gs; ret */
108 5, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
110 /* test accessing a zero selector */
111 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
112 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es */
113 5, 6, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
115 /* test moving %cs -> %ss */
116 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
117 1, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
119 /* 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 },
125 /* test invalid interrupt */
126 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
127 0, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
129 /* test moves to/from Crx */
130 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
131 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
132 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
133 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
134 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
135 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
136 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
137 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
139 /* test moves to/from Drx */
140 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
141 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
142 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
143 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
144 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
145 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
146 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
147 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
148 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
149 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
150 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
151 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
153 /* test memory reads */
154 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
155 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
156 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
157 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
158 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
159 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
160 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
161 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
163 /* test memory writes */
164 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
165 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
166 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
167 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
168 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
169 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
170 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
171 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
174 static int got_exception;
175 static BOOL have_vectored_api;
177 static void run_exception_test(void *handler, const void* context,
178 const void *code, unsigned int code_size)
180 struct {
181 EXCEPTION_REGISTRATION_RECORD frame;
182 const void *context;
183 } exc_frame;
184 void (*func)(void) = code_mem;
186 exc_frame.frame.Handler = handler;
187 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
188 exc_frame.context = context;
190 memcpy(code_mem, code, code_size);
192 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
193 func();
194 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
197 LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
199 PCONTEXT context = ExceptionInfo->ContextRecord;
200 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
201 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
202 rec->ExceptionAddress, context->Eip);
204 todo_wine {
205 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
206 rec->ExceptionAddress, (char *)code_mem + 0xb);
208 if (pNtCurrentTeb()->Peb->BeingDebugged)
209 ok((void *)context->Eax == pRtlRaiseException, "debugger managed to modify Eax to %x should be %p\n",
210 context->Eax, pRtlRaiseException);
213 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
214 * even if raised by RtlRaiseException
216 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
218 ok(context->Eip == (DWORD)code_mem + 0xa ||
219 context->Eip == (DWORD)code_mem + 0xb, /* win2k3 */
220 "Eip at %x instead of %x or %x\n", context->Eip,
221 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
223 else
225 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
226 context->Eip, (DWORD)code_mem + 0xb);
229 /* test if context change is preserved from vectored handler to stack handlers */
230 context->Eax = 0xf00f00f0;
231 return EXCEPTION_CONTINUE_SEARCH;
234 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
235 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
237 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
238 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
240 todo_wine {
241 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
242 rec->ExceptionAddress, (char *)code_mem + 0xb);
245 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
246 * even if raised by RtlRaiseException
248 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
250 ok(context->Eip == (DWORD)code_mem + 0xa ||
251 context->Eip == (DWORD)code_mem + 0xb, /* win2k3 */
252 "Eip at %x instead of %x or %x\n", context->Eip,
253 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
255 else
257 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
258 context->Eip, (DWORD)code_mem + 0xb);
261 if(have_vectored_api)
262 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
263 context->Eax);
265 /* give the debugger a chance to examine the state a second time */
266 /* without the exception handler changing Eip */
267 if (test_stage == 2)
268 return ExceptionContinueSearch;
270 /* Eip in context is decreased by 1
271 * Increase it again, else execution will continue in the middle of a instruction */
272 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
273 context->Eip += 1;
274 return ExceptionContinueExecution;
278 static const BYTE call_one_arg_code[] = {
279 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
280 0x50, /* push %eax */
281 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
282 0xff, 0xd0, /* call *%eax */
283 0x90, /* nop */
284 0x90, /* nop */
285 0x90, /* nop */
286 0x90, /* nop */
287 0xc3, /* ret */
291 static void run_rtlraiseexception_test(DWORD exceptioncode)
293 EXCEPTION_REGISTRATION_RECORD frame;
294 EXCEPTION_RECORD record;
295 PVOID vectored_handler = NULL;
297 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
299 record.ExceptionCode = exceptioncode;
300 record.ExceptionFlags = 0;
301 record.ExceptionRecord = NULL;
302 record.ExceptionAddress = NULL; /* does not matter, copied return address */
303 record.NumberParameters = 0;
305 frame.Handler = rtlraiseexception_handler;
306 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
308 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
310 pNtCurrentTeb()->Tib.ExceptionList = &frame;
311 if (have_vectored_api)
313 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
314 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
317 func(pRtlRaiseException, &record);
319 if (have_vectored_api)
320 pRtlRemoveVectoredExceptionHandler(vectored_handler);
321 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
324 static void test_rtlraiseexception(void)
326 if (!pRtlRaiseException)
328 skip("RtlRaiseException not found\n");
329 return;
332 /* test without debugger */
333 run_rtlraiseexception_test(0x12345);
334 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
335 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
338 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
339 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
341 const struct exception *except = *(const struct exception **)(frame + 1);
342 unsigned int i, entry = except - exceptions;
344 got_exception++;
345 trace( "exception: %x flags:%x addr:%p\n",
346 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
348 ok( rec->ExceptionCode == except->status,
349 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
350 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
351 "%u: Wrong exception address %p/%p\n", entry,
352 rec->ExceptionAddress, (char*)code_mem + except->offset );
354 ok( rec->NumberParameters == except->nb_params,
355 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
357 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
358 /* instead of page faults for accesses beyond 0xffffffff */
359 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
361 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
362 goto skip_params;
365 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
366 if (except->nb_params == 2 && rec->NumberParameters == 2
367 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
368 && except->params[0] == rec->ExceptionInformation[0])
370 goto skip_params;
373 for (i = 0; i < rec->NumberParameters; i++)
374 ok( rec->ExceptionInformation[i] == except->params[i],
375 "%u: Wrong parameter %d: %lx/%x\n",
376 entry, i, rec->ExceptionInformation[i], except->params[i] );
378 skip_params:
379 /* don't handle exception if it's not the address we expected */
380 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
382 context->Eip += except->length;
383 return ExceptionContinueExecution;
386 static void test_prot_fault(void)
388 unsigned int i;
390 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
392 got_exception = 0;
393 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
394 sizeof(exceptions[i].code));
395 if (!i && !got_exception)
397 trace( "No exception, assuming win9x, no point in testing further\n" );
398 break;
400 ok( got_exception == (exceptions[i].status != 0),
401 "%u: bad exception count %d\n", i, got_exception );
405 /* test handling of debug registers */
406 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
407 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
409 context->Eip += 2; /* Skips the popl (%eax) */
410 context->Dr0 = 0x42424242;
411 context->Dr1 = 0;
412 context->Dr2 = 0;
413 context->Dr3 = 0;
414 context->Dr6 = 0;
415 context->Dr7 = 0x155;
416 return ExceptionContinueExecution;
419 static const BYTE segfault_code[5] = {
420 0x31, 0xc0, /* xor %eax,%eax */
421 0x8f, 0x00, /* popl (%eax) - cause exception */
422 0xc3 /* ret */
425 /* test the single step exception behaviour */
426 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
427 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
429 got_exception++;
430 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
432 if( got_exception < 3)
433 context->EFlags |= 0x100; /* single step until popf instruction */
434 else {
435 /* show that the last single step exception on the popf instruction
436 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
437 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
438 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
441 return ExceptionContinueExecution;
444 static const BYTE single_stepcode[] = {
445 0x9c, /* pushf */
446 0x58, /* pop %eax */
447 0x0d,0,1,0,0, /* or $0x100,%eax */
448 0x50, /* push %eax */
449 0x9d, /* popf */
450 0x35,0,1,0,0, /* xor $0x100,%eax */
451 0x50, /* push %eax */
452 0x9d, /* popf */
453 0xc3
456 /* Test the alignment check (AC) flag handling. */
457 static const BYTE align_check_code[] = {
458 0x55, /* push %ebp */
459 0x89,0xe5, /* mov %esp,%ebp */
460 0x9c, /* pushf */
461 0x58, /* pop %eax */
462 0x0d,0,0,4,0, /* or $0x40000,%eax */
463 0x50, /* push %eax */
464 0x9d, /* popf */
465 0x89,0xe0, /* mov %esp, %eax */
466 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
467 0x9c, /* pushf */
468 0x58, /* pop %eax */
469 0x35,0,0,4,0, /* xor $0x40000,%eax */
470 0x50, /* push %eax */
471 0x9d, /* popf */
472 0x5d, /* pop %ebp */
473 0xc3, /* ret */
476 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
477 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
479 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
480 got_exception++;
481 return ExceptionContinueExecution;
484 /* Test the direction flag handling. */
485 static const BYTE direction_flag_code[] = {
486 0x55, /* push %ebp */
487 0x89,0xe5, /* mov %esp,%ebp */
488 0xfd, /* std */
489 0xfa, /* cli - cause exception */
490 0x5d, /* pop %ebp */
491 0xc3, /* ret */
494 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
495 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
497 #ifdef __GNUC__
498 unsigned int flags;
499 __asm__("pushfl; popl %0" : "=r" (flags) );
500 /* older windows versions don't clear DF properly so don't test */
501 if (flags & 0x400) trace( "eflags has DF bit set\n" );
502 #endif
503 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
504 got_exception++;
505 context->Eip++; /* skip cli */
506 context->EFlags &= ~0x400; /* make sure it is cleared on return */
507 return ExceptionContinueExecution;
510 /* test single stepping over hardware breakpoint */
511 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
512 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
514 got_exception++;
515 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
516 "wrong exception code: %x\n", rec->ExceptionCode);
518 if(got_exception == 1) {
519 /* hw bp exception on first nop */
520 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
521 context->Eip, (DWORD)code_mem);
522 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
523 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
524 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
525 context->EFlags |= 0x100; /* enable single stepping */
526 } else if( got_exception == 2) {
527 /* single step exception on second nop */
528 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
529 context->Eip, (DWORD)code_mem + 1);
530 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
531 /* depending on the win version the B0 bit is already set here as well
532 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
533 context->EFlags |= 0x100;
534 } else if( got_exception == 3) {
535 /* hw bp exception on second nop */
536 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
537 context->Eip, (DWORD)code_mem + 1);
538 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
539 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
540 context->Dr0 = 0; /* clear breakpoint */
541 context->EFlags |= 0x100;
542 } else {
543 /* single step exception on ret */
544 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
545 context->Eip, (DWORD)code_mem + 2);
546 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
547 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
550 context->Dr6 = 0; /* clear status register */
551 return ExceptionContinueExecution;
554 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
556 /* test int3 handling */
557 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
558 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
560 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
561 code_mem, rec->ExceptionAddress);
562 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
563 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
565 return ExceptionContinueExecution;
568 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
571 static void test_exceptions(void)
573 CONTEXT ctx;
574 NTSTATUS res;
576 if (!pNtGetContextThread || !pNtSetContextThread)
578 skip( "NtGetContextThread/NtSetContextThread not found\n" );
579 return;
582 /* test handling of debug registers */
583 run_exception_test(dreg_handler, NULL, &segfault_code, sizeof(segfault_code));
585 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
586 res = pNtGetContextThread(GetCurrentThread(), &ctx);
587 ok (res == STATUS_SUCCESS,"NtGetContextThread failed with %x\n", res);
588 ok(ctx.Dr0 == 0x42424242,"failed to set debugregister 0 to 0x42424242, got %x\n", ctx.Dr0);
589 ok((ctx.Dr7 & ~0xdc00) == 0x155,"failed to set debugregister 7 to 0x155, got %x\n", ctx.Dr7);
591 /* test single stepping behavior */
592 got_exception = 0;
593 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode));
594 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
596 /* test alignment exceptions */
597 got_exception = 0;
598 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code));
599 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
601 /* test direction flag */
602 got_exception = 0;
603 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code));
604 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
606 /* test single stepping over hardware breakpoint */
607 memset(&ctx, 0, sizeof(ctx));
608 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
609 ctx.Dr7 = 3;
610 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
611 res = pNtSetContextThread( GetCurrentThread(), &ctx);
612 ok( res == STATUS_SUCCESS, "NtSetContextThread faild with %x\n", res);
614 got_exception = 0;
615 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code));
616 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
618 /* test int3 handling */
619 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code));
622 static void test_debugger(void)
624 char cmdline[MAX_PATH];
625 PROCESS_INFORMATION pi;
626 STARTUPINFO si = { 0 };
627 DEBUG_EVENT de;
628 DWORD continuestatus;
629 PVOID code_mem_address = NULL;
630 NTSTATUS status;
631 SIZE_T size_read;
632 BOOL ret;
633 int counter = 0;
634 si.cb = sizeof(si);
636 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
638 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n)");
639 return;
642 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
643 ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
644 ok(ret, "could not create child process error: %u\n", GetLastError());
645 if (!ret)
646 return;
650 continuestatus = DBG_CONTINUE;
651 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
653 if (de.dwThreadId != pi.dwThreadId)
655 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
656 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
657 continue;
660 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
662 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
664 skip("child process loaded at different address, terminating it\n");
665 pNtTerminateProcess(pi.hProcess, 0);
668 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
670 CONTEXT ctx;
671 int stage;
673 counter++;
674 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
675 sizeof(code_mem_address), &size_read);
676 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
677 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
678 sizeof(stage), &size_read);
679 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
681 ctx.ContextFlags = CONTEXT_FULL;
682 status = pNtGetContextThread(pi.hThread, &ctx);
683 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
685 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
686 de.u.Exception.ExceptionRecord.ExceptionCode,
687 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
689 if (counter > 100)
691 ok(FALSE, "got way too many exceptions, probaby caught in a infinite loop, terminating child\n");
692 pNtTerminateProcess(pi.hProcess, 1);
694 else if (counter >= 2) /* skip startup breakpoint */
696 if (stage == 1)
698 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
699 ctx.Eip, (char *)code_mem_address + 0xb);
700 /* setting the context from debugger does not affect the context, the exception handlers gets */
701 /* uncomment once wine is fixed */
702 /* ctx.Eip = 0x12345; */
703 ctx.Eax = 0xf00f00f1;
705 /* let the debuggee handle the exception */
706 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
708 else if (stage == 2)
710 if (de.u.Exception.dwFirstChance)
712 /* debugger gets first chance exception with unmodified ctx.Eip */
713 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
714 ctx.Eip, (char *)code_mem_address + 0xb);
716 /* setting the context from debugger does not affect the context, the exception handlers gets */
717 /* uncomment once wine is fixed */
718 /* ctx.Eip = 0x12345; */
719 ctx.Eax = 0xf00f00f1;
721 /* pass exception to debuggee
722 * exception will not be handled and
723 * a second chance exception will be raised */
724 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
726 else
728 /* debugger gets context after exception handler has played with it */
729 /* ctx.Eip is the same value the exception handler got */
730 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
732 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa, "Eip at 0x%x instead of %p\n",
733 ctx.Eip, (char *)code_mem_address + 0xa);
734 /* need to fixup Eip for debuggee */
735 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
736 ctx.Eip += 1;
738 else
739 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
740 ctx.Eip, (char *)code_mem_address + 0xb);
741 /* here we handle exception */
744 else
745 ok(FALSE, "unexpected stage %x\n", stage);
747 status = pNtSetContextThread(pi.hThread, &ctx);
748 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
752 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
754 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
756 winetest_wait_child_process( pi.hProcess );
757 ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
758 ok(CloseHandle(pi.hProcess) != 0, "error %u\n", GetLastError());
760 return;
763 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
764 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
766 int *stage = *(int **)(frame + 1);
768 got_exception++;
770 if( *stage == 1) {
771 /* fault while executing sse instruction */
772 context->Eip += 3; /* skip addps */
773 return ExceptionContinueExecution;
776 /* stage 2 - divide by zero fault */
777 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
778 skip("system doesn't support SIMD exceptions\n");
779 else {
780 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
781 "exception code: %#x, should be %#x\n",
782 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
783 ok( rec->NumberParameters == 1, "# of params: %i, should be 1\n",
784 rec->NumberParameters);
785 if( rec->NumberParameters == 1 )
786 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
789 context->Eip += 3; /* skip divps */
791 return ExceptionContinueExecution;
794 static const BYTE simd_exception_test[] = {
795 0x83, 0xec, 0x4, /* sub $0x4, %esp */
796 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
797 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
798 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
799 0x6a, 0x01, /* push $0x1 */
800 0x6a, 0x01, /* push $0x1 */
801 0x6a, 0x01, /* push $0x1 */
802 0x6a, 0x01, /* push $0x1 */
803 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
804 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
805 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
806 0x83, 0xc4, 0x10, /* add $0x10,%esp */
807 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
808 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
809 0x83, 0xc4, 0x04, /* add $0x4,%esp */
810 0xc3, /* ret */
813 static const BYTE sse_check[] = {
814 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
815 0xc3, /* ret */
818 static void test_simd_exceptions(void)
820 int stage;
822 /* test if CPU & OS can do sse */
823 stage = 1;
824 got_exception = 0;
825 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check));
826 if(got_exception) {
827 skip("system doesn't support SSE\n");
828 return;
831 /* generate a SIMD exception */
832 stage = 2;
833 got_exception = 0;
834 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
835 sizeof(simd_exception_test));
836 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
839 #endif /* __i386__ */
841 START_TEST(exception)
843 #ifdef __i386__
844 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
846 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
847 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
848 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
849 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
850 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
851 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
852 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
853 "RtlAddVectoredExceptionHandler" );
854 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
855 "RtlRemoveVectoredExceptionHandler" );
856 if (!pNtCurrentTeb)
858 skip( "NtCurrentTeb not found\n" );
859 return;
862 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
863 have_vectored_api = TRUE;
864 else
865 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
867 /* 1024 byte should be sufficient */
868 code_mem = VirtualAlloc(NULL, 1024, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
869 if(!code_mem) {
870 trace("VirtualAlloc failed\n");
871 return;
874 my_argc = winetest_get_mainargs( &my_argv );
875 if (my_argc >= 4)
877 void *addr;
878 sscanf( my_argv[3], "%p", &addr );
880 if (addr != &test_stage)
882 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
883 return;
886 /* child must be run under a debugger */
887 if (!pNtCurrentTeb()->Peb->BeingDebugged)
889 ok(FALSE, "child process not being debugged?\n");
890 return;
893 if (pRtlRaiseException)
895 test_stage = 1;
896 run_rtlraiseexception_test(0x12345);
897 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
898 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
899 test_stage = 2;
900 run_rtlraiseexception_test(0x12345);
901 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
902 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
904 else
905 skip( "RtlRaiseException not found\n" );
907 /* rest of tests only run in parent */
908 return;
911 test_prot_fault();
912 test_exceptions();
913 test_rtlraiseexception();
914 test_debugger();
915 test_simd_exceptions();
917 VirtualFree(code_mem, 1024, MEM_RELEASE);
918 #endif