xref: /llvm-project/compiler-rt/test/tsan/signal_thread2.cpp (revision 3a13c5a2862fdc957d751a7679581d5da151efc2)
1 // RUN: %clangxx_tsan %s -o %t && %run %t 2>&1 | FileCheck %s
2 // UNSUPPORTED: darwin
3 
4 // FIXME: Very flaky on PPC with COMPILER_RT_DEBUG.
5 // https://github.com/google/sanitizers/issues/1792
6 // UNSUPPORTED: !compiler-rt-optimized && ppc
7 
8 // Test case for https://github.com/google/sanitizers/issues/1540
9 
10 #include <errno.h>
11 #include <pthread.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 
19 volatile int X;
20 
21 static void handler(int sig) {
22   (void)sig;
23   if (X != 0)
24     printf("bad");
25 }
26 
27 static void *thr1(void *p) {
28   sleep(1);
29   return 0;
30 }
31 
32 static void *thr(void *p) {
33   pthread_t th[10];
34   for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
35     pthread_create(&th[i], 0, thr1, 0);
36   for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
37     pthread_join(th[i], 0);
38   return 0;
39 }
40 
41 int main() {
42   struct sigaction act = {};
43   act.sa_handler = &handler;
44   if (sigaction(SIGPROF, &act, 0)) {
45     perror("sigaction");
46     exit(1);
47   }
48 
49   itimerval t;
50   t.it_value.tv_sec = 0;
51   t.it_value.tv_usec = 10;
52   t.it_interval = t.it_value;
53   if (setitimer(ITIMER_PROF, &t, 0)) {
54     perror("setitimer");
55     exit(1);
56   }
57 
58   pthread_t th[100];
59   for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
60     pthread_create(&th[i], 0, thr, 0);
61   for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++)
62     pthread_join(th[i], 0);
63 
64   fprintf(stderr, "DONE\n");
65   return 0;
66 }
67 
68 // CHECK-NOT: WARNING: ThreadSanitizer:
69 // CHECK: DONE
70 // CHECK-NOT: WARNING: ThreadSanitizer:
71