56 lines · cpp
1// RUN: %clangxx_tsan -O0 %s -o %t2// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-RACE3// 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-RACE5// RUN: %run %t ignore 2>&1 | FileCheck %s --check-prefix=CHECK-IGNORE6 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. We15// only want to check races in the mmap'd memory to isolate the test that mmap16// respects ignore annotations.17std::atomic<int*> global_p;18 19void 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 write26 global_p.store((int *)p, std::memory_order_relaxed);27 barrier_wait(&barrier);28}29 30void *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 memory37int 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 race53// CHECK-RACE: OK54// CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race55// CHECK-IGNORE: OK56