gcc 4.0 warning fixes.
[wine] / dlls / ntdll / exception.c
index 62fbec9..b5d5de6 100644 (file)
@@ -1,33 +1,87 @@
 /*
  * NT exception handling routines
- * 
+ *
  * Copyright 1999 Turchanov Sergey
  * Copyright 1999 Alexandre Julliard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "debugtools.h"
-#include "winnt.h"
-#include "ntddk.h"
-#include "except.h"
-#include "stackframe.h"
+#include "config.h"
+#include "wine/port.h"
 
-DEFAULT_DEBUG_CHANNEL(seh)
+#include <assert.h>
+#include <signal.h>
+#include <stdarg.h>
+
+#include "ntstatus.h"
+#include "windef.h"
+#include "winternl.h"
+#include "wine/exception.h"
+#include "wine/server.h"
+#include "wine/list.h"
+#include "wine/debug.h"
+#include "excpt.h"
+#include "ntdll_misc.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(seh);
 
 /* Exception record for handling exceptions happening inside exception handlers */
 typedef struct
 {
-    EXCEPTION_FRAME frame;
-    EXCEPTION_FRAME *prevFrame;
+    EXCEPTION_REGISTRATION_RECORD frame;
+    EXCEPTION_REGISTRATION_RECORD *prevFrame;
 } EXC_NESTED_FRAME;
 
+typedef struct
+{
+    struct list                 entry;
+    PVECTORED_EXCEPTION_HANDLER func;
+} VECTORED_HANDLER;
+
+static struct list vectored_handlers = LIST_INIT(vectored_handlers);
+
+static RTL_CRITICAL_SECTION vectored_handlers_section;
+static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
+{
+    0, 0, &vectored_handlers_section,
+    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
+      0, 0, { 0, (DWORD)(__FILE__ ": vectored_handlers_section") }
+};
+static RTL_CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
+
+#ifdef __i386__
+# define GET_IP(context) ((LPVOID)(context)->Eip)
+#elif defined(__sparc__)
+# define GET_IP(context) ((LPVOID)(context)->pc)
+#elif defined(__powerpc__)
+# define GET_IP(context) ((LPVOID)(context)->Iar)
+#elif defined(__ALPHA__)
+# define GET_IP(context) ((LPVOID)(context)->Fir)
+#else
+# error You must define GET_IP for this CPU
+#endif
+
 
 /*******************************************************************
  *         EXC_RaiseHandler
  *
  * Handler for exceptions happening inside a handler.
  */
-static DWORD CALLBACK EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
-                                        CONTEXT *context, EXCEPTION_FRAME **dispatcher )
+static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
+                               CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
 {
     if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
         return ExceptionContinueSearch;
@@ -42,8 +96,8 @@ static DWORD CALLBACK EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *
  *
  * Handler for exceptions happening inside an unwind handler.
  */
-static DWORD CALLBACK EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
-                                         CONTEXT *context, EXCEPTION_FRAME **dispatcher )
+static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_REGISTRATION_RECORD *frame,
+                                CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
 {
     if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
         return ExceptionContinueSearch;
@@ -58,21 +112,89 @@ static DWORD CALLBACK EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME
  *
  * Call an exception handler, setting up an exception frame to catch exceptions
  * happening during the handler execution.
+ * Please do not change the first 4 parameters order in any way - some exceptions handlers
+ * rely on Base Pointer (EBP) to have a fixed position related to the exception frame
  */
-static DWORD EXC_CallHandler( PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler,
-                              EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
-                              CONTEXT *context, EXCEPTION_FRAME **dispatcher )
+static DWORD EXC_CallHandler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
+                              CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher,
+                              PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nested_handler)
 {
     EXC_NESTED_FRAME newframe;
     DWORD ret;
 
     newframe.frame.Handler = nested_handler;
     newframe.prevFrame     = frame;
-    EXC_push_frame( &newframe.frame );
-    TRACE( "calling handler at %p\n", handler );
+    __wine_push_frame( &newframe.frame );
+    TRACE( "calling handler at %p code=%lx flags=%lx\n",
+           handler, record->ExceptionCode, record->ExceptionFlags );
     ret = handler( record, frame, context, dispatcher );
     TRACE( "handler returned %lx\n", ret );
-    EXC_pop_frame( &newframe.frame );
+    __wine_pop_frame( &newframe.frame );
+    return ret;
+}
+
+
+/**********************************************************************
+ *           send_debug_event
+ *
+ * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
+ */
+static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
+{
+    int ret;
+    HANDLE handle = 0;
+
+    if (!NtCurrentTeb()->Peb->BeingDebugged) return 0;  /* no debugger present */
+
+    SERVER_START_REQ( queue_exception_event )
+    {
+        req->first   = first_chance;
+        wine_server_add_data( req, context, sizeof(*context) );
+        wine_server_add_data( req, rec, sizeof(*rec) );
+        if (!wine_server_call( req )) handle = reply->handle;
+    }
+    SERVER_END_REQ;
+    if (!handle) return 0;
+
+    /* No need to wait on the handle since the process gets suspended
+     * once the event is passed to the debugger, so when we get back
+     * here the event has been continued already.
+     */
+    SERVER_START_REQ( get_exception_status )
+    {
+        req->handle = handle;
+        wine_server_set_reply( req, context, sizeof(*context) );
+        wine_server_call( req );
+        ret = reply->status;
+    }
+    SERVER_END_REQ;
+    NtClose( handle );
+    return ret;
+}
+
+
+/**********************************************************************
+ *           call_vectored_handlers
+ *
+ * Call the vectored handlers chain.
+ */
+static LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
+{
+    struct list *ptr;
+    LONG ret = EXCEPTION_CONTINUE_SEARCH;
+    EXCEPTION_POINTERS except_ptrs;
+
+    except_ptrs.ExceptionRecord = rec;
+    except_ptrs.ContextRecord = context;
+
+    RtlEnterCriticalSection( &vectored_handlers_section );
+    LIST_FOR_EACH( ptr, &vectored_handlers )
+    {
+        VECTORED_HANDLER *handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
+        ret = handler->func( &except_ptrs );
+        if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
+    }
+    RtlLeaveCriticalSection( &vectored_handlers_section );
     return ret;
 }
 
@@ -84,44 +206,72 @@ static DWORD EXC_CallHandler( PEXCEPTION_HANDLER handler, PEXCEPTION_HANDLER nes
  */
 static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
 {
+    if (send_debug_event( rec, FALSE, context ) == DBG_CONTINUE) return;  /* continue execution */
+
     if (rec->ExceptionFlags & EH_STACK_INVALID)
         ERR("Exception frame is not in stack limits => unable to dispatch exception.\n");
-    else if (rec->ExceptionCode == EXCEPTION_NONCONTINUABLE_EXCEPTION)
+    else if (rec->ExceptionCode == STATUS_NONCONTINUABLE_EXCEPTION)
         ERR("Process attempted to continue execution after noncontinuable exception.\n");
     else
         ERR("Unhandled exception code %lx flags %lx addr %p\n",
             rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
-    /* Should I add here a back trace ? */
-    TerminateProcess( GetCurrentProcess(), 1 );
+    NtTerminateProcess( NtCurrentProcess(), 1 );
 }
 
 
-/*******************************************************************
- *         EXC_RaiseException
- *
- * Implementation of NtRaiseException.
+/***********************************************************************
+ *             RtlRaiseException (NTDLL.@)
  */
-static void EXC_RaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
+void WINAPI __regs_RtlRaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
 {
-    PEXCEPTION_FRAME frame, dispatch, nested_frame;
+    EXCEPTION_REGISTRATION_RECORD *frame, *dispatch, *nested_frame;
     EXCEPTION_RECORD newrec;
-    DWORD res;
+    DWORD res, c;
 
-    frame = NtCurrentTeb()->except;
+    TRACE( "code=%lx flags=%lx addr=%p\n", rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress );
+    for (c=0; c<rec->NumberParameters; c++) TRACE(" info[%ld]=%08lx\n", c, rec->ExceptionInformation[c]);
+    if (rec->ExceptionCode == EXCEPTION_WINE_STUB)
+    {
+        if (HIWORD(rec->ExceptionInformation[1]))
+            MESSAGE( "wine: Call from %p to unimplemented function %s.%s, aborting\n",
+                   rec->ExceptionAddress,
+                   (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
+        else
+            MESSAGE( "wine: Call from %p to unimplemented function %s.%ld, aborting\n",
+                   rec->ExceptionAddress,
+                   (char*)rec->ExceptionInformation[0], rec->ExceptionInformation[1] );
+    }
+#ifdef __i386__
+    else
+    {
+        TRACE(" eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\n",
+              context->Eax, context->Ebx, context->Ecx,
+              context->Edx, context->Esi, context->Edi );
+        TRACE(" ebp=%08lx esp=%08lx cs=%04lx ds=%04lx es=%04lx fs=%04lx gs=%04lx flags=%08lx\n",
+              context->Ebp, context->Esp, context->SegCs, context->SegDs,
+              context->SegEs, context->SegFs, context->SegGs, context->EFlags );
+    }
+#endif
+
+    if (send_debug_event( rec, TRUE, context ) == DBG_CONTINUE) return;  /* continue execution */
+
+    if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
+
+    frame = NtCurrentTeb()->Tib.ExceptionList;
     nested_frame = NULL;
-    while (frame != (PEXCEPTION_FRAME)0xFFFFFFFF)
+    while (frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL)
     {
         /* Check frame address */
-        if (((void*)frame < NtCurrentTeb()->stack_low) ||
-            ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
-            (int)frame & 3)
+        if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
+            ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
+            (ULONG_PTR)frame & 3)
         {
             rec->ExceptionFlags |= EH_STACK_INVALID;
             break;
         }
 
         /* Call handler */
-        res = EXC_CallHandler( frame->Handler, EXC_RaiseHandler, rec, frame, context, &dispatch );
+        res = EXC_CallHandler( rec, frame, context, &dispatch, frame->Handler, EXC_RaiseHandler );
         if (frame == nested_frame)
         {
             /* no longer nested */
@@ -158,17 +308,32 @@ static void EXC_RaiseException( EXCEPTION_RECORD *rec, CONTEXT *context )
     EXC_DefaultHandling( rec, context );
 }
 
+/**********************************************************************/
+
+#ifdef DEFINE_REGS_ENTRYPOINT
+DEFINE_REGS_ENTRYPOINT( RtlRaiseException, 4, 4 );
+#else
+void WINAPI RtlRaiseException( EXCEPTION_RECORD *rec )
+{
+    CONTEXT context;
+    memset( &context, 0, sizeof(context) );
+    __regs_RtlRaiseException( rec, &context );
+}
+#endif
+
 
 /*******************************************************************
- *         EXC_RtlUnwind
- *
- * Implementation of RtlUnwind.
+ *             RtlUnwind (NTDLL.@)
  */
-static void EXC_RtlUnwind( EXCEPTION_FRAME *pEndFrame, EXCEPTION_RECORD *pRecord,
-                           CONTEXT *context )
+void WINAPI __regs_RtlUnwind( EXCEPTION_REGISTRATION_RECORD* pEndFrame, PVOID unusedEip,
+                              PEXCEPTION_RECORD pRecord, PVOID returnEax, CONTEXT *context )
 {
     EXCEPTION_RECORD record, newrec;
-    PEXCEPTION_FRAME frame, dispatch;
+    EXCEPTION_REGISTRATION_RECORD *frame, *dispatch;
+
+#ifdef __i386__
+    context->Eax = (DWORD)returnEax;
+#endif
 
     /* build an exception record, if we do not have one */
     if (!pRecord)
@@ -176,16 +341,18 @@ static void EXC_RtlUnwind( EXCEPTION_FRAME *pEndFrame, EXCEPTION_RECORD *pRecord
         record.ExceptionCode    = STATUS_UNWIND;
         record.ExceptionFlags   = 0;
         record.ExceptionRecord  = NULL;
-        record.ExceptionAddress = (LPVOID)EIP_reg(context); 
+        record.ExceptionAddress = GET_IP(context);
         record.NumberParameters = 0;
         pRecord = &record;
     }
 
     pRecord->ExceptionFlags |= EH_UNWINDING | (pEndFrame ? 0 : EH_EXIT_UNWIND);
-  
+
+    TRACE( "code=%lx flags=%lx\n", pRecord->ExceptionCode, pRecord->ExceptionFlags );
+
     /* get chain of exception frames */
-    frame = NtCurrentTeb()->except;
-    while ((frame != (PEXCEPTION_FRAME)0xffffffff) && (frame != pEndFrame))
+    frame = NtCurrentTeb()->Tib.ExceptionList;
+    while ((frame != (EXCEPTION_REGISTRATION_RECORD*)~0UL) && (frame != pEndFrame))
     {
         /* Check frame address */
         if (pEndFrame && (frame > pEndFrame))
@@ -196,8 +363,8 @@ static void EXC_RtlUnwind( EXCEPTION_FRAME *pEndFrame, EXCEPTION_RECORD *pRecord
             newrec.NumberParameters = 0;
             RtlRaiseException( &newrec );  /* never returns */
         }
-        if (((void*)frame < NtCurrentTeb()->stack_low) ||
-            ((void*)(frame+1) > NtCurrentTeb()->stack_top) ||
+        if (((void*)frame < NtCurrentTeb()->Tib.StackLimit) ||
+            ((void*)(frame+1) > NtCurrentTeb()->Tib.StackBase) ||
             (int)frame & 3)
         {
             newrec.ExceptionCode    = STATUS_BAD_STACK;
@@ -208,8 +375,8 @@ static void EXC_RtlUnwind( EXCEPTION_FRAME *pEndFrame, EXCEPTION_RECORD *pRecord
         }
 
         /* Call handler */
-        switch(EXC_CallHandler( frame->Handler, EXC_UnwindHandler, pRecord,
-                                frame, context, &dispatch ))
+        switch(EXC_CallHandler( pRecord, frame, context, &dispatch,
+                                frame->Handler, EXC_UnwindHandler ))
         {
         case ExceptionContinueSearch:
             break;
@@ -224,94 +391,159 @@ static void EXC_RtlUnwind( EXCEPTION_FRAME *pEndFrame, EXCEPTION_RECORD *pRecord
             RtlRaiseException( &newrec );  /* never returns */
             break;
         }
-        NtCurrentTeb()->except = frame = frame->Prev;
+        frame = __wine_pop_frame( frame );
     }
 }
 
+/**********************************************************************/
 
-/*******************************************************************
- *         NtRaiseException    (NTDLL.175)
- *
- * Real prototype:
- *    DWORD WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first );
- */
-REGS_ENTRYPOINT(NtRaiseException)
+#ifdef DEFINE_REGS_ENTRYPOINT
+DEFINE_REGS_ENTRYPOINT( RtlUnwind, 16, 16 );
+#else
+void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
+                       PEXCEPTION_RECORD pRecord, PVOID returnEax )
 {
-    DWORD ret;
-    EXCEPTION_RECORD *rec;
-    CONTEXT *ctx;
-    BOOL first;
+    CONTEXT context;
+    memset( &context, 0, sizeof(context) );
+    __regs_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
+}
+#endif
 
-    ret   = STACK32_POP(context);  /* return addr */
-    rec   = (PEXCEPTION_RECORD)STACK32_POP(context);
-    ctx   = (PCONTEXT)STACK32_POP(context);
-    first = (BOOL)STACK32_POP(context);
-    STACK32_PUSH(context,ret);  /* restore return addr */
 
-    EXC_RaiseException( rec, context );
+/*******************************************************************
+ *             NtRaiseException (NTDLL.@)
+ */
+void WINAPI __regs_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
+                                  BOOL first, CONTEXT *context )
+{
+    __regs_RtlRaiseException( rec, ctx );
     *context = *ctx;
 }
 
+#ifdef DEFINE_REGS_ENTRYPOINT
+DEFINE_REGS_ENTRYPOINT( NtRaiseException, 12, 12 );
+#else
+void WINAPI NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx, BOOL first )
+{
+    CONTEXT context;
+    memset( &context, 0, sizeof(context) );
+    __regs_NtRaiseException( rec, ctx, first, &context );
+}
+#endif
+
 
 /***********************************************************************
- *            RtlRaiseException  (NTDLL.464)
+ *            RtlRaiseStatus  (NTDLL.@)
  *
- * Real prototype:
- *     void WINAPI RtlRaiseException(PEXCEPTION_RECORD pRecord)
+ * Raise an exception with ExceptionCode = status
  */
-REGS_ENTRYPOINT(RtlRaiseException)
+void WINAPI RtlRaiseStatus( NTSTATUS status )
 {
-    EXCEPTION_RECORD *rec;
-    DWORD ret;
+    EXCEPTION_RECORD ExceptionRec;
+
+    ExceptionRec.ExceptionCode    = status;
+    ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
+    ExceptionRec.ExceptionRecord  = NULL;
+    ExceptionRec.NumberParameters = 0;
+    RtlRaiseException( &ExceptionRec );
+}
 
-    ret = STACK32_POP(context);  /* return addr */
-    rec = (PEXCEPTION_RECORD)STACK32_POP(context);
-    STACK32_PUSH(context,ret);  /* restore return addr */
 
-    rec->ExceptionAddress = (LPVOID)EIP_reg(context);
-    EXC_RaiseException( rec, context );
+/*******************************************************************
+ *         RtlAddVectoredExceptionHandler   (NTDLL.@)
+ */
+PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
+{
+    VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
+    if (handler)
+    {
+        handler->func = func;
+        RtlEnterCriticalSection( &vectored_handlers_section );
+        if (first) list_add_head( &vectored_handlers, &handler->entry );
+        else list_add_tail( &vectored_handlers, &handler->entry );
+        RtlLeaveCriticalSection( &vectored_handlers_section );
+    }
+    return handler;
 }
 
 
 /*******************************************************************
- *         RtlUnwind  (KERNEL32.590) (NTDLL.518)
- *
- *  This function is undocumented. This is the general idea of 
- *  RtlUnwind, though. Note that error handling is not yet implemented.
- *
- * The real prototype is:
- * void WINAPI RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip, 
- *                        PEXCEPTION_RECORD pRecord, DWORD returnEax );
+ *         RtlRemoveVectoredExceptionHandler   (NTDLL.@)
  */
-REGS_ENTRYPOINT(RtlUnwind)
+ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
 {
-    PEXCEPTION_FRAME pEndFrame;
-    PEXCEPTION_RECORD pRecord;
-
-    /* get the arguments from the stack */
-    DWORD ret        = STACK32_POP(context);  /* return addr */
-    pEndFrame        = (PEXCEPTION_FRAME)STACK32_POP(context);
-    (void)STACK32_POP(context);  /* unused arg */
-    pRecord          = (PEXCEPTION_RECORD)STACK32_POP(context);
-    EAX_reg(context) = STACK32_POP(context);
-    STACK32_PUSH(context,ret);  /* restore return addr */
-
-    EXC_RtlUnwind( pEndFrame, pRecord, context );
+    struct list *ptr;
+    ULONG ret = FALSE;
+
+    RtlEnterCriticalSection( &vectored_handlers_section );
+    LIST_FOR_EACH( ptr, &vectored_handlers )
+    {
+        VECTORED_HANDLER *curr_handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
+        if (curr_handler == handler)
+        {
+            list_remove( ptr );
+            ret = TRUE;
+            break;
+        }
+    }
+    RtlLeaveCriticalSection( &vectored_handlers_section );
+    if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
+    return ret;
 }
 
 
-/***********************************************************************
- *            RtlRaiseStatus  (NTDLL.465)
+/*************************************************************
+ *            __wine_exception_handler (NTDLL.@)
  *
- * Raise an exception with ExceptionCode = status
+ * Exception handler for exception blocks declared in Wine code.
  */
-void WINAPI RtlRaiseStatus( NTSTATUS status )
+DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
+                                CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
 {
-    EXCEPTION_RECORD ExceptionRec;
+    __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
 
-    ExceptionRec.ExceptionCode    = status;
-    ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
-    ExceptionRec.ExceptionRecord  = NULL;
-    ExceptionRec.NumberParameters = 0;
-    RtlRaiseException( &ExceptionRec );
+    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))
+        return ExceptionContinueSearch;
+    if (wine_frame->u.filter)
+    {
+        EXCEPTION_POINTERS ptrs;
+        ptrs.ExceptionRecord = record;
+        ptrs.ContextRecord = context;
+        switch(wine_frame->u.filter( &ptrs ))
+        {
+        case EXCEPTION_CONTINUE_SEARCH:
+            return ExceptionContinueSearch;
+        case EXCEPTION_CONTINUE_EXECUTION:
+            return ExceptionContinueExecution;
+        case EXCEPTION_EXECUTE_HANDLER:
+            break;
+        default:
+            MESSAGE( "Invalid return value from exception filter\n" );
+            assert( FALSE );
+        }
+    }
+    /* hack to make GetExceptionCode() work in handler */
+    wine_frame->ExceptionCode   = record->ExceptionCode;
+    wine_frame->ExceptionRecord = wine_frame;
+
+    RtlUnwind( frame, 0, record, 0 );
+    __wine_pop_frame( frame );
+    siglongjmp( wine_frame->jmp, 1 );
+}
+
+
+/*************************************************************
+ *            __wine_finally_handler (NTDLL.@)
+ *
+ * Exception handler for try/finally blocks declared in Wine code.
+ */
+DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
+                              CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
+{
+    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
+    {
+        __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
+        wine_frame->u.finally_func( FALSE );
+    }
+    return ExceptionContinueSearch;
 }