exception.c 8.97 KB
Newer Older
1 2
/*
 * NT exception handling routines
3
 *
4 5
 * Copyright 1999 Turchanov Sergey
 * Copyright 1999 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#include "config.h"
23
#include "wine/port.h"
24

25
#include <assert.h>
26
#include <errno.h>
27
#include <signal.h>
28
#include <stdarg.h>
29

30
#include "ntstatus.h"
31
#define WIN32_NO_STATUS
32
#include "windef.h"
33
#include "winternl.h"
34
#include "wine/exception.h"
35
#include "wine/server.h"
36
#include "wine/list.h"
37
#include "wine/debug.h"
38
#include "excpt.h"
39
#include "ntdll_misc.h"
40

41
WINE_DEFAULT_DEBUG_CHANNEL(seh);
42

43 44 45 46
typedef struct
{
    struct list                 entry;
    PVECTORED_EXCEPTION_HANDLER func;
47
    ULONG                       count;
48 49 50 51
} VECTORED_HANDLER;

static struct list vectored_handlers = LIST_INIT(vectored_handlers);

52 53
static RTL_CRITICAL_SECTION vectored_handlers_section;
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
54
{
55 56 57 58 59
    0, 0, &vectored_handlers_section,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": vectored_handlers_section") }
};
static RTL_CRITICAL_SECTION vectored_handlers_section = { &critsect_debug, -1, 0, 0, 0, 0 };
60

61 62 63 64 65 66 67 68
/**********************************************************************
 *           wait_suspend
 *
 * Wait until the thread is no longer suspended.
 */
void wait_suspend( CONTEXT *context )
{
    LARGE_INTEGER timeout;
69
    int saved_errno = errno;
70 71 72
    context_t server_context;

    context_to_server( &server_context, context );
73 74

    /* store the context we got at suspend time */
75
    SERVER_START_REQ( set_suspend_context )
76
    {
77
        wine_server_add_data( req, &server_context, sizeof(server_context) );
78 79 80 81 82 83
        wine_server_call( req );
    }
    SERVER_END_REQ;

    /* wait with 0 timeout, will only return once the thread is no longer suspended */
    timeout.QuadPart = 0;
84
    NTDLL_wait_for_multiple_objects( 0, NULL, SELECT_INTERRUPTIBLE, &timeout, 0 );
85 86

    /* retrieve the new context */
87
    SERVER_START_REQ( get_suspend_context )
88
    {
89
        wine_server_set_reply( req, &server_context, sizeof(server_context) );
90 91 92
        wine_server_call( req );
    }
    SERVER_END_REQ;
93

94
    context_from_server( context, &server_context );
95
    errno = saved_errno;
96 97 98
}


99
/**********************************************************************
100
 *           send_debug_event
101 102 103
 *
 * Send an EXCEPTION_DEBUG_EVENT event to the debugger.
 */
104
NTSTATUS send_debug_event( EXCEPTION_RECORD *rec, int first_chance, CONTEXT *context )
105
{
106
    NTSTATUS ret;
107
    DWORD i;
108
    HANDLE handle = 0;
109
    client_ptr_t params[EXCEPTION_MAXIMUM_PARAMETERS];
110
    context_t server_context;
111

112 113
    if (!NtCurrentTeb()->Peb->BeingDebugged) return 0;  /* no debugger present */

114 115 116
    for (i = 0; i < min( rec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS ); i++)
        params[i] = rec->ExceptionInformation[i];

117 118
    context_to_server( &server_context, context );

119
    SERVER_START_REQ( queue_exception_event )
120 121
    {
        req->first   = first_chance;
122 123 124 125 126 127
        req->code    = rec->ExceptionCode;
        req->flags   = rec->ExceptionFlags;
        req->record  = wine_server_client_ptr( rec->ExceptionRecord );
        req->address = wine_server_client_ptr( rec->ExceptionAddress );
        req->len     = i * sizeof(params[0]);
        wine_server_add_data( req, params, req->len );
128
        wine_server_add_data( req, &server_context, sizeof(server_context) );
129
        if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
130
    }
131
    SERVER_END_REQ;
132
    if (!handle) return 0;
133

134
    NTDLL_wait_for_multiple_objects( 1, &handle, SELECT_INTERRUPTIBLE, NULL, 0 );
135

136
    SERVER_START_REQ( get_exception_status )
137
    {
138
        req->handle = wine_server_obj_handle( handle );
139
        wine_server_set_reply( req, &server_context, sizeof(server_context) );
140
        ret = wine_server_call( req );
141
    }
142
    SERVER_END_REQ;
143
    if (ret >= 0) context_from_server( context, &server_context );
144
    return ret;
145 146 147
}


148 149 150 151 152
/**********************************************************************
 *           call_vectored_handlers
 *
 * Call the vectored handlers chain.
 */
153
LONG call_vectored_handlers( EXCEPTION_RECORD *rec, CONTEXT *context )
154 155 156 157
{
    struct list *ptr;
    LONG ret = EXCEPTION_CONTINUE_SEARCH;
    EXCEPTION_POINTERS except_ptrs;
158
    VECTORED_HANDLER *handler, *to_free = NULL;
159 160 161 162

    except_ptrs.ExceptionRecord = rec;
    except_ptrs.ContextRecord = context;

163
    RtlEnterCriticalSection( &vectored_handlers_section );
164 165
    ptr = list_head( &vectored_handlers );
    while (ptr)
166
    {
167 168 169 170 171 172
        handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
        handler->count++;
        RtlLeaveCriticalSection( &vectored_handlers_section );
        RtlFreeHeap( GetProcessHeap(), 0, to_free );
        to_free = NULL;

173 174
        TRACE( "calling handler at %p code=%x flags=%x\n",
               handler->func, rec->ExceptionCode, rec->ExceptionFlags );
175
        ret = handler->func( &except_ptrs );
176
        TRACE( "handler at %p returned %x\n", handler->func, ret );
177 178 179 180 181 182 183 184

        RtlEnterCriticalSection( &vectored_handlers_section );
        ptr = list_next( &vectored_handlers, ptr );
        if (!--handler->count)  /* removed during execution */
        {
            list_remove( &handler->entry );
            to_free = handler;
        }
185 186
        if (ret == EXCEPTION_CONTINUE_EXECUTION) break;
    }
187
    RtlLeaveCriticalSection( &vectored_handlers_section );
188
    RtlFreeHeap( GetProcessHeap(), 0, to_free );
189 190 191 192
    return ret;
}


193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/*******************************************************************
 *		raise_status
 *
 * Implementation of RtlRaiseStatus with a specific exception record.
 */
void raise_status( NTSTATUS status, EXCEPTION_RECORD *rec )
{
    EXCEPTION_RECORD ExceptionRec;

    ExceptionRec.ExceptionCode    = status;
    ExceptionRec.ExceptionFlags   = EH_NONCONTINUABLE;
    ExceptionRec.ExceptionRecord  = rec;
    ExceptionRec.NumberParameters = 0;
    for (;;) RtlRaiseException( &ExceptionRec );  /* never returns */
}


210
/***********************************************************************
211
 *            RtlRaiseStatus  (NTDLL.@)
212 213 214 215 216
 *
 * Raise an exception with ExceptionCode = status
 */
void WINAPI RtlRaiseStatus( NTSTATUS status )
{
217
    raise_status( status, NULL );
218
}
219 220


221 222 223 224 225 226 227 228 229
/*******************************************************************
 *         RtlAddVectoredExceptionHandler   (NTDLL.@)
 */
PVOID WINAPI RtlAddVectoredExceptionHandler( ULONG first, PVECTORED_EXCEPTION_HANDLER func )
{
    VECTORED_HANDLER *handler = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*handler) );
    if (handler)
    {
        handler->func = func;
230
        handler->count = 1;
231
        RtlEnterCriticalSection( &vectored_handlers_section );
232 233
        if (first) list_add_head( &vectored_handlers, &handler->entry );
        else list_add_tail( &vectored_handlers, &handler->entry );
234
        RtlLeaveCriticalSection( &vectored_handlers_section );
235 236 237 238 239 240 241 242 243 244 245 246 247
    }
    return handler;
}


/*******************************************************************
 *         RtlRemoveVectoredExceptionHandler   (NTDLL.@)
 */
ULONG WINAPI RtlRemoveVectoredExceptionHandler( PVOID handler )
{
    struct list *ptr;
    ULONG ret = FALSE;

248
    RtlEnterCriticalSection( &vectored_handlers_section );
249 250
    LIST_FOR_EACH( ptr, &vectored_handlers )
    {
251 252
        VECTORED_HANDLER *curr_handler = LIST_ENTRY( ptr, VECTORED_HANDLER, entry );
        if (curr_handler == handler)
253
        {
254 255
            if (!--curr_handler->count) list_remove( ptr );
            else handler = NULL;  /* don't free it yet */
256 257 258 259
            ret = TRUE;
            break;
        }
    }
260
    RtlLeaveCriticalSection( &vectored_handlers_section );
261 262 263 264 265
    if (ret) RtlFreeHeap( GetProcessHeap(), 0, handler );
    return ret;
}


266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/*************************************************************
 *            __wine_spec_unimplemented_stub
 *
 * ntdll-specific implementation to avoid depending on kernel functions.
 * Can be removed once ntdll.spec no longer contains stubs.
 */
void __wine_spec_unimplemented_stub( const char *module, const char *function )
{
    EXCEPTION_RECORD record;

    record.ExceptionCode    = EXCEPTION_WINE_STUB;
    record.ExceptionFlags   = EH_NONCONTINUABLE;
    record.ExceptionRecord  = NULL;
    record.ExceptionAddress = __wine_spec_unimplemented_stub;
    record.NumberParameters = 2;
    record.ExceptionInformation[0] = (ULONG_PTR)module;
    record.ExceptionInformation[1] = (ULONG_PTR)function;
283
    for (;;) RtlRaiseException( &record );
284
}