1 | // RUN: %clangxx -O1 %s -o %t && TSAN_OPTIONS="flush_memory_ms=1 memory_limit_mb=1" %run %t 2>&1 | FileCheck %s |
2 | |
3 | // JVM uses SEGV to preempt threads. All threads do a load from a known address |
4 | // periodically. When runtime needs to preempt threads, it unmaps the page. |
5 | // Threads start triggering SEGV one by one. The signal handler blocks |
6 | // threads while runtime does its thing. Then runtime maps the page again |
7 | // and resumes the threads. |
8 | // Previously this pattern conflicted with stop-the-world machinery, |
9 | // because it briefly reset SEGV handler to SIG_DFL. |
10 | // As the consequence JVM just silently died. |
11 | |
12 | // This test sets memory flushing rate to maximum, then does series of |
13 | // "benign" SEGVs that are handled by signal handler, and ensures that |
14 | // the process survive. |
15 | |
16 | #include <assert.h> |
17 | #include <signal.h> |
18 | #include <stdio.h> |
19 | #include <stdlib.h> |
20 | #include <string.h> |
21 | #include <sys/mman.h> |
22 | #include <unistd.h> |
23 | |
24 | unsigned long page_size; |
25 | void *guard; |
26 | |
27 | void handler(int signo, siginfo_t *info, void *uctx) { |
28 | mprotect(addr: guard, len: page_size, PROT_READ | PROT_WRITE); |
29 | } |
30 | |
31 | int main() { |
32 | page_size = sysconf(_SC_PAGESIZE); |
33 | struct sigaction a, old; |
34 | memset(s: &a, c: 0, n: sizeof(a)); |
35 | memset(s: &old, c: 0, n: sizeof(old)); |
36 | a.sa_sigaction = handler; |
37 | a.sa_flags = SA_SIGINFO; |
38 | sigaction(SIGSEGV, act: &a, oact: &old); |
39 | |
40 | memset(s: &a, c: 0, n: sizeof(a)); |
41 | sigaction(SIGSEGV, act: 0, oact: &a); |
42 | assert(a.sa_sigaction == handler); |
43 | assert(a.sa_flags & SA_SIGINFO); |
44 | |
45 | guard = mmap(addr: 0, len: 3 * page_size, PROT_NONE, MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0); |
46 | guard = (char*)guard + page_size; // work around a kernel bug |
47 | for (int i = 0; i < 1000000; i++) { |
48 | mprotect(addr: guard, len: page_size, PROT_NONE); |
49 | *(int*)guard = 1; |
50 | } |
51 | sigaction(SIGSEGV, act: &old, oact: 0); |
52 | fprintf(stderr, format: "DONE\n" ); |
53 | } |
54 | |
55 | // CHECK: DONE |
56 | |