• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx -O1 %s -o %t && TSAN_OPTIONS="flush_memory_ms=1 memory_limit_mb=1" ASAN_OPTIONS="handle_segv=0 allow_user_segv_handler=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 <stdio.h>
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <sys/mman.h>
20 
21 void *guard;
22 
handler(int signo,siginfo_t * info,void * uctx)23 void handler(int signo, siginfo_t *info, void *uctx) {
24   mprotect(guard, 4096, PROT_READ | PROT_WRITE);
25 }
26 
main()27 int main() {
28   struct sigaction a, old;
29   a.sa_sigaction = handler;
30   a.sa_flags = SA_SIGINFO;
31   sigaction(SIGSEGV, &a, &old);
32   guard = mmap(0, 4096, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
33   for (int i = 0; i < 1000000; i++) {
34     mprotect(guard, 4096, PROT_NONE);
35     *(int*)guard = 1;
36   }
37   sigaction(SIGSEGV, &old, 0);
38   fprintf(stderr, "DONE\n");
39 }
40 
41 // CHECK: DONE
42