Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / allocator_interface.cpp
blob6f5f05639800a7f1e0e29d18ec6936bcd5166b23
1 // RUN: %clangxx %s -o %t && %run %t 1234
2 // RUN: %clangxx %s -o %t && %run %t 5678910
4 // No allocator.
5 // UNSUPPORTED: ubsan
7 #include <assert.h>
8 #include <sanitizer/allocator_interface.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <thread>
13 void Test(int size) {
14 auto allocated_bytes_before = __sanitizer_get_current_allocated_bytes();
15 int *p = (int *)malloc(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(p);
30 // These are not implemented.
31 assert(__sanitizer_get_unmapped_bytes() <= 1);
32 assert(__sanitizer_get_free_bytes() > 0);
34 __sanitizer_purge_allocator();
37 int main(int argc, char **argv) {
38 int size = atoi(argv[1]);
40 Test(size);
42 // Check the thread local caches work as well.
43 std::thread t(Test, size);
44 t.join();
46 return 0;