| 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 |
| 13 |
int 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("Testing size %d\n", sizes[i]); |
| 18 |
|
| 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); |
| 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(array + j); |
| 30 |
if (array != start) { |
| 31 |
printf("j: %d\n", j); |
| 32 |
printf("Start: %p (expected: %p)\n", start, array); |
| 33 |
fflush(stdout); |
| 34 |
} |
| 35 |
assert(array == start); |
| 36 |
} |
| 37 |
|
| 38 |
start = __sanitizer_get_allocated_begin(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(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 |
} |