xref: /llvm-project/compiler-rt/test/msan/allocator_mapping.cpp (revision 17f804ef4b8fff0e8860ce656f2c4dfbf16d56c6)
1d21b3d34SFangrui Song // Test that a module constructor can not map memory over the MSan heap
2d21b3d34SFangrui Song // (without MAP_FIXED, of course). Current implementation ensures this by
3d21b3d34SFangrui Song // mapping the heap early, in __msan_init.
4d21b3d34SFangrui Song //
5d21b3d34SFangrui Song // RUN: %clangxx_msan -O0 %s -o %t_1
6d21b3d34SFangrui Song // RUN: %clangxx_msan -O0 -DHEAP_ADDRESS=$(%run %t_1) %s -o %t_2 && %run %t_2
7d21b3d34SFangrui Song //
8d21b3d34SFangrui Song // This test only makes sense for the 64-bit allocator. The 32-bit allocator
9d21b3d34SFangrui Song // does not have a fixed mapping. Exclude platforms that use the 32-bit
10d21b3d34SFangrui Song // allocator.
11*17f804efSPaul Robinson // UNSUPPORTED: target-is-mips64,target-is-mips64el,target=aarch64{{.*}}
12d21b3d34SFangrui Song 
13d21b3d34SFangrui Song #include <assert.h>
14d21b3d34SFangrui Song #include <stdio.h>
15d21b3d34SFangrui Song #include <sys/mman.h>
16d21b3d34SFangrui Song #include <stdlib.h>
17d21b3d34SFangrui Song 
18d21b3d34SFangrui Song #ifdef HEAP_ADDRESS
19d21b3d34SFangrui Song struct A {
AA20d21b3d34SFangrui Song   A() {
21d21b3d34SFangrui Song     void *const hint = reinterpret_cast<void *>(HEAP_ADDRESS);
22d21b3d34SFangrui Song     void *p = mmap(hint, 4096, PROT_READ | PROT_WRITE,
23d21b3d34SFangrui Song                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
24d21b3d34SFangrui Song     // This address must be already mapped. Check that mmap() succeeds, but at a
25d21b3d34SFangrui Song     // different address.
26d21b3d34SFangrui Song     assert(p != reinterpret_cast<void *>(-1));
27d21b3d34SFangrui Song     assert(p != hint);
28d21b3d34SFangrui Song   }
29d21b3d34SFangrui Song } a;
30d21b3d34SFangrui Song #endif
31d21b3d34SFangrui Song 
main()32d21b3d34SFangrui Song int main() {
33d21b3d34SFangrui Song   void *p = malloc(10);
34d21b3d34SFangrui Song   printf("0x%zx\n", reinterpret_cast<size_t>(p) & (~0xfff));
35d21b3d34SFangrui Song   free(p);
36d21b3d34SFangrui Song }
37