2005-12-16 16:27:39 +01:00
|
|
|
/*
|
|
|
|
* Unit test suite for ntdll exceptions
|
|
|
|
*
|
|
|
|
* Copyright 2005 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-12-16 16:27:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2007-02-26 23:42:09 +01:00
|
|
|
#include <stdio.h>
|
2005-12-16 16:27:39 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32_WINNT
|
|
|
|
#define _WIN32_WINNT 0x500 /* For NTSTATUS */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnt.h"
|
|
|
|
#include "winreg.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "excpt.h"
|
|
|
|
#include "wine/test.h"
|
|
|
|
|
|
|
|
#ifdef __i386__
|
2007-02-26 23:42:09 +01:00
|
|
|
static int my_argc;
|
|
|
|
static char** my_argv;
|
2007-02-26 23:42:12 +01:00
|
|
|
static int test_stage;
|
2005-12-16 16:27:39 +01:00
|
|
|
|
2005-12-31 13:24:02 +01:00
|
|
|
static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
|
2006-09-30 15:09:55 +02:00
|
|
|
static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
|
2006-11-30 17:54:05 +01:00
|
|
|
static NTSTATUS (WINAPI *pNtSetContextThread)(HANDLE,CONTEXT*);
|
2007-02-12 21:22:37 +01:00
|
|
|
static NTSTATUS (WINAPI *pRtlRaiseException)(EXCEPTION_RECORD *rec);
|
2007-02-15 19:45:51 +01:00
|
|
|
static PVOID (WINAPI *pRtlAddVectoredExceptionHandler)(ULONG first, PVECTORED_EXCEPTION_HANDLER func);
|
|
|
|
static ULONG (WINAPI *pRtlRemoveVectoredExceptionHandler)(PVOID handler);
|
2007-02-26 23:42:09 +01:00
|
|
|
static NTSTATUS (WINAPI *pNtReadVirtualMemory)(HANDLE, const void*, void*, SIZE_T, SIZE_T*);
|
|
|
|
static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE handle, LONG exit_code);
|
2006-11-30 17:53:48 +01:00
|
|
|
static void *code_mem;
|
2005-12-31 13:24:02 +01:00
|
|
|
|
2005-12-16 16:27:39 +01:00
|
|
|
/* Test various instruction combinations that cause a protection fault on the i386,
|
|
|
|
* and check what the resulting exception looks like.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct exception
|
|
|
|
{
|
|
|
|
BYTE code[18]; /* asm code */
|
|
|
|
BYTE offset; /* offset of faulting instruction */
|
|
|
|
BYTE length; /* length of faulting instruction */
|
|
|
|
NTSTATUS status; /* expected status code */
|
|
|
|
DWORD nb_params; /* expected number of parameters */
|
|
|
|
DWORD params[4]; /* expected parameters */
|
|
|
|
} exceptions[] =
|
|
|
|
{
|
|
|
|
/* test some privileged instructions */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xfb, 0xc3 }, /* 0: sti; ret */
|
2005-12-31 13:24:02 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x6c, 0xc3 }, /* 1: insb (%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x6d, 0xc3 }, /* 2: insl (%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x6e, 0xc3 }, /* 3: outsb (%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x6f, 0xc3 }, /* 4: outsl (%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xe4, 0x11, 0xc3 }, /* 5: inb $0x11,%al; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xe5, 0x11, 0xc3 }, /* 6: inl $0x11,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xe6, 0x11, 0xc3 }, /* 7: outb %al,$0x11; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xe7, 0x11, 0xc3 }, /* 8: outl %eax,$0x11; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xed, 0xc3 }, /* 9: inl (%dx),%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xee, 0xc3 }, /* 10: outb %al,(%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xef, 0xc3 }, /* 11: outl %eax,(%dx); ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xf4, 0xc3 }, /* 12: hlt; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xfa, 0xc3 }, /* 13: cli; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
|
|
|
|
|
|
|
/* test long jump to invalid selector */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* 14: ljmp $0,$0; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 7, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
|
|
|
/* test iret to invalid selector */
|
|
|
|
{ { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
|
2008-09-16 22:35:48 +02:00
|
|
|
/* 15: pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
6, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
|
|
|
/* test loading an invalid selector */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* 16: mov $beef,%ax; mov %ax,%gs; ret */
|
2008-05-07 19:41:52 +02:00
|
|
|
5, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } }, /* 0xbee8 or 0xffffffff */
|
2005-12-16 16:27:39 +01:00
|
|
|
|
|
|
|
/* test accessing a zero selector */
|
|
|
|
{ { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
|
2008-09-16 22:35:48 +02:00
|
|
|
/* 17: push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
5, 6, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
|
|
|
/* test moving %cs -> %ss */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0e, 0x17, 0x58, 0xc3 }, /* 18: pushl %cs; popl %ss; popl %eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
1, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
2008-09-16 22:35:48 +02:00
|
|
|
/* 19: test overlong instruction (limit is 16 bytes) */
|
2005-12-16 16:27:39 +01:00
|
|
|
{ { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
|
2007-03-14 12:02:43 +01:00
|
|
|
0, 16, STATUS_ILLEGAL_INSTRUCTION, 0 },
|
2005-12-16 16:27:39 +01:00
|
|
|
{ { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
|
|
|
|
0, 15, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
|
|
|
|
|
|
|
/* test invalid interrupt */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xcd, 0xff, 0xc3 }, /* 21: int $0xff; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
|
|
|
/* test moves to/from Crx */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x20, 0xc0, 0xc3 }, /* 22: movl %cr0,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x20, 0xe0, 0xc3 }, /* 23: movl %cr4,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x22, 0xc0, 0xc3 }, /* 24: movl %eax,%cr0; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x22, 0xe0, 0xc3 }, /* 25: movl %eax,%cr4; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
|
|
|
|
|
|
|
/* test moves to/from Drx */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x21, 0xc0, 0xc3 }, /* 26: movl %dr0,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x21, 0xc8, 0xc3 }, /* 27: movl %dr1,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x21, 0xf8, 0xc3 }, /* 28: movl %dr7,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x23, 0xc0, 0xc3 }, /* 29: movl %eax,%dr0; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x23, 0xc8, 0xc3 }, /* 30: movl %eax,%dr1; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0x0f, 0x23, 0xf8, 0xc3 }, /* 31: movl %eax,%dr7; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
|
|
|
|
|
|
|
/* test memory reads */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* 32: movl 0xfffffffc,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* 33: movl 0xfffffffd,%eax; ret */
|
2007-01-10 11:58:16 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffd } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* 34: movl 0xfffffffe,%eax; ret */
|
2007-01-10 11:58:16 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffe } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* 35: movl 0xffffffff,%eax; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
|
|
|
|
|
|
|
|
/* test memory writes */
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* 36: movl %eax,0xfffffffc; ret */
|
2005-12-16 16:27:39 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* 37: movl %eax,0xfffffffd; ret */
|
2007-01-10 11:58:16 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffd } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* 38: movl %eax,0xfffffffe; ret */
|
2007-01-10 11:58:16 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffe } },
|
2008-09-16 22:35:48 +02:00
|
|
|
{ { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* 39: movl %eax,0xffffffff; ret */
|
2007-01-10 11:58:16 +01:00
|
|
|
0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xffffffff } },
|
2008-07-01 21:53:09 +02:00
|
|
|
|
2008-09-16 22:35:48 +02:00
|
|
|
/* 40: test exception with cleared %ds and %es */
|
2008-07-01 21:53:09 +02:00
|
|
|
{ { 0x1e, 0x06, 0x31, 0xc0, 0x8e, 0xd8, 0x8e, 0xc0, 0xfa, 0x07, 0x1f, 0xc3 },
|
|
|
|
/* push %ds; push %es; xorl %eax,%eax; mov %ax,%ds; mov %ax,%es; cli; pop %es; pop %ds; ret */
|
|
|
|
8, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
|
2005-12-16 16:27:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int got_exception;
|
2007-02-15 19:45:51 +01:00
|
|
|
static BOOL have_vectored_api;
|
2005-12-16 16:27:39 +01:00
|
|
|
|
2007-05-28 01:13:24 +02:00
|
|
|
static void run_exception_test(void *handler, const void* context,
|
2006-11-30 17:53:48 +01:00
|
|
|
const void *code, unsigned int code_size)
|
2006-11-30 17:52:50 +01:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
EXCEPTION_REGISTRATION_RECORD frame;
|
|
|
|
const void *context;
|
|
|
|
} exc_frame;
|
2006-11-30 17:53:48 +01:00
|
|
|
void (*func)(void) = code_mem;
|
2006-11-30 17:52:50 +01:00
|
|
|
|
|
|
|
exc_frame.frame.Handler = handler;
|
|
|
|
exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
|
|
|
|
exc_frame.context = context;
|
|
|
|
|
2006-11-30 17:53:48 +01:00
|
|
|
memcpy(code_mem, code, code_size);
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
|
|
|
|
func();
|
|
|
|
pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
|
|
|
|
}
|
|
|
|
|
2008-12-12 10:21:38 +01:00
|
|
|
static LONG CALLBACK rtlraiseexception_vectored_handler(EXCEPTION_POINTERS *ExceptionInfo)
|
2007-02-15 19:45:51 +01:00
|
|
|
{
|
|
|
|
PCONTEXT context = ExceptionInfo->ContextRecord;
|
|
|
|
PEXCEPTION_RECORD rec = ExceptionInfo->ExceptionRecord;
|
|
|
|
trace("vect. handler %08x addr:%p context.Eip:%x\n", rec->ExceptionCode,
|
|
|
|
rec->ExceptionAddress, context->Eip);
|
|
|
|
|
|
|
|
todo_wine {
|
|
|
|
ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
|
|
|
|
rec->ExceptionAddress, (char *)code_mem + 0xb);
|
2007-02-26 23:42:09 +01:00
|
|
|
|
|
|
|
if (pNtCurrentTeb()->Peb->BeingDebugged)
|
|
|
|
ok((void *)context->Eax == pRtlRaiseException, "debugger managed to modify Eax to %x should be %p\n",
|
|
|
|
context->Eax, pRtlRaiseException);
|
2007-02-15 19:45:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
|
|
|
|
* even if raised by RtlRaiseException
|
|
|
|
*/
|
|
|
|
if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
|
|
|
|
{
|
2008-05-14 21:55:00 +02:00
|
|
|
ok(context->Eip == (DWORD)code_mem + 0xa ||
|
2008-07-01 21:53:09 +02:00
|
|
|
broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
|
2008-05-14 21:55:00 +02:00
|
|
|
"Eip at %x instead of %x or %x\n", context->Eip,
|
|
|
|
(DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
|
2007-02-15 19:45:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem + 0xb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test if context change is preserved from vectored handler to stack handlers */
|
|
|
|
context->Eax = 0xf00f00f0;
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
2007-02-12 21:22:37 +01:00
|
|
|
static DWORD rtlraiseexception_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 );
|
|
|
|
|
|
|
|
todo_wine {
|
|
|
|
ok(rec->ExceptionAddress == (char *)code_mem + 0xb, "ExceptionAddress at %p instead of %p\n",
|
|
|
|
rec->ExceptionAddress, (char *)code_mem + 0xb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check that context.Eip is fixed up only for EXCEPTION_BREAKPOINT
|
|
|
|
* even if raised by RtlRaiseException
|
|
|
|
*/
|
|
|
|
if(rec->ExceptionCode == EXCEPTION_BREAKPOINT)
|
|
|
|
{
|
2008-05-14 21:55:00 +02:00
|
|
|
ok(context->Eip == (DWORD)code_mem + 0xa ||
|
2008-07-01 21:53:09 +02:00
|
|
|
broken(context->Eip == (DWORD)code_mem + 0xb), /* win2k3 */
|
2008-05-14 21:55:00 +02:00
|
|
|
"Eip at %x instead of %x or %x\n", context->Eip,
|
|
|
|
(DWORD)code_mem + 0xa, (DWORD)code_mem + 0xb);
|
2007-02-12 21:22:37 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ok(context->Eip == (DWORD)code_mem + 0xb, "Eip at %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem + 0xb);
|
|
|
|
}
|
|
|
|
|
2007-02-15 19:45:51 +01:00
|
|
|
if(have_vectored_api)
|
|
|
|
ok(context->Eax == 0xf00f00f0, "Eax is %x, should have been set to 0xf00f00f0 in vectored handler\n",
|
|
|
|
context->Eax);
|
|
|
|
|
2007-02-26 23:42:12 +01:00
|
|
|
/* give the debugger a chance to examine the state a second time */
|
|
|
|
/* without the exception handler changing Eip */
|
|
|
|
if (test_stage == 2)
|
|
|
|
return ExceptionContinueSearch;
|
|
|
|
|
2007-02-12 21:22:37 +01:00
|
|
|
/* Eip in context is decreased by 1
|
|
|
|
* Increase it again, else execution will continue in the middle of a instruction */
|
|
|
|
if(rec->ExceptionCode == EXCEPTION_BREAKPOINT && (context->Eip == (DWORD)code_mem + 0xa))
|
|
|
|
context->Eip += 1;
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const BYTE call_one_arg_code[] = {
|
|
|
|
0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
|
|
|
|
0x50, /* push %eax */
|
|
|
|
0x8b, 0x44, 0x24, 0x08, /* mov 0x8(%esp),%eax */
|
|
|
|
0xff, 0xd0, /* call *%eax */
|
|
|
|
0x90, /* nop */
|
|
|
|
0x90, /* nop */
|
|
|
|
0x90, /* nop */
|
|
|
|
0x90, /* nop */
|
|
|
|
0xc3, /* ret */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void run_rtlraiseexception_test(DWORD exceptioncode)
|
|
|
|
{
|
|
|
|
EXCEPTION_REGISTRATION_RECORD frame;
|
|
|
|
EXCEPTION_RECORD record;
|
2007-02-15 19:45:51 +01:00
|
|
|
PVOID vectored_handler = NULL;
|
2007-02-12 21:22:37 +01:00
|
|
|
|
|
|
|
void (*func)(void* function, EXCEPTION_RECORD* record) = code_mem;
|
|
|
|
|
|
|
|
record.ExceptionCode = exceptioncode;
|
|
|
|
record.ExceptionFlags = 0;
|
|
|
|
record.ExceptionRecord = NULL;
|
|
|
|
record.ExceptionAddress = NULL; /* does not matter, copied return address */
|
|
|
|
record.NumberParameters = 0;
|
|
|
|
|
|
|
|
frame.Handler = rtlraiseexception_handler;
|
|
|
|
frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
|
|
|
|
|
|
|
|
memcpy(code_mem, call_one_arg_code, sizeof(call_one_arg_code));
|
|
|
|
|
|
|
|
pNtCurrentTeb()->Tib.ExceptionList = &frame;
|
2007-02-15 19:45:51 +01:00
|
|
|
if (have_vectored_api)
|
|
|
|
{
|
|
|
|
vectored_handler = pRtlAddVectoredExceptionHandler(TRUE, &rtlraiseexception_vectored_handler);
|
|
|
|
ok(vectored_handler != 0, "RtlAddVectoredExceptionHandler failed\n");
|
|
|
|
}
|
|
|
|
|
2007-02-12 21:22:37 +01:00
|
|
|
func(pRtlRaiseException, &record);
|
2007-02-15 19:45:51 +01:00
|
|
|
|
|
|
|
if (have_vectored_api)
|
|
|
|
pRtlRemoveVectoredExceptionHandler(vectored_handler);
|
2007-02-12 21:22:37 +01:00
|
|
|
pNtCurrentTeb()->Tib.ExceptionList = frame.Prev;
|
|
|
|
}
|
|
|
|
|
2007-03-14 12:01:12 +01:00
|
|
|
static void test_rtlraiseexception(void)
|
2007-02-26 23:42:09 +01:00
|
|
|
{
|
|
|
|
if (!pRtlRaiseException)
|
|
|
|
{
|
|
|
|
skip("RtlRaiseException not found\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test without debugger */
|
|
|
|
run_rtlraiseexception_test(0x12345);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
|
|
|
|
}
|
2007-02-12 21:22:37 +01:00
|
|
|
|
2005-12-16 16:27:39 +01:00
|
|
|
static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
const struct exception *except = *(const struct exception **)(frame + 1);
|
|
|
|
unsigned int i, entry = except - exceptions;
|
|
|
|
|
|
|
|
got_exception++;
|
2006-10-13 23:33:02 +02:00
|
|
|
trace( "exception: %x flags:%x addr:%p\n",
|
2005-12-16 16:27:39 +01:00
|
|
|
rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
|
|
|
|
|
|
|
|
ok( rec->ExceptionCode == except->status,
|
2006-10-13 23:33:02 +02:00
|
|
|
"%u: Wrong exception code %x/%x\n", entry, rec->ExceptionCode, except->status );
|
2006-11-30 17:53:48 +01:00
|
|
|
ok( rec->ExceptionAddress == (char*)code_mem + except->offset,
|
2005-12-16 16:27:39 +01:00
|
|
|
"%u: Wrong exception address %p/%p\n", entry,
|
2006-11-30 17:53:48 +01:00
|
|
|
rec->ExceptionAddress, (char*)code_mem + except->offset );
|
2005-12-16 16:27:39 +01:00
|
|
|
|
|
|
|
ok( rec->NumberParameters == except->nb_params,
|
2006-10-13 23:33:02 +02:00
|
|
|
"%u: Wrong number of parameters %u/%u\n", entry, rec->NumberParameters, except->nb_params );
|
2007-01-10 11:58:16 +01:00
|
|
|
|
|
|
|
/* Most CPUs (except Intel Core apparently) report a segment limit violation */
|
|
|
|
/* instead of page faults for accesses beyond 0xffffffff */
|
|
|
|
if (except->nb_params == 2 && except->params[1] >= 0xfffffffd)
|
|
|
|
{
|
|
|
|
if (rec->ExceptionInformation[0] == 0 && rec->ExceptionInformation[1] == 0xffffffff)
|
|
|
|
goto skip_params;
|
|
|
|
}
|
|
|
|
|
2008-05-07 19:41:52 +02:00
|
|
|
/* Seems that both 0xbee8 and 0xfffffffff can be returned in windows */
|
|
|
|
if (except->nb_params == 2 && rec->NumberParameters == 2
|
|
|
|
&& except->params[1] == 0xbee8 && rec->ExceptionInformation[1] == 0xffffffff
|
|
|
|
&& except->params[0] == rec->ExceptionInformation[0])
|
|
|
|
{
|
|
|
|
goto skip_params;
|
|
|
|
}
|
|
|
|
|
2005-12-16 16:27:39 +01:00
|
|
|
for (i = 0; i < rec->NumberParameters; i++)
|
|
|
|
ok( rec->ExceptionInformation[i] == except->params[i],
|
2006-10-13 23:33:02 +02:00
|
|
|
"%u: Wrong parameter %d: %lx/%x\n",
|
2005-12-16 16:27:39 +01:00
|
|
|
entry, i, rec->ExceptionInformation[i], except->params[i] );
|
|
|
|
|
2007-01-10 11:58:16 +01:00
|
|
|
skip_params:
|
2005-12-16 16:27:39 +01:00
|
|
|
/* don't handle exception if it's not the address we expected */
|
2006-11-30 17:53:48 +01:00
|
|
|
if (rec->ExceptionAddress != (char*)code_mem + except->offset) return ExceptionContinueSearch;
|
2005-12-16 16:27:39 +01:00
|
|
|
|
|
|
|
context->Eip += except->length;
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_prot_fault(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
|
|
|
|
{
|
|
|
|
got_exception = 0;
|
2006-11-30 17:53:48 +01:00
|
|
|
run_exception_test(handler, &exceptions[i], &exceptions[i].code,
|
|
|
|
sizeof(exceptions[i].code));
|
2005-12-31 13:24:02 +01:00
|
|
|
if (!i && !got_exception)
|
|
|
|
{
|
|
|
|
trace( "No exception, assuming win9x, no point in testing further\n" );
|
|
|
|
break;
|
|
|
|
}
|
2005-12-16 16:27:39 +01:00
|
|
|
ok( got_exception == (exceptions[i].status != 0),
|
|
|
|
"%u: bad exception count %d\n", i, got_exception );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
/* test handling of debug registers */
|
2006-09-30 15:09:55 +02:00
|
|
|
static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
context->Eip += 2; /* Skips the popl (%eax) */
|
2006-10-03 17:19:36 +02:00
|
|
|
context->Dr0 = 0x42424242;
|
2006-09-30 15:09:55 +02:00
|
|
|
context->Dr1 = 0;
|
|
|
|
context->Dr2 = 0;
|
|
|
|
context->Dr3 = 0;
|
|
|
|
context->Dr6 = 0;
|
|
|
|
context->Dr7 = 0x155;
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
static const BYTE segfault_code[5] = {
|
2006-09-30 15:09:55 +02:00
|
|
|
0x31, 0xc0, /* xor %eax,%eax */
|
|
|
|
0x8f, 0x00, /* popl (%eax) - cause exception */
|
|
|
|
0xc3 /* ret */
|
|
|
|
};
|
|
|
|
|
2006-10-03 17:19:36 +02:00
|
|
|
/* test the single step exception behaviour */
|
|
|
|
static DWORD single_step_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
2006-11-30 17:52:50 +01:00
|
|
|
got_exception++;
|
2006-10-03 17:19:36 +02:00
|
|
|
ok (!(context->EFlags & 0x100), "eflags has single stepping bit set\n");
|
2006-11-30 17:54:05 +01:00
|
|
|
|
|
|
|
if( got_exception < 3)
|
|
|
|
context->EFlags |= 0x100; /* single step until popf instruction */
|
|
|
|
else {
|
|
|
|
/* show that the last single step exception on the popf instruction
|
|
|
|
* (which removed the TF bit), still is a EXCEPTION_SINGLE_STEP exception */
|
2006-12-03 20:30:06 +01:00
|
|
|
ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
|
|
|
|
"exception is not EXCEPTION_SINGLE_STEP: %x\n", rec->ExceptionCode);
|
2006-11-30 17:54:05 +01:00
|
|
|
}
|
|
|
|
|
2006-10-03 17:19:36 +02:00
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
static const BYTE single_stepcode[] = {
|
2006-10-03 17:19:36 +02:00
|
|
|
0x9c, /* pushf */
|
|
|
|
0x58, /* pop %eax */
|
|
|
|
0x0d,0,1,0,0, /* or $0x100,%eax */
|
|
|
|
0x50, /* push %eax */
|
|
|
|
0x9d, /* popf */
|
|
|
|
0x35,0,1,0,0, /* xor $0x100,%eax */
|
|
|
|
0x50, /* push %eax */
|
|
|
|
0x9d, /* popf */
|
|
|
|
0xc3
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Test the alignment check (AC) flag handling. */
|
2006-11-30 17:52:50 +01:00
|
|
|
static const BYTE align_check_code[] = {
|
2006-10-03 17:19:36 +02:00
|
|
|
0x55, /* push %ebp */
|
|
|
|
0x89,0xe5, /* mov %esp,%ebp */
|
|
|
|
0x9c, /* pushf */
|
|
|
|
0x58, /* pop %eax */
|
|
|
|
0x0d,0,0,4,0, /* or $0x40000,%eax */
|
|
|
|
0x50, /* push %eax */
|
|
|
|
0x9d, /* popf */
|
2006-11-30 17:52:50 +01:00
|
|
|
0x89,0xe0, /* mov %esp, %eax */
|
|
|
|
0x8b,0x40,0x1, /* mov 0x1(%eax), %eax - cause exception */
|
2006-10-03 17:19:36 +02:00
|
|
|
0x9c, /* pushf */
|
|
|
|
0x58, /* pop %eax */
|
|
|
|
0x35,0,0,4,0, /* xor $0x40000,%eax */
|
|
|
|
0x50, /* push %eax */
|
|
|
|
0x9d, /* popf */
|
|
|
|
0x5d, /* pop %ebp */
|
|
|
|
0xc3, /* ret */
|
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD align_check_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
ok (!(context->EFlags & 0x40000), "eflags has AC bit set\n");
|
2006-11-30 17:52:50 +01:00
|
|
|
got_exception++;
|
2006-10-03 17:19:36 +02:00
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
2008-03-20 12:24:54 +01:00
|
|
|
/* Test the direction flag handling. */
|
|
|
|
static const BYTE direction_flag_code[] = {
|
|
|
|
0x55, /* push %ebp */
|
|
|
|
0x89,0xe5, /* mov %esp,%ebp */
|
|
|
|
0xfd, /* std */
|
|
|
|
0xfa, /* cli - cause exception */
|
|
|
|
0x5d, /* pop %ebp */
|
|
|
|
0xc3, /* ret */
|
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD direction_flag_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
#ifdef __GNUC__
|
|
|
|
unsigned int flags;
|
2008-09-16 22:35:48 +02:00
|
|
|
__asm__("pushfl; popl %0; cld" : "=r" (flags) );
|
2008-05-07 19:41:52 +02:00
|
|
|
/* older windows versions don't clear DF properly so don't test */
|
|
|
|
if (flags & 0x400) trace( "eflags has DF bit set\n" );
|
2008-03-20 12:24:54 +01:00
|
|
|
#endif
|
|
|
|
ok( context->EFlags & 0x400, "context eflags has DF bit cleared\n" );
|
|
|
|
got_exception++;
|
|
|
|
context->Eip++; /* skip cli */
|
2008-05-19 23:19:24 +02:00
|
|
|
context->EFlags &= ~0x400; /* make sure it is cleared on return */
|
2008-03-20 12:24:54 +01:00
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
2006-11-30 17:54:05 +01:00
|
|
|
/* test single stepping over hardware breakpoint */
|
|
|
|
static DWORD bpx_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
got_exception++;
|
|
|
|
ok( rec->ExceptionCode == EXCEPTION_SINGLE_STEP,
|
|
|
|
"wrong exception code: %x\n", rec->ExceptionCode);
|
|
|
|
|
|
|
|
if(got_exception == 1) {
|
|
|
|
/* hw bp exception on first nop */
|
|
|
|
ok( context->Eip == (DWORD)code_mem, "eip is wrong: %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem);
|
|
|
|
ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
|
|
|
|
ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
|
|
|
|
context->Dr0 = context->Dr0 + 1; /* set hw bp again on next instruction */
|
|
|
|
context->EFlags |= 0x100; /* enable single stepping */
|
|
|
|
} else if( got_exception == 2) {
|
|
|
|
/* single step exception on second nop */
|
|
|
|
ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem + 1);
|
2007-11-09 17:49:06 +01:00
|
|
|
ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
|
2007-04-04 21:29:14 +02:00
|
|
|
/* depending on the win version the B0 bit is already set here as well
|
|
|
|
ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n"); */
|
2006-11-30 17:54:05 +01:00
|
|
|
context->EFlags |= 0x100;
|
|
|
|
} else if( got_exception == 3) {
|
|
|
|
/* hw bp exception on second nop */
|
|
|
|
ok( context->Eip == (DWORD)code_mem + 1, "eip is wrong: %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem + 1);
|
2007-11-09 17:49:06 +01:00
|
|
|
ok( (context->Dr6 & 0xf) == 1, "B0 flag is not set in Dr6\n");
|
2006-11-30 17:54:05 +01:00
|
|
|
ok( !(context->Dr6 & 0x4000), "BS flag is set in Dr6\n");
|
|
|
|
context->Dr0 = 0; /* clear breakpoint */
|
|
|
|
context->EFlags |= 0x100;
|
|
|
|
} else {
|
|
|
|
/* single step exception on ret */
|
|
|
|
ok( context->Eip == (DWORD)code_mem + 2, "eip is wrong: %x instead of %x\n",
|
|
|
|
context->Eip, (DWORD)code_mem + 2);
|
|
|
|
ok( (context->Dr6 & 0xf) == 0, "B0...3 flags in Dr6 shouldn't be set\n");
|
2007-11-09 17:49:06 +01:00
|
|
|
ok( (context->Dr6 & 0x4000), "BS flag is not set in Dr6\n");
|
2006-11-30 17:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
context->Dr6 = 0; /* clear status register */
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const BYTE dummy_code[] = { 0x90, 0x90, 0xc3 }; /* nop, nop, ret */
|
|
|
|
|
|
|
|
/* test int3 handling */
|
|
|
|
static DWORD int3_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
ok( rec->ExceptionAddress == code_mem, "exception address not at: %p, but at %p\n",
|
|
|
|
code_mem, rec->ExceptionAddress);
|
2007-05-22 18:28:53 +02:00
|
|
|
ok( context->Eip == (DWORD)code_mem, "eip not at: %p, but at %#x\n", code_mem, context->Eip);
|
2006-11-30 17:54:05 +01:00
|
|
|
if(context->Eip == (DWORD)code_mem) context->Eip++; /* skip breakpoint */
|
|
|
|
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const BYTE int3_code[] = { 0xCC, 0xc3 }; /* int 3, ret */
|
|
|
|
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
static void test_exceptions(void)
|
2006-10-03 17:19:36 +02:00
|
|
|
{
|
2006-11-30 17:52:50 +01:00
|
|
|
CONTEXT ctx;
|
|
|
|
NTSTATUS res;
|
2006-10-03 17:19:36 +02:00
|
|
|
|
2006-11-30 17:54:05 +01:00
|
|
|
if (!pNtGetContextThread || !pNtSetContextThread)
|
2006-11-30 17:52:50 +01:00
|
|
|
{
|
2007-08-21 11:28:25 +02:00
|
|
|
skip( "NtGetContextThread/NtSetContextThread not found\n" );
|
2006-10-03 17:19:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-11-30 17:52:50 +01:00
|
|
|
/* test handling of debug registers */
|
2006-11-30 17:53:48 +01:00
|
|
|
run_exception_test(dreg_handler, NULL, &segfault_code, sizeof(segfault_code));
|
2006-11-30 17:52:50 +01:00
|
|
|
|
|
|
|
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
|
|
|
|
res = pNtGetContextThread(GetCurrentThread(), &ctx);
|
|
|
|
ok (res == STATUS_SUCCESS,"NtGetContextThread failed with %x\n", res);
|
|
|
|
ok(ctx.Dr0 == 0x42424242,"failed to set debugregister 0 to 0x42424242, got %x\n", ctx.Dr0);
|
2007-03-14 12:01:12 +01:00
|
|
|
ok((ctx.Dr7 & ~0xdc00) == 0x155,"failed to set debugregister 7 to 0x155, got %x\n", ctx.Dr7);
|
2006-11-30 17:52:50 +01:00
|
|
|
|
|
|
|
/* test single stepping behavior */
|
|
|
|
got_exception = 0;
|
2006-11-30 17:53:48 +01:00
|
|
|
run_exception_test(single_step_handler, NULL, &single_stepcode, sizeof(single_stepcode));
|
2006-11-30 17:54:05 +01:00
|
|
|
ok(got_exception == 3, "expected 3 single step exceptions, got %d\n", got_exception);
|
2006-11-30 17:52:50 +01:00
|
|
|
|
|
|
|
/* test alignment exceptions */
|
|
|
|
got_exception = 0;
|
2006-11-30 17:53:48 +01:00
|
|
|
run_exception_test(align_check_handler, NULL, align_check_code, sizeof(align_check_code));
|
2006-11-30 17:52:50 +01:00
|
|
|
ok(got_exception == 0, "got %d alignment faults, expected 0\n", got_exception);
|
2006-11-30 17:54:05 +01:00
|
|
|
|
2008-03-20 12:24:54 +01:00
|
|
|
/* test direction flag */
|
|
|
|
got_exception = 0;
|
|
|
|
run_exception_test(direction_flag_handler, NULL, direction_flag_code, sizeof(direction_flag_code));
|
|
|
|
ok(got_exception == 1, "got %d exceptions, expected 1\n", got_exception);
|
|
|
|
|
2006-11-30 17:54:05 +01:00
|
|
|
/* test single stepping over hardware breakpoint */
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
ctx.Dr0 = (DWORD) code_mem; /* set hw bp on first nop */
|
|
|
|
ctx.Dr7 = 3;
|
|
|
|
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
|
|
|
|
res = pNtSetContextThread( GetCurrentThread(), &ctx);
|
|
|
|
ok( res == STATUS_SUCCESS, "NtSetContextThread faild with %x\n", res);
|
|
|
|
|
|
|
|
got_exception = 0;
|
|
|
|
run_exception_test(bpx_handler, NULL, dummy_code, sizeof(dummy_code));
|
|
|
|
ok( got_exception == 4,"expected 4 exceptions, got %d\n", got_exception);
|
|
|
|
|
|
|
|
/* test int3 handling */
|
|
|
|
run_exception_test(int3_handler, NULL, int3_code, sizeof(int3_code));
|
2006-10-03 17:19:36 +02:00
|
|
|
}
|
|
|
|
|
2007-02-26 23:42:09 +01:00
|
|
|
static void test_debugger(void)
|
|
|
|
{
|
|
|
|
char cmdline[MAX_PATH];
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
STARTUPINFO si = { 0 };
|
|
|
|
DEBUG_EVENT de;
|
|
|
|
DWORD continuestatus;
|
|
|
|
PVOID code_mem_address = NULL;
|
|
|
|
NTSTATUS status;
|
|
|
|
SIZE_T size_read;
|
2007-03-02 16:12:54 +01:00
|
|
|
BOOL ret;
|
2007-02-26 23:42:09 +01:00
|
|
|
int counter = 0;
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
|
|
|
|
if(!pNtGetContextThread || !pNtSetContextThread || !pNtReadVirtualMemory || !pNtTerminateProcess)
|
|
|
|
{
|
|
|
|
skip("NtGetContextThread, NtSetContextThread, NtReadVirtualMemory or NtTerminateProcess not found\n)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-20 17:01:31 +02:00
|
|
|
sprintf(cmdline, "%s %s %s %p", my_argv[0], my_argv[1], "debuggee", &test_stage);
|
2007-03-02 16:12:54 +01:00
|
|
|
ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, DEBUG_PROCESS, NULL, NULL, &si, &pi);
|
|
|
|
ok(ret, "could not create child process error: %u\n", GetLastError());
|
|
|
|
if (!ret)
|
|
|
|
return;
|
2007-02-26 23:42:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
continuestatus = DBG_CONTINUE;
|
|
|
|
ok(WaitForDebugEvent(&de, INFINITE), "reading debug event\n");
|
|
|
|
|
|
|
|
if (de.dwThreadId != pi.dwThreadId)
|
|
|
|
{
|
|
|
|
trace("event %d not coming from main thread, ignoring\n", de.dwDebugEventCode);
|
|
|
|
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (de.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
|
|
|
|
{
|
|
|
|
if(de.u.CreateProcessInfo.lpBaseOfImage != pNtCurrentTeb()->Peb->ImageBaseAddress)
|
|
|
|
{
|
|
|
|
skip("child process loaded at different address, terminating it\n");
|
2008-02-07 12:56:00 +01:00
|
|
|
pNtTerminateProcess(pi.hProcess, 0);
|
2007-02-26 23:42:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (de.dwDebugEventCode == EXCEPTION_DEBUG_EVENT)
|
|
|
|
{
|
|
|
|
CONTEXT ctx;
|
2007-02-26 23:42:12 +01:00
|
|
|
int stage;
|
2007-02-26 23:42:09 +01:00
|
|
|
|
|
|
|
counter++;
|
|
|
|
status = pNtReadVirtualMemory(pi.hProcess, &code_mem, &code_mem_address,
|
|
|
|
sizeof(code_mem_address), &size_read);
|
|
|
|
ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
|
2007-02-26 23:42:12 +01:00
|
|
|
status = pNtReadVirtualMemory(pi.hProcess, &test_stage, &stage,
|
|
|
|
sizeof(stage), &size_read);
|
|
|
|
ok(!status,"NtReadVirtualMemory failed with 0x%x\n", status);
|
2007-02-26 23:42:09 +01:00
|
|
|
|
|
|
|
ctx.ContextFlags = CONTEXT_FULL;
|
|
|
|
status = pNtGetContextThread(pi.hThread, &ctx);
|
|
|
|
ok(!status, "NtGetContextThread failed with 0x%x\n", status);
|
|
|
|
|
2007-03-14 12:01:12 +01:00
|
|
|
trace("exception 0x%x at %p firstchance=%d Eip=0x%x, Eax=0x%x\n",
|
2007-02-26 23:42:09 +01:00
|
|
|
de.u.Exception.ExceptionRecord.ExceptionCode,
|
|
|
|
de.u.Exception.ExceptionRecord.ExceptionAddress, de.u.Exception.dwFirstChance, ctx.Eip, ctx.Eax);
|
|
|
|
|
|
|
|
if (counter > 100)
|
|
|
|
{
|
|
|
|
ok(FALSE, "got way too many exceptions, probaby caught in a infinite loop, terminating child\n");
|
|
|
|
pNtTerminateProcess(pi.hProcess, 1);
|
|
|
|
}
|
|
|
|
else if (counter >= 2) /* skip startup breakpoint */
|
|
|
|
{
|
2007-02-26 23:42:12 +01:00
|
|
|
if (stage == 1)
|
|
|
|
{
|
|
|
|
ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at %x instead of %p\n",
|
|
|
|
ctx.Eip, (char *)code_mem_address + 0xb);
|
|
|
|
/* setting the context from debugger does not affect the context, the exception handlers gets */
|
|
|
|
/* uncomment once wine is fixed */
|
|
|
|
/* ctx.Eip = 0x12345; */
|
|
|
|
ctx.Eax = 0xf00f00f1;
|
|
|
|
|
|
|
|
/* let the debuggee handle the exception */
|
|
|
|
continuestatus = DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
else if (stage == 2)
|
|
|
|
{
|
|
|
|
if (de.u.Exception.dwFirstChance)
|
|
|
|
{
|
|
|
|
/* debugger gets first chance exception with unmodified ctx.Eip */
|
|
|
|
ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
|
|
|
|
ctx.Eip, (char *)code_mem_address + 0xb);
|
|
|
|
|
|
|
|
/* setting the context from debugger does not affect the context, the exception handlers gets */
|
|
|
|
/* uncomment once wine is fixed */
|
|
|
|
/* ctx.Eip = 0x12345; */
|
|
|
|
ctx.Eax = 0xf00f00f1;
|
|
|
|
|
|
|
|
/* pass exception to debuggee
|
|
|
|
* exception will not be handled and
|
|
|
|
* a second chance exception will be raised */
|
|
|
|
continuestatus = DBG_EXCEPTION_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* debugger gets context after exception handler has played with it */
|
|
|
|
/* ctx.Eip is the same value the exception handler got */
|
|
|
|
if (de.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT)
|
|
|
|
{
|
|
|
|
ok((char *)ctx.Eip == (char *)code_mem_address + 0xa, "Eip at 0x%x instead of %p\n",
|
|
|
|
ctx.Eip, (char *)code_mem_address + 0xa);
|
|
|
|
/* need to fixup Eip for debuggee */
|
|
|
|
if ((char *)ctx.Eip == (char *)code_mem_address + 0xa)
|
|
|
|
ctx.Eip += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ok((char *)ctx.Eip == (char *)code_mem_address + 0xb, "Eip at 0x%x instead of %p\n",
|
|
|
|
ctx.Eip, (char *)code_mem_address + 0xb);
|
|
|
|
/* here we handle exception */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-09-20 17:01:31 +02:00
|
|
|
ok(FALSE, "unexpected stage %x\n", stage);
|
2007-02-26 23:42:12 +01:00
|
|
|
|
2007-02-26 23:42:09 +01:00
|
|
|
status = pNtSetContextThread(pi.hThread, &ctx);
|
|
|
|
ok(!status, "NtSetContextThread failed with 0x%x\n", status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, continuestatus);
|
|
|
|
|
|
|
|
} while (de.dwDebugEventCode != EXIT_PROCESS_DEBUG_EVENT);
|
|
|
|
|
2008-02-07 12:56:00 +01:00
|
|
|
winetest_wait_child_process( pi.hProcess );
|
2007-02-26 23:42:09 +01:00
|
|
|
ok(CloseHandle(pi.hThread) != 0, "error %u\n", GetLastError());
|
|
|
|
ok(CloseHandle(pi.hProcess) != 0, "error %u\n", GetLastError());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-08 22:47:14 +01:00
|
|
|
static DWORD simd_fault_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
|
|
|
|
CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
|
|
|
|
{
|
|
|
|
int *stage = *(int **)(frame + 1);
|
|
|
|
|
|
|
|
got_exception++;
|
|
|
|
|
|
|
|
if( *stage == 1) {
|
|
|
|
/* fault while executing sse instruction */
|
|
|
|
context->Eip += 3; /* skip addps */
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stage 2 - divide by zero fault */
|
|
|
|
if( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION)
|
|
|
|
skip("system doesn't support SIMD exceptions\n");
|
|
|
|
else {
|
|
|
|
ok( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS,
|
|
|
|
"exception code: %#x, should be %#x\n",
|
|
|
|
rec->ExceptionCode, STATUS_FLOAT_MULTIPLE_TRAPS);
|
|
|
|
ok( rec->NumberParameters == 1, "# of params: %i, should be 1\n",
|
|
|
|
rec->NumberParameters);
|
|
|
|
if( rec->NumberParameters == 1 )
|
|
|
|
ok( rec->ExceptionInformation[0] == 0, "param #1: %lx, should be 0\n", rec->ExceptionInformation[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->Eip += 3; /* skip divps */
|
|
|
|
|
|
|
|
return ExceptionContinueExecution;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const BYTE simd_exception_test[] = {
|
|
|
|
0x83, 0xec, 0x4, /* sub $0x4, %esp */
|
|
|
|
0x0f, 0xae, 0x1c, 0x24, /* stmxcsr (%esp) */
|
|
|
|
0x66, 0x81, 0x24, 0x24, 0xff, 0xfd, /* andw $0xfdff,(%esp) * enable divide by */
|
|
|
|
0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) * zero exceptions */
|
|
|
|
0x6a, 0x01, /* push $0x1 */
|
|
|
|
0x6a, 0x01, /* push $0x1 */
|
|
|
|
0x6a, 0x01, /* push $0x1 */
|
|
|
|
0x6a, 0x01, /* push $0x1 */
|
|
|
|
0x0f, 0x10, 0x0c, 0x24, /* movups (%esp),%xmm1 * fill dividend */
|
|
|
|
0x0f, 0x57, 0xc0, /* xorps %xmm0,%xmm0 * clear divisor */
|
|
|
|
0x0f, 0x5e, 0xc8, /* divps %xmm0,%xmm1 * generate fault */
|
|
|
|
0x83, 0xc4, 0x10, /* add $0x10,%esp */
|
|
|
|
0x66, 0x81, 0x0c, 0x24, 0x00, 0x02, /* orw $0x200,(%esp) * disable exceptions */
|
|
|
|
0x0f, 0xae, 0x14, 0x24, /* ldmxcsr (%esp) */
|
|
|
|
0x83, 0xc4, 0x04, /* add $0x4,%esp */
|
|
|
|
0xc3, /* ret */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const BYTE sse_check[] = {
|
|
|
|
0x0f, 0x58, 0xc8, /* addps %xmm0,%xmm1 */
|
|
|
|
0xc3, /* ret */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_simd_exceptions(void)
|
|
|
|
{
|
|
|
|
int stage;
|
|
|
|
|
|
|
|
/* test if CPU & OS can do sse */
|
|
|
|
stage = 1;
|
|
|
|
got_exception = 0;
|
|
|
|
run_exception_test(simd_fault_handler, &stage, sse_check, sizeof(sse_check));
|
|
|
|
if(got_exception) {
|
|
|
|
skip("system doesn't support SSE\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a SIMD exception */
|
|
|
|
stage = 2;
|
|
|
|
got_exception = 0;
|
|
|
|
run_exception_test(simd_fault_handler, &stage, simd_exception_test,
|
|
|
|
sizeof(simd_exception_test));
|
|
|
|
ok( got_exception == 1, "got exception: %i, should be 1\n", got_exception);
|
|
|
|
}
|
|
|
|
|
2005-12-16 16:27:39 +01:00
|
|
|
#endif /* __i386__ */
|
|
|
|
|
|
|
|
START_TEST(exception)
|
|
|
|
{
|
|
|
|
#ifdef __i386__
|
2007-08-21 11:28:25 +02:00
|
|
|
HMODULE hntdll = GetModuleHandleA("ntdll.dll");
|
|
|
|
|
|
|
|
pNtCurrentTeb = (void *)GetProcAddress( hntdll, "NtCurrentTeb" );
|
|
|
|
pNtGetContextThread = (void *)GetProcAddress( hntdll, "NtGetContextThread" );
|
|
|
|
pNtSetContextThread = (void *)GetProcAddress( hntdll, "NtSetContextThread" );
|
|
|
|
pNtReadVirtualMemory = (void *)GetProcAddress( hntdll, "NtReadVirtualMemory" );
|
|
|
|
pRtlRaiseException = (void *)GetProcAddress( hntdll, "RtlRaiseException" );
|
|
|
|
pNtTerminateProcess = (void *)GetProcAddress( hntdll, "NtTerminateProcess" );
|
|
|
|
pRtlAddVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
|
2007-02-26 23:42:09 +01:00
|
|
|
"RtlAddVectoredExceptionHandler" );
|
2007-08-21 11:28:25 +02:00
|
|
|
pRtlRemoveVectoredExceptionHandler = (void *)GetProcAddress( hntdll,
|
2007-02-26 23:42:09 +01:00
|
|
|
"RtlRemoveVectoredExceptionHandler" );
|
2006-11-30 17:52:50 +01:00
|
|
|
if (!pNtCurrentTeb)
|
|
|
|
{
|
2007-08-21 11:28:25 +02:00
|
|
|
skip( "NtCurrentTeb not found\n" );
|
2006-11-30 17:52:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-26 23:42:09 +01:00
|
|
|
if (pRtlAddVectoredExceptionHandler && pRtlRemoveVectoredExceptionHandler)
|
|
|
|
have_vectored_api = TRUE;
|
|
|
|
else
|
|
|
|
skip("RtlAddVectoredExceptionHandler or RtlRemoveVectoredExceptionHandler not found\n");
|
|
|
|
|
2006-11-30 17:53:48 +01:00
|
|
|
/* 1024 byte should be sufficient */
|
|
|
|
code_mem = VirtualAlloc(NULL, 1024, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
|
|
if(!code_mem) {
|
|
|
|
trace("VirtualAlloc failed\n");
|
|
|
|
return;
|
|
|
|
}
|
2007-02-26 23:42:09 +01:00
|
|
|
|
|
|
|
my_argc = winetest_get_mainargs( &my_argv );
|
2007-09-20 17:01:31 +02:00
|
|
|
if (my_argc >= 4)
|
2007-02-26 23:42:09 +01:00
|
|
|
{
|
2007-09-20 17:01:31 +02:00
|
|
|
void *addr;
|
|
|
|
sscanf( my_argv[3], "%p", &addr );
|
|
|
|
|
|
|
|
if (addr != &test_stage)
|
|
|
|
{
|
|
|
|
skip( "child process not mapped at same address (%p/%p)\n", &test_stage, addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-26 23:42:09 +01:00
|
|
|
/* child must be run under a debugger */
|
|
|
|
if (!pNtCurrentTeb()->Peb->BeingDebugged)
|
|
|
|
{
|
|
|
|
ok(FALSE, "child process not being debugged?\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pRtlRaiseException)
|
|
|
|
{
|
2007-02-26 23:42:12 +01:00
|
|
|
test_stage = 1;
|
|
|
|
run_rtlraiseexception_test(0x12345);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
|
|
|
|
test_stage = 2;
|
2007-02-26 23:42:09 +01:00
|
|
|
run_rtlraiseexception_test(0x12345);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_BREAKPOINT);
|
|
|
|
run_rtlraiseexception_test(EXCEPTION_INVALID_HANDLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
skip( "RtlRaiseException not found\n" );
|
|
|
|
|
|
|
|
/* rest of tests only run in parent */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-16 16:27:39 +01:00
|
|
|
test_prot_fault();
|
2006-11-30 17:52:50 +01:00
|
|
|
test_exceptions();
|
2007-03-14 12:01:12 +01:00
|
|
|
test_rtlraiseexception();
|
2007-02-26 23:42:09 +01:00
|
|
|
test_debugger();
|
2007-03-08 22:47:14 +01:00
|
|
|
test_simd_exceptions();
|
2006-11-30 17:53:48 +01:00
|
|
|
|
|
|
|
VirtualFree(code_mem, 1024, MEM_RELEASE);
|
2005-12-16 16:27:39 +01:00
|
|
|
#endif
|
|
|
|
}
|