sl@0
|
1 |
/*
|
sl@0
|
2 |
* LIBOIL - Library of Optimized Inner Loops
|
sl@0
|
3 |
* Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*
|
sl@0
|
6 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
7 |
* modification, are permitted provided that the following conditions
|
sl@0
|
8 |
* are met:
|
sl@0
|
9 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
10 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
11 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
13 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
14 |
*
|
sl@0
|
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
sl@0
|
16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
sl@0
|
17 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
sl@0
|
19 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
sl@0
|
20 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
sl@0
|
21 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
23 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
sl@0
|
24 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
sl@0
|
25 |
* POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
26 |
*/
|
sl@0
|
27 |
|
sl@0
|
28 |
//Portions Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
29 |
|
sl@0
|
30 |
|
sl@0
|
31 |
#ifdef HAVE_CONFIG_H
|
sl@0
|
32 |
#include "config.h"
|
sl@0
|
33 |
#endif
|
sl@0
|
34 |
|
sl@0
|
35 |
#include <liboil/liboilfunction.h>
|
sl@0
|
36 |
#include <liboil/liboildebug.h>
|
sl@0
|
37 |
#include <liboil/liboilfault.h>
|
sl@0
|
38 |
|
sl@0
|
39 |
#include <stdlib.h>
|
sl@0
|
40 |
#include <string.h>
|
sl@0
|
41 |
#include <setjmp.h>
|
sl@0
|
42 |
#include <signal.h>
|
sl@0
|
43 |
|
sl@0
|
44 |
#ifndef __SYMBIAN32__
|
sl@0
|
45 |
#ifndef _WIN32
|
sl@0
|
46 |
#include <windows.h>
|
sl@0
|
47 |
#endif
|
sl@0
|
48 |
#endif
|
sl@0
|
49 |
|
sl@0
|
50 |
static jmp_buf jump_env;
|
sl@0
|
51 |
#ifdef HAVE_SIGACTION
|
sl@0
|
52 |
static struct sigaction action;
|
sl@0
|
53 |
static struct sigaction oldaction;
|
sl@0
|
54 |
#else
|
sl@0
|
55 |
static void * oldhandler;
|
sl@0
|
56 |
#endif
|
sl@0
|
57 |
static int in_try_block;
|
sl@0
|
58 |
static int enable_level;
|
sl@0
|
59 |
|
sl@0
|
60 |
#ifdef __SYMBIAN32__
|
sl@0
|
61 |
#ifdef __WINSCW__
|
sl@0
|
62 |
#pragma warn_unusedarg off
|
sl@0
|
63 |
#endif//__WINSCW__
|
sl@0
|
64 |
#endif//__SYMBIAN32__
|
sl@0
|
65 |
|
sl@0
|
66 |
#if 0
|
sl@0
|
67 |
#ifndef _WIN32
|
sl@0
|
68 |
static LONG __stdcall
|
sl@0
|
69 |
illegal_instruction_handler (EXCEPTION_POINTERS *e)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
if (in_try_block) {
|
sl@0
|
72 |
/* according to the laws of win32, this isn't allowed.
|
sl@0
|
73 |
* It does, however, work. */
|
sl@0
|
74 |
longjmp (jump_env, 1);
|
sl@0
|
75 |
}
|
sl@0
|
76 |
/* kill the process */
|
sl@0
|
77 |
return EXCEPTION_EXECUTE_HANDLER;
|
sl@0
|
78 |
}
|
sl@0
|
79 |
#endif
|
sl@0
|
80 |
#endif
|
sl@0
|
81 |
//#else
|
sl@0
|
82 |
static void
|
sl@0
|
83 |
illegal_instruction_handler (int num)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
if (in_try_block) {
|
sl@0
|
86 |
#ifdef HAVE_SIGPROCMASK
|
sl@0
|
87 |
sigset_t set;
|
sl@0
|
88 |
sigemptyset (&set);
|
sl@0
|
89 |
sigaddset (&set, SIGILL);
|
sl@0
|
90 |
sigprocmask (SIG_UNBLOCK, &set, NULL);
|
sl@0
|
91 |
longjmp (jump_env, 1);
|
sl@0
|
92 |
} else {
|
sl@0
|
93 |
abort ();
|
sl@0
|
94 |
#endif
|
sl@0
|
95 |
}
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
/**
|
sl@0
|
99 |
* oil_fault_check_enable:
|
sl@0
|
100 |
*
|
sl@0
|
101 |
* Enables fault checking mode. This function may be called multiple times.
|
sl@0
|
102 |
* Each call to this function must be paired with a corresponding call
|
sl@0
|
103 |
* to oil_fault_check_disable().
|
sl@0
|
104 |
*
|
sl@0
|
105 |
* This function sets a signal handler for SIGILL.
|
sl@0
|
106 |
*/
|
sl@0
|
107 |
#ifdef __SYMBIAN32__
|
sl@0
|
108 |
EXPORT_C
|
sl@0
|
109 |
#endif
|
sl@0
|
110 |
void
|
sl@0
|
111 |
oil_fault_check_enable (void)
|
sl@0
|
112 |
{
|
sl@0
|
113 |
#ifndef __SYMBIAN32__
|
sl@0
|
114 |
if (enable_level == 0) {
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
#ifndef _WIN32
|
sl@0
|
118 |
#ifdef HAVE_SIGACTION
|
sl@0
|
119 |
memset (&action, 0, sizeof(action));
|
sl@0
|
120 |
action.sa_handler = &illegal_instruction_handler;
|
sl@0
|
121 |
sigaction (SIGILL, &action, &oldaction);
|
sl@0
|
122 |
#else
|
sl@0
|
123 |
oldhandler = signal (SIGILL, illegal_instruction_handler);
|
sl@0
|
124 |
#endif
|
sl@0
|
125 |
#else
|
sl@0
|
126 |
oldhandler = SetUnhandledExceptionFilter(illegal_instruction_handler);
|
sl@0
|
127 |
#endif
|
sl@0
|
128 |
|
sl@0
|
129 |
in_try_block = 0;
|
sl@0
|
130 |
OIL_INFO("enabling SIGILL handler. Make sure to continue past "
|
sl@0
|
131 |
"any SIGILL signals caught by gdb.");
|
sl@0
|
132 |
}
|
sl@0
|
133 |
enable_level++;
|
sl@0
|
134 |
#endif
|
sl@0
|
135 |
}
|
sl@0
|
136 |
|
sl@0
|
137 |
/**
|
sl@0
|
138 |
* oil_fault_check_try:
|
sl@0
|
139 |
* @func: the function to attempt
|
sl@0
|
140 |
* @priv: a value to pass to the function
|
sl@0
|
141 |
*
|
sl@0
|
142 |
* Calls to this
|
sl@0
|
143 |
* function must be preceded by a call to oil_fault_check_enable()
|
sl@0
|
144 |
* to enable fault checking mode. This function sets up recovery
|
sl@0
|
145 |
* information and then calls the function @func with the parameter
|
sl@0
|
146 |
* @priv. If @func or any other functions it calls attempt to execute
|
sl@0
|
147 |
* an illegal instruction, the exception will be caught and recovered from.
|
sl@0
|
148 |
*
|
sl@0
|
149 |
* Returns: 1 if the function was executed sucessfully, 0 if the
|
sl@0
|
150 |
* function attempted to execute an illegal instruction.
|
sl@0
|
151 |
*/
|
sl@0
|
152 |
#ifdef __SYMBIAN32__
|
sl@0
|
153 |
EXPORT_C
|
sl@0
|
154 |
#endif
|
sl@0
|
155 |
int
|
sl@0
|
156 |
oil_fault_check_try (void (*func) (void *), void *priv)
|
sl@0
|
157 |
{
|
sl@0
|
158 |
int ret;
|
sl@0
|
159 |
|
sl@0
|
160 |
in_try_block = 1;
|
sl@0
|
161 |
ret = setjmp (jump_env);
|
sl@0
|
162 |
if (!ret) {
|
sl@0
|
163 |
func (priv);
|
sl@0
|
164 |
}
|
sl@0
|
165 |
in_try_block = 0;
|
sl@0
|
166 |
|
sl@0
|
167 |
return (ret == 0);
|
sl@0
|
168 |
}
|
sl@0
|
169 |
|
sl@0
|
170 |
/**
|
sl@0
|
171 |
* oil_fault_check_disable:
|
sl@0
|
172 |
*
|
sl@0
|
173 |
* Disables fault checking mode. See oil_fault_check_enable()
|
sl@0
|
174 |
* for details.
|
sl@0
|
175 |
*/
|
sl@0
|
176 |
#ifdef __SYMBIAN32__
|
sl@0
|
177 |
EXPORT_C
|
sl@0
|
178 |
#endif
|
sl@0
|
179 |
void
|
sl@0
|
180 |
oil_fault_check_disable (void)
|
sl@0
|
181 |
{
|
sl@0
|
182 |
#ifndef __SYMBIAN32__
|
sl@0
|
183 |
enable_level--;
|
sl@0
|
184 |
|
sl@0
|
185 |
if (enable_level == 0) {
|
sl@0
|
186 |
#ifndef _WIN32
|
sl@0
|
187 |
#ifdef HAVE_SIGACTION
|
sl@0
|
188 |
sigaction (SIGILL, &oldaction, NULL);
|
sl@0
|
189 |
#else
|
sl@0
|
190 |
signal (SIGILL, oldhandler);
|
sl@0
|
191 |
#endif
|
sl@0
|
192 |
#else
|
sl@0
|
193 |
SetUnhandledExceptionFilter(oldhandler);
|
sl@0
|
194 |
#endif
|
sl@0
|
195 |
OIL_INFO("disabling SIGILL handler");
|
sl@0
|
196 |
}
|
sl@0
|
197 |
#endif
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|