1/// From msan/allocator_mapping.cpp
2/// Test that a module constructor can not map memory over the NSan heap
3/// (without MAP_FIXED, of course).
4// RUN: %clangxx_nsan -O0 %s -o %t_1
5// RUN: %clangxx_nsan -O0 -DHEAP_ADDRESS=$(%run %t_1) %s -o %t_2 && %run %t_2
6
7#include <assert.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/mman.h>
11
12#ifdef HEAP_ADDRESS
13struct A {
14 A() {
15 void *const hint = reinterpret_cast<void *>(HEAP_ADDRESS);
16 void *p = mmap(hint, 4096, PROT_READ | PROT_WRITE,
17 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
18 // This address must be already mapped. Check that mmap() succeeds, but at a
19 // different address.
20 assert(p != reinterpret_cast<void *>(-1));
21 assert(p != hint);
22 }
23} a;
24#endif
25
26int main() {
27 void *p = malloc(size: 10);
28 printf(format: "0x%zx\n", reinterpret_cast<size_t>(p) & (~0xfff));
29 free(ptr: p);
30}
31

source code of compiler-rt/test/nsan/Posix/allocator_mapping.cpp