except.c 7.52 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll exception handling
 *
 * Copyright 2000 Jon Griffiths
Juan Lang's avatar
Juan Lang committed
5
 * Copyright 2005 Juan Lang
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
 * FIXME: Incomplete support for nested exceptions/try block cleanup.
22
 */
23

24
#include "config.h"
25
#include "wine/port.h"
26

27 28
#include <stdarg.h>

29 30
#include "ntstatus.h"
#define WIN32_NO_STATUS
31 32
#include "windef.h"
#include "winbase.h"
33
#include "winternl.h"
34
#include "wine/exception.h"
35
#include "msvcrt.h"
36
#include "excpt.h"
Juan Lang's avatar
Juan Lang committed
37
#include "wincon.h"
38 39
#include "wine/debug.h"

40
WINE_DEFAULT_DEBUG_CHANNEL(seh);
41

42 43
static MSVCRT_security_error_handler security_error_handler;

44
static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
Juan Lang's avatar
Juan Lang committed
45 46 47 48 49 50 51 52

static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
{
    BOOL ret = FALSE;

    switch (ctrlType)
    {
    case CTRL_C_EVENT:
53
        if (sighandlers[MSVCRT_SIGINT])
Juan Lang's avatar
Juan Lang committed
54
        {
55 56
            if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
                sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
Juan Lang's avatar
Juan Lang committed
57 58 59 60 61 62 63
            ret = TRUE;
        }
        break;
    }
    return ret;
}

64
typedef void (CDECL *float_handler)(int, int);
Juan Lang's avatar
Juan Lang committed
65 66

/* The exception codes are actually NTSTATUS values */
67
static const struct
Juan Lang's avatar
Juan Lang committed
68 69 70 71
{
    NTSTATUS status;
    int signal;
} float_exception_map[] = {
72 73 74 75 76 77 78
 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
Juan Lang's avatar
Juan Lang committed
79 80 81 82 83
};

static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
{
    LONG ret = EXCEPTION_CONTINUE_SEARCH;
84
    MSVCRT___sighandler_t handler;
Juan Lang's avatar
Juan Lang committed
85

86 87 88
    if (!except || !except->ExceptionRecord)
        return EXCEPTION_CONTINUE_SEARCH;

Juan Lang's avatar
Juan Lang committed
89 90 91
    switch (except->ExceptionRecord->ExceptionCode)
    {
    case EXCEPTION_ACCESS_VIOLATION:
92
        if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
Juan Lang's avatar
Juan Lang committed
93
        {
94 95 96 97 98
            if (handler != MSVCRT_SIG_IGN)
            {
                sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
                handler(MSVCRT_SIGSEGV);
            }
Juan Lang's avatar
Juan Lang committed
99 100 101
            ret = EXCEPTION_CONTINUE_EXECUTION;
        }
        break;
102
    /* According to msdn,
Juan Lang's avatar
Juan Lang committed
103 104 105 106 107 108 109 110 111 112
     * the FPE signal handler takes as a second argument the type of
     * floating point exception.
     */
    case EXCEPTION_FLT_DENORMAL_OPERAND:
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
    case EXCEPTION_FLT_INEXACT_RESULT:
    case EXCEPTION_FLT_INVALID_OPERATION:
    case EXCEPTION_FLT_OVERFLOW:
    case EXCEPTION_FLT_STACK_CHECK:
    case EXCEPTION_FLT_UNDERFLOW:
113
        if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
Juan Lang's avatar
Juan Lang committed
114
        {
115
            if (handler != MSVCRT_SIG_IGN)
Juan Lang's avatar
Juan Lang committed
116
            {
117
                unsigned int i;
118
                int float_signal = MSVCRT__FPE_INVALID;
Juan Lang's avatar
Juan Lang committed
119

120
                sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
Juan Lang's avatar
Juan Lang committed
121
                for (i = 0; i < sizeof(float_exception_map) /
122 123
                         sizeof(float_exception_map[0]); i++)
                {
Juan Lang's avatar
Juan Lang committed
124
                    if (float_exception_map[i].status ==
125
                        except->ExceptionRecord->ExceptionCode)
Juan Lang's avatar
Juan Lang committed
126 127 128 129
                    {
                        float_signal = float_exception_map[i].signal;
                        break;
                    }
130 131
                }
                ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
Juan Lang's avatar
Juan Lang committed
132 133 134 135 136
            }
            ret = EXCEPTION_CONTINUE_EXECUTION;
        }
        break;
    case EXCEPTION_ILLEGAL_INSTRUCTION:
137
    case EXCEPTION_PRIV_INSTRUCTION:
138
        if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
Juan Lang's avatar
Juan Lang committed
139
        {
140 141 142 143 144
            if (handler != MSVCRT_SIG_IGN)
            {
                sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
                handler(MSVCRT_SIGILL);
            }
Juan Lang's avatar
Juan Lang committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            ret = EXCEPTION_CONTINUE_EXECUTION;
        }
        break;
    }
    return ret;
}

void msvcrt_init_signals(void)
{
    SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
    SetUnhandledExceptionFilter(msvcrt_exception_filter);
}

void msvcrt_free_signals(void)
{
    SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
    SetUnhandledExceptionFilter(NULL);
}

164 165
/*********************************************************************
 *		signal (MSVCRT.@)
Juan Lang's avatar
Juan Lang committed
166 167
 * Some signals may never be generated except through an explicit call to
 * raise.
168
 */
169
MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
170
{
171
    MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
Juan Lang's avatar
Juan Lang committed
172 173 174

    TRACE("(%d, %p)\n", sig, func);

175
    if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
Juan Lang's avatar
Juan Lang committed
176 177 178 179 180 181

    switch (sig)
    {
    /* Cases handled internally.  Note SIGTERM is never generated by Windows,
     * so we effectively mask it.
     */
182 183 184 185 186 187
    case MSVCRT_SIGABRT:
    case MSVCRT_SIGFPE:
    case MSVCRT_SIGILL:
    case MSVCRT_SIGSEGV:
    case MSVCRT_SIGINT:
    case MSVCRT_SIGTERM:
188
    case MSVCRT_SIGBREAK:
Juan Lang's avatar
Juan Lang committed
189 190 191 192
        ret = sighandlers[sig];
        sighandlers[sig] = func;
        break;
    default:
193
        ret = MSVCRT_SIG_ERR;
Juan Lang's avatar
Juan Lang committed
194 195
    }
    return ret;
196
}
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
/*********************************************************************
 *		raise (MSVCRT.@)
 */
int CDECL MSVCRT_raise(int sig)
{
    MSVCRT___sighandler_t handler;

    TRACE("(%d)\n", sig);

    switch (sig)
    {
    case MSVCRT_SIGABRT:
    case MSVCRT_SIGFPE:
    case MSVCRT_SIGILL:
    case MSVCRT_SIGSEGV:
    case MSVCRT_SIGINT:
    case MSVCRT_SIGTERM:
215
    case MSVCRT_SIGBREAK:
216 217 218 219 220 221
        handler = sighandlers[sig];
        if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
        if (handler != MSVCRT_SIG_IGN)
        {
            sighandlers[sig] = MSVCRT_SIG_DFL;
            if (sig == MSVCRT_SIGFPE)
222
                ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
223 224 225 226 227 228 229 230 231 232
            else
                handler(sig);
        }
        break;
    default:
        return -1;
    }
    return 0;
}

233 234 235
/*********************************************************************
 *		_XcptFilter (MSVCRT.@)
 */
236
int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
237
{
238
    TRACE("(%08x,%p)\n", ex, ptr);
239 240 241
    /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
    return msvcrt_exception_filter(ptr);
}
242

243 244 245 246 247 248 249 250 251
/*********************************************************************
 *		_abnormal_termination (MSVCRT.@)
 */
int CDECL _abnormal_termination(void)
{
  FIXME("(void)stub\n");
  return 0;
}

252 253 254 255 256 257 258
/******************************************************************
 *		MSVCRT___uncaught_exception
 */
BOOL CDECL MSVCRT___uncaught_exception(void)
{
    return FALSE;
}
259 260 261 262 263 264 265 266 267 268 269 270

/* _set_security_error_handler - not exported in native msvcrt, added in msvcr70 */
MSVCRT_security_error_handler CDECL _set_security_error_handler(
    MSVCRT_security_error_handler handler )
{
    MSVCRT_security_error_handler old = security_error_handler;

    TRACE("(%p)\n", handler);

    security_error_handler = handler;
    return old;
}