Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / get_allocated_begin.cpp
blob7ae2df5d5647b5092a4ec7f9fbedde4c87c66678
1 // RUN: %clangxx -O0 -g %s -o %t && %run %t
3 // Must not be implemented, no other reason to install interceptors.
4 // XFAIL: ubsan
6 #include <assert.h>
7 #include <sanitizer/allocator_interface.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 // Based on lib/msan/tests/msan_test.cpp::get_allocated_size_and_ownership
13 int main(void) {
14 int sizes[] = {10, 100, 1000, 10000, 100000, 1000000};
16 for (int i = 0; i < sizeof(sizes) / sizeof(int); i++) {
17 printf("Testing size %d\n", sizes[i]);
19 char *array = reinterpret_cast<char *>(malloc(sizes[i]));
20 int *int_ptr = new int;
21 printf("array: %p\n", array);
22 printf("int_ptr: %p\n", int_ptr);
24 // Bogus value to unpoison start. Calling __sanitizer_get_allocated_begin
25 // does not unpoison it.
26 const void *start = NULL;
27 for (int j = 0; j < sizes[i]; j++) {
29 start = __sanitizer_get_allocated_begin(array + j);
30 if (array != start) {
31 printf("j: %d\n", j);
32 printf("Start: %p (expected: %p)\n", start, array);
33 fflush(stdout);
35 assert(array == start);
38 start = __sanitizer_get_allocated_begin(int_ptr);
39 assert(int_ptr == start);
41 void *wild_addr = reinterpret_cast<void *>(4096 * 160);
42 assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
44 wild_addr = reinterpret_cast<void *>(0x1);
45 assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
47 // NULL is a valid argument for GetAllocatedSize but is not owned.
48 assert(__sanitizer_get_allocated_begin(NULL) == NULL);
50 free(array);
51 for (int j = 0; j < sizes[i]; j++) {
52 assert(__sanitizer_get_allocated_begin(array + j) == NULL);
55 delete int_ptr;
56 assert(__sanitizer_get_allocated_begin(int_ptr) == NULL);
59 return 0;