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: target=mips{{.*}} |
12 | // XFAIL: target=aarch64{{.*}} |
13 | // XFAIL: target=powerpc64{{.*}} |
14 | // XFAIL: target=s390x{{.*}} |
15 | // XFAIL: target=loongarch64{{.*}} |
16 | // XFAIL: target=riscv64{{.*}} |
17 | |
18 | // MAP_32BIT doesn't exist on OS X and NetBSD. |
19 | // UNSUPPORTED: darwin,target={{.*netbsd.*}} |
20 | |
21 | void *Thread(void *ptr) { |
22 | *(int*)ptr = 42; |
23 | barrier_wait(barrier: &barrier); |
24 | return 0; |
25 | } |
26 | |
27 | int main() { |
28 | barrier_init(barrier: &barrier, count: 2); |
29 | void *ptr = mmap(addr: 0, len: 128 << 10, PROT_READ|PROT_WRITE, |
30 | MAP_32BIT|MAP_PRIVATE|MAP_ANONYMOUS, fd: -1, offset: 0); |
31 | fprintf(stderr, format: "ptr=%p\n" , ptr); |
32 | if (ptr == MAP_FAILED) { |
33 | fprintf(stderr, format: "mmap failed: %d\n" , errno); |
34 | return 1; |
35 | } |
36 | if ((uintptr_t)ptr >= (1ull << 32)) { |
37 | fprintf(stderr, format: "ptr is too high\n" ); |
38 | return 1; |
39 | } |
40 | pthread_t t; |
41 | pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: ptr); |
42 | barrier_wait(barrier: &barrier); |
43 | *(int*)ptr = 42; |
44 | pthread_join(th: t, thread_return: 0); |
45 | munmap(addr: ptr, len: 128 << 10); |
46 | fprintf(stderr, format: "DONE\n" ); |
47 | } |
48 | |
49 | // CHECK: WARNING: ThreadSanitizer: data race |
50 | // CHECK: DONE |
51 | |