1 // Test mmap behavior when map address is below shadow range. 2 // With MAP_FIXED, we return EINVAL. 3 // Without MAP_FIXED, we ignore the address hint and map somewhere in 4 // application range. 5 6 // RUN: %clangxx_msan -O0 -DFIXED=0 %s -o %t && %run %t 7 // RUN: %clangxx_msan -O0 -DFIXED=1 %s -o %t && %run %t 8 // RUN: %clangxx_msan -O0 -DFIXED=0 -D_FILE_OFFSET_BITS=64 %s -o %t && %run %t 9 // RUN: %clangxx_msan -O0 -DFIXED=1 -D_FILE_OFFSET_BITS=64 %s -o %t && %run %t 10 11 #include <assert.h> 12 #include <errno.h> 13 #include <stdint.h> 14 #include <sys/mman.h> 15 16 int main(void) { 17 // Hint address just below shadow. 18 #if defined(__x86_64__) 19 uintptr_t hint = 0x4f0000000000ULL; 20 const uintptr_t app_start = 0x600000000000ULL; 21 #elif defined (__mips64) 22 uintptr_t hint = 0x4f00000000ULL; 23 const uintptr_t app_start = 0x6000000000ULL; 24 #endif 25 uintptr_t p = (uintptr_t)mmap( 26 (void *)hint, 4096, PROT_WRITE, 27 MAP_PRIVATE | MAP_ANONYMOUS | (FIXED ? MAP_FIXED : 0), -1, 0); 28 if (FIXED) 29 assert(p == (uintptr_t)-1 && errno == EINVAL); 30 else 31 assert(p >= app_start); 32 return 0; 33 } 34