2017-11-21 07:37:46 +08:00
|
|
|
// clang-format off
|
|
|
|
// RUN: %clangxx -O1 %s -o %t && TSAN_OPTIONS="flush_memory_ms=1 memory_limit_mb=1" %run %t 2>&1 | FileCheck %s
|
|
|
|
// clang-format on
|
2015-03-05 22:37:28 +08:00
|
|
|
|
|
|
|
// JVM uses SEGV to preempt threads. All threads do a load from a known address
|
|
|
|
// periodically. When runtime needs to preempt threads, it unmaps the page.
|
|
|
|
// Threads start triggering SEGV one by one. The signal handler blocks
|
|
|
|
// threads while runtime does its thing. Then runtime maps the page again
|
|
|
|
// and resumes the threads.
|
|
|
|
// Previously this pattern conflicted with stop-the-world machinery,
|
|
|
|
// because it briefly reset SEGV handler to SIG_DFL.
|
|
|
|
// As the consequence JVM just silently died.
|
|
|
|
|
|
|
|
// This test sets memory flushing rate to maximum, then does series of
|
|
|
|
// "benign" SEGVs that are handled by signal handler, and ensures that
|
|
|
|
// the process survive.
|
|
|
|
|
2017-11-21 07:37:46 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <signal.h>
|
2015-03-05 22:37:28 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-06-30 00:31:10 +08:00
|
|
|
#include <string.h>
|
2017-11-21 07:37:46 +08:00
|
|
|
#include <sys/mman.h>
|
2015-07-20 05:44:49 +08:00
|
|
|
#include <unistd.h>
|
2015-03-05 22:37:28 +08:00
|
|
|
|
2015-07-20 05:44:49 +08:00
|
|
|
unsigned long page_size;
|
2015-03-05 22:37:28 +08:00
|
|
|
void *guard;
|
|
|
|
|
|
|
|
void handler(int signo, siginfo_t *info, void *uctx) {
|
2015-07-20 05:44:49 +08:00
|
|
|
mprotect(guard, page_size, PROT_READ | PROT_WRITE);
|
2015-03-05 22:37:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2015-07-20 05:44:49 +08:00
|
|
|
page_size = sysconf(_SC_PAGESIZE);
|
2015-03-05 22:37:28 +08:00
|
|
|
struct sigaction a, old;
|
2015-06-30 00:31:10 +08:00
|
|
|
memset(&a, 0, sizeof(a));
|
|
|
|
memset(&old, 0, sizeof(old));
|
2015-03-05 22:37:28 +08:00
|
|
|
a.sa_sigaction = handler;
|
|
|
|
a.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction(SIGSEGV, &a, &old);
|
2017-11-21 07:37:46 +08:00
|
|
|
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
|
|
sigaction(SIGSEGV, 0, &a);
|
|
|
|
assert(a.sa_sigaction == handler);
|
|
|
|
assert(a.sa_flags & SA_SIGINFO);
|
|
|
|
|
2015-07-20 05:44:49 +08:00
|
|
|
guard = mmap(0, 3 * page_size, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
guard = (char*)guard + page_size; // work around a kernel bug
|
2015-03-05 22:37:28 +08:00
|
|
|
for (int i = 0; i < 1000000; i++) {
|
2015-07-20 05:44:49 +08:00
|
|
|
mprotect(guard, page_size, PROT_NONE);
|
2015-03-05 22:37:28 +08:00
|
|
|
*(int*)guard = 1;
|
|
|
|
}
|
|
|
|
sigaction(SIGSEGV, &old, 0);
|
|
|
|
fprintf(stderr, "DONE\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK: DONE
|