Get rid of the argv0 and full_argv0 global variables.
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  *
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <signal.h>
27 #include <stdarg.h>
28
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"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(seh);
40
41 /* Exception record for handling exceptions happening inside exception handlers */
42 typedef struct
43 {
44     EXCEPTION_REGISTRATION_RECORD frame;
45     EXCEPTION_REGISTRATION_RECORD *prevFrame;
46 } EXC_NESTED_FRAME;
47
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
57
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 );
63
64 /*******************************************************************
65  *         EXC_RaiseHandler
66  *
67  * Handler for exceptions happening inside a handler.
68  */
69 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
70                                CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
71 {
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;
77 }
78
79
80 /*******************************************************************
81  *         EXC_UnwindHandler
82  *
83  * Handler for exceptions happening inside an unwind handler.
84  */
85 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
86                                 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
87 {
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;
93 }
94
95
96 /*******************************************************************
97  *         EXC_CallHandler
98  *
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
103  */
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)
107 {
108     EXC_NESTED_FRAME newframe;
109     DWORD ret;
110
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;
120 }
121
122
123 /**********************************************************************
124  *           send_debug_event
125  *
126  * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
127  */
128 static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
129 {
130     int ret;
131     HANDLE handle = 0;
132
133     SERVER_START_REQ( queue_exception_event )
134     {
135         req->first   = first_chance;
136         wine_server_add_data( req, context, sizeof(*context) );
137         wine_server_add_data( req, rec, sizeof(*rec) );
138         if (!wine_server_call( req )) handle = reply->handle;
139     }
140     SERVER_END_REQ;
141     if (!handle) return 0;  /* no debugger present or other error */
142
143     /* No need to wait on the handle since the process gets suspended
144      * once the event is passed to the debugger, so when we get back
145      * here the event has been continued already.
146      */
147     SERVER_START_REQ( get_exception_status )
148     {
149         req->handle = handle;
150         wine_server_set_reply( req, context, sizeof(*context) );
151         wine_server_call( req );
152         ret = reply->status;
153     }
154     SERVER_END_REQ;
155     NtClose( handle );
156     return ret;
157 }
158
159
160 /*******************************************************************
161  *         EXC_DefaultHandling
162  *
163  * Default handling for exceptions. Called when we didn't find a suitable handler.
164  */
165 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
166 {
167     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
168
169     if (rec->ExceptionFlags & EH_STACK_INVALID)
170         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
171     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
172         ERR("Process attempted to continue execution after noncontinuable exception.\n");
173     else
174         ERR("Unhandled exception code %lx flags %lx addr %p\n",
175             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
176     NtTerminateProcess( NtCurrentProcess(), 1 );
177 }
178
179
180 /***********************************************************************
181  *              RtlRaiseException (NTDLL.@)
182  */
183 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
184 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
185 {
186     PEXCEPTION_REGISTRATION_RECORD frame, dispatch, nested_frame;
187     EXCEPTION_RECORD newrec;
188     DWORD res, c;
189
190     TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
191     for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
192     if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
193         FIXME( "call to unimplemented function %s.%s\n",
194                (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
195
196     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
197
198     SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
199
200     frame = NtCurrentTeb()->Tib.ExceptionList;
201     nested_frame = NULL;
202     while (frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL)
203     {
204         /* Check frame address */
205         if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
206             ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
207             (int)frame & 3)
208         {
209             rec->ExceptionFlags |= EH_STACK_INVALID;
210             break;
211         }
212
213         /* Call handler */
214         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
215         if (frame == nested_frame)
216         {
217             /* no longer nested */
218             nested_frame = NULL;
219             rec->ExceptionFlags &= ~EH_NESTED_CALL;
220         }
221
222         switch(res)
223         {
224         case ExceptionContinueExecution:
225             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
226             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
227             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
228             newrec.ExceptionRecord  = rec;
229             newrec.NumberParameters = 0;
230             RtlRaiseException( &newrec );  /* never returns */
231             break;
232         case ExceptionContinueSearch:
233             break;
234         case ExceptionNestedException:
235             if (nested_frame < dispatch) nested_frame = dispatch;
236             rec->ExceptionFlags |= EH_NESTED_CALL;
237             break;
238         default:
239             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
240             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
241             newrec.ExceptionRecord  = rec;
242             newrec.NumberParameters = 0;
243             RtlRaiseException( &newrec );  /* never returns */
244             break;
245         }
246         frame = frame->Prev;
247     }
248     EXC_DefaultHandling( rec, context );
249 }
250
251
252 /*******************************************************************
253  *              RtlUnwind (NTDLL.@)
254  */
255 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
256                           PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
257 void WINAPI EXC_RtlUnwind( PEXCEPTION_REGISTRATION_RECORD pEndFrame, LPVOID unusedEip,
258                            PEXCEPTION_RECORD pRecord, DWORD returnEax,
259                            CONTEXT *context )
260 {
261     EXCEPTION_RECORD record, newrec;
262     PEXCEPTION_REGISTRATION_RECORD frame, dispatch;
263
264 #ifdef __i386__
265     context->Eax = returnEax;
266 #endif
267
268     /* build an exception record, if we do not have one */
269     if (!pRecord)
270     {
271         record.ExceptionCode    = STATUS_UNWIND;
272         record.ExceptionFlags   = 0;
273         record.ExceptionRecord  = NULL;
274         record.ExceptionAddress = GET_IP(context);
275         record.NumberParameters = 0;
276         pRecord = &record;
277     }
278
279     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
280
281     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
282
283     /* get chain of exception frames */
284     frame = NtCurrentTeb()->Tib.ExceptionList;
285     while ((frame != (PEXCEPTION_REGISTRATION_RECORD)~0UL) && (frame != pEndFrame))
286     {
287         /* Check frame address */
288         if (pEndFrame && (frame > pEndFrame))
289         {
290             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
291             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
292             newrec.ExceptionRecord  = pRecord;
293             newrec.NumberParameters = 0;
294             RtlRaiseException( &newrec );  /* never returns */
295         }
296         if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
297             ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
298             (int)frame & 3)
299         {
300             newrec.ExceptionCode    = STATUS_BAD_STACK;
301             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
302             newrec.ExceptionRecord  = pRecord;
303             newrec.NumberParameters = 0;
304             RtlRaiseException( &newrec );  /* never returns */
305         }
306
307         /* Call handler */
308         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
309                                 frame->Handler, EXC_UnwindHandler ))
310         {
311         case ExceptionContinueSearch:
312             break;
313         case ExceptionCollidedUnwind:
314             frame = dispatch;
315             break;
316         default:
317             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
318             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
319             newrec.ExceptionRecord  = pRecord;
320             newrec.NumberParameters = 0;
321             RtlRaiseException( &newrec );  /* never returns */
322             break;
323         }
324         frame = __wine_pop_frame( frame );
325     }
326 }
327
328
329 /*******************************************************************
330  *              NtRaiseException (NTDLL.@)
331  */
332 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
333                           EXCEPTION_RECORD *, CONTEXT *, BOOL );
334 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
335                                   BOOL first, CONTEXT *context )
336 {
337     EXC_RtlRaiseException( rec, ctx );
338     *context = *ctx;
339 }
340
341
342 /***********************************************************************
343  *            RtlRaiseStatus  (NTDLL.@)
344  *
345  * Raise an exception with ExceptionCode = status
346  */
347 void WINAPI RtlRaiseStatus( NTSTATUS status )
348 {
349     EXCEPTION_RECORD ExceptionRec;
350
351     ExceptionRec.ExceptionCode    = status;
352     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
353     ExceptionRec.ExceptionRecord  = NULL;
354     ExceptionRec.NumberParameters = 0;
355     RtlRaiseException( &ExceptionRec );
356 }
357
358
359 /*************************************************************
360  *            __wine_exception_handler (NTDLL.@)
361  *
362  * Exception handler for exception blocks declared in Wine code.
363  */
364 DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
365                                 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
366 {
367     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
368
369     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
370         return ExceptionContinueSearch;
371     if (wine_frame->u.filter)
372     {
373         EXCEPTION_POINTERS ptrs;
374         ptrs.ExceptionRecord = record;
375         ptrs.ContextRecord = context;
376         switch(wine_frame->u.filter( &ptrs ))
377         {
378         case EXCEPTION_CONTINUE_SEARCH:
379             return ExceptionContinueSearch;
380         case EXCEPTION_CONTINUE_EXECUTION:
381             return ExceptionContinueExecution;
382         case EXCEPTION_EXECUTE_HANDLER:
383             break;
384         default:
385             MESSAGE( "Invalid return value from exception filter\n" );
386             assert( FALSE );
387         }
388     }
389     /* hack to make GetExceptionCode() work in handler */
390     wine_frame->ExceptionCode   = record->ExceptionCode;
391     wine_frame->ExceptionRecord = wine_frame;
392
393     RtlUnwind( frame, 0, record, 0 );
394     __wine_pop_frame( frame );
395     longjmp( wine_frame->jmp, 1 );
396 }
397
398
399 /*************************************************************
400  *            __wine_finally_handler (NTDLL.@)
401  *
402  * Exception handler for try/finally blocks declared in Wine code.
403  */
404 DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
405                               CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
406 {
407     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
408     {
409         __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
410         wine_frame->u.finally_func( FALSE );
411     }
412     return ExceptionContinueSearch;
413 }