35 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s2#include <stdio.h>3#include <stdlib.h>4 5// Defined by tsan.6extern "C" FILE *__interceptor_fopen(const char *file, const char *mode);7extern "C" int __interceptor_fileno(FILE *f);8 9extern "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 16extern "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 23int 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 fopen33// CHECK-NOT: ThreadSanitizer34 35