1 // RUN: %clangxx_tsan -O0 %s -o %t
2 // RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RACE
3 // Test size larger than clear_shadow_mmap_threshold, which is handled differently.
4 // RUN: not %run %t - 262144 2>&1 | FileCheck %s --check-prefix=CHECK-RACE
5 // RUN: %run %t ignore 2>&1 | FileCheck %s --check-prefix=CHECK-IGNORE
14 // Use atomic to ensure we do not have a race for the pointer value itself. We
15 // only want to check races in the mmap'd memory to isolate the test that mmap
16 // respects ignore annotations.
17 std::atomic
<int*> global_p
;
19 void mmap_ignored(bool ignore
, size_t size
) {
20 if (ignore
) AnnotateIgnoreWritesBegin(__FILE__
, __LINE__
);
22 mmap(0, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
23 if (ignore
) AnnotateIgnoreWritesEnd(__FILE__
, __LINE__
);
25 // Use relaxed to retain the race between the mmap call and the memory write
26 global_p
.store((int *)p
, std::memory_order_relaxed
);
27 barrier_wait(&barrier
);
30 void *WriteToMemory(void *unused
) {
31 barrier_wait(&barrier
);
36 // Create race between allocating (mmap) and writing memory
37 int main(int argc
, const char *argv
[]) {
38 bool ignore
= (argc
> 1) && (strcmp(argv
[1], "ignore") == 0);
39 size_t size
= argc
> 2 ? atoi(argv
[2]) : sysconf(_SC_PAGESIZE
);
41 barrier_init(&barrier
, 2);
43 pthread_create(&t
, 0, WriteToMemory
, 0);
44 mmap_ignored(ignore
, size
);
47 assert(global_p
[0] == 7);
52 // CHECK-RACE: WARNING: ThreadSanitizer: data race
54 // CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race