Protect BeginPaint and EndPaint from lps being NULL.
[wine/multimedia.git] / dlls / ntdll / exception.c
blob05aec881bf686bab9ecfb402a63d85c99b9bbc00
1 /*
2 * NT exception handling routines
4 * Copyright 1999 Turchanov Sergey
5 * Copyright 1999 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <signal.h>
28 #include "windef.h"
29 #include "winternl.h"
30 #include "global.h"
31 #include "wine/exception.h"
32 #include "stackframe.h"
33 #include "miscemu.h"
34 #include "wine/server.h"
35 #include "wine/debug.h"
36 #include "excpt.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40 /* Exception record for handling exceptions happening inside exception handlers */
41 typedef struct
43 EXCEPTION_FRAME frame;
44 EXCEPTION_FRAME *prevFrame;
45 } EXC_NESTED_FRAME;
47 #ifdef __i386__
48 # define GET_IP(context) ((LPVOID)(context)->Eip)
49 #elif defined(__sparc__)
50 # define GET_IP(context) ((LPVOID)(context)->pc)
51 #elif defined(__powerpc__)
52 # define GET_IP(context) ((LPVOID)(context)->Iar)
53 #else
54 # error You must define GET_IP for this CPU
55 #endif
57 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
58 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
59 PEXCEPTION_RECORD, DWORD, PCONTEXT );
60 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
61 BOOL, PCONTEXT );
63 /*******************************************************************
64 * EXC_RaiseHandler
66 * Handler for exceptions happening inside a handler.
68 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
69 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
71 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
72 return ExceptionContinueSearch;
73 /* We shouldn't get here so we store faulty frame in dispatcher */
74 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
75 return ExceptionNestedException;
79 /*******************************************************************
80 * EXC_UnwindHandler
82 * Handler for exceptions happening inside an unwind handler.
84 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
85 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
87 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
88 return ExceptionContinueSearch;
89 /* We shouldn't get here so we store faulty frame in dispatcher */
90 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
91 return ExceptionCollidedUnwind;
95 /*******************************************************************
96 * EXC_CallHandler
98 * Call an exception handler, setting up an exception frame to catch exceptions
99 * happening during the handler execution.
100 * Please do not change the first 4 parameters order in any way - some exceptions handlers
101 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
103 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
104 CONTEXT *context, EXCEPTION_FRAME **dispatcher,
105 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
107 EXC_NESTED_FRAME newframe;
108 DWORD ret;
110 newframe.frame.Handler = nested_handler;
111 newframe.prevFrame = frame;
112 __wine_push_frame( &newframe.frame );
113 TRACE( "calling handler at %p code=%lx flags=%lx\n",
114 handler, record->ExceptionCode, record->ExceptionFlags );
115 ret = handler( record, frame, context, dispatcher );
116 TRACE( "handler returned %lx\n", ret );
117 __wine_pop_frame( &newframe.frame );
118 return ret;
122 /**********************************************************************
123 * send_debug_event
125 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
127 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
129 int ret;
130 HANDLE handle = 0;
132 SERVER_START_REQ( queue_exception_event )
134 req->first = first_chance;
135 wine_server_add_data( req, context, sizeof(*context) );
136 wine_server_add_data( req, rec, sizeof(*rec) );
137 if (!wine_server_call( req )) handle = reply->handle;
139 SERVER_END_REQ;
140 if (!handle) return 0; /* no debugger present or other error */
142 /* No need to wait on the handle since the process gets suspended
143 * once the event is passed to the debugger, so when we get back
144 * here the event has been continued already.
146 SERVER_START_REQ( get_exception_status )
148 req->handle = handle;
149 wine_server_set_reply( req, context, sizeof(*context) );
150 wine_server_call( req );
151 ret = reply->status;
153 SERVER_END_REQ;
154 NtClose( handle );
155 return ret;
159 /*******************************************************************
160 * EXC_DefaultHandling
162 * Default handling for exceptions. Called when we didn't find a suitable handler.
164 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
166 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
168 if (rec->ExceptionFlags & EH_STACK_INVALID)
169 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
170 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
171 ERR("Process attempted to continue execution after noncontinuable exception.\n");
172 else
173 ERR("Unhandled exception code %lx flags %lx addr %p\n",
174 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
175 NtTerminateProcess( NtCurrentProcess(), 1 );
179 /***********************************************************************
180 * RtlRaiseException (NTDLL.@)
182 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
183 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
185 PEXCEPTION_FRAME frame, dispatch, nested_frame;
186 EXCEPTION_RECORD newrec;
187 DWORD res, c;
189 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
190 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
191 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
192 FIXME( "call to unimplemented function %s.%s\n",
193 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
195 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
197 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
199 frame = NtCurrentTeb()->except;
200 nested_frame = NULL;
201 while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
203 /* Check frame address */
204 if (((void*)frame < NtCurrentTeb()->stack_low) ||
205 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
206 (int)frame & 3)
208 rec->ExceptionFlags |= EH_STACK_INVALID;
209 break;
212 /* Call handler */
213 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
214 if (frame == nested_frame)
216 /* no longer nested */
217 nested_frame = NULL;
218 rec->ExceptionFlags &= ~EH_NESTED_CALL;
221 switch(res)
223 case ExceptionContinueExecution:
224 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
225 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
226 newrec.ExceptionFlags = EH_NONCONTINUABLE;
227 newrec.ExceptionRecord = rec;
228 newrec.NumberParameters = 0;
229 RtlRaiseException( &newrec ); /* never returns */
230 break;
231 case ExceptionContinueSearch:
232 break;
233 case ExceptionNestedException:
234 if (nested_frame < dispatch) nested_frame = dispatch;
235 rec->ExceptionFlags |= EH_NESTED_CALL;
236 break;
237 default:
238 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
239 newrec.ExceptionFlags = EH_NONCONTINUABLE;
240 newrec.ExceptionRecord = rec;
241 newrec.NumberParameters = 0;
242 RtlRaiseException( &newrec ); /* never returns */
243 break;
245 frame = frame->Prev;
247 EXC_DefaultHandling( rec, context );
251 /*******************************************************************
252 * RtlUnwind (NTDLL.@)
254 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
255 PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
256 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
257 PEXCEPTION_RECORD pRecord, DWORD returnEax,
258 CONTEXT *context )
260 EXCEPTION_RECORD record, newrec;
261 PEXCEPTION_FRAME frame, dispatch;
263 #ifdef __i386__
264 context->Eax = returnEax;
265 #endif
267 /* build an exception record, if we do not have one */
268 if (!pRecord)
270 record.ExceptionCode = STATUS_UNWIND;
271 record.ExceptionFlags = 0;
272 record.ExceptionRecord = NULL;
273 record.ExceptionAddress = GET_IP(context);
274 record.NumberParameters = 0;
275 pRecord = &record;
278 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
280 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
282 /* get chain of exception frames */
283 frame = NtCurrentTeb()->except;
284 while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
286 /* Check frame address */
287 if (pEndFrame && (frame > pEndFrame))
289 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
290 newrec.ExceptionFlags = EH_NONCONTINUABLE;
291 newrec.ExceptionRecord = pRecord;
292 newrec.NumberParameters = 0;
293 RtlRaiseException( &newrec ); /* never returns */
295 if (((void*)frame < NtCurrentTeb()->stack_low) ||
296 ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
297 (int)frame & 3)
299 newrec.ExceptionCode = STATUS_BAD_STACK;
300 newrec.ExceptionFlags = EH_NONCONTINUABLE;
301 newrec.ExceptionRecord = pRecord;
302 newrec.NumberParameters = 0;
303 RtlRaiseException( &newrec ); /* never returns */
306 /* Call handler */
307 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
308 frame->Handler, EXC_UnwindHandler ))
310 case ExceptionContinueSearch:
311 break;
312 case ExceptionCollidedUnwind:
313 frame = dispatch;
314 break;
315 default:
316 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
317 newrec.ExceptionFlags = EH_NONCONTINUABLE;
318 newrec.ExceptionRecord = pRecord;
319 newrec.NumberParameters = 0;
320 RtlRaiseException( &newrec ); /* never returns */
321 break;
323 frame = __wine_pop_frame( frame );
328 /*******************************************************************
329 * NtRaiseException (NTDLL.@)
331 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
332 EXCEPTION_RECORD *, CONTEXT *, BOOL );
333 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
334 BOOL first, CONTEXT *context )
336 EXC_RtlRaiseException( rec, ctx );
337 *context = *ctx;
341 /***********************************************************************
342 * RtlRaiseStatus (NTDLL.@)
344 * Raise an exception with ExceptionCode = status
346 void WINAPI RtlRaiseStatus( NTSTATUS status )
348 EXCEPTION_RECORD ExceptionRec;
350 ExceptionRec.ExceptionCode = status;
351 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
352 ExceptionRec.ExceptionRecord = NULL;
353 ExceptionRec.NumberParameters = 0;
354 RtlRaiseException( &ExceptionRec );
358 /*************************************************************
359 * __wine_exception_handler (NTDLL.@)
361 * Exception handler for exception blocks declared in Wine code.
363 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
364 CONTEXT *context, LPVOID pdispatcher )
366 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
368 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
369 return ExceptionContinueSearch;
370 if (wine_frame->u.filter)
372 EXCEPTION_POINTERS ptrs;
373 ptrs.ExceptionRecord = record;
374 ptrs.ContextRecord = context;
375 switch(wine_frame->u.filter( &ptrs ))
377 case EXCEPTION_CONTINUE_SEARCH:
378 return ExceptionContinueSearch;
379 case EXCEPTION_CONTINUE_EXECUTION:
380 return ExceptionContinueExecution;
381 case EXCEPTION_EXECUTE_HANDLER:
382 break;
383 default:
384 MESSAGE( "Invalid return value from exception filter\n" );
385 assert( FALSE );
388 /* hack to make GetExceptionCode() work in handler */
389 wine_frame->ExceptionCode = record->ExceptionCode;
390 wine_frame->ExceptionRecord = wine_frame;
392 RtlUnwind( frame, 0, record, 0 );
393 __wine_pop_frame( frame );
394 longjmp( wine_frame->jmp, 1 );
398 /*************************************************************
399 * __wine_finally_handler (NTDLL.@)
401 * Exception handler for try/finally blocks declared in Wine code.
403 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
404 CONTEXT *context, LPVOID pdispatcher )
406 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
408 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
409 wine_frame->u.finally_func( FALSE );
411 return ExceptionContinueSearch;
415 /*************************************************************
416 * __wine_callto16_handler
418 * Handler for exceptions occuring in 16-bit code.
420 DWORD __wine_callto16_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
421 CONTEXT *context, LPVOID pdispatcher )
423 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
425 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
426 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
427 NtCurrentTeb()->cur_stack = frame32->frame16;
428 _LeaveWin16Lock();
430 return ExceptionContinueSearch;