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://code.google.com/p/thread-sanitizer/issues/detail?id=5 9 10 // MAP_32BIT flag for mmap is supported only for x86_64. 11 // XFAIL: mips64 12 13 void *Thread(void *ptr) { 14 *(int*)ptr = 42; 15 barrier_wait(&barrier); 16 return 0; 17 } 18 19 int main() { 20 barrier_init(&barrier, 2); 21 void *ptr = mmap(0, 128 << 10, PROT_READ|PROT_WRITE, 22 MAP_32BIT|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 23 fprintf(stderr, "ptr=%p\n", ptr); 24 if (ptr == MAP_FAILED) { 25 fprintf(stderr, "mmap failed: %d\n", errno); 26 return 1; 27 } 28 if ((uintptr_t)ptr >= (1ull << 32)) { 29 fprintf(stderr, "ptr is too high\n"); 30 return 1; 31 } 32 pthread_t t; 33 pthread_create(&t, 0, Thread, ptr); 34 barrier_wait(&barrier); 35 *(int*)ptr = 42; 36 pthread_join(t, 0); 37 munmap(ptr, 128 << 10); 38 fprintf(stderr, "DONE\n"); 39 } 40 41 // CHECK: WARNING: ThreadSanitizer: data race 42 // CHECK: DONE 43 44