signal_longjmp.cpp
1.82 KB
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
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
// Test case for longjumping out of signal handler:
// https://github.com/google/sanitizers/issues/482
// This test fails on powerpc64 BE (VMA=44), a segmentation fault
// error happens at the second assignment
// "((volatile int *volatile)mem)[1] = 1".
// XFAIL: powerpc64-unknown-linux-gnu
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#ifdef __APPLE__
#define SIGNAL_TO_HANDLE SIGBUS
#else
#define SIGNAL_TO_HANDLE SIGSEGV
#endif
sigjmp_buf fault_jmp;
volatile int fault_expected;
void sigfault_handler(int sig) {
if (!fault_expected)
abort();
/* just return from sighandler to proper place */
fault_expected = 0;
siglongjmp(fault_jmp, 1);
}
#define MUST_FAULT(code) do { \
fault_expected = 1; \
if (!sigsetjmp(fault_jmp, 1)) { \
code; /* should pagefault -> sihandler does longjmp */ \
fprintf(stderr, "%s not faulted\n", #code); \
abort(); \
} else { \
fprintf(stderr, "%s faulted ok\n", #code); \
} \
} while (0)
int main() {
struct sigaction act;
act.sa_handler = sigfault_handler;
act.sa_flags = 0;
if (sigemptyset(&act.sa_mask)) {
perror("sigemptyset");
exit(1);
}
if (sigaction(SIGNAL_TO_HANDLE, &act, NULL)) {
perror("sigaction");
exit(1);
}
void *mem = mmap(0, 4096, PROT_NONE, MAP_PRIVATE | MAP_ANON,
-1, 0);
MUST_FAULT(((volatile int *volatile)mem)[0] = 0);
MUST_FAULT(((volatile int *volatile)mem)[1] = 1);
MUST_FAULT(((volatile int *volatile)mem)[3] = 1);
// Ensure that tsan does not think that we are
// in a signal handler.
void *volatile p = malloc(10);
((volatile int*)p)[1] = 1;
free((void*)p);
munmap(p, 4096);
fprintf(stderr, "DONE\n");
return 0;
}
// CHECK-NOT: WARNING: ThreadSanitizer
// CHECK: DONE