tsan: add __tsan_java_finalize interface function
authorDmitry Vyukov <dvyukov@google.com>
Fri, 27 Jun 2014 00:47:38 +0000 (00:47 +0000)
committerDmitry Vyukov <dvyukov@google.com>
Fri, 27 Jun 2014 00:47:38 +0000 (00:47 +0000)
It is required to prevent false positives between object ctor and finalizer,
as otherwise they look completely unsynchronized.

llvm-svn: 211829

compiler-rt/lib/tsan/rtl/tsan_interface_java.cc
compiler-rt/lib/tsan/rtl/tsan_interface_java.h
compiler-rt/test/tsan/java.h
compiler-rt/test/tsan/java_finalizer.cc [new file with mode: 0644]

index ee61018..e63b93f 100644 (file)
@@ -142,6 +142,12 @@ void __tsan_java_move(jptr src, jptr dst, jptr size) {
   }
 }
 
+void __tsan_java_finalize() {
+  SCOPED_JAVA_FUNC(__tsan_java_finalize);
+  DPrintf("#%d: java_mutex_finalize()\n", thr->tid);
+  AcquireGlobal(thr, 0);
+}
+
 void __tsan_java_mutex_lock(jptr addr) {
   SCOPED_JAVA_FUNC(__tsan_java_mutex_lock);
   DPrintf("#%d: java_mutex_lock(%p)\n", thr->tid, addr);
index 9ac78e0..6a83885 100644 (file)
@@ -52,6 +52,11 @@ void __tsan_java_free(jptr ptr, jptr size) INTERFACE_ATTRIBUTE;
 // Can be aggregated for several objects (preferably).
 // The ranges must not overlap.
 void __tsan_java_move(jptr src, jptr dst, jptr size) INTERFACE_ATTRIBUTE;
+// This function must be called on the finalizer thread
+// before executing a batch of finalizers.
+// It ensures necessary synchronization between
+// java object creation and finalization.
+void __tsan_java_finalize() INTERFACE_ATTRIBUTE;
 
 // Mutex lock.
 // Addr is any unique address associated with the mutex.
index 7aa0bca..d986d08 100644 (file)
@@ -11,6 +11,7 @@ int  __tsan_java_fini();
 void __tsan_java_alloc(jptr ptr, jptr size);
 void __tsan_java_free(jptr ptr, jptr size);
 void __tsan_java_move(jptr src, jptr dst, jptr size);
+void __tsan_java_finalize();
 void __tsan_java_mutex_lock(jptr addr);
 void __tsan_java_mutex_unlock(jptr addr);
 void __tsan_java_mutex_read_lock(jptr addr);
diff --git a/compiler-rt/test/tsan/java_finalizer.cc b/compiler-rt/test/tsan/java_finalizer.cc
new file mode 100644 (file)
index 0000000..706b946
--- /dev/null
@@ -0,0 +1,27 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %run %t | FileCheck %s
+#include "java.h"
+
+void *Thread(void *p) {
+  sleep(1);
+  __tsan_java_finalize();
+  *(int*)p = 42;
+  return 0;
+}
+
+int main() {
+  int const kHeapSize = 1024 * 1024;
+  void *jheap = (char*)malloc(kHeapSize + 8) + 8;
+  __tsan_java_init((jptr)jheap, kHeapSize);
+  const int kBlockSize = 16;
+  __tsan_java_alloc((jptr)jheap, kBlockSize);
+  pthread_t th;
+  pthread_create(&th, 0, Thread, jheap);
+  *(int*)jheap = 43;
+  pthread_join(th, 0);
+  __tsan_java_free((jptr)jheap, kBlockSize);
+  fprintf(stderr, "DONE\n");
+  return __tsan_java_fini();
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer: data race
+// CHECK: DONE