Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 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 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 | |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <sys/mman.h> |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 20 | #include <string.h> |
| 21 | #include <unistd.h> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 22 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 23 | unsigned long page_size; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | void *guard; |
| 25 | |
| 26 | void handler(int signo, siginfo_t *info, void *uctx) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 27 | mprotect(guard, page_size, PROT_READ | PROT_WRITE); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | int main() { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 31 | page_size = sysconf(_SC_PAGESIZE); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 32 | struct sigaction a, old; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 33 | memset(&a, 0, sizeof(a)); |
| 34 | memset(&old, 0, sizeof(old)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 35 | a.sa_sigaction = handler; |
| 36 | a.sa_flags = SA_SIGINFO; |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 37 | sigaction(SIGSEGV, &a, &old); |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 38 | guard = mmap(0, 3 * page_size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 39 | guard = (char*)guard + page_size; // work around a kernel bug |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 40 | for (int i = 0; i < 1000000; i++) { |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 41 | mprotect(guard, page_size, PROT_NONE); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 42 | *(int*)guard = 1; |
| 43 | } |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 44 | sigaction(SIGSEGV, &old, 0); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 45 | fprintf(stderr, "DONE\n"); |
| 46 | } |
| 47 | |
| 48 | // CHECK: DONE |