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 Test(int size)13void 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); 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 main(int argc,char ** argv)37int main(int argc, char **argv) { 38 int size = atoi(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