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

source code of compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp