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