1 // RUN: %clang_scudo %s -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 int main(int argc, char **argv) 18 { 19 void *p, *old_p; 20 size_t size = 32; 21 22 assert(argc == 2); 23 if (!strcmp(argv[1], "pointers")) { 24 old_p = p = realloc(nullptr, size); 25 if (!p) 26 return 1; 27 size = malloc_usable_size(p); 28 // Our realloc implementation will return the same pointer if the size 29 // requested is lower or equal to the usable size of the associated chunk. 30 p = realloc(p, size - 1); 31 if (p != old_p) 32 return 1; 33 p = realloc(p, size); 34 if (p != old_p) 35 return 1; 36 // And a new one if the size is greater. 37 p = realloc(p, size + 1); 38 if (p == old_p) 39 return 1; 40 // A size of 0 will free the chunk and return nullptr. 41 p = realloc(p, 0); 42 if (p) 43 return 1; 44 old_p = nullptr; 45 } 46 if (!strcmp(argv[1], "contents")) { 47 p = realloc(nullptr, size); 48 if (!p) 49 return 1; 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 if (reinterpret_cast<char *>(p)[i] != 'A') 56 return 1; 57 } 58 if (!strcmp(argv[1], "memalign")) { 59 // A chunk coming from memalign cannot be reallocated. 60 p = memalign(16, size); 61 if (!p) 62 return 1; 63 p = realloc(p, size); 64 free(p); 65 } 66 return 0; 67 } 68 69 // CHECK: ERROR: invalid chunk type when reallocating address 70