blob: 8305e84930f3fa0550878129c762772f941ecc0a [file] [log] [blame]
Stephen Hines6a211c52014-07-21 00:49:56 -07001// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
Stephen Hines86277eb2015-03-23 12:06:32 -07002#include "test.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -07003#include <signal.h>
4#include <sys/types.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -07005#include <errno.h>
6
7pthread_t mainth;
8volatile int done;
9
10static void MyHandler(int, siginfo_t *s, void *c) {
11 errno = 1;
12 done = 1;
13}
14
15static void* sendsignal(void *p) {
Stephen Hines86277eb2015-03-23 12:06:32 -070016 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070017 pthread_kill(mainth, SIGPROF);
18 return 0;
19}
20
21static __attribute__((noinline)) void loop() {
Stephen Hines86277eb2015-03-23 12:06:32 -070022 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023 while (done == 0) {
24 volatile char *p = (char*)malloc(1);
25 p[0] = 0;
26 free((void*)p);
27 pthread_yield();
28 }
29}
30
31int main() {
Stephen Hines86277eb2015-03-23 12:06:32 -070032 barrier_init(&barrier, 2);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033 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
Stephen Hines6d186232014-11-26 17:56:19 -080045// CHECK: #0 MyHandler(int, {{(__)?}}siginfo{{(_t)?}}*, void*) {{.*}}signal_errno.cc
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046// CHECK: main
47// CHECK: SUMMARY: ThreadSanitizer: signal handler spoils errno{{.*}}MyHandler
48