ntdll/tests: Add tests for dynamic unwind table.
[wine/wine-gecko.git] / dlls / ntdll / tests / exception.c
blob817a19c454e9ec76e17df0c9c9e9c7a361d19c31
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 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winnt.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "excpt.h"
38 #include "wine/test.h"
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);
54 #if defined(__x86_64__)
55 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
56 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
57 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
58 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
59 #endif
61 #ifdef __i386__
63 #ifndef __WINE_WINTRNL_H
64 #define ProcessExecuteFlags 0x22
65 #define MEM_EXECUTE_OPTION_DISABLE 0x01
66 #define MEM_EXECUTE_OPTION_ENABLE 0x02
67 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
68 #endif
70 static int my_argc;
71 static char** my_argv;
72 static int test_stage;
74 static BOOL is_wow64;
76 /* Test various instruction combinations that cause a protection fault on the i386,
77 * and check what the resulting exception looks like.
80 static const struct exception
82 BYTE code[18]; /* asm code */
83 BYTE offset; /* offset of faulting instruction */
84 BYTE length; /* length of faulting instruction */
85 BOOL wow64_broken; /* broken on Wow64, should be skipped */
86 NTSTATUS status; /* expected status code */
87 DWORD nb_params; /* expected number of parameters */
88 DWORD params[4]; /* expected parameters */
89 NTSTATUS alt_status; /* alternative status code */
90 DWORD alt_nb_params; /* alternative number of parameters */
91 DWORD alt_params[4]; /* alternative parameters */
92 } exceptions[] =
94 /* 0 */
95 /* test some privileged instructions */
96 { { 0xfb, 0xc3 }, /* 0: sti; ret */
97 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
98 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
99 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
100 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
101 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
102 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
103 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
104 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
105 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
106 /* 5 */
107 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
108 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
109 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
110 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
111 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
112 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
113 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
114 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
115 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
116 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
117 /* 10 */
118 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
119 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
120 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
121 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
122 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
123 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
124 { { 0xfa, 0xc3 }, /* 13: cli; ret */
125 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
127 /* test long jump to invalid selector */
128 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
129 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
131 /* 15 */
132 /* test iret to invalid selector */
133 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
134 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
135 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
137 /* test loading an invalid selector */
138 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
139 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
141 /* test accessing a zero selector (%es broken on Wow64) */
142 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
143 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
144 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
145 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
146 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
147 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
149 /* test moving %cs -> %ss */
150 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
151 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
153 /* 20 */
154 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
155 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
156 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
157 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
158 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
159 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
161 /* test invalid interrupt */
162 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
163 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
165 /* test moves to/from Crx */
166 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
167 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
168 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
169 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
170 /* 25 */
171 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
172 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
173 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
174 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
176 /* test moves to/from Drx */
177 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
178 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
179 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
180 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
181 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
182 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
183 /* 30 */
184 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
185 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
186 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
187 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
188 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
189 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
191 /* test memory reads */
192 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
193 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
194 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
195 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
196 /* 35 */
197 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
198 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
199 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
200 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
202 /* test memory writes */
203 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
204 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
205 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
206 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
207 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
208 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
209 /* 40 */
210 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
211 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
213 /* test exception with cleared %ds and %es (broken on Wow64) */
214 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
215 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
216 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
218 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
219 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
222 static int got_exception;
223 static BOOL have_vectored_api;
225 static void run_exception_test(void *handler, const void* context,
226 const void *code, unsigned int code_size,
227 DWORD access)
229 struct {
230 EXCEPTION_REGISTRATION_RECORD frame;
231 const void *context;
232 } exc_frame;
233 void (*func)(void) = code_mem;
234 DWORD oldaccess, oldaccess2;
236 exc_frame.frame.Handler = handler;
237 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
238 exc_frame.context = context;
240 memcpy(code_mem, code, code_size);
241 if(access)
242 VirtualProtect(code_mem, code_size, access, &oldaccess);
244 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
245 func();
246 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
248 if(access)
249 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
252 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
254 PCONTEXT context = ExceptionInfo->ContextRecord;
255 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
256 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
257 rec->ExceptionAddress, context->Eip);
259 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
260 rec->ExceptionAddress, (char *)code_mem + 0xb);
262 if (pNtCurrentTeb()->Peb->BeingDebugged)
263 ok((void *)context->Eax == pRtlRaiseException ||
264 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
265 "debugger managed to modify Eax to %x should be %p\n",
266 context->Eax, pRtlRaiseException);
268 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
269 * even if raised by RtlRaiseException
271 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
273 ok(context->Eip == (DWORD)code_mem + 0xa ||
274 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
275 "Eip at %x instead of %x or %x\n", context->Eip,
276 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
278 else
280 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
281 context->Eip, (DWORD)code_mem + 0xb);
284 /* test if context change is preserved from vectored handler to stack handlers */
285 context->Eax = 0xf00f00f0;
286 return EXCEPTION_CONTINUE_SEARCH;
289 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
290 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
292 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
293 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
295 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
296 rec->ExceptionAddress, (char *)code_mem + 0xb);
298 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
299 * even if raised by RtlRaiseException
301 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
303 ok(context->Eip == (DWORD)code_mem + 0xa ||
304 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
305 "Eip at %x instead of %x or %x\n", context->Eip,
306 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
308 else
310 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
311 context->Eip, (DWORD)code_mem + 0xb);
314 if(have_vectored_api)
315 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
316 context->Eax);
318 /* give the debugger a chance to examine the state a second time */
319 /* without the exception handler changing Eip */
320 if (test_stage == 2)
321 return ExceptionContinueSearch;
323 /* Eip in context is decreased by 1
324 * Increase it again, else execution will continue in the middle of an instruction */
325 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
326 context->Eip += 1;
327 return ExceptionContinueExecution;
331 static const BYTE call_one_arg_code[] = {
332 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
333 0x50, /* push %eax */
334 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
335 0xff, 0xd0, /* call *%eax */
336 0x90, /* nop */
337 0x90, /* nop */
338 0x90, /* nop */
339 0x90, /* nop */
340 0xc3, /* ret */
344 static void run_rtlraiseexception_test(DWORD exceptioncode)
346 EXCEPTION_REGISTRATION_RECORD frame;
347 EXCEPTION_RECORD record;
348 PVOID vectored_handler = NULL;
350 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
352 record.ExceptionCode = exceptioncode;
353 record.ExceptionFlags = 0;
354 record.ExceptionRecord = NULL;
355 record.ExceptionAddress = NULL; /* does not matter, copied return address */
356 record.NumberParameters = 0;
358 frame.Handler = rtlraiseexception_handler;
359 frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
361 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
363 pNtCurrentTeb()->Tib.ExceptionList = &frame;
364 if (have_vectored_api)
366 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
367 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
370 func(pRtlRaiseException, &record);
371 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
372 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
374 if (have_vectored_api)
375 pRtlRemoveVectoredExceptionHandler(vectored_handler);
376 pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
379 static void test_rtlraiseexception(void)
381 if (!pRtlRaiseException)
383 skip("RtlRaiseException not found\n");
384 return;
387 /* test without debugger */
388 run_rtlraiseexception_test(0x12345);
389 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
390 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
393 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
394 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
396 const struct exception *except = *(const struct exception **)(frame + 1);
397 unsigned int i, entry = except - exceptions;
399 got_exception++;
400 trace( "exception %u: %x flags:%x addr:%p\n",
401 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
403 ok( rec->ExceptionCode == except->status ||
404 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
405 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
406 ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
407 "%u: Wrong exception address %p/%p\n", entry,
408 rec->ExceptionAddress, (char*)code_mem + except->offset );
410 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
412 ok( rec->NumberParameters == except->nb_params,
413 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
415 else
417 ok( rec->NumberParameters == except->alt_nb_params,
418 "%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
421 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
422 /* instead of page faults for accesses beyond 0xffffffff */
423 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
425 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
426 goto skip_params;
429 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
430 if (except->nb_params == 2 && rec->NumberParameters == 2
431 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
432 && except->params[0] == rec->ExceptionInformation[0])
434 goto skip_params;
437 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
439 for (i = 0; i < rec->NumberParameters; i++)
440 ok( rec->ExceptionInformation[i] == except->params[i],
441 "%u: Wrong parameter %d: %lx/%x\n",
442 entry, i, rec->ExceptionInformation[i], except->params[i] );
444 else
446 for (i = 0; i < rec->NumberParameters; i++)
447 ok( rec->ExceptionInformation[i] == except->alt_params[i],
448 "%u: Wrong parameter %d: %lx/%x\n",
449 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
452 skip_params:
453 /* don't handle exception if it's not the address we expected */
454 if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
456 context->Eip += except->length;
457 return ExceptionContinueExecution;
460 static void test_prot_fault(void)
462 unsigned int i;
464 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
466 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
468 skip( "Exception %u broken on Wow64\n", i );
469 continue;
471 got_exception = 0;
472 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
473 sizeof(exceptions[i].code), 0);
474 if (!i && !got_exception)
476 trace( "No exception, assuming win9x, no point in testing further\n" );
477 break;
479 ok( got_exception == (exceptions[i].status != 0),
480 "%u: bad exception count %d\n", i, got_exception );
484 struct dbgreg_test {
485 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
488 /* test handling of debug registers */
489 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
490 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
492 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
494 context->Eip += 2; /* Skips the popl (%eax) */
495 context->Dr0 = test->dr0;
496 context->Dr1 = test->dr1;
497 context->Dr2 = test->dr2;
498 context->Dr3 = test->dr3;
499 context->Dr6 = test->dr6;
500 context->Dr7 = test->dr7;
501 return ExceptionContinueExecution;
504 #define CHECK_DEBUG_REG(n, m) \
505 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
506 test_num, test->dr##n, ctx.Dr##n)
508 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
510 CONTEXT ctx;
511 NTSTATUS status;
513 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
514 status = pNtGetContextThread(GetCurrentThread(), &ctx);
515 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
517 CHECK_DEBUG_REG(0, ~0);
518 CHECK_DEBUG_REG(1, ~0);
519 CHECK_DEBUG_REG(2, ~0);
520 CHECK_DEBUG_REG(3, ~0);
521 CHECK_DEBUG_REG(6, 0x0f);
522 CHECK_DEBUG_REG(7, ~0xdc00);
525 static const BYTE segfault_code[5] = {
526 0x31, 0xc0, /* xor %eax,%eax */
527 0x8f, 0x00, /* popl (%eax) - cause exception */
528 0xc3 /* ret */
531 /* test the single step exception behaviour */
532 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
533 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
535 got_exception++;
536 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
538 if( got_exception < 3)
539 context->EFlags |= 0x100; /* single step until popf instruction */
540 else {
541 /* show that the last single step exception on the popf instruction
542 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
543 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
544 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
547 return ExceptionContinueExecution;
550 static const BYTE single_stepcode[] = {
551 0x9c, /* pushf */
552 0x58, /* pop %eax */
553 0x0d,0,1,0,0, /* or $0x100,%eax */
554 0x50, /* push %eax */
555 0x9d, /* popf */
556 0x35,0,1,0,0, /* xor $0x100,%eax */
557 0x50, /* push %eax */
558 0x9d, /* popf */
559 0xc3
562 /* Test the alignment check (AC) flag handling. */
563 static const BYTE align_check_code[] = {
564 0x55, /* push %ebp */
565 0x89,0xe5, /* mov %esp,%ebp */
566 0x9c, /* pushf */
567 0x58, /* pop %eax */
568 0x0d,0,0,4,0, /* or $0x40000,%eax */
569 0x50, /* push %eax */
570 0x9d, /* popf */
571 0x89,0xe0, /* mov %esp, %eax */
572 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
573 0x9c, /* pushf */
574 0x58, /* pop %eax */
575 0x35,0,0,4,0, /* xor $0x40000,%eax */
576 0x50, /* push %eax */
577 0x9d, /* popf */
578 0x5d, /* pop %ebp */
579 0xc3, /* ret */
582 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
583 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
585 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
586 got_exception++;
587 return ExceptionContinueExecution;
590 /* Test the direction flag handling. */
591 static const BYTE direction_flag_code[] = {
592 0x55, /* push %ebp */
593 0x89,0xe5, /* mov %esp,%ebp */
594 0xfd, /* std */
595 0xfa, /* cli - cause exception */
596 0x5d, /* pop %ebp */
597 0xc3, /* ret */
600 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
601 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
603 #ifdef __GNUC__
604 unsigned int flags;
605 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
606 /* older windows versions don't clear DF properly so don't test */
607 if (flags & 0x400) trace( "eflags has DF bit set\n" );
608 #endif
609 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
610 got_exception++;
611 context->Eip++; /* skip cli */
612 context->EFlags &= ~0x400; /* make sure it is cleared on return */
613 return ExceptionContinueExecution;
616 /* test single stepping over hardware breakpoint */
617 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
618 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
620 got_exception++;
621 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
622 "wrong exception code: %x\n", rec->ExceptionCode);
624 if(got_exception == 1) {
625 /* hw bp exception on first nop */
626 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
627 context->Eip, (DWORD)code_mem);
628 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
629 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
630 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
631 context->EFlags |= 0x100; /* enable single stepping */
632 } else if( got_exception == 2) {
633 /* single step exception on second nop */
634 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
635 context->Eip, (DWORD)code_mem + 1);
636 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
637 /* depending on the win version the B0 bit is already set here as well
638 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
639 context->EFlags |= 0x100;
640 } else if( got_exception == 3) {
641 /* hw bp exception on second nop */
642 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
643 context->Eip, (DWORD)code_mem + 1);
644 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
645 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
646 context->Dr0 = 0; /* clear breakpoint */
647 context->EFlags |= 0x100;
648 } else {
649 /* single step exception on ret */
650 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
651 context->Eip, (DWORD)code_mem + 2);
652 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
653 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
656 context->Dr6 = 0; /* clear status register */
657 return ExceptionContinueExecution;
660 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
662 /* test int3 handling */
663 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
664 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
666 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
667 code_mem, rec->ExceptionAddress);
668 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
669 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
671 return ExceptionContinueExecution;
674 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
677 static void test_exceptions(void)
679 CONTEXT ctx;
680 NTSTATUS res;
681 struct dbgreg_test dreg_test;
683 if (!pNtGetContextThread || !pNtSetContextThread)
685 skip( "NtGetContextThread/NtSetContextThread not found\n" );
686 return;
689 /* test handling of debug registers */
690 memset(&dreg_test, 0, sizeof(dreg_test));
692 dreg_test.dr0 = 0x42424240;
693 dreg_test.dr2 = 0x126bb070;
694 dreg_test.dr3 = 0x0badbad0;
695 dreg_test.dr7 = 0xffff0115;
696 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
697 check_debug_registers(1, &dreg_test);
699 dreg_test.dr0 = 0x42424242;
700 dreg_test.dr2 = 0x100f0fe7;
701 dreg_test.dr3 = 0x0abebabe;
702 dreg_test.dr7 = 0x115;
703 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
704 check_debug_registers(2, &dreg_test);
706 /* test single stepping behavior */
707 got_exception = 0;
708 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
709 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
711 /* test alignment exceptions */
712 got_exception = 0;
713 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
714 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
716 /* test direction flag */
717 got_exception = 0;
718 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
719 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
721 /* test single stepping over hardware breakpoint */
722 memset(&ctx, 0, sizeof(ctx));
723 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
724 ctx.Dr7 = 3;
725 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
726 res = pNtSetContextThread( GetCurrentThread(), &ctx);
727 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
729 got_exception = 0;
730 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
731 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
733 /* test int3 handling */
734 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
737 static void test_debugger(void)
739 char cmdline[MAX_PATH];
740 PROCESS_INFORMATION pi;
741 STARTUPINFOA si = { 0 };
742 DEBUG_EVENT de;
743 DWORD continuestatus;
744 PVOID code_mem_address = NULL;
745 NTSTATUS status;
746 SIZE_T size_read;
747 BOOL ret;
748 int counter = 0;
749 si.cb = sizeof(si);
751 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
753 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
754 return;
757 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
758 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
759 ok(ret, "could not create child process error: %u\n", GetLastError());
760 if (!ret)
761 return;
765 continuestatus = DBG_CONTINUE;
766 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
768 if (de.dwThreadId != pi.dwThreadId)
770 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
771 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
772 continue;
775 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
777 if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
779 skip("child process loaded at different address, terminating it\n");
780 pNtTerminateProcess(pi.hProcess, 0);
783 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
785 CONTEXT ctx;
786 int stage;
788 counter++;
789 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
790 sizeof(code_mem_address), &size_read);
791 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
792 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
793 sizeof(stage), &size_read);
794 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
796 ctx.ContextFlags = CONTEXT_FULL;
797 status = pNtGetContextThread(pi.hThread, &ctx);
798 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
800 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
801 de.u.Exception.ExceptionRecord.ExceptionCode,
802 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
804 if (counter > 100)
806 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
807 pNtTerminateProcess(pi.hProcess, 1);
809 else if (counter >= 2) /* skip startup breakpoint */
811 if (stage == 1)
813 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
814 ctx.Eip, (char *)code_mem_address + 0xb);
815 /* setting the context from debugger does not affect the context, the exception handlers gets */
816 /* uncomment once wine is fixed */
817 /* ctx.Eip = 0x12345; */
818 ctx.Eax = 0xf00f00f1;
820 /* let the debuggee handle the exception */
821 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
823 else if (stage == 2)
825 if (de.u.Exception.dwFirstChance)
827 /* debugger gets first chance exception with unmodified ctx.Eip */
828 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
829 ctx.Eip, (char *)code_mem_address + 0xb);
831 /* setting the context from debugger does not affect the context, the exception handlers gets */
832 /* uncomment once wine is fixed */
833 /* ctx.Eip = 0x12345; */
834 ctx.Eax = 0xf00f00f1;
836 /* pass exception to debuggee
837 * exception will not be handled and
838 * a second chance exception will be raised */
839 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
841 else
843 /* debugger gets context after exception handler has played with it */
844 /* ctx.Eip is the same value the exception handler got */
845 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
847 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
848 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
849 "Eip at 0x%x instead of %p\n",
850 ctx.Eip, (char *)code_mem_address + 0xa);
851 /* need to fixup Eip for debuggee */
852 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
853 ctx.Eip += 1;
855 else
856 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
857 ctx.Eip, (char *)code_mem_address + 0xb);
858 /* here we handle exception */
861 else
862 ok(FALSE, "unexpected stage %x\n", stage);
864 status = pNtSetContextThread(pi.hThread, &ctx);
865 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
869 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
871 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
873 winetest_wait_child_process( pi.hProcess );
874 ret = CloseHandle(pi.hThread);
875 ok(ret, "error %u\n", GetLastError());
876 ret = CloseHandle(pi.hProcess);
877 ok(ret, "error %u\n", GetLastError());
879 return;
882 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
883 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
885 int *stage = *(int **)(frame + 1);
887 got_exception++;
889 if( *stage == 1) {
890 /* fault while executing sse instruction */
891 context->Eip += 3; /* skip addps */
892 return ExceptionContinueExecution;
895 /* stage 2 - divide by zero fault */
896 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
897 skip("system doesn't support SIMD exceptions\n");
898 else {
899 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
900 "exception code: %#x, should be %#x\n",
901 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
902 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
903 "# of params: %i, should be 1\n",
904 rec->NumberParameters);
905 if( rec->NumberParameters == 1 )
906 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
909 context->Eip += 3; /* skip divps */
911 return ExceptionContinueExecution;
914 static const BYTE simd_exception_test[] = {
915 0x83, 0xec, 0x4, /* sub $0x4, %esp */
916 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
917 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
918 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
919 0x6a, 0x01, /* push $0x1 */
920 0x6a, 0x01, /* push $0x1 */
921 0x6a, 0x01, /* push $0x1 */
922 0x6a, 0x01, /* push $0x1 */
923 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
924 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
925 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
926 0x83, 0xc4, 0x10, /* add $0x10,%esp */
927 0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
928 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
929 0x83, 0xc4, 0x04, /* add $0x4,%esp */
930 0xc3, /* ret */
933 static const BYTE sse_check[] = {
934 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
935 0xc3, /* ret */
938 static void test_simd_exceptions(void)
940 int stage;
942 /* test if CPU & OS can do sse */
943 stage = 1;
944 got_exception = 0;
945 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
946 if(got_exception) {
947 skip("system doesn't support SSE\n");
948 return;
951 /* generate a SIMD exception */
952 stage = 2;
953 got_exception = 0;
954 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
955 sizeof(simd_exception_test), 0);
956 ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
959 struct fpu_exception_info
961 DWORD exception_code;
962 DWORD exception_offset;
963 DWORD eip_offset;
966 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
967 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
969 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
971 info->exception_code = rec->ExceptionCode;
972 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
973 info->eip_offset = context->Eip - (DWORD)code_mem;
975 ++context->Eip;
976 return ExceptionContinueExecution;
979 static void test_fpu_exceptions(void)
981 static const BYTE fpu_exception_test_ie[] =
983 0x83, 0xec, 0x04, /* sub $0x4,%esp */
984 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
985 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
986 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
987 0xd9, 0xee, /* fldz */
988 0xd9, 0xe8, /* fld1 */
989 0xde, 0xf1, /* fdivp */
990 0xdd, 0xd8, /* fstp %st(0) */
991 0xdd, 0xd8, /* fstp %st(0) */
992 0x9b, /* fwait */
993 0xdb, 0xe2, /* fnclex */
994 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
995 0x83, 0xc4, 0x04, /* add $0x4,%esp */
996 0xc3, /* ret */
999 static const BYTE fpu_exception_test_de[] =
1001 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1002 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1003 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1004 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1005 0xdd, 0xd8, /* fstp %st(0) */
1006 0xd9, 0xee, /* fldz */
1007 0xd9, 0xe8, /* fld1 */
1008 0xde, 0xf1, /* fdivp */
1009 0x9b, /* fwait */
1010 0xdb, 0xe2, /* fnclex */
1011 0xdd, 0xd8, /* fstp %st(0) */
1012 0xdd, 0xd8, /* fstp %st(0) */
1013 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1014 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1015 0xc3, /* ret */
1018 struct fpu_exception_info info;
1020 memset(&info, 0, sizeof(info));
1021 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1022 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1023 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1024 ok(info.exception_offset == 0x19 ||
1025 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1026 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1027 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1029 memset(&info, 0, sizeof(info));
1030 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1031 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1032 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1033 ok(info.exception_offset == 0x17 ||
1034 broken( is_wow64 && info.exception_offset == info.eip_offset ),
1035 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1036 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1039 struct dpe_exception_info {
1040 BOOL exception_caught;
1041 DWORD exception_info;
1044 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1045 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1047 DWORD old_prot;
1048 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1050 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1051 "Exception code %08x\n", rec->ExceptionCode);
1052 ok(rec->NumberParameters == 2,
1053 "Parameter count: %d\n", rec->NumberParameters);
1054 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1055 "Exception address: %p, expected %p\n",
1056 (LPVOID)rec->ExceptionInformation[1], code_mem);
1058 info->exception_info = rec->ExceptionInformation[0];
1059 info->exception_caught = TRUE;
1061 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1062 return ExceptionContinueExecution;
1065 static void test_dpe_exceptions(void)
1067 static const BYTE single_ret[] = {0xC3};
1068 struct dpe_exception_info info;
1069 NTSTATUS stat;
1070 BOOL has_hw_support;
1071 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1072 DWORD val;
1073 ULONG len;
1075 /* Query DEP with len too small */
1076 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1077 if(stat == STATUS_INVALID_INFO_CLASS)
1079 skip("This software platform does not support DEP\n");
1080 return;
1082 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1084 /* Query DEP */
1085 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1086 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1087 if(stat == STATUS_SUCCESS)
1089 ok(len == sizeof val, "returned length: %d\n", len);
1090 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1092 skip("toggling DEP impossible - status locked\n");
1093 is_permanent = TRUE;
1094 if(val & MEM_EXECUTE_OPTION_DISABLE)
1095 can_test_without = FALSE;
1096 else
1097 can_test_with = FALSE;
1101 if(!is_permanent)
1103 /* Enable DEP */
1104 val = MEM_EXECUTE_OPTION_DISABLE;
1105 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1106 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1109 if(can_test_with)
1111 /* Try access to locked page with DEP on*/
1112 info.exception_caught = FALSE;
1113 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1114 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1115 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1116 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1117 "Access violation type: %08x\n", (unsigned)info.exception_info);
1118 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1119 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1121 /* Try execution of data with DEP on*/
1122 info.exception_caught = FALSE;
1123 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1124 if(has_hw_support)
1126 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1127 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1128 "Access violation type: %08x\n", (unsigned)info.exception_info);
1130 else
1131 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1133 else
1134 skip("DEP is in AlwaysOff state\n");
1136 if(!is_permanent)
1138 /* Disable DEP */
1139 val = MEM_EXECUTE_OPTION_ENABLE;
1140 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1141 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1144 /* page is read without exec here */
1145 if(can_test_without)
1147 /* Try execution of data with DEP off */
1148 info.exception_caught = FALSE;
1149 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1150 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1152 /* Try access to locked page with DEP off - error code is different than
1153 with hardware DEP on */
1154 info.exception_caught = FALSE;
1155 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1156 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1157 ok(info.exception_info == EXCEPTION_READ_FAULT,
1158 "Access violation type: %08x\n", (unsigned)info.exception_info);
1160 else
1161 skip("DEP is in AlwaysOn state\n");
1163 if(!is_permanent)
1165 /* Turn off DEP permanently */
1166 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1167 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1168 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1171 /* Try to turn off DEP */
1172 val = MEM_EXECUTE_OPTION_ENABLE;
1173 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1174 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1176 /* Try to turn on DEP */
1177 val = MEM_EXECUTE_OPTION_DISABLE;
1178 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1179 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1182 #elif defined(__x86_64__)
1184 #define UNW_FLAG_NHANDLER 0
1185 #define UNW_FLAG_EHANDLER 1
1186 #define UNW_FLAG_UHANDLER 2
1187 #define UNW_FLAG_CHAININFO 4
1189 #define UWOP_PUSH_NONVOL 0
1190 #define UWOP_ALLOC_LARGE 1
1191 #define UWOP_ALLOC_SMALL 2
1192 #define UWOP_SET_FPREG 3
1193 #define UWOP_SAVE_NONVOL 4
1194 #define UWOP_SAVE_NONVOL_FAR 5
1195 #define UWOP_SAVE_XMM128 8
1196 #define UWOP_SAVE_XMM128_FAR 9
1197 #define UWOP_PUSH_MACHFRAME 10
1199 struct results
1201 int rip_offset; /* rip offset from code start */
1202 int rbp_offset; /* rbp offset from stack pointer */
1203 int handler; /* expect handler to be set? */
1204 int rip; /* expected final rip value */
1205 int frame; /* expected frame return value */
1206 int regs[8][2]; /* expected values for registers */
1209 struct unwind_test
1211 const BYTE *function;
1212 size_t function_size;
1213 const BYTE *unwind_info;
1214 const struct results *results;
1215 unsigned int nb_results;
1218 enum regs
1220 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1221 r8, r9, r10, r11, r12, r13, r14, r15
1224 static const char * const reg_names[16] =
1226 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1227 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1230 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1232 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1234 static const int code_offset = 1024;
1235 static const int unwind_offset = 2048;
1236 void *handler, *data;
1237 CONTEXT context;
1238 RUNTIME_FUNCTION runtime_func;
1239 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1240 UINT i, j, k;
1241 ULONG64 fake_stack[256];
1242 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1243 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1245 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1246 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1248 runtime_func.BeginAddress = code_offset;
1249 runtime_func.EndAddress = code_offset + test->function_size;
1250 runtime_func.UnwindData = unwind_offset;
1252 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1254 for (i = 0; i < test->nb_results; i++)
1256 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1257 memset( &context, 0x55, sizeof(context) );
1258 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1259 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1261 context.Rsp = (ULONG_PTR)fake_stack;
1262 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1263 orig_rbp = context.Rbp;
1264 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1266 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1267 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1269 data = (void *)0xdeadbeef;
1270 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1271 &runtime_func, &context, &data, &frame, &ctx_ptr );
1272 if (test->results[i].handler)
1274 ok( (char *)handler == (char *)code_mem + 0x200,
1275 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1276 if (handler) ok( *(DWORD *)data == 0x08070605,
1277 "%u/%u: wrong handler data %p\n", testnum, i, data );
1279 else
1281 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1282 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1285 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1286 testnum, i, (void *)context.Rip, test->results[i].rip );
1287 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1288 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1290 for (j = 0; j < 16; j++)
1292 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1294 for (k = 0; k < nb_regs; k++)
1296 if (test->results[i].regs[k][0] == -1)
1298 k = nb_regs;
1299 break;
1301 if (test->results[i].regs[k][0] == j) break;
1304 if (j == rsp) /* rsp is special */
1306 ok( !ctx_ptr.u2.IntegerContext[j],
1307 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1308 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1309 "%u/%u: register rsp wrong %p/%p\n",
1310 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1311 continue;
1314 if (ctx_ptr.u2.IntegerContext[j])
1316 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1317 testnum, i, reg_names[j], *(&context.Rax + j) );
1318 if (k < nb_regs)
1319 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1320 "%u/%u: register %s wrong %p/%x\n",
1321 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1323 else
1325 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1326 if (j == rbp)
1327 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1328 testnum, i, (void *)context.Rbp );
1329 else
1330 ok( *(&context.Rax + j) == unset_reg,
1331 "%u/%u: register %s wrong %p/unset\n",
1332 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1338 static void test_virtual_unwind(void)
1340 static const BYTE function_0[] =
1342 0xff, 0xf5, /* 00: push %rbp */
1343 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1344 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1345 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1346 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1347 0x90, /* 1c: nop */
1348 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1349 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1350 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1351 0x5d, /* 32: pop %rbp */
1352 0xc3 /* 33: ret */
1355 static const BYTE unwind_info_0[] =
1357 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1358 0x1c, /* prolog size */
1359 8, /* opcode count */
1360 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1362 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1363 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1364 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1365 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1366 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1368 0x00, 0x02, 0x00, 0x00, /* handler */
1369 0x05, 0x06, 0x07, 0x08, /* data */
1372 static const struct results results_0[] =
1374 /* offset rbp handler rip frame registers */
1375 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1376 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1377 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1378 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1379 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1380 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1381 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1382 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1383 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1384 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1385 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1389 static const BYTE function_1[] =
1391 0x53, /* 00: push %rbx */
1392 0x55, /* 01: push %rbp */
1393 0x56, /* 02: push %rsi */
1394 0x57, /* 03: push %rdi */
1395 0x41, 0x54, /* 04: push %r12 */
1396 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1397 0x90, 0x90, /* 0a: nop; nop */
1398 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1399 0x41, 0x5c, /* 10: pop %r12 */
1400 0x5f, /* 12: pop %rdi */
1401 0x5e, /* 13: pop %rsi */
1402 0x5d, /* 14: pop %rbp */
1403 0x5b, /* 15: pop %rbx */
1404 0xc3 /* 16: ret */
1407 static const BYTE unwind_info_1[] =
1409 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1410 0x0a, /* prolog size */
1411 6, /* opcode count */
1412 0, /* frame reg */
1414 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1415 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1416 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1417 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1418 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1419 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1421 0x00, 0x02, 0x00, 0x00, /* handler */
1422 0x05, 0x06, 0x07, 0x08, /* data */
1425 static const struct results results_1[] =
1427 /* offset rbp handler rip frame registers */
1428 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1429 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1430 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1431 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1432 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1433 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1434 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1435 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1436 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1437 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1438 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1439 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1440 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1441 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1444 static const struct unwind_test tests[] =
1446 { function_0, sizeof(function_0), unwind_info_0,
1447 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1448 { function_1, sizeof(function_1), unwind_info_1,
1449 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1451 unsigned int i;
1453 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1454 call_virtual_unwind( i, &tests[i] );
1457 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
1459 static const int code_offset = 1024;
1460 static RUNTIME_FUNCTION runtime_func;
1461 (*(DWORD *)context)++;
1463 runtime_func.BeginAddress = code_offset + 16;
1464 runtime_func.EndAddress = code_offset + 32;
1465 runtime_func.UnwindData = 0;
1466 return &runtime_func;
1469 static void test_dynamic_unwind(void)
1471 static const int code_offset = 1024;
1472 char buf[sizeof(RUNTIME_FUNCTION) + 4];
1473 RUNTIME_FUNCTION *runtime_func, *func;
1474 ULONG_PTR table, base;
1475 DWORD count;
1477 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1478 runtime_func = (RUNTIME_FUNCTION *)buf;
1479 runtime_func->BeginAddress = code_offset;
1480 runtime_func->EndAddress = code_offset + 16;
1481 runtime_func->UnwindData = 0;
1482 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1483 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1485 /* Lookup function outside of any function table */
1486 base = 0xdeadbeef;
1487 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
1488 ok( func == NULL,
1489 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1490 ok( base == 0xdeadbeef,
1491 "RtlLookupFunctionEntry modified base address, expected: 0xdeadbeef, got: %lx\n", base );
1493 /* Test with pointer inside of our function */
1494 base = 0xdeadbeef;
1495 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
1496 ok( func == runtime_func,
1497 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
1498 ok( base == (ULONG_PTR)code_mem,
1499 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1501 /* Test RtlDeleteFunctionTable */
1502 ok( pRtlDeleteFunctionTable( runtime_func ),
1503 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
1504 ok( !pRtlDeleteFunctionTable( runtime_func ),
1505 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1507 /* Unaligned RUNTIME_FUNCTION pointer */
1508 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
1509 runtime_func->BeginAddress = code_offset;
1510 runtime_func->EndAddress = code_offset + 16;
1511 runtime_func->UnwindData = 0;
1512 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1513 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1514 ok( pRtlDeleteFunctionTable( runtime_func ),
1515 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
1517 /* Attempt to insert the same entry twice */
1518 runtime_func = (RUNTIME_FUNCTION *)buf;
1519 runtime_func->BeginAddress = code_offset;
1520 runtime_func->EndAddress = code_offset + 16;
1521 runtime_func->UnwindData = 0;
1522 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1523 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1524 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
1525 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1526 ok( pRtlDeleteFunctionTable( runtime_func ),
1527 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
1528 ok( pRtlDeleteFunctionTable( runtime_func ),
1529 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
1530 ok( !pRtlDeleteFunctionTable( runtime_func ),
1531 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
1533 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1534 table = (ULONG_PTR)code_mem;
1535 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1536 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
1538 /* Test RtlInstallFunctionTableCallback with both low bits set */
1539 table = (ULONG_PTR)code_mem | 0x3;
1540 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
1541 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
1543 /* Lookup function outside of any function table */
1544 count = 0;
1545 base = 0xdeadbeef;
1546 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
1547 ok( func == NULL,
1548 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
1549 ok( base == 0xdeadbeef,
1550 "RtlLookupFunctionEntry modified base address, expected: 0xdeadbeef, got: %lx\n", base );
1551 ok( !count,
1552 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
1554 /* Test with pointer inside of our function */
1555 count = 0;
1556 base = 0xdeadbeef;
1557 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
1558 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
1559 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
1560 ok( base == (ULONG_PTR)code_mem,
1561 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
1562 ok( count == 1,
1563 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
1565 /* Clean up again */
1566 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1567 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
1568 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
1569 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
1573 #endif /* __x86_64__ */
1575 START_TEST(exception)
1577 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
1579 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
1580 if(!code_mem) {
1581 trace("VirtualAlloc failed\n");
1582 return;
1585 pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
1586 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
1587 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
1588 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
1589 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
1590 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
1591 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1592 "RtlAddVectoredExceptionHandler" );
1593 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
1594 "RtlRemoveVectoredExceptionHandler" );
1595 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
1596 "NtQueryInformationProcess" );
1597 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
1598 "NtSetInformationProcess" );
1599 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
1601 #ifdef __i386__
1602 if (!pNtCurrentTeb)
1604 skip( "NtCurrentTeb not found\n" );
1605 return;
1607 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
1609 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
1610 have_vectored_api = TRUE;
1611 else
1612 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1614 my_argc = winetest_get_mainargs( &my_argv );
1615 if (my_argc >= 4)
1617 void *addr;
1618 sscanf( my_argv[3], "%p", &addr );
1620 if (addr != &test_stage)
1622 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
1623 return;
1626 /* child must be run under a debugger */
1627 if (!pNtCurrentTeb()->Peb->BeingDebugged)
1629 ok(FALSE, "child process not being debugged?\n");
1630 return;
1633 if (pRtlRaiseException)
1635 test_stage = 1;
1636 run_rtlraiseexception_test(0x12345);
1637 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1638 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1639 test_stage = 2;
1640 run_rtlraiseexception_test(0x12345);
1641 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
1642 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
1644 else
1645 skip( "RtlRaiseException not found\n" );
1647 /* rest of tests only run in parent */
1648 return;
1651 test_prot_fault();
1652 test_exceptions();
1653 test_rtlraiseexception();
1654 test_debugger();
1655 test_simd_exceptions();
1656 test_fpu_exceptions();
1657 test_dpe_exceptions();
1659 #elif defined(__x86_64__)
1660 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
1661 "RtlAddFunctionTable" );
1662 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
1663 "RtlDeleteFunctionTable" );
1664 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
1665 "RtlInstallFunctionTableCallback" );
1666 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
1667 "RtlLookupFunctionEntry" );
1669 test_virtual_unwind();
1671 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
1672 test_dynamic_unwind();
1673 else
1674 skip( "Dynamic unwind functions not found\n" );
1676 #endif
1678 VirtualFree(code_mem, 0, MEM_FREE);