[go: up one dir, main page]

File: error.c

package info (click to toggle)
sablevm 1.11.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,980 kB
  • ctags: 7,922
  • sloc: ansic: 116,013; sh: 8,679; makefile: 489
file content (584 lines) | stat: -rw-r--r-- 13,360 bytes parent folder | download
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This source file is part of SableVM.                            *
 *                                                                 *
 * See the file "LICENSE" for the copyright information and for    *
 * the terms and conditions for copying, distribution and          *
 * modification of this source file.                               *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* We remember previously established signal handlers, so that we can
   delegate back to them when we don't handle a signal. */
static sigset_t _svmv_old_sigset;

static struct sigaction _svmv_old_sigquit;
static struct sigaction _svmv_old_siginterrupt;

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

static struct sigaction _svmv_old_sigsegv;
static struct sigaction _svmv_old_sigfpe;

#ifdef _SABLEVM_INLINABILITY_TESTING

static struct sigaction _svmv_old_sigill;
static struct sigaction _svmv_old_sigtrap;
static struct sigaction _svmv_old_sigsys;
static struct sigaction _svmv_old_sigbus;
static struct sigaction _svmv_old_sigxcpu;
static struct sigaction _svmv_old_sigxfsz;

#endif /* _SABLEVM_INLINABILITY_TESTING */

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

/*
----------------------------------------------------------------------
_svmf_initialization_unrecoverable_exception
----------------------------------------------------------------------
*/

svm_static void
_svmf_initialization_unrecoverable_exception (_svmt_JNIEnv *env)
{
  siglongjmp (env->vm->initialization->unrecoverable_exception_handler, 1);
}

/*
----------------------------------------------------------------------
_svmf_signal_throw
----------------------------------------------------------------------
*/

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

svm_static void
_svmf_signal_throw (_svmt_JNIEnv *env, jint signal_code)
{
  assert (env->signal_code == SVM_SIGNAL_NONE);

  if (env->signal_handler == NULL)
    {
      _svmt_JavaVM *vm = env->vm;

      if (vm->initialization == NULL)
	{
	  return;		/* delegate the signal */
	}

      _svmf_initialization_unrecoverable_exception (env);
    }

  env->signal_code = signal_code;
  siglongjmp (*(env->signal_handler), 1);
}

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

/*
----------------------------------------------------------------------
_svmf_internal_sigaction
----------------------------------------------------------------------
*/

#if defined(_SABLEVM_HAS_SIGINFO)

svm_static void
_svmf_internal_sigaction (int signo, siginfo_t * info, void *ignored)
{
  _svmt_JNIEnv *env;

  /* find the current thread */
  env = _svmf_get_current_env ();

  if (env == NULL)
    {
      /* This thread is not attached to VM, so let's delegate to previous handler, if there was one */
      goto delegate;
    }

#ifdef _SABLEVM_INLINABILITY_TESTING

#ifdef COMMENT
  /* NO PRINTFs in signal handler!!! (but it's sometimes useful ;-) */
  if (!_svmf_no_inlining (env, signo, SVM_IS_INLINED_FROM_SIGHANDLER))
    {
      printf ("expected = %i, signo = %i\n",
	      env->inlinability_testing.sigsegv_expected, signo);
      fflush (NULL);
    }
#endif /* COMMENT */

  /* EG: TODO: Remove preprocessor directives within an expression. */
  if (
#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS)
       (!((env->inlinability_testing.sigsegv_expected)
	  && (signo == SIGSEGV))) &&
#endif
       (signo != SIGFPE)
       && (!_svmf_no_inlining (env, signo, SVM_IS_INLINED_FROM_SIGHANDLER)))
    {
      _svmf_signal_throw (env, SVM_SIGNAL_INLINING_FAILURE);
      return;
    }

#endif /* _SABLEVM_INLINABILITY_TESTING */


  switch (signo)
    {

      /* needed for Thread.interrupt(); */
    case SVM_INTERRUPT_SIGNAL:
      {
	/* do nothing */
      }
      break;

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

    case SIGSEGV:
      {
	jint status = env->thread_status;

#ifndef NDEBUG

	if (!(env->sigsegv_expected))
	  {
	    _svmm_fatal_error ("unexpected segmentation fault");
	  }
	else
	  {
	    /* reset flag */
	    env->sigsegv_expected = JNI_FALSE;
	  }

#endif /* not NDEBUG */

	if (status == SVM_THREAD_STATUS_NOT_RUNNING_JAVA_RESUMING_ALLOWED ||
	    status == SVM_THREAD_STATUS_NOT_RUNNING_JAVA_RESUMING_DISALLOWED)
	  {
	    volatile _svmt_word *cause_segfault = NULL;
	    _svmf_printf (env, stderr,
			  "** Segfault in native code on thread %d.  Java stack trace follows.\n",
			  env->thread.id);
	    _svmf_dump_stack_trace (env);
	    fflush(NULL);
	    /* Stack trace can be long, let the user know (again) what happened. */
	    _svmf_printf (env, stderr,
			  "** Segfault in native code on thread %d.  Java stack trace done.\n",
			  env->thread.id);
	    /* We force a "real" segfault to get core dump and ease debugging. */
	    *cause_segfault = 0xdeadbeef;
	    _svmm_fatal_error ("Impossible control flow after segmentation fault catch.");
	  }
	else
	  {
	    _svmf_signal_throw (env, SVM_SIGNAL_NULL_POINTER_EXCEPTION);
	  }
	goto delegate;
      }
      break;

    case SIGFPE:
      {
	switch (info->si_code)
	  {
#ifndef __powerpc__
	    /*
	     * Note: Division by zero is undefined on powerpc.
	     *       It does not cause an exception.
	     *
	     * Moreover, the code will not compile on Darwin / OS X
	     * since some of the FPE_* including FPE_INTDIV are undefined.
	     *
	     */

#ifdef __alpha__
	    /* On alpha, for some reason (maybe a Linux kernel bug?) after division
	       by 0 we get value 128 in info->si_code. Apparently there's no
	       #define for this anywhere. */
	  case 128:
#endif

#ifdef __arm__
	    /* On ARM si_code seems to always be 0.
	     * Checked at least with Linux 2.6.8.1. */
	  case 0:
#endif

	  case FPE_INTDIV:
	    {

#ifndef NDEBUG

	      if (!(env->sigfpe_expected))
		{
		  _svmm_fatal_error ("unexpected floating point signal");
		}
	      else
		{
		  env->sigfpe_expected = JNI_FALSE;
		}

#endif /* not NDEBUG */

	      _svmf_signal_throw (env, SVM_SIGNAL_ARITHMETIC_EXCEPTION);
	      goto delegate;
	    }
	    break;

	  case FPE_INTOVF:
	  case FPE_FLTDIV:
	  case FPE_FLTOVF:
	  case FPE_FLTUND:
/* some of these might not be defined for ex. on some *BSD */
#ifdef FPE_FLTRES
	  case FPE_FLTRES:
#endif
#ifdef FPE_FLTINV
	  case FPE_FLTINV:
#endif
#ifdef FPE_FLTSUB
	  case FPE_FLTSUB:
#endif

#endif /* !__powerpc__ */
	  default:
	    {
	      _svmm_fatal_error ("unexpected floating point signal");
	    }
	    break;
	  }
      }
      break;

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

    case SIGQUIT:
      {
	/* Thread dump requested */
	_svmf_dump_stack_trace (env);
      }
      break;

    default:
      {
	/* this signal handler shouldn't be registered to catch these! */
	_svmm_fatal_error ("impossible control flow");
      }
    }

  return;

delegate:
  /* find out if there was another signal handler before we set our own.
     If yes call it. If not, abort (as the process would have core
     dumped, anyway).
     Of course, we don't support ignoring (SIG_IGN) these signals, as it
     would leave us in an ugly infinite loop. */
  switch (signo)
    {

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

    case SIGSEGV:
      {
	if ((_svmv_old_sigsegv.sa_flags & SA_SIGINFO) != 0)
	  {
	    _svmv_old_sigsegv.sa_sigaction (signo, info, ignored);
	  }
	else if ((_svmv_old_sigsegv.sa_handler != SIG_DFL) &&
		 (_svmv_old_sigsegv.sa_handler != SIG_IGN))
	  {
	    _svmv_old_sigsegv.sa_handler (signo);
	  }
	else
	  {
	    _svmm_fatal_error ("unhandled segmentation fault");
	  }
      }
      break;

    case SIGFPE:
      {
	if ((_svmv_old_sigfpe.sa_flags & SA_SIGINFO) != 0)
	  {
	    _svmv_old_sigfpe.sa_sigaction (signo, info, ignored);
	  }
	else if ((_svmv_old_sigfpe.sa_handler != SIG_DFL) &&
		 (_svmv_old_sigfpe.sa_handler != SIG_IGN))
	  {
	    _svmv_old_sigfpe.sa_handler (signo);
	  }
	else
	  {
	    _svmm_fatal_error ("unhandled floating point signal");
	  }
      }
      break;

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

    case SIGQUIT:
      {
	if ((_svmv_old_sigquit.sa_flags & SA_SIGINFO) != 0)
	  {
	    _svmv_old_sigquit.sa_sigaction (signo, info, ignored);
	  }
	else if ((_svmv_old_sigquit.sa_handler != SIG_DFL) &&
		 (_svmv_old_sigquit.sa_handler != SIG_IGN))
	  {
	    _svmv_old_sigquit.sa_handler (signo);
	  }
	else
	  {
	    _svmm_fatal_error ("unhandled quit signal");
	  }
      }
      break;

    default:
      {
	/* this signal handler shouldn't be registered to catch these! */
	_svmm_fatal_error ("impossible control flow");
      }
      break;
    }
}

#endif /* _SABLEVM_HAS_SIGINFO */

/*
----------------------------------------------------------------------
_svmf_error_init
----------------------------------------------------------------------
*/

svm_static jint
_svmf_error_init (void)
{
  sigset_t svmv_sigset;
  struct sigaction sa;

  /* save old sigmask before doing any modifications */

  if (sigprocmask (0, NULL, &_svmv_old_sigset) != 0)
    {
      goto error;
    }

  _svmm_zero_memory (sa);

  /* mask all signals handled by the signal handler */
  if (sigemptyset (&sa.sa_mask) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGQUIT) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SVM_INTERRUPT_SIGNAL) != 0)
    {
      goto error;
    }

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

  if (sigaddset (&sa.sa_mask, SIGFPE) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGSEGV) != 0)
    {
      goto error;
    }

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

#ifdef _SABLEVM_INLINABILITY_TESTING

/* SIGABRT is generated by assert() - don't mess with it! */
/* SIGALRM is not catched because JVM is too messy to continue anyway */

  if (sigaddset (&sa.sa_mask, SIGILL) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGTRAP) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGSYS) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGBUS) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGXCPU) != 0)
    {
      goto error;
    }

  if (sigaddset (&sa.sa_mask, SIGXFSZ) != 0)
    {
      goto error;
    }

#endif /* _SABLEVM_INLINABILITY_TESTING */

#if defined(_SABLEVM_HAS_SIGINFO)

  /* TODO: add signal support for systems w/o SA_SIGINFO */

  /* use extended info version */
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = _svmf_internal_sigaction;

  if (sigaction (SIGQUIT, &sa, &_svmv_old_sigquit) != 0)
    {
      goto error;
    }

  if (sigaction (SVM_INTERRUPT_SIGNAL, &sa, &_svmv_old_siginterrupt) != 0)
    {
      goto error;
    }

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

  if (sigaction (SIGSEGV, &sa, &_svmv_old_sigsegv) != 0)
    {
      goto error;
    }

  if (sigaction (SIGFPE, &sa, &_svmv_old_sigfpe) != 0)
    {
      goto error;
    }

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

#ifdef _SABLEVM_INLINABILITY_TESTING

  if (sigaction (SIGILL, &sa, &_svmv_old_sigill) != 0)
    {
      goto error;
    }

  if (sigaction (SIGTRAP, &sa, &_svmv_old_sigtrap) != 0)
    {
      goto error;
    }

  if (sigaction (SIGSYS, &sa, &_svmv_old_sigsys) != 0)
    {
      goto error;
    }

  if (sigaction (SIGBUS, &sa, &_svmv_old_sigbus) != 0)
    {
      goto error;
    }

  if (sigaction (SIGXCPU, &sa, &_svmv_old_sigxcpu) != 0)
    {
      goto error;
    }

  if (sigaction (SIGXFSZ, &sa, &_svmv_old_sigxfsz) != 0)
    {
      goto error;
    }

#endif /* _SABLEVM_INLINABILITY_TESTING */

#endif /* _SABLEVM_HAS_SIGINFO */

  /* ignore SIGPIPE */

  if (sigemptyset (&svmv_sigset) != 0)
    {
      goto error;
    }

  if (sigaddset (&svmv_sigset, SIGPIPE) != 0)
    {
      goto error;
    }

  if (sigprocmask (SIG_BLOCK, &svmv_sigset, NULL) != 0)
    {
      goto error;
    }

  return JNI_OK;

error:

  return JNI_ERR;
}

/*
----------------------------------------------------------------------
_svmf_error_restore
----------------------------------------------------------------------
*/

/* TODO: call this function from the right place. It's unused currently. */

#ifdef COMMENT

svm_static jint
_svmf_error_restore (void)
{
#if defined(_SABLEVM_HAS_SIGINFO)

  if (sigaction (SIGQUIT, &_svmv_old_sigquit, NULL) != 0)
    {
      goto error;
    }

  if (sigaction (SVM_INTERRUPT_SIGNAL, &_svmv_old_siginterrupt, NULL) != 0)
    {
      goto error;
    }

#if defined(_SABLEVM_SIGNALS_FOR_EXCEPTIONS) || defined(_SABLEVM_INLINABILITY_TESTING)

  if (sigaction (SIGSEGV, &_svmv_old_sigsegv, NULL) != 0)
    {
      goto error;
    }

  if (sigaction (SIGFPE, &_svmv_old_sigfpe, NULL) != 0)
    {
      goto error;
    }

#endif /* _SABLEVM_SIGNALS_FOR_EXCEPTIONS || _SABLEVM_INLINABILITY_TESTING */

#endif /* _SABLEVM_HAS_SIGINFO */

  if (sigprocmask (SIG_SETMASK, &_svmv_old_sigset, NULL) != 0)
    {
      goto error;
    }

  return JNI_OK;

error:

  return JNI_ERR;
}

#endif /* COMMENT */