2 * Unit test suite for ntdll exceptions
4 * Copyright 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
29 #define WIN32_NO_STATUS
30 #define NONAMELESSUNION
37 #include "wine/test.h"
39 static void *code_mem
;
41 static struct _TEB
* (WINAPI
*pNtCurrentTeb
)(void);
42 static NTSTATUS (WINAPI
*pNtGetContextThread
)(HANDLE
,CONTEXT
*);
43 static NTSTATUS (WINAPI
*pNtSetContextThread
)(HANDLE
,CONTEXT
*);
44 static NTSTATUS (WINAPI
*pRtlRaiseException
)(EXCEPTION_RECORD
*rec
);
45 static PVOID (WINAPI
*pRtlUnwind
)(PVOID
, PVOID
, PEXCEPTION_RECORD
, PVOID
);
46 static PVOID (WINAPI
*pRtlAddVectoredExceptionHandler
)(ULONG first
, PVECTORED_EXCEPTION_HANDLER func
);
47 static ULONG (WINAPI
*pRtlRemoveVectoredExceptionHandler
)(PVOID handler
);
48 static PVOID (WINAPI
*pRtlAddVectoredContinueHandler
)(ULONG first
, PVECTORED_EXCEPTION_HANDLER func
);
49 static ULONG (WINAPI
*pRtlRemoveVectoredContinueHandler
)(PVOID handler
);
50 static NTSTATUS (WINAPI
*pNtReadVirtualMemory
)(HANDLE
, const void*, void*, SIZE_T
, SIZE_T
*);
51 static NTSTATUS (WINAPI
*pNtTerminateProcess
)(HANDLE handle
, LONG exit_code
);
52 static NTSTATUS (WINAPI
*pNtQueryInformationProcess
)(HANDLE
, PROCESSINFOCLASS
, PVOID
, ULONG
, PULONG
);
53 static NTSTATUS (WINAPI
*pNtSetInformationProcess
)(HANDLE
, PROCESSINFOCLASS
, PVOID
, ULONG
);
54 static BOOL (WINAPI
*pIsWow64Process
)(HANDLE
, PBOOL
);
56 #if defined(__x86_64__)
57 static BOOLEAN (CDECL
*pRtlAddFunctionTable
)(RUNTIME_FUNCTION
*, DWORD
, DWORD64
);
58 static BOOLEAN (CDECL
*pRtlDeleteFunctionTable
)(RUNTIME_FUNCTION
*);
59 static BOOLEAN (CDECL
*pRtlInstallFunctionTableCallback
)(DWORD64
, DWORD64
, DWORD
, PGET_RUNTIME_FUNCTION_CALLBACK
, PVOID
, PCWSTR
);
60 static PRUNTIME_FUNCTION (WINAPI
*pRtlLookupFunctionEntry
)(ULONG64
, ULONG64
*, UNWIND_HISTORY_TABLE
*);
65 #ifndef __WINE_WINTRNL_H
66 #define ProcessExecuteFlags 0x22
67 #define MEM_EXECUTE_OPTION_DISABLE 0x01
68 #define MEM_EXECUTE_OPTION_ENABLE 0x02
69 #define MEM_EXECUTE_OPTION_PERMANENT 0x08
73 static char** my_argv
;
74 static int test_stage
;
78 /* Test various instruction combinations that cause a protection fault on the i386,
79 * and check what the resulting exception looks like.
82 static const struct exception
84 BYTE code
[18]; /* asm code */
85 BYTE offset
; /* offset of faulting instruction */
86 BYTE length
; /* length of faulting instruction */
87 BOOL wow64_broken
; /* broken on Wow64, should be skipped */
88 NTSTATUS status
; /* expected status code */
89 DWORD nb_params
; /* expected number of parameters */
90 DWORD params
[4]; /* expected parameters */
91 NTSTATUS alt_status
; /* alternative status code */
92 DWORD alt_nb_params
; /* alternative number of parameters */
93 DWORD alt_params
[4]; /* alternative parameters */
97 /* test some privileged instructions */
98 { { 0xfb, 0xc3 }, /* 0: sti; ret */
99 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
100 { { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
101 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
102 { { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
103 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
104 { { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
105 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
106 { { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
107 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
109 { { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
110 0, 2, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
111 { { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
112 0, 2, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
113 { { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
114 0, 2, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
115 { { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
116 0, 2, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
117 { { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
118 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
120 { { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
121 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
122 { { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
123 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
124 { { 0xf4, 0xc3 }, /* 12: hlt; ret */
125 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
126 { { 0xfa, 0xc3 }, /* 13: cli; ret */
127 0, 1, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
129 /* test long jump to invalid selector */
130 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
131 0, 7, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
134 /* test iret to invalid selector */
135 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
136 /* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
137 6, 1, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
139 /* test loading an invalid selector */
140 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
141 5, 2, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
143 /* test accessing a zero selector (%es broken on Wow64) */
144 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
145 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
146 5, 6, TRUE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
147 { { 0x0f, 0xa8, 0x31, 0xc0, 0x8e, 0xe8, 0x65, 0xa1, 0, 0, 0, 0, 0x0f, 0xa9, 0xc3 },
148 /* push %gs; xor %eax,%eax; mov %ax,%gs; mov %gs:(0),%ax; pop %gs; ret */
149 6, 6, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
151 /* test moving %cs -> %ss */
152 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
153 1, 1, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
156 /* test overlong instruction (limit is 15 bytes, 5 on Win7) */
157 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
158 0, 16, TRUE
, STATUS_ILLEGAL_INSTRUCTION
, 0, { 0 },
159 STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
160 { { 0x64,0x64,0x64,0x64,0xfa,0xc3 },
161 0, 5, TRUE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
163 /* test invalid interrupt */
164 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
165 0, 2, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
167 /* test moves to/from Crx */
168 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
169 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
170 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
171 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
173 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
174 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
175 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
176 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
178 /* test moves to/from Drx */
179 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
180 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
181 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
182 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
183 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
184 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
186 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
187 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
188 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
189 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
190 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
191 0, 3, FALSE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
193 /* test memory reads */
194 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
195 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xfffffffc } },
196 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
197 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xfffffffd } },
199 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
200 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xfffffffe } },
201 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
202 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 0, 0xffffffff } },
204 /* test memory writes */
205 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
206 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 1, 0xfffffffc } },
207 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
208 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 1, 0xfffffffd } },
209 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
210 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 1, 0xfffffffe } },
212 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
213 0, 5, FALSE
, STATUS_ACCESS_VIOLATION
, 2, { 1, 0xffffffff } },
215 /* test exception with cleared %ds and %es (broken on Wow64) */
216 { { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
217 /* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
218 8, 1, TRUE
, STATUS_PRIVILEGED_INSTRUCTION
, 0 },
220 { { 0xf1, 0x90, 0xc3 }, /* icebp; nop; ret */
221 1, 1, FALSE
, STATUS_SINGLE_STEP
, 0 },
222 { { 0xb8, 0xb8, 0xb8, 0xb8, 0xb8, /* mov $0xb8b8b8b8, %eax */
223 0xb9, 0xb9, 0xb9, 0xb9, 0xb9, /* mov $0xb9b9b9b9, %ecx */
224 0xba, 0xba, 0xba, 0xba, 0xba, /* mov $0xbabababa, %edx */
225 0xcd, 0x2d, 0xc3 }, /* int $0x2d; ret */
226 17, 0, FALSE
, STATUS_BREAKPOINT
, 3, { 0xb8b8b8b8, 0xb9b9b9b9, 0xbabababa } },
229 static int got_exception
;
230 static BOOL have_vectored_api
;
232 static void run_exception_test(void *handler
, const void* context
,
233 const void *code
, unsigned int code_size
,
237 EXCEPTION_REGISTRATION_RECORD frame
;
240 void (*func
)(void) = code_mem
;
241 DWORD oldaccess
, oldaccess2
;
243 exc_frame
.frame
.Handler
= handler
;
244 exc_frame
.frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
245 exc_frame
.context
= context
;
247 memcpy(code_mem
, code
, code_size
);
249 VirtualProtect(code_mem
, code_size
, access
, &oldaccess
);
251 pNtCurrentTeb()->Tib
.ExceptionList
= &exc_frame
.frame
;
253 pNtCurrentTeb()->Tib
.ExceptionList
= exc_frame
.frame
.Prev
;
256 VirtualProtect(code_mem
, code_size
, oldaccess
, &oldaccess2
);
259 static LONG CALLBACK
rtlraiseexception_vectored_handler(EXCEPTION_POINTERS
*ExceptionInfo
)
261 PCONTEXT context
= ExceptionInfo
->ContextRecord
;
262 PEXCEPTION_RECORD rec
= ExceptionInfo
->ExceptionRecord
;
263 trace("vect. handler %08x addr:%p context.Eip:%x\n", rec
->ExceptionCode
,
264 rec
->ExceptionAddress
, context
->Eip
);
266 ok(rec
->ExceptionAddress
== (char *)code_mem
+ 0xb, "ExceptionAddress at %p instead of %p\n",
267 rec
->ExceptionAddress
, (char *)code_mem
+ 0xb);
269 if (pNtCurrentTeb()->Peb
->BeingDebugged
)
270 ok((void *)context
->Eax
== pRtlRaiseException
||
271 broken( is_wow64
&& context
->Eax
== 0xf00f00f1 ), /* broken on vista */
272 "debugger managed to modify Eax to %x should be %p\n",
273 context
->Eax
, pRtlRaiseException
);
275 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
276 * even if raised by RtlRaiseException
278 if(rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
)
280 ok(context
->Eip
== (DWORD
)code_mem
+ 0xa ||
281 broken(context
->Eip
== (DWORD
)code_mem
+ 0xb), /* win2k3 */
282 "Eip at %x instead of %x or %x\n", context
->Eip
,
283 (DWORD
)code_mem
+ 0xa, (DWORD
)code_mem
+ 0xb);
287 ok(context
->Eip
== (DWORD
)code_mem
+ 0xb, "Eip at %x instead of %x\n",
288 context
->Eip
, (DWORD
)code_mem
+ 0xb);
291 /* test if context change is preserved from vectored handler to stack handlers */
292 context
->Eax
= 0xf00f00f0;
293 return EXCEPTION_CONTINUE_SEARCH
;
296 static DWORD
rtlraiseexception_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
297 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
299 trace( "exception: %08x flags:%x addr:%p context: Eip:%x\n",
300 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
, context
->Eip
);
302 ok(rec
->ExceptionAddress
== (char *)code_mem
+ 0xb, "ExceptionAddress at %p instead of %p\n",
303 rec
->ExceptionAddress
, (char *)code_mem
+ 0xb);
305 /* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
306 * even if raised by RtlRaiseException
308 if(rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
)
310 ok(context
->Eip
== (DWORD
)code_mem
+ 0xa ||
311 broken(context
->Eip
== (DWORD
)code_mem
+ 0xb), /* win2k3 */
312 "Eip at %x instead of %x or %x\n", context
->Eip
,
313 (DWORD
)code_mem
+ 0xa, (DWORD
)code_mem
+ 0xb);
317 ok(context
->Eip
== (DWORD
)code_mem
+ 0xb, "Eip at %x instead of %x\n",
318 context
->Eip
, (DWORD
)code_mem
+ 0xb);
321 if(have_vectored_api
)
322 ok(context
->Eax
== 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
325 /* give the debugger a chance to examine the state a second time */
326 /* without the exception handler changing Eip */
328 return ExceptionContinueSearch
;
330 /* Eip in context is decreased by 1
331 * Increase it again, else execution will continue in the middle of an instruction */
332 if(rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
&& (context
->Eip
== (DWORD
)code_mem
+ 0xa))
334 return ExceptionContinueExecution
;
338 static const BYTE call_one_arg_code
[] = {
339 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
340 0x50, /* push %eax */
341 0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
342 0xff, 0xd0, /* call *%eax */
351 static void run_rtlraiseexception_test(DWORD exceptioncode
)
353 EXCEPTION_REGISTRATION_RECORD frame
;
354 EXCEPTION_RECORD record
;
355 PVOID vectored_handler
= NULL
;
357 void (*func
)(void* function
, EXCEPTION_RECORD
* record
) = code_mem
;
359 record
.ExceptionCode
= exceptioncode
;
360 record
.ExceptionFlags
= 0;
361 record
.ExceptionRecord
= NULL
;
362 record
.ExceptionAddress
= NULL
; /* does not matter, copied return address */
363 record
.NumberParameters
= 0;
365 frame
.Handler
= rtlraiseexception_handler
;
366 frame
.Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
368 memcpy(code_mem
, call_one_arg_code
, sizeof(call_one_arg_code
));
370 pNtCurrentTeb()->Tib
.ExceptionList
= &frame
;
371 if (have_vectored_api
)
373 vectored_handler
= pRtlAddVectoredExceptionHandler(TRUE
, &rtlraiseexception_vectored_handler
);
374 ok(vectored_handler
!= 0, "RtlAddVectoredExceptionHandler failed\n");
377 func(pRtlRaiseException
, &record
);
378 ok( record
.ExceptionAddress
== (char *)code_mem
+ 0x0b,
379 "address set to %p instead of %p\n", record
.ExceptionAddress
, (char *)code_mem
+ 0x0b );
381 if (have_vectored_api
)
382 pRtlRemoveVectoredExceptionHandler(vectored_handler
);
383 pNtCurrentTeb()->Tib
.ExceptionList
= frame
.Prev
;
386 static void test_rtlraiseexception(void)
388 if (!pRtlRaiseException
)
390 skip("RtlRaiseException not found\n");
394 /* test without debugger */
395 run_rtlraiseexception_test(0x12345);
396 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT
);
397 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE
);
400 static DWORD unwind_expected_eax
;
402 static DWORD
unwind_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
403 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
405 trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
406 rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
, context
->Eip
);
408 ok(rec
->ExceptionCode
== STATUS_UNWIND
, "ExceptionCode is %08x instead of %08x\n",
409 rec
->ExceptionCode
, STATUS_UNWIND
);
410 ok(rec
->ExceptionAddress
== (char *)code_mem
+ 0x22, "ExceptionAddress at %p instead of %p\n",
411 rec
->ExceptionAddress
, (char *)code_mem
+ 0x22);
412 ok(context
->Eax
== unwind_expected_eax
, "context->Eax is %08x instead of %08x\n",
413 context
->Eax
, unwind_expected_eax
);
416 return ExceptionContinueSearch
;
419 static const BYTE call_unwind_code
[] = {
420 0x55, /* push %ebp */
421 0x53, /* push %ebx */
422 0x56, /* push %esi */
423 0x57, /* push %edi */
424 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
425 0x58, /* 0: pop %eax */
426 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
427 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
428 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
429 0x50, /* push %eax */
430 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
431 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
432 0xff, 0xd0, /* call *%eax */
441 static void test_unwind(void)
443 EXCEPTION_REGISTRATION_RECORD frames
[2], *frame2
= &frames
[0], *frame1
= &frames
[1];
444 DWORD (*func
)(void* function
, EXCEPTION_REGISTRATION_RECORD
*pEndFrame
, EXCEPTION_RECORD
* record
, DWORD retval
) = code_mem
;
447 memcpy(code_mem
, call_unwind_code
, sizeof(call_unwind_code
));
449 /* add first unwind handler */
450 frame1
->Handler
= unwind_handler
;
451 frame1
->Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
452 pNtCurrentTeb()->Tib
.ExceptionList
= frame1
;
454 /* add second unwind handler */
455 frame2
->Handler
= unwind_handler
;
456 frame2
->Prev
= pNtCurrentTeb()->Tib
.ExceptionList
;
457 pNtCurrentTeb()->Tib
.ExceptionList
= frame2
;
459 /* test unwind to current frame */
460 unwind_expected_eax
= 0xDEAD0000;
461 retval
= func(pRtlUnwind
, frame2
, NULL
, 0xDEAD0000);
462 ok(retval
== 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval
, 0xDEAD0000);
463 ok(pNtCurrentTeb()->Tib
.ExceptionList
== frame2
, "Exception record points to %p instead of %p\n",
464 pNtCurrentTeb()->Tib
.ExceptionList
, frame2
);
466 /* unwind to frame1 */
467 unwind_expected_eax
= 0xDEAD0000;
468 retval
= func(pRtlUnwind
, frame1
, NULL
, 0xDEAD0000);
469 ok(retval
== 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval
, 0xDEAD0001);
470 ok(pNtCurrentTeb()->Tib
.ExceptionList
== frame1
, "Exception record points to %p instead of %p\n",
471 pNtCurrentTeb()->Tib
.ExceptionList
, frame1
);
473 /* restore original handler */
474 pNtCurrentTeb()->Tib
.ExceptionList
= frame1
->Prev
;
477 static DWORD
handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
478 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
480 const struct exception
*except
= *(const struct exception
**)(frame
+ 1);
481 unsigned int i
, parameter_count
, entry
= except
- exceptions
;
484 trace( "exception %u: %x flags:%x addr:%p\n",
485 entry
, rec
->ExceptionCode
, rec
->ExceptionFlags
, rec
->ExceptionAddress
);
487 ok( rec
->ExceptionCode
== except
->status
||
488 (except
->alt_status
!= 0 && rec
->ExceptionCode
== except
->alt_status
),
489 "%u: Wrong exception code %x/%x\n", entry
, rec
->ExceptionCode
, except
->status
);
490 ok( context
->Eip
== (DWORD_PTR
)code_mem
+ except
->offset
,
491 "%u: Unexpected eip %#x/%#lx\n", entry
,
492 context
->Eip
, (DWORD_PTR
)code_mem
+ except
->offset
);
493 ok( rec
->ExceptionAddress
== (char*)context
->Eip
||
494 (rec
->ExceptionCode
== STATUS_BREAKPOINT
&& rec
->ExceptionAddress
== (char*)context
->Eip
+ 1),
495 "%u: Unexpected exception address %p/%p\n", entry
,
496 rec
->ExceptionAddress
, (char*)context
->Eip
);
498 if (except
->status
== STATUS_BREAKPOINT
&& is_wow64
)
500 else if (except
->alt_status
== 0 || rec
->ExceptionCode
!= except
->alt_status
)
501 parameter_count
= except
->nb_params
;
503 parameter_count
= except
->alt_nb_params
;
505 ok( rec
->NumberParameters
== parameter_count
,
506 "%u: Unexpected parameter count %u/%u\n", entry
, rec
->NumberParameters
, parameter_count
);
508 /* Most CPUs (except Intel Core apparently) report a segment limit violation */
509 /* instead of page faults for accesses beyond 0xffffffff */
510 if (except
->nb_params
== 2 && except
->params
[1] >= 0xfffffffd)
512 if (rec
->ExceptionInformation
[0] == 0 && rec
->ExceptionInformation
[1] == 0xffffffff)
516 /* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
517 if (except
->nb_params
== 2 && rec
->NumberParameters
== 2
518 && except
->params
[1] == 0xbee8 && rec
->ExceptionInformation
[1] == 0xffffffff
519 && except
->params
[0] == rec
->ExceptionInformation
[0])
524 if (except
->alt_status
== 0 || rec
->ExceptionCode
!= except
->alt_status
)
526 for (i
= 0; i
< rec
->NumberParameters
; i
++)
527 ok( rec
->ExceptionInformation
[i
] == except
->params
[i
],
528 "%u: Wrong parameter %d: %lx/%x\n",
529 entry
, i
, rec
->ExceptionInformation
[i
], except
->params
[i
] );
533 for (i
= 0; i
< rec
->NumberParameters
; i
++)
534 ok( rec
->ExceptionInformation
[i
] == except
->alt_params
[i
],
535 "%u: Wrong parameter %d: %lx/%x\n",
536 entry
, i
, rec
->ExceptionInformation
[i
], except
->alt_params
[i
] );
540 /* don't handle exception if it's not the address we expected */
541 if (context
->Eip
!= (DWORD_PTR
)code_mem
+ except
->offset
) return ExceptionContinueSearch
;
543 context
->Eip
+= except
->length
;
544 return ExceptionContinueExecution
;
547 static void test_prot_fault(void)
551 for (i
= 0; i
< sizeof(exceptions
)/sizeof(exceptions
[0]); i
++)
553 if (is_wow64
&& exceptions
[i
].wow64_broken
&& !strcmp( winetest_platform
, "windows" ))
555 skip( "Exception %u broken on Wow64\n", i
);
559 run_exception_test(handler
, &exceptions
[i
], &exceptions
[i
].code
,
560 sizeof(exceptions
[i
].code
), 0);
561 if (!i
&& !got_exception
)
563 trace( "No exception, assuming win9x, no point in testing further\n" );
566 ok( got_exception
== (exceptions
[i
].status
!= 0),
567 "%u: bad exception count %d\n", i
, got_exception
);
572 DWORD dr0
, dr1
, dr2
, dr3
, dr6
, dr7
;
575 /* test handling of debug registers */
576 static DWORD
dreg_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
577 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
579 const struct dbgreg_test
*test
= *(const struct dbgreg_test
**)(frame
+ 1);
581 context
->Eip
+= 2; /* Skips the popl (%eax) */
582 context
->Dr0
= test
->dr0
;
583 context
->Dr1
= test
->dr1
;
584 context
->Dr2
= test
->dr2
;
585 context
->Dr3
= test
->dr3
;
586 context
->Dr6
= test
->dr6
;
587 context
->Dr7
= test
->dr7
;
588 return ExceptionContinueExecution
;
591 #define CHECK_DEBUG_REG(n, m) \
592 ok((ctx.Dr##n & m) == test->dr##n, "(%d) failed to set debug register " #n " to %x, got %x\n", \
593 test_num, test->dr##n, ctx.Dr##n)
595 static void check_debug_registers(int test_num
, const struct dbgreg_test
*test
)
600 ctx
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
601 status
= pNtGetContextThread(GetCurrentThread(), &ctx
);
602 ok(status
== STATUS_SUCCESS
, "NtGetContextThread failed with %x\n", status
);
604 CHECK_DEBUG_REG(0, ~0);
605 CHECK_DEBUG_REG(1, ~0);
606 CHECK_DEBUG_REG(2, ~0);
607 CHECK_DEBUG_REG(3, ~0);
608 CHECK_DEBUG_REG(6, 0x0f);
609 CHECK_DEBUG_REG(7, ~0xdc00);
612 static const BYTE segfault_code
[5] = {
613 0x31, 0xc0, /* xor %eax,%eax */
614 0x8f, 0x00, /* popl (%eax) - cause exception */
618 /* test the single step exception behaviour */
619 static DWORD
single_step_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
620 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
623 ok (!(context
->EFlags
& 0x100), "eflags has single stepping bit set\n");
625 if( got_exception
< 3)
626 context
->EFlags
|= 0x100; /* single step until popf instruction */
628 /* show that the last single step exception on the popf instruction
629 * (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
630 ok( rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
,
631 "exception is not EXCEPTION_SINGLE_STEP: %x\n", rec
->ExceptionCode
);
634 return ExceptionContinueExecution
;
637 static const BYTE single_stepcode
[] = {
640 0x0d,0,1,0,0, /* or $0x100,%eax */
641 0x50, /* push %eax */
643 0x35,0,1,0,0, /* xor $0x100,%eax */
644 0x50, /* push %eax */
649 /* Test the alignment check (AC) flag handling. */
650 static const BYTE align_check_code
[] = {
651 0x55, /* push %ebp */
652 0x89,0xe5, /* mov %esp,%ebp */
655 0x0d,0,0,4,0, /* or $0x40000,%eax */
656 0x50, /* push %eax */
658 0x89,0xe0, /* mov %esp, %eax */
659 0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
662 0x35,0,0,4,0, /* xor $0x40000,%eax */
663 0x50, /* push %eax */
669 static DWORD
align_check_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
670 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
672 ok (!(context
->EFlags
& 0x40000), "eflags has AC bit set\n");
674 return ExceptionContinueExecution
;
677 /* Test the direction flag handling. */
678 static const BYTE direction_flag_code
[] = {
679 0x55, /* push %ebp */
680 0x89,0xe5, /* mov %esp,%ebp */
682 0xfa, /* cli - cause exception */
687 static DWORD
direction_flag_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
688 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
692 __asm__("pushfl; popl %0; cld" : "=r" (flags
) );
693 /* older windows versions don't clear DF properly so don't test */
694 if (flags
& 0x400) trace( "eflags has DF bit set\n" );
696 ok( context
->EFlags
& 0x400, "context eflags has DF bit cleared\n" );
698 context
->Eip
++; /* skip cli */
699 context
->EFlags
&= ~0x400; /* make sure it is cleared on return */
700 return ExceptionContinueExecution
;
703 /* test single stepping over hardware breakpoint */
704 static DWORD
bpx_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
705 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
708 ok( rec
->ExceptionCode
== EXCEPTION_SINGLE_STEP
,
709 "wrong exception code: %x\n", rec
->ExceptionCode
);
711 if(got_exception
== 1) {
712 /* hw bp exception on first nop */
713 ok( context
->Eip
== (DWORD
)code_mem
, "eip is wrong: %x instead of %x\n",
714 context
->Eip
, (DWORD
)code_mem
);
715 ok( (context
->Dr6
& 0xf) == 1, "B0 flag is not set in Dr6\n");
716 ok( !(context
->Dr6
& 0x4000), "BS flag is set in Dr6\n");
717 context
->Dr0
= context
->Dr0
+ 1; /* set hw bp again on next instruction */
718 context
->EFlags
|= 0x100; /* enable single stepping */
719 } else if( got_exception
== 2) {
720 /* single step exception on second nop */
721 ok( context
->Eip
== (DWORD
)code_mem
+ 1, "eip is wrong: %x instead of %x\n",
722 context
->Eip
, (DWORD
)code_mem
+ 1);
723 ok( (context
->Dr6
& 0x4000), "BS flag is not set in Dr6\n");
724 /* depending on the win version the B0 bit is already set here as well
725 ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
726 context
->EFlags
|= 0x100;
727 } else if( got_exception
== 3) {
728 /* hw bp exception on second nop */
729 ok( context
->Eip
== (DWORD
)code_mem
+ 1, "eip is wrong: %x instead of %x\n",
730 context
->Eip
, (DWORD
)code_mem
+ 1);
731 ok( (context
->Dr6
& 0xf) == 1, "B0 flag is not set in Dr6\n");
732 ok( !(context
->Dr6
& 0x4000), "BS flag is set in Dr6\n");
733 context
->Dr0
= 0; /* clear breakpoint */
734 context
->EFlags
|= 0x100;
736 /* single step exception on ret */
737 ok( context
->Eip
== (DWORD
)code_mem
+ 2, "eip is wrong: %x instead of %x\n",
738 context
->Eip
, (DWORD
)code_mem
+ 2);
739 ok( (context
->Dr6
& 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
740 ok( (context
->Dr6
& 0x4000), "BS flag is not set in Dr6\n");
743 context
->Dr6
= 0; /* clear status register */
744 return ExceptionContinueExecution
;
747 static const BYTE dummy_code
[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
749 /* test int3 handling */
750 static DWORD
int3_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
751 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
753 ok( rec
->ExceptionAddress
== code_mem
, "exception address not at: %p, but at %p\n",
754 code_mem
, rec
->ExceptionAddress
);
755 ok( context
->Eip
== (DWORD
)code_mem
, "eip not at: %p, but at %#x\n", code_mem
, context
->Eip
);
756 if(context
->Eip
== (DWORD
)code_mem
) context
->Eip
++; /* skip breakpoint */
758 return ExceptionContinueExecution
;
761 static const BYTE int3_code
[] = { 0xCC, 0xc3 }; /* int 3, ret */
764 static void test_exceptions(void)
768 struct dbgreg_test dreg_test
;
770 if (!pNtGetContextThread
|| !pNtSetContextThread
)
772 skip( "NtGetContextThread/NtSetContextThread not found\n" );
776 /* test handling of debug registers */
777 memset(&dreg_test
, 0, sizeof(dreg_test
));
779 dreg_test
.dr0
= 0x42424240;
780 dreg_test
.dr2
= 0x126bb070;
781 dreg_test
.dr3
= 0x0badbad0;
782 dreg_test
.dr7
= 0xffff0115;
783 run_exception_test(dreg_handler
, &dreg_test
, &segfault_code
, sizeof(segfault_code
), 0);
784 check_debug_registers(1, &dreg_test
);
786 dreg_test
.dr0
= 0x42424242;
787 dreg_test
.dr2
= 0x100f0fe7;
788 dreg_test
.dr3
= 0x0abebabe;
789 dreg_test
.dr7
= 0x115;
790 run_exception_test(dreg_handler
, &dreg_test
, &segfault_code
, sizeof(segfault_code
), 0);
791 check_debug_registers(2, &dreg_test
);
793 /* test single stepping behavior */
795 run_exception_test(single_step_handler
, NULL
, &single_stepcode
, sizeof(single_stepcode
), 0);
796 ok(got_exception
== 3, "expected 3 single step exceptions, got %d\n", got_exception
);
798 /* test alignment exceptions */
800 run_exception_test(align_check_handler
, NULL
, align_check_code
, sizeof(align_check_code
), 0);
801 ok(got_exception
== 0, "got %d alignment faults, expected 0\n", got_exception
);
803 /* test direction flag */
805 run_exception_test(direction_flag_handler
, NULL
, direction_flag_code
, sizeof(direction_flag_code
), 0);
806 ok(got_exception
== 1, "got %d exceptions, expected 1\n", got_exception
);
808 /* test single stepping over hardware breakpoint */
809 memset(&ctx
, 0, sizeof(ctx
));
810 ctx
.Dr0
= (DWORD
) code_mem
; /* set hw bp on first nop */
812 ctx
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
813 res
= pNtSetContextThread( GetCurrentThread(), &ctx
);
814 ok( res
== STATUS_SUCCESS
, "NtSetContextThread failed with %x\n", res
);
817 run_exception_test(bpx_handler
, NULL
, dummy_code
, sizeof(dummy_code
), 0);
818 ok( got_exception
== 4,"expected 4 exceptions, got %d\n", got_exception
);
820 /* test int3 handling */
821 run_exception_test(int3_handler
, NULL
, int3_code
, sizeof(int3_code
), 0);
824 static void test_debugger(void)
826 char cmdline
[MAX_PATH
];
827 PROCESS_INFORMATION pi
;
828 STARTUPINFOA si
= { 0 };
830 DWORD continuestatus
;
831 PVOID code_mem_address
= NULL
;
838 if(!pNtGetContextThread
|| !pNtSetContextThread
|| !pNtReadVirtualMemory
|| !pNtTerminateProcess
)
840 skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n");
844 sprintf(cmdline
, "%s %s %s %p", my_argv
[0], my_argv
[1], "debuggee", &test_stage
);
845 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, DEBUG_PROCESS
, NULL
, NULL
, &si
, &pi
);
846 ok(ret
, "could not create child process error: %u\n", GetLastError());
852 continuestatus
= DBG_CONTINUE
;
853 ok(WaitForDebugEvent(&de
, INFINITE
), "reading debug event\n");
855 if (de
.dwThreadId
!= pi
.dwThreadId
)
857 trace("event %d not coming from main thread, ignoring\n", de
.dwDebugEventCode
);
858 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, DBG_CONTINUE
);
862 if (de
.dwDebugEventCode
== CREATE_PROCESS_DEBUG_EVENT
)
864 if(de
.u
.CreateProcessInfo
.lpBaseOfImage
!= pNtCurrentTeb()->Peb
->ImageBaseAddress
)
866 skip("child process loaded at different address, terminating it\n");
867 pNtTerminateProcess(pi
.hProcess
, 0);
870 else if (de
.dwDebugEventCode
== EXCEPTION_DEBUG_EVENT
)
876 status
= pNtReadVirtualMemory(pi
.hProcess
, &code_mem
, &code_mem_address
,
877 sizeof(code_mem_address
), &size_read
);
878 ok(!status
,"NtReadVirtualMemory failed with 0x%x\n", status
);
879 status
= pNtReadVirtualMemory(pi
.hProcess
, &test_stage
, &stage
,
880 sizeof(stage
), &size_read
);
881 ok(!status
,"NtReadVirtualMemory failed with 0x%x\n", status
);
883 ctx
.ContextFlags
= CONTEXT_FULL
;
884 status
= pNtGetContextThread(pi
.hThread
, &ctx
);
885 ok(!status
, "NtGetContextThread failed with 0x%x\n", status
);
887 trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
888 de
.u
.Exception
.ExceptionRecord
.ExceptionCode
,
889 de
.u
.Exception
.ExceptionRecord
.ExceptionAddress
, de
.u
.Exception
.dwFirstChance
, ctx
.Eip
, ctx
.Eax
);
893 ok(FALSE
, "got way too many exceptions, probably caught in an infinite loop, terminating child\n");
894 pNtTerminateProcess(pi
.hProcess
, 1);
896 else if (counter
>= 2) /* skip startup breakpoint */
900 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 0xb, "Eip at %x instead of %p\n",
901 ctx
.Eip
, (char *)code_mem_address
+ 0xb);
902 /* setting the context from debugger does not affect the context, the exception handlers gets */
903 /* uncomment once wine is fixed */
904 /* ctx.Eip = 0x12345; */
905 ctx
.Eax
= 0xf00f00f1;
907 /* let the debuggee handle the exception */
908 continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
912 if (de
.u
.Exception
.dwFirstChance
)
914 /* debugger gets first chance exception with unmodified ctx.Eip */
915 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 0xb, "Eip at 0x%x instead of %p\n",
916 ctx
.Eip
, (char *)code_mem_address
+ 0xb);
918 /* setting the context from debugger does not affect the context, the exception handlers gets */
919 /* uncomment once wine is fixed */
920 /* ctx.Eip = 0x12345; */
921 ctx
.Eax
= 0xf00f00f1;
923 /* pass exception to debuggee
924 * exception will not be handled and
925 * a second chance exception will be raised */
926 continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
930 /* debugger gets context after exception handler has played with it */
931 /* ctx.Eip is the same value the exception handler got */
932 if (de
.u
.Exception
.ExceptionRecord
.ExceptionCode
== EXCEPTION_BREAKPOINT
)
934 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 0xa ||
935 broken(is_wow64
&& (char *)ctx
.Eip
== (char *)code_mem_address
+ 0xb),
936 "Eip at 0x%x instead of %p\n",
937 ctx
.Eip
, (char *)code_mem_address
+ 0xa);
938 /* need to fixup Eip for debuggee */
939 if ((char *)ctx
.Eip
== (char *)code_mem_address
+ 0xa)
943 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 0xb, "Eip at 0x%x instead of %p\n",
944 ctx
.Eip
, (char *)code_mem_address
+ 0xb);
945 /* here we handle exception */
948 else if (stage
== 7 || stage
== 8)
950 ok(de
.u
.Exception
.ExceptionRecord
.ExceptionCode
== EXCEPTION_BREAKPOINT
,
951 "expected EXCEPTION_BREAKPOINT, got %08x\n", de
.u
.Exception
.ExceptionRecord
.ExceptionCode
);
952 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 0x1d,
953 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address
+ 0x1d, ctx
.Eip
);
955 if (stage
== 8) continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
957 else if (stage
== 9 || stage
== 10)
959 ok(de
.u
.Exception
.ExceptionRecord
.ExceptionCode
== EXCEPTION_BREAKPOINT
,
960 "expected EXCEPTION_BREAKPOINT, got %08x\n", de
.u
.Exception
.ExceptionRecord
.ExceptionCode
);
961 ok((char *)ctx
.Eip
== (char *)code_mem_address
+ 2,
962 "expected Eip = %p, got 0x%x\n", (char *)code_mem_address
+ 2, ctx
.Eip
);
964 if (stage
== 10) continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
967 ok(FALSE
, "unexpected stage %x\n", stage
);
969 status
= pNtSetContextThread(pi
.hThread
, &ctx
);
970 ok(!status
, "NtSetContextThread failed with 0x%x\n", status
);
973 else if (de
.dwDebugEventCode
== OUTPUT_DEBUG_STRING_EVENT
)
978 status
= pNtReadVirtualMemory(pi
.hProcess
, &test_stage
, &stage
,
979 sizeof(stage
), &size_read
);
980 ok(!status
,"NtReadVirtualMemory failed with 0x%x\n", status
);
982 ok(!de
.u
.DebugString
.fUnicode
, "unexpected unicode debug string event\n");
983 ok(de
.u
.DebugString
.nDebugStringLength
< sizeof(buffer
) - 1, "buffer not large enough to hold %d bytes\n",
984 de
.u
.DebugString
.nDebugStringLength
);
986 memset(buffer
, 0, sizeof(buffer
));
987 status
= pNtReadVirtualMemory(pi
.hProcess
, de
.u
.DebugString
.lpDebugStringData
, buffer
,
988 de
.u
.DebugString
.nDebugStringLength
, &size_read
);
989 ok(!status
,"NtReadVirtualMemory failed with 0x%x\n", status
);
991 if (stage
== 3 || stage
== 4)
992 ok(!strcmp(buffer
, "Hello World"), "got unexpected debug string '%s'\n", buffer
);
993 else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
994 ok(strstr(buffer
, "SHIMVIEW") != NULL
, "unexpected stage %x, got debug string event '%s'\n", stage
, buffer
);
996 if (stage
== 4) continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
998 else if (de
.dwDebugEventCode
== RIP_EVENT
)
1002 status
= pNtReadVirtualMemory(pi
.hProcess
, &test_stage
, &stage
,
1003 sizeof(stage
), &size_read
);
1004 ok(!status
,"NtReadVirtualMemory failed with 0x%x\n", status
);
1006 if (stage
== 5 || stage
== 6)
1008 ok(de
.u
.RipInfo
.dwError
== 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
1009 de
.u
.RipInfo
.dwError
, 0x11223344);
1010 ok(de
.u
.RipInfo
.dwType
== 0x55667788, "got unexpected rip type %08x, expected %08x\n",
1011 de
.u
.RipInfo
.dwType
, 0x55667788);
1014 ok(FALSE
, "unexpected stage %x\n", stage
);
1016 if (stage
== 6) continuestatus
= DBG_EXCEPTION_NOT_HANDLED
;
1019 ContinueDebugEvent(de
.dwProcessId
, de
.dwThreadId
, continuestatus
);
1021 } while (de
.dwDebugEventCode
!= EXIT_PROCESS_DEBUG_EVENT
);
1023 winetest_wait_child_process( pi
.hProcess
);
1024 ret
= CloseHandle(pi
.hThread
);
1025 ok(ret
, "error %u\n", GetLastError());
1026 ret
= CloseHandle(pi
.hProcess
);
1027 ok(ret
, "error %u\n", GetLastError());
1032 static DWORD
simd_fault_handler( EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
1033 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
1035 int *stage
= *(int **)(frame
+ 1);
1040 /* fault while executing sse instruction */
1041 context
->Eip
+= 3; /* skip addps */
1042 return ExceptionContinueExecution
;
1044 else if ( *stage
== 2 || *stage
== 3 ) {
1045 /* stage 2 - divide by zero fault */
1046 /* stage 3 - invalid operation fault */
1047 if( rec
->ExceptionCode
== EXCEPTION_ILLEGAL_INSTRUCTION
)
1048 skip("system doesn't support SIMD exceptions\n");
1050 ok( rec
->ExceptionCode
== STATUS_FLOAT_MULTIPLE_TRAPS
,
1051 "exception code: %#x, should be %#x\n",
1052 rec
->ExceptionCode
, STATUS_FLOAT_MULTIPLE_TRAPS
);
1053 ok( rec
->NumberParameters
== 1 || broken(is_wow64
&& rec
->NumberParameters
== 2),
1054 "# of params: %i, should be 1\n",
1055 rec
->NumberParameters
);
1056 if( rec
->NumberParameters
== 1 )
1057 ok( rec
->ExceptionInformation
[0] == 0, "param #1: %lx, should be 0\n", rec
->ExceptionInformation
[0]);
1059 context
->Eip
+= 3; /* skip divps */
1062 ok(FALSE
, "unexpected stage %x\n", *stage
);
1064 return ExceptionContinueExecution
;
1067 static const BYTE simd_exception_test
[] = {
1068 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1069 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1070 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1071 0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
1072 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
1073 0x6a, 0x01, /* push $0x1 */
1074 0x6a, 0x01, /* push $0x1 */
1075 0x6a, 0x01, /* push $0x1 */
1076 0x6a, 0x01, /* push $0x1 */
1077 0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
1078 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1079 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1080 0x83, 0xc4, 0x10, /* add $0x10,%esp */
1081 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1082 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1083 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1087 static const BYTE simd_exception_test2
[] = {
1088 0x83, 0xec, 0x4, /* sub $0x4, %esp */
1089 0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
1090 0x8b, 0x04, 0x24, /* mov (%esp),%eax * store mxcsr */
1091 0x66, 0x81, 0x24, 0x24, 0x7f, 0xff, /* andw $0xff7f,(%esp) * enable invalid */
1092 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * operation exceptions */
1093 0x0f, 0x57, 0xc9, /* xorps %xmm1,%xmm1 * clear dividend */
1094 0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
1095 0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
1096 0x89, 0x04, 0x24, /* mov %eax,(%esp) * restore to old mxcsr */
1097 0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
1098 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1102 static const BYTE sse_check
[] = {
1103 0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
1107 static void test_simd_exceptions(void)
1111 /* test if CPU & OS can do sse */
1114 run_exception_test(simd_fault_handler
, &stage
, sse_check
, sizeof(sse_check
), 0);
1116 skip("system doesn't support SSE\n");
1120 /* generate a SIMD exception */
1123 run_exception_test(simd_fault_handler
, &stage
, simd_exception_test
,
1124 sizeof(simd_exception_test
), 0);
1125 ok(got_exception
== 1, "got exception: %i, should be 1\n", got_exception
);
1127 /* generate a SIMD exception, test FPE_FLTINV */
1130 run_exception_test(simd_fault_handler
, &stage
, simd_exception_test2
,
1131 sizeof(simd_exception_test2
), 0);
1132 ok(got_exception
== 1, "got exception: %i, should be 1\n", got_exception
);
1135 struct fpu_exception_info
1137 DWORD exception_code
;
1138 DWORD exception_offset
;
1142 static DWORD
fpu_exception_handler(EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
1143 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
1145 struct fpu_exception_info
*info
= *(struct fpu_exception_info
**)(frame
+ 1);
1147 info
->exception_code
= rec
->ExceptionCode
;
1148 info
->exception_offset
= (BYTE
*)rec
->ExceptionAddress
- (BYTE
*)code_mem
;
1149 info
->eip_offset
= context
->Eip
- (DWORD
)code_mem
;
1152 return ExceptionContinueExecution
;
1155 static void test_fpu_exceptions(void)
1157 static const BYTE fpu_exception_test_ie
[] =
1159 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1160 0x66, 0xc7, 0x04, 0x24, 0xfe, 0x03, /* movw $0x3fe,(%esp) */
1161 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1162 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1163 0xd9, 0xee, /* fldz */
1164 0xd9, 0xe8, /* fld1 */
1165 0xde, 0xf1, /* fdivp */
1166 0xdd, 0xd8, /* fstp %st(0) */
1167 0xdd, 0xd8, /* fstp %st(0) */
1169 0xdb, 0xe2, /* fnclex */
1170 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1171 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1175 static const BYTE fpu_exception_test_de
[] =
1177 0x83, 0xec, 0x04, /* sub $0x4,%esp */
1178 0x66, 0xc7, 0x04, 0x24, 0xfb, 0x03, /* movw $0x3fb,(%esp) */
1179 0x9b, 0xd9, 0x7c, 0x24, 0x02, /* fstcw 0x2(%esp) */
1180 0xd9, 0x2c, 0x24, /* fldcw (%esp) */
1181 0xdd, 0xd8, /* fstp %st(0) */
1182 0xd9, 0xee, /* fldz */
1183 0xd9, 0xe8, /* fld1 */
1184 0xde, 0xf1, /* fdivp */
1186 0xdb, 0xe2, /* fnclex */
1187 0xdd, 0xd8, /* fstp %st(0) */
1188 0xdd, 0xd8, /* fstp %st(0) */
1189 0xd9, 0x6c, 0x24, 0x02, /* fldcw 0x2(%esp) */
1190 0x83, 0xc4, 0x04, /* add $0x4,%esp */
1194 struct fpu_exception_info info
;
1196 memset(&info
, 0, sizeof(info
));
1197 run_exception_test(fpu_exception_handler
, &info
, fpu_exception_test_ie
, sizeof(fpu_exception_test_ie
), 0);
1198 ok(info
.exception_code
== EXCEPTION_FLT_STACK_CHECK
,
1199 "Got exception code %#x, expected EXCEPTION_FLT_STACK_CHECK\n", info
.exception_code
);
1200 ok(info
.exception_offset
== 0x19 ||
1201 broken( is_wow64
&& info
.exception_offset
== info
.eip_offset
),
1202 "Got exception offset %#x, expected 0x19\n", info
.exception_offset
);
1203 ok(info
.eip_offset
== 0x1b, "Got EIP offset %#x, expected 0x1b\n", info
.eip_offset
);
1205 memset(&info
, 0, sizeof(info
));
1206 run_exception_test(fpu_exception_handler
, &info
, fpu_exception_test_de
, sizeof(fpu_exception_test_de
), 0);
1207 ok(info
.exception_code
== EXCEPTION_FLT_DIVIDE_BY_ZERO
,
1208 "Got exception code %#x, expected EXCEPTION_FLT_DIVIDE_BY_ZERO\n", info
.exception_code
);
1209 ok(info
.exception_offset
== 0x17 ||
1210 broken( is_wow64
&& info
.exception_offset
== info
.eip_offset
),
1211 "Got exception offset %#x, expected 0x17\n", info
.exception_offset
);
1212 ok(info
.eip_offset
== 0x19, "Got EIP offset %#x, expected 0x19\n", info
.eip_offset
);
1215 struct dpe_exception_info
{
1216 BOOL exception_caught
;
1217 DWORD exception_info
;
1220 static DWORD
dpe_exception_handler(EXCEPTION_RECORD
*rec
, EXCEPTION_REGISTRATION_RECORD
*frame
,
1221 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**dispatcher
)
1224 struct dpe_exception_info
*info
= *(struct dpe_exception_info
**)(frame
+ 1);
1226 ok(rec
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
,
1227 "Exception code %08x\n", rec
->ExceptionCode
);
1228 ok(rec
->NumberParameters
== 2,
1229 "Parameter count: %d\n", rec
->NumberParameters
);
1230 ok((LPVOID
)rec
->ExceptionInformation
[1] == code_mem
,
1231 "Exception address: %p, expected %p\n",
1232 (LPVOID
)rec
->ExceptionInformation
[1], code_mem
);
1234 info
->exception_info
= rec
->ExceptionInformation
[0];
1235 info
->exception_caught
= TRUE
;
1237 VirtualProtect(code_mem
, 1, PAGE_EXECUTE_READWRITE
, &old_prot
);
1238 return ExceptionContinueExecution
;
1241 static void test_dpe_exceptions(void)
1243 static const BYTE single_ret
[] = {0xC3};
1244 struct dpe_exception_info info
;
1246 BOOL has_hw_support
;
1247 BOOL is_permanent
= FALSE
, can_test_without
= TRUE
, can_test_with
= TRUE
;
1251 /* Query DEP with len too small */
1252 stat
= pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
- 1, &len
);
1253 if(stat
== STATUS_INVALID_INFO_CLASS
)
1255 skip("This software platform does not support DEP\n");
1258 ok(stat
== STATUS_INFO_LENGTH_MISMATCH
, "buffer too small: %08x\n", stat
);
1261 stat
= pNtQueryInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
, &len
);
1262 ok(stat
== STATUS_SUCCESS
, "querying DEP: status %08x\n", stat
);
1263 if(stat
== STATUS_SUCCESS
)
1265 ok(len
== sizeof val
, "returned length: %d\n", len
);
1266 if(val
& MEM_EXECUTE_OPTION_PERMANENT
)
1268 skip("toggling DEP impossible - status locked\n");
1269 is_permanent
= TRUE
;
1270 if(val
& MEM_EXECUTE_OPTION_DISABLE
)
1271 can_test_without
= FALSE
;
1273 can_test_with
= FALSE
;
1280 val
= MEM_EXECUTE_OPTION_DISABLE
;
1281 stat
= pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
);
1282 ok(stat
== STATUS_SUCCESS
, "enabling DEP: status %08x\n", stat
);
1287 /* Try access to locked page with DEP on*/
1288 info
.exception_caught
= FALSE
;
1289 run_exception_test(dpe_exception_handler
, &info
, single_ret
, sizeof(single_ret
), PAGE_NOACCESS
);
1290 ok(info
.exception_caught
== TRUE
, "Execution of disabled memory succeeded\n");
1291 ok(info
.exception_info
== EXCEPTION_READ_FAULT
||
1292 info
.exception_info
== EXCEPTION_EXECUTE_FAULT
,
1293 "Access violation type: %08x\n", (unsigned)info
.exception_info
);
1294 has_hw_support
= info
.exception_info
== EXCEPTION_EXECUTE_FAULT
;
1295 trace("DEP hardware support: %s\n", has_hw_support
?"Yes":"No");
1297 /* Try execution of data with DEP on*/
1298 info
.exception_caught
= FALSE
;
1299 run_exception_test(dpe_exception_handler
, &info
, single_ret
, sizeof(single_ret
), PAGE_READWRITE
);
1302 ok(info
.exception_caught
== TRUE
, "Execution of data memory succeeded\n");
1303 ok(info
.exception_info
== EXCEPTION_EXECUTE_FAULT
,
1304 "Access violation type: %08x\n", (unsigned)info
.exception_info
);
1307 ok(info
.exception_caught
== FALSE
, "Execution trapped without hardware support\n");
1310 skip("DEP is in AlwaysOff state\n");
1315 val
= MEM_EXECUTE_OPTION_ENABLE
;
1316 stat
= pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
);
1317 ok(stat
== STATUS_SUCCESS
, "disabling DEP: status %08x\n", stat
);
1320 /* page is read without exec here */
1321 if(can_test_without
)
1323 /* Try execution of data with DEP off */
1324 info
.exception_caught
= FALSE
;
1325 run_exception_test(dpe_exception_handler
, &info
, single_ret
, sizeof(single_ret
), PAGE_READWRITE
);
1326 ok(info
.exception_caught
== FALSE
, "Execution trapped with DEP turned off\n");
1328 /* Try access to locked page with DEP off - error code is different than
1329 with hardware DEP on */
1330 info
.exception_caught
= FALSE
;
1331 run_exception_test(dpe_exception_handler
, &info
, single_ret
, sizeof(single_ret
), PAGE_NOACCESS
);
1332 ok(info
.exception_caught
== TRUE
, "Execution of disabled memory succeeded\n");
1333 ok(info
.exception_info
== EXCEPTION_READ_FAULT
,
1334 "Access violation type: %08x\n", (unsigned)info
.exception_info
);
1337 skip("DEP is in AlwaysOn state\n");
1341 /* Turn off DEP permanently */
1342 val
= MEM_EXECUTE_OPTION_ENABLE
| MEM_EXECUTE_OPTION_PERMANENT
;
1343 stat
= pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
);
1344 ok(stat
== STATUS_SUCCESS
, "disabling DEP permanently: status %08x\n", stat
);
1347 /* Try to turn off DEP */
1348 val
= MEM_EXECUTE_OPTION_ENABLE
;
1349 stat
= pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
);
1350 ok(stat
== STATUS_ACCESS_DENIED
, "disabling DEP while permanent: status %08x\n", stat
);
1352 /* Try to turn on DEP */
1353 val
= MEM_EXECUTE_OPTION_DISABLE
;
1354 stat
= pNtSetInformationProcess(GetCurrentProcess(), ProcessExecuteFlags
, &val
, sizeof val
);
1355 ok(stat
== STATUS_ACCESS_DENIED
, "enabling DEP while permanent: status %08x\n", stat
);
1358 #elif defined(__x86_64__)
1362 #define UNW_FLAG_NHANDLER 0
1363 #define UNW_FLAG_EHANDLER 1
1364 #define UNW_FLAG_UHANDLER 2
1365 #define UNW_FLAG_CHAININFO 4
1367 #define UWOP_PUSH_NONVOL 0
1368 #define UWOP_ALLOC_LARGE 1
1369 #define UWOP_ALLOC_SMALL 2
1370 #define UWOP_SET_FPREG 3
1371 #define UWOP_SAVE_NONVOL 4
1372 #define UWOP_SAVE_NONVOL_FAR 5
1373 #define UWOP_SAVE_XMM128 8
1374 #define UWOP_SAVE_XMM128_FAR 9
1375 #define UWOP_PUSH_MACHFRAME 10
1379 int rip_offset
; /* rip offset from code start */
1380 int rbp_offset
; /* rbp offset from stack pointer */
1381 int handler
; /* expect handler to be set? */
1382 int rip
; /* expected final rip value */
1383 int frame
; /* expected frame return value */
1384 int regs
[8][2]; /* expected values for registers */
1389 const BYTE
*function
;
1390 size_t function_size
;
1391 const BYTE
*unwind_info
;
1392 const struct results
*results
;
1393 unsigned int nb_results
;
1398 rax
, rcx
, rdx
, rbx
, rsp
, rbp
, rsi
, rdi
,
1399 r8
, r9
, r10
, r11
, r12
, r13
, r14
, r15
1402 static const char * const reg_names
[16] =
1404 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1405 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1408 #define UWOP(code,info) (UWOP_##code | ((info) << 4))
1410 static void call_virtual_unwind( int testnum
, const struct unwind_test
*test
)
1412 static const int code_offset
= 1024;
1413 static const int unwind_offset
= 2048;
1414 void *handler
, *data
;
1416 RUNTIME_FUNCTION runtime_func
;
1417 KNONVOLATILE_CONTEXT_POINTERS ctx_ptr
;
1419 ULONG64 fake_stack
[256];
1420 ULONG64 frame
, orig_rip
, orig_rbp
, unset_reg
;
1421 UINT unwind_size
= 4 + 2 * test
->unwind_info
[2] + 8;
1423 memcpy( (char *)code_mem
+ code_offset
, test
->function
, test
->function_size
);
1424 memcpy( (char *)code_mem
+ unwind_offset
, test
->unwind_info
, unwind_size
);
1426 runtime_func
.BeginAddress
= code_offset
;
1427 runtime_func
.EndAddress
= code_offset
+ test
->function_size
;
1428 runtime_func
.UnwindData
= unwind_offset
;
1430 trace( "code: %p stack: %p\n", code_mem
, fake_stack
);
1432 for (i
= 0; i
< test
->nb_results
; i
++)
1434 memset( &ctx_ptr
, 0, sizeof(ctx_ptr
) );
1435 memset( &context
, 0x55, sizeof(context
) );
1436 memset( &unset_reg
, 0x55, sizeof(unset_reg
) );
1437 for (j
= 0; j
< 256; j
++) fake_stack
[j
] = j
* 8;
1439 context
.Rsp
= (ULONG_PTR
)fake_stack
;
1440 context
.Rbp
= (ULONG_PTR
)fake_stack
+ test
->results
[i
].rbp_offset
;
1441 orig_rbp
= context
.Rbp
;
1442 orig_rip
= (ULONG64
)code_mem
+ code_offset
+ test
->results
[i
].rip_offset
;
1444 trace( "%u/%u: rip=%p (%02x) rbp=%p rsp=%p\n", testnum
, i
,
1445 (void *)orig_rip
, *(BYTE
*)orig_rip
, (void *)orig_rbp
, (void *)context
.Rsp
);
1447 data
= (void *)0xdeadbeef;
1448 handler
= RtlVirtualUnwind( UNW_FLAG_EHANDLER
, (ULONG64
)code_mem
, orig_rip
,
1449 &runtime_func
, &context
, &data
, &frame
, &ctx_ptr
);
1450 if (test
->results
[i
].handler
)
1452 ok( (char *)handler
== (char *)code_mem
+ 0x200,
1453 "%u/%u: wrong handler %p/%p\n", testnum
, i
, handler
, (char *)code_mem
+ 0x200 );
1454 if (handler
) ok( *(DWORD
*)data
== 0x08070605,
1455 "%u/%u: wrong handler data %p\n", testnum
, i
, data
);
1459 ok( handler
== NULL
, "%u/%u: handler %p instead of NULL\n", testnum
, i
, handler
);
1460 ok( data
== (void *)0xdeadbeef, "%u/%u: handler data set to %p\n", testnum
, i
, data
);
1463 ok( context
.Rip
== test
->results
[i
].rip
, "%u/%u: wrong rip %p/%x\n",
1464 testnum
, i
, (void *)context
.Rip
, test
->results
[i
].rip
);
1465 ok( frame
== (ULONG64
)fake_stack
+ test
->results
[i
].frame
, "%u/%u: wrong frame %p/%p\n",
1466 testnum
, i
, (void *)frame
, (char *)fake_stack
+ test
->results
[i
].frame
);
1468 for (j
= 0; j
< 16; j
++)
1470 static const UINT nb_regs
= sizeof(test
->results
[i
].regs
) / sizeof(test
->results
[i
].regs
[0]);
1472 for (k
= 0; k
< nb_regs
; k
++)
1474 if (test
->results
[i
].regs
[k
][0] == -1)
1479 if (test
->results
[i
].regs
[k
][0] == j
) break;
1482 if (j
== rsp
) /* rsp is special */
1484 ok( !ctx_ptr
.u2
.IntegerContext
[j
],
1485 "%u/%u: rsp should not be set in ctx_ptr\n", testnum
, i
);
1486 ok( context
.Rsp
== (ULONG64
)fake_stack
+ test
->results
[i
].regs
[k
][1],
1487 "%u/%u: register rsp wrong %p/%p\n",
1488 testnum
, i
, (void *)context
.Rsp
, (char *)fake_stack
+ test
->results
[i
].regs
[k
][1] );
1492 if (ctx_ptr
.u2
.IntegerContext
[j
])
1494 ok( k
< nb_regs
, "%u/%u: register %s should not be set to %lx\n",
1495 testnum
, i
, reg_names
[j
], *(&context
.Rax
+ j
) );
1497 ok( *(&context
.Rax
+ j
) == test
->results
[i
].regs
[k
][1],
1498 "%u/%u: register %s wrong %p/%x\n",
1499 testnum
, i
, reg_names
[j
], (void *)*(&context
.Rax
+ j
), test
->results
[i
].regs
[k
][1] );
1503 ok( k
== nb_regs
, "%u/%u: register %s should be set\n", testnum
, i
, reg_names
[j
] );
1505 ok( context
.Rbp
== orig_rbp
, "%u/%u: register rbp wrong %p/unset\n",
1506 testnum
, i
, (void *)context
.Rbp
);
1508 ok( *(&context
.Rax
+ j
) == unset_reg
,
1509 "%u/%u: register %s wrong %p/unset\n",
1510 testnum
, i
, reg_names
[j
], (void *)*(&context
.Rax
+ j
));
1516 static void test_virtual_unwind(void)
1518 static const BYTE function_0
[] =
1520 0xff, 0xf5, /* 00: push %rbp */
1521 0x48, 0x81, 0xec, 0x10, 0x01, 0x00, 0x00, /* 02: sub $0x110,%rsp */
1522 0x48, 0x8d, 0x6c, 0x24, 0x30, /* 09: lea 0x30(%rsp),%rbp */
1523 0x48, 0x89, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 0e: mov %rbx,0xf0(%rbp) */
1524 0x48, 0x89, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 15: mov %rsi,0xf8(%rbp) */
1526 0x48, 0x8b, 0x9d, 0xf0, 0x00, 0x00, 0x00, /* 1d: mov 0xf0(%rbp),%rbx */
1527 0x48, 0x8b, 0xb5, 0xf8, 0x00, 0x00, 0x00, /* 24: mov 0xf8(%rbp),%rsi */
1528 0x48, 0x8d, 0xa5, 0xe0, 0x00, 0x00, 0x00, /* 2b: lea 0xe0(%rbp),%rsp */
1529 0x5d, /* 32: pop %rbp */
1533 static const BYTE unwind_info_0
[] =
1535 1 | (UNW_FLAG_EHANDLER
<< 3), /* version + flags */
1536 0x1c, /* prolog size */
1537 8, /* opcode count */
1538 (0x03 << 4) | rbp
, /* frame reg rbp offset 0x30 */
1540 0x1c, UWOP(SAVE_NONVOL
, rsi
), 0x25, 0, /* 1c: mov %rsi,0x128(%rsp) */
1541 0x15, UWOP(SAVE_NONVOL
, rbx
), 0x24, 0, /* 15: mov %rbx,0x120(%rsp) */
1542 0x0e, UWOP(SET_FPREG
, rbp
), /* 0e: lea 0x30(%rsp),rbp */
1543 0x09, UWOP(ALLOC_LARGE
, 0), 0x22, 0, /* 09: sub $0x110,%rsp */
1544 0x02, UWOP(PUSH_NONVOL
, rbp
), /* 02: push %rbp */
1546 0x00, 0x02, 0x00, 0x00, /* handler */
1547 0x05, 0x06, 0x07, 0x08, /* data */
1550 static const struct results results_0
[] =
1552 /* offset rbp handler rip frame registers */
1553 { 0x00, 0x40, FALSE
, 0x000, 0x000, { {rsp
,0x008}, {-1,-1} }},
1554 { 0x02, 0x40, FALSE
, 0x008, 0x000, { {rsp
,0x010}, {rbp
,0x000}, {-1,-1} }},
1555 { 0x09, 0x40, FALSE
, 0x118, 0x000, { {rsp
,0x120}, {rbp
,0x110}, {-1,-1} }},
1556 { 0x0e, 0x40, FALSE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {-1,-1} }},
1557 { 0x15, 0x40, FALSE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {rbx
,0x130}, {-1,-1} }},
1558 { 0x1c, 0x40, TRUE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {rbx
,0x130}, {rsi
,0x138}, {-1,-1}}},
1559 { 0x1d, 0x40, TRUE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {rbx
,0x130}, {rsi
,0x138}, {-1,-1}}},
1560 { 0x24, 0x40, TRUE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {rbx
,0x130}, {rsi
,0x138}, {-1,-1}}},
1561 { 0x2b, 0x40, FALSE
, 0x128, 0x010, { {rsp
,0x130}, {rbp
,0x120}, {-1,-1}}},
1562 { 0x32, 0x40, FALSE
, 0x008, 0x010, { {rsp
,0x010}, {rbp
,0x000}, {-1,-1}}},
1563 { 0x33, 0x40, FALSE
, 0x000, 0x010, { {rsp
,0x008}, {-1,-1}}},
1567 static const BYTE function_1
[] =
1569 0x53, /* 00: push %rbx */
1570 0x55, /* 01: push %rbp */
1571 0x56, /* 02: push %rsi */
1572 0x57, /* 03: push %rdi */
1573 0x41, 0x54, /* 04: push %r12 */
1574 0x48, 0x83, 0xec, 0x30, /* 06: sub $0x30,%rsp */
1575 0x90, 0x90, /* 0a: nop; nop */
1576 0x48, 0x83, 0xc4, 0x30, /* 0c: add $0x30,%rsp */
1577 0x41, 0x5c, /* 10: pop %r12 */
1578 0x5f, /* 12: pop %rdi */
1579 0x5e, /* 13: pop %rsi */
1580 0x5d, /* 14: pop %rbp */
1581 0x5b, /* 15: pop %rbx */
1585 static const BYTE unwind_info_1
[] =
1587 1 | (UNW_FLAG_EHANDLER
<< 3), /* version + flags */
1588 0x0a, /* prolog size */
1589 6, /* opcode count */
1592 0x0a, UWOP(ALLOC_SMALL
, 5), /* 0a: sub $0x30,%rsp */
1593 0x06, UWOP(PUSH_NONVOL
, r12
), /* 06: push %r12 */
1594 0x04, UWOP(PUSH_NONVOL
, rdi
), /* 04: push %rdi */
1595 0x03, UWOP(PUSH_NONVOL
, rsi
), /* 03: push %rsi */
1596 0x02, UWOP(PUSH_NONVOL
, rbp
), /* 02: push %rbp */
1597 0x01, UWOP(PUSH_NONVOL
, rbx
), /* 01: push %rbx */
1599 0x00, 0x02, 0x00, 0x00, /* handler */
1600 0x05, 0x06, 0x07, 0x08, /* data */
1603 static const struct results results_1
[] =
1605 /* offset rbp handler rip frame registers */
1606 { 0x00, 0x50, FALSE
, 0x000, 0x000, { {rsp
,0x008}, {-1,-1} }},
1607 { 0x01, 0x50, FALSE
, 0x008, 0x000, { {rsp
,0x010}, {rbx
,0x000}, {-1,-1} }},
1608 { 0x02, 0x50, FALSE
, 0x010, 0x000, { {rsp
,0x018}, {rbx
,0x008}, {rbp
,0x000}, {-1,-1} }},
1609 { 0x03, 0x50, FALSE
, 0x018, 0x000, { {rsp
,0x020}, {rbx
,0x010}, {rbp
,0x008}, {rsi
,0x000}, {-1,-1} }},
1610 { 0x04, 0x50, FALSE
, 0x020, 0x000, { {rsp
,0x028}, {rbx
,0x018}, {rbp
,0x010}, {rsi
,0x008}, {rdi
,0x000}, {-1,-1} }},
1611 { 0x06, 0x50, FALSE
, 0x028, 0x000, { {rsp
,0x030}, {rbx
,0x020}, {rbp
,0x018}, {rsi
,0x010}, {rdi
,0x008}, {r12
,0x000}, {-1,-1} }},
1612 { 0x0a, 0x50, TRUE
, 0x058, 0x000, { {rsp
,0x060}, {rbx
,0x050}, {rbp
,0x048}, {rsi
,0x040}, {rdi
,0x038}, {r12
,0x030}, {-1,-1} }},
1613 { 0x0c, 0x50, FALSE
, 0x058, 0x000, { {rsp
,0x060}, {rbx
,0x050}, {rbp
,0x048}, {rsi
,0x040}, {rdi
,0x038}, {r12
,0x030}, {-1,-1} }},
1614 { 0x10, 0x50, FALSE
, 0x028, 0x000, { {rsp
,0x030}, {rbx
,0x020}, {rbp
,0x018}, {rsi
,0x010}, {rdi
,0x008}, {r12
,0x000}, {-1,-1} }},
1615 { 0x12, 0x50, FALSE
, 0x020, 0x000, { {rsp
,0x028}, {rbx
,0x018}, {rbp
,0x010}, {rsi
,0x008}, {rdi
,0x000}, {-1,-1} }},
1616 { 0x13, 0x50, FALSE
, 0x018, 0x000, { {rsp
,0x020}, {rbx
,0x010}, {rbp
,0x008}, {rsi
,0x000}, {-1,-1} }},
1617 { 0x14, 0x50, FALSE
, 0x010, 0x000, { {rsp
,0x018}, {rbx
,0x008}, {rbp
,0x000}, {-1,-1} }},
1618 { 0x15, 0x50, FALSE
, 0x008, 0x000, { {rsp
,0x010}, {rbx
,0x000}, {-1,-1} }},
1619 { 0x16, 0x50, FALSE
, 0x000, 0x000, { {rsp
,0x008}, {-1,-1} }},
1622 static const struct unwind_test tests
[] =
1624 { function_0
, sizeof(function_0
), unwind_info_0
,
1625 results_0
, sizeof(results_0
)/sizeof(results_0
[0]) },
1626 { function_1
, sizeof(function_1
), unwind_info_1
,
1627 results_1
, sizeof(results_1
)/sizeof(results_1
[0]) }
1631 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++)
1632 call_virtual_unwind( i
, &tests
[i
] );
1635 static RUNTIME_FUNCTION
* CALLBACK
dynamic_unwind_callback( DWORD64 pc
, PVOID context
)
1637 static const int code_offset
= 1024;
1638 static RUNTIME_FUNCTION runtime_func
;
1639 (*(DWORD
*)context
)++;
1641 runtime_func
.BeginAddress
= code_offset
+ 16;
1642 runtime_func
.EndAddress
= code_offset
+ 32;
1643 runtime_func
.UnwindData
= 0;
1644 return &runtime_func
;
1647 static void test_dynamic_unwind(void)
1649 static const int code_offset
= 1024;
1650 char buf
[sizeof(RUNTIME_FUNCTION
) + 4];
1651 RUNTIME_FUNCTION
*runtime_func
, *func
;
1652 ULONG_PTR table
, base
;
1655 /* Test RtlAddFunctionTable with aligned RUNTIME_FUNCTION pointer */
1656 runtime_func
= (RUNTIME_FUNCTION
*)buf
;
1657 runtime_func
->BeginAddress
= code_offset
;
1658 runtime_func
->EndAddress
= code_offset
+ 16;
1659 runtime_func
->UnwindData
= 0;
1660 ok( pRtlAddFunctionTable( runtime_func
, 1, (ULONG_PTR
)code_mem
),
1661 "RtlAddFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func
);
1663 /* Lookup function outside of any function table */
1665 func
= pRtlLookupFunctionEntry( (ULONG_PTR
)code_mem
+ code_offset
+ 16, &base
, NULL
);
1667 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func
);
1668 ok( !base
|| broken(base
== 0xdeadbeef),
1669 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base
);
1671 /* Test with pointer inside of our function */
1673 func
= pRtlLookupFunctionEntry( (ULONG_PTR
)code_mem
+ code_offset
+ 8, &base
, NULL
);
1674 ok( func
== runtime_func
,
1675 "RtlLookupFunctionEntry didn't return expected function, expected: %p, got: %p\n", runtime_func
, func
);
1676 ok( base
== (ULONG_PTR
)code_mem
,
1677 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR
)code_mem
, base
);
1679 /* Test RtlDeleteFunctionTable */
1680 ok( pRtlDeleteFunctionTable( runtime_func
),
1681 "RtlDeleteFunctionTable failed for runtime_func = %p (aligned)\n", runtime_func
);
1682 ok( !pRtlDeleteFunctionTable( runtime_func
),
1683 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func
);
1685 /* Unaligned RUNTIME_FUNCTION pointer */
1686 runtime_func
= (RUNTIME_FUNCTION
*)((ULONG_PTR
)buf
| 0x3);
1687 runtime_func
->BeginAddress
= code_offset
;
1688 runtime_func
->EndAddress
= code_offset
+ 16;
1689 runtime_func
->UnwindData
= 0;
1690 ok( pRtlAddFunctionTable( runtime_func
, 1, (ULONG_PTR
)code_mem
),
1691 "RtlAddFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func
);
1692 ok( pRtlDeleteFunctionTable( runtime_func
),
1693 "RtlDeleteFunctionTable failed for runtime_func = %p (unaligned)\n", runtime_func
);
1695 /* Attempt to insert the same entry twice */
1696 runtime_func
= (RUNTIME_FUNCTION
*)buf
;
1697 runtime_func
->BeginAddress
= code_offset
;
1698 runtime_func
->EndAddress
= code_offset
+ 16;
1699 runtime_func
->UnwindData
= 0;
1700 ok( pRtlAddFunctionTable( runtime_func
, 1, (ULONG_PTR
)code_mem
),
1701 "RtlAddFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func
);
1702 ok( pRtlAddFunctionTable( runtime_func
, 1, (ULONG_PTR
)code_mem
),
1703 "RtlAddFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func
);
1704 ok( pRtlDeleteFunctionTable( runtime_func
),
1705 "RtlDeleteFunctionTable failed for runtime_func = %p (first attempt)\n", runtime_func
);
1706 ok( pRtlDeleteFunctionTable( runtime_func
),
1707 "RtlDeleteFunctionTable failed for runtime_func = %p (second attempt)\n", runtime_func
);
1708 ok( !pRtlDeleteFunctionTable( runtime_func
),
1709 "RtlDeleteFunctionTable returned success for nonexistent table runtime_func = %p\n", runtime_func
);
1711 /* Test RtlInstallFunctionTableCallback with both low bits unset */
1712 table
= (ULONG_PTR
)code_mem
;
1713 ok( !pRtlInstallFunctionTableCallback( table
, (ULONG_PTR
)code_mem
, code_offset
+ 32, &dynamic_unwind_callback
, (PVOID
*)&count
, NULL
),
1714 "RtlInstallFunctionTableCallback returned success for table = %lx\n", table
);
1716 /* Test RtlInstallFunctionTableCallback with both low bits set */
1717 table
= (ULONG_PTR
)code_mem
| 0x3;
1718 ok( pRtlInstallFunctionTableCallback( table
, (ULONG_PTR
)code_mem
, code_offset
+ 32, &dynamic_unwind_callback
, (PVOID
*)&count
, NULL
),
1719 "RtlInstallFunctionTableCallback failed for table = %lx\n", table
);
1721 /* Lookup function outside of any function table */
1724 func
= pRtlLookupFunctionEntry( (ULONG_PTR
)code_mem
+ code_offset
+ 32, &base
, NULL
);
1726 "RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func
);
1727 ok( !base
|| broken(base
== 0xdeadbeef),
1728 "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base
);
1730 "RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count
);
1732 /* Test with pointer inside of our function */
1735 func
= pRtlLookupFunctionEntry( (ULONG_PTR
)code_mem
+ code_offset
+ 24, &base
, NULL
);
1736 ok( func
!= NULL
&& func
->BeginAddress
== code_offset
+ 16 && func
->EndAddress
== code_offset
+ 32,
1737 "RtlLookupFunctionEntry didn't return expected function, got: %p\n", func
);
1738 ok( base
== (ULONG_PTR
)code_mem
,
1739 "RtlLookupFunctionEntry returned invalid base, expected: %lx, got: %lx\n", (ULONG_PTR
)code_mem
, base
);
1741 "RtlLookupFunctionEntry issued %d calls to dynamic_unwind_callback, expected: 1\n", count
);
1743 /* Clean up again */
1744 ok( pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION
)table
),
1745 "RtlDeleteFunctionTable failed for table = %p\n", (PVOID
)table
);
1746 ok( !pRtlDeleteFunctionTable( (PRUNTIME_FUNCTION
)table
),
1747 "RtlDeleteFunctionTable returned success for nonexistent table = %p\n", (PVOID
)table
);
1751 #endif /* __x86_64__ */
1753 #if defined(__i386__) || defined(__x86_64__)
1755 static void test_debug_registers(void)
1759 ULONG_PTR dr0
, dr1
, dr2
, dr3
, dr6
, dr7
;
1763 { 0x42424240, 0, 0x126bb070, 0x0badbad0, 0, 0xffff0115 },
1764 { 0x42424242, 0, 0x100f0fe7, 0x0abebabe, 0, 0x115 },
1770 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++)
1772 memset(&ctx
, 0, sizeof(ctx
));
1773 ctx
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1774 ctx
.Dr0
= tests
[i
].dr0
;
1775 ctx
.Dr1
= tests
[i
].dr1
;
1776 ctx
.Dr2
= tests
[i
].dr2
;
1777 ctx
.Dr3
= tests
[i
].dr3
;
1778 ctx
.Dr6
= tests
[i
].dr6
;
1779 ctx
.Dr7
= tests
[i
].dr7
;
1781 status
= pNtSetContextThread(GetCurrentThread(), &ctx
);
1782 ok(status
== STATUS_SUCCESS
, "NtGetContextThread failed with %08x\n", status
);
1784 memset(&ctx
, 0, sizeof(ctx
));
1785 ctx
.ContextFlags
= CONTEXT_DEBUG_REGISTERS
;
1787 status
= pNtGetContextThread(GetCurrentThread(), &ctx
);
1788 ok(status
== STATUS_SUCCESS
, "NtGetContextThread failed with %08x\n", status
);
1789 ok(ctx
.Dr0
== tests
[i
].dr0
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr0
, (DWORD_PTR
)ctx
.Dr0
);
1790 ok(ctx
.Dr1
== tests
[i
].dr1
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr1
, (DWORD_PTR
)ctx
.Dr1
);
1791 ok(ctx
.Dr2
== tests
[i
].dr2
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr2
, (DWORD_PTR
)ctx
.Dr2
);
1792 ok(ctx
.Dr3
== tests
[i
].dr3
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr3
, (DWORD_PTR
)ctx
.Dr3
);
1793 ok((ctx
.Dr6
& 0xf00f) == tests
[i
].dr6
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr6
, (DWORD_PTR
)ctx
.Dr6
);
1794 ok((ctx
.Dr7
& ~0xdc00) == tests
[i
].dr7
, "test %d: expected %lx, got %lx\n", i
, tests
[i
].dr7
, (DWORD_PTR
)ctx
.Dr7
);
1798 static DWORD outputdebugstring_exceptions
;
1800 static LONG CALLBACK
outputdebugstring_vectored_handler(EXCEPTION_POINTERS
*ExceptionInfo
)
1802 PEXCEPTION_RECORD rec
= ExceptionInfo
->ExceptionRecord
;
1803 trace("vect. handler %08x addr:%p\n", rec
->ExceptionCode
, rec
->ExceptionAddress
);
1805 ok(rec
->ExceptionCode
== DBG_PRINTEXCEPTION_C
, "ExceptionCode is %08x instead of %08x\n",
1806 rec
->ExceptionCode
, DBG_PRINTEXCEPTION_C
);
1807 ok(rec
->NumberParameters
== 2, "ExceptionParameters is %d instead of 2\n", rec
->NumberParameters
);
1808 ok(rec
->ExceptionInformation
[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD
)rec
->ExceptionInformation
[0]);
1809 ok(!strcmp((char *)rec
->ExceptionInformation
[1], "Hello World"),
1810 "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec
->ExceptionInformation
[1]);
1812 outputdebugstring_exceptions
++;
1813 return EXCEPTION_CONTINUE_SEARCH
;
1816 static void test_outputdebugstring(DWORD numexc
, BOOL todo
)
1818 PVOID vectored_handler
;
1820 if (!pRtlAddVectoredExceptionHandler
|| !pRtlRemoveVectoredExceptionHandler
)
1822 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
1826 vectored_handler
= pRtlAddVectoredExceptionHandler(TRUE
, &outputdebugstring_vectored_handler
);
1827 ok(vectored_handler
!= 0, "RtlAddVectoredExceptionHandler failed\n");
1829 outputdebugstring_exceptions
= 0;
1830 OutputDebugStringA("Hello World");
1833 ok(outputdebugstring_exceptions
== numexc
, "OutputDebugStringA generated %d exceptions, expected %d\n",
1834 outputdebugstring_exceptions
, numexc
);
1836 pRtlRemoveVectoredExceptionHandler(vectored_handler
);
1839 static DWORD ripevent_exceptions
;
1841 static LONG CALLBACK
ripevent_vectored_handler(EXCEPTION_POINTERS
*ExceptionInfo
)
1843 PEXCEPTION_RECORD rec
= ExceptionInfo
->ExceptionRecord
;
1844 trace("vect. handler %08x addr:%p\n", rec
->ExceptionCode
, rec
->ExceptionAddress
);
1846 ok(rec
->ExceptionCode
== DBG_RIPEXCEPTION
, "ExceptionCode is %08x instead of %08x\n",
1847 rec
->ExceptionCode
, DBG_RIPEXCEPTION
);
1848 ok(rec
->NumberParameters
== 2, "ExceptionParameters is %d instead of 2\n", rec
->NumberParameters
);
1849 ok(rec
->ExceptionInformation
[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
1850 (NTSTATUS
)rec
->ExceptionInformation
[0], 0x11223344);
1851 ok(rec
->ExceptionInformation
[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
1852 (NTSTATUS
)rec
->ExceptionInformation
[1], 0x55667788);
1854 ripevent_exceptions
++;
1855 return (rec
->ExceptionCode
== DBG_RIPEXCEPTION
) ? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_CONTINUE_SEARCH
;
1858 static void test_ripevent(DWORD numexc
)
1860 EXCEPTION_RECORD record
;
1861 PVOID vectored_handler
;
1863 if (!pRtlAddVectoredExceptionHandler
|| !pRtlRemoveVectoredExceptionHandler
|| !pRtlRaiseException
)
1865 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
1869 vectored_handler
= pRtlAddVectoredExceptionHandler(TRUE
, &ripevent_vectored_handler
);
1870 ok(vectored_handler
!= 0, "RtlAddVectoredExceptionHandler failed\n");
1872 record
.ExceptionCode
= DBG_RIPEXCEPTION
;
1873 record
.ExceptionFlags
= 0;
1874 record
.ExceptionRecord
= NULL
;
1875 record
.ExceptionAddress
= NULL
;
1876 record
.NumberParameters
= 2;
1877 record
.ExceptionInformation
[0] = 0x11223344;
1878 record
.ExceptionInformation
[1] = 0x55667788;
1880 ripevent_exceptions
= 0;
1881 pRtlRaiseException(&record
);
1882 ok(ripevent_exceptions
== numexc
, "RtlRaiseException generated %d exceptions, expected %d\n",
1883 ripevent_exceptions
, numexc
);
1885 pRtlRemoveVectoredExceptionHandler(vectored_handler
);
1888 static DWORD debug_service_exceptions
;
1890 static LONG CALLBACK
debug_service_handler(EXCEPTION_POINTERS
*ExceptionInfo
)
1892 EXCEPTION_RECORD
*rec
= ExceptionInfo
->ExceptionRecord
;
1893 trace("vect. handler %08x addr:%p\n", rec
->ExceptionCode
, rec
->ExceptionAddress
);
1895 ok(rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
, "ExceptionCode is %08x instead of %08x\n",
1896 rec
->ExceptionCode
, EXCEPTION_BREAKPOINT
);
1899 ok(ExceptionInfo
->ContextRecord
->Eip
== (DWORD
)code_mem
+ 0x1c,
1900 "expected Eip = %x, got %x\n", (DWORD
)code_mem
+ 0x1c, ExceptionInfo
->ContextRecord
->Eip
);
1901 ok(rec
->NumberParameters
== (is_wow64
? 1 : 3),
1902 "ExceptionParameters is %d instead of %d\n", rec
->NumberParameters
, is_wow64
? 1 : 3);
1903 ok(rec
->ExceptionInformation
[0] == ExceptionInfo
->ContextRecord
->Eax
,
1904 "expected ExceptionInformation[0] = %x, got %lx\n",
1905 ExceptionInfo
->ContextRecord
->Eax
, rec
->ExceptionInformation
[0]);
1908 ok(rec
->ExceptionInformation
[1] == 0x11111111,
1909 "got ExceptionInformation[1] = %lx\n", rec
->ExceptionInformation
[1]);
1910 ok(rec
->ExceptionInformation
[2] == 0x22222222,
1911 "got ExceptionInformation[2] = %lx\n", rec
->ExceptionInformation
[2]);
1914 ok(ExceptionInfo
->ContextRecord
->Rip
== (DWORD_PTR
)code_mem
+ 0x2f,
1915 "expected Rip = %lx, got %lx\n", (DWORD_PTR
)code_mem
+ 0x2f, ExceptionInfo
->ContextRecord
->Rip
);
1916 ok(rec
->NumberParameters
== 1,
1917 "ExceptionParameters is %d instead of 1\n", rec
->NumberParameters
);
1918 ok(rec
->ExceptionInformation
[0] == ExceptionInfo
->ContextRecord
->Rax
,
1919 "expected ExceptionInformation[0] = %lx, got %lx\n",
1920 ExceptionInfo
->ContextRecord
->Rax
, rec
->ExceptionInformation
[0]);
1923 debug_service_exceptions
++;
1924 return (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) ? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_CONTINUE_SEARCH
;
1929 static const BYTE call_debug_service_code
[] = {
1930 0x53, /* pushl %ebx */
1931 0x57, /* pushl %edi */
1932 0x8b, 0x44, 0x24, 0x0c, /* movl 12(%esp),%eax */
1933 0xb9, 0x11, 0x11, 0x11, 0x11, /* movl $0x11111111,%ecx */
1934 0xba, 0x22, 0x22, 0x22, 0x22, /* movl $0x22222222,%edx */
1935 0xbb, 0x33, 0x33, 0x33, 0x33, /* movl $0x33333333,%ebx */
1936 0xbf, 0x44, 0x44, 0x44, 0x44, /* movl $0x44444444,%edi */
1937 0xcd, 0x2d, /* int $0x2d */
1938 0xeb, /* jmp $+17 */
1939 0x0f, 0x1f, 0x00, /* nop */
1940 0x31, 0xc0, /* xorl %eax,%eax */
1941 0xeb, 0x0c, /* jmp $+14 */
1942 0x90, 0x90, 0x90, 0x90, /* nop */
1943 0x90, 0x90, 0x90, 0x90,
1945 0x31, 0xc0, /* xorl %eax,%eax */
1946 0x40, /* incl %eax */
1947 0x5f, /* popl %edi */
1948 0x5b, /* popl %ebx */
1954 static const BYTE call_debug_service_code
[] = {
1955 0x53, /* push %rbx */
1956 0x57, /* push %rdi */
1957 0x48, 0x89, 0xc8, /* movl %rcx,%rax */
1958 0x48, 0xb9, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, /* movabs $0x1111111111111111,%rcx */
1959 0x48, 0xba, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* movabs $0x2222222222222222,%rdx */
1960 0x48, 0xbb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, /* movabs $0x3333333333333333,%rbx */
1961 0x48, 0xbf, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, /* movabs $0x4444444444444444,%rdi */
1962 0xcd, 0x2d, /* int $0x2d */
1963 0xeb, /* jmp $+17 */
1964 0x0f, 0x1f, 0x00, /* nop */
1965 0x48, 0x31, 0xc0, /* xor %rax,%rax */
1966 0xeb, 0x0e, /* jmp $+16 */
1967 0x90, 0x90, 0x90, 0x90, /* nop */
1968 0x90, 0x90, 0x90, 0x90,
1969 0x48, 0x31, 0xc0, /* xor %rax,%rax */
1970 0x48, 0xff, 0xc0, /* inc %rax */
1971 0x5f, /* pop %rdi */
1972 0x5b, /* pop %rbx */
1978 static void test_debug_service(DWORD numexc
)
1980 DWORD (CDECL
*func
)(DWORD_PTR
) = code_mem
;
1981 DWORD expected_exc
, expected_ret
;
1982 void *vectored_handler
;
1985 /* code will return 0 if execution resumes immediately after "int $0x2d", otherwise 1 */
1986 memcpy(code_mem
, call_debug_service_code
, sizeof(call_debug_service_code
));
1988 vectored_handler
= pRtlAddVectoredExceptionHandler(TRUE
, &debug_service_handler
);
1989 ok(vectored_handler
!= 0, "RtlAddVectoredExceptionHandler failed\n");
1991 expected_exc
= numexc
;
1992 expected_ret
= (numexc
!= 0);
1994 /* BREAKPOINT_BREAK */
1995 debug_service_exceptions
= 0;
1997 ok(debug_service_exceptions
== expected_exc
,
1998 "BREAKPOINT_BREAK generated %u exceptions, expected %u\n",
1999 debug_service_exceptions
, expected_exc
);
2000 ok(ret
== expected_ret
,
2001 "BREAKPOINT_BREAK returned %u, expected %u\n", ret
, expected_ret
);
2003 /* BREAKPOINT_PROMPT */
2004 debug_service_exceptions
= 0;
2006 ok(debug_service_exceptions
== expected_exc
,
2007 "BREAKPOINT_PROMPT generated %u exceptions, expected %u\n",
2008 debug_service_exceptions
, expected_exc
);
2009 ok(ret
== expected_ret
,
2010 "BREAKPOINT_PROMPT returned %u, expected %u\n", ret
, expected_ret
);
2012 /* invalid debug service */
2013 debug_service_exceptions
= 0;
2015 ok(debug_service_exceptions
== expected_exc
,
2016 "invalid debug service generated %u exceptions, expected %u\n",
2017 debug_service_exceptions
, expected_exc
);
2018 ok(ret
== expected_ret
,
2019 "invalid debug service returned %u, expected %u\n", ret
, expected_ret
);
2021 expected_exc
= (is_wow64
? numexc
: 0);
2022 expected_ret
= (is_wow64
&& numexc
);
2024 /* BREAKPOINT_PRINT */
2025 debug_service_exceptions
= 0;
2027 ok(debug_service_exceptions
== expected_exc
,
2028 "BREAKPOINT_PRINT generated %u exceptions, expected %u\n",
2029 debug_service_exceptions
, expected_exc
);
2030 ok(ret
== expected_ret
,
2031 "BREAKPOINT_PRINT returned %u, expected %u\n", ret
, expected_ret
);
2033 /* BREAKPOINT_LOAD_SYMBOLS */
2034 debug_service_exceptions
= 0;
2036 ok(debug_service_exceptions
== expected_exc
,
2037 "BREAKPOINT_LOAD_SYMBOLS generated %u exceptions, expected %u\n",
2038 debug_service_exceptions
, expected_exc
);
2039 ok(ret
== expected_ret
,
2040 "BREAKPOINT_LOAD_SYMBOLS returned %u, expected %u\n", ret
, expected_ret
);
2042 /* BREAKPOINT_UNLOAD_SYMBOLS */
2043 debug_service_exceptions
= 0;
2045 ok(debug_service_exceptions
== expected_exc
,
2046 "BREAKPOINT_UNLOAD_SYMBOLS generated %u exceptions, expected %u\n",
2047 debug_service_exceptions
, expected_exc
);
2048 ok(ret
== expected_ret
,
2049 "BREAKPOINT_UNLOAD_SYMBOLS returned %u, expected %u\n", ret
, expected_ret
);
2051 /* BREAKPOINT_COMMAND_STRING */
2052 debug_service_exceptions
= 0;
2054 ok(debug_service_exceptions
== expected_exc
|| broken(debug_service_exceptions
== numexc
),
2055 "BREAKPOINT_COMMAND_STRING generated %u exceptions, expected %u\n",
2056 debug_service_exceptions
, expected_exc
);
2057 ok(ret
== expected_ret
|| broken(ret
== (numexc
!= 0)),
2058 "BREAKPOINT_COMMAND_STRING returned %u, expected %u\n", ret
, expected_ret
);
2060 pRtlRemoveVectoredExceptionHandler(vectored_handler
);
2063 static DWORD breakpoint_exceptions
;
2065 static LONG CALLBACK
breakpoint_handler(EXCEPTION_POINTERS
*ExceptionInfo
)
2067 EXCEPTION_RECORD
*rec
= ExceptionInfo
->ExceptionRecord
;
2068 trace("vect. handler %08x addr:%p\n", rec
->ExceptionCode
, rec
->ExceptionAddress
);
2070 ok(rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
, "ExceptionCode is %08x instead of %08x\n",
2071 rec
->ExceptionCode
, EXCEPTION_BREAKPOINT
);
2074 ok(ExceptionInfo
->ContextRecord
->Eip
== (DWORD
)code_mem
+ 1,
2075 "expected Eip = %x, got %x\n", (DWORD
)code_mem
+ 1, ExceptionInfo
->ContextRecord
->Eip
);
2076 ok(rec
->NumberParameters
== (is_wow64
? 1 : 3),
2077 "ExceptionParameters is %d instead of %d\n", rec
->NumberParameters
, is_wow64
? 1 : 3);
2078 ok(rec
->ExceptionInformation
[0] == 0,
2079 "got ExceptionInformation[0] = %lx\n", rec
->ExceptionInformation
[0]);
2080 ExceptionInfo
->ContextRecord
->Eip
= (DWORD
)code_mem
+ 2;
2082 ok(ExceptionInfo
->ContextRecord
->Rip
== (DWORD_PTR
)code_mem
+ 1,
2083 "expected Rip = %lx, got %lx\n", (DWORD_PTR
)code_mem
+ 1, ExceptionInfo
->ContextRecord
->Rip
);
2084 ok(rec
->NumberParameters
== 1,
2085 "ExceptionParameters is %d instead of 1\n", rec
->NumberParameters
);
2086 ok(rec
->ExceptionInformation
[0] == 0,
2087 "got ExceptionInformation[0] = %lx\n", rec
->ExceptionInformation
[0]);
2088 ExceptionInfo
->ContextRecord
->Rip
= (DWORD_PTR
)code_mem
+ 2;
2091 breakpoint_exceptions
++;
2092 return (rec
->ExceptionCode
== EXCEPTION_BREAKPOINT
) ? EXCEPTION_CONTINUE_EXECUTION
: EXCEPTION_CONTINUE_SEARCH
;
2095 static const BYTE breakpoint_code
[] = {
2096 0xcd, 0x03, /* int $0x3 */
2100 static void test_breakpoint(DWORD numexc
)
2102 DWORD (CDECL
*func
)(void) = code_mem
;
2103 void *vectored_handler
;
2105 memcpy(code_mem
, breakpoint_code
, sizeof(breakpoint_code
));
2107 vectored_handler
= pRtlAddVectoredExceptionHandler(TRUE
, &breakpoint_handler
);
2108 ok(vectored_handler
!= 0, "RtlAddVectoredExceptionHandler failed\n");
2110 breakpoint_exceptions
= 0;
2112 ok(breakpoint_exceptions
== numexc
, "int $0x3 generated %u exceptions, expected %u\n",
2113 breakpoint_exceptions
, numexc
);
2115 pRtlRemoveVectoredExceptionHandler(vectored_handler
);
2118 static void test_vectored_continue_handler(void)
2120 PVOID handler1
, handler2
;
2123 if (!pRtlAddVectoredContinueHandler
|| !pRtlRemoveVectoredContinueHandler
)
2125 skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
2129 handler1
= pRtlAddVectoredContinueHandler(TRUE
, (void *)0xdeadbeef);
2130 ok(handler1
!= 0, "RtlAddVectoredContinueHandler failed\n");
2132 handler2
= pRtlAddVectoredContinueHandler(TRUE
, (void *)0xdeadbeef);
2133 ok(handler2
!= 0, "RtlAddVectoredContinueHandler failed\n");
2134 ok(handler1
!= handler2
, "RtlAddVectoredContinueHandler returned same handler\n");
2136 if (pRtlRemoveVectoredExceptionHandler
)
2138 ret
= pRtlRemoveVectoredExceptionHandler(handler1
);
2139 ok(!ret
, "RtlRemoveVectoredExceptionHandler succeeded\n");
2142 ret
= pRtlRemoveVectoredContinueHandler(handler1
);
2143 ok(ret
, "RtlRemoveVectoredContinueHandler failed\n");
2145 ret
= pRtlRemoveVectoredContinueHandler(handler2
);
2146 ok(ret
, "RtlRemoveVectoredContinueHandler failed\n");
2148 ret
= pRtlRemoveVectoredContinueHandler(handler1
);
2149 ok(!ret
, "RtlRemoveVectoredContinueHandler succeeded\n");
2151 ret
= pRtlRemoveVectoredContinueHandler((void *)0x11223344);
2152 ok(!ret
, "RtlRemoveVectoredContinueHandler succeeded\n");
2154 #endif /* defined(__i386__) || defined(__x86_64__) */
2156 START_TEST(exception
)
2158 HMODULE hntdll
= GetModuleHandleA("ntdll.dll");
2160 code_mem
= VirtualAlloc(NULL
, 65536, MEM_RESERVE
| MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
2162 trace("VirtualAlloc failed\n");
2166 pNtCurrentTeb
= (void *)GetProcAddress( hntdll
, "NtCurrentTeb" );
2167 pNtGetContextThread
= (void *)GetProcAddress( hntdll
, "NtGetContextThread" );
2168 pNtSetContextThread
= (void *)GetProcAddress( hntdll
, "NtSetContextThread" );
2169 pNtReadVirtualMemory
= (void *)GetProcAddress( hntdll
, "NtReadVirtualMemory" );
2170 pRtlUnwind
= (void *)GetProcAddress( hntdll
, "RtlUnwind" );
2171 pRtlRaiseException
= (void *)GetProcAddress( hntdll
, "RtlRaiseException" );
2172 pNtTerminateProcess
= (void *)GetProcAddress( hntdll
, "NtTerminateProcess" );
2173 pRtlAddVectoredExceptionHandler
= (void *)GetProcAddress( hntdll
,
2174 "RtlAddVectoredExceptionHandler" );
2175 pRtlRemoveVectoredExceptionHandler
= (void *)GetProcAddress( hntdll
,
2176 "RtlRemoveVectoredExceptionHandler" );
2177 pRtlAddVectoredContinueHandler
= (void *)GetProcAddress( hntdll
,
2178 "RtlAddVectoredContinueHandler" );
2179 pRtlRemoveVectoredContinueHandler
= (void *)GetProcAddress( hntdll
,
2180 "RtlRemoveVectoredContinueHandler" );
2181 pNtQueryInformationProcess
= (void*)GetProcAddress( hntdll
,
2182 "NtQueryInformationProcess" );
2183 pNtSetInformationProcess
= (void*)GetProcAddress( hntdll
,
2184 "NtSetInformationProcess" );
2185 pIsWow64Process
= (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
2190 skip( "NtCurrentTeb not found\n" );
2193 if (!pIsWow64Process
|| !pIsWow64Process( GetCurrentProcess(), &is_wow64
)) is_wow64
= FALSE
;
2195 if (pRtlAddVectoredExceptionHandler
&& pRtlRemoveVectoredExceptionHandler
)
2196 have_vectored_api
= TRUE
;
2198 skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
2200 my_argc
= winetest_get_mainargs( &my_argv
);
2204 sscanf( my_argv
[3], "%p", &addr
);
2206 if (addr
!= &test_stage
)
2208 skip( "child process not mapped at same address (%p/%p)\n", &test_stage
, addr
);
2212 /* child must be run under a debugger */
2213 if (!pNtCurrentTeb()->Peb
->BeingDebugged
)
2215 ok(FALSE
, "child process not being debugged?\n");
2219 if (pRtlRaiseException
)
2222 run_rtlraiseexception_test(0x12345);
2223 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT
);
2224 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE
);
2226 run_rtlraiseexception_test(0x12345);
2227 run_rtlraiseexception_test(EXCEPTION_BREAKPOINT
);
2228 run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE
);
2230 test_outputdebugstring(0, FALSE
);
2232 test_outputdebugstring(2, TRUE
); /* is this a Windows bug? */
2238 test_debug_service(0);
2240 test_debug_service(1);
2247 skip( "RtlRaiseException not found\n" );
2249 /* rest of tests only run in parent */
2255 test_rtlraiseexception();
2256 test_debug_registers();
2257 test_outputdebugstring(1, FALSE
);
2259 test_debug_service(1);
2261 test_vectored_continue_handler();
2263 test_simd_exceptions();
2264 test_fpu_exceptions();
2265 test_dpe_exceptions();
2268 #elif defined(__x86_64__)
2269 pRtlAddFunctionTable
= (void *)GetProcAddress( hntdll
,
2270 "RtlAddFunctionTable" );
2271 pRtlDeleteFunctionTable
= (void *)GetProcAddress( hntdll
,
2272 "RtlDeleteFunctionTable" );
2273 pRtlInstallFunctionTableCallback
= (void *)GetProcAddress( hntdll
,
2274 "RtlInstallFunctionTableCallback" );
2275 pRtlLookupFunctionEntry
= (void *)GetProcAddress( hntdll
,
2276 "RtlLookupFunctionEntry" );
2278 test_debug_registers();
2279 test_outputdebugstring(1, FALSE
);
2281 test_debug_service(1);
2283 test_vectored_continue_handler();
2284 test_virtual_unwind();
2286 if (pRtlAddFunctionTable
&& pRtlDeleteFunctionTable
&& pRtlInstallFunctionTableCallback
&& pRtlLookupFunctionEntry
)
2287 test_dynamic_unwind();
2289 skip( "Dynamic unwind functions not found\n" );
2293 VirtualFree(code_mem
, 0, MEM_RELEASE
);