ntdll: Add a test for NtNotifyChangeDirectoryFile.
[wine/multimedia.git] / dlls / ntdll / tests / exception.c
blob9cc431b0507cd0e98615c38361fcb9e579344b2e
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #ifndef _WIN32_WINNT
24 #define _WIN32_WINNT 0x500 /* For NTSTATUS */
25 #endif
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnt.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "excpt.h"
35 #include "wine/test.h"
37 #ifdef __i386__
39 static struct _TEB * (WINAPI *pNtCurrentTeb)(void);
41 /* Test various instruction combinations that cause a protection fault on the i386,
42 * and check what the resulting exception looks like.
45 static const struct exception
47 BYTE code[18]; /* asm code */
48 BYTE offset; /* offset of faulting instruction */
49 BYTE length; /* length of faulting instruction */
50 NTSTATUS status; /* expected status code */
51 DWORD nb_params; /* expected number of parameters */
52 DWORD params[4]; /* expected parameters */
53 } exceptions[] =
55 /* test some privileged instructions */
56 { { 0xfb, 0xc3 }, /* sti; ret */
57 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
58 { { 0x6c, 0xc3 }, /* insb (%dx); ret */
59 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
60 { { 0x6d, 0xc3 }, /* insl (%dx); ret */
61 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
62 { { 0x6e, 0xc3 }, /* outsb (%dx); ret */
63 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
64 { { 0x6f, 0xc3 }, /* outsl (%dx); ret */
65 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
66 { { 0xe4, 0x11, 0xc3 }, /* inb $0x11,%al; ret */
67 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
68 { { 0xe5, 0x11, 0xc3 }, /* inl $0x11,%eax; ret */
69 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
70 { { 0xe6, 0x11, 0xc3 }, /* outb %al,$0x11; ret */
71 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
72 { { 0xe7, 0x11, 0xc3 }, /* outl %eax,$0x11; ret */
73 0, 2, STATUS_PRIVILEGED_INSTRUCTION, 0 },
74 { { 0xed, 0xc3 }, /* inl (%dx),%eax; ret */
75 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
76 { { 0xee, 0xc3 }, /* outb %al,(%dx); ret */
77 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
78 { { 0xef, 0xc3 }, /* outl %eax,(%dx); ret */
79 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
80 { { 0xf4, 0xc3 }, /* hlt; ret */
81 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
82 { { 0xfa, 0xc3 }, /* cli; ret */
83 0, 1, STATUS_PRIVILEGED_INSTRUCTION, 0 },
85 /* test long jump to invalid selector */
86 { { 0xea, 0, 0, 0, 0, 0, 0, 0xc3 }, /* ljmp $0,$0; ret */
87 0, 7, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
89 /* test iret to invalid selector */
90 { { 0x6a, 0x00, 0x6a, 0x00, 0x6a, 0x00, 0xcf, 0x83, 0xc4, 0x0c, 0xc3 },
91 /* pushl $0; pushl $0; pushl $0; iret; addl $12,%esp; ret */
92 6, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
94 /* test loading an invalid selector */
95 { { 0xb8, 0xef, 0xbe, 0x00, 0x00, 0x8e, 0xe8, 0xc3 }, /* mov $beef,%ax; mov %ax,%gs; ret */
96 5, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xbee8 } },
98 /* test accessing a zero selector */
99 { { 0x06, 0x31, 0xc0, 0x8e, 0xc0, 0x26, 0xa1, 0, 0, 0, 0, 0x07, 0xc3 },
100 /* push %es; xor %eax,%eax; mov %ax,%es; mov %es:(0),%ax; pop %es */
101 5, 6, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
103 /* test moving %cs -> %ss */
104 { { 0x0e, 0x17, 0x58, 0xc3 }, /* pushl %cs; popl %ss; popl %eax; ret */
105 1, 1, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
107 /* test overlong instruction (limit is 16 bytes) */
108 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
109 0, 16, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
110 { { 0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0x64,0xfa,0xc3 },
111 0, 15, STATUS_PRIVILEGED_INSTRUCTION, 0 },
113 /* test invalid interrupt */
114 { { 0xcd, 0xff, 0xc3 }, /* int $0xff; ret */
115 0, 2, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
117 /* test moves to/from Crx */
118 { { 0x0f, 0x20, 0xc0, 0xc3 }, /* movl %cr0,%eax; ret */
119 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
120 { { 0x0f, 0x20, 0xe0, 0xc3 }, /* movl %cr4,%eax; ret */
121 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
122 { { 0x0f, 0x22, 0xc0, 0xc3 }, /* movl %eax,%cr0; ret */
123 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
124 { { 0x0f, 0x22, 0xe0, 0xc3 }, /* movl %eax,%cr4; ret */
125 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
127 /* test moves to/from Drx */
128 { { 0x0f, 0x21, 0xc0, 0xc3 }, /* movl %dr0,%eax; ret */
129 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
130 { { 0x0f, 0x21, 0xc8, 0xc3 }, /* movl %dr1,%eax; ret */
131 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
132 { { 0x0f, 0x21, 0xf8, 0xc3 }, /* movl %dr7,%eax; ret */
133 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
134 { { 0x0f, 0x23, 0xc0, 0xc3 }, /* movl %eax,%dr0; ret */
135 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
136 { { 0x0f, 0x23, 0xc8, 0xc3 }, /* movl %eax,%dr1; ret */
137 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
138 { { 0x0f, 0x23, 0xf8, 0xc3 }, /* movl %eax,%dr7; ret */
139 0, 3, STATUS_PRIVILEGED_INSTRUCTION, 0 },
141 /* test memory reads */
142 { { 0xa1, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffc,%eax; ret */
143 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xfffffffc } },
144 { { 0xa1, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffd,%eax; ret */
145 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
146 { { 0xa1, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xfffffffe,%eax; ret */
147 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
148 { { 0xa1, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl 0xffffffff,%eax; ret */
149 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
151 /* test memory writes */
152 { { 0xa3, 0xfc, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffc; ret */
153 0, 5, STATUS_ACCESS_VIOLATION, 2, { 1, 0xfffffffc } },
154 { { 0xa3, 0xfd, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffd; ret */
155 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
156 { { 0xa3, 0xfe, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xfffffffe; ret */
157 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
158 { { 0xa3, 0xff, 0xff, 0xff, 0xff, 0xc3 }, /* movl %eax,0xffffffff; ret */
159 0, 5, STATUS_ACCESS_VIOLATION, 2, { 0, 0xffffffff } },
162 static int got_exception;
164 static DWORD handler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
165 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
167 const struct exception *except = *(const struct exception **)(frame + 1);
168 unsigned int i, entry = except - exceptions;
170 got_exception++;
171 trace( "exception: %lx flags:%lx addr:%p\n",
172 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
174 ok( rec->ExceptionCode == except->status,
175 "%u: Wrong exception code %lx/%lx\n", entry, rec->ExceptionCode, except->status );
176 ok( rec->ExceptionAddress == except->code + except->offset,
177 "%u: Wrong exception address %p/%p\n", entry,
178 rec->ExceptionAddress, except->code + except->offset );
180 ok( rec->NumberParameters == except->nb_params,
181 "%u: Wrong number of parameters %lu/%lu\n", entry, rec->NumberParameters, except->nb_params );
182 for (i = 0; i < rec->NumberParameters; i++)
183 ok( rec->ExceptionInformation[i] == except->params[i],
184 "%u: Wrong parameter %d: %lx/%lx\n",
185 entry, i, rec->ExceptionInformation[i], except->params[i] );
187 /* don't handle exception if it's not the address we expected */
188 if (rec->ExceptionAddress != except->code + except->offset) return ExceptionContinueSearch;
190 context->Eip += except->length;
191 return ExceptionContinueExecution;
194 static void test_prot_fault(void)
196 unsigned int i;
197 struct
199 EXCEPTION_REGISTRATION_RECORD frame;
200 const struct exception *except;
201 } exc_frame;
203 pNtCurrentTeb = (void *)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtCurrentTeb" );
204 if (!pNtCurrentTeb)
206 trace( "NtCurrentTeb not found, skipping tests\n" );
207 return;
210 exc_frame.frame.Handler = handler;
211 exc_frame.frame.Prev = pNtCurrentTeb()->Tib.ExceptionList;
212 pNtCurrentTeb()->Tib.ExceptionList = &exc_frame.frame;
213 for (i = 0; i < sizeof(exceptions)/sizeof(exceptions[0]); i++)
215 void (*func)(void) = (void *)exceptions[i].code;
216 exc_frame.except = &exceptions[i];
217 got_exception = 0;
218 func();
219 if (!i && !got_exception)
221 trace( "No exception, assuming win9x, no point in testing further\n" );
222 break;
224 ok( got_exception == (exceptions[i].status != 0),
225 "%u: bad exception count %d\n", i, got_exception );
227 pNtCurrentTeb()->Tib.ExceptionList = exc_frame.frame.Prev;
230 #endif /* __i386__ */
232 START_TEST(exception)
234 #ifdef __i386__
235 test_prot_fault();
236 #endif