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