Removed a few external dependencies from ntdll.
[wine] / dlls / ntdll / exception.c
1 /*
2  * NT exception handling routines
3  * 
4  * Copyright 1999 Turchanov Sergey
5  * Copyright 1999 Alexandre Julliard
6  */
7
8 #include <signal.h>
9 #include "winnt.h"
10 #include "ntddk.h"
11 #include "process.h"
12 #include "global.h"
13 #include "wine/exception.h"
14 #include "stackframe.h"
15 #include "miscemu.h"
16 #include "server.h"
17 #include "debugtools.h"
18
19 DEFAULT_DEBUG_CHANNEL(seh);
20
21 /* Exception record for handling exceptions happening inside exception handlers */
22 typedef struct
23 {
24     EXCEPTION_FRAME frame;
25     EXCEPTION_FRAME *prevFrame;
26 } EXC_NESTED_FRAME;
27
28  
29 /*******************************************************************
30  *         EXC_RaiseHandler
31  *
32  * Handler for exceptions happening inside a handler.
33  */
34 static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
35                                CONTEXT *context, EXCEPTION_FRAME **dispatcher )
36 {
37     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
38         return ExceptionContinueSearch;
39     /* We shouldn't get here so we store faulty frame in dispatcher */
40     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
41     return ExceptionNestedException;
42 }
43
44
45 /*******************************************************************
46  *         EXC_UnwindHandler
47  *
48  * Handler for exceptions happening inside an unwind handler.
49  */
50 static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
51                                 CONTEXT *context, EXCEPTION_FRAME **dispatcher )
52 {
53     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
54         return ExceptionContinueSearch;
55     /* We shouldn't get here so we store faulty frame in dispatcher */
56     *dispatcher = ((EXC_NESTED_FRAME*)frame)->prevFrame;
57     return ExceptionCollidedUnwind;
58 }
59
60
61 /*******************************************************************
62  *         EXC_CallHandler
63  *
64  * Call an exception handler, setting up an exception frame to catch exceptions
65  * happening during the handler execution.
66  * Please do not change the first 4 parameters order in any way - some exceptions handlers
67  * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
68  */
69 static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
70                               CONTEXT *context, EXCEPTION_FRAME **dispatcher, 
71                               PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
72 {
73     EXC_NESTED_FRAME newframe;
74     DWORD ret;
75
76     newframe.frame.Handler = nested_handler;
77     newframe.prevFrame     = frame;
78     EXC_push_frame( &newframe.frame );
79     TRACE( "calling handler at %p code=%lx flags=%lx\n",
80            handler, record->ExceptionCode, record->ExceptionFlags );
81     ret = handler( record, frame, context, dispatcher );
82     TRACE( "handler returned %lx\n", ret );
83     EXC_pop_frame( &newframe.frame );
84     return ret;
85 }
86
87
88 /**********************************************************************
89  *           EXC_SendEvent
90  *
91  * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
92  */
93 static inline int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
94 {
95     struct exception_event_request *req = get_req_buffer();
96     req->record  = *rec;
97     req->first   = first_chance;
98     req->context = *context;
99     if (!server_call_noerr( REQ_EXCEPTION_EVENT )) *context = req->context;
100     return req->status;
101 }
102
103
104 /*******************************************************************
105  *         EXC_DefaultHandling
106  *
107  * Default handling for exceptions. Called when we didn't find a suitable handler.
108  */
109 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
110 {
111     if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
112
113     if (rec->ExceptionFlags & EH_STACK_INVALID)
114         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
115     else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
116         ERR("Process attempted to continue execution after noncontinuable exception.\n");
117     else
118         ERR("Unhandled exception code %lx flags %lx addr %p\n",
119             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
120     NtTerminateProcess( NtCurrentProcess(), 1 );
121 }
122
123
124 /***********************************************************************
125  *            EXC_RtlRaiseException / RtlRaiseException (NTDLL.464)
126  */
127 DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * )
128 void WINAPI EXC_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
129 {
130     PEXCEPTION_FRAME frame, dispatch, nested_frame;
131     EXCEPTION_RECORD newrec;
132     DWORD res;
133
134     TRACE( "code=%lx flags=%lx\n", rec->ExceptionCode, rec->ExceptionFlags );
135
136     if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
137
138     frame = NtCurrentTeb()->except;
139     nested_frame = NULL;
140     while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
141     {
142         /* Check frame address */
143         if (((void*)frame < NtCurrentTeb()->stack_low) ||
144             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
145             (int)frame & 3)
146         {
147             rec->ExceptionFlags |= EH_STACK_INVALID;
148             break;
149         }
150
151         /* Call handler */
152         res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
153         if (frame == nested_frame)
154         {
155             /* no longer nested */
156             nested_frame = NULL;
157             rec->ExceptionFlags &= ~EH_NESTED_CALL;
158         }
159
160         switch(res)
161         {
162         case ExceptionContinueExecution:
163             if (!(rec->ExceptionFlags & EH_NONCONTINUABLE)) return;
164             newrec.ExceptionCode    = STATUS_NONCONTINUABLE_EXCEPTION;
165             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
166             newrec.ExceptionRecord  = rec;
167             newrec.NumberParameters = 0;
168             RtlRaiseException( &newrec );  /* never returns */
169             break;
170         case ExceptionContinueSearch:
171             break;
172         case ExceptionNestedException:
173             if (nested_frame < dispatch) nested_frame = dispatch;
174             rec->ExceptionFlags |= EH_NESTED_CALL;
175             break;
176         default:
177             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
178             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
179             newrec.ExceptionRecord  = rec;
180             newrec.NumberParameters = 0;
181             RtlRaiseException( &newrec );  /* never returns */
182             break;
183         }
184         frame = frame->Prev;
185     }
186     EXC_DefaultHandling( rec, context );
187 }
188
189
190 /*******************************************************************
191  *         EXC_RtlUnwind / RtlUnwind  (KERNEL32.590) (NTDLL.518)
192  */
193 DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind, 
194                           PEXCEPTION_FRAME, LPVOID, PEXCEPTION_RECORD, DWORD )
195 void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
196                            PEXCEPTION_RECORD pRecord, DWORD returnEax,
197                            CONTEXT *context )
198 {
199     EXCEPTION_RECORD record, newrec;
200     PEXCEPTION_FRAME frame, dispatch;
201
202 #ifdef __i386__
203     context->Eax = returnEax;
204 #endif
205
206     /* build an exception record, if we do not have one */
207     if (!pRecord)
208     {
209         record.ExceptionCode    = STATUS_UNWIND;
210         record.ExceptionFlags   = 0;
211         record.ExceptionRecord  = NULL;
212         record.ExceptionAddress = GET_IP(context); 
213         record.NumberParameters = 0;
214         pRecord = &record;
215     }
216
217     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
218
219     TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
220
221     /* get chain of exception frames */
222     frame = NtCurrentTeb()->except;
223     while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
224     {
225         /* Check frame address */
226         if (pEndFrame && (frame > pEndFrame))
227         {
228             newrec.ExceptionCode    = STATUS_INVALID_UNWIND_TARGET;
229             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
230             newrec.ExceptionRecord  = pRecord;
231             newrec.NumberParameters = 0;
232             RtlRaiseException( &newrec );  /* never returns */
233         }
234         if (((void*)frame < NtCurrentTeb()->stack_low) ||
235             ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
236             (int)frame & 3)
237         {
238             newrec.ExceptionCode    = STATUS_BAD_STACK;
239             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
240             newrec.ExceptionRecord  = pRecord;
241             newrec.NumberParameters = 0;
242             RtlRaiseException( &newrec );  /* never returns */
243         }
244
245         /* Call handler */
246         switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
247                                 frame->Handler, EXC_UnwindHandler ))
248         {
249         case ExceptionContinueSearch:
250             break;
251         case ExceptionCollidedUnwind:
252             frame = dispatch;
253             break;
254         default:
255             newrec.ExceptionCode    = STATUS_INVALID_DISPOSITION;
256             newrec.ExceptionFlags   = EH_NONCONTINUABLE;
257             newrec.ExceptionRecord  = pRecord;
258             newrec.NumberParameters = 0;
259             RtlRaiseException( &newrec );  /* never returns */
260             break;
261         }
262         frame = EXC_pop_frame( frame );
263     }
264 }
265
266
267 /*******************************************************************
268  *            EXC_NtRaiseException / NtRaiseException  (NTDLL.175)
269  */
270 DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
271                           EXCEPTION_RECORD *, CONTEXT *, BOOL )
272 void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
273                                   BOOL first, CONTEXT *context )
274 {
275     EXC_RtlRaiseException( rec, ctx );
276     *context = *ctx;
277 }
278
279
280 /***********************************************************************
281  *            RtlRaiseStatus  (NTDLL.465)
282  *
283  * Raise an exception with ExceptionCode = status
284  */
285 void WINAPI RtlRaiseStatus( NTSTATUS status )
286 {
287     EXCEPTION_RECORD ExceptionRec;
288
289     ExceptionRec.ExceptionCode    = status;
290     ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
291     ExceptionRec.ExceptionRecord  = NULL;
292     ExceptionRec.NumberParameters = 0;
293     RtlRaiseException( &ExceptionRec );
294 }