File: faulthandler.c

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (784 lines) | stat: -rw-r--r-- 21,417 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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#include "faulthandler.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/resource.h>
#endif
#include <math.h>

#ifdef RPYTHON_LL2CTYPES
#  include "../../../rpython/rlib/rvmprof/src/rvmprof.h"
#else
#  include "common_header.h"
#  include "structdef.h"
#  include "rvmprof.h"
#endif
#include "src/threadlocal.h"

#define MAX_FRAME_DEPTH   100
#define FRAME_DEPTH_N     RVMPROF_TRACEBACK_ESTIMATE_N(MAX_FRAME_DEPTH)

#ifndef _WIN32
#define HAVE_SIGACTION 1
#define HAVE_SIGALTSTACK 1
#endif

#ifdef HAVE_SIGACTION
typedef struct sigaction _Py_sighandler_t;
#else
typedef void (*_Py_sighandler_t)(int);
#endif

typedef struct {
    const int signum;
    volatile int enabled;
    const char* name;
    _Py_sighandler_t previous;
} fault_handler_t;

static struct {
    int initialized;
    int enabled;
    volatile int fd, all_threads;
    volatile pypy_faulthandler_cb_t dump_traceback;
} fatal_error;

#ifdef HAVE_SIGALTSTACK
static stack_t stack;
#endif


static fault_handler_t faulthandler_handlers[] = {
#ifdef SIGBUS
    {SIGBUS, 0, "Bus error", },
#endif
#ifdef SIGILL
    {SIGILL, 0, "Illegal instruction", },
#endif
    {SIGFPE, 0, "Floating point exception", },
    {SIGABRT, 0, "Aborted", },
    /* define SIGSEGV at the end to make it the default choice if searching the
       handler fails in faulthandler_fatal_error() */
    {SIGSEGV, 0, "Segmentation fault", }
};
static const int faulthandler_nsignals =
    sizeof(faulthandler_handlers) / sizeof(fault_handler_t);

RPY_EXTERN
void pypy_faulthandler_write(int fd, const char *str)
{
    ssize_t n, count;
    count = 0;
    while (str[count] != 0)
        count++;

    while (count > 0) {
        n = write(fd, str, count);
        if (n < 0) {
            if (errno != EINTR)
                return;   /* give up */
            n = 0;
        }
        str += n;
        count -= n;
    }
}

RPY_EXTERN
void pypy_faulthandler_write_uint(int fd, unsigned long uvalue, int min_digits)
{
    char buf[48], *p = buf + 48;
    *--p = 0;
    while (uvalue || min_digits > 0) {
        assert(p > buf);
        *--p = '0' + (uvalue % 10UL);
        uvalue /= 10UL;
        min_digits--;
    }

    pypy_faulthandler_write(fd, p);
}

static void pypy_faulthandler_write_hex(int fd, unsigned long uvalue)
{
    char buf[48], *p = buf + 48;
    *--p = 0;
    do {
        unsigned long byte = uvalue % 16UL;
        assert(p > buf);
        if (byte < 10)
            *--p = '0' + byte;
        else
            *--p = 'A' + byte - 10;
        uvalue /= 16UL;
    } while (uvalue > 0UL);

    pypy_faulthandler_write(fd, p);
}


RPY_EXTERN
void pypy_faulthandler_dump_traceback(int fd, int all_threads,
                                      void *ucontext)
{
    pypy_faulthandler_cb_t fn;
    intptr_t array_p[FRAME_DEPTH_N], array_length;

    fn = fatal_error.dump_traceback;
    if (!fn)
        return;

#ifndef RPYTHON_LL2CTYPES
    if (all_threads && _RPython_ThreadLocals_AcquireTimeout(10000) == 0) {
        /* This is known not to be perfectly safe against segfaults if we
           don't hold the GIL ourselves.  Too bad.  I suspect that CPython
           has issues there too.
        */
        struct pypy_threadlocal_s *my, *p;
        int blankline = 0;

        my = (struct pypy_threadlocal_s *)_RPy_ThreadLocals_Get();
        p = _RPython_ThreadLocals_Head();
        p = _RPython_ThreadLocals_Enum(p);
        while (p != NULL) {
            if (blankline)
                pypy_faulthandler_write(fd, "\n");
            blankline = 1;

            pypy_faulthandler_write(fd, my == p ? "Current thread 0x"
                                                : "Thread 0x");
            pypy_faulthandler_write_hex(fd, (unsigned long)p->thread_ident);
            pypy_faulthandler_write(fd, " (most recent call first,"
                                        " approximate line numbers):\n");

            array_length = vmprof_get_traceback(p->vmprof_tl_stack,
                                                my == p ? ucontext : NULL,
                                                array_p, FRAME_DEPTH_N);
            fn(fd, array_p, array_length);

            p = _RPython_ThreadLocals_Enum(p);
        }
        _RPython_ThreadLocals_Release();
    }
    else {
        pypy_faulthandler_write(fd, "Stack (most recent call first,"
                                    " approximate line numbers):\n");
        array_length = vmprof_get_traceback(NULL, ucontext,
                                            array_p, FRAME_DEPTH_N);
        fn(fd, array_p, array_length);
    }
#else
    pypy_faulthandler_write(fd, "(no traceback when untranslated)\n");
#endif
}

static void
faulthandler_dump_traceback(int fd, int all_threads, void *ucontext)
{
    static volatile int reentrant = 0;

    if (reentrant)
        return;
    reentrant = 1;
    pypy_faulthandler_dump_traceback(fd, all_threads, ucontext);
    reentrant = 0;
}


/************************************************************/


#ifdef PYPY_FAULTHANDLER_LATER
#include "src/thread.h"
static struct {
    int fd;
    long long microseconds;
    int repeat, exit;
    /* The main thread always holds this lock. It is only released when
       faulthandler_thread() is interrupted before this thread exits, or at
       Python exit. */
    struct RPyOpaque_ThreadLock cancel_event;
    /* released by child thread when joined */
    struct RPyOpaque_ThreadLock running;
} thread_later;

static void faulthandler_thread(void)
{
#ifndef _WIN32
    /* we don't want to receive any signal */
    sigset_t set;
    sigfillset(&set);
    pthread_sigmask(SIG_SETMASK, &set, NULL);
#endif

    RPyLockStatus st;
    unsigned long hours, minutes, seconds, fraction;
    long long t;
    int fd;

    do {
        st = RPyThreadAcquireLockTimed(&thread_later.cancel_event,
                                       thread_later.microseconds, 0);
        if (st == RPY_LOCK_ACQUIRED) {
            RPyThreadReleaseLock(&thread_later.cancel_event);
            break;
        }
        /* Timeout => dump traceback */
        assert(st == RPY_LOCK_FAILURE);

        /* getting to know which thread holds the GIL is not as simple
         * as in CPython, so for now we don't */

        t = thread_later.microseconds;
        fraction = (unsigned long)(t % 1000000);
        t /= 1000000;
        seconds = (unsigned long)(t % 60);
        t /= 60;
        minutes = (unsigned long)(t % 60);
        t /= 60;
        hours = (unsigned long)t;

        fd = thread_later.fd;
        pypy_faulthandler_write(fd, "Timeout (");
        pypy_faulthandler_write_uint(fd, hours, 1);
        pypy_faulthandler_write(fd, ":");
        pypy_faulthandler_write_uint(fd, minutes, 2);
        pypy_faulthandler_write(fd, ":");
        pypy_faulthandler_write_uint(fd, seconds, 2);
        if (fraction != 0) {
            pypy_faulthandler_write(fd, ".");
            pypy_faulthandler_write_uint(fd, fraction, 6);
        }
        pypy_faulthandler_write(fd, ")!\n");
        pypy_faulthandler_dump_traceback(fd, 1, NULL);

        if (thread_later.exit)
            _exit(1);
    } while (thread_later.repeat);

    /* The only way out */
    RPyThreadReleaseLock(&thread_later.running);
}

RPY_EXTERN
char *pypy_faulthandler_dump_traceback_later(long long microseconds, int repeat,
                                             int fd, int exit)
{
    pypy_faulthandler_cancel_dump_traceback_later();

    thread_later.fd = fd;
    thread_later.microseconds = microseconds;
    thread_later.repeat = repeat;
    thread_later.exit = exit;

    RPyThreadAcquireLock(&thread_later.running, 1);

    if (RPyThreadStart(&faulthandler_thread) == -1) {
        RPyThreadReleaseLock(&thread_later.running);
        return "unable to start watchdog thread";
    }
    return NULL;
}
#endif   /* PYPY_FAULTHANDLER_LATER */

RPY_EXTERN
void pypy_faulthandler_cancel_dump_traceback_later(void)
{
#ifdef PYPY_FAULTHANDLER_LATER
    /* Notify cancellation */
    RPyThreadReleaseLock(&thread_later.cancel_event);

    /* Wait for thread to join (or does nothing if no thread is running) */
    RPyThreadAcquireLock(&thread_later.running, 1);
    RPyThreadReleaseLock(&thread_later.running);

    /* The main thread should always hold the cancel_event lock */
    RPyThreadAcquireLock(&thread_later.cancel_event, 1);
#endif   /* PYPY_FAULTHANDLER_LATER */
}


/************************************************************/


#ifdef PYPY_FAULTHANDLER_USER
typedef struct {
    int enabled;
    int fd;
    int all_threads;
    int chain;
    _Py_sighandler_t previous;
} user_signal_t;

static user_signal_t *user_signals;

#ifndef NSIG
# if defined(_NSIG)
#  define NSIG _NSIG            /* For BSD/SysV */
# elif defined(_SIGMAX)
#  define NSIG (_SIGMAX + 1)    /* For QNX */
# elif defined(SIGMAX)
#  define NSIG (SIGMAX + 1)     /* For djgpp */
# else
#  define NSIG 64               /* Use a reasonable default value */
# endif
#endif

#ifdef HAVE_SIGACTION
static void faulthandler_user(int signum, siginfo_t *info, void *ucontext);
#else
static void faulthandler_user(int signum);
#endif

static int
faulthandler_register(int signum, int chain, _Py_sighandler_t *p_previous)
{
#ifdef HAVE_SIGACTION
    struct sigaction action;
    action.sa_sigaction = faulthandler_user;
    sigemptyset(&action.sa_mask);
    /* if the signal is received while the kernel is executing a system
       call, try to restart the system call instead of interrupting it and
       return EINTR. */
    action.sa_flags = SA_RESTART | SA_SIGINFO;
    if (chain) {
        /* do not prevent the signal from being received from within its
           own signal handler */
        action.sa_flags = SA_NODEFER;
    }
    if (stack.ss_sp != NULL) {
        /* Call the signal handler on an alternate signal stack
           provided by sigaltstack() */
        action.sa_flags |= SA_ONSTACK;
    }
    return sigaction(signum, &action, p_previous);
#else
    _Py_sighandler_t previous;
    previous = signal(signum, faulthandler_user);
    if (p_previous != NULL)
        *p_previous = previous;
    return (previous == SIG_ERR);
#endif
}

#ifdef HAVE_SIGACTION
static void faulthandler_user(int signum, siginfo_t *info, void *ucontext)
{
#else
static void faulthandler_user(int signum)
{
    void *ucontext = NULL;
#endif
    int save_errno;
    user_signal_t *user = &user_signals[signum];

    if (!user->enabled)
        return;

    save_errno = errno;
    faulthandler_dump_traceback(user->fd, user->all_threads, ucontext);

#ifdef HAVE_SIGACTION
    if (user->chain) {
        (void)sigaction(signum, &user->previous, NULL);
        errno = save_errno;

        /* call the previous signal handler */
        raise(signum);

        save_errno = errno;
        (void)faulthandler_register(signum, user->chain, NULL);
    }

    errno = save_errno;
#else
    if (user->chain) {
        errno = save_errno;
        /* call the previous signal handler */
        user->previous(signum);
    }
#endif
}

RPY_EXTERN
int pypy_faulthandler_check_signum(long signum)
{
    unsigned int i;

    for (i = 0; i < faulthandler_nsignals; i++) {
        if (faulthandler_handlers[i].signum == signum) {
            return -1;
        }
    }
    if (signum < 1 || NSIG <= signum) {
        return -2;
    }
    return 0;
}

RPY_EXTERN
char *pypy_faulthandler_register(int signum, int fd, int all_threads, int chain)
{
    user_signal_t *user;
    _Py_sighandler_t previous;
    int err;

    if (user_signals == NULL) {
        user_signals = malloc(NSIG * sizeof(user_signal_t));
        if (user_signals == NULL)
            return "out of memory";
        memset(user_signals, 0, NSIG * sizeof(user_signal_t));
    }

    user = &user_signals[signum];
    user->fd = fd;
    user->all_threads = all_threads;
    user->chain = chain;

    if (!user->enabled) {
        err = faulthandler_register(signum, chain, &previous);
        if (err)
            return strerror(errno);

        user->previous = previous;
        user->enabled = 1;
    }
    return NULL;
}

RPY_EXTERN
int pypy_faulthandler_unregister(int signum)
{
    user_signal_t *user;

    if (user_signals == NULL)
        return 0;

    user = &user_signals[signum];
    if (user->enabled) {
        user->enabled = 0;
        (void)sigaction(signum, &user->previous, NULL);
        user->fd = -1;
        return 1;
    }
    else
        return 0;
}
#endif   /* PYPY_FAULTHANDLER_USER */


/************************************************************/


/* Handler for SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals.

   Display the current Python traceback, restore the previous handler and call
   the previous handler.

   On Windows, don't explicitly call the previous handler, because the Windows
   signal handler would not be called (for an unknown reason). The execution of
   the program continues at faulthandler_fatal_error() exit, but the same
   instruction will raise the same fault (signal), and so the previous handler
   will be called.

   This function is signal-safe and should only call signal-safe functions. */

static void
#ifdef HAVE_SIGACTION
faulthandler_fatal_error(int signum, siginfo_t *info, void *ucontext)
{
#else
faulthandler_fatal_error(int signum)
{
    void *ucontext = NULL;
#endif
    int fd = fatal_error.fd;
    int i;
    fault_handler_t *handler = NULL;
    int save_errno = errno;

    for (i = 0; i < faulthandler_nsignals; i++) {
        handler = &faulthandler_handlers[i];
        if (handler->signum == signum)
            break;
    }
    /* If not found, we use the SIGSEGV handler (the last one in the list) */

    /* restore the previous handler */
    if (handler->enabled) {
#ifdef HAVE_SIGACTION
        (void)sigaction(signum, &handler->previous, NULL);
#else
        (void)signal(signum, handler->previous);
#endif
        handler->enabled = 0;
    }

    pypy_faulthandler_write(fd, "Fatal Python error: ");
    pypy_faulthandler_write(fd, handler->name);
    pypy_faulthandler_write(fd, "\n\n");

    faulthandler_dump_traceback(fd, fatal_error.all_threads, ucontext);

    errno = save_errno;
#ifdef _WIN32
    if (signum == SIGSEGV) {
        /* don't explicitly call the previous handler for SIGSEGV in this signal
           handler, because the Windows signal handler would not be called */
        return;
    }
#endif
    /* call the previous signal handler: it is called immediately if we use
       sigaction() thanks to SA_NODEFER flag, otherwise it is deferred */
    raise(signum);
}


RPY_EXTERN
char *pypy_faulthandler_setup(pypy_faulthandler_cb_t dump_callback)
{
    if (fatal_error.initialized)
        return NULL;
    assert(!fatal_error.enabled);
    fatal_error.dump_traceback = dump_callback;

#ifdef HAVE_SIGALTSTACK
    /* Try to allocate an alternate stack for faulthandler() signal handler to
     * be able to allocate memory on the stack, even on a stack overflow. If it
     * fails, ignore the error. */
    stack.ss_flags = 0;
    stack.ss_size = SIGSTKSZ;
    stack.ss_sp = malloc(stack.ss_size);
    if (stack.ss_sp != NULL) {
        int err = sigaltstack(&stack, NULL);
        if (err) {
            free(stack.ss_sp);
            stack.ss_sp = NULL;
        }
    }
#endif

#ifdef PYPY_FAULTHANDLER_LATER
    if (!RPyThreadLockInit(&thread_later.cancel_event) ||
        !RPyThreadLockInit(&thread_later.running))
        return "failed to initialize locks";
    RPyThreadAcquireLock(&thread_later.cancel_event, 1);
#endif

    fatal_error.fd = -1;
    fatal_error.initialized = 1;

    return NULL;
}

RPY_EXTERN
void pypy_faulthandler_teardown(void)
{
    if (fatal_error.initialized) {

#ifdef PYPY_FAULTHANDLER_LATER
        pypy_faulthandler_cancel_dump_traceback_later();
        RPyThreadReleaseLock(&thread_later.cancel_event);
        RPyOpaqueDealloc_ThreadLock(&thread_later.running);
        RPyOpaqueDealloc_ThreadLock(&thread_later.cancel_event);
#endif

#ifdef PYPY_FAULTHANDLER_USER
        int signum;
        for (signum = 0; signum < NSIG; signum++)
            pypy_faulthandler_unregister(signum);
        /* don't free 'user_signals', the gain is very minor and it can
           lead to rare crashes if another thread is still busy */
#endif

        pypy_faulthandler_disable();
        fatal_error.initialized = 0;
#ifdef HAVE_SIGALTSTACK
        if (stack.ss_sp) {
            stack.ss_flags = SS_DISABLE;
            sigaltstack(&stack, NULL);
            free(stack.ss_sp);
            stack.ss_sp = NULL;
        }
#endif
    }
}

RPY_EXTERN
char *pypy_faulthandler_enable(int fd, int all_threads)
{
    /* Install the handler for fatal signals, faulthandler_fatal_error(). */
    int i;
    fatal_error.fd = fd;
    fatal_error.all_threads = all_threads;

    if (!fatal_error.enabled) {
        fatal_error.enabled = 1;

        for (i = 0; i < faulthandler_nsignals; i++) {
            int err;
            fault_handler_t *handler = &faulthandler_handlers[i];
#ifdef HAVE_SIGACTION
            struct sigaction action;
            action.sa_sigaction = faulthandler_fatal_error;
            sigemptyset(&action.sa_mask);
            /* Do not prevent the signal from being received from within
               its own signal handler */
            action.sa_flags = SA_NODEFER | SA_SIGINFO;
            if (stack.ss_sp != NULL) {
                /* Call the signal handler on an alternate signal stack
                   provided by sigaltstack() */
                action.sa_flags |= SA_ONSTACK;
            }
            err = sigaction(handler->signum, &action, &handler->previous);
#else
            handler->previous = signal(handler->signum,
                                       faulthandler_fatal_error);
            err = (handler->previous == SIG_ERR);
#endif
            if (err) {
                return strerror(errno);
            }
            handler->enabled = 1;
        }
    }
    return NULL;
}

RPY_EXTERN
void pypy_faulthandler_disable(void)
{
    int i;
    if (fatal_error.enabled) {
        fatal_error.enabled = 0;
        for (i = 0; i < faulthandler_nsignals; i++) {
            fault_handler_t *handler = &faulthandler_handlers[i];
            if (!handler->enabled)
                continue;
#ifdef HAVE_SIGACTION
            (void)sigaction(handler->signum, &handler->previous, NULL);
#else
            (void)signal(handler->signum, handler->previous);
#endif
            handler->enabled = 0;
        }
    }
    fatal_error.fd = -1;
}

RPY_EXTERN
int pypy_faulthandler_is_enabled(void)
{
    return fatal_error.enabled;
}


/************************************************************/


/* for tests... */

static void
faulthandler_suppress_crash_report(void)
{
#ifdef _WIN32
    UINT mode;

    /* Configure Windows to not display the Windows Error Reporting dialog */
    mode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
    SetErrorMode(mode | SEM_NOGPFAULTERRORBOX);
#endif

#ifndef _WIN32
    struct rlimit rl;

    /* Disable creation of core dump */
    if (getrlimit(RLIMIT_CORE, &rl) != 0) {
        rl.rlim_cur = 0;
        setrlimit(RLIMIT_CORE, &rl);
    }
#endif

#ifdef _MSC_VER
    /* Visual Studio: configure abort() to not display an error message nor
       open a popup asking to report the fault. */
    _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
#endif
}

RPY_EXTERN
int pypy_faulthandler_read_null(void)
{
    int *volatile x;

    faulthandler_suppress_crash_report();
    x = NULL;
    return *x;
}

RPY_EXTERN
void pypy_faulthandler_sigsegv(void)
{
    faulthandler_suppress_crash_report();
#ifdef _WIN32
    /* For SIGSEGV, faulthandler_fatal_error() restores the previous signal
       handler and then gives back the execution flow to the program (without
       explicitly calling the previous error handler). In a normal case, the
       SIGSEGV was raised by the kernel because of a fault, and so if the
       program retries to execute the same instruction, the fault will be
       raised again.

       Here the fault is simulated by a fake SIGSEGV signal raised by the
       application. We have to raise SIGSEGV at lease twice: once for
       faulthandler_fatal_error(), and one more time for the previous signal
       handler. */
    while(1)
        raise(SIGSEGV);
#else
    raise(SIGSEGV);
#endif
}

RPY_EXTERN
int pypy_faulthandler_sigfpe(void)
{
    /* Do an integer division by zero: raise a SIGFPE on Intel CPU, but not on
       PowerPC. Use volatile to disable compile-time optimizations. */
    volatile int x = 1, y = 0, z;
    faulthandler_suppress_crash_report();
    z = x / y;
    /* If the division by zero didn't raise a SIGFPE (e.g. on PowerPC),
       raise it manually. */
    raise(SIGFPE);
    /* This line is never reached, but we pretend to make something with z
       to silence a compiler warning. */
    return z;
}

RPY_EXTERN
void pypy_faulthandler_sigabrt(void)
{
    faulthandler_suppress_crash_report();
    abort();
}

static double fh_stack_overflow(double levels)
{
    if (levels > 2.5) {
        return (sqrt(fh_stack_overflow(levels - 1.0))
                + fh_stack_overflow(levels * 1e-10));
    }
    return 1e100 + levels;
}

RPY_EXTERN
double pypy_faulthandler_stackoverflow(double levels)
{
    faulthandler_suppress_crash_report();
    return fh_stack_overflow(levels);
}