Use sigsetjmp instead of setjmp in Wine internal exception handlers to
[wine/wine64.git] / dlls / ntdll / exception.c
blob22e57857ff7a44a2bbd798705ef4e1dc567a27ce
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>
27 #include <stdarg.h>
29 #include "ntstatus.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "thread.h"
33 #include "winternl.h"
34 #include "wine/exception.h"
35 #include "wine/server.h"
36 #include "wine/debug.h"
37 #include "excpt.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41 /* Exception record for handling exceptions happening inside exception handlers */
42 typedef struct
44 EXCEPTION_REGISTRATION_RECORD frame;
45 EXCEPTION_REGISTRATION_RECORD *prevFrame;
46 } EXC_NESTED_FRAME;
48 #ifdef __i386__
49 # define GET_IP(context) ((LPVOID)(context)->Eip)
50 #elif defined(__sparc__)
51 # define GET_IP(context) ((LPVOID)(context)->pc)
52 #elif defined(__powerpc__)
53 # define GET_IP(context) ((LPVOID)(context)->Iar)
54 #else
55 # error You must define GET_IP for this CPU
56 #endif
58 void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
59 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD, LPVOID,
60 PEXCEPTION_RECORD, DWORD, PCONTEXT );
61 void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
62 BOOL, PCONTEXT );
64 /*******************************************************************
65 * EXC_RaiseHandler
67 * Handler for exceptions happening inside a handler.
69 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
70 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
72 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
73 return ExceptionContinueSearch;
74 /* We shouldn't get here so we store faulty frame in dispatcher */
75 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
76 return ExceptionNestedException;
80 /*******************************************************************
81 * EXC_UnwindHandler
83 * Handler for exceptions happening inside an unwind handler.
85 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
86 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
88 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
89 return ExceptionContinueSearch;
90 /* We shouldn't get here so we store faulty frame in dispatcher */
91 *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
92 return ExceptionCollidedUnwind;
96 /*******************************************************************
97 * EXC_CallHandler
99 * Call an exception handler, setting up an exception frame to catch exceptions
100 * happening during the handler execution.
101 * Please do not change the first 4 parameters order in any way - some exceptions handlers
102 * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
104 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
105 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
106 PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
108 EXC_NESTED_FRAME newframe;
109 DWORD ret;
111 newframe.frame.Handler = nested_handler;
112 newframe.prevFrame = frame;
113 __wine_push_frame( &newframe.frame );
114 TRACE( "calling handler at %p code=%lx flags=%lx\n",
115 handler, record->ExceptionCode, record->ExceptionFlags );
116 ret = handler( record, frame, context, dispatcher );
117 TRACE( "handler returned %lx\n", ret );
118 __wine_pop_frame( &newframe.frame );
119 return ret;
123 /**********************************************************************
124 * send_debug_event
126 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
128 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
130 int ret;
131 HANDLE handle = 0;
133 if (!NtCurrentTeb()->Peb->BeingDebugged) return 0; /* no debugger present */
135 SERVER_START_REQ( queue_exception_event )
137 req->first = first_chance;
138 wine_server_add_data( req, context, sizeof(*context) );
139 wine_server_add_data( req, rec, sizeof(*rec) );
140 if (!wine_server_call( req )) handle = reply->handle;
142 SERVER_END_REQ;
143 if (!handle) return 0;
145 /* No need to wait on the handle since the process gets suspended
146 * once the event is passed to the debugger, so when we get back
147 * here the event has been continued already.
149 SERVER_START_REQ( get_exception_status )
151 req->handle = handle;
152 wine_server_set_reply( req, context, sizeof(*context) );
153 wine_server_call( req );
154 ret = reply->status;
156 SERVER_END_REQ;
157 NtClose( handle );
158 return ret;
162 /*******************************************************************
163 * EXC_DefaultHandling
165 * Default handling for exceptions. Called when we didn't find a suitable handler.
167 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
169 if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return; /* continue execution */
171 if (rec->ExceptionFlags & EH_STACK_INVALID)
172 ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
173 else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
174 ERR("Process attempted to continue execution after noncontinuable exception.\n");
175 else
176 ERR("Unhandled exception code %lx flags %lx addr %p\n",
177 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
178 NtTerminateProcess( NtCurrentProcess(), 1 );
182 /***********************************************************************
183 * RtlRaiseException (NTDLL.@)
185 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
186 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
188 PEXCEPTION_REGISTRATION_RECORD frame, dispatch, nested_frame;
189 EXCEPTION_RECORD newrec;
190 DWORD res, c;
192 TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
193 for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
194 if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
195 FIXME( "call to unimplemented function %s.%s\n",
196 (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
198 if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return; /* continue execution */
200 SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
202 frame = NtCurrentTeb()->Tib.ExceptionList;
203 nested_frame = NULL;
204 while (frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL)
206 /* Check frame address */
207 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
208 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
209 (int)frame & 3)
211 rec->ExceptionFlags |= EH_STACK_INVALID;
212 break;
215 /* Call handler */
216 res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
217 if (frame == nested_frame)
219 /* no longer nested */
220 nested_frame = NULL;
221 rec->ExceptionFlags &= ~EH_NESTED_CALL;
224 switch(res)
226 case ExceptionContinueExecution:
227 if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
228 newrec.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
229 newrec.ExceptionFlags = EH_NONCONTINUABLE;
230 newrec.ExceptionRecord = rec;
231 newrec.NumberParameters = 0;
232 RtlRaiseException( &newrec ); /* never returns */
233 break;
234 case ExceptionContinueSearch:
235 break;
236 case ExceptionNestedException:
237 if (nested_frame < dispatch) nested_frame = dispatch;
238 rec->ExceptionFlags |= EH_NESTED_CALL;
239 break;
240 default:
241 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
242 newrec.ExceptionFlags = EH_NONCONTINUABLE;
243 newrec.ExceptionRecord = rec;
244 newrec.NumberParameters = 0;
245 RtlRaiseException( &newrec ); /* never returns */
246 break;
248 frame = frame->Prev;
250 EXC_DefaultHandling( rec, context );
254 /*******************************************************************
255 * RtlUnwind (NTDLL.@)
257 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
258 PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
259 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD pEndFrame, LPVOID unusedEip,
260 PEXCEPTION_RECORD pRecord, DWORD returnEax,
261 CONTEXT *context )
263 EXCEPTION_RECORD record, newrec;
264 PEXCEPTION_REGISTRATION_RECORD frame, dispatch;
266 #ifdef __i386__
267 context->Eax = returnEax;
268 #endif
270 /* build an exception record, if we do not have one */
271 if (!pRecord)
273 record.ExceptionCode = STATUS_UNWIND;
274 record.ExceptionFlags = 0;
275 record.ExceptionRecord = NULL;
276 record.ExceptionAddress = GET_IP(context);
277 record.NumberParameters = 0;
278 pRecord = &record;
281 pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
283 TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
285 /* get chain of exception frames */
286 frame = NtCurrentTeb()->Tib.ExceptionList;
287 while ((frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL) && (frame != pEndFrame))
289 /* Check frame address */
290 if (pEndFrame && (frame > pEndFrame))
292 newrec.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
293 newrec.ExceptionFlags = EH_NONCONTINUABLE;
294 newrec.ExceptionRecord = pRecord;
295 newrec.NumberParameters = 0;
296 RtlRaiseException( &newrec ); /* never returns */
298 if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
299 ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
300 (int)frame & 3)
302 newrec.ExceptionCode = STATUS_BAD_STACK;
303 newrec.ExceptionFlags = EH_NONCONTINUABLE;
304 newrec.ExceptionRecord = pRecord;
305 newrec.NumberParameters = 0;
306 RtlRaiseException( &newrec ); /* never returns */
309 /* Call handler */
310 switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
311 frame->Handler, EXC_UnwindHandler ))
313 case ExceptionContinueSearch:
314 break;
315 case ExceptionCollidedUnwind:
316 frame = dispatch;
317 break;
318 default:
319 newrec.ExceptionCode = STATUS_INVALID_DISPOSITION;
320 newrec.ExceptionFlags = EH_NONCONTINUABLE;
321 newrec.ExceptionRecord = pRecord;
322 newrec.NumberParameters = 0;
323 RtlRaiseException( &newrec ); /* never returns */
324 break;
326 frame = __wine_pop_frame( frame );
331 /*******************************************************************
332 * NtRaiseException (NTDLL.@)
334 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
335 EXCEPTION_RECORD *, CONTEXT *, BOOL );
336 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
337 BOOL first, CONTEXT *context )
339 EXC_RtlRaiseException( rec, ctx );
340 *context = *ctx;
344 /***********************************************************************
345 * RtlRaiseStatus (NTDLL.@)
347 * Raise an exception with ExceptionCode = status
349 void WINAPI RtlRaiseStatus( NTSTATUS status )
351 EXCEPTION_RECORD ExceptionRec;
353 ExceptionRec.ExceptionCode = status;
354 ExceptionRec.ExceptionFlags = EH_NONCONTINUABLE;
355 ExceptionRec.ExceptionRecord = NULL;
356 ExceptionRec.NumberParameters = 0;
357 RtlRaiseException( &ExceptionRec );
361 /*************************************************************
362 * __wine_exception_handler (NTDLL.@)
364 * Exception handler for exception blocks declared in Wine code.
366 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
367 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
369 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
371 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
372 return ExceptionContinueSearch;
373 if (wine_frame->u.filter)
375 EXCEPTION_POINTERS ptrs;
376 ptrs.ExceptionRecord = record;
377 ptrs.ContextRecord = context;
378 switch(wine_frame->u.filter( &ptrs ))
380 case EXCEPTION_CONTINUE_SEARCH:
381 return ExceptionContinueSearch;
382 case EXCEPTION_CONTINUE_EXECUTION:
383 return ExceptionContinueExecution;
384 case EXCEPTION_EXECUTE_HANDLER:
385 break;
386 default:
387 MESSAGE( "Invalid return value from exception filter\n" );
388 assert( FALSE );
391 /* hack to make GetExceptionCode() work in handler */
392 wine_frame->ExceptionCode = record->ExceptionCode;
393 wine_frame->ExceptionRecord = wine_frame;
395 RtlUnwind( frame, 0, record, 0 );
396 __wine_pop_frame( frame );
397 siglongjmp( wine_frame->jmp, 1 );
401 /*************************************************************
402 * __wine_finally_handler (NTDLL.@)
404 * Exception handler for try/finally blocks declared in Wine code.
406 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
407 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
409 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
411 __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
412 wine_frame->u.finally_func( FALSE );
414 return ExceptionContinueSearch;