1 // RUN: %clang_scudo %s -lstdc++ -o %t 2 // RUN: %run %t pointers 2>&1 3 // RUN: %run %t contents 2>&1 4 // RUN: not %run %t memalign 2>&1 | FileCheck %s 5 6 // Tests that our reallocation function returns the same pointer when the 7 // requested size can fit into the previously allocated chunk. Also tests that 8 // a new chunk is returned if the size is greater, and that the contents of the 9 // chunk are left unchanged. 10 // As a final test, make sure that a chunk allocated by memalign cannot be 11 // reallocated. 12 13 #include <assert.h> 14 #include <malloc.h> 15 #include <string.h> 16 17 #include <vector> 18 19 int main(int argc, char **argv) 20 { 21 void *p, *old_p; 22 // Those sizes will exercise both allocators (Primary & Secondary). 23 std::vector<size_t> sizes{1, 16, 1024, 32768, 1 << 16, 1 << 17, 1 << 20}; 24 25 assert(argc == 2); 26 27 for (size_t size : sizes) { 28 if (!strcmp(argv[1], "pointers")) { 29 old_p = p = realloc(nullptr, size); 30 assert(p); 31 size = malloc_usable_size(p); 32 // Our realloc implementation will return the same pointer if the size 33 // requested is lower than or equal to the usable size of the associated 34 // chunk. 35 p = realloc(p, size - 1); 36 assert(p == old_p); 37 p = realloc(p, size); 38 assert(p == old_p); 39 // And a new one if the size is greater. 40 p = realloc(p, size + 1); 41 assert(p != old_p); 42 // A size of 0 will free the chunk and return nullptr. 43 p = realloc(p, 0); 44 assert(!p); 45 old_p = nullptr; 46 } 47 if (!strcmp(argv[1], "contents")) { 48 p = realloc(nullptr, size); 49 assert(p); 50 for (int i = 0; i < size; i++) 51 reinterpret_cast<char *>(p)[i] = 'A'; 52 p = realloc(p, size + 1); 53 // The contents of the reallocated chunk must match the original one. 54 for (int i = 0; i < size; i++) 55 assert(reinterpret_cast<char *>(p)[i] == 'A'); 56 } 57 if (!strcmp(argv[1], "memalign")) { 58 // A chunk coming from memalign cannot be reallocated. 59 p = memalign(16, size); 60 assert(p); 61 p = realloc(p, size); 62 free(p); 63 } 64 } 65 return 0; 66 } 67 68 // CHECK: ERROR: invalid chunk type when reallocating address 69