Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame^] | 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s |
| 2 | #include <pthread.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stddef.h> |
| 5 | #include <stdint.h> |
| 6 | #include <unistd.h> |
| 7 | #include <errno.h> |
| 8 | #include <sys/mman.h> |
| 9 | |
| 10 | // Test for issue: |
| 11 | // https://code.google.com/p/thread-sanitizer/issues/detail?id=5 |
| 12 | |
| 13 | void *Thread(void *ptr) { |
| 14 | *(int*)ptr = 42; |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | int main() { |
| 19 | void *ptr = mmap(0, 128 << 10, PROT_READ|PROT_WRITE, |
| 20 | MAP_32BIT|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 21 | fprintf(stderr, "ptr=%p\n", ptr); |
| 22 | if (ptr == MAP_FAILED) { |
| 23 | fprintf(stderr, "mmap failed: %d\n", errno); |
| 24 | return 1; |
| 25 | } |
| 26 | if ((uintptr_t)ptr >= (1ull << 32)) { |
| 27 | fprintf(stderr, "ptr is too high\n"); |
| 28 | return 1; |
| 29 | } |
| 30 | pthread_t t; |
| 31 | pthread_create(&t, 0, Thread, ptr); |
| 32 | sleep(1); |
| 33 | *(int*)ptr = 42; |
| 34 | pthread_join(t, 0); |
| 35 | munmap(ptr, 128 << 10); |
| 36 | fprintf(stderr, "DONE\n"); |
| 37 | } |
| 38 | |
| 39 | // CHECK: WARNING: ThreadSanitizer: data race |
| 40 | // CHECK: DONE |
| 41 | |