| 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(format: "bad" ); |
| 25 | } |
| 26 | |
| 27 | static void *thr1(void *p) { |
| 28 | sleep(seconds: 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(newthread: &th[i], attr: 0, start_routine: thr1, arg: 0); |
| 36 | for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++) |
| 37 | pthread_join(th: th[i], thread_return: 0); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | int main() { |
| 42 | struct sigaction act = {}; |
| 43 | act.sa_handler = &handler; |
| 44 | if (sigaction(SIGPROF, act: &act, oact: 0)) { |
| 45 | perror(s: "sigaction" ); |
| 46 | exit(status: 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, new: &t, old: 0)) { |
| 54 | perror(s: "setitimer" ); |
| 55 | exit(status: 1); |
| 56 | } |
| 57 | |
| 58 | pthread_t th[100]; |
| 59 | for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++) |
| 60 | pthread_create(newthread: &th[i], attr: 0, start_routine: thr, arg: 0); |
| 61 | for (int i = 0; i < sizeof(th) / sizeof(th[0]); i++) |
| 62 | pthread_join(th: th[i], thread_return: 0); |
| 63 | |
| 64 | fprintf(stderr, format: "DONE\n" ); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | // CHECK-NOT: WARNING: ThreadSanitizer: |
| 69 | // CHECK: DONE |
| 70 | // CHECK-NOT: WARNING: ThreadSanitizer: |
| 71 | |