gcc 4.0 warning fixes.
[wine] / dlls / ntdll / exception.c
index 6a2c579..b5d5de6 100644 (file)
 
 #include <assert.h>
 #include <signal.h>
+#include <stdarg.h>
 
+#include "ntstatus.h"
 #include "windef.h"
 #include "winternl.h"
-#include "global.h"
 #include "wine/exception.h"
-#include "stackframe.h"
-#include "miscemu.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
 
-void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
-void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME, LPVOID,
-                           PEXCEPTION_RECORD, DWORD, PCONTEXT );
-void WINAPI EXC_NtRaiseException( PEXCEPTION_RECORD, PCONTEXT,
-                                  BOOL, PCONTEXT );
 
 /*******************************************************************
  *         EXC_RaiseHandler
  *
  * Handler for exceptions happening inside a handler.
  */
-static DWORD 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;
@@ -81,8 +96,8 @@ static DWORD EXC_RaiseHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
  *
  * Handler for exceptions happening inside an unwind handler.
  */
-static DWORD 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;
@@ -100,8 +115,8 @@ static DWORD EXC_UnwindHandler( EXCEPTION_RECORD *rec, EXCEPTION_FRAME *frame,
  * 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( 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;
@@ -129,6 +144,8 @@ static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *c
     int ret;
     HANDLE handle = 0;
 
+    if (!NtCurrentTeb()->Peb->BeingDebugged) return 0;  /* no debugger present */
+
     SERVER_START_REQ( queue_exception_event )
     {
         req->first   = first_chance;
@@ -137,7 +154,7 @@ static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *c
         if (!wine_server_call( req )) handle = reply->handle;
     }
     SERVER_END_REQ;
-    if (!handle) return 0;  /* no debugger present or other error */
+    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
@@ -156,6 +173,32 @@ static int send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *c
 }
 
 
+/**********************************************************************
+ *           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;
+}
+
+
 /*******************************************************************
  *         EXC_DefaultHandling
  *
@@ -167,7 +210,7 @@ static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
 
     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",
@@ -179,31 +222,49 @@ static void EXC_DefaultHandling( EXCEPTION_RECORD *rec, CONTEXT *context )
 /***********************************************************************
  *             RtlRaiseException (NTDLL.@)
  */
-DEFINE_REGS_ENTRYPOINT_1( RtlRaiseException, EXC_RtlRaiseException, EXCEPTION_RECORD * );
-void WINAPI EXC_RtlRaiseException( 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, c;
 
     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)
-        FIXME( "call to unimplemented function %s.%s\n",
-               (char*)rec->ExceptionInformation[0], (char*)rec->ExceptionInformation[1] );
+    {
+        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 */
 
-    SIGNAL_Unblock(); /* we may be in a signal handler, and exception handlers may jump out */
+    if (call_vectored_handlers( rec, context ) == EXCEPTION_CONTINUE_EXECUTION) return;
 
-    frame = NtCurrentTeb()->except;
+    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;
@@ -247,21 +308,31 @@ void WINAPI EXC_RtlRaiseException( 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
+
 
 /*******************************************************************
  *             RtlUnwind (NTDLL.@)
  */
-DEFINE_REGS_ENTRYPOINT_4( RtlUnwind, EXC_RtlUnwind,
-                          PVOID, PVOID, PEXCEPTION_RECORD, PVOID );
-void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
-                           PEXCEPTION_RECORD pRecord, DWORD returnEax,
-                           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 = returnEax;
+    context->Eax = (DWORD)returnEax;
 #endif
 
     /* build an exception record, if we do not have one */
@@ -280,8 +351,8 @@ void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
     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))
@@ -292,8 +363,8 @@ void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
             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;
@@ -324,19 +395,42 @@ void WINAPI EXC_RtlUnwind( PEXCEPTION_FRAME pEndFrame, LPVOID unusedEip,
     }
 }
 
+/**********************************************************************/
+
+#ifdef DEFINE_REGS_ENTRYPOINT
+DEFINE_REGS_ENTRYPOINT( RtlUnwind, 16, 16 );
+#else
+void WINAPI RtlUnwind( PVOID pEndFrame, PVOID unusedEip,
+                       PEXCEPTION_RECORD pRecord, PVOID returnEax )
+{
+    CONTEXT context;
+    memset( &context, 0, sizeof(context) );
+    __regs_RtlUnwind( pEndFrame, unusedEip, pRecord, returnEax, &context );
+}
+#endif
+
 
 /*******************************************************************
  *             NtRaiseException (NTDLL.@)
  */
-DEFINE_REGS_ENTRYPOINT_3( NtRaiseException, EXC_NtRaiseException,
-                          EXCEPTION_RECORD *, CONTEXT *, BOOL );
-void WINAPI EXC_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
+void WINAPI __regs_NtRaiseException( EXCEPTION_RECORD *rec, CONTEXT *ctx,
                                   BOOL first, CONTEXT *context )
 {
-    EXC_RtlRaiseException( rec, ctx );
+    __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
+
 
 /***********************************************************************
  *            RtlRaiseStatus  (NTDLL.@)
@@ -355,13 +449,56 @@ void WINAPI RtlRaiseStatus( NTSTATUS status )
 }
 
 
+/*******************************************************************
+ *         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;
+}
+
+
+/*******************************************************************
+ *         RtlRemoveVectoredExceptionHandler   (NTDLL.@)
+ */
+ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
+{
+    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;
+}
+
+
 /*************************************************************
  *            __wine_exception_handler (NTDLL.@)
  *
  * Exception handler for exception blocks declared in Wine code.
  */
-DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
-                                CONTEXT *context, LPVOID pdispatcher )
+DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
+                                CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
 {
     __WINE_FRAME *wine_frame = (__WINE_FRAME *)frame;
 
@@ -391,7 +528,7 @@ DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame
 
     RtlUnwind( frame, 0, record, 0 );
     __wine_pop_frame( frame );
-    longjmp( wine_frame->jmp, 1 );
+    siglongjmp( wine_frame->jmp, 1 );
 }
 
 
@@ -400,8 +537,8 @@ DWORD __wine_exception_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame
  *
  * Exception handler for try/finally blocks declared in Wine code.
  */
-DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
-                              CONTEXT *context, LPVOID pdispatcher )
+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))
     {
@@ -410,22 +547,3 @@ DWORD __wine_finally_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
     }
     return ExceptionContinueSearch;
 }
-
-
-/*************************************************************
- *            __wine_callto16_handler
- *
- * Handler for exceptions occurring in 16-bit code.
- */
-DWORD __wine_callto16_handler( EXCEPTION_RECORD *record, EXCEPTION_FRAME *frame,
-                               CONTEXT *context, LPVOID pdispatcher )
-{
-    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
-    {
-        /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
-        STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
-        NtCurrentTeb()->cur_stack = frame32->frame16;
-        _LeaveWin16Lock();
-    }
-    return ExceptionContinueSearch;
-}