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
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #define WIN32_NO_STATUS
38 #include "wine/test.h"
40 static void *code_mem;
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);
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
64 static char** my_argv;
65 static int test_stage;
69 /* Test various instruction combinations that cause a protection fault on the i386,
70 * and check what the resulting exception looks like.
73 static const struct exception
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 NTSTATUS alt_status; /* alternative status code */
83 DWORD alt_nb_params; /* alternative number of parameters */
84 DWORD alt_params[4]; /* alternative parameters */
88 /* test some privileged instructions */
89 { { 0xfb, 0xc3 }, /* 0: sti; ret */
90 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
91 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
92 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
93 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
94 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
95 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
96 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
97 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
98 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
100 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
101 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
102 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
103 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
104 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
105 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
106 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
107 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
108 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
109 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
111 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
112 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
113 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
114 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
115 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
116 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
117 { { 0xfa, 0xc3 }, /* 13: cli; ret */
118 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
120 /* test long jump to invalid selector */
121 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
122 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
125 /* test iret to invalid selector */
126 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
127 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
128 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
130 /* test loading an invalid selector */
131 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
132 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
134 /* test accessing a zero selector (%es broken on Wow64) */
135 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
136 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
137 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
138 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
139 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
140 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
142 /* test moving %cs -> %ss */
143 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
144 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
147 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
148 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
149 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
150 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
151 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
152 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
154 /* test invalid interrupt */
155 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
156 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
158 /* test moves to/from Crx */
159 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
160 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
161 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
162 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
164 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
165 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
166 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
167 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
169 /* test moves to/from Drx */
170 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
171 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
172 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
173 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
174 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
175 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
177 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
178 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
179 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
180 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
181 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
182 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
184 /* test memory reads */
185 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
186 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
187 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
188 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
190 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
191 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
192 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
193 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
195 /* test memory writes */
196 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
197 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
198 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
199 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
200 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
201 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
203 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
204 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
206 /* test exception with cleared %ds and %es (broken on Wow64) */
207 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
208 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
209 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
211 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
212 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
215 static int got_exception;
216 static BOOL have_vectored_api;
218 static void run_exception_test(void *handler, const void* context,
219 const void *code, unsigned int code_size,
223 EXCEPTION_REGISTRATION_RECORD frame;
226 void (*func)(void) = code_mem;
227 DWORD oldaccess, oldaccess2;
229 exc_frame.frame.Handler = handler;
230 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
231 exc_frame.context = context;
233 memcpy(code_mem, code, code_size);
235 VirtualProtect(code_mem, code_size, access, &oldaccess);
237 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
239 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
242 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
245 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
247 PCONTEXT context = ExceptionInfo->ContextRecord;
248 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
249 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
250 rec->ExceptionAddress, context->Eip);
252 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
253 rec->ExceptionAddress, (char *)code_mem + 0xb);
255 if (pNtCurrentTeb()->Peb->BeingDebugged)
256 ok((void *)context->Eax == pRtlRaiseException ||
257 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
258 "debugger managed to modify Eax to %x should be %p\n",
259 context->Eax, pRtlRaiseException);
261 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
262 * even if raised by RtlRaiseException
264 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
266 ok(context->Eip == (DWORD)code_mem + 0xa ||
267 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
268 "Eip at %x instead of %x or %x\n", context->Eip,
269 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
273 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
274 context->Eip, (DWORD)code_mem + 0xb);
277 /* test if context change is preserved from vectored handler to stack handlers */
278 context->Eax = 0xf00f00f0;
279 return EXCEPTION_CONTINUE_SEARCH;
282 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
283 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
285 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
286 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
288 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
289 rec->ExceptionAddress, (char *)code_mem + 0xb);
291 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
292 * even if raised by RtlRaiseException
294 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
296 ok(context->Eip == (DWORD)code_mem + 0xa ||
297 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
298 "Eip at %x instead of %x or %x\n", context->Eip,
299 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
303 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
304 context->Eip, (DWORD)code_mem + 0xb);
307 if(have_vectored_api)
308 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
311 /* give the debugger a chance to examine the state a second time */
312 /* without the exception handler changing Eip */
314 return ExceptionContinueSearch;
316 /* Eip in context is decreased by 1
317 * Increase it again, else execution will continue in the middle of a instruction */
318 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
320 return ExceptionContinueExecution;
324 static const BYTE call_one_arg_code[] = {
325 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
326 0x50, /* push %eax */
327 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
328 0xff, 0xd0, /* call *%eax */
337 static void run_rtlraiseexception_test(DWORD exceptioncode)
339 EXCEPTION_REGISTRATION_RECORD frame;
340 EXCEPTION_RECORD record;
341 PVOID vectored_handler = NULL;
343 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
345 record.ExceptionCode = exceptioncode;
346 record.ExceptionFlags = 0;
347 record.ExceptionRecord = NULL;
348 record.ExceptionAddress = NULL; /* does not matter, copied return address */
349 record.NumberParameters = 0;
351 frame.Handler = rtlraiseexception_handler;
352 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
354 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
356 pNtCurrentTeb()->Tib.ExceptionList = &frame;
357 if (have_vectored_api)
359 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
360 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
363 func(pRtlRaiseException, &record);
364 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
365 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
367 if (have_vectored_api)
368 pRtlRemoveVectoredExceptionHandler(vectored_handler);
369 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
372 static void test_rtlraiseexception(void)
374 if (!pRtlRaiseException)
376 skip("RtlRaiseException not found\n");
380 /* test without debugger */
381 run_rtlraiseexception_test(0x12345);
382 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
383 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
386 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
387 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
389 const struct exception *except = *(const struct exception **)(frame + 1);
390 unsigned int i, entry = except - exceptions;
393 trace( "exception %u: %x flags:%x addr:%p\n",
394 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
396 ok( rec->ExceptionCode == except->status ||
397 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
398 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
399 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
400 "%u: Wrong exception address %p/%p\n", entry,
401 rec->ExceptionAddress, (char*)code_mem + except->offset );
403 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
405 ok( rec->NumberParameters == except->nb_params,
406 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
410 ok( rec->NumberParameters == except->alt_nb_params,
411 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
414 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
415 /* instead of page faults for accesses beyond 0xffffffff */
416 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
418 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
422 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
423 if (except->nb_params == 2 && rec->NumberParameters == 2
424 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
425 && except->params[0] == rec->ExceptionInformation[0])
430 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
432 for (i = 0; i < rec->NumberParameters; i++)
433 ok( rec->ExceptionInformation[i] == except->params[i],
434 "%u: Wrong parameter %d: %lx/%x\n",
435 entry, i, rec->ExceptionInformation[i], except->params[i] );
439 for (i = 0; i < rec->NumberParameters; i++)
440 ok( rec->ExceptionInformation[i] == except->alt_params[i],
441 "%u: Wrong parameter %d: %lx/%x\n",
442 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
446 /* don't handle exception if it's not the address we expected */
447 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
449 context->Eip += except->length;
450 return ExceptionContinueExecution;
453 static void test_prot_fault(void)
457 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
459 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
461 skip( "Exception %u broken on Wow64\n", i );
465 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
466 sizeof(exceptions[i].code), 0);
467 if (!i && !got_exception)
469 trace( "No exception, assuming win9x, no point in testing further\n" );
472 ok( got_exception == (exceptions[i].status != 0),
473 "%u: bad exception count %d\n", i, got_exception );
477 /* test handling of debug registers */
478 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
479 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
481 context->Eip += 2; /* Skips the popl (%eax) */
482 context->Dr0 = 0x42424242;
487 context->Dr7 = 0x155;
488 return ExceptionContinueExecution;
491 static const BYTE segfault_code[5] = {
492 0x31, 0xc0, /* xor %eax,%eax */
493 0x8f, 0x00, /* popl (%eax) - cause exception */
497 /* test the single step exception behaviour */
498 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
499 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
502 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
504 if( got_exception < 3)
505 context->EFlags |= 0x100; /* single step until popf instruction */
507 /* show that the last single step exception on the popf instruction
508 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
509 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
510 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
513 return ExceptionContinueExecution;
516 static const BYTE single_stepcode[] = {
519 0x0d,0,1,0,0, /* or $0x100,%eax */
520 0x50, /* push %eax */
522 0x35,0,1,0,0, /* xor $0x100,%eax */
523 0x50, /* push %eax */
528 /* Test the alignment check (AC) flag handling. */
529 static const BYTE align_check_code[] = {
530 0x55, /* push %ebp */
531 0x89,0xe5, /* mov %esp,%ebp */
534 0x0d,0,0,4,0, /* or $0x40000,%eax */
535 0x50, /* push %eax */
537 0x89,0xe0, /* mov %esp, %eax */
538 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
541 0x35,0,0,4,0, /* xor $0x40000,%eax */
542 0x50, /* push %eax */
548 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
549 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
551 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
553 return ExceptionContinueExecution;
556 /* Test the direction flag handling. */
557 static const BYTE direction_flag_code[] = {
558 0x55, /* push %ebp */
559 0x89,0xe5, /* mov %esp,%ebp */
561 0xfa, /* cli - cause exception */
566 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
567 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
571 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
572 /* older windows versions don't clear DF properly so don't test */
573 if (flags & 0x400) trace( "eflags has DF bit set\n" );
575 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
577 context->Eip++; /* skip cli */
578 context->EFlags &= ~0x400; /* make sure it is cleared on return */
579 return ExceptionContinueExecution;
582 /* test single stepping over hardware breakpoint */
583 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
584 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
587 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
588 "wrong exception code: %x\n", rec->ExceptionCode);
590 if(got_exception == 1) {
591 /* hw bp exception on first nop */
592 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
593 context->Eip, (DWORD)code_mem);
594 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
595 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
596 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
597 context->EFlags |= 0x100; /* enable single stepping */
598 } else if( got_exception == 2) {
599 /* single step exception on second nop */
600 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
601 context->Eip, (DWORD)code_mem + 1);
602 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
603 /* depending on the win version the B0 bit is already set here as well
604 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
605 context->EFlags |= 0x100;
606 } else if( got_exception == 3) {
607 /* hw bp exception on second nop */
608 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
609 context->Eip, (DWORD)code_mem + 1);
610 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
611 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
612 context->Dr0 = 0; /* clear breakpoint */
613 context->EFlags |= 0x100;
615 /* single step exception on ret */
616 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
617 context->Eip, (DWORD)code_mem + 2);
618 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
619 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
622 context->Dr6 = 0; /* clear status register */
623 return ExceptionContinueExecution;
626 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
628 /* test int3 handling */
629 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
630 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
632 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
633 code_mem, rec->ExceptionAddress);
634 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
635 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
637 return ExceptionContinueExecution;
640 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
643 static void test_exceptions(void)
648 if (!pNtGetContextThread || !pNtSetContextThread)
650 skip( "NtGetContextThread/NtSetContextThread not found\n" );
654 /* test handling of debug registers */
655 run_exception_test(dreg_handler, NULL, &segfault_code, sizeof(segfault_code), 0);
657 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
658 res = pNtGetContextThread(GetCurrentThread(), &ctx);
659 ok (res == STATUS_SUCCESS,"NtGetContextThread failed with %x\n", res);
660 ok(ctx.Dr0 == 0x42424242,"failed to set debugregister 0 to 0x42424242, got %x\n", ctx.Dr0);
661 ok((ctx.Dr7 & ~0xdc00) == 0x155,"failed to set debugregister 7 to 0x155, got %x\n", ctx.Dr7);
663 /* test single stepping behavior */
665 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
666 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
668 /* test alignment exceptions */
670 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
671 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
673 /* test direction flag */
675 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
676 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
678 /* test single stepping over hardware breakpoint */
679 memset(&ctx, 0, sizeof(ctx));
680 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
682 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
683 res = pNtSetContextThread( GetCurrentThread(), &ctx);
684 ok( res == STATUS_SUCCESS, "NtSetContextThread faild with %x\n", res);
687 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
688 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
690 /* test int3 handling */
691 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
694 static void test_debugger(void)
696 char cmdline[MAX_PATH];
697 PROCESS_INFORMATION pi;
698 STARTUPINFO si = { 0 };
700 DWORD continuestatus;
701 PVOID code_mem_address = NULL;
708 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
710 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
714 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
715 ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
716 ok(ret, "could not create child process error: %u\n", GetLastError());
722 continuestatus = DBG_CONTINUE;
723 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
725 if (de.dwThreadId != pi.dwThreadId)
727 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
728 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
732 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
734 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
736 skip("child process loaded at different address, terminating it\n");
737 pNtTerminateProcess(pi.hProcess, 0);
740 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
746 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
747 sizeof(code_mem_address), &size_read);
748 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
749 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
750 sizeof(stage), &size_read);
751 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
753 ctx.ContextFlags = CONTEXT_FULL;
754 status = pNtGetContextThread(pi.hThread, &ctx);
755 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
757 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
758 de.u.Exception.ExceptionRecord.ExceptionCode,
759 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
763 ok(FALSE, "got way too many exceptions, probaby caught in a infinite loop, terminating child\n");
764 pNtTerminateProcess(pi.hProcess, 1);
766 else if (counter >= 2) /* skip startup breakpoint */
770 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
771 ctx.Eip, (char *)code_mem_address + 0xb);
772 /* setting the context from debugger does not affect the context, the exception handlers gets */
773 /* uncomment once wine is fixed */
774 /* ctx.Eip = 0x12345; */
775 ctx.Eax = 0xf00f00f1;
777 /* let the debuggee handle the exception */
778 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
782 if (de.u.Exception.dwFirstChance)
784 /* debugger gets first chance exception with unmodified ctx.Eip */
785 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
786 ctx.Eip, (char *)code_mem_address + 0xb);
788 /* setting the context from debugger does not affect the context, the exception handlers gets */
789 /* uncomment once wine is fixed */
790 /* ctx.Eip = 0x12345; */
791 ctx.Eax = 0xf00f00f1;
793 /* pass exception to debuggee
794 * exception will not be handled and
795 * a second chance exception will be raised */
796 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
800 /* debugger gets context after exception handler has played with it */
801 /* ctx.Eip is the same value the exception handler got */
802 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
804 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
805 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
806 "Eip at 0x%x instead of %p\n",
807 ctx.Eip, (char *)code_mem_address + 0xa);
808 /* need to fixup Eip for debuggee */
809 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
813 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
814 ctx.Eip, (char *)code_mem_address + 0xb);
815 /* here we handle exception */
819 ok(FALSE, "unexpected stage %x\n", stage);
821 status = pNtSetContextThread(pi.hThread, &ctx);
822 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
826 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
828 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
830 winetest_wait_child_process( pi.hProcess );
831 ret = CloseHandle(pi.hThread);
832 ok(ret, "error %u\n", GetLastError());
833 ret = CloseHandle(pi.hProcess);
834 ok(ret, "error %u\n", GetLastError());
839 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
840 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
842 int *stage = *(int **)(frame + 1);
847 /* fault while executing sse instruction */
848 context->Eip += 3; /* skip addps */
849 return ExceptionContinueExecution;
852 /* stage 2 - divide by zero fault */
853 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
854 skip("system doesn't support SIMD exceptions\n");
856 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
857 "exception code: %#x, should be %#x\n",
858 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
859 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
860 "# of params: %i, should be 1\n",
861 rec->NumberParameters);
862 if( rec->NumberParameters == 1 )
863 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
866 context->Eip += 3; /* skip divps */
868 return ExceptionContinueExecution;
871 static const BYTE simd_exception_test[] = {
872 0x83, 0xec, 0x4, /* sub $0x4, %esp */
873 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
874 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
875 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
876 0x6a, 0x01, /* push $0x1 */
877 0x6a, 0x01, /* push $0x1 */
878 0x6a, 0x01, /* push $0x1 */
879 0x6a, 0x01, /* push $0x1 */
880 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
881 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
882 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
883 0x83, 0xc4, 0x10, /* add $0x10,%esp */
884 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
885 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
886 0x83, 0xc4, 0x04, /* add $0x4,%esp */
890 static const BYTE sse_check[] = {
891 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
895 static void test_simd_exceptions(void)
899 /* test if CPU & OS can do sse */
902 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
904 skip("system doesn't support SSE\n");
908 /* generate a SIMD exception */
911 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
912 sizeof(simd_exception_test), 0);
913 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
916 struct fpu_exception_info
918 DWORD exception_code;
919 DWORD exception_offset;
923 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
924 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
926 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
928 info->exception_code = rec->ExceptionCode;
929 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
930 info->eip_offset = context->Eip - (DWORD)code_mem;
933 return ExceptionContinueExecution;
936 static void test_fpu_exceptions(void)
938 static const BYTE fpu_exception_test_ie[] =
940 0x83, 0xec, 0x04, /* sub $0x4,%esp */
941 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
942 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
943 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
944 0xd9, 0xee, /* fldz */
945 0xd9, 0xe8, /* fld1 */
946 0xde, 0xf1, /* fdivp */
947 0xdd, 0xd8, /* fstp %st(0) */
948 0xdd, 0xd8, /* fstp %st(0) */
950 0xdb, 0xe2, /* fnclex */
951 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
952 0x83, 0xc4, 0x04, /* add $0x4,%esp */
956 static const BYTE fpu_exception_test_de[] =
958 0x83, 0xec, 0x04, /* sub $0x4,%esp */
959 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
960 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
961 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
962 0xdd, 0xd8, /* fstp %st(0) */
963 0xd9, 0xee, /* fldz */
964 0xd9, 0xe8, /* fld1 */
965 0xde, 0xf1, /* fdivp */
967 0xdb, 0xe2, /* fnclex */
968 0xdd, 0xd8, /* fstp %st(0) */
969 0xdd, 0xd8, /* fstp %st(0) */
970 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
971 0x83, 0xc4, 0x04, /* add $0x4,%esp */
975 struct fpu_exception_info info;
977 memset(&info, 0, sizeof(info));
978 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
979 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
980 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
981 ok(info.exception_offset == 0x19 ||
982 broken( is_wow64 && info.exception_offset == info.eip_offset ),
983 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
984 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
986 memset(&info, 0, sizeof(info));
987 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
988 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
989 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
990 ok(info.exception_offset == 0x17 ||
991 broken( is_wow64 && info.exception_offset == info.eip_offset ),
992 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
993 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
996 struct dpe_exception_info {
997 BOOL exception_caught;
998 DWORD exception_info;
1001 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1002 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1005 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1007 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1008 "Exception code %08x\n", rec->ExceptionCode);
1009 ok(rec->NumberParameters == 2,
1010 "Parameter count: %d\n", rec->NumberParameters);
1011 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1012 "Exception address: %p, expected %p\n",
1013 (LPVOID)rec->ExceptionInformation[1], code_mem);
1015 info->exception_info = rec->ExceptionInformation[0];
1016 info->exception_caught = TRUE;
1018 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1019 return ExceptionContinueExecution;
1022 static void test_dpe_exceptions(void)
1024 static char single_ret[] = {0xC3};
1025 struct dpe_exception_info info;
1027 BOOL has_hw_support;
1028 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1032 /* Query DEP with len to small */
1033 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1034 if(stat == STATUS_INVALID_INFO_CLASS)
1036 skip("This software platform does not support DEP\n");
1039 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1042 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1043 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1044 if(stat == STATUS_SUCCESS)
1046 ok(len == sizeof val, "returned length: %d\n", len);
1047 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1049 skip("toggling DEP impossible - status locked\n");
1050 is_permanent = TRUE;
1051 if(val & MEM_EXECUTE_OPTION_DISABLE)
1052 can_test_without = FALSE;
1054 can_test_with = FALSE;
1061 val = MEM_EXECUTE_OPTION_DISABLE;
1062 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1063 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1068 /* Try access to locked page with DEP on*/
1069 info.exception_caught = FALSE;
1070 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1071 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1072 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1073 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1074 "Access violation type: %08x\n", (unsigned)info.exception_info);
1075 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1076 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1078 /* Try execution of data with DEP on*/
1079 info.exception_caught = FALSE;
1080 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1083 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1084 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1085 "Access violation type: %08x\n", (unsigned)info.exception_info);
1088 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1091 skip("DEP is in AlwaysOff state\n");
1096 val = MEM_EXECUTE_OPTION_ENABLE;
1097 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1098 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1101 /* page is read without exec here */
1102 if(can_test_without)
1104 /* Try execution of data with DEP off */
1105 info.exception_caught = FALSE;
1106 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1107 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1109 /* Try access to locked page with DEP off - error code is different than
1110 with hardware DEP on */
1111 info.exception_caught = FALSE;
1112 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1113 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1114 ok(info.exception_info == EXCEPTION_READ_FAULT,
1115 "Access violation type: %08x\n", (unsigned)info.exception_info);
1118 skip("DEP is in AlwaysOn state\n");
1122 /* Turn off DEP permanently */
1123 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1124 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1125 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1128 /* Try to turn off DEP */
1129 val = MEM_EXECUTE_OPTION_ENABLE;
1130 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1131 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1133 /* Try to turn on DEP */
1134 val = MEM_EXECUTE_OPTION_DISABLE;
1135 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1136 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1139 #elif defined(__x86_64__)
1141 #define UNW_FLAG_NHANDLER 0
1142 #define UNW_FLAG_EHANDLER 1
1143 #define UNW_FLAG_UHANDLER 2
1144 #define UNW_FLAG_CHAININFO 4
1146 #define UWOP_PUSH_NONVOL 0
1147 #define UWOP_ALLOC_LARGE 1
1148 #define UWOP_ALLOC_SMALL 2
1149 #define UWOP_SET_FPREG 3
1150 #define UWOP_SAVE_NONVOL 4
1151 #define UWOP_SAVE_NONVOL_FAR 5
1152 #define UWOP_SAVE_XMM128 8
1153 #define UWOP_SAVE_XMM128_FAR 9
1154 #define UWOP_PUSH_MACHFRAME 10
1158 int rip_offset; /* rip offset from code start */
1159 int rbp_offset; /* rbp offset from stack pointer */
1160 int handler; /* expect handler to be set? */
1161 int rip; /* expected final rip value */
1162 int frame; /* expected frame return value */
1163 int regs[8][2]; /* expected values for registers */
1168 const BYTE *function;
1169 size_t function_size;
1170 const BYTE *unwind_info;
1171 const struct results *results;
1172 unsigned int nb_results;
1177 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1178 r8, r9, r10, r11, r12, r13, r14, r15
1181 static const char * const reg_names[16] =
1183 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1184 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1187 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1189 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1191 static const int code_offset = 1024;
1192 static const int unwind_offset = 2048;
1193 void *handler, *data;
1195 RUNTIME_FUNCTION runtime_func;
1196 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1198 ULONG64 fake_stack[256];
1199 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1200 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1202 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1203 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1205 runtime_func.BeginAddress = code_offset;
1206 runtime_func.EndAddress = code_offset + test->function_size;
1207 runtime_func.UnwindData = unwind_offset;
1209 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1211 for (i = 0; i < test->nb_results; i++)
1213 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1214 memset( &context, 0x55, sizeof(context) );
1215 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1216 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1218 context.Rsp = (ULONG_PTR)fake_stack;
1219 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1220 orig_rbp = context.Rbp;
1221 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1223 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1224 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1226 data = (void *)0xdeadbeef;
1227 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1228 &runtime_func, &context, &data, &frame, &ctx_ptr );
1229 if (test->results[i].handler)
1231 ok( (char *)handler == (char *)code_mem + 0x200,
1232 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1233 if (handler) ok( *(DWORD *)data == 0x08070605,
1234 "%u/%u: wrong handler data %p\n", testnum, i, data );
1238 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1239 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1242 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1243 testnum, i, (void *)context.Rip, test->results[i].rip );
1244 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1245 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1247 for (j = 0; j < 16; j++)
1249 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1251 for (k = 0; k < nb_regs; k++)
1253 if (test->results[i].regs[k][0] == -1)
1258 if (test->results[i].regs[k][0] == j) break;
1261 if (j == rsp) /* rsp is special */
1263 ok( !ctx_ptr.u2.IntegerContext[j],
1264 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1265 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1266 "%u/%u: register rsp wrong %p/%p\n",
1267 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1271 if (ctx_ptr.u2.IntegerContext[j])
1273 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1274 testnum, i, reg_names[j], *(&context.Rax + j) );
1276 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1277 "%u/%u: register %s wrong %p/%x\n",
1278 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1282 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1284 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1285 testnum, i, (void *)context.Rbp );
1287 ok( *(&context.Rax + j) == unset_reg,
1288 "%u/%u: register %s wrong %p/unset\n",
1289 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1295 static void test_virtual_unwind(void)
1297 static const BYTE function_0[] =
1299 0xff, 0xf5, /* 00: push %rbp */
1300 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1301 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1302 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1303 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1305 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1306 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1307 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1308 0x5d, /* 32: pop %rbp */
1312 static const BYTE unwind_info_0[] =
1314 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1315 0x1c, /* prolog size */
1316 8, /* opcode count */
1317 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1319 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1320 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1321 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1322 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1323 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1325 0x00, 0x02, 0x00, 0x00, /* handler */
1326 0x05, 0x06, 0x07, 0x08, /* data */
1329 static const struct results results_0[] =
1331 /* offset rbp handler rip frame registers */
1332 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1333 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1334 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1335 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1336 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1337 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1338 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1339 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1340 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1341 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1342 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1346 static const BYTE function_1[] =
1348 0x53, /* 00: push %rbx */
1349 0x55, /* 01: push %rbp */
1350 0x56, /* 02: push %rsi */
1351 0x57, /* 03: push %rdi */
1352 0x41, 0x54, /* 04: push %r12 */
1353 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1354 0x90, 0x90, /* 0a: nop; nop */
1355 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1356 0x41, 0x5c, /* 10: pop %r12 */
1357 0x5f, /* 12: pop %rdi */
1358 0x5e, /* 13: pop %rsi */
1359 0x5d, /* 14: pop %rbp */
1360 0x5b, /* 15: pop %rbx */
1364 static const BYTE unwind_info_1[] =
1366 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1367 0x0a, /* prolog size */
1368 6, /* opcode count */
1371 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1372 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1373 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1374 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1375 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1376 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1378 0x00, 0x02, 0x00, 0x00, /* handler */
1379 0x05, 0x06, 0x07, 0x08, /* data */
1382 static const struct results results_1[] =
1384 /* offset rbp handler rip frame registers */
1385 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1386 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1387 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1388 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1389 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1390 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1391 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1392 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1393 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1394 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1395 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1396 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1397 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1398 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1401 static const struct unwind_test tests[] =
1403 { function_0, sizeof(function_0), unwind_info_0,
1404 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1405 { function_1, sizeof(function_1), unwind_info_1,
1406 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1410 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1411 call_virtual_unwind( i, &tests[i] );
1414 #endif /* __x86_64__ */
1416 START_TEST(exception)
1418 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1420 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1422 trace("VirtualAlloc failed\n");
1426 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1427 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1428 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1429 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1430 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1431 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1432 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1433 "RtlAddVectoredExceptionHandler" );
1434 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1435 "RtlRemoveVectoredExceptionHandler" );
1436 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1437 "NtQueryInformationProcess" );
1438 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1439 "NtSetInformationProcess" );
1440 pIsWow64Process = (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "IsWow64Process");
1445 skip( "NtCurrentTeb not found\n" );
1448 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1450 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1451 have_vectored_api = TRUE;
1453 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1455 my_argc = winetest_get_mainargs( &my_argv );
1459 sscanf( my_argv[3], "%p", &addr );
1461 if (addr != &test_stage)
1463 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1467 /* child must be run under a debugger */
1468 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1470 ok(FALSE, "child process not being debugged?\n");
1474 if (pRtlRaiseException)
1477 run_rtlraiseexception_test(0x12345);
1478 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1479 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1481 run_rtlraiseexception_test(0x12345);
1482 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1483 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1486 skip( "RtlRaiseException not found\n" );
1488 /* rest of tests only run in parent */
1494 test_rtlraiseexception();
1496 test_simd_exceptions();
1497 test_fpu_exceptions();
1498 test_dpe_exceptions();
1500 #elif defined(__x86_64__)
1502 test_virtual_unwind();
1506 VirtualFree(code_mem, 0, MEM_FREE);