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

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