]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/tsan/map32bit.cc
Vendor import of compiler-rt trunk r256633:
[FreeBSD/FreeBSD.git] / test / tsan / map32bit.cc
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
2 #include "test.h"
3 #include <stdint.h>
4 #include <errno.h>
5 #include <sys/mman.h>
6
7 // Test for issue:
8 // https://github.com/google/sanitizers/issues/412
9
10 // MAP_32BIT flag for mmap is supported only for x86_64.
11 // XFAIL: mips64
12 // XFAIL: aarch64
13 // XFAIL: powerpc64
14
15 // MAP_32BIT doesn't exist on OS X.
16 // UNSUPPORTED: darwin
17
18 void *Thread(void *ptr) {
19   *(int*)ptr = 42;
20   barrier_wait(&barrier);
21   return 0;
22 }
23
24 int main() {
25   barrier_init(&barrier, 2);
26   void *ptr = mmap(0, 128 << 10, PROT_READ|PROT_WRITE,
27       MAP_32BIT|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
28   fprintf(stderr, "ptr=%p\n", ptr);
29   if (ptr == MAP_FAILED) {
30     fprintf(stderr, "mmap failed: %d\n", errno);
31     return 1;
32   }
33   if ((uintptr_t)ptr >= (1ull << 32)) {
34     fprintf(stderr, "ptr is too high\n");
35     return 1;
36   }
37   pthread_t t;
38   pthread_create(&t, 0, Thread, ptr);
39   barrier_wait(&barrier);
40   *(int*)ptr = 42;
41   pthread_join(t, 0);
42   munmap(ptr, 128 << 10);
43   fprintf(stderr, "DONE\n");
44 }
45
46 // CHECK: WARNING: ThreadSanitizer: data race
47 // CHECK: DONE
48