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