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