1 // RUN: %clang_scudo %s -lstdc++ -o %t 2 // RUN: %run %t pointers 2>&1 3 // RUN: %run %t contents 2>&1 4 // RUN: %run %t usablesize 2>&1 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. Finally, checks that realloc copies the usable 10 // size of the old chunk to the new one (as opposed to the requested size). 11 12 #include <assert.h> 13 #include <malloc.h> 14 #include <string.h> 15 16 #include <vector> 17 18 int main(int argc, char **argv) 19 { 20 void *p, *old_p; 21 // Those sizes will exercise both allocators (Primary & Secondary). 22 std::vector<size_t> sizes{1, 16, 1024, 32768, 1 << 16, 1 << 17, 1 << 20}; 23 24 assert(argc == 2); 25 26 if (!strcmp(argv[1], "usablesize")) { 27 // This tests a sketchy behavior inherited from poorly written libraries 28 // that have become somewhat standard. When realloc'ing a chunk, the 29 // copied contents should span the usable size of the chunk, not the 30 // requested size. 31 size_t size = 496, usable_size; 32 p = nullptr; 33 // Make sure we get a chunk with a usable size actually larger than size. 34 do { 35 if (p) free(p); 36 size += 16; 37 p = malloc(size); 38 usable_size = malloc_usable_size(p); 39 assert(usable_size >= size); 40 } while (usable_size == size); 41 for (int i = 0; i < usable_size; i++) 42 reinterpret_cast<char *>(p)[i] = 'A'; 43 old_p = p; 44 // Make sure we get a different chunk so that the data is actually copied. 45 do { 46 size *= 2; 47 p = realloc(p, size); 48 assert(p); 49 } while (p == old_p); 50 // The contents of the new chunk must match the old one up to usable_size. 51 for (int i = 0; i < usable_size; i++) 52 assert(reinterpret_cast<char *>(p)[i] == 'A'); 53 free(p); 54 } else { 55 for (size_t size : sizes) { 56 if (!strcmp(argv[1], "pointers")) { 57 old_p = p = realloc(nullptr, size); 58 assert(p); 59 size = malloc_usable_size(p); 60 // Our realloc implementation will return the same pointer if the size 61 // requested is lower than or equal to the usable size of the associated 62 // chunk. 63 p = realloc(p, size - 1); 64 assert(p == old_p); 65 p = realloc(p, size); 66 assert(p == old_p); 67 // And a new one if the size is greater. 68 p = realloc(p, size + 1); 69 assert(p != old_p); 70 // A size of 0 will free the chunk and return nullptr. 71 p = realloc(p, 0); 72 assert(!p); 73 old_p = nullptr; 74 } 75 if (!strcmp(argv[1], "contents")) { 76 p = realloc(nullptr, size); 77 assert(p); 78 for (int i = 0; i < size; i++) 79 reinterpret_cast<char *>(p)[i] = 'A'; 80 p = realloc(p, size + 1); 81 // The contents of the reallocated chunk must match the original one. 82 for (int i = 0; i < size; i++) 83 assert(reinterpret_cast<char *>(p)[i] == 'A'); 84 } 85 } 86 } 87 return 0; 88 } 89 90 // CHECK: ERROR: invalid chunk type when reallocating address 91