aboutsummaryrefslogtreecommitdiffstats
path: root/src/grt/config/jumps.c
blob: 0b01409e73d50d1e7db4523975a67069fab20917 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*  Longjump/Setjump wrapper
    Copyright (C) 2002 - 2015 Tristan Gingold.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <gnu.org/licenses>.

    As a special exception, if other files instantiate generics from this
    unit, or you link this unit with other files to produce an executable,
    this unit does not by itself cause the resulting executable to be
    covered by the GNU General Public License. This exception does not
    however invalidate any other reasons why the executable file might be
    covered by the GNU Public License.
*/

#define _GNU_SOURCE
#include <stddef.h>
#include <signal.h>
#include <fcntl.h>

#if ( (defined (__linux__) && defined (__GLIBC__) ) || defined (__APPLE__) ) && !defined (__ANDROID__)
#define HAVE_BACKTRACE 1
#include <sys/ucontext.h>
#endif

#ifdef HAVE_BACKTRACE
#include <execinfo.h>
#endif

#include "grt_itf.h"

/* There is a simple setjmp/longjmp mechanism used to report failures.
   We have the choice between 3 mechanisms:
   * USE_BUITLIN_SJLJ: gcc builtin setjmp/longjmp, very fast but gcc specific.
   * USE__SETJMP: _setjmp/_longjmp
   * USE_SETJMP: setjmp/longjmp, slower because signals mask is saved/restored.
*/

#if defined (__GNUC__) && !defined (__clang__)
# define USE_BUILTIN_SJLJ
#else
# define USE__SETJMP
#endif

#ifdef USE_BUILTIN_SJLJ
typedef void *JMP_BUF[5];
static int sjlj_val;
# define SETJMP(BUF) (sjlj_val = 0, __builtin_setjmp (BUF), sjlj_val)
# define LONGJMP(BUF, VAL) \
  do { sjlj_val = (VAL); __builtin_longjmp (BUF, 1); } while (0)
#else
# include <setjmp.h>
typedef jmp_buf JMP_BUF;
# ifdef USE__SETJMP
#  define SETJMP _setjmp
#  define LONGJMP _longjmp
# elif defined (USE_SETJMP)
#  define SETJMP setjmp
#  define LONGJMP longjmp
# else
#  error "SETJMP/LONGJMP not configued"
# endif
#endif

static int run_env_en;
static JMP_BUF run_env;

#ifdef __APPLE__
#define NEED_SIGFPE_HANDLER
#define NEED_SIGBUS_HANDLER
#endif

static struct sigaction prev_sigsegv_act;

#ifdef NEED_SIGFPE_HANDLER
static struct sigaction prev_sigfpe_act;
#endif
#ifdef NEED_SIGBUS_HANDLER
static struct sigaction prev_sigbus_act;
#endif

static void
get_bt_from_ucontext (void *uctxt, struct backtrace_addrs *bt)
{
  void *pc = NULL;
  int i;

#ifdef HAVE_BACKTRACE
  bt->size = backtrace (bt->addrs, sizeof (bt->addrs) / sizeof (void *));
  bt->skip = 0;
#else
  bt->size = 0;
  return;
#endif

#if defined (__linux__) && defined (__x86_64__)
  ucontext_t *u = (ucontext_t *)uctxt;
  pc = (void *)u->uc_mcontext.gregs[REG_RIP];
#endif
#if defined (__linux__) && defined (__i386__)
  ucontext_t *u = (ucontext_t *)uctxt;
  pc = (void *)u->uc_mcontext.gregs[REG_EIP];
#endif
#if defined (__APPLE__) && defined (__i386__)
  ucontext_t *u = (ucontext_t *)uctxt;
  pc = (void *)u->uc_mcontext->__ss.__eip;
  bt->skip = 3;  /* This frame + sighandler + trampoline + marker - pc.  */
  bt->addrs[3] = pc;
  return;
#endif

  for (i = 0; i < bt->size; i++)
    if (bt->addrs[i] == pc)
      {
        bt->skip = i;
        break;
      }
}

/* Handler for SIGFPE signal.
   It is also raised in case of overflow (i386 linux).  */
static void
grt_overflow_handler (int signo, siginfo_t *info, void *ptr)
{
  struct backtrace_addrs bt;

  get_bt_from_ucontext (ptr, &bt);
  grt_overflow_error (&bt);
}

/* Posix handler for overflow.  This is used only by mcode.  */
static void
grt_sigsegv_handler (int signo, siginfo_t *info, void *ptr)
{
  struct backtrace_addrs bt;

  get_bt_from_ucontext (ptr, &bt);

#if defined (__linux__) && (defined (__i386__) || defined (__x86_64__))
  if (signo == SIGSEGV)
    {
      ucontext_t *uctxt = (ucontext_t *)ptr;

      /* Linux generates a SIGSEGV (!) for an overflow exception.  */
      if (uctxt->uc_mcontext.gregs[REG_TRAPNO] == 4)
	grt_overflow_error (&bt);
    }
#endif

  /* We loose.  */
  grt_null_access_error (&bt);
}

static void
grt_signal_setup (void)
{
  {
    struct sigaction sigsegv_act;

    sigsegv_act.sa_sigaction = &grt_sigsegv_handler;
    sigemptyset (&sigsegv_act.sa_mask);
    sigsegv_act.sa_flags = SA_SIGINFO;
#ifdef SA_ONESHOT
    sigsegv_act.sa_flags |= SA_ONESHOT;
#elif defined (SA_RESETHAND)
    sigsegv_act.sa_flags |= SA_RESETHAND;
#endif

    /* We don't care about the return status.
       If the handler is not installed, then some feature are lost.  */
    sigaction (SIGSEGV, &sigsegv_act, &prev_sigsegv_act);

#ifdef NEED_SIGBUS_HANDLER
    sigaction (SIGBUS, &sigsegv_act, &prev_sigbus_act);
#endif
  }

#ifdef NEED_SIGFPE_HANDLER
  {
    struct sigaction sig_ovf_act;

    sig_ovf_act.sa_sigaction = &grt_overflow_handler;
    sigemptyset (&sig_ovf_act.sa_mask);
    sig_ovf_act.sa_flags = SA_SIGINFO;

    sigaction (SIGFPE, &sig_ovf_act, &prev_sigfpe_act);
  }
#endif
}

static void
grt_signal_restore (void)
{
  sigaction (SIGSEGV, &prev_sigsegv_act, NULL);

#ifdef NEED_SIGBUS_HANDLER
  sigaction (SIGBUS, &prev_sigbus_act, NULL);
#endif

#ifdef NEED_SIGFPE_HANDLER
  sigaction (SIGFPE, &prev_sigfpe_act, NULL);
#endif
}

void
__ghdl_maybe_return_via_longjump (int val)
{
  if (run_env_en)
    LONGJMP (run_env, val);
}

int
__ghdl_run_through_longjump (int (*func)(void))
{
  int res;

  run_env_en = 1;
  grt_signal_setup ();
  res = SETJMP (run_env);
  if (res == 0)
    res = (*func)();
  grt_signal_restore ();
  run_env_en = 0;
  return res;
}

void
grt_save_backtrace (struct backtrace_addrs *bt, int skip)
{
#ifdef HAVE_BACKTRACE
  bt->size = backtrace (bt->addrs, sizeof (bt->addrs) / sizeof (void *));
  bt->skip = skip + 1;
#else
  bt->size = 0;
#endif
}