[compiler-rt] r191153 - tsan: ignore malloc/free/strdup when called from libjvm

Dmitry Vyukov dvyukov at google.com
Sat Sep 21 16:44:19 PDT 2013


Author: dvyukov
Date: Sat Sep 21 18:44:19 2013
New Revision: 191153

URL: http://llvm.org/viewvc/llvm-project?rev=191153&view=rev
Log:
tsan: ignore malloc/free/strdup when called from libjvm

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

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=191153&r1=191152&r2=191153&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Sat Sep 21 18:44:19 2013
@@ -444,7 +444,7 @@ TSAN_INTERCEPTOR(void, siglongjmp, uptr
 }
 
 TSAN_INTERCEPTOR(void*, malloc, uptr size) {
-  if (cur_thread()->in_symbolizer)
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC()))
     return __libc_malloc(size);
   void *p = 0;
   {
@@ -461,7 +461,7 @@ TSAN_INTERCEPTOR(void*, __libc_memalign,
 }
 
 TSAN_INTERCEPTOR(void*, calloc, uptr size, uptr n) {
-  if (cur_thread()->in_symbolizer)
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC()))
     return __libc_calloc(size, n);
   if (__sanitizer::CallocShouldReturnNullDueToOverflow(size, n))
     return AllocatorReturnNull();
@@ -477,7 +477,7 @@ TSAN_INTERCEPTOR(void*, calloc, uptr siz
 }
 
 TSAN_INTERCEPTOR(void*, realloc, void *p, uptr size) {
-  if (cur_thread()->in_symbolizer)
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC()))
     return __libc_realloc(p, size);
   if (p)
     invoke_free_hook(p);
@@ -492,7 +492,7 @@ TSAN_INTERCEPTOR(void*, realloc, void *p
 TSAN_INTERCEPTOR(void, free, void *p) {
   if (p == 0)
     return;
-  if (cur_thread()->in_symbolizer)
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC()))
     return __libc_free(p);
   invoke_free_hook(p);
   SCOPED_INTERCEPTOR_RAW(free, p);
@@ -502,7 +502,7 @@ TSAN_INTERCEPTOR(void, free, void *p) {
 TSAN_INTERCEPTOR(void, cfree, void *p) {
   if (p == 0)
     return;
-  if (cur_thread()->in_symbolizer)
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC()))
     return __libc_free(p);
   invoke_free_hook(p);
   SCOPED_INTERCEPTOR_RAW(cfree, p);
@@ -511,11 +511,13 @@ TSAN_INTERCEPTOR(void, cfree, void *p) {
 
 TSAN_INTERCEPTOR(uptr, malloc_usable_size, void *p) {
   SCOPED_INTERCEPTOR_RAW(malloc_usable_size, p);
+  if (libjvm_check(pc))
+    return malloc_usable_size(p);
   return user_alloc_usable_size(thr, pc, p);
 }
 
 #define OPERATOR_NEW_BODY(mangled_name) \
-  if (cur_thread()->in_symbolizer) \
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC())) \
     return __libc_malloc(size); \
   void *p = 0; \
   {  \
@@ -551,7 +553,7 @@ void *operator new[](__sanitizer::uptr s
 
 #define OPERATOR_DELETE_BODY(mangled_name) \
   if (ptr == 0) return;  \
-  if (cur_thread()->in_symbolizer) \
+  if (cur_thread()->in_symbolizer || libjvm_check(GET_CALLER_PC())) \
     return __libc_free(ptr); \
   invoke_free_hook(ptr);  \
   SCOPED_INTERCEPTOR_RAW(mangled_name, ptr);  \
@@ -683,6 +685,21 @@ TSAN_INTERCEPTOR(const char*, strstr, co
   return res;
 }
 
+TSAN_INTERCEPTOR(char*, strdup, const char *str) {
+  SCOPED_TSAN_INTERCEPTOR(strdup, str);
+  if (libjvm_check(pc)) {
+    // The memory must come from libc malloc,
+    // and we must not instrument accesses in this case.
+    uptr n = internal_strlen(str) + 1;
+    void *p = __libc_malloc(n);
+    if (p == 0)
+      return 0;
+    return (char*)internal_memcpy(p, str, n);
+  }
+  // strdup will call malloc, so no instrumentation is required here.
+  return REAL(strdup)(str);
+}
+
 static bool fix_mmap_addr(void **addr, long_t sz, int flags) {
   if (*addr) {
     if (!IsAppMem((uptr)*addr) || !IsAppMem((uptr)*addr + sz - 1)) {
@@ -2037,6 +2054,7 @@ void InitializeInterceptors() {
   TSAN_INTERCEPT(strcpy);  // NOLINT
   TSAN_INTERCEPT(strncpy);
   TSAN_INTERCEPT(strstr);
+  TSAN_INTERCEPT(strdup);
 
   TSAN_INTERCEPT(pthread_create);
   TSAN_INTERCEPT(pthread_join);

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=191153&r1=191152&r2=191153&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.cc Sat Sep 21 18:44:19 2013
@@ -138,6 +138,7 @@ void StatOutput(u64 *stat) {
   name[StatInt_strcpy]                   = "  strcpy                          ";
   name[StatInt_strncpy]                  = "  strncpy                         ";
   name[StatInt_strstr]                   = "  strstr                          ";
+  name[StatInt_strdup]                   = "  strdup                          ";
   name[StatInt_strcasecmp]               = "  strcasecmp                      ";
   name[StatInt_strncasecmp]              = "  strncasecmp                     ";
   name[StatInt_atexit]                   = "  atexit                          ";

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=191153&r1=191152&r2=191153&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_stat.h Sat Sep 21 18:44:19 2013
@@ -137,6 +137,7 @@ enum StatType {
   StatInt_strcasecmp,
   StatInt_strncasecmp,
   StatInt_strstr,
+  StatInt_strdup,
   StatInt_atexit,
   StatInt___cxa_guard_acquire,
   StatInt___cxa_guard_release,





More information about the llvm-commits mailing list