File: ignored-interceptors-mmap.cpp

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (55 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download | duplicates (25)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// RUN: %clangxx_tsan -O0 %s -o %t
// RUN: not %run %t          2>&1 | FileCheck %s --check-prefix=CHECK-RACE
// Test size larger than clear_shadow_mmap_threshold, which is handled differently.
// RUN: not %run %t - 262144 2>&1 | FileCheck %s --check-prefix=CHECK-RACE
// RUN:     %run %t ignore   2>&1 | FileCheck %s --check-prefix=CHECK-IGNORE

#include <sys/mman.h>
#include <string.h>
#include <assert.h>
#include <atomic>

#include "test.h"

// Use atomic to ensure we do not have a race for the pointer value itself.  We
// only want to check races in the mmap'd memory to isolate the test that mmap
// respects ignore annotations.
std::atomic<int*> global_p;

void mmap_ignored(bool ignore, size_t size) {
  if (ignore) AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
  void *p =
      mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  if (ignore) AnnotateIgnoreWritesEnd(__FILE__, __LINE__);

  // Use relaxed to retain the race between the mmap call and the memory write
  global_p.store((int *)p, std::memory_order_relaxed);
  barrier_wait(&barrier);
}

void *WriteToMemory(void *unused) {
  barrier_wait(&barrier);
  global_p[0] = 7;
  return 0;
}

// Create race between allocating (mmap) and writing memory
int main(int argc, const char *argv[]) {
  bool ignore = (argc > 1) && (strcmp(argv[1], "ignore") == 0);
  size_t size = argc > 2 ? atoi(argv[2]) : sysconf(_SC_PAGESIZE);

  barrier_init(&barrier, 2);
  pthread_t t;
  pthread_create(&t, 0, WriteToMemory, 0);
  mmap_ignored(ignore, size);
  pthread_join(t, 0);

  assert(global_p[0] == 7);
  printf("OK\n");
  return 0;
}

// CHECK-RACE: WARNING: ThreadSanitizer: data race
// CHECK-RACE: OK
// CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race
// CHECK-IGNORE: OK