1// RUN: %clangxx -O0 -g %s -o %t && %run %t
2
3// Must not be implemented, no other reason to install interceptors.
4// XFAIL: ubsan
5
6#include <assert.h>
7#include <sanitizer/allocator_interface.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12// Based on lib/msan/tests/msan_test.cpp::get_allocated_size_and_ownership
13int main(void) {
14 int sizes[] = {10, 100, 1000, 10000, 100000, 1000000};
15
16 for (int i = 0; i < sizeof(sizes) / sizeof(int); i++) {
17 printf(format: "Testing size %d\n", sizes[i]);
18
19 char *array = reinterpret_cast<char *>(malloc(size: sizes[i]));
20 int *int_ptr = new int;
21 printf(format: "array: %p\n", array);
22 printf(format: "int_ptr: %p\n", int_ptr);
23
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++) {
28
29 start = __sanitizer_get_allocated_begin(p: array + j);
30 if (array != start) {
31 printf(format: "j: %d\n", j);
32 printf(format: "Start: %p (expected: %p)\n", start, array);
33 fflush(stdout);
34 }
35 assert(array == start);
36 }
37
38 start = __sanitizer_get_allocated_begin(p: int_ptr);
39 assert(int_ptr == start);
40
41 void *wild_addr = reinterpret_cast<void *>(4096 * 160);
42 assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
43
44 wild_addr = reinterpret_cast<void *>(0x1);
45 assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
46
47 // NULL is a valid argument for GetAllocatedSize but is not owned.
48 assert(__sanitizer_get_allocated_begin(NULL) == NULL);
49
50 free(ptr: array);
51 for (int j = 0; j < sizes[i]; j++) {
52 assert(__sanitizer_get_allocated_begin(array + j) == NULL);
53 }
54
55 delete int_ptr;
56 assert(__sanitizer_get_allocated_begin(int_ptr) == NULL);
57 }
58
59 return 0;
60}
61

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