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
16int main(void) {
17 // Hint address just below shadow.
18#if defined(__FreeBSD__) && defined(__x86_64__)
19 uintptr_t hint = 0x0f0000000000ULL;
20 const uintptr_t app_start = 0x000000000000ULL;
21#elif defined(__x86_64__)
22 uintptr_t hint = 0x4f0000000000ULL;
23 const uintptr_t app_start = 0x600000000000ULL;
24#elif defined(__loongarch_lp64)
25 uintptr_t hint = 0x4f0000000000ULL;
26 const uintptr_t app_start = 0x600000000000ULL;
27#elif defined (__mips64)
28 uintptr_t hint = 0x4f00000000ULL;
29 const uintptr_t app_start = 0x6000000000ULL;
30#elif defined (__powerpc64__)
31 uintptr_t hint = 0x2f0000000000ULL;
32 const uintptr_t app_start = 0x300000000000ULL;
33#elif defined(__s390x__)
34 uintptr_t hint = 0x07f000000000ULL;
35 const uintptr_t app_start = 0x020000000000ULL;
36#elif defined (__aarch64__)
37 uintptr_t hint = 0X0110000000000;
38 // Unfortunately we don't have a stronger condition for this
39 const uintptr_t app_start = 0x0ULL;
40#endif
41 uintptr_t p = (uintptr_t)mmap(
42 (void *)hint, 4096, PROT_WRITE,
43 MAP_PRIVATE | MAP_ANONYMOUS | (FIXED ? MAP_FIXED : 0), -1, 0);
44 if (FIXED)
45 assert(p == (uintptr_t)-1 && errno == EINVAL);
46 else
47 assert(p >= app_start);
48 return 0;
49}
50

source code of compiler-rt/test/msan/mmap_below_shadow.cpp