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