#include "winbase.h"
#include "winnt.h"
#include "winreg.h"
-#include "winternl.h"
+#include "wine/winternl.h"
#include "wine/exception.h"
#include "wine/test.h"
static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
+static PVOID (WINAPI *pRtlUnwind)(PVOID, PVOID, PEXCEPTION_RECORD, PVOID);
static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
+static PVOID (WINAPI *pRtlAddVectoredContinueHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
+static ULONG (WINAPI *pRtlRemoveVectoredContinueHandler)(PVOID handler);
static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
+static NTSTATUS (WINAPI *pNtClose)(HANDLE);
#if defined(__x86_64__)
static BOOLEAN (CDECL *pRtlAddFunctionTable)(RUNTIME_FUNCTION*, DWORD, DWORD64);
run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
}
+static DWORD unwind_expected_eax;
+
+static DWORD unwind_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
+ CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
+{
+ trace("exception: %08x flags:%x addr:%p context: Eip:%x\n",
+ rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress, context->Eip);
+
+ ok(rec->ExceptionCode == STATUS_UNWIND, "ExceptionCode is %08x instead of %08x\n",
+ rec->ExceptionCode, STATUS_UNWIND);
+ ok(rec->ExceptionAddress == (char *)code_mem + 0x22, "ExceptionAddress at %p instead of %p\n",
+ rec->ExceptionAddress, (char *)code_mem + 0x22);
+ ok(context->Eax == unwind_expected_eax, "context->Eax is %08x instead of %08x\n",
+ context->Eax, unwind_expected_eax);
+
+ context->Eax += 1;
+ return ExceptionContinueSearch;
+}
+
+static const BYTE call_unwind_code[] = {
+ 0x55, /* push %ebp */
+ 0x53, /* push %ebx */
+ 0x56, /* push %esi */
+ 0x57, /* push %edi */
+ 0xe8, 0x00, 0x00, 0x00, 0x00, /* call 0 */
+ 0x58, /* 0: pop %eax */
+ 0x05, 0x1e, 0x00, 0x00, 0x00, /* add $0x1e,%eax */
+ 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
+ 0xff, 0x74, 0x24, 0x20, /* push 0x20(%esp) */
+ 0x50, /* push %eax */
+ 0xff, 0x74, 0x24, 0x24, /* push 0x24(%esp) */
+ 0x8B, 0x44, 0x24, 0x24, /* mov 0x24(%esp),%eax */
+ 0xff, 0xd0, /* call *%eax */
+ 0x5f, /* pop %edi */
+ 0x5e, /* pop %esi */
+ 0x5b, /* pop %ebx */
+ 0x5d, /* pop %ebp */
+ 0xc3, /* ret */
+ 0xcc, /* int $3 */
+};
+
+static void test_unwind(void)
+{
+ EXCEPTION_REGISTRATION_RECORD frames[2], *frame2 = &frames[0], *frame1 = &frames[1];
+ DWORD (*func)(void* function, EXCEPTION_REGISTRATION_RECORD *pEndFrame, EXCEPTION_RECORD* record, DWORD retval) = code_mem;
+ DWORD retval;
+
+ memcpy(code_mem, call_unwind_code, sizeof(call_unwind_code));
+
+ /* add first unwind handler */
+ frame1->Handler = unwind_handler;
+ frame1->Prev = pNtCurrentTeb()->Tib.ExceptionList;
+ pNtCurrentTeb()->Tib.ExceptionList = frame1;
+
+ /* add second unwind handler */
+ frame2->Handler = unwind_handler;
+ frame2->Prev = pNtCurrentTeb()->Tib.ExceptionList;
+ pNtCurrentTeb()->Tib.ExceptionList = frame2;
+
+ /* test unwind to current frame */
+ unwind_expected_eax = 0xDEAD0000;
+ retval = func(pRtlUnwind, frame2, NULL, 0xDEAD0000);
+ ok(retval == 0xDEAD0000, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0000);
+ ok(pNtCurrentTeb()->Tib.ExceptionList == frame2, "Exception record points to %p instead of %p\n",
+ pNtCurrentTeb()->Tib.ExceptionList, frame2);
+
+ /* unwind to frame1 */
+ unwind_expected_eax = 0xDEAD0000;
+ retval = func(pRtlUnwind, frame1, NULL, 0xDEAD0000);
+ ok(retval == 0xDEAD0001, "RtlUnwind returned eax %08x instead of %08x\n", retval, 0xDEAD0001);
+ ok(pNtCurrentTeb()->Tib.ExceptionList == frame1, "Exception record points to %p instead of %p\n",
+ pNtCurrentTeb()->Tib.ExceptionList, frame1);
+
+ /* restore original handler */
+ pNtCurrentTeb()->Tib.ExceptionList = frame1->Prev;
+}
+
static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
{
/* here we handle exception */
}
}
+ else if (stage == 7 || stage == 8)
+ {
+ ok(de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_INVALID_HANDLE,
+ "unexpected exception code %08x, expected %08x\n", de.u.Exception.ExceptionRecord.ExceptionCode,
+ EXCEPTION_INVALID_HANDLE);
+ ok(de.u.Exception.ExceptionRecord.NumberParameters == 0,
+ "unexpected number of parameters %d, expected 0\n", de.u.Exception.ExceptionRecord.NumberParameters);
+
+ if (stage == 8) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
+ }
else
ok(FALSE, "unexpected stage %x\n", stage);
ok(!status, "NtSetContextThread failed with 0x%x\n", status);
}
}
+ else if (de.dwDebugEventCode == OUTPUT_DEBUG_STRING_EVENT)
+ {
+ int stage;
+#ifdef __REACTOS__
+ /* This will catch our DPRINTs, such as
+ * "WARNING: RtlpDphTargetDllsLogicInitialize at ..\..\lib\rtl\heappage.c:1283 is UNIMPLEMENTED!"
+ * so we need a full-size buffer to avoid a stack overflow
+ */
+ char buffer[513];
+#else
+ char buffer[64];
+#endif
+
+ status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
+ sizeof(stage), &size_read);
+ ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
+
+ ok(!de.u.DebugString.fUnicode, "unexpected unicode debug string event\n");
+ ok(de.u.DebugString.nDebugStringLength < sizeof(buffer) - 1, "buffer not large enough to hold %d bytes\n",
+ de.u.DebugString.nDebugStringLength);
+
+ memset(buffer, 0, sizeof(buffer));
+ status = pNtReadVirtualMemory(pi.hProcess, de.u.DebugString.lpDebugStringData, buffer,
+ de.u.DebugString.nDebugStringLength, &size_read);
+ ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
+
+ if (stage == 3 || stage == 4)
+ ok(!strcmp(buffer, "Hello World"), "got unexpected debug string '%s'\n", buffer);
+ else /* ignore unrelated debug strings like 'SHIMVIEW: ShimInfo(Complete)' */
+ ok(strstr(buffer, "SHIMVIEW") != NULL, "unexpected stage %x, got debug string event '%s'\n", stage, buffer);
+
+ if (stage == 4) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
+ }
+ else if (de.dwDebugEventCode == RIP_EVENT)
+ {
+ int stage;
+
+ status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
+ sizeof(stage), &size_read);
+ ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
+
+ if (stage == 5 || stage == 6)
+ {
+ ok(de.u.RipInfo.dwError == 0x11223344, "got unexpected rip error code %08x, expected %08x\n",
+ de.u.RipInfo.dwError, 0x11223344);
+ ok(de.u.RipInfo.dwType == 0x55667788, "got unexpected rip type %08x, expected %08x\n",
+ de.u.RipInfo.dwType, 0x55667788);
+ }
+ else
+ ok(FALSE, "unexpected stage %x\n", stage);
+
+ if (stage == 6) continuestatus = DBG_EXCEPTION_NOT_HANDLED;
+ }
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 16, &base, NULL );
ok( func == NULL,
"RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
- ok( base == 0xdeadbeef,
- "RtlLookupFunctionEntry modified base address, expected: 0xdeadbeef, got: %lx\n", base );
+ ok( !base || broken(base == 0xdeadbeef),
+ "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
/* Test with pointer inside of our function */
base = 0xdeadbeef;
func = pRtlLookupFunctionEntry( (ULONG_PTR)code_mem + code_offset + 32, &base, NULL );
ok( func == NULL,
"RtlLookupFunctionEntry returned unexpected function, expected: NULL, got: %p\n", func );
- ok( base == 0xdeadbeef,
- "RtlLookupFunctionEntry modified base address, expected: 0xdeadbeef, got: %lx\n", base );
+ ok( !base || broken(base == 0xdeadbeef),
+ "RtlLookupFunctionEntry modified base address, expected: 0, got: %lx\n", base );
ok( !count,
"RtlLookupFunctionEntry issued %d unexpected calls to dynamic_unwind_callback\n", count );
#endif /* __x86_64__ */
+#if defined(__i386__) || defined(__x86_64__)
+static DWORD outputdebugstring_exceptions;
+
+static LONG CALLBACK outputdebugstring_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
+{
+ PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
+ trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
+
+ ok(rec->ExceptionCode == DBG_PRINTEXCEPTION_C, "ExceptionCode is %08x instead of %08x\n",
+ rec->ExceptionCode, DBG_PRINTEXCEPTION_C);
+ ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
+ ok(rec->ExceptionInformation[0] == 12, "ExceptionInformation[0] = %d instead of 12\n", (DWORD)rec->ExceptionInformation[0]);
+ ok(!strcmp((char *)rec->ExceptionInformation[1], "Hello World"),
+ "ExceptionInformation[1] = '%s' instead of 'Hello World'\n", (char *)rec->ExceptionInformation[1]);
+
+ outputdebugstring_exceptions++;
+ return EXCEPTION_CONTINUE_SEARCH;
+}
+
+static void test_outputdebugstring(DWORD numexc)
+{
+ PVOID vectored_handler;
+
+ if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler)
+ {
+ skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
+ return;
+ }
+
+ vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &outputdebugstring_vectored_handler);
+ ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
+
+ outputdebugstring_exceptions = 0;
+ OutputDebugStringA("Hello World");
+ ok(outputdebugstring_exceptions == numexc, "OutputDebugStringA generated %d exceptions, expected %d\n",
+ outputdebugstring_exceptions, numexc);
+
+ pRtlRemoveVectoredExceptionHandler(vectored_handler);
+}
+
+static DWORD ripevent_exceptions;
+
+static LONG CALLBACK ripevent_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
+{
+ PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
+ trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
+
+ ok(rec->ExceptionCode == DBG_RIPEXCEPTION, "ExceptionCode is %08x instead of %08x\n",
+ rec->ExceptionCode, DBG_RIPEXCEPTION);
+ ok(rec->NumberParameters == 2, "ExceptionParameters is %d instead of 2\n", rec->NumberParameters);
+ ok(rec->ExceptionInformation[0] == 0x11223344, "ExceptionInformation[0] = %08x instead of %08x\n",
+ (NTSTATUS)rec->ExceptionInformation[0], 0x11223344);
+ ok(rec->ExceptionInformation[1] == 0x55667788, "ExceptionInformation[1] = %08x instead of %08x\n",
+ (NTSTATUS)rec->ExceptionInformation[1], 0x55667788);
+
+ ripevent_exceptions++;
+ return (rec->ExceptionCode == DBG_RIPEXCEPTION) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
+}
+
+static void test_ripevent(DWORD numexc)
+{
+ EXCEPTION_RECORD record;
+ PVOID vectored_handler;
+
+ if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
+ {
+ skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
+ return;
+ }
+
+ vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &ripevent_vectored_handler);
+ ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
+
+ record.ExceptionCode = DBG_RIPEXCEPTION;
+ record.ExceptionFlags = 0;
+ record.ExceptionRecord = NULL;
+ record.ExceptionAddress = NULL;
+ record.NumberParameters = 2;
+ record.ExceptionInformation[0] = 0x11223344;
+ record.ExceptionInformation[1] = 0x55667788;
+
+ ripevent_exceptions = 0;
+ pRtlRaiseException(&record);
+ ok(ripevent_exceptions == numexc, "RtlRaiseException generated %d exceptions, expected %d\n",
+ ripevent_exceptions, numexc);
+
+ pRtlRemoveVectoredExceptionHandler(vectored_handler);
+}
+
+static DWORD invalid_handle_exceptions;
+
+static LONG CALLBACK invalid_handle_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
+{
+ PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
+ trace("vect. handler %08x addr:%p\n", rec->ExceptionCode, rec->ExceptionAddress);
+
+ ok(rec->ExceptionCode == EXCEPTION_INVALID_HANDLE, "ExceptionCode is %08x instead of %08x\n",
+ rec->ExceptionCode, EXCEPTION_INVALID_HANDLE);
+ ok(rec->NumberParameters == 0, "ExceptionParameters is %d instead of 0\n", rec->NumberParameters);
+
+ invalid_handle_exceptions++;
+ return (rec->ExceptionCode == EXCEPTION_INVALID_HANDLE) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH;
+}
+
+static void test_closehandle(DWORD numexc)
+{
+ PVOID vectored_handler;
+ NTSTATUS status;
+ DWORD res;
+
+ if (!pRtlAddVectoredExceptionHandler || !pRtlRemoveVectoredExceptionHandler || !pRtlRaiseException)
+ {
+ skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler or RtlRaiseException not found\n");
+ return;
+ }
+
+ vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &invalid_handle_vectored_handler);
+ ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
+
+ invalid_handle_exceptions = 0;
+ res = CloseHandle((HANDLE)0xdeadbeef);
+ ok(!res, "CloseHandle(0xdeadbeef) unexpectedly succeeded\n");
+ ok(GetLastError() == ERROR_INVALID_HANDLE, "wrong error code %d instead of %d\n",
+ GetLastError(), ERROR_INVALID_HANDLE);
+ ok(invalid_handle_exceptions == numexc, "CloseHandle generated %d exceptions, expected %d\n",
+ invalid_handle_exceptions, numexc);
+
+ invalid_handle_exceptions = 0;
+ status = pNtClose((HANDLE)0xdeadbeef);
+ ok(status == STATUS_INVALID_HANDLE, "NtClose(0xdeadbeef) returned status %08x\n", status);
+ ok(invalid_handle_exceptions == numexc, "NtClose generated %d exceptions, expected %d\n",
+ invalid_handle_exceptions, numexc);
+
+ pRtlRemoveVectoredExceptionHandler(vectored_handler);
+}
+
+static void test_vectored_continue_handler(void)
+{
+ PVOID handler1, handler2;
+ ULONG ret;
+
+ if (!pRtlAddVectoredContinueHandler || !pRtlRemoveVectoredContinueHandler)
+ {
+ skip("RtlAddVectoredContinueHandler or RtlRemoveVectoredContinueHandler not found\n");
+ return;
+ }
+
+ handler1 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
+ ok(handler1 != 0, "RtlAddVectoredContinueHandler failed\n");
+
+ handler2 = pRtlAddVectoredContinueHandler(TRUE, (void *)0xdeadbeef);
+ ok(handler2 != 0, "RtlAddVectoredContinueHandler failed\n");
+ ok(handler1 != handler2, "RtlAddVectoredContinueHandler returned same handler\n");
+
+ if (pRtlRemoveVectoredExceptionHandler)
+ {
+ ret = pRtlRemoveVectoredExceptionHandler(handler1);
+ ok(!ret, "RtlRemoveVectoredExceptionHandler succeeded\n");
+ }
+
+ ret = pRtlRemoveVectoredContinueHandler(handler1);
+ ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
+
+ ret = pRtlRemoveVectoredContinueHandler(handler2);
+ ok(ret, "RtlRemoveVectoredContinueHandler failed\n");
+
+ ret = pRtlRemoveVectoredContinueHandler(handler1);
+ ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
+
+ ret = pRtlRemoveVectoredContinueHandler((void *)0x11223344);
+ ok(!ret, "RtlRemoveVectoredContinueHandler succeeded\n");
+}
+#endif /* defined(__i386__) || defined(__x86_64__) */
+
START_TEST(exception)
{
HMODULE hntdll = GetModuleHandleA("ntdll.dll");
pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
+ pNtClose = (void *)GetProcAddress( hntdll, "NtClose" );
+ pRtlUnwind = (void *)GetProcAddress( hntdll, "RtlUnwind" );
pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
"RtlAddVectoredExceptionHandler" );
pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
"RtlRemoveVectoredExceptionHandler" );
+ pRtlAddVectoredContinueHandler = (void *)GetProcAddress( hntdll,
+ "RtlAddVectoredContinueHandler" );
+ pRtlRemoveVectoredContinueHandler = (void *)GetProcAddress( hntdll,
+ "RtlRemoveVectoredContinueHandler" );
pNtQueryInformationProcess = (void*)GetProcAddress( hntdll,
"NtQueryInformationProcess" );
pNtSetInformationProcess = (void*)GetProcAddress( hntdll,
run_rtlraiseexception_test(0x12345);
run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
+ test_stage = 3;
+ test_outputdebugstring(0);
+ test_stage = 4;
+ test_outputdebugstring(2);
+ test_stage = 5;
+ test_ripevent(0);
+ test_stage = 6;
+ test_ripevent(1);
+ test_stage = 7;
+ test_closehandle(0);
+ test_stage = 8;
+ test_closehandle(1);
}
else
skip( "RtlRaiseException not found\n" );
return;
}
- test_prot_fault();
+ test_unwind();
test_exceptions();
test_rtlraiseexception();
+ test_outputdebugstring(1);
+ test_ripevent(1);
+ test_closehandle(0);
+ test_vectored_continue_handler();
test_debugger();
test_simd_exceptions();
test_fpu_exceptions();
test_dpe_exceptions();
+ test_prot_fault();
#elif defined(__x86_64__)
pRtlAddFunctionTable = (void *)GetProcAddress( hntdll,
pRtlLookupFunctionEntry = (void *)GetProcAddress( hntdll,
"RtlLookupFunctionEntry" );
+ test_outputdebugstring(1);
+ test_ripevent(1);
+ test_closehandle(0);
+ test_vectored_continue_handler();
test_virtual_unwind();
if (pRtlAddFunctionTable && pRtlDeleteFunctionTable && pRtlInstallFunctionTableCallback && pRtlLookupFunctionEntry)