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 |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 2 | #include "test.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 3 | #include <stdint.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 10 | // MAP_32BIT flag for mmap is supported only for x86_64. |
| 11 | // XFAIL: mips64 |
| 12 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 13 | void *Thread(void *ptr) { |
| 14 | *(int*)ptr = 42; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 15 | barrier_wait(&barrier); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 16 | return 0; |
| 17 | } |
| 18 | |
| 19 | int main() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 20 | barrier_init(&barrier, 2); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 34 | barrier_wait(&barrier); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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 | |