Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / ignored-interceptors-mmap.cpp
blob2b35627229df7495b4eaac9da27c0eca91ce71b5
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
7 #include <sys/mman.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <atomic>
12 #include "test.h"
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__);
21 void *p =
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);
32 global_p[0] = 7;
33 return 0;
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);
42 pthread_t t;
43 pthread_create(&t, 0, WriteToMemory, 0);
44 mmap_ignored(ignore, size);
45 pthread_join(t, 0);
47 assert(global_p[0] == 7);
48 printf("OK\n");
49 return 0;
52 // CHECK-RACE: WARNING: ThreadSanitizer: data race
53 // CHECK-RACE: OK
54 // CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race
55 // CHECK-IGNORE: OK