1/// From sanitizer_common/TestCases/allocator_interface.cpp
2// RUN: %clangxx_nsan %s -o %t && %run %t 1234
3// RUN: %clangxx_nsan %s -o %t && %run %t 5678910
4
5#include <assert.h>
6#include <sanitizer/allocator_interface.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <thread>
10
11void Test(int size) {
12 auto allocated_bytes_before = __sanitizer_get_current_allocated_bytes();
13 int *p = (int *)malloc(size: size);
14 assert(__sanitizer_get_estimated_allocated_size(size) >= size);
15 assert(__sanitizer_get_ownership(p));
16 assert(!__sanitizer_get_ownership(&p));
17 assert(__sanitizer_get_allocated_size(p) == size);
18 assert(__sanitizer_get_allocated_size_fast(p) == size);
19 assert(__sanitizer_get_allocated_begin(p) == p);
20 assert(__sanitizer_get_allocated_begin(p + 1) == p);
21 assert(__sanitizer_get_current_allocated_bytes() >=
22 size + allocated_bytes_before);
23 assert(__sanitizer_get_current_allocated_bytes() <=
24 2 * size + allocated_bytes_before);
25 assert(__sanitizer_get_heap_size() >= size);
26 free(ptr: p);
27
28 // These are not implemented.
29 assert(__sanitizer_get_unmapped_bytes() <= 1);
30 assert(__sanitizer_get_free_bytes() > 0);
31
32 __sanitizer_purge_allocator();
33}
34
35int main(int argc, char **argv) {
36 int size = atoi(nptr: argv[1]);
37
38 Test(size);
39
40 // Check the thread local caches work as well.
41 std::thread t(Test, size);
42 t.join();
43
44 return 0;
45}
46

source code of compiler-rt/test/nsan/allocator_interface.cpp