1 // RUN: %clangxx_tsan -O1 %s -o %t && not %t 2>&1 | FileCheck %s 2 #include "java.h" 3 #include <unistd.h> 4 5 jptr varaddr; 6 jptr lockaddr; 7 8 void *Thread(void *p) { 9 __tsan_java_mutex_lock(lockaddr); 10 __tsan_java_mutex_lock(lockaddr); 11 __tsan_java_mutex_lock(lockaddr); 12 int rec = __tsan_java_mutex_unlock_rec(lockaddr); 13 if (rec != 3) { 14 printf("FAILED 0 rec=%d\n", rec); 15 exit(1); 16 } 17 *(int*)varaddr = 42; 18 sleep(2); 19 __tsan_java_mutex_lock_rec(lockaddr, rec); 20 __tsan_java_mutex_unlock(lockaddr); 21 __tsan_java_mutex_unlock(lockaddr); 22 __tsan_java_mutex_unlock(lockaddr); 23 return 0; 24 } 25 26 int main() { 27 int const kHeapSize = 1024 * 1024; 28 void *jheap = malloc(kHeapSize); 29 __tsan_java_init((jptr)jheap, kHeapSize); 30 const int kBlockSize = 16; 31 __tsan_java_alloc((jptr)jheap, kBlockSize); 32 varaddr = (jptr)jheap; 33 *(int*)varaddr = 0; 34 lockaddr = (jptr)jheap + 8; 35 pthread_t th; 36 pthread_create(&th, 0, Thread, 0); 37 sleep(1); 38 __tsan_java_mutex_lock(lockaddr); 39 *(int*)varaddr = 43; 40 __tsan_java_mutex_unlock(lockaddr); 41 pthread_join(th, 0); 42 __tsan_java_free((jptr)jheap, kBlockSize); 43 printf("OK\n"); 44 return __tsan_java_fini(); 45 } 46 47 // CHECK: WARNING: ThreadSanitizer: data race 48 // CHECK-NOT: FAILED 49