Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 1 | // Test that mmap (without MAP_FIXED) always returns valid application addresses. |
| 2 | // RUN: %clangxx_msan -O0 %s -o %t && %run %t |
| 3 | // RUN: %clangxx_msan -O0 -fsanitize-memory-track-origins %s -o %t && %run %t |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <errno.h> |
| 7 | #include <stdint.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <stdio.h> |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 10 | #include <stdlib.h> |
| 11 | #include "test.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 12 | |
| 13 | bool AddrIsApp(void *p) { |
| 14 | uintptr_t addr = (uintptr_t)p; |
| 15 | #if defined(__FreeBSD__) && defined(__x86_64__) |
| 16 | return addr < 0x010000000000ULL || addr >= 0x600000000000ULL; |
| 17 | #elif defined(__x86_64__) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 18 | return (addr >= 0x000000000000ULL && addr < 0x010000000000ULL) || |
| 19 | (addr >= 0x510000000000ULL && addr < 0x600000000000ULL) || |
| 20 | (addr >= 0x700000000000ULL && addr < 0x800000000000ULL); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 21 | #elif defined(__mips64) |
| 22 | return addr >= 0x00e000000000ULL; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 23 | #elif defined(__powerpc64__) |
| 24 | return addr < 0x000100000000ULL || addr >= 0x300000000000ULL; |
| 25 | #elif defined(__aarch64__) |
| 26 | |
| 27 | struct AddrMapping { |
| 28 | uintptr_t start; |
| 29 | uintptr_t end; |
| 30 | } mappings[] = { |
| 31 | {0x05000000000ULL, 0x06000000000ULL}, |
| 32 | {0x07000000000ULL, 0x08000000000ULL}, |
| 33 | {0x0F000000000ULL, 0x10000000000ULL}, |
| 34 | {0x11000000000ULL, 0x12000000000ULL}, |
| 35 | {0x20000000000ULL, 0x21000000000ULL}, |
| 36 | {0x2A000000000ULL, 0x2B000000000ULL}, |
| 37 | {0x2E000000000ULL, 0x2F000000000ULL}, |
| 38 | {0x3B000000000ULL, 0x3C000000000ULL}, |
| 39 | {0x3F000000000ULL, 0x40000000000ULL}, |
| 40 | }; |
| 41 | const size_t mappingsSize = sizeof (mappings) / sizeof (mappings[0]); |
| 42 | |
| 43 | for (int i=0; i<mappingsSize; ++i) |
| 44 | if (addr >= mappings[i].start && addr < mappings[i].end) |
| 45 | return true; |
| 46 | return false; |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 47 | #endif |
| 48 | } |
| 49 | |
| 50 | int main() { |
| 51 | // Large enough to quickly exhaust the entire address space. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 52 | #if defined(__mips64) || defined(__aarch64__) |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 53 | const size_t kMapSize = 0x100000000ULL; |
| 54 | #else |
| 55 | const size_t kMapSize = 0x1000000000ULL; |
| 56 | #endif |
| 57 | int success_count = 0; |
| 58 | while (true) { |
| 59 | void *p = mmap(0, kMapSize, PROT_WRITE, |
| 60 | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); |
| 61 | printf("%p\n", p); |
| 62 | if (p == MAP_FAILED) { |
| 63 | assert(errno == ENOMEM); |
| 64 | break; |
| 65 | } |
| 66 | assert(AddrIsApp(p)); |
| 67 | success_count++; |
| 68 | } |
| 69 | printf("successful mappings: %d\n", success_count); |
| 70 | assert(success_count > 5); |
| 71 | } |