1415b1cfdSThurston Dang // RUN: %clangxx -O0 -g %s -o %t && %run %t
2415b1cfdSThurston Dang
369e01fa1SVitaly Buka // Must not be implemented, no other reason to install interceptors.
469e01fa1SVitaly Buka // XFAIL: ubsan
569e01fa1SVitaly Buka
6415b1cfdSThurston Dang #include <assert.h>
7415b1cfdSThurston Dang #include <sanitizer/allocator_interface.h>
8415b1cfdSThurston Dang #include <stdio.h>
9415b1cfdSThurston Dang #include <stdlib.h>
10415b1cfdSThurston Dang #include <string.h>
11415b1cfdSThurston Dang
12415b1cfdSThurston Dang // Based on lib/msan/tests/msan_test.cpp::get_allocated_size_and_ownership
main(void)13415b1cfdSThurston Dang int main(void) {
14415b1cfdSThurston Dang int sizes[] = {10, 100, 1000, 10000, 100000, 1000000};
15415b1cfdSThurston Dang
16415b1cfdSThurston Dang for (int i = 0; i < sizeof(sizes) / sizeof(int); i++) {
17415b1cfdSThurston Dang printf("Testing size %d\n", sizes[i]);
18415b1cfdSThurston Dang
19415b1cfdSThurston Dang char *array = reinterpret_cast<char *>(malloc(sizes[i]));
20415b1cfdSThurston Dang int *int_ptr = new int;
21415b1cfdSThurston Dang printf("array: %p\n", array);
22415b1cfdSThurston Dang printf("int_ptr: %p\n", int_ptr);
23415b1cfdSThurston Dang
24415b1cfdSThurston Dang // Bogus value to unpoison start. Calling __sanitizer_get_allocated_begin
25415b1cfdSThurston Dang // does not unpoison it.
26d644ab02SThurston Dang const void *start = NULL;
27415b1cfdSThurston Dang for (int j = 0; j < sizes[i]; j++) {
28415b1cfdSThurston Dang
29415b1cfdSThurston Dang start = __sanitizer_get_allocated_begin(array + j);
30*8362ddb6SVitaly Buka if (array != start) {
31*8362ddb6SVitaly Buka printf("j: %d\n", j);
32415b1cfdSThurston Dang printf("Start: %p (expected: %p)\n", start, array);
33415b1cfdSThurston Dang fflush(stdout);
34*8362ddb6SVitaly Buka }
35415b1cfdSThurston Dang assert(array == start);
36415b1cfdSThurston Dang }
37415b1cfdSThurston Dang
38415b1cfdSThurston Dang start = __sanitizer_get_allocated_begin(int_ptr);
39415b1cfdSThurston Dang assert(int_ptr == start);
40415b1cfdSThurston Dang
41415b1cfdSThurston Dang void *wild_addr = reinterpret_cast<void *>(4096 * 160);
42415b1cfdSThurston Dang assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
43415b1cfdSThurston Dang
44415b1cfdSThurston Dang wild_addr = reinterpret_cast<void *>(0x1);
45415b1cfdSThurston Dang assert(__sanitizer_get_allocated_begin(wild_addr) == NULL);
46415b1cfdSThurston Dang
47415b1cfdSThurston Dang // NULL is a valid argument for GetAllocatedSize but is not owned.
48415b1cfdSThurston Dang assert(__sanitizer_get_allocated_begin(NULL) == NULL);
49415b1cfdSThurston Dang
50415b1cfdSThurston Dang free(array);
51415b1cfdSThurston Dang for (int j = 0; j < sizes[i]; j++) {
52415b1cfdSThurston Dang assert(__sanitizer_get_allocated_begin(array + j) == NULL);
53415b1cfdSThurston Dang }
54415b1cfdSThurston Dang
55415b1cfdSThurston Dang delete int_ptr;
56415b1cfdSThurston Dang assert(__sanitizer_get_allocated_begin(int_ptr) == NULL);
57415b1cfdSThurston Dang }
58415b1cfdSThurston Dang
59415b1cfdSThurston Dang return 0;
60415b1cfdSThurston Dang }
61