llvm-project/compiler-rt/test/nsan/allocator_interface.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

46 lines
1.3 KiB
C++

/// From sanitizer_common/TestCases/allocator_interface.cpp
// RUN: %clangxx_nsan %s -o %t && %run %t 1234
// RUN: %clangxx_nsan %s -o %t && %run %t 5678910
#include <assert.h>
#include <sanitizer/allocator_interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
void Test(int size) {
auto allocated_bytes_before = __sanitizer_get_current_allocated_bytes();
int *p = (int *)malloc(size);
assert(__sanitizer_get_estimated_allocated_size(size) >= size);
assert(__sanitizer_get_ownership(p));
assert(!__sanitizer_get_ownership(&p));
assert(__sanitizer_get_allocated_size(p) == size);
assert(__sanitizer_get_allocated_size_fast(p) == size);
assert(__sanitizer_get_allocated_begin(p) == p);
assert(__sanitizer_get_allocated_begin(p + 1) == p);
assert(__sanitizer_get_current_allocated_bytes() >=
size + allocated_bytes_before);
assert(__sanitizer_get_current_allocated_bytes() <=
2 * size + allocated_bytes_before);
assert(__sanitizer_get_heap_size() >= size);
free(p);
// These are not implemented.
assert(__sanitizer_get_unmapped_bytes() <= 1);
assert(__sanitizer_get_free_bytes() > 0);
__sanitizer_purge_allocator();
}
int main(int argc, char **argv) {
int size = atoi(argv[1]);
Test(size);
// Check the thread local caches work as well.
std::thread t(Test, size);
t.join();
return 0;
}