2 * Unit test suite for ntdll exceptions
4 * Copyright 2005 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
28 #define WIN32_NO_STATUS
35 #include "wine/test.h"
39 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
40 static NTSTATUS (WINAPI *pNtGetContextThread)(HANDLE,CONTEXT*);
42 /* Test various instruction combinations that cause a protection fault on the i386,
43 * and check what the resulting exception looks like.
46 static const struct exception
48 BYTE code[18]; /* asm code */
49 BYTE offset; /* offset of faulting instruction */
50 BYTE length; /* length of faulting instruction */
51 NTSTATUS status; /* expected status code */
52 DWORD nb_params; /* expected number of parameters */
53 DWORD params[4]; /* expected parameters */
56 /* test some privileged instructions */
57 { { 0xfb, 0xc3 }, /* sti; ret */
58 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
59 { { 0x6c, 0xc3 }, /* insb (%dx); ret */
60 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
61 { { 0x6d, 0xc3 }, /* insl (%dx); ret */
62 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
63 { { 0x6e, 0xc3 }, /* outsb (%dx); ret */
64 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
65 { { 0x6f, 0xc3 }, /* outsl (%dx); ret */
66 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
67 { { 0xe4, 0x11, 0xc3 }, /* inb $0x11,%al; ret */
68 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
69 { { 0xe5, 0x11, 0xc3 }, /* inl $0x11,%eax; ret */
70 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
71 { { 0xe6, 0x11, 0xc3 }, /* outb %al,$0x11; ret */
72 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
73 { { 0xe7, 0x11, 0xc3 }, /* outl %eax,$0x11; ret */
74 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
75 { { 0xed, 0xc3 }, /* inl (%dx),%eax; ret */
76 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
77 { { 0xee, 0xc3 }, /* outb %al,(%dx); ret */
78 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
79 { { 0xef, 0xc3 }, /* outl %eax,(%dx); ret */
80 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
81 { { 0xf4, 0xc3 }, /* hlt; ret */
82 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
83 { { 0xfa, 0xc3 }, /* cli; ret */
84 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
86 /* test long jump to invalid selector */
87 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* ljmp $0,$0; ret */
88 0, 7, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
90 /* test iret to invalid selector */
91 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
92 /* pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
93 6, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
95 /* test loading an invalid selector */
96 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* mov $beef,%ax; mov %ax,%gs; ret */
97 5, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } },
99 /* test accessing a zero selector */
100 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
101 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es */
102 5, 6, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
104 /* test moving %cs -> %ss */
105 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
106 1, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
108 /* test overlong instruction (limit is 16 bytes) */
109 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
110 0, 16, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
111 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
112 0, 15, STATUS_PRIVILEGED_INSTRUCTION, 0 },
114 /* test invalid interrupt */
115 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
116 0, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
118 /* test moves to/from Crx */
119 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
120 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
121 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
122 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
123 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
124 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
125 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
126 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
128 /* test moves to/from Drx */
129 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
130 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
131 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
132 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
133 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
134 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
135 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
136 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
137 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
138 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
139 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
140 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
142 /* test memory reads */
143 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
144 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
145 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
146 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
147 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
148 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
149 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
150 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
152 /* test memory writes */
153 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
154 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
155 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
156 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
157 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
158 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
159 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
160 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
163 static int got_exception;
165 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
166 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
168 const struct exception *except = *(const struct exception **)(frame + 1);
169 unsigned int i, entry = except - exceptions;
172 trace( "exception: %lx flags:%lx addr:%p\n",
173 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
175 ok( rec->ExceptionCode == except->status,
176 "%u: Wrong exception code %lx/%lx\n", entry, rec->ExceptionCode, except->status );
177 ok( rec->ExceptionAddress == except->code + except->offset,
178 "%u: Wrong exception address %p/%p\n", entry,
179 rec->ExceptionAddress, except->code + except->offset );
181 ok( rec->NumberParameters == except->nb_params,
182 "%u: Wrong number of parameters %lu/%lu\n", entry, rec->NumberParameters, except->nb_params );
183 for (i = 0; i < rec->NumberParameters; i++)
184 ok( rec->ExceptionInformation[i] == except->params[i],
185 "%u: Wrong parameter %d: %lx/%lx\n",
186 entry, i, rec->ExceptionInformation[i], except->params[i] );
188 /* don't handle exception if it's not the address we expected */
189 if (rec->ExceptionAddress != except->code + except->offset) return ExceptionContinueSearch;
191 context->Eip += except->length;
192 return ExceptionContinueExecution;
195 static void test_prot_fault(void)
200 EXCEPTION_REGISTRATION_RECORD frame;
201 const struct exception *except;
204 pNtCurrentTeb = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtCurrentTeb" );
207 trace( "NtCurrentTeb not found, skipping tests\n" );
211 exc_frame.frame.Handler = handler;
212 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
213 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
214 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
216 void (*func)(void) = (void *)exceptions[i].code;
217 exc_frame.except = &exceptions[i];
220 if (!i && !got_exception)
222 trace( "No exception, assuming win9x, no point in testing further\n" );
225 ok( got_exception == (exceptions[i].status != 0),
226 "%u: bad exception count %d\n", i, got_exception );
228 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
231 static DWORD dreg_handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
232 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
234 context->Eip += 2; /* Skips the popl (%eax) */
240 context->Dr7 = 0x155;
241 return ExceptionContinueExecution;
244 static BYTE code[5] = {
245 0x31, 0xc0, /* xor %eax,%eax */
246 0x8f, 0x00, /* popl (%eax) - cause exception */
250 static void test_debug_regs(void)
256 EXCEPTION_REGISTRATION_RECORD frame;
258 void (*func)(void) = (void*)code;
260 pNtCurrentTeb = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtCurrentTeb" );
263 trace( "NtCurrentTeb not found, skipping tests\n" );
266 pNtGetContextThread = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtGetContextThread" );
267 if (!pNtGetContextThread)
269 trace( "NtGetContextThread not found, skipping tests\n" );
273 exc_frame.frame.Handler = dreg_handler;
274 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
275 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
277 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
278 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
279 res = pNtGetContextThread(GetCurrentThread(), &ctx);
280 ok(res == STATUS_SUCCESS,"NtGetContextThread failed with %lx\n", res);
281 ok(ctx.Dr7 == 0x155,"failed to set debugregister 7 to 0x155, got %lx\n", ctx.Dr7);
284 #endif /* __i386__ */
286 START_TEST(exception)