1 // RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
3 #include <signal.h>
4 #include <sys/types.h>
5 #include <errno.h>
6 
7 pthread_t mainth;
8 volatile int done;
9 
MyHandler(int,siginfo_t * s,void * c)10 static void MyHandler(int, siginfo_t *s, void *c) {
11   errno = 1;
12   done = 1;
13 }
14 
sendsignal(void * p)15 static void* sendsignal(void *p) {
16   barrier_wait(&barrier);
17   pthread_kill(mainth, SIGPROF);
18   return 0;
19 }
20 
loop()21 static __attribute__((noinline)) void loop() {
22   barrier_wait(&barrier);
23   while (done == 0) {
24     volatile char *p = (char*)malloc(1);
25     p[0] = 0;
26     free((void*)p);
27     pthread_yield();
28   }
29 }
30 
main()31 int main() {
32   barrier_init(&barrier, 2);
33   mainth = pthread_self();
34   struct sigaction act = {};
35   act.sa_sigaction = &MyHandler;
36   sigaction(SIGPROF, &act, 0);
37   pthread_t th;
38   pthread_create(&th, 0, sendsignal, 0);
39   loop();
40   pthread_join(th, 0);
41   return 0;
42 }
43 
44 // CHECK: WARNING: ThreadSanitizer: signal handler spoils errno
45 // CHECK:     #0 MyHandler(int, {{(__)?}}siginfo{{(_t)?}}*, void*) {{.*}}signal_errno.cc
46 // CHECK:     main
47 // CHECK: SUMMARY: ThreadSanitizer: signal handler spoils errno{{.*}}MyHandler
48 
49