xref: /llvm-project/compiler-rt/test/tsan/ignored-interceptors-mmap.cpp (revision 764b35d89f57a9052d84898422a865dc2e08edca)
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
6 
7 #include <sys/mman.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <atomic>
11 
12 #include "test.h"
13 
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;
18 
mmap_ignored(bool ignore,size_t size)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__);
24 
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);
28 }
29 
WriteToMemory(void * unused)30 void *WriteToMemory(void *unused) {
31   barrier_wait(&barrier);
32   global_p[0] = 7;
33   return 0;
34 }
35 
36 // Create race between allocating (mmap) and writing memory
main(int argc,const char * argv[])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);
40 
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);
46 
47   assert(global_p[0] == 7);
48   printf("OK\n");
49   return 0;
50 }
51 
52 // CHECK-RACE: WARNING: ThreadSanitizer: data race
53 // CHECK-RACE: OK
54 // CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race
55 // CHECK-IGNORE: OK
56