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
6static void handler(int, siginfo_t*, void*) {
7 // CHECK: WARNING: ThreadSanitizer: signal-unsafe call inside of a signal
8 // CHECK: #0 malloc
9 // CHECK: #{{(1|2)}} handler(int, {{(__)?}}siginfo{{(_t)?}}*, void*) {{.*}}signal_malloc.cpp:[[@LINE+2]]
10 // CHECK: SUMMARY: ThreadSanitizer: signal-unsafe call inside of a signal{{.*}}handler
11 volatile char *p = (char*)malloc(size: 1);
12 p[0] = 0;
13 // CHECK: WARNING: ThreadSanitizer: signal-unsafe call inside of a signal
14 // CHECK: #0 free
15 // CHECK: #{{(1|2)}} handler(int, {{(__)?}}siginfo{{(_t)?}}*, void*) {{.*}}signal_malloc.cpp:[[@LINE+2]]
16 // CHECK: SUMMARY: ThreadSanitizer: signal-unsafe call inside of a signal{{.*}}handler
17 free(ptr: (void*)p);
18}
19
20int main() {
21 struct sigaction act = {};
22 act.sa_sigaction = &handler;
23 sigaction(SIGPROF, act: &act, oact: 0);
24 kill(pid: getpid(), SIGPROF);
25 sleep(seconds: 1); // let the signal handler run
26 fprintf(stderr, format: "DONE\n");
27 // CHECK: DONE
28 return 0;
29}
30
31

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