acbde7b2cb4b0d83de3eece71fb8afce803071c1
[reactos.git] / modules / rostests / winetests / ntdll / exception.c
1 /*
2 * Unit test suite for ntdll exceptions
3 *
4 * Copyright 2005 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #ifndef _WIN32_WINNT
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
26 #endif
27
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnt.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #ifdef __REACTOS__
37 #include <wine/exception.h>
38 #else
39 #include "excpt.h"
40 #endif
41 #include "wine/test.h"
42
43 static void *code_mem;
44
45 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
46 static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
47 static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
48 static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
49 static VOID (WINAPI *pRtlCaptureContext)(CONTEXT*);
50 static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
51 static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
52 static PVOID (WINAPI *pRtlAddVectoredContinueHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
53 static ULONG (WINAPI *pRtlRemoveVectoredContinueHandler)(PVOID handler);
54 static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
55 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
56 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
57 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
58 static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
59 static NTSTATUS (WINAPI *pNtClose)(HANDLE);
60
61 #if defined(__x86_64__)
62 typedef struct
63 {
64 ULONG Count;
65 struct
66 {
67 ULONG BeginAddress;
68 ULONG EndAddress;
69 ULONG HandlerAddress;
70 ULONG JumpTarget;
71 } ScopeRecord[1];
72 } SCOPE_TABLE;
73
74 typedef struct
75 {
76 ULONG64 ControlPc;
77 ULONG64 ImageBase;
78 PRUNTIME_FUNCTION FunctionEntry;
79 ULONG64 EstablisherFrame;
80 ULONG64 TargetIp;
81 PCONTEXT ContextRecord;
82 void* /*PEXCEPTION_ROUTINE*/ LanguageHandler;
83 PVOID HandlerData;
84 PUNWIND_HISTORY_TABLE HistoryTable;
85 ULONG ScopeIndex;
86 } DISPATCHER_CONTEXT;
87
88 typedef struct _SETJMP_FLOAT128
89 {
90 unsigned __int64 DECLSPEC_ALIGN(16) Part[2];
91 } SETJMP_FLOAT128;
92
93 typedef struct _JUMP_BUFFER
94 {
95 unsigned __int64 Frame;
96 unsigned __int64 Rbx;
97 unsigned __int64 Rsp;
98 unsigned __int64 Rbp;
99 unsigned __int64 Rsi;
100 unsigned __int64 Rdi;
101 unsigned __int64 R12;
102 unsigned __int64 R13;
103 unsigned __int64 R14;
104 unsigned __int64 R15;
105 unsigned __int64 Rip;
106 unsigned __int64 Spare;
107 SETJMP_FLOAT128 Xmm6;
108 SETJMP_FLOAT128 Xmm7;
109 SETJMP_FLOAT128 Xmm8;
110 SETJMP_FLOAT128 Xmm9;
111 SETJMP_FLOAT128 Xmm10;
112 SETJMP_FLOAT128 Xmm11;
113 SETJMP_FLOAT128 Xmm12;
114 SETJMP_FLOAT128 Xmm13;
115 SETJMP_FLOAT128 Xmm14;
116 SETJMP_FLOAT128 Xmm15;
117 } _JUMP_BUFFER;
118
119 static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
120 static BOOLEAN (CDECL *pRtlDeleteFunctionTable)(RUNTIME_FUNCTION*);
121 static BOOLEAN (CDECL *pRtlInstallFunctionTableCallback)(DWORD64, DWORD64, DWORD, PGET_RUNTIME_FUNCTION_CALLBACK, PVOID, PCWSTR);
122 static PRUNTIME_FUNCTION (WINAPI *pRtlLookupFunctionEntry)(ULONG64, ULONG64*, UNWIND_HISTORY_TABLE*);
123 static EXCEPTION_DISPOSITION (WINAPI *p__C_specific_handler)(EXCEPTION_RECORD*, ULONG64, CONTEXT*, DISPATCHER_CONTEXT*);
124 static VOID (WINAPI *pRtlCaptureContext)(CONTEXT*);
125 static VOID (CDECL *pRtlRestoreContext)(CONTEXT*, EXCEPTION_RECORD*);
126 static VOID (CDECL *pRtlUnwindEx)(VOID*, VOID*, EXCEPTION_RECORD*, VOID*, CONTEXT*, UNWIND_HISTORY_TABLE*);
127 static int (CDECL *p_setjmp)(_JUMP_BUFFER*);
128 #endif
129
130 #ifdef __i386__
131
132 #ifndef __WINE_WINTRNL_H
133 #define ProcessExecuteFlags 0x22
134 #define MEM_EXECUTE_OPTION_DISABLE 0x01
135 #define MEM_EXECUTE_OPTION_ENABLE 0x02
136 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
137 #endif
138
139 static int my_argc;
140 static char** my_argv;
141 static int test_stage;
142
143 static BOOL is_wow64;
144
145 /* Test various instruction combinations that cause a protection fault on the i386,
146 * and check what the resulting exception looks like.
147 */
148
149 static const struct exception
150 {
151 BYTE code[18]; /* asm code */
152 BYTE offset; /* offset of faulting instruction */
153 BYTE length; /* length of faulting instruction */
154 BOOL wow64_broken; /* broken on Wow64, should be skipped */
155 NTSTATUS status; /* expected status code */
156 DWORD nb_params; /* expected number of parameters */
157 DWORD params[4]; /* expected parameters */
158 NTSTATUS alt_status; /* alternative status code */
159 DWORD alt_nb_params; /* alternative number of parameters */
160 DWORD alt_params[4]; /* alternative parameters */
161 } exceptions[] =
162 {
163 /* 0 */
164 /* test some privileged instructions */
165 { { 0xfb, 0xc3 }, /* 0: sti; ret */
166 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
167 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
168 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
169 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
170 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
171 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
172 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
173 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
174 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
175 /* 5 */
176 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
177 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
178 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
179 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
180 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
181 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
182 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
183 0, 2, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
184 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
185 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
186 /* 10 */
187 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
188 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
189 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
190 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
191 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
192 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
193 { { 0xfa, 0xc3 }, /* 13: cli; ret */
194 0, 1, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
195
196 /* test long jump to invalid selector */
197 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
198 0, 7, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
199
200 /* 15 */
201 /* test iret to invalid selector */
202 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
203 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
204 6, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
205
206 /* test loading an invalid selector */
207 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
208 5, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
209
210 /* test accessing a zero selector (%es broken on Wow64) */
211 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
212 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
213 5, 6, TRUE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
214 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
215 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
216 6, 6, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
217
218 /* test moving %cs -> %ss */
219 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
220 1, 1, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
221
222 /* 20 */
223 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
224 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
225 0, 16, TRUE, STATUS_ILLEGAL_INSTRUCTION, 0, { 0 },
226 STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
227 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
228 0, 5, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
229
230 /* test invalid interrupt */
231 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
232 0, 2, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
233
234 /* test moves to/from Crx */
235 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
236 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
237 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
238 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
239 /* 25 */
240 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
241 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
242 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
243 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
244
245 /* test moves to/from Drx */
246 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
247 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
248 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
249 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
250 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
251 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
252 /* 30 */
253 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
254 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
255 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
256 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
257 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
258 0, 3, FALSE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
259
260 /* test memory reads */
261 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
262 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
263 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
264 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
265 /* 35 */
266 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
267 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
268 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
269 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
270
271 /* test memory writes */
272 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
273 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
274 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
275 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
276 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
277 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
278 /* 40 */
279 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
280 0, 5, FALSE, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
281
282 /* test exception with cleared %ds and %es (broken on Wow64) */
283 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
284 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
285 8, 1, TRUE, STATUS_PRIVILEGED_INSTRUCTION, 0 },
286
287 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
288 1, 1, FALSE, STATUS_SINGLE_STEP, 0 },
289 { { 0xb8, 0xb8, 0xb8, 0xb8, 0xb8, /* mov $0xb8b8b8b8, %eax */
290 0xb9, 0xb9, 0xb9, 0xb9, 0xb9, /* mov $0xb9b9b9b9, %ecx */
291 0xba, 0xba, 0xba, 0xba, 0xba, /* mov $0xbabababa, %edx */
292 0xcd, 0x2d, 0xc3 }, /* int $0x2d; ret */
293 17, 0, FALSE, STATUS_BREAKPOINT, 3, { 0xb8b8b8b8, 0xb9b9b9b9, 0xbabababa } },
294 };
295
296 static int got_exception;
297 static BOOL have_vectored_api;
298
299 static void run_exception_test(void *handler, const void* context,
300 const void *code, unsigned int code_size,
301 DWORD access)
302 {
303 struct {
304 EXCEPTION_REGISTRATION_RECORD frame;
305 const void *context;
306 } exc_frame;
307 void (*func)(void) = code_mem;
308 DWORD oldaccess, oldaccess2;
309
310 exc_frame.frame.Handler = handler;
311 exc_frame.frame.Prev = NtCurrentTeb()->Tib.ExceptionList;
312 exc_frame.context = context;
313
314 memcpy(code_mem, code, code_size);
315 if(access)
316 VirtualProtect(code_mem, code_size, access, &oldaccess);
317
318 NtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
319 func();
320 NtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
321
322 if(access)
323 VirtualProtect(code_mem, code_size, oldaccess, &oldaccess2);
324 }
325
326 static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
327 {
328 PCONTEXT context = ExceptionInfo->ContextRecord;
329 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
330 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
331 rec->ExceptionAddress, context->Eip);
332
333 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
334 rec->ExceptionAddress, (char *)code_mem + 0xb);
335
336 if (NtCurrentTeb()->Peb->BeingDebugged)
337 ok((void *)context->Eax == pRtlRaiseException ||
338 broken( is_wow64 && context->Eax == 0xf00f00f1 ), /* broken on vista */
339 "debugger managed to modify Eax to %x should be %p\n",
340 context->Eax, pRtlRaiseException);
341
342 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
343 * even if raised by RtlRaiseException
344 */
345 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
346 {
347 ok(context->Eip == (DWORD)code_mem + 0xa ||
348 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
349 "Eip at %x instead of %x or %x\n", context->Eip,
350 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
351 }
352 else
353 {
354 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
355 context->Eip, (DWORD)code_mem + 0xb);
356 }
357
358 /* test if context change is preserved from vectored handler to stack handlers */
359 context->Eax = 0xf00f00f0;
360 return EXCEPTION_CONTINUE_SEARCH;
361 }
362
363 static DWORD rtlraiseexception_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
364 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
365 {
366 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
367 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip );
368
369 ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
370 rec->ExceptionAddress, (char *)code_mem + 0xb);
371
372 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
373 * even if raised by RtlRaiseException
374 */
375 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
376 {
377 ok(context->Eip == (DWORD)code_mem + 0xa ||
378 broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
379 "Eip at %x instead of %x or %x\n", context->Eip,
380 (DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
381 }
382 else
383 {
384 ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
385 context->Eip, (DWORD)code_mem + 0xb);
386 }
387
388 if(have_vectored_api)
389 ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
390 context->Eax);
391
392 /* give the debugger a chance to examine the state a second time */
393 /* without the exception handler changing Eip */
394 if (test_stage == 2)
395 return ExceptionContinueSearch;
396
397 /* Eip in context is decreased by 1
398 * Increase it again, else execution will continue in the middle of an instruction */
399 if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
400 context->Eip += 1;
401 return ExceptionContinueExecution;
402 }
403
404
405 static const BYTE call_one_arg_code[] = {
406 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
407 0x50, /* push %eax */
408 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
409 0xff, 0xd0, /* call *%eax */
410 0x90, /* nop */
411 0x90, /* nop */
412 0x90, /* nop */
413 0x90, /* nop */
414 0xc3, /* ret */
415 };
416
417
418 static void run_rtlraiseexception_test(DWORD exceptioncode)
419 {
420 EXCEPTION_REGISTRATION_RECORD frame;
421 EXCEPTION_RECORD record;
422 PVOID vectored_handler = NULL;
423
424 void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
425
426 record.ExceptionCode = exceptioncode;
427 record.ExceptionFlags = 0;
428 record.ExceptionRecord = NULL;
429 record.ExceptionAddress = NULL; /* does not matter, copied return address */
430 record.NumberParameters = 0;
431
432 frame.Handler = rtlraiseexception_handler;
433 frame.Prev = NtCurrentTeb()->Tib.ExceptionList;
434
435 memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
436
437 NtCurrentTeb()->Tib.ExceptionList = &frame;
438 if (have_vectored_api)
439 {
440 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
441 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
442 }
443
444 func(pRtlRaiseException, &record);
445 ok( record.ExceptionAddress == (char *)code_mem + 0x0b,
446 "address set to %p instead of %p\n", record.ExceptionAddress, (char *)code_mem + 0x0b );
447
448 if (have_vectored_api)
449 pRtlRemoveVectoredExceptionHandler(vectored_handler);
450 NtCurrentTeb()->Tib.ExceptionList = frame.Prev;
451 }
452
453 static void test_rtlraiseexception(void)
454 {
455 if (!pRtlRaiseException)
456 {
457 skip("RtlRaiseException not found\n");
458 return;
459 }
460
461 /* test without debugger */
462 run_rtlraiseexception_test(0x12345);
463 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
464 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
465 }
466
467 static DWORD unwind_expected_eax;
468
469 static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
470 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
471 {
472 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
473 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
474
475 ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
476 rec->ExceptionCode, STATUS_UNWIND);
477 ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
478 rec->ExceptionAddress, (char *)code_mem + 0x22);
479 ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
480 context->Eax, unwind_expected_eax);
481
482 context->Eax += 1;
483 return ExceptionContinueSearch;
484 }
485
486 static const BYTE call_unwind_code[] = {
487 0x55, /* push %ebp */
488 0x53, /* push %ebx */
489 0x56, /* push %esi */
490 0x57, /* push %edi */
491 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
492 0x58, /* 0: pop %eax */
493 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
494 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
495 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
496 0x50, /* push %eax */
497 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
498 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
499 0xff, 0xd0, /* call *%eax */
500 0x5f, /* pop %edi */
501 0x5e, /* pop %esi */
502 0x5b, /* pop %ebx */
503 0x5d, /* pop %ebp */
504 0xc3, /* ret */
505 0xcc, /* int $3 */
506 };
507
508 static void test_unwind(void)
509 {
510 EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
511 DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
512 DWORD retval;
513
514 memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
515
516 /* add first unwind handler */
517 frame1->Handler = unwind_handler;
518 frame1->Prev = NtCurrentTeb()->Tib.ExceptionList;
519 NtCurrentTeb()->Tib.ExceptionList = frame1;
520
521 /* add second unwind handler */
522 frame2->Handler = unwind_handler;
523 frame2->Prev = NtCurrentTeb()->Tib.ExceptionList;
524 NtCurrentTeb()->Tib.ExceptionList = frame2;
525
526 /* test unwind to current frame */
527 unwind_expected_eax = 0xDEAD0000;
528 retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
529 ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
530 ok(NtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
531 NtCurrentTeb()->Tib.ExceptionList, frame2);
532
533 /* unwind to frame1 */
534 unwind_expected_eax = 0xDEAD0000;
535 retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
536 ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
537 ok(NtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
538 NtCurrentTeb()->Tib.ExceptionList, frame1);
539
540 /* restore original handler */
541 NtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
542 }
543
544 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
545 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
546 {
547 const struct exception *except = *(const struct exception **)(frame + 1);
548 unsigned int i, parameter_count, entry = except - exceptions;
549
550 got_exception++;
551 trace( "exception %u: %x flags:%x addr:%p\n",
552 entry, rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
553
554 ok( rec->ExceptionCode == except->status ||
555 (except->alt_status != 0 && rec->ExceptionCode == except->alt_status),
556 "%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
557 ok( context->Eip == (DWORD_PTR)code_mem + except->offset,
558 "%u: Unexpected eip %#x/%#lx\n", entry,
559 context->Eip, (DWORD_PTR)code_mem + except->offset );
560 ok( rec->ExceptionAddress == (char*)context->Eip ||
561 (rec->ExceptionCode == STATUS_BREAKPOINT && rec->ExceptionAddress == (char*)context->Eip + 1),
562 "%u: Unexpected exception address %p/%p\n", entry,
563 rec->ExceptionAddress, (char*)context->Eip );
564
565 if (except->status == STATUS_BREAKPOINT && is_wow64)
566 parameter_count = 1;
567 else if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
568 parameter_count = except->nb_params;
569 else
570 parameter_count = except->alt_nb_params;
571
572 ok( rec->NumberParameters == parameter_count,
573 "%u: Unexpected parameter count %u/%u\n", entry, rec->NumberParameters, parameter_count );
574
575 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
576 /* instead of page faults for accesses beyond 0xffffffff */
577 if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
578 {
579 if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
580 goto skip_params;
581 }
582
583 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
584 if (except->nb_params == 2 && rec->NumberParameters == 2
585 && except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
586 && except->params[0] == rec->ExceptionInformation[0])
587 {
588 goto skip_params;
589 }
590
591 if (except->alt_status == 0 || rec->ExceptionCode != except->alt_status)
592 {
593 for (i = 0; i < rec->NumberParameters; i++)
594 ok( rec->ExceptionInformation[i] == except->params[i],
595 "%u: Wrong parameter %d: %lx/%x\n",
596 entry, i, rec->ExceptionInformation[i], except->params[i] );
597 }
598 else
599 {
600 for (i = 0; i < rec->NumberParameters; i++)
601 ok( rec->ExceptionInformation[i] == except->alt_params[i],
602 "%u: Wrong parameter %d: %lx/%x\n",
603 entry, i, rec->ExceptionInformation[i], except->alt_params[i] );
604 }
605
606 skip_params:
607 /* don't handle exception if it's not the address we expected */
608 if (context->Eip != (DWORD_PTR)code_mem + except->offset) return ExceptionContinueSearch;
609
610 context->Eip += except->length;
611 return ExceptionContinueExecution;
612 }
613
614 static void test_prot_fault(void)
615 {
616 unsigned int i;
617
618 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
619 {
620 if (is_wow64 && exceptions[i].wow64_broken && !strcmp( winetest_platform, "windows" ))
621 {
622 skip( "Exception %u broken on Wow64\n", i );
623 continue;
624 }
625 got_exception = 0;
626 run_exception_test(handler, &exceptions[i], &exceptions[i].code,
627 sizeof(exceptions[i].code), 0);
628 if (!i && !got_exception)
629 {
630 trace( "No exception, assuming win9x, no point in testing further\n" );
631 break;
632 }
633 ok( got_exception == (exceptions[i].status != 0),
634 "%u: bad exception count %d\n", i, got_exception );
635 }
636 }
637
638 struct dbgreg_test {
639 DWORD dr0, dr1, dr2, dr3, dr6, dr7;
640 };
641
642 /* test handling of debug registers */
643 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
644 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
645 {
646 const struct dbgreg_test *test = *(const struct dbgreg_test **)(frame + 1);
647
648 context->Eip += 2; /* Skips the popl (%eax) */
649 context->Dr0 = test->dr0;
650 context->Dr1 = test->dr1;
651 context->Dr2 = test->dr2;
652 context->Dr3 = test->dr3;
653 context->Dr6 = test->dr6;
654 context->Dr7 = test->dr7;
655 return ExceptionContinueExecution;
656 }
657
658 #define CHECK_DEBUG_REG(n, m) \
659 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
660 test_num, test->dr##n, ctx.Dr##n)
661
662 static void check_debug_registers(int test_num, const struct dbgreg_test *test)
663 {
664 CONTEXT ctx;
665 NTSTATUS status;
666
667 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
668 status = pNtGetContextThread(GetCurrentThread(), &ctx);
669 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
670
671 CHECK_DEBUG_REG(0, ~0);
672 CHECK_DEBUG_REG(1, ~0);
673 CHECK_DEBUG_REG(2, ~0);
674 CHECK_DEBUG_REG(3, ~0);
675 CHECK_DEBUG_REG(6, 0x0f);
676 CHECK_DEBUG_REG(7, ~0xdc00);
677 }
678
679 static const BYTE segfault_code[5] = {
680 0x31, 0xc0, /* xor %eax,%eax */
681 0x8f, 0x00, /* popl (%eax) - cause exception */
682 0xc3 /* ret */
683 };
684
685 /* test the single step exception behaviour */
686 static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
687 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
688 {
689 got_exception++;
690 ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
691
692 if( got_exception < 3)
693 context->EFlags |= 0x100; /* single step until popf instruction */
694 else {
695 /* show that the last single step exception on the popf instruction
696 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
697 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
698 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
699 }
700
701 return ExceptionContinueExecution;
702 }
703
704 static const BYTE single_stepcode[] = {
705 0x9c, /* pushf */
706 0x58, /* pop %eax */
707 0x0d,0,1,0,0, /* or $0x100,%eax */
708 0x50, /* push %eax */
709 0x9d, /* popf */
710 0x35,0,1,0,0, /* xor $0x100,%eax */
711 0x50, /* push %eax */
712 0x9d, /* popf */
713 0xc3
714 };
715
716 /* Test the alignment check (AC) flag handling. */
717 static const BYTE align_check_code[] = {
718 0x55, /* push %ebp */
719 0x89,0xe5, /* mov %esp,%ebp */
720 0x9c, /* pushf */
721 0x58, /* pop %eax */
722 0x0d,0,0,4,0, /* or $0x40000,%eax */
723 0x50, /* push %eax */
724 0x9d, /* popf */
725 0x89,0xe0, /* mov %esp, %eax */
726 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
727 0x9c, /* pushf */
728 0x58, /* pop %eax */
729 0x35,0,0,4,0, /* xor $0x40000,%eax */
730 0x50, /* push %eax */
731 0x9d, /* popf */
732 0x5d, /* pop %ebp */
733 0xc3, /* ret */
734 };
735
736 static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
737 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
738 {
739 ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
740 got_exception++;
741 return ExceptionContinueExecution;
742 }
743
744 /* Test the direction flag handling. */
745 static const BYTE direction_flag_code[] = {
746 0x55, /* push %ebp */
747 0x89,0xe5, /* mov %esp,%ebp */
748 0xfd, /* std */
749 0xfa, /* cli - cause exception */
750 0x5d, /* pop %ebp */
751 0xc3, /* ret */
752 };
753
754 static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
755 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
756 {
757 #ifdef __GNUC__
758 unsigned int flags;
759 __asm__("pushfl; popl %0; cld" : "=r" (flags) );
760 /* older windows versions don't clear DF properly so don't test */
761 if (flags & 0x400) trace( "eflags has DF bit set\n" );
762 #endif
763 ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
764 got_exception++;
765 context->Eip++; /* skip cli */
766 context->EFlags &= ~0x400; /* make sure it is cleared on return */
767 return ExceptionContinueExecution;
768 }
769
770 /* test single stepping over hardware breakpoint */
771 static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
772 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
773 {
774 got_exception++;
775 ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
776 "wrong exception code: %x\n", rec->ExceptionCode);
777
778 if(got_exception == 1) {
779 /* hw bp exception on first nop */
780 ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
781 context->Eip, (DWORD)code_mem);
782 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
783 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
784 context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
785 context->EFlags |= 0x100; /* enable single stepping */
786 } else if( got_exception == 2) {
787 /* single step exception on second nop */
788 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
789 context->Eip, (DWORD)code_mem + 1);
790 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
791 /* depending on the win version the B0 bit is already set here as well
792 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
793 context->EFlags |= 0x100;
794 } else if( got_exception == 3) {
795 /* hw bp exception on second nop */
796 ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
797 context->Eip, (DWORD)code_mem + 1);
798 ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
799 ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
800 context->Dr0 = 0; /* clear breakpoint */
801 context->EFlags |= 0x100;
802 } else {
803 /* single step exception on ret */
804 ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
805 context->Eip, (DWORD)code_mem + 2);
806 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
807 ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
808 }
809
810 context->Dr6 = 0; /* clear status register */
811 return ExceptionContinueExecution;
812 }
813
814 static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
815
816 /* test int3 handling */
817 static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
818 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
819 {
820 ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
821 code_mem, rec->ExceptionAddress);
822 ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
823 if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
824
825 return ExceptionContinueExecution;
826 }
827
828 static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
829
830 static DWORD WINAPI hw_reg_exception_thread( void *arg )
831 {
832 int expect = (ULONG_PTR)arg;
833 got_exception = 0;
834 run_exception_test( bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0 );
835 ok( got_exception == expect, "expected %u exceptions, got %d\n", expect, got_exception );
836 return 0;
837 }
838
839 static void test_exceptions(void)
840 {
841 CONTEXT ctx;
842 NTSTATUS res;
843 struct dbgreg_test dreg_test;
844 HANDLE h;
845
846 if (!pNtGetContextThread || !pNtSetContextThread)
847 {
848 skip( "NtGetContextThread/NtSetContextThread not found\n" );
849 return;
850 }
851
852 /* test handling of debug registers */
853 memset(&dreg_test, 0, sizeof(dreg_test));
854
855 dreg_test.dr0 = 0x42424240;
856 dreg_test.dr2 = 0x126bb070;
857 dreg_test.dr3 = 0x0badbad0;
858 dreg_test.dr7 = 0xffff0115;
859 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
860 check_debug_registers(1, &dreg_test);
861
862 dreg_test.dr0 = 0x42424242;
863 dreg_test.dr2 = 0x100f0fe7;
864 dreg_test.dr3 = 0x0abebabe;
865 dreg_test.dr7 = 0x115;
866 run_exception_test(dreg_handler, &dreg_test, &segfault_code, sizeof(segfault_code), 0);
867 check_debug_registers(2, &dreg_test);
868
869 /* test single stepping behavior */
870 got_exception = 0;
871 run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode), 0);
872 ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
873
874 /* test alignment exceptions */
875 got_exception = 0;
876 run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code), 0);
877 ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
878
879 /* test direction flag */
880 got_exception = 0;
881 run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code), 0);
882 ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
883
884 /* test single stepping over hardware breakpoint */
885 memset(&ctx, 0, sizeof(ctx));
886 ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
887 ctx.Dr7 = 3;
888 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
889 res = pNtSetContextThread( GetCurrentThread(), &ctx);
890 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res);
891
892 got_exception = 0;
893 run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code), 0);
894 ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
895
896 /* test int3 handling */
897 run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code), 0);
898
899 /* test that hardware breakpoints are not inherited by created threads */
900 res = pNtSetContextThread( GetCurrentThread(), &ctx );
901 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res );
902
903 h = CreateThread( NULL, 0, hw_reg_exception_thread, 0, 0, NULL );
904 WaitForSingleObject( h, 10000 );
905 CloseHandle( h );
906
907 h = CreateThread( NULL, 0, hw_reg_exception_thread, (void *)4, CREATE_SUSPENDED, NULL );
908 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
909 res = pNtGetContextThread( h, &ctx );
910 ok( res == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", res );
911 ok( ctx.Dr0 == 0, "dr0 %x\n", ctx.Dr0 );
912 ok( ctx.Dr7 == 0, "dr7 %x\n", ctx.Dr7 );
913 ctx.Dr0 = (DWORD)code_mem;
914 ctx.Dr7 = 3;
915 res = pNtSetContextThread( h, &ctx );
916 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res );
917 ResumeThread( h );
918 WaitForSingleObject( h, 10000 );
919 CloseHandle( h );
920
921 ctx.Dr0 = 0;
922 ctx.Dr7 = 0;
923 res = pNtSetContextThread( GetCurrentThread(), &ctx );
924 ok( res == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", res );
925 }
926
927 static void test_debugger(void)
928 {
929 char cmdline[MAX_PATH];
930 PROCESS_INFORMATION pi;
931 STARTUPINFOA si = { 0 };
932 DEBUG_EVENT de;
933 DWORD continuestatus;
934 PVOID code_mem_address = NULL;
935 NTSTATUS status;
936 SIZE_T size_read;
937 BOOL ret;
938 int counter = 0;
939 si.cb = sizeof(si);
940
941 if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
942 {
943 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
944 return;
945 }
946
947 sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
948 ret = CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
949 ok(ret, "could not create child process error: %u\n", GetLastError());
950 if (!ret)
951 return;
952
953 do
954 {
955 continuestatus = DBG_CONTINUE;
956 ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
957
958 if (de.dwThreadId != pi.dwThreadId)
959 {
960 trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
961 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
962 continue;
963 }
964
965 if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
966 {
967 if(de.u.CreateProcessInfo.lpBaseOfImage != NtCurrentTeb()->Peb->ImageBaseAddress)
968 {
969 skip("child process loaded at different address, terminating it\n");
970 pNtTerminateProcess(pi.hProcess, 0);
971 }
972 }
973 else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
974 {
975 CONTEXT ctx;
976 int stage;
977
978 counter++;
979 status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
980 sizeof(code_mem_address), &size_read);
981 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
982 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
983 sizeof(stage), &size_read);
984 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
985
986 ctx.ContextFlags = CONTEXT_FULL;
987 status = pNtGetContextThread(pi.hThread, &ctx);
988 ok(!status, "NtGetContextThread failed with 0x%x\n", status);
989
990 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
991 de.u.Exception.ExceptionRecord.ExceptionCode,
992 de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
993
994 if (counter > 100)
995 {
996 ok(FALSE, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
997 pNtTerminateProcess(pi.hProcess, 1);
998 }
999 else if (counter >= 2) /* skip startup breakpoint */
1000 {
1001 if (stage == 1)
1002 {
1003 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
1004 ctx.Eip, (char *)code_mem_address + 0xb);
1005 /* setting the context from debugger does not affect the context, the exception handlers gets */
1006 /* uncomment once wine is fixed */
1007 /* ctx.Eip = 0x12345; */
1008 ctx.Eax = 0xf00f00f1;
1009
1010 /* let the debuggee handle the exception */
1011 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1012 }
1013 else if (stage == 2)
1014 {
1015 if (de.u.Exception.dwFirstChance)
1016 {
1017 /* debugger gets first chance exception with unmodified ctx.Eip */
1018 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
1019 ctx.Eip, (char *)code_mem_address + 0xb);
1020
1021 /* setting the context from debugger does not affect the context, the exception handlers gets */
1022 /* uncomment once wine is fixed */
1023 /* ctx.Eip = 0x12345; */
1024 ctx.Eax = 0xf00f00f1;
1025
1026 /* pass exception to debuggee
1027 * exception will not be handled and
1028 * a second chance exception will be raised */
1029 continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1030 }
1031 else
1032 {
1033 /* debugger gets context after exception handler has played with it */
1034 /* ctx.Eip is the same value the exception handler got */
1035 if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
1036 {
1037 ok((char *)ctx.Eip == (char *)code_mem_address + 0xa ||
1038 broken(is_wow64 && (char *)ctx.Eip == (char *)code_mem_address + 0xb),
1039 "Eip at 0x%x instead of %p\n",
1040 ctx.Eip, (char *)code_mem_address + 0xa);
1041 /* need to fixup Eip for debuggee */
1042 if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
1043 ctx.Eip += 1;
1044 }
1045 else
1046 ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
1047 ctx.Eip, (char *)code_mem_address + 0xb);
1048 /* here we handle exception */
1049 }
1050 }
1051 else if (stage == 7 || stage == 8)
1052 {
1053 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT,
1054 "expected EXCEPTION_BREAKPOINT, got %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode);
1055 ok((char *)ctx.Eip == (char *)code_mem_address + 0x1d,
1056 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address + 0x1d, ctx.Eip);
1057
1058 if (stage == 8) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1059 }
1060 else if (stage == 9 || stage == 10)
1061 {
1062 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT,
1063 "expected EXCEPTION_BREAKPOINT, got %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode);
1064 ok((char *)ctx.Eip == (char *)code_mem_address + 2,
1065 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address + 2, ctx.Eip);
1066
1067 if (stage == 10) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1068 }
1069 else if (stage == 11 || stage == 12)
1070 {
1071 ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_INVALID_HANDLE,
1072 "unexpected exception code %08x, expected %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode,
1073 EXCEPTION_INVALID_HANDLE);
1074 ok(de.u.Exception.ExceptionRecord.NumberParameters == 0,
1075 "unexpected number of parameters %d, expected 0\n", de.u.Exception.ExceptionRecord.NumberParameters);
1076
1077 if (stage == 12) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1078 }
1079 else
1080 ok(FALSE, "unexpected stage %x\n", stage);
1081
1082 status = pNtSetContextThread(pi.hThread, &ctx);
1083 ok(!status, "NtSetContextThread failed with 0x%x\n", status);
1084 }
1085 }
1086 else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
1087 {
1088 int stage;
1089 #ifdef __REACTOS__
1090 /* This will catch our DPRINTs, such as
1091 * "WARNING: RtlpDphTargetDllsLogicInitialize at ..\..\lib\rtl\heappage.c:1283 is UNIMPLEMENTED!"
1092 * so we need a full-size buffer to avoid a stack overflow
1093 */
1094 char buffer[513];
1095 #else
1096 char buffer[64];
1097 #endif
1098
1099 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
1100 sizeof(stage), &size_read);
1101 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1102
1103 ok(!de.u.DebugString.fUnicode, "unexpected unicode debug string event\n");
1104 ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
1105 de.u.DebugString.nDebugStringLength);
1106
1107 memset(buffer, 0, sizeof(buffer));
1108 status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
1109 de.u.DebugString.nDebugStringLength, &size_read);
1110 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1111
1112 if (stage == 3 || stage == 4)
1113 ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
1114 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
1115 ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
1116
1117 if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1118 }
1119 else if (de.dwDebugEventCode == RIP_EVENT)
1120 {
1121 int stage;
1122
1123 status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
1124 sizeof(stage), &size_read);
1125 ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
1126
1127 if (stage == 5 || stage == 6)
1128 {
1129 ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
1130 de.u.RipInfo.dwError, 0x11223344);
1131 ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
1132 de.u.RipInfo.dwType, 0x55667788);
1133 }
1134 else
1135 ok(FALSE, "unexpected stage %x\n", stage);
1136
1137 if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
1138 }
1139
1140 ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
1141
1142 } while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
1143
1144 winetest_wait_child_process( pi.hProcess );
1145 ret = CloseHandle(pi.hThread);
1146 ok(ret, "error %u\n", GetLastError());
1147 ret = CloseHandle(pi.hProcess);
1148 ok(ret, "error %u\n", GetLastError());
1149
1150 return;
1151 }
1152
1153 static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1154 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
1155 {
1156 int *stage = *(int **)(frame + 1);
1157
1158 got_exception++;
1159
1160 if( *stage == 1) {
1161 /* fault while executing sse instruction */
1162 context->Eip += 3; /* skip addps */
1163 return ExceptionContinueExecution;
1164 }
1165 else if ( *stage == 2 || *stage == 3 ) {
1166 /* stage 2 - divide by zero fault */
1167 /* stage 3 - invalid operation fault */
1168 if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
1169 skip("system doesn't support SIMD exceptions\n");
1170 else {
1171 ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
1172 "exception code: %#x, should be %#x\n",
1173 rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
1174 ok( rec->NumberParameters == 1 || broken(is_wow64 && rec->NumberParameters == 2),
1175 "# of params: %i, should be 1\n",
1176 rec->NumberParameters);
1177 if( rec->NumberParameters == 1 )
1178 ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
1179 }
1180 context->Eip += 3; /* skip divps */
1181 }
1182 else
1183 ok(FALSE, "unexpected stage %x\n", *stage);
1184
1185 return ExceptionContinueExecution;
1186 }
1187
1188 static const BYTE simd_exception_test[] = {
1189 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1190 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1191 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1192 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1193 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1194 0x6a, 0x01, /* push $0x1 */
1195 0x6a, 0x01, /* push $0x1 */
1196 0x6a, 0x01, /* push $0x1 */
1197 0x6a, 0x01, /* push $0x1 */
1198 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1199 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1200 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1201 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1202 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1203 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1204 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1205 0xc3, /* ret */
1206 };
1207
1208 static const BYTE simd_exception_test2[] = {
1209 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1210 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1211 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1212 0x66, 0x81, 0x24, 0x24, 0x7f, 0xff, /* andw $0xff7f,(%esp) * enable invalid */
1213 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * operation exceptions */
1214 0x0f, 0x57, 0xc9, /* xorps %xmm1,%xmm1 * clear dividend */
1215 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1216 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1217 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1218 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1219 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1220 0xc3, /* ret */
1221 };
1222
1223 static const BYTE sse_check[] = {
1224 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1225 0xc3, /* ret */
1226 };
1227
1228 static void test_simd_exceptions(void)
1229 {
1230 int stage;
1231
1232 /* test if CPU & OS can do sse */
1233 stage = 1;
1234 got_exception = 0;
1235 run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check), 0);
1236 if(got_exception) {
1237 skip("system doesn't support SSE\n");
1238 return;
1239 }
1240
1241 /* generate a SIMD exception */
1242 stage = 2;
1243 got_exception = 0;
1244 run_exception_test(simd_fault_handler, &stage, simd_exception_test,
1245 sizeof(simd_exception_test), 0);
1246 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1247
1248 /* generate a SIMD exception, test FPE_FLTINV */
1249 stage = 3;
1250 got_exception = 0;
1251 run_exception_test(simd_fault_handler, &stage, simd_exception_test2,
1252 sizeof(simd_exception_test2), 0);
1253 ok(got_exception == 1, "got exception: %i, should be 1\n", got_exception);
1254 }
1255
1256 struct fpu_exception_info
1257 {
1258 DWORD exception_code;
1259 DWORD exception_offset;
1260 DWORD eip_offset;
1261 };
1262
1263 static DWORD fpu_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1264 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1265 {
1266 struct fpu_exception_info *info = *(struct fpu_exception_info **)(frame + 1);
1267
1268 info->exception_code = rec->ExceptionCode;
1269 info->exception_offset = (BYTE *)rec->ExceptionAddress - (BYTE *)code_mem;
1270 info->eip_offset = context->Eip - (DWORD)code_mem;
1271
1272 ++context->Eip;
1273 return ExceptionContinueExecution;
1274 }
1275
1276 static void test_fpu_exceptions(void)
1277 {
1278 static const BYTE fpu_exception_test_ie[] =
1279 {
1280 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1281 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1282 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1283 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1284 0xd9, 0xee, /* fldz */
1285 0xd9, 0xe8, /* fld1 */
1286 0xde, 0xf1, /* fdivp */
1287 0xdd, 0xd8, /* fstp %st(0) */
1288 0xdd, 0xd8, /* fstp %st(0) */
1289 0x9b, /* fwait */
1290 0xdb, 0xe2, /* fnclex */
1291 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1292 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1293 0xc3, /* ret */
1294 };
1295
1296 static const BYTE fpu_exception_test_de[] =
1297 {
1298 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1299 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1300 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1301 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1302 0xdd, 0xd8, /* fstp %st(0) */
1303 0xd9, 0xee, /* fldz */
1304 0xd9, 0xe8, /* fld1 */
1305 0xde, 0xf1, /* fdivp */
1306 0x9b, /* fwait */
1307 0xdb, 0xe2, /* fnclex */
1308 0xdd, 0xd8, /* fstp %st(0) */
1309 0xdd, 0xd8, /* fstp %st(0) */
1310 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1311 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1312 0xc3, /* ret */
1313 };
1314
1315 struct fpu_exception_info info;
1316
1317 memset(&info, 0, sizeof(info));
1318 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_ie, sizeof(fpu_exception_test_ie), 0);
1319 ok(info.exception_code == EXCEPTION_FLT_STACK_CHECK,
1320 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info.exception_code);
1321 ok(info.exception_offset == 0x19 ||
1322 broken( info.exception_offset == info.eip_offset ),
1323 "Got exception offset %#x, expected 0x19\n", info.exception_offset);
1324 ok(info.eip_offset == 0x1b, "Got EIP offset %#x, expected 0x1b\n", info.eip_offset);
1325
1326 memset(&info, 0, sizeof(info));
1327 run_exception_test(fpu_exception_handler, &info, fpu_exception_test_de, sizeof(fpu_exception_test_de), 0);
1328 ok(info.exception_code == EXCEPTION_FLT_DIVIDE_BY_ZERO,
1329 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info.exception_code);
1330 ok(info.exception_offset == 0x17 ||
1331 broken( info.exception_offset == info.eip_offset ),
1332 "Got exception offset %#x, expected 0x17\n", info.exception_offset);
1333 ok(info.eip_offset == 0x19, "Got EIP offset %#x, expected 0x19\n", info.eip_offset);
1334 }
1335
1336 struct dpe_exception_info {
1337 BOOL exception_caught;
1338 DWORD exception_info;
1339 };
1340
1341 static DWORD dpe_exception_handler(EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
1342 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher)
1343 {
1344 DWORD old_prot;
1345 struct dpe_exception_info *info = *(struct dpe_exception_info **)(frame + 1);
1346
1347 ok(rec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION,
1348 "Exception code %08x\n", rec->ExceptionCode);
1349 ok(rec->NumberParameters == 2,
1350 "Parameter count: %d\n", rec->NumberParameters);
1351 ok((LPVOID)rec->ExceptionInformation[1] == code_mem,
1352 "Exception address: %p, expected %p\n",
1353 (LPVOID)rec->ExceptionInformation[1], code_mem);
1354
1355 info->exception_info = rec->ExceptionInformation[0];
1356 info->exception_caught = TRUE;
1357
1358 VirtualProtect(code_mem, 1, PAGE_EXECUTE_READWRITE, &old_prot);
1359 return ExceptionContinueExecution;
1360 }
1361
1362 static void test_dpe_exceptions(void)
1363 {
1364 static const BYTE single_ret[] = {0xC3};
1365 struct dpe_exception_info info;
1366 NTSTATUS stat;
1367 BOOL has_hw_support;
1368 BOOL is_permanent = FALSE, can_test_without = TRUE, can_test_with = TRUE;
1369 DWORD val;
1370 ULONG len;
1371
1372 /* Query DEP with len too small */
1373 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val - 1, &len);
1374 if(stat == STATUS_INVALID_INFO_CLASS)
1375 {
1376 skip("This software platform does not support DEP\n");
1377 return;
1378 }
1379 ok(stat == STATUS_INFO_LENGTH_MISMATCH, "buffer too small: %08x\n", stat);
1380
1381 /* Query DEP */
1382 stat = pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val, &len);
1383 ok(stat == STATUS_SUCCESS, "querying DEP: status %08x\n", stat);
1384 if(stat == STATUS_SUCCESS)
1385 {
1386 ok(len == sizeof val, "returned length: %d\n", len);
1387 if(val & MEM_EXECUTE_OPTION_PERMANENT)
1388 {
1389 skip("toggling DEP impossible - status locked\n");
1390 is_permanent = TRUE;
1391 if(val & MEM_EXECUTE_OPTION_DISABLE)
1392 can_test_without = FALSE;
1393 else
1394 can_test_with = FALSE;
1395 }
1396 }
1397
1398 if(!is_permanent)
1399 {
1400 /* Enable DEP */
1401 val = MEM_EXECUTE_OPTION_DISABLE;
1402 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1403 ok(stat == STATUS_SUCCESS, "enabling DEP: status %08x\n", stat);
1404 }
1405
1406 if(can_test_with)
1407 {
1408 /* Try access to locked page with DEP on*/
1409 info.exception_caught = FALSE;
1410 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1411 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1412 ok(info.exception_info == EXCEPTION_READ_FAULT ||
1413 info.exception_info == EXCEPTION_EXECUTE_FAULT,
1414 "Access violation type: %08x\n", (unsigned)info.exception_info);
1415 has_hw_support = info.exception_info == EXCEPTION_EXECUTE_FAULT;
1416 trace("DEP hardware support: %s\n", has_hw_support?"Yes":"No");
1417
1418 /* Try execution of data with DEP on*/
1419 info.exception_caught = FALSE;
1420 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1421 if(has_hw_support)
1422 {
1423 ok(info.exception_caught == TRUE, "Execution of data memory succeeded\n");
1424 ok(info.exception_info == EXCEPTION_EXECUTE_FAULT,
1425 "Access violation type: %08x\n", (unsigned)info.exception_info);
1426 }
1427 else
1428 ok(info.exception_caught == FALSE, "Execution trapped without hardware support\n");
1429 }
1430 else
1431 skip("DEP is in AlwaysOff state\n");
1432
1433 if(!is_permanent)
1434 {
1435 /* Disable DEP */
1436 val = MEM_EXECUTE_OPTION_ENABLE;
1437 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1438 ok(stat == STATUS_SUCCESS, "disabling DEP: status %08x\n", stat);
1439 }
1440
1441 /* page is read without exec here */
1442 if(can_test_without)
1443 {
1444 /* Try execution of data with DEP off */
1445 info.exception_caught = FALSE;
1446 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_READWRITE);
1447 ok(info.exception_caught == FALSE, "Execution trapped with DEP turned off\n");
1448
1449 /* Try access to locked page with DEP off - error code is different than
1450 with hardware DEP on */
1451 info.exception_caught = FALSE;
1452 run_exception_test(dpe_exception_handler, &info, single_ret, sizeof(single_ret), PAGE_NOACCESS);
1453 ok(info.exception_caught == TRUE, "Execution of disabled memory succeeded\n");
1454 ok(info.exception_info == EXCEPTION_READ_FAULT,
1455 "Access violation type: %08x\n", (unsigned)info.exception_info);
1456 }
1457 else
1458 skip("DEP is in AlwaysOn state\n");
1459
1460 if(!is_permanent)
1461 {
1462 /* Turn off DEP permanently */
1463 val = MEM_EXECUTE_OPTION_ENABLE | MEM_EXECUTE_OPTION_PERMANENT;
1464 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1465 ok(stat == STATUS_SUCCESS, "disabling DEP permanently: status %08x\n", stat);
1466 }
1467
1468 /* Try to turn off DEP */
1469 val = MEM_EXECUTE_OPTION_ENABLE;
1470 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1471 ok(stat == STATUS_ACCESS_DENIED, "disabling DEP while permanent: status %08x\n", stat);
1472
1473 /* Try to turn on DEP */
1474 val = MEM_EXECUTE_OPTION_DISABLE;
1475 stat = pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags, &val, sizeof val);
1476 ok(stat == STATUS_ACCESS_DENIED, "enabling DEP while permanent: status %08x\n", stat);
1477 }
1478
1479 static void test_thread_context(void)
1480 {
1481 CONTEXT context;
1482 NTSTATUS status;
1483 struct expected
1484 {
1485 DWORD Eax, Ebx, Ecx, Edx, Esi, Edi, Ebp, Esp, Eip,
1486 SegCs, SegDs, SegEs, SegFs, SegGs, SegSs, EFlags, prev_frame;
1487 } expect;
1488 NTSTATUS (*func_ptr)( struct expected *res, void *func, void *arg1, void *arg2 ) = (void *)code_mem;
1489
1490 static const BYTE call_func[] =
1491 {
1492 0x55, /* pushl %ebp */
1493 0x89, 0xe5, /* mov %esp,%ebp */
1494 0x50, /* pushl %eax ; add a bit of offset to the stack */
1495 0x50, /* pushl %eax */
1496 0x50, /* pushl %eax */
1497 0x50, /* pushl %eax */
1498 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
1499 0x8f, 0x00, /* popl (%eax) */
1500 0x89, 0x58, 0x04, /* mov %ebx,0x4(%eax) */
1501 0x89, 0x48, 0x08, /* mov %ecx,0x8(%eax) */
1502 0x89, 0x50, 0x0c, /* mov %edx,0xc(%eax) */
1503 0x89, 0x70, 0x10, /* mov %esi,0x10(%eax) */
1504 0x89, 0x78, 0x14, /* mov %edi,0x14(%eax) */
1505 0x89, 0x68, 0x18, /* mov %ebp,0x18(%eax) */
1506 0x89, 0x60, 0x1c, /* mov %esp,0x1c(%eax) */
1507 0xff, 0x75, 0x04, /* pushl 0x4(%ebp) */
1508 0x8f, 0x40, 0x20, /* popl 0x20(%eax) */
1509 0x8c, 0x48, 0x24, /* mov %cs,0x24(%eax) */
1510 0x8c, 0x58, 0x28, /* mov %ds,0x28(%eax) */
1511 0x8c, 0x40, 0x2c, /* mov %es,0x2c(%eax) */
1512 0x8c, 0x60, 0x30, /* mov %fs,0x30(%eax) */
1513 0x8c, 0x68, 0x34, /* mov %gs,0x34(%eax) */
1514 0x8c, 0x50, 0x38, /* mov %ss,0x38(%eax) */
1515 0x9c, /* pushf */
1516 0x8f, 0x40, 0x3c, /* popl 0x3c(%eax) */
1517 0xff, 0x75, 0x00, /* pushl 0x0(%ebp) ; previous stack frame */
1518 0x8f, 0x40, 0x40, /* popl 0x40(%eax) */
1519 0x8b, 0x00, /* mov (%eax),%eax */
1520 0xff, 0x75, 0x14, /* pushl 0x14(%ebp) */
1521 0xff, 0x75, 0x10, /* pushl 0x10(%ebp) */
1522 0xff, 0x55, 0x0c, /* call *0xc(%ebp) */
1523 0xc9, /* leave */
1524 0xc3, /* ret */
1525 };
1526
1527 memcpy( func_ptr, call_func, sizeof(call_func) );
1528
1529 #define COMPARE(reg) \
1530 ok( context.reg == expect.reg, "wrong " #reg " %08x/%08x\n", context.reg, expect.reg )
1531
1532 memset( &context, 0xcc, sizeof(context) );
1533 memset( &expect, 0xcc, sizeof(expect) );
1534 func_ptr( &expect, pRtlCaptureContext, &context, 0 );
1535 trace( "expect: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x esp=%08x "
1536 "eip=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x ss=%04x flags=%08x prev=%08x\n",
1537 expect.Eax, expect.Ebx, expect.Ecx, expect.Edx, expect.Esi, expect.Edi,
1538 expect.Ebp, expect.Esp, expect.Eip, expect.SegCs, expect.SegDs, expect.SegEs,
1539 expect.SegFs, expect.SegGs, expect.SegSs, expect.EFlags, expect.prev_frame );
1540 trace( "actual: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x esp=%08x "
1541 "eip=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x ss=%04x flags=%08x\n",
1542 context.Eax, context.Ebx, context.Ecx, context.Edx, context.Esi, context.Edi,
1543 context.Ebp, context.Esp, context.Eip, context.SegCs, context.SegDs, context.SegEs,
1544 context.SegFs, context.SegGs, context.SegSs, context.EFlags );
1545
1546 ok( context.ContextFlags == (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS) ||
1547 broken( context.ContextFlags == 0xcccccccc ), /* <= vista */
1548 "wrong flags %08x\n", context.ContextFlags );
1549 COMPARE( Eax );
1550 COMPARE( Ebx );
1551 COMPARE( Ecx );
1552 COMPARE( Edx );
1553 COMPARE( Esi );
1554 COMPARE( Edi );
1555 COMPARE( Eip );
1556 COMPARE( SegCs );
1557 COMPARE( SegDs );
1558 COMPARE( SegEs );
1559 COMPARE( SegFs );
1560 COMPARE( SegGs );
1561 COMPARE( SegSs );
1562 COMPARE( EFlags );
1563 /* Ebp is from the previous stackframe */
1564 ok( context.Ebp == expect.prev_frame, "wrong Ebp %08x/%08x\n", context.Ebp, expect.prev_frame );
1565 /* Esp is the value on entry to the previous stackframe */
1566 ok( context.Esp == expect.Ebp + 8, "wrong Esp %08x/%08x\n", context.Esp, expect.Ebp + 8 );
1567
1568 memset( &context, 0xcc, sizeof(context) );
1569 memset( &expect, 0xcc, sizeof(expect) );
1570 context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS;
1571 status = func_ptr( &expect, pNtGetContextThread, (void *)GetCurrentThread(), &context );
1572 ok( status == STATUS_SUCCESS, "NtGetContextThread failed %08x\n", status );
1573 trace( "expect: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x esp=%08x "
1574 "eip=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x ss=%04x flags=%08x prev=%08x\n",
1575 expect.Eax, expect.Ebx, expect.Ecx, expect.Edx, expect.Esi, expect.Edi,
1576 expect.Ebp, expect.Esp, expect.Eip, expect.SegCs, expect.SegDs, expect.SegEs,
1577 expect.SegFs, expect.SegGs, expect.SegSs, expect.EFlags, expect.prev_frame );
1578 trace( "actual: eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x esp=%08x "
1579 "eip=%08x cs=%04x ds=%04x es=%04x fs=%04x gs=%04x ss=%04x flags=%08x\n",
1580 context.Eax, context.Ebx, context.Ecx, context.Edx, context.Esi, context.Edi,
1581 context.Ebp, context.Esp, context.Eip, context.SegCs, context.SegDs, context.SegEs,
1582 context.SegFs, context.SegGs, context.SegSs, context.EFlags );
1583 /* Eax, Ecx, Edx, EFlags are not preserved */
1584 COMPARE( Ebx );
1585 COMPARE( Esi );
1586 COMPARE( Edi );
1587 COMPARE( Ebp );
1588 /* Esp is the stack upon entry to NtGetContextThread */
1589 ok( context.Esp == expect.Esp - 12 || context.Esp == expect.Esp - 16,
1590 "wrong Esp %08x/%08x\n", context.Esp, expect.Esp );
1591 /* Eip is somewhere close to the NtGetContextThread implementation */
1592 ok( (char *)context.Eip >= (char *)pNtGetContextThread - 0x10000 &&
1593 (char *)context.Eip <= (char *)pNtGetContextThread + 0x10000,
1594 "wrong Eip %08x/%08x\n", context.Eip, (DWORD)pNtGetContextThread );
1595 ok( *(WORD *)context.Eip == 0xc483 || *(WORD *)context.Eip == 0x08c2 || *(WORD *)context.Eip == 0x8dc3,
1596 "expected 0xc483 or 0x08c2 or 0x8dc3, got %04x\n", *(WORD *)context.Eip );
1597 /* segment registers clear the high word */
1598 ok( context.SegCs == LOWORD(expect.SegCs), "wrong SegCs %08x/%08x\n", context.SegCs, expect.SegCs );
1599 ok( context.SegDs == LOWORD(expect.SegDs), "wrong SegDs %08x/%08x\n", context.SegDs, expect.SegDs );
1600 ok( context.SegEs == LOWORD(expect.SegEs), "wrong SegEs %08x/%08x\n", context.SegEs, expect.SegEs );
1601 ok( context.SegFs == LOWORD(expect.SegFs), "wrong SegFs %08x/%08x\n", context.SegFs, expect.SegFs );
1602 ok( context.SegGs == LOWORD(expect.SegGs), "wrong SegGs %08x/%08x\n", context.SegGs, expect.SegGs );
1603 ok( context.SegSs == LOWORD(expect.SegSs), "wrong SegSs %08x/%08x\n", context.SegSs, expect.SegGs );
1604 #undef COMPARE
1605 }
1606
1607 #elif defined(__x86_64__)
1608
1609 #define is_wow64 0
1610
1611 #define UNW_FLAG_NHANDLER 0
1612 #define UNW_FLAG_EHANDLER 1
1613 #define UNW_FLAG_UHANDLER 2
1614 #define UNW_FLAG_CHAININFO 4
1615
1616 #define UWOP_PUSH_NONVOL 0
1617 #define UWOP_ALLOC_LARGE 1
1618 #define UWOP_ALLOC_SMALL 2
1619 #define UWOP_SET_FPREG 3
1620 #define UWOP_SAVE_NONVOL 4
1621 #define UWOP_SAVE_NONVOL_FAR 5
1622 #define UWOP_SAVE_XMM128 8
1623 #define UWOP_SAVE_XMM128_FAR 9
1624 #define UWOP_PUSH_MACHFRAME 10
1625
1626 struct results
1627 {
1628 int rip_offset; /* rip offset from code start */
1629 int rbp_offset; /* rbp offset from stack pointer */
1630 int handler; /* expect handler to be set? */
1631 int rip; /* expected final rip value */
1632 int frame; /* expected frame return value */
1633 int regs[8][2]; /* expected values for registers */
1634 };
1635
1636 struct unwind_test
1637 {
1638 const BYTE *function;
1639 size_t function_size;
1640 const BYTE *unwind_info;
1641 const struct results *results;
1642 unsigned int nb_results;
1643 };
1644
1645 enum regs
1646 {
1647 rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
1648 r8, r9, r10, r11, r12, r13, r14, r15
1649 };
1650
1651 static const char * const reg_names[16] =
1652 {
1653 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1654 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1655 };
1656
1657 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1658
1659 static void call_virtual_unwind( int testnum, const struct unwind_test *test )
1660 {
1661 static const int code_offset = 1024;
1662 static const int unwind_offset = 2048;
1663 void *handler, *data;
1664 CONTEXT context;
1665 RUNTIME_FUNCTION runtime_func;
1666 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr;
1667 UINT i, j, k;
1668 ULONG64 fake_stack[256];
1669 ULONG64 frame, orig_rip, orig_rbp, unset_reg;
1670 UINT unwind_size = 4 + 2 * test->unwind_info[2] + 8;
1671
1672 memcpy( (char *)code_mem + code_offset, test->function, test->function_size );
1673 memcpy( (char *)code_mem + unwind_offset, test->unwind_info, unwind_size );
1674
1675 runtime_func.BeginAddress = code_offset;
1676 runtime_func.EndAddress = code_offset + test->function_size;
1677 runtime_func.UnwindData = unwind_offset;
1678
1679 trace( "code: %p stack: %p\n", code_mem, fake_stack );
1680
1681 for (i = 0; i < test->nb_results; i++)
1682 {
1683 memset( &ctx_ptr, 0, sizeof(ctx_ptr) );
1684 memset( &context, 0x55, sizeof(context) );
1685 memset( &unset_reg, 0x55, sizeof(unset_reg) );
1686 for (j = 0; j < 256; j++) fake_stack[j] = j * 8;
1687
1688 context.Rsp = (ULONG_PTR)fake_stack;
1689 context.Rbp = (ULONG_PTR)fake_stack + test->results[i].rbp_offset;
1690 orig_rbp = context.Rbp;
1691 orig_rip = (ULONG64)code_mem + code_offset + test->results[i].rip_offset;
1692
1693 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum, i,
1694 (void *)orig_rip, *(BYTE *)orig_rip, (void *)orig_rbp, (void *)context.Rsp );
1695
1696 data = (void *)0xdeadbeef;
1697 handler = RtlVirtualUnwind( UNW_FLAG_EHANDLER, (ULONG64)code_mem, orig_rip,
1698 &runtime_func, &context, &data, &frame, &ctx_ptr );
1699 if (test->results[i].handler)
1700 {
1701 ok( (char *)handler == (char *)code_mem + 0x200,
1702 "%u/%u: wrong handler %p/%p\n", testnum, i, handler, (char *)code_mem + 0x200 );
1703 if (handler) ok( *(DWORD *)data == 0x08070605,
1704 "%u/%u: wrong handler data %p\n", testnum, i, data );
1705 }
1706 else
1707 {
1708 ok( handler == NULL, "%u/%u: handler %p instead of NULL\n", testnum, i, handler );
1709 ok( data == (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum, i, data );
1710 }
1711
1712 ok( context.Rip == test->results[i].rip, "%u/%u: wrong rip %p/%x\n",
1713 testnum, i, (void *)context.Rip, test->results[i].rip );
1714 ok( frame == (ULONG64)fake_stack + test->results[i].frame, "%u/%u: wrong frame %p/%p\n",
1715 testnum, i, (void *)frame, (char *)fake_stack + test->results[i].frame );
1716
1717 for (j = 0; j < 16; j++)
1718 {
1719 static const UINT nb_regs = sizeof(test->results[i].regs) / sizeof(test->results[i].regs[0]);
1720
1721 for (k = 0; k < nb_regs; k++)
1722 {
1723 if (test->results[i].regs[k][0] == -1)
1724 {
1725 k = nb_regs;
1726 break;
1727 }
1728 if (test->results[i].regs[k][0] == j) break;
1729 }
1730
1731 if (j == rsp) /* rsp is special */
1732 {
1733 ok( !ctx_ptr.u2.IntegerContext[j],
1734 "%u/%u: rsp should not be set in ctx_ptr\n", testnum, i );
1735 ok( context.Rsp == (ULONG64)fake_stack + test->results[i].regs[k][1],
1736 "%u/%u: register rsp wrong %p/%p\n",
1737 testnum, i, (void *)context.Rsp, (char *)fake_stack + test->results[i].regs[k][1] );
1738 continue;
1739 }
1740
1741 if (ctx_ptr.u2.IntegerContext[j])
1742 {
1743 ok( k < nb_regs, "%u/%u: register %s should not be set to %lx\n",
1744 testnum, i, reg_names[j], *(&context.Rax + j) );
1745 if (k < nb_regs)
1746 ok( *(&context.Rax + j) == test->results[i].regs[k][1],
1747 "%u/%u: register %s wrong %p/%x\n",
1748 testnum, i, reg_names[j], (void *)*(&context.Rax + j), test->results[i].regs[k][1] );
1749 }
1750 else
1751 {
1752 ok( k == nb_regs, "%u/%u: register %s should be set\n", testnum, i, reg_names[j] );
1753 if (j == rbp)
1754 ok( context.Rbp == orig_rbp, "%u/%u: register rbp wrong %p/unset\n",
1755 testnum, i, (void *)context.Rbp );
1756 else
1757 ok( *(&context.Rax + j) == unset_reg,
1758 "%u/%u: register %s wrong %p/unset\n",
1759 testnum, i, reg_names[j], (void *)*(&context.Rax + j));
1760 }
1761 }
1762 }
1763 }
1764
1765 static void test_virtual_unwind(void)
1766 {
1767 static const BYTE function_0[] =
1768 {
1769 0xff, 0xf5, /* 00: push %rbp */
1770 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1771 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1772 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1773 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1774 0x90, /* 1c: nop */
1775 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1776 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1777 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1778 0x5d, /* 32: pop %rbp */
1779 0xc3 /* 33: ret */
1780 };
1781
1782 static const BYTE unwind_info_0[] =
1783 {
1784 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1785 0x1c, /* prolog size */
1786 8, /* opcode count */
1787 (0x03 << 4) | rbp, /* frame reg rbp offset 0x30 */
1788
1789 0x1c, UWOP(SAVE_NONVOL, rsi), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1790 0x15, UWOP(SAVE_NONVOL, rbx), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1791 0x0e, UWOP(SET_FPREG, rbp), /* 0e: lea 0x30(%rsp),rbp */
1792 0x09, UWOP(ALLOC_LARGE, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1793 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1794
1795 0x00, 0x02, 0x00, 0x00, /* handler */
1796 0x05, 0x06, 0x07, 0x08, /* data */
1797 };
1798
1799 static const struct results results_0[] =
1800 {
1801 /* offset rbp handler rip frame registers */
1802 { 0x00, 0x40, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1803 { 0x02, 0x40, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbp,0x000}, {-1,-1} }},
1804 { 0x09, 0x40, FALSE, 0x118, 0x000, { {rsp,0x120}, {rbp,0x110}, {-1,-1} }},
1805 { 0x0e, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1} }},
1806 { 0x15, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {-1,-1} }},
1807 { 0x1c, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1808 { 0x1d, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1809 { 0x24, 0x40, TRUE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {rbx,0x130}, {rsi,0x138}, {-1,-1}}},
1810 { 0x2b, 0x40, FALSE, 0x128, 0x010, { {rsp,0x130}, {rbp,0x120}, {-1,-1}}},
1811 { 0x32, 0x40, FALSE, 0x008, 0x010, { {rsp,0x010}, {rbp,0x000}, {-1,-1}}},
1812 { 0x33, 0x40, FALSE, 0x000, 0x010, { {rsp,0x008}, {-1,-1}}},
1813 };
1814
1815
1816 static const BYTE function_1[] =
1817 {
1818 0x53, /* 00: push %rbx */
1819 0x55, /* 01: push %rbp */
1820 0x56, /* 02: push %rsi */
1821 0x57, /* 03: push %rdi */
1822 0x41, 0x54, /* 04: push %r12 */
1823 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1824 0x90, 0x90, /* 0a: nop; nop */
1825 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1826 0x41, 0x5c, /* 10: pop %r12 */
1827 0x5f, /* 12: pop %rdi */
1828 0x5e, /* 13: pop %rsi */
1829 0x5d, /* 14: pop %rbp */
1830 0x5b, /* 15: pop %rbx */
1831 0xc3 /* 16: ret */
1832 };
1833
1834 static const BYTE unwind_info_1[] =
1835 {
1836 1 | (UNW_FLAG_EHANDLER << 3), /* version + flags */
1837 0x0a, /* prolog size */
1838 6, /* opcode count */
1839 0, /* frame reg */
1840
1841 0x0a, UWOP(ALLOC_SMALL, 5), /* 0a: sub $0x30,%rsp */
1842 0x06, UWOP(PUSH_NONVOL, r12), /* 06: push %r12 */
1843 0x04, UWOP(PUSH_NONVOL, rdi), /* 04: push %rdi */
1844 0x03, UWOP(PUSH_NONVOL, rsi), /* 03: push %rsi */
1845 0x02, UWOP(PUSH_NONVOL, rbp), /* 02: push %rbp */
1846 0x01, UWOP(PUSH_NONVOL, rbx), /* 01: push %rbx */
1847
1848 0x00, 0x02, 0x00, 0x00, /* handler */
1849 0x05, 0x06, 0x07, 0x08, /* data */
1850 };
1851
1852 static const struct results results_1[] =
1853 {
1854 /* offset rbp handler rip frame registers */
1855 { 0x00, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1856 { 0x01, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1857 { 0x02, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1858 { 0x03, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1859 { 0x04, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1860 { 0x06, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1861 { 0x0a, 0x50, TRUE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1862 { 0x0c, 0x50, FALSE, 0x058, 0x000, { {rsp,0x060}, {rbx,0x050}, {rbp,0x048}, {rsi,0x040}, {rdi,0x038}, {r12,0x030}, {-1,-1} }},
1863 { 0x10, 0x50, FALSE, 0x028, 0x000, { {rsp,0x030}, {rbx,0x020}, {rbp,0x018}, {rsi,0x010}, {rdi,0x008}, {r12,0x000}, {-1,-1} }},
1864 { 0x12, 0x50, FALSE, 0x020, 0x000, { {rsp,0x028}, {rbx,0x018}, {rbp,0x010}, {rsi,0x008}, {rdi,0x000}, {-1,-1} }},
1865 { 0x13, 0x50, FALSE, 0x018, 0x000, { {rsp,0x020}, {rbx,0x010}, {rbp,0x008}, {rsi,0x000}, {-1,-1} }},
1866 { 0x14, 0x50, FALSE, 0x010, 0x000, { {rsp,0x018}, {rbx,0x008}, {rbp,0x000}, {-1,-1} }},
1867 { 0x15, 0x50, FALSE, 0x008, 0x000, { {rsp,0x010}, {rbx,0x000}, {-1,-1} }},
1868 { 0x16, 0x50, FALSE, 0x000, 0x000, { {rsp,0x008}, {-1,-1} }},
1869 };
1870
1871 static const struct unwind_test tests[] =
1872 {
1873 { function_0, sizeof(function_0), unwind_info_0,
1874 results_0, sizeof(results_0)/sizeof(results_0[0]) },
1875 { function_1, sizeof(function_1), unwind_info_1,
1876 results_1, sizeof(results_1)/sizeof(results_1[0]) }
1877 };
1878 unsigned int i;
1879
1880 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
1881 call_virtual_unwind( i, &tests[i] );
1882 }
1883
1884 static int consolidate_dummy_called;
1885 static PVOID CALLBACK test_consolidate_dummy(EXCEPTION_RECORD *rec)
1886 {
1887 CONTEXT *ctx = (CONTEXT *)rec->ExceptionInformation[1];
1888 consolidate_dummy_called = 1;
1889 ok(ctx->Rip == 0xdeadbeef, "test_consolidate_dummy failed for Rip, expected: 0xdeadbeef, got: %lx\n", ctx->Rip);
1890 return (PVOID)rec->ExceptionInformation[2];
1891 }
1892
1893 static void test_restore_context(void)
1894 {
1895 SETJMP_FLOAT128 *fltsave;
1896 EXCEPTION_RECORD rec;
1897 _JUMP_BUFFER buf;
1898 CONTEXT ctx;
1899 int i, pass;
1900
1901 if (!pRtlUnwindEx || !pRtlRestoreContext || !pRtlCaptureContext || !p_setjmp)
1902 {
1903 skip("RtlUnwindEx/RtlCaptureContext/RtlRestoreContext/_setjmp not found\n");
1904 return;
1905 }
1906
1907 /* RtlRestoreContext(NULL, NULL); crashes on Windows */
1908
1909 /* test simple case of capture and restore context */
1910 pass = 0;
1911 InterlockedIncrement(&pass); /* interlocked to prevent compiler from moving after capture */
1912 pRtlCaptureContext(&ctx);
1913 if (InterlockedIncrement(&pass) == 2) /* interlocked to prevent compiler from moving before capture */
1914 {
1915 pRtlRestoreContext(&ctx, NULL);
1916 ok(0, "shouldn't be reached\n");
1917 }
1918 else
1919 ok(pass < 4, "unexpected pass %d\n", pass);
1920
1921 /* test with jmp using RltRestoreContext */
1922 pass = 0;
1923 InterlockedIncrement(&pass);
1924 RtlCaptureContext(&ctx);
1925 InterlockedIncrement(&pass); /* only called once */
1926 p_setjmp(&buf);
1927 InterlockedIncrement(&pass);
1928 if (pass == 3)
1929 {
1930 rec.ExceptionCode = STATUS_LONGJUMP;
1931 rec.NumberParameters = 1;
1932 rec.ExceptionInformation[0] = (DWORD64)&buf;
1933
1934 /* uses buf.Rip instead of ctx.Rip */
1935 pRtlRestoreContext(&ctx, &rec);
1936 ok(0, "shouldn't be reached\n");
1937 }
1938 else if (pass == 4)
1939 {
1940 ok(buf.Rbx == ctx.Rbx, "longjmp failed for Rbx, expected: %lx, got: %lx\n", buf.Rbx, ctx.Rbx);
1941 ok(buf.Rsp == ctx.Rsp, "longjmp failed for Rsp, expected: %lx, got: %lx\n", buf.Rsp, ctx.Rsp);
1942 ok(buf.Rbp == ctx.Rbp, "longjmp failed for Rbp, expected: %lx, got: %lx\n", buf.Rbp, ctx.Rbp);
1943 ok(buf.Rsi == ctx.Rsi, "longjmp failed for Rsi, expected: %lx, got: %lx\n", buf.Rsi, ctx.Rsi);
1944 ok(buf.Rdi == ctx.Rdi, "longjmp failed for Rdi, expected: %lx, got: %lx\n", buf.Rdi, ctx.Rdi);
1945 ok(buf.R12 == ctx.R12, "longjmp failed for R12, expected: %lx, got: %lx\n", buf.R12, ctx.R12);
1946 ok(buf.R13 == ctx.R13, "longjmp failed for R13, expected: %lx, got: %lx\n", buf.R13, ctx.R13);
1947 ok(buf.R14 == ctx.R14, "longjmp failed for R14, expected: %lx, got: %lx\n", buf.R14, ctx.R14);
1948 ok(buf.R15 == ctx.R15, "longjmp failed for R15, expected: %lx, got: %lx\n", buf.R15, ctx.R15);
1949
1950 fltsave = &buf.Xmm6;
1951 for (i = 0; i < 10; i++)
1952 {
1953 ok(fltsave[i].Part[0] == ctx.u.FltSave.XmmRegisters[i + 6].Low,
1954 "longjmp failed for Xmm%d, expected %lx, got %lx\n", i + 6,
1955 fltsave[i].Part[0], ctx.u.FltSave.XmmRegisters[i + 6].Low);
1956
1957 ok(fltsave[i].Part[1] == ctx.u.FltSave.XmmRegisters[i + 6].High,
1958 "longjmp failed for Xmm%d, expected %lx, got %lx\n", i + 6,
1959 fltsave[i].Part[1], ctx.u.FltSave.XmmRegisters[i + 6].High);
1960 }
1961 }
1962 else
1963 ok(0, "unexpected pass %d\n", pass);
1964
1965 /* test with jmp through RtlUnwindEx */
1966 pass = 0;
1967 InterlockedIncrement(&pass);
1968 pRtlCaptureContext(&ctx);
1969 InterlockedIncrement(&pass); /* only called once */
1970 p_setjmp(&buf);
1971 InterlockedIncrement(&pass);
1972 if (pass == 3)
1973 {
1974 rec.ExceptionCode = STATUS_LONGJUMP;
1975 rec.NumberParameters = 1;
1976 rec.ExceptionInformation[0] = (DWORD64)&buf;
1977
1978 /* uses buf.Rip instead of bogus 0xdeadbeef */
1979 pRtlUnwindEx((void*)buf.Rsp, (void*)0xdeadbeef, &rec, NULL, &ctx, NULL);
1980 ok(0, "shouldn't be reached\n");
1981 }
1982 else
1983 ok(pass == 4, "unexpected pass %d\n", pass);
1984
1985
1986 /* test with consolidate */
1987 pass = 0;
1988 InterlockedIncrement(&pass);
1989 RtlCaptureContext(&ctx);
1990 InterlockedIncrement(&pass);
1991 if (pass == 2)
1992 {
1993 rec.ExceptionCode = STATUS_UNWIND_CONSOLIDATE;
1994 rec.NumberParameters = 3;
1995 rec.ExceptionInformation[0] = (DWORD64)test_consolidate_dummy;
1996 rec.ExceptionInformation[1] = (DWORD64)&ctx;
1997 rec.ExceptionInformation[2] = ctx.Rip;
1998 ctx.Rip = 0xdeadbeef;
1999
2000 pRtlRestoreContext(&ctx, &rec);
2001 ok(0, "shouldn't be reached\n");
2002 }
2003 else if (pass == 3)
2004 ok(consolidate_dummy_called, "test_consolidate_dummy not called\n");
2005 else
2006 ok(0, "unexpected pass %d\n", pass);
2007 }
2008
2009 static RUNTIME_FUNCTION* CALLBACK dynamic_unwind_callback( DWORD64 pc, PVOID context )
2010 {
2011 static const int code_offset = 1024;
2012 static RUNTIME_FUNCTION runtime_func;
2013 (*(DWORD *)context)++;
2014
2015 runtime_func.BeginAddress = code_offset + 16;
2016 runtime_func.EndAddress = code_offset + 32;
2017 runtime_func.UnwindData = 0;
2018 return &runtime_func;
2019 }
2020
2021 static void test_dynamic_unwind(void)
2022 {
2023 static const int code_offset = 1024;
2024 char buf[sizeof(RUNTIME_FUNCTION) + 4];
2025 RUNTIME_FUNCTION *runtime_func, *func;
2026 ULONG_PTR table, base;
2027 DWORD count;
2028
2029 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
2030 runtime_func = (RUNTIME_FUNCTION *)buf;
2031 runtime_func->BeginAddress = code_offset;
2032 runtime_func->EndAddress = code_offset + 16;
2033 runtime_func->UnwindData = 0;
2034 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
2035 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
2036
2037 /* Lookup function outside of any function table */
2038 base = 0xdeadbeef;
2039 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
2040 ok( func == NULL,
2041 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
2042 ok( !base || broken(base == 0xdeadbeef),
2043 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
2044
2045 /* Test with pointer inside of our function */
2046 base = 0xdeadbeef;
2047 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 8, &base, NULL );
2048 ok( func == runtime_func,
2049 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func, func );
2050 ok( base == (ULONG_PTR)code_mem,
2051 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
2052
2053 /* Test RtlDeleteFunctionTable */
2054 ok( pRtlDeleteFunctionTable( runtime_func ),
2055 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func );
2056 ok( !pRtlDeleteFunctionTable( runtime_func ),
2057 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
2058
2059 /* Unaligned RUNTIME_FUNCTION pointer */
2060 runtime_func = (RUNTIME_FUNCTION *)((ULONG_PTR)buf | 0x3);
2061 runtime_func->BeginAddress = code_offset;
2062 runtime_func->EndAddress = code_offset + 16;
2063 runtime_func->UnwindData = 0;
2064 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
2065 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
2066 ok( pRtlDeleteFunctionTable( runtime_func ),
2067 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func );
2068
2069 /* Attempt to insert the same entry twice */
2070 runtime_func = (RUNTIME_FUNCTION *)buf;
2071 runtime_func->BeginAddress = code_offset;
2072 runtime_func->EndAddress = code_offset + 16;
2073 runtime_func->UnwindData = 0;
2074 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
2075 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
2076 ok( pRtlAddFunctionTable( runtime_func, 1, (ULONG_PTR)code_mem ),
2077 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
2078 ok( pRtlDeleteFunctionTable( runtime_func ),
2079 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func );
2080 ok( pRtlDeleteFunctionTable( runtime_func ),
2081 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func );
2082 ok( !pRtlDeleteFunctionTable( runtime_func ),
2083 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func );
2084
2085 /* Test RtlInstallFunctionTableCallback with both low bits unset */
2086 table = (ULONG_PTR)code_mem;
2087 ok( !pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
2088 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table );
2089
2090 /* Test RtlInstallFunctionTableCallback with both low bits set */
2091 table = (ULONG_PTR)code_mem | 0x3;
2092 ok( pRtlInstallFunctionTableCallback( table, (ULONG_PTR)code_mem, code_offset + 32, &dynamic_unwind_callback, (PVOID*)&count, NULL ),
2093 "RtlInstallFunctionTableCallback failed for table = %lx\n", table );
2094
2095 /* Lookup function outside of any function table */
2096 count = 0;
2097 base = 0xdeadbeef;
2098 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
2099 ok( func == NULL,
2100 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
2101 ok( !base || broken(base == 0xdeadbeef),
2102 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
2103 ok( !count,
2104 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
2105
2106 /* Test with pointer inside of our function */
2107 count = 0;
2108 base = 0xdeadbeef;
2109 func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 24, &base, NULL );
2110 ok( func != NULL && func->BeginAddress == code_offset + 16 && func->EndAddress == code_offset + 32,
2111 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func );
2112 ok( base == (ULONG_PTR)code_mem,
2113 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR)code_mem, base );
2114 ok( count == 1,
2115 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count );
2116
2117 /* Clean up again */
2118 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
2119 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID)table );
2120 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION)table ),
2121 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID)table );
2122
2123 }
2124
2125 static int termination_handler_called;
2126 static void WINAPI termination_handler(ULONG flags, ULONG64 frame)
2127 {
2128 termination_handler_called++;
2129
2130 ok(flags == 1 || broken(flags == 0x401), "flags = %x\n", flags);
2131 ok(frame == 0x1234, "frame = %p\n", (void*)frame);
2132 }
2133
2134 static void test___C_specific_handler(void)
2135 {
2136 DISPATCHER_CONTEXT dispatch;
2137 EXCEPTION_RECORD rec;
2138 CONTEXT context;
2139 ULONG64 frame;
2140 EXCEPTION_DISPOSITION ret;
2141 SCOPE_TABLE scope_table;
2142
2143 if (!p__C_specific_handler)
2144 {
2145 win_skip("__C_specific_handler not available\n");
2146 return;
2147 }
2148
2149 memset(&rec, 0, sizeof(rec));
2150 rec.ExceptionFlags = 2; /* EH_UNWINDING */
2151 frame = 0x1234;
2152 memset(&dispatch, 0, sizeof(dispatch));
2153 dispatch.ImageBase = (ULONG_PTR)GetModuleHandleA(NULL);
2154 dispatch.ControlPc = dispatch.ImageBase + 0x200;
2155 dispatch.HandlerData = &scope_table;
2156 dispatch.ContextRecord = &context;
2157 scope_table.Count = 1;
2158 scope_table.ScopeRecord[0].BeginAddress = 0x200;
2159 scope_table.ScopeRecord[0].EndAddress = 0x400;
2160 scope_table.ScopeRecord[0].HandlerAddress = (ULONG_PTR)termination_handler-dispatch.ImageBase;
2161 scope_table.ScopeRecord[0].JumpTarget = 0;
2162 memset(&context, 0, sizeof(context));
2163
2164 termination_handler_called = 0;
2165 ret = p__C_specific_handler(&rec, frame, &context, &dispatch);
2166 ok(ret == ExceptionContinueSearch, "__C_specific_handler returned %x\n", ret);
2167 ok(termination_handler_called == 1, "termination_handler_called = %d\n",
2168 termination_handler_called);
2169 ok(dispatch.ScopeIndex == 1, "dispatch.ScopeIndex = %d\n", dispatch.ScopeIndex);
2170
2171 ret = p__C_specific_handler(&rec, frame, &context, &dispatch);
2172 ok(ret == ExceptionContinueSearch, "__C_specific_handler returned %x\n", ret);
2173 ok(termination_handler_called == 1, "termination_handler_called = %d\n",
2174 termination_handler_called);
2175 ok(dispatch.ScopeIndex == 1, "dispatch.ScopeIndex = %d\n", dispatch.ScopeIndex);
2176 }
2177
2178 #endif /* __x86_64__ */
2179
2180 #if defined(__i386__) || defined(__x86_64__)
2181
2182 static DWORD WINAPI register_check_thread(void *arg)
2183 {
2184 NTSTATUS status;
2185 CONTEXT ctx;
2186
2187 memset(&ctx, 0, sizeof(ctx));
2188 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2189
2190 status = pNtGetContextThread(GetCurrentThread(), &ctx);
2191 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
2192 ok(!ctx.Dr0, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr0);
2193 ok(!ctx.Dr1, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr1);
2194 ok(!ctx.Dr2, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr2);
2195 ok(!ctx.Dr3, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr3);
2196 ok(!ctx.Dr6, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr6);
2197 ok(!ctx.Dr7, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr7);
2198
2199 return 0;
2200 }
2201
2202 static void test_debug_registers(void)
2203 {
2204 static const struct
2205 {
2206 ULONG_PTR dr0, dr1, dr2, dr3, dr6, dr7;
2207 }
2208 tests[] =
2209 {
2210 { 0x42424240, 0, 0x126bb070, 0x0badbad0, 0, 0xffff0115 },
2211 { 0x42424242, 0, 0x100f0fe7, 0x0abebabe, 0, 0x115 },
2212 };
2213 NTSTATUS status;
2214 CONTEXT ctx;
2215 HANDLE thread;
2216 int i;
2217
2218 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
2219 {
2220 memset(&ctx, 0, sizeof(ctx));
2221 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2222 ctx.Dr0 = tests[i].dr0;
2223 ctx.Dr1 = tests[i].dr1;
2224 ctx.Dr2 = tests[i].dr2;
2225 ctx.Dr3 = tests[i].dr3;
2226 ctx.Dr6 = tests[i].dr6;
2227 ctx.Dr7 = tests[i].dr7;
2228
2229 status = pNtSetContextThread(GetCurrentThread(), &ctx);
2230 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %08x\n", status);
2231
2232 memset(&ctx, 0, sizeof(ctx));
2233 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2234
2235 status = pNtGetContextThread(GetCurrentThread(), &ctx);
2236 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %08x\n", status);
2237 ok(ctx.Dr0 == tests[i].dr0, "test %d: expected %lx, got %lx\n", i, tests[i].dr0, (DWORD_PTR)ctx.Dr0);
2238 ok(ctx.Dr1 == tests[i].dr1, "test %d: expected %lx, got %lx\n", i, tests[i].dr1, (DWORD_PTR)ctx.Dr1);
2239 ok(ctx.Dr2 == tests[i].dr2, "test %d: expected %lx, got %lx\n", i, tests[i].dr2, (DWORD_PTR)ctx.Dr2);
2240 ok(ctx.Dr3 == tests[i].dr3, "test %d: expected %lx, got %lx\n", i, tests[i].dr3, (DWORD_PTR)ctx.Dr3);
2241 ok((ctx.Dr6 & 0xf00f) == tests[i].dr6, "test %d: expected %lx, got %lx\n", i, tests[i].dr6, (DWORD_PTR)ctx.Dr6);
2242 ok((ctx.Dr7 & ~0xdc00) == tests[i].dr7, "test %d: expected %lx, got %lx\n", i, tests[i].dr7, (DWORD_PTR)ctx.Dr7);
2243 }
2244
2245 memset(&ctx, 0, sizeof(ctx));
2246 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2247 ctx.Dr0 = 0xffffffff;
2248 ctx.Dr1 = 0xffffffff;
2249 ctx.Dr2 = 0xffffffff;
2250 ctx.Dr3 = 0xffffffff;
2251 ctx.Dr6 = 0xffffffff;
2252 ctx.Dr7 = 0x00000400;
2253 status = pNtSetContextThread(GetCurrentThread(), &ctx);
2254 ok(status == STATUS_SUCCESS, "NtSetContextThread failed with %x\n", status);
2255
2256 thread = CreateThread(NULL, 0, register_check_thread, NULL, CREATE_SUSPENDED, NULL);
2257 ok(thread != INVALID_HANDLE_VALUE, "CreateThread failed with %d\n", GetLastError());
2258
2259 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
2260 status = pNtGetContextThread(thread, &ctx);
2261 ok(status == STATUS_SUCCESS, "NtGetContextThread failed with %x\n", status);
2262 ok(!ctx.Dr0, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr0);
2263 ok(!ctx.Dr1, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr1);
2264 ok(!ctx.Dr2, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr2);
2265 ok(!ctx.Dr3, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr3);
2266 ok(!ctx.Dr6, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr6);
2267 ok(!ctx.Dr7, "expected 0, got %lx\n", (DWORD_PTR)ctx.Dr7);
2268
2269 ResumeThread(thread);
2270 WaitForSingleObject(thread, 10000);
2271 CloseHandle(thread);
2272 }
2273
2274 static DWORD outputdebugstring_exceptions;
2275
2276 static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
2277 {
2278 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2279 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2280
2281 ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
2282 rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
2283 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
2284 ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
2285 ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
2286 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
2287
2288 outputdebugstring_exceptions++;
2289 return EXCEPTION_CONTINUE_SEARCH;
2290 }
2291
2292 static void test_outputdebugstring(DWORD numexc)
2293 {
2294 PVOID vectored_handler;
2295
2296 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
2297 {
2298 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
2299 return;
2300 }
2301
2302 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
2303 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2304
2305 outputdebugstring_exceptions = 0;
2306 OutputDebugStringA("Hello World");
2307
2308 ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
2309 outputdebugstring_exceptions, numexc);
2310
2311 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2312 }
2313
2314 static DWORD ripevent_exceptions;
2315
2316 static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
2317 {
2318 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2319 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2320
2321 ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
2322 rec->ExceptionCode, DBG_RIPEXCEPTION);
2323 ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
2324 ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
2325 (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
2326 ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
2327 (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
2328
2329 ripevent_exceptions++;
2330 return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2331 }
2332
2333 static void test_ripevent(DWORD numexc)
2334 {
2335 EXCEPTION_RECORD record;
2336 PVOID vectored_handler;
2337
2338 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
2339 {
2340 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
2341 return;
2342 }
2343
2344 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
2345 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2346
2347 record.ExceptionCode = DBG_RIPEXCEPTION;
2348 record.ExceptionFlags = 0;
2349 record.ExceptionRecord = NULL;
2350 record.ExceptionAddress = NULL;
2351 record.NumberParameters = 2;
2352 record.ExceptionInformation[0] = 0x11223344;
2353 record.ExceptionInformation[1] = 0x55667788;
2354
2355 ripevent_exceptions = 0;
2356 pRtlRaiseException(&record);
2357 ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
2358 ripevent_exceptions, numexc);
2359
2360 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2361 }
2362
2363 static DWORD debug_service_exceptions;
2364
2365 static LONG CALLBACK debug_service_handler(EXCEPTION_POINTERS *ExceptionInfo)
2366 {
2367 EXCEPTION_RECORD *rec = ExceptionInfo->ExceptionRecord;
2368 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2369
2370 ok(rec->ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode is %08x instead of %08x\n",
2371 rec->ExceptionCode, EXCEPTION_BREAKPOINT);
2372
2373 #ifdef __i386__
2374 ok(ExceptionInfo->ContextRecord->Eip == (DWORD)code_mem + 0x1c,
2375 "expected Eip = %x, got %x\n", (DWORD)code_mem + 0x1c, ExceptionInfo->ContextRecord->Eip);
2376 ok(rec->NumberParameters == (is_wow64 ? 1 : 3),
2377 "ExceptionParameters is %d instead of %d\n", rec->NumberParameters, is_wow64 ? 1 : 3);
2378 ok(rec->ExceptionInformation[0] == ExceptionInfo->ContextRecord->Eax,
2379 "expected ExceptionInformation[0] = %x, got %lx\n",
2380 ExceptionInfo->ContextRecord->Eax, rec->ExceptionInformation[0]);
2381 if (!is_wow64)
2382 {
2383 ok(rec->ExceptionInformation[1] == 0x11111111,
2384 "got ExceptionInformation[1] = %lx\n", rec->ExceptionInformation[1]);
2385 ok(rec->ExceptionInformation[2] == 0x22222222,
2386 "got ExceptionInformation[2] = %lx\n", rec->ExceptionInformation[2]);
2387 }
2388 #else
2389 ok(ExceptionInfo->ContextRecord->Rip == (DWORD_PTR)code_mem + 0x2f,
2390 "expected Rip = %lx, got %lx\n", (DWORD_PTR)code_mem + 0x2f, ExceptionInfo->ContextRecord->Rip);
2391 ok(rec->NumberParameters == 1,
2392 "ExceptionParameters is %d instead of 1\n", rec->NumberParameters);
2393 ok(rec->ExceptionInformation[0] == ExceptionInfo->ContextRecord->Rax,
2394 "expected ExceptionInformation[0] = %lx, got %lx\n",
2395 ExceptionInfo->ContextRecord->Rax, rec->ExceptionInformation[0]);
2396 #endif
2397
2398 debug_service_exceptions++;
2399 return (rec->ExceptionCode == EXCEPTION_BREAKPOINT) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2400 }
2401
2402 #ifdef __i386__
2403
2404 static const BYTE call_debug_service_code[] = {
2405 0x53, /* pushl %ebx */
2406 0x57, /* pushl %edi */
2407 0x8b, 0x44, 0x24, 0x0c, /* movl 12(%esp),%eax */
2408 0xb9, 0x11, 0x11, 0x11, 0x11, /* movl $0x11111111,%ecx */
2409 0xba, 0x22, 0x22, 0x22, 0x22, /* movl $0x22222222,%edx */
2410 0xbb, 0x33, 0x33, 0x33, 0x33, /* movl $0x33333333,%ebx */
2411 0xbf, 0x44, 0x44, 0x44, 0x44, /* movl $0x44444444,%edi */
2412 0xcd, 0x2d, /* int $0x2d */
2413 0xeb, /* jmp $+17 */
2414 0x0f, 0x1f, 0x00, /* nop */
2415 0x31, 0xc0, /* xorl %eax,%eax */
2416 0xeb, 0x0c, /* jmp $+14 */
2417 0x90, 0x90, 0x90, 0x90, /* nop */
2418 0x90, 0x90, 0x90, 0x90,
2419 0x90,
2420 0x31, 0xc0, /* xorl %eax,%eax */
2421 0x40, /* incl %eax */
2422 0x5f, /* popl %edi */
2423 0x5b, /* popl %ebx */
2424 0xc3, /* ret */
2425 };
2426
2427 #else
2428
2429 static const BYTE call_debug_service_code[] = {
2430 0x53, /* push %rbx */
2431 0x57, /* push %rdi */
2432 0x48, 0x89, 0xc8, /* movl %rcx,%rax */
2433 0x48, 0xb9, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, /* movabs $0x1111111111111111,%rcx */
2434 0x48, 0xba, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* movabs $0x2222222222222222,%rdx */
2435 0x48, 0xbb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, /* movabs $0x3333333333333333,%rbx */
2436 0x48, 0xbf, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, /* movabs $0x4444444444444444,%rdi */
2437 0xcd, 0x2d, /* int $0x2d */
2438 0xeb, /* jmp $+17 */
2439 0x0f, 0x1f, 0x00, /* nop */
2440 0x48, 0x31, 0xc0, /* xor %rax,%rax */
2441 0xeb, 0x0e, /* jmp $+16 */
2442 0x90, 0x90, 0x90, 0x90, /* nop */
2443 0x90, 0x90, 0x90, 0x90,
2444 0x48, 0x31, 0xc0, /* xor %rax,%rax */
2445 0x48, 0xff, 0xc0, /* inc %rax */
2446 0x5f, /* pop %rdi */
2447 0x5b, /* pop %rbx */
2448 0xc3, /* ret */
2449 };
2450
2451 #endif
2452
2453 static void test_debug_service(DWORD numexc)
2454 {
2455 DWORD (CDECL *func)(DWORD_PTR) = code_mem;
2456 DWORD expected_exc, expected_ret;
2457 void *vectored_handler;
2458 DWORD ret;
2459
2460 /* code will return 0 if execution resumes immediately after "int $0x2d", otherwise 1 */
2461 memcpy(code_mem, call_debug_service_code, sizeof(call_debug_service_code));
2462
2463 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &debug_service_handler);
2464 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2465
2466 expected_exc = numexc;
2467 expected_ret = (numexc != 0);
2468
2469 /* BREAKPOINT_BREAK */
2470 debug_service_exceptions = 0;
2471 ret = func(0);
2472 ok(debug_service_exceptions == expected_exc,
2473 "BREAKPOINT_BREAK generated %u exceptions, expected %u\n",
2474 debug_service_exceptions, expected_exc);
2475 ok(ret == expected_ret,
2476 "BREAKPOINT_BREAK returned %u, expected %u\n", ret, expected_ret);
2477
2478 /* BREAKPOINT_PROMPT */
2479 debug_service_exceptions = 0;
2480 ret = func(2);
2481 ok(debug_service_exceptions == expected_exc,
2482 "BREAKPOINT_PROMPT generated %u exceptions, expected %u\n",
2483 debug_service_exceptions, expected_exc);
2484 ok(ret == expected_ret,
2485 "BREAKPOINT_PROMPT returned %u, expected %u\n", ret, expected_ret);
2486
2487 /* invalid debug service */
2488 debug_service_exceptions = 0;
2489 ret = func(6);
2490 ok(debug_service_exceptions == expected_exc,
2491 "invalid debug service generated %u exceptions, expected %u\n",
2492 debug_service_exceptions, expected_exc);
2493 ok(ret == expected_ret,
2494 "invalid debug service returned %u, expected %u\n", ret, expected_ret);
2495
2496 expected_exc = (is_wow64 ? numexc : 0);
2497 expected_ret = (is_wow64 && numexc);
2498
2499 /* BREAKPOINT_PRINT */
2500 debug_service_exceptions = 0;
2501 ret = func(1);
2502 ok(debug_service_exceptions == expected_exc,
2503 "BREAKPOINT_PRINT generated %u exceptions, expected %u\n",
2504 debug_service_exceptions, expected_exc);
2505 ok(ret == expected_ret,
2506 "BREAKPOINT_PRINT returned %u, expected %u\n", ret, expected_ret);
2507
2508 /* BREAKPOINT_LOAD_SYMBOLS */
2509 debug_service_exceptions = 0;
2510 ret = func(3);
2511 ok(debug_service_exceptions == expected_exc,
2512 "BREAKPOINT_LOAD_SYMBOLS generated %u exceptions, expected %u\n",
2513 debug_service_exceptions, expected_exc);
2514 ok(ret == expected_ret,
2515 "BREAKPOINT_LOAD_SYMBOLS returned %u, expected %u\n", ret, expected_ret);
2516
2517 /* BREAKPOINT_UNLOAD_SYMBOLS */
2518 debug_service_exceptions = 0;
2519 ret = func(4);
2520 ok(debug_service_exceptions == expected_exc,
2521 "BREAKPOINT_UNLOAD_SYMBOLS generated %u exceptions, expected %u\n",
2522 debug_service_exceptions, expected_exc);
2523 ok(ret == expected_ret,
2524 "BREAKPOINT_UNLOAD_SYMBOLS returned %u, expected %u\n", ret, expected_ret);
2525
2526 /* BREAKPOINT_COMMAND_STRING */
2527 debug_service_exceptions = 0;
2528 ret = func(5);
2529 ok(debug_service_exceptions == expected_exc || broken(debug_service_exceptions == numexc),
2530 "BREAKPOINT_COMMAND_STRING generated %u exceptions, expected %u\n",
2531 debug_service_exceptions, expected_exc);
2532 ok(ret == expected_ret || broken(ret == (numexc != 0)),
2533 "BREAKPOINT_COMMAND_STRING returned %u, expected %u\n", ret, expected_ret);
2534
2535 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2536 }
2537
2538 static DWORD breakpoint_exceptions;
2539
2540 static LONG CALLBACK breakpoint_handler(EXCEPTION_POINTERS *ExceptionInfo)
2541 {
2542 EXCEPTION_RECORD *rec = ExceptionInfo->ExceptionRecord;
2543 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2544
2545 ok(rec->ExceptionCode == EXCEPTION_BREAKPOINT, "ExceptionCode is %08x instead of %08x\n",
2546 rec->ExceptionCode, EXCEPTION_BREAKPOINT);
2547
2548 #ifdef __i386__
2549 ok(ExceptionInfo->ContextRecord->Eip == (DWORD)code_mem + 1,
2550 "expected Eip = %x, got %x\n", (DWORD)code_mem + 1, ExceptionInfo->ContextRecord->Eip);
2551 ok(rec->NumberParameters == (is_wow64 ? 1 : 3),
2552 "ExceptionParameters is %d instead of %d\n", rec->NumberParameters, is_wow64 ? 1 : 3);
2553 ok(rec->ExceptionInformation[0] == 0,
2554 "got ExceptionInformation[0] = %lx\n", rec->ExceptionInformation[0]);
2555 ExceptionInfo->ContextRecord->Eip = (DWORD)code_mem + 2;
2556 #else
2557 ok(ExceptionInfo->ContextRecord->Rip == (DWORD_PTR)code_mem + 1,
2558 "expected Rip = %lx, got %lx\n", (DWORD_PTR)code_mem + 1, ExceptionInfo->ContextRecord->Rip);
2559 ok(rec->NumberParameters == 1,
2560 "ExceptionParameters is %d instead of 1\n", rec->NumberParameters);
2561 ok(rec->ExceptionInformation[0] == 0,
2562 "got ExceptionInformation[0] = %lx\n", rec->ExceptionInformation[0]);
2563 ExceptionInfo->ContextRecord->Rip = (DWORD_PTR)code_mem + 2;
2564 #endif
2565
2566 breakpoint_exceptions++;
2567 return (rec->ExceptionCode == EXCEPTION_BREAKPOINT) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2568 }
2569
2570 static const BYTE breakpoint_code[] = {
2571 0xcd, 0x03, /* int $0x3 */
2572 0xc3, /* ret */
2573 };
2574
2575 static void test_breakpoint(DWORD numexc)
2576 {
2577 DWORD (CDECL *func)(void) = code_mem;
2578 void *vectored_handler;
2579
2580 memcpy(code_mem, breakpoint_code, sizeof(breakpoint_code));
2581
2582 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &breakpoint_handler);
2583 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2584
2585 breakpoint_exceptions = 0;
2586 func();
2587 ok(breakpoint_exceptions == numexc, "int $0x3 generated %u exceptions, expected %u\n",
2588 breakpoint_exceptions, numexc);
2589
2590 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2591 }
2592
2593 static DWORD invalid_handle_exceptions;
2594
2595 static LONG CALLBACK invalid_handle_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
2596 {
2597 PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
2598 trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
2599
2600 ok(rec->ExceptionCode == EXCEPTION_INVALID_HANDLE, "ExceptionCode is %08x instead of %08x\n",
2601 rec->ExceptionCode, EXCEPTION_INVALID_HANDLE);
2602 ok(rec->NumberParameters == 0, "ExceptionParameters is %d instead of 0\n", rec->NumberParameters);
2603
2604 invalid_handle_exceptions++;
2605 return (rec->ExceptionCode == EXCEPTION_INVALID_HANDLE) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
2606 }
2607
2608 static void test_closehandle(DWORD numexc)
2609 {
2610 PVOID vectored_handler;
2611 NTSTATUS status;
2612 DWORD res;
2613
2614 if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
2615 {
2616 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
2617 return;
2618 }
2619
2620 vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &invalid_handle_vectored_handler);
2621 ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
2622
2623 invalid_handle_exceptions = 0;
2624 res = CloseHandle((HANDLE)0xdeadbeef);
2625 ok(!res, "CloseHandle(0xdeadbeef) unexpectedly succeeded\n");
2626 ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error code %d instead of %d\n",
2627 GetLastError(), ERROR_INVALID_HANDLE);
2628 ok(invalid_handle_exceptions == numexc, "CloseHandle generated %d exceptions, expected %d\n",
2629 invalid_handle_exceptions, numexc);
2630
2631 invalid_handle_exceptions = 0;
2632 status = pNtClose((HANDLE)0xdeadbeef);
2633 ok(status == STATUS_INVALID_HANDLE, "NtClose(0xdeadbeef) returned status %08x\n", status);
2634 ok(invalid_handle_exceptions == numexc, "NtClose generated %d exceptions, expected %d\n",
2635 invalid_handle_exceptions, numexc);
2636
2637 pRtlRemoveVectoredExceptionHandler(vectored_handler);
2638 }
2639
2640 static void test_vectored_continue_handler(void)
2641 {
2642 PVOID handler1, handler2;
2643 ULONG ret;
2644
2645 if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
2646 {
2647 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
2648 return;
2649 }
2650
2651 handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
2652 ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
2653
2654 handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
2655 ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
2656 ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
2657
2658 if (pRtlRemoveVectoredExceptionHandler)
2659 {
2660 ret = pRtlRemoveVectoredExceptionHandler(handler1);
2661 ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
2662 }
2663
2664 ret = pRtlRemoveVectoredContinueHandler(handler1);
2665 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
2666
2667 ret = pRtlRemoveVectoredContinueHandler(handler2);
2668 ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
2669
2670 ret = pRtlRemoveVectoredContinueHandler(handler1);
2671 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
2672
2673 ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
2674 ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
2675 }
2676 #endif /* defined(__i386__) || defined(__x86_64__) */
2677
2678 START_TEST(exception)
2679 {
2680 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
2681 #if defined(__x86_64__)
2682 HMODULE hmsvcrt = LoadLibraryA("msvcrt.dll");
2683 #endif
2684
2685 #ifdef __REACTOS__
2686 if (!winetest_interactive &&
2687 !strcmp(winetest_platform, "windows"))
2688 {
2689 skip("ROSTESTS-240: Skipping ntdll_winetest:exception because it hangs on WHS-Testbot. Set winetest_interactive to run it anyway.\n");
2690 return;
2691 }
2692 #endif
2693 code_mem = VirtualAlloc(NULL, 65536, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2694 if(!code_mem) {
2695 trace("VirtualAlloc failed\n");
2696 return;
2697 }
2698
2699 pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
2700 pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
2701 pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
2702 pNtClose = (void *)GetProcAddress( hntdll, "NtClose" );
2703 pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
2704 pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
2705 pRtlCaptureContext = (void *)GetProcAddress( hntdll, "RtlCaptureContext" );
2706 pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
2707 pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
2708 "RtlAddVectoredExceptionHandler" );
2709 pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
2710 "RtlRemoveVectoredExceptionHandler" );
2711 pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
2712 "RtlAddVectoredContinueHandler" );
2713 pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
2714 "RtlRemoveVectoredContinueHandler" );
2715 pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
2716 "NtQueryInformationProcess" );
2717 pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
2718 "NtSetInformationProcess" );
2719 pIsWow64Process = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
2720
2721 #ifdef __i386__
2722 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
2723
2724 if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
2725 have_vectored_api = TRUE;
2726 else
2727 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
2728
2729 my_argc = winetest_get_mainargs( &my_argv );
2730 if (my_argc >= 4)
2731 {
2732 void *addr;
2733 sscanf( my_argv[3], "%p", &addr );
2734
2735 if (addr != &test_stage)
2736 {
2737 skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
2738 return;
2739 }
2740
2741 /* child must be run under a debugger */
2742 if (!NtCurrentTeb()->Peb->BeingDebugged)
2743 {
2744 ok(FALSE, "child process not being debugged?\n");
2745 return;
2746 }
2747
2748 if (pRtlRaiseException)
2749 {
2750 test_stage = 1;
2751 run_rtlraiseexception_test(0x12345);
2752 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
2753 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
2754 test_stage = 2;
2755 run_rtlraiseexception_test(0x12345);
2756 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
2757 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
2758 test_stage = 3;
2759 test_outputdebugstring(0);
2760 test_stage = 4;
2761 test_outputdebugstring(2);
2762 test_stage = 5;
2763 test_ripevent(0);
2764 test_stage = 6;
2765 test_ripevent(1);
2766 test_stage = 7;
2767 test_debug_service(0);
2768 test_stage = 8;
2769 test_debug_service(1);
2770 test_stage = 9;
2771 test_breakpoint(0);
2772 test_stage = 10;
2773 test_breakpoint(1);
2774 test_stage = 11;
2775 test_closehandle(0);
2776 test_stage = 12;
2777 test_closehandle(1);
2778 }
2779 else
2780 skip( "RtlRaiseException not found\n" );
2781
2782 /* rest of tests only run in parent */
2783 return;
2784 }
2785
2786 test_unwind();
2787 test_exceptions();
2788 test_rtlraiseexception();
2789 test_debug_registers();
2790 test_outputdebugstring(1);
2791 test_ripevent(1);
2792 test_debug_service(1);
2793 test_breakpoint(1);
2794 test_closehandle(0);
2795 test_vectored_continue_handler();
2796 test_debugger();
2797 test_simd_exceptions();
2798 test_fpu_exceptions();
2799 test_dpe_exceptions();
2800 test_prot_fault();
2801 test_thread_context();
2802
2803 #elif defined(__x86_64__)
2804 pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
2805 "RtlAddFunctionTable" );
2806 pRtlDeleteFunctionTable = (void *)GetProcAddress( hntdll,
2807 "RtlDeleteFunctionTable" );
2808 pRtlInstallFunctionTableCallback = (void *)GetProcAddress( hntdll,
2809 "RtlInstallFunctionTableCallback" );
2810 pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
2811 "RtlLookupFunctionEntry" );
2812 p__C_specific_handler = (void *)GetProcAddress( hntdll,
2813 "__C_specific_handler" );
2814 pRtlCaptureContext = (void *)GetProcAddress( hntdll,
2815 "RtlCaptureContext" );
2816 pRtlRestoreContext = (void *)GetProcAddress( hntdll,
2817 "RtlRestoreContext" );
2818 pRtlUnwindEx = (void *)GetProcAddress( hntdll,
2819 "RtlUnwindEx" );
2820 p_setjmp = (void *)GetProcAddress( hmsvcrt,
2821 "_setjmp" );
2822
2823 test_debug_registers();
2824 test_outputdebugstring(1);
2825 test_ripevent(1);
2826 test_debug_service(1);
2827 test_breakpoint(1);
2828 test_closehandle(0);
2829 test_vectored_continue_handler();
2830 test_virtual_unwind();
2831 test___C_specific_handler();
2832 test_restore_context();
2833
2834 if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)
2835 test_dynamic_unwind();
2836 else
2837 skip( "Dynamic unwind functions not found\n" );
2838
2839 #endif
2840
2841 VirtualFree(code_mem, 0, MEM_RELEASE);
2842 }