[compiler-rt] r224697 - tsan: add acquire/release functions to java interface
Dmitry Vyukov
dvyukov at google.com
Mon Dec 22 01:44:56 PST 2014
Author: dvyukov
Date: Mon Dec 22 03:44:56 2014
New Revision: 224697
URL: http://llvm.org/viewvc/llvm-project?rev=224697&view=rev
Log:
tsan: add acquire/release functions to java interface
they are required to handle synchronization on volatile/final fields
Added:
compiler-rt/trunk/test/tsan/java_volatile.cc
Modified:
compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.cc
compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.h
compiler-rt/trunk/test/tsan/java.h
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.cc?rev=224697&r1=224696&r2=224697&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.cc Mon Dec 22 03:44:56 2014
@@ -219,3 +219,33 @@ int __tsan_java_mutex_unlock_rec(jptr ad
return MutexUnlock(thr, pc, addr, true);
}
+
+void __tsan_java_acquire(jptr addr) {
+ SCOPED_JAVA_FUNC(__tsan_java_acquire);
+ DPrintf("#%d: java_acquire(%p)\n", thr->tid, addr);
+ CHECK_NE(jctx, 0);
+ CHECK_GE(addr, jctx->heap_begin);
+ CHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
+
+ Acquire(thr, caller_pc, addr);
+}
+
+void __tsan_java_release(jptr addr) {
+ SCOPED_JAVA_FUNC(__tsan_java_release);
+ DPrintf("#%d: java_release(%p)\n", thr->tid, addr);
+ CHECK_NE(jctx, 0);
+ CHECK_GE(addr, jctx->heap_begin);
+ CHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
+
+ Release(thr, caller_pc, addr);
+}
+
+void __tsan_java_release_store(jptr addr) {
+ SCOPED_JAVA_FUNC(__tsan_java_release);
+ DPrintf("#%d: java_release_store(%p)\n", thr->tid, addr);
+ CHECK_NE(jctx, 0);
+ CHECK_GE(addr, jctx->heap_begin);
+ CHECK_LT(addr, jctx->heap_begin + jctx->heap_size);
+
+ ReleaseStore(thr, caller_pc, addr);
+}
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.h?rev=224697&r1=224696&r2=224697&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interface_java.h Mon Dec 22 03:44:56 2014
@@ -79,6 +79,14 @@ void __tsan_java_mutex_lock_rec(jptr add
// the same recursion level.
int __tsan_java_mutex_unlock_rec(jptr addr) INTERFACE_ATTRIBUTE;
+// Raw acquire/release primitives.
+// Can be used to establish happens-before edges on volatile/final fields,
+// in atomic operations, etc. release_store is the same as release, but it
+// breaks release sequence on addr (see C++ standard 1.10/7 for details).
+void __tsan_java_acquire(jptr addr) INTERFACE_ATTRIBUTE;
+void __tsan_java_release(jptr addr) INTERFACE_ATTRIBUTE;
+void __tsan_java_release_store(jptr addr) INTERFACE_ATTRIBUTE;
+
#ifdef __cplusplus
} // extern "C"
#endif
Modified: compiler-rt/trunk/test/tsan/java.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/java.h?rev=224697&r1=224696&r2=224697&view=diff
==============================================================================
--- compiler-rt/trunk/test/tsan/java.h (original)
+++ compiler-rt/trunk/test/tsan/java.h Mon Dec 22 03:44:56 2014
@@ -18,4 +18,7 @@ void __tsan_java_mutex_read_lock(jptr ad
void __tsan_java_mutex_read_unlock(jptr addr);
void __tsan_java_mutex_lock_rec(jptr addr, int rec);
int __tsan_java_mutex_unlock_rec(jptr addr);
+int __tsan_java_acquire(jptr addr);
+int __tsan_java_release(jptr addr);
+int __tsan_java_release_store(jptr addr);
}
Added: compiler-rt/trunk/test/tsan/java_volatile.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/java_volatile.cc?rev=224697&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/java_volatile.cc (added)
+++ compiler-rt/trunk/test/tsan/java_volatile.cc Mon Dec 22 03:44:56 2014
@@ -0,0 +1,42 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
+#include "java.h"
+#include <unistd.h>
+
+jptr varaddr;
+jptr lockaddr;
+
+void *Thread(void *p) {
+ while (__atomic_load_n((int*)lockaddr, __ATOMIC_RELAXED) == 0)
+ usleep(1000);
+ __tsan_java_acquire(lockaddr);
+ *(int*)varaddr = 42;
+ return 0;
+}
+
+int main() {
+ int const kHeapSize = 1024 * 1024;
+ jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
+ __tsan_java_init(jheap, kHeapSize);
+ const int kBlockSize = 16;
+ __tsan_java_alloc(jheap, kBlockSize);
+ varaddr = jheap;
+ lockaddr = jheap + 8;
+ pthread_t th;
+ pthread_create(&th, 0, Thread, 0);
+ *(int*)varaddr = 43;
+ __tsan_java_release(lockaddr);
+ __atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
+ pthread_join(th, 0);
+ *(int*)lockaddr = 0;
+ pthread_create(&th, 0, Thread, 0);
+ *(int*)varaddr = 43;
+ __tsan_java_release_store(lockaddr);
+ __atomic_store_n((int*)lockaddr, 1, __ATOMIC_RELAXED);
+ pthread_join(th, 0);
+ __tsan_java_free(jheap, kBlockSize);
+ fprintf(stderr, "DONE\n");
+ return __tsan_java_fini();
+}
+
+// CHECK-NOT: WARNING: ThreadSanitizer: data race
+// CHECK: DONE
More information about the llvm-commits
mailing list