brintos

brintos / llvm-project-archived public Read only

0
0
Text · 912 B · a92962e Raw
32 lines · cpp
1/// From msan/allocator_mapping.cpp2/// Test that a module constructor can not map memory over the NSan heap3/// (without MAP_FIXED, of course).4// RUN: %clangxx_nsan -O0 %s -o %t_15// RUN: %run %t_1 > %t.heap_address6// RUN: %clangxx_nsan -O0 -DHEAP_ADDRESS=%{readfile:%t.heap_address} %s -o %t_2 && %run %t_27 8#include <assert.h>9#include <stdio.h>10#include <stdlib.h>11#include <sys/mman.h>12 13#ifdef HEAP_ADDRESS14struct A {15  A() {16    void *const hint = reinterpret_cast<void *>(HEAP_ADDRESS);17    void *p = mmap(hint, 4096, PROT_READ | PROT_WRITE,18                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);19    // This address must be already mapped. Check that mmap() succeeds, but at a20    // different address.21    assert(p != reinterpret_cast<void *>(-1));22    assert(p != hint);23  }24} a;25#endif26 27int main() {28  void *p = malloc(10);29  printf("0x%zx\n", reinterpret_cast<size_t>(p) & (~0xfff));30  free(p);31}32