- Move NCI generated files to arch-specific directories
[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 "../include/debug.h"
17
18 LPTOP_LEVEL_EXCEPTION_FILTER GlobalTopLevelExceptionFilter = UnhandledExceptionFilter;
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 static VOID
128 _dump_context(PCONTEXT pc)
129 {
130 /*
131 * Print out the CPU registers
132 */
133 DbgPrint("CS:EIP %x:%x\n", pc->SegCs&0xffff, pc->Eip );
134 DbgPrint("DS %x ES %x FS %x GS %x\n", pc->SegDs&0xffff, pc->SegEs&0xffff,
135 pc->SegFs&0xffff, pc->SegGs&0xfff);
136 DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", pc->Eax, pc->Ebx, pc->Ecx);
137 DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x ESP: %.8x\n", pc->Edx,
138 pc->Ebp, pc->Esi, pc->Esp);
139 DbgPrint("EDI: %.8x EFLAGS: %.8x\n", pc->Edi, pc->EFlags);
140 }
141
142 static LONG
143 BasepCheckForReadOnlyResource(IN PVOID Ptr)
144 {
145 PVOID Data;
146 ULONG Size, OldProtect;
147 MEMORY_BASIC_INFORMATION mbi;
148 NTSTATUS Status;
149 LONG Ret = EXCEPTION_CONTINUE_SEARCH;
150
151 /* Check if it was an attempt to write to a read-only image section! */
152 Status = NtQueryVirtualMemory(NtCurrentProcess(),
153 Ptr,
154 MemoryBasicInformation,
155 &mbi,
156 sizeof(mbi),
157 NULL);
158 if (NT_SUCCESS(Status) &&
159 mbi.Protect == PAGE_READONLY && mbi.Type == MEM_IMAGE)
160 {
161 /* Attempt to treat it as a resource section. We need to
162 use SEH here because we don't know if it's actually a
163 resource mapping */
164
165 _SEH_TRY
166 {
167 Data = RtlImageDirectoryEntryToData(mbi.AllocationBase,
168 TRUE,
169 IMAGE_DIRECTORY_ENTRY_RESOURCE,
170 &Size);
171
172 if (Data != NULL &&
173 (ULONG_PTR)Ptr >= (ULONG_PTR)Data &&
174 (ULONG_PTR)Ptr < (ULONG_PTR)Data + Size)
175 {
176 /* The user tried to write into the resources. Make the page
177 writable... */
178 Size = 1;
179 Status = NtProtectVirtualMemory(NtCurrentProcess(),
180 &Ptr,
181 &Size,
182 PAGE_READWRITE,
183 &OldProtect);
184 if (NT_SUCCESS(Status))
185 {
186 Ret = EXCEPTION_CONTINUE_EXECUTION;
187 }
188 }
189 }
190 _SEH_HANDLE
191 {
192 }
193 _SEH_END;
194 }
195
196 return Ret;
197 }
198
199 /*
200 * @unimplemented
201 */
202 LONG STDCALL
203 UnhandledExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo)
204 {
205 LONG RetValue;
206 HANDLE DebugPort = NULL;
207 NTSTATUS ErrCode;
208
209 if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
210 ExceptionInfo->ExceptionRecord->ExceptionInformation[0])
211 {
212 /* Change the protection on some write attempts, some InstallShield setups
213 have this bug */
214 RetValue = BasepCheckForReadOnlyResource(
215 (PVOID)ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
216 if (RetValue == EXCEPTION_CONTINUE_EXECUTION)
217 return EXCEPTION_CONTINUE_EXECUTION;
218 }
219
220 /* Is there a debugger running ? */
221 ErrCode = NtQueryInformationProcess(NtCurrentProcess(), ProcessDebugPort,
222 &DebugPort, sizeof(HANDLE), NULL);
223 if (!NT_SUCCESS(ErrCode) && ErrCode != STATUS_NOT_IMPLEMENTED)
224 {
225 SetLastErrorByStatus(ErrCode);
226 return EXCEPTION_EXECUTE_HANDLER;
227 }
228
229 if (DebugPort)
230 {
231 /* Pass the exception to debugger. */
232 DPRINT("Passing exception to debugger\n");
233 return EXCEPTION_CONTINUE_SEARCH;
234 }
235
236 if ((GetErrorMode() & SEM_NOGPFAULTERRORBOX) == 0)
237 {
238 #ifdef _X86_
239 PULONG Frame;
240 PVOID StartAddr;
241 CHAR szMod[128] = "";
242 #endif
243
244 /* Print a stack trace. */
245 DbgPrint("Unhandled exception\n");
246 DbgPrint("ExceptionCode: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionCode);
247 if (ExceptionInfo->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION &&
248 ExceptionInfo->ExceptionRecord->NumberParameters == 2)
249 {
250 DbgPrint("Faulting Address: %8x\n", ExceptionInfo->ExceptionRecord->ExceptionInformation[1]);
251 }
252 DbgPrint("Address: %8x %s\n",
253 ExceptionInfo->ExceptionRecord->ExceptionAddress,
254 _module_name_from_addr(ExceptionInfo->ExceptionRecord->ExceptionAddress, &StartAddr, szMod, sizeof(szMod)));
255 _dump_context ( ExceptionInfo->ContextRecord );
256 #ifdef _X86_
257 DbgPrint("Frames:\n");
258 _SEH_TRY
259 {
260 Frame = (PULONG)ExceptionInfo->ContextRecord->Ebp;
261 while (Frame[1] != 0 && Frame[1] != 0xdeadbeef)
262 {
263 if (IsBadReadPtr((PVOID)Frame[1], 4)) {
264 DbgPrint(" %8x%9s %s\n", Frame[1], "<invalid address>"," ");
265 } else {
266 _module_name_from_addr((const void*)Frame[1], &StartAddr,
267 szMod, sizeof(szMod));
268 DbgPrint(" %8x+%-8x %s\n",
269 (PVOID)StartAddr,
270 (ULONG_PTR)Frame[1] - (ULONG_PTR)StartAddr, szMod);
271 }
272 if (IsBadReadPtr((PVOID)Frame[0], sizeof(*Frame) * 2)) {
273 break;
274 }
275 Frame = (PULONG)Frame[0];
276 }
277 }
278 _SEH_HANDLE
279 {
280 DbgPrint("<error dumping stack trace: 0x%x>\n", _SEH_GetExceptionCode());
281 }
282 _SEH_END;
283 #endif
284 }
285
286 /*
287 * Returning EXCEPTION_EXECUTE_HANDLER means that the code in
288 * the __except block will be executed. Normally this will end up in a
289 * Terminate process.
290 */
291
292 return EXCEPTION_EXECUTE_HANDLER;
293 }
294
295 /*
296 * @implemented
297 */
298 VOID
299 WINAPI
300 RaiseException(IN DWORD dwExceptionCode,
301 IN DWORD dwExceptionFlags,
302 IN DWORD nNumberOfArguments,
303 IN CONST ULONG_PTR *lpArguments OPTIONAL)
304 {
305 EXCEPTION_RECORD ExceptionRecord;
306
307 /* Setup the exception record */
308 ExceptionRecord.ExceptionCode = dwExceptionCode;
309 ExceptionRecord.ExceptionRecord = NULL;
310 ExceptionRecord.ExceptionAddress = (PVOID)RaiseException;
311 ExceptionRecord.ExceptionFlags = dwExceptionFlags & EXCEPTION_NONCONTINUABLE;
312
313 /* Check if we have arguments */
314 if (!lpArguments)
315 {
316 /* We don't */
317 ExceptionRecord.NumberParameters = 0;
318 }
319 else
320 {
321 /* We do, normalize the count */
322 if (nNumberOfArguments > EXCEPTION_MAXIMUM_PARAMETERS)
323 {
324 nNumberOfArguments = EXCEPTION_MAXIMUM_PARAMETERS;
325 }
326
327 /* Set the count of parameters */
328 ExceptionRecord.NumberParameters = nNumberOfArguments;
329
330 /* Loop each parameter */
331 for (nNumberOfArguments = 0;
332 (nNumberOfArguments < ExceptionRecord.NumberParameters);
333 nNumberOfArguments ++)
334 {
335 /* Copy the exception information */
336 ExceptionRecord.ExceptionInformation[nNumberOfArguments] =
337 *lpArguments++;
338 }
339 }
340
341 /* Raise the exception */
342 RtlRaiseException(&ExceptionRecord);
343 }
344
345 /* EOF */