1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// Test that a signal is not delivered when it is blocked.
4
5// FIXME: Very flaky on PPC with COMPILER_RT_DEBUG.
6// https://github.com/google/sanitizers/issues/1792
7// UNSUPPORTED: !compiler-rt-optimized && ppc
8
9#include "test.h"
10#include <semaphore.h>
11#include <signal.h>
12#include <errno.h>
13
14int stop;
15sig_atomic_t signal_blocked;
16
17void handler(int signum) {
18 if (signal_blocked) {
19 fprintf(stderr, format: "signal arrived when blocked\n");
20 exit(status: 1);
21 }
22}
23
24void *thread(void *arg) {
25 sigset_t myset;
26 sigemptyset(set: &myset);
27 sigaddset(set: &myset, SIGUSR1);
28 while (!__atomic_load_n(&stop, __ATOMIC_RELAXED)) {
29 usleep(useconds: 1);
30 if (pthread_sigmask(SIG_BLOCK, newmask: &myset, oldmask: 0)) {
31 fprintf(stderr, format: "pthread_sigmask failed %d\n", errno);
32 exit(status: 1);
33 }
34 signal_blocked = 1;
35 usleep(useconds: 1);
36 signal_blocked = 0;
37 if (pthread_sigmask(SIG_UNBLOCK, newmask: &myset, oldmask: 0)) {
38 fprintf(stderr, format: "pthread_sigmask failed %d\n", errno);
39 exit(status: 1);
40 }
41 }
42 return 0;
43}
44
45int main(int argc, char** argv) {
46 struct sigaction act = {};
47 act.sa_handler = &handler;
48 if (sigaction(SIGUSR1, act: &act, oact: 0)) {
49 fprintf(stderr, format: "sigaction failed %d\n", errno);
50 return 1;
51 }
52 pthread_t th;
53 pthread_create(newthread: &th, attr: 0, start_routine: thread, arg: 0);
54 for (int i = 0; i < 100000; i++)
55 pthread_kill(threadid: th, SIGUSR1);
56 __atomic_store_n(&stop, 1, __ATOMIC_RELAXED);
57 pthread_join(th: th, thread_return: 0);
58 fprintf(stderr, format: "DONE\n");
59 return 0;
60}
61
62// CHECK-NOT: ThreadSanitizer CHECK
63// CHECK-NOT: WARNING: ThreadSanitizer:
64// CHECK: DONE
65

source code of compiler-rt/test/tsan/signal_block.cpp