llvm-project/compiler-rt/test/nsan/Posix/allocator_mapping.cpp
Fangrui Song 652707a645
[nsan] Use sanitizer allocator
* The performance is better than the glibc allocator.
* Allocator interface functions, sanitizer allocator options, and
  MallocHooks/FreeHooks are supported.
* Shadow memory has specific memory layout requirement. Using libc
  allocator could lead to conflicts.
* When we add a mmap interceptor for reliability (the VMA could reuse a
  previously released VMA that is poisoned): glibc may invoke an
  internal system call to call unmmap, which cannot be intercepted. We
  will not be able to return the shadow memory to the OS.

Similar to dfsan https://reviews.llvm.org/D101204 . Also intercept
operator new/delete to be similar to other sanitizers using the
sanitizer allocator. The align_val_t overload of operator new has
slightly less overhead.

Pull Request: https://github.com/llvm/llvm-project/pull/102764
2024-08-12 13:56:40 -07:00

31 lines
861 B
C++

/// From msan/allocator_mapping.cpp
/// Test that a module constructor can not map memory over the NSan heap
/// (without MAP_FIXED, of course).
// RUN: %clangxx_nsan -O0 %s -o %t_1
// RUN: %clangxx_nsan -O0 -DHEAP_ADDRESS=$(%run %t_1) %s -o %t_2 && %run %t_2
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#ifdef HEAP_ADDRESS
struct A {
A() {
void *const hint = reinterpret_cast<void *>(HEAP_ADDRESS);
void *p = mmap(hint, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// This address must be already mapped. Check that mmap() succeeds, but at a
// different address.
assert(p != reinterpret_cast<void *>(-1));
assert(p != hint);
}
} a;
#endif
int main() {
void *p = malloc(10);
printf("0x%zx\n", reinterpret_cast<size_t>(p) & (~0xfff));
free(p);
}