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