summaryrefslogtreecommitdiff
path: root/liboil/liboilfault.c
blob: 19cfd4dce0bca8bc878cf3f9c2802f15e0a4263f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * LIBOIL - Library of Optimized Inner Loops
 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <liboil/liboilfunction.h>
#include <liboil/liboildebug.h>
#include <liboil/liboilfault.h>

#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <signal.h>

#ifdef _WIN32
#include <windows.h>
#endif

static jmp_buf jump_env;
#ifdef HAVE_SIGACTION
static struct sigaction action;
static struct sigaction oldaction;
#else
static void * oldhandler;
#endif
static int in_try_block;
static int enable_level;

#ifdef _WIN32
static LONG __stdcall
illegal_instruction_handler (EXCEPTION_POINTERS *e)
{
  if (in_try_block) {
    /* according to the laws of win32, this isn't allowed.
     * It does, however, work. */
    longjmp (jump_env, 1);
  }
  /* kill the process */
  return EXCEPTION_EXECUTE_HANDLER;
}
#else
static void
illegal_instruction_handler (int num)
{
  if (in_try_block) {
//#ifdef HAVE_SIGPROCMASK
    sigset_t set;
    sigemptyset (&set);
    sigaddset (&set, SIGILL);
    sigprocmask (SIG_UNBLOCK, &set, NULL);
//#endif
    longjmp (jump_env, 1);
  } else {
    abort ();
  }
}
#endif

/**
 * oil_fault_check_enable:
 *
 * Enables fault checking mode.  This function may be called multiple times.
 * Each call to this function must be paired with a corresponding call
 * to oil_fault_check_disable().
 *
 * This function sets a signal handler for SIGILL.
 */
void
oil_fault_check_enable (void)
{
  if (enable_level == 0) {
#ifndef _WIN32
#ifdef HAVE_SIGACTION
    memset (&action, 0, sizeof(action));
    action.sa_handler = &illegal_instruction_handler;
    sigaction (SIGILL, &action, &oldaction);
#else
    oldhandler = signal (SIGILL, illegal_instruction_handler);
#endif
#else
    oldhandler = SetUnhandledExceptionFilter(illegal_instruction_handler);
#endif
    in_try_block = 0;
    OIL_INFO("enabling SIGILL handler.  Make sure to continue past "
        "any SIGILL signals caught by gdb.");
  }
  enable_level++;
}

/**
 * oil_fault_check_try:
 * @func: the function to attempt
 * @priv: a value to pass to the function
 *
 * Calls to this
 * function must be preceded by a call to oil_fault_check_enable()
 * to enable fault checking mode.  This function sets up recovery
 * information and then calls the function @func with the parameter
 * @priv.  If @func or any other functions it calls attempt to execute
 * an illegal instruction, the exception will be caught and recovered from.
 *
 * Returns: 1 if the function was executed sucessfully, 0 if the
 * function attempted to execute an illegal instruction.
 */
int
oil_fault_check_try (void (*func) (void *), void *priv)
{
  int ret;

  in_try_block = 1;
  ret = setjmp (jump_env);
  if (!ret) {
    func (priv);
  }
  in_try_block = 0;

  return (ret == 0);
}

/**
 * oil_fault_check_disable:
 *
 * Disables fault checking mode.  See oil_fault_check_enable()
 * for details.
 */
void
oil_fault_check_disable (void)
{
  enable_level--;
  if (enable_level == 0) {
#ifndef _WIN32
#ifdef HAVE_SIGACTION
    sigaction (SIGILL, &oldaction, NULL);
#else
    signal (SIGILL, oldhandler);
#endif
#else
    SetUnhandledExceptionFilter(oldhandler);
#endif
    OIL_INFO("disabling SIGILL handler");
  }
}