blob: 67fc9c0ec9a3259f147afb51661b9b9e109e9aee [file] [log] [blame]
Stephen Hines6d186232014-11-26 17:56:19 -08001// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// Test case for recursive signal handlers, adopted from:
4// https://code.google.com/p/thread-sanitizer/issues/detail?id=71
5
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -07006// REQUIRES: disabled
7
Stephen Hines86277eb2015-03-23 12:06:32 -07008#include "test.h"
Stephen Hines6d186232014-11-26 17:56:19 -08009#include <semaphore.h>
10#include <signal.h>
Stephen Hines6d186232014-11-26 17:56:19 -080011#include <errno.h>
Stephen Hines6d186232014-11-26 17:56:19 -080012
13static const int kSigSuspend = SIGUSR1;
14static const int kSigRestart = SIGUSR2;
Stephen Hines6d186232014-11-26 17:56:19 -080015
16static sem_t g_thread_suspend_ack_sem;
17
18static bool g_busy_thread_received_restart;
19
20static volatile bool g_busy_thread_garbage_collected;
21
22static void SaveRegistersInStack() {
23 // Mono walks thread stacks to detect unreferenced objects.
24 // If last object reference is kept in register the object will be collected
25 // This is why threads can't be suspended with something like pthread_suspend
Stephen Hines86277eb2015-03-23 12:06:32 -070026}
Stephen Hines6d186232014-11-26 17:56:19 -080027
28static void fail(const char *what) {
29 fprintf(stderr, "FAILED: %s (errno=%d)\n", what, errno);
30 exit(1);
31}
32
33static void SuspendHandler(int sig) {
34 int old_errno = errno;
35 SaveRegistersInStack();
Stephen Hines86277eb2015-03-23 12:06:32 -070036
37 // Enable kSigRestart handling, tsan disables signals around signal handlers.
38 sigset_t sigset;
39 sigemptyset(&sigset);
40 pthread_sigmask(SIG_SETMASK, &sigset, 0);
41
Stephen Hines6d186232014-11-26 17:56:19 -080042 // Acknowledge that thread is saved and suspended
43 if (sem_post(&g_thread_suspend_ack_sem) != 0)
44 fail("sem_post failed");
45
Stephen Hines86277eb2015-03-23 12:06:32 -070046 // Wait for wakeup signal.
47 while (!g_busy_thread_received_restart)
48 usleep(100); // wait for kSigRestart signal
Stephen Hines6d186232014-11-26 17:56:19 -080049
50 // Acknowledge that thread restarted
51 if (sem_post(&g_thread_suspend_ack_sem) != 0)
52 fail("sem_post failed");
53
54 g_busy_thread_garbage_collected = true;
55
56 errno = old_errno;
57}
58
59static void RestartHandler(int sig) {
60 g_busy_thread_received_restart = true;
61}
62
63static void StopWorld(pthread_t thread) {
Stephen Hines86277eb2015-03-23 12:06:32 -070064 if (pthread_kill(thread, kSigSuspend) != 0)
Stephen Hines6d186232014-11-26 17:56:19 -080065 fail("pthread_kill failed");
66
Stephen Hines86277eb2015-03-23 12:06:32 -070067 while (sem_wait(&g_thread_suspend_ack_sem) != 0) {
68 if (errno != EINTR)
Stephen Hines6d186232014-11-26 17:56:19 -080069 fail("sem_wait failed");
Stephen Hines6d186232014-11-26 17:56:19 -080070 }
71}
72
73static void StartWorld(pthread_t thread) {
Stephen Hines86277eb2015-03-23 12:06:32 -070074 if (pthread_kill(thread, kSigRestart) != 0)
Stephen Hines6d186232014-11-26 17:56:19 -080075 fail("pthread_kill failed");
76
Stephen Hines86277eb2015-03-23 12:06:32 -070077 while (sem_wait(&g_thread_suspend_ack_sem) != 0) {
78 if (errno != EINTR)
Stephen Hines6d186232014-11-26 17:56:19 -080079 fail("sem_wait failed");
Stephen Hines6d186232014-11-26 17:56:19 -080080 }
81}
82
83static void CollectGarbage(pthread_t thread) {
84 StopWorld(thread);
85 // Walk stacks
Stephen Hines86277eb2015-03-23 12:06:32 -070086 StartWorld(thread);
Stephen Hines6d186232014-11-26 17:56:19 -080087}
88
89static void Init() {
Stephen Hines6d186232014-11-26 17:56:19 -080090 if (sem_init(&g_thread_suspend_ack_sem, 0, 0) != 0)
91 fail("sem_init failed");
92
93 struct sigaction act = {};
94 act.sa_flags = SA_RESTART;
Stephen Hines6d186232014-11-26 17:56:19 -080095 act.sa_handler = &SuspendHandler;
96 if (sigaction(kSigSuspend, &act, NULL) != 0)
97 fail("sigaction failed");
98 act.sa_handler = &RestartHandler;
99 if (sigaction(kSigRestart, &act, NULL) != 0)
100 fail("sigaction failed");
101}
102
103void* BusyThread(void *arg) {
104 (void)arg;
105 while (!g_busy_thread_garbage_collected) {
106 usleep(100); // Tsan deadlocks without these sleeps
107 }
108 return NULL;
109}
110
111int main(int argc, const char *argv[]) {
112 Init();
113 pthread_t busy_thread;
Stephen Hines86277eb2015-03-23 12:06:32 -0700114 if (pthread_create(&busy_thread, NULL, &BusyThread, NULL) != 0)
115 fail("pthread_create failed");
Stephen Hines6d186232014-11-26 17:56:19 -0800116 CollectGarbage(busy_thread);
Stephen Hines86277eb2015-03-23 12:06:32 -0700117 if (pthread_join(busy_thread, 0) != 0)
118 fail("pthread_join failed");
Stephen Hines6d186232014-11-26 17:56:19 -0800119 fprintf(stderr, "DONE\n");
120 return 0;
121}
122
123// CHECK-NOT: FAILED
124// CHECK-NOT: ThreadSanitizer CHECK failed
125// CHECK-NOT: WARNING: ThreadSanitizer:
126// CHECK: DONE