[llvm-commits] [compiler-rt] r171958 - in /compiler-rt/trunk/lib/tsan: lit_tests/user_fopen.cc lit_tests/user_malloc.cc rtl/tsan_interceptors.cc rtl/tsan_stat.cc rtl/tsan_stat.h

Dmitry Vyukov dvyukov at google.com
Wed Jan 9 00:22:06 PST 2013


Author: dvyukov
Date: Wed Jan  9 02:22:06 2013
New Revision: 171958

URL: http://llvm.org/viewvc/llvm-project?rev=171958&view=rev
Log:
tsan: fix crash when user defines own fopen/fileno

Added:
    compiler-rt/trunk/lib/tsan/lit_tests/user_fopen.cc
    compiler-rt/trunk/lib/tsan/lit_tests/user_malloc.cc
Modified:
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h

Added: compiler-rt/trunk/lib/tsan/lit_tests/user_fopen.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/user_fopen.cc?rev=171958&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/user_fopen.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/user_fopen.cc Wed Jan  9 02:22:06 2013
@@ -0,0 +1,34 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <stdio.h>
+#include <stdlib.h>
+
+// defined by tsan.
+extern "C" FILE *__interceptor_fopen(const char *file, const char *mode);
+extern "C" int __interceptor_fileno(FILE *f);
+
+extern "C" FILE *fopen(const char *file, const char *mode) {
+  static int first = 0;
+  if (__sync_lock_test_and_set(&first, 1) == 0)
+    printf("user fopen\n");
+  return __interceptor_fopen(file, mode);
+}
+
+extern "C" int fileno(FILE *f) {
+  static int first = 0;
+  if (__sync_lock_test_and_set(&first, 1) == 0)
+    printf("user fileno\n");
+  return __interceptor_fileno(f);
+}
+
+int main() {
+  FILE *f = fopen("/dev/zero", "r");
+  if (f) {
+    char buf;
+    fread(&buf, 1, 1, f);
+    fclose(f);
+  }
+}
+
+// CHECK: user fopen
+// CHECK-NOT: ThreadSanitizer
+

Added: compiler-rt/trunk/lib/tsan/lit_tests/user_malloc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/lit_tests/user_malloc.cc?rev=171958&view=auto
==============================================================================
--- compiler-rt/trunk/lib/tsan/lit_tests/user_malloc.cc (added)
+++ compiler-rt/trunk/lib/tsan/lit_tests/user_malloc.cc Wed Jan  9 02:22:06 2013
@@ -0,0 +1,27 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %t 2>&1 | FileCheck %s
+#include <stdio.h>
+
+// defined by tsan.
+extern "C" void *__interceptor_malloc(unsigned long size);
+extern "C" void __interceptor_free(void *p);
+
+extern "C" void *malloc(unsigned long size) {
+  static int first = 0;
+  if (__sync_lock_test_and_set(&first, 1) == 0)
+    printf("user malloc\n");
+  return __interceptor_malloc(size);
+}
+
+extern "C" void free(void *p) {
+  __interceptor_free(p);
+}
+
+int main() {
+  volatile char *p = (char*)malloc(10);
+  p[0] = 0;
+  free((void*)p);
+}
+
+// CHECK: user malloc
+// CHECK-NOT: ThreadSanitizer
+

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc?rev=171958&r1=171957&r2=171958&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Wed Jan  9 02:22:06 2013
@@ -55,7 +55,6 @@
 extern "C" void _exit(int status);
 extern "C" int __cxa_atexit(void (*func)(void *arg), void *arg, void *dso);
 extern "C" int *__errno_location();
-extern "C" int fileno(void *stream);
 const int PTHREAD_MUTEX_RECURSIVE = 1;
 const int PTHREAD_MUTEX_RECURSIVE_NP = 1;
 const int kPthreadAttrSize = 56;
@@ -1367,12 +1366,17 @@
   return res;
 }
 
+TSAN_INTERCEPTOR(int, fileno, void *f) {
+  SCOPED_TSAN_INTERCEPTOR(fileno, f);
+  return REAL(fileno)(f);
+}
+
 TSAN_INTERCEPTOR(void*, fopen, char *path, char *mode) {
   SCOPED_TSAN_INTERCEPTOR(fopen, path, mode);
   void *res = REAL(fopen)(path, mode);
   Acquire(thr, pc, File2addr(path));
   if (res) {
-    int fd = fileno(res);
+    int fd = REAL(fileno)(res);
     if (fd >= 0)
       FdFileCreate(thr, pc, fd);
   }
@@ -1382,14 +1386,14 @@
 TSAN_INTERCEPTOR(void*, freopen, char *path, char *mode, void *stream) {
   SCOPED_TSAN_INTERCEPTOR(freopen, path, mode, stream);
   if (stream) {
-    int fd = fileno(stream);
+    int fd = REAL(fileno)(stream);
     if (fd >= 0)
       FdClose(thr, pc, fd);
   }
   void *res = REAL(freopen)(path, mode, stream);
   Acquire(thr, pc, File2addr(path));
   if (res) {
-    int fd = fileno(res);
+    int fd = REAL(fileno)(res);
     if (fd >= 0)
       FdFileCreate(thr, pc, fd);
   }
@@ -1399,7 +1403,7 @@
 TSAN_INTERCEPTOR(int, fclose, void *stream) {
   SCOPED_TSAN_INTERCEPTOR(fclose, stream);
   if (stream) {
-    int fd = fileno(stream);
+    int fd = REAL(fileno)(stream);
     if (fd >= 0)
       FdClose(thr, pc, fd);
   }
@@ -1819,6 +1823,7 @@
   TSAN_INTERCEPT(recvmsg);
 
   TSAN_INTERCEPT(unlink);
+  TSAN_INTERCEPT(fileno);
   TSAN_INTERCEPT(fopen);
   TSAN_INTERCEPT(freopen);
   TSAN_INTERCEPT(fclose);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc?rev=171958&r1=171957&r2=171958&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc Wed Jan  9 02:22:06 2013
@@ -218,6 +218,7 @@
   name[StatInt_recv]                     = "  recv                            ";
   name[StatInt_recvmsg]                  = "  recvmsg                         ";
   name[StatInt_unlink]                   = "  unlink                          ";
+  name[StatInt_fileno]                   = "  fileno                          ";
   name[StatInt_fopen]                    = "  fopen                           ";
   name[StatInt_freopen]                  = "  freopen                         ";
   name[StatInt_fclose]                   = "  fclose                          ";

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h?rev=171958&r1=171957&r2=171958&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h Wed Jan  9 02:22:06 2013
@@ -213,6 +213,7 @@
   StatInt_recv,
   StatInt_recvmsg,
   StatInt_unlink,
+  StatInt_fileno,
   StatInt_fopen,
   StatInt_freopen,
   StatInt_fclose,





More information about the llvm-commits mailing list