llvm-project
43 строки · 1.1 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "java.h"
3
4jptr varaddr;
5jptr lockaddr;
6jptr varaddr2;
7jptr lockaddr2;
8
9void *Thread(void *p) {
10barrier_wait(&barrier);
11__tsan_java_mutex_lock(lockaddr2);
12*(int*)varaddr2 = 42;
13__tsan_java_mutex_unlock(lockaddr2);
14return 0;
15}
16
17int main() {
18barrier_init(&barrier, 2);
19int const kHeapSize = 1024 * 1024;
20jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
21__tsan_java_init(jheap, kHeapSize);
22const int kBlockSize = 64;
23int const kMove = 1024;
24__tsan_java_alloc(jheap, kBlockSize);
25varaddr = jheap;
26lockaddr = jheap + 46;
27varaddr2 = varaddr + kMove;
28lockaddr2 = lockaddr + kMove;
29pthread_t th;
30pthread_create(&th, 0, Thread, 0);
31__tsan_java_mutex_lock(lockaddr);
32*(int*)varaddr = 43;
33__tsan_java_mutex_unlock(lockaddr);
34__tsan_java_move(varaddr, varaddr2, kBlockSize);
35barrier_wait(&barrier);
36pthread_join(th, 0);
37__tsan_java_free(varaddr2, kBlockSize);
38fprintf(stderr, "DONE\n");
39return __tsan_java_fini();
40}
41
42// CHECK-NOT: WARNING: ThreadSanitizer: data race
43// CHECK: DONE
44