- Sync up Mm interface with WinLdr branch (introduce the concept of a memory type...
[reactos.git] / reactos / dll / win32 / kernel32 / except / except.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/except.c
6 * PURPOSE: Exception functions
7 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
8 * modified from WINE [ Onno Hovers, (onno@stack.urc.tue.nl) ]
9 * UPDATE HISTORY:
10 * Created 01/11/98
11 */
12
13 #include <k32.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter = NULL;
19
20 UINT
21 WINAPI
22 GetErrorMode(VOID)
23 {
24 NTSTATUS Status;
25 UINT ErrMode;
26
27 /* Query the current setting */
28 Status = NtQueryInformationProcess(NtCurrentProcess(),
29 ProcessDefaultHardErrorMode,
30 (PVOID)&ErrMode,
31 sizeof(ErrMode),
32 NULL);
33 if (!NT_SUCCESS(Status))
34 {
35 /* Fail if we couldn't query */
36 SetLastErrorByStatus(Status);
37 return 0;
38 }
39
40 /* Check if NOT failing critical errors was requested */
41 if (ErrMode & SEM_FAILCRITICALERRORS)
42 {
43 /* Mask it out, since the native API works differently */
44 ErrMode &= ~SEM_FAILCRITICALERRORS;
45 }
46 else
47 {
48 /* OR it if the caller didn't, due to different native semantics */
49 ErrMode |= SEM_FAILCRITICALERRORS;
50 }
51
52 /* Return the mode */
53 return ErrMode;
54 }
55
56 /*
57 * @implemented
58 */
59 UINT
60 WINAPI
61 SetErrorMode(IN UINT uMode)
62 {
63 UINT PrevErrMode, NewMode;
64 NTSTATUS Status;
65
66 /* Get the previous mode */
67 PrevErrMode = GetErrorMode();
68 NewMode = uMode;
69
70 /* Check if failing critical errors was requested */
71 if (NewMode & SEM_FAILCRITICALERRORS)
72 {
73 /* Mask it out, since the native API works differently */
74 NewMode &= ~SEM_FAILCRITICALERRORS;
75 }
76 else
77 {
78 /* OR it if the caller didn't, due to different native semantics */
79 NewMode |= SEM_FAILCRITICALERRORS;
80 }
81
82 /* Set the new mode */
83 Status = NtSetInformationProcess(NtCurrentProcess(),
84 ProcessDefaultHardErrorMode,
85 (PVOID)&NewMode,
86 sizeof(NewMode));
87 if(!NT_SUCCESS(Status)) SetLastErrorByStatus(Status);
88
89 /* Return the previous mode */
90 return PrevErrMode;
91 }
92
93 /*
94 * @implemented
95 */
96 LPTOP_LEVEL_EXCEPTION_FILTER
97 WINAPI
98 SetUnhandledExceptionFilter(
99 IN LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
100 {
101 return InterlockedExchangePointer(&GlobalTopLevelExceptionFilter,
102 lpTopLevelExceptionFilter);
103 }
104
105 /*
106 * Private helper function to lookup the module name from a given address.
107 * The address can point to anywhere within the module.
108 */
109 static const char*
110 _module_name_from_addr(const void* addr, void **module_start_addr,
111 char* psz, size_t nChars)
112 {
113 MEMORY_BASIC_INFORMATION mbi;
114 if (VirtualQuery(addr, &mbi, sizeof(mbi)) != sizeof(mbi) ||
115 !GetModuleFileNameA((HMODULE)mbi.AllocationBase, psz, nChars))
116 {
117 psz[0] = '\0';
118 *module_start_addr = 0;
119 }
120 else
121 {
122 *module_start_addr = (void *)mbi.AllocationBase;
123 }
124 return psz;
125 }
126
127 #ifdef _M_IX86
128 static VOID
129 _dump_context(PCONTEXT pc)
130 {
131 /*
132 * Print out the CPU registers
133 */
134 DbgPrint("CS:EIP %x:%x\n", pc->SegCs&0xffff, pc->Eip );
135 DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
136 pc->SegFs&0xffff, pc->SegGs&0xfff);
137 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc->Eax, pc->Ebx, pc->Ecx);
138 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc->Edx,
139 pc->Ebp, pc->Esi, pc->Esp);
140 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc->Edi, pc->EFlags);
141 }
142 #else
143 #warning Unknown architecture
144 static VOID
145 _dump_context(PCONTEXT pc)
146 {
147 }
148 #endif
149
150 static LONG
151 BasepCheckForReadOnlyResource(IN PVOID Ptr)
152 {
153 PVOID Data;
154 ULONG Size, OldProtect;
155 MEMORY_BASIC_INFORMATION mbi;
156 NTSTATUS Status;
157 LONG Ret = EXCEPTION_CONTINUE_SEARCH;
158
159 /* Check if it was an attempt to write to a read-only image section! */
160 Status = NtQueryVirtualMemory(NtCurrentProcess(),
161 Ptr,
162 MemoryBasicInformation,
163 &mbi,
164 sizeof(mbi),
165 NULL);
166 if (NT_SUCCESS(Status) &&
167 mbi.Protect == PAGE_READONLY && mbi.Type == MEM_IMAGE)
168 {
169 /* Attempt to treat it as a resource section. We need to
170 use SEH here because we don't know if it's actually a
171 resource mapping */
172
173 _SEH_TRY
174 {
175 Data = RtlImageDirectoryEntryToData(mbi.AllocationBase,
176 TRUE,
177 IMAGE_DIRECTORY_ENTRY_RESOURCE,
178 &Size);
179
180 if (Data != NULL &&
181 (ULONG_PTR)Ptr >= (ULONG_PTR)Data &&
182 (ULONG_PTR)Ptr < (ULONG_PTR)Data + Size)
183 {
184 /* The user tried to write into the resources. Make the page
185 writable... */
186 Size = 1;
187 Status = NtProtectVirtualMemory(NtCurrentProcess(),
188 &Ptr,
189 &Size,
190 PAGE_READWRITE,
191 &OldProtect);
192 if (NT_SUCCESS(Status))
193 {
194 Ret = EXCEPTION_CONTINUE_EXECUTION;
195 }
196 }
197 }
198 _SEH_HANDLE
199 {
200 }
201 _SEH_END;
202 }
203
204 return Ret;
205 }
206
207 /*
208 * @unimplemented
209 */
210 LONG STDCALL
211 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
212 {
213 LONG RetValue;
214 HANDLE DebugPort = NULL;
215 NTSTATUS ErrCode;
216 ULONG ErrorParameters[4];
217 ULONG ErrorResponse;
218
219 if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
220 ExceptionInfo->ExceptionRecord->NumberParameters >= 2)
221 {
222 switch(ExceptionInfo->ExceptionRecord->ExceptionInformation[0])
223 {
224 case EXCEPTION_WRITE_FAULT:
225 /* Change the protection on some write attempts, some InstallShield setups
226 have this bug */
227 RetValue = BasepCheckForReadOnlyResource(
228 (PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
229 if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
230 return EXCEPTION_CONTINUE_EXECUTION;
231 break;
232 case EXCEPTION_EXECUTE_FAULT:
233 /* FIXME */
234 break;
235 }
236 }
237
238 /* Is there a debugger running ? */
239 ErrCode = NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort,
240 &DebugPort, sizeof(HANDLE), NULL);
241 if (!NT_SUCCESS(ErrCode) && ErrCode != STATUS_NOT_IMPLEMENTED)
242 {
243 SetLastErrorByStatus(ErrCode);
244 return EXCEPTION_EXECUTE_HANDLER;
245 }
246
247 if (DebugPort)
248 {
249 /* Pass the exception to debugger. */
250 DPRINT("Passing exception to debugger\n");
251 return EXCEPTION_CONTINUE_SEARCH;
252 }
253
254 if (GlobalTopLevelExceptionFilter)
255 {
256 LONG ret = GlobalTopLevelExceptionFilter( ExceptionInfo );
257 if (ret != EXCEPTION_CONTINUE_SEARCH)
258 return ret;
259 }
260
261 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
262 {
263 #ifdef _X86_
264 PULONG Frame;
265 #endif
266 PVOID StartAddr;
267 CHAR szMod[128] = "";
268
269 /* Print a stack trace. */
270 DbgPrint("Unhandled exception\n");
271 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionCode);
272 if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
273 ExceptionInfo->ExceptionRecord->NumberParameters == 2)
274 {
275 DbgPrint("Faulting Address: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
276 }
277 DbgPrint("Address: %8x %s\n",
278 ExceptionInfo->ExceptionRecord->ExceptionAddress,
279 _module_name_from_addr(ExceptionInfo->ExceptionRecord->ExceptionAddress, &StartAddr, szMod, sizeof(szMod)));
280 _dump_context ( ExceptionInfo->ContextRecord );
281 #ifdef _X86_
282 DbgPrint("Frames:\n");
283 _SEH_TRY
284 {
285 Frame = (PULONG)ExceptionInfo->ContextRecord->Ebp;
286 while (Frame[1] != 0 && Frame[1] != 0xdeadbeef)
287 {
288 if (IsBadReadPtr((PVOID)Frame[1], 4)) {
289 DbgPrint(" %8x%9s %s\n", Frame[1], "<invalid address>"," ");
290 } else {
291 _module_name_from_addr((const void*)Frame[1], &StartAddr,
292 szMod, sizeof(szMod));
293 DbgPrint(" %8x+%-8x %s\n",
294 (PVOID)StartAddr,
295 (ULONG_PTR)Frame[1] - (ULONG_PTR)StartAddr, szMod);
296 }
297 if (IsBadReadPtr((PVOID)Frame[0], sizeof(*Frame) * 2)) {
298 break;
299 }
300 Frame = (PULONG)Frame[0];
301 }
302 }
303 _SEH_HANDLE
304 {
305 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
306 }
307 _SEH_END;
308 #endif
309 }
310
311 /* Save exception code and address */
312 ErrorParameters[0] = (ULONG)ExceptionInfo->ExceptionRecord->ExceptionCode;
313 ErrorParameters[1] = (ULONG)ExceptionInfo->ExceptionRecord->ExceptionAddress;
314
315 if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION)
316 {
317 /* get the type of operation that caused the access violation */
318 ErrorParameters[2] = ExceptionInfo->ExceptionRecord->ExceptionInformation[0];
319 }
320 else
321 {
322 ErrorParameters[2] = ExceptionInfo->ExceptionRecord->ExceptionInformation[2];
323 }
324
325 /* Save faulting address */
326 ErrorParameters[3] = ExceptionInfo->ExceptionRecord->ExceptionInformation[1];
327
328 /* Raise the harderror */
329 ErrCode = NtRaiseHardError(STATUS_UNHANDLED_EXCEPTION | 0x10000000,
330 4, 0, ErrorParameters, OptionOkCancel, &ErrorResponse);
331
332 if (NT_SUCCESS(ErrCode) && (ErrorResponse == ResponseCancel))
333 {
334 /* FIXME: Check the result, if the "Cancel" button was
335 clicked run a debugger */
336 DPRINT1("Debugging is not implemented yet\n");
337 }
338
339 /*
340 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
341 * the __except block will be executed. Normally this will end up in a
342 * Terminate process.
343 */
344
345 return EXCEPTION_EXECUTE_HANDLER;
346 }
347
348 /*
349 * @implemented
350 */
351 VOID
352 WINAPI
353 RaiseException(IN DWORD dwExceptionCode,
354 IN DWORD dwExceptionFlags,
355 IN DWORD nNumberOfArguments,
356 IN CONST ULONG_PTR *lpArguments OPTIONAL)
357 {
358 EXCEPTION_RECORD ExceptionRecord;
359
360 /* Setup the exception record */
361 ExceptionRecord.ExceptionCode = dwExceptionCode;
362 ExceptionRecord.ExceptionRecord = NULL;
363 ExceptionRecord.ExceptionAddress = (PVOID)RaiseException;
364 ExceptionRecord.ExceptionFlags = dwExceptionFlags & EXCEPTION_NONCONTINUABLE;
365
366 /* Check if we have arguments */
367 if (!lpArguments)
368 {
369 /* We don't */
370 ExceptionRecord.NumberParameters = 0;
371 }
372 else
373 {
374 /* We do, normalize the count */
375 if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
376 {
377 nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
378 }
379
380 /* Set the count of parameters */
381 ExceptionRecord.NumberParameters = nNumberOfArguments;
382
383 /* Loop each parameter */
384 for (nNumberOfArguments = 0;
385 (nNumberOfArguments < ExceptionRecord.NumberParameters);
386 nNumberOfArguments ++)
387 {
388 /* Copy the exception information */
389 ExceptionRecord.ExceptionInformation[nNumberOfArguments] =
390 *lpArguments++;
391 }
392 }
393
394 /* Raise the exception */
395 RtlRaiseException(&ExceptionRecord);
396 }
397
398 /* EOF */