1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 // Defined by tsan.
6 extern "C" FILE *__interceptor_fopen(const char *file, const char *mode);
7 extern "C" int __interceptor_fileno(FILE *f);
8 
fopen(const char * file,const char * mode)9 extern "C" FILE *fopen(const char *file, const char *mode) {
10   static int first = 0;
11   if (__sync_lock_test_and_set(&first, 1) == 0)
12     printf("user fopen\n");
13   return __interceptor_fopen(file, mode);
14 }
15 
fileno(FILE * f)16 extern "C" int fileno(FILE *f) {
17   static int first = 0;
18   if (__sync_lock_test_and_set(&first, 1) == 0)
19     printf("user fileno\n");
20   return 1;
21 }
22 
main()23 int main() {
24   FILE *f = fopen("/dev/zero", "r");
25   if (f) {
26     char buf;
27     fread(&buf, 1, 1, f);
28     fclose(f);
29   }
30 }
31 
32 // CHECK: user fopen
33 // CHECK-NOT: ThreadSanitizer
34 
35