1 // RUN: %clang_scudo %s -o %t 2 // RUN: SCUDO_OPTIONS=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s 3 // RUN: SCUDO_OPTIONS=allocator_may_return_null=1 %run %t malloc 2>&1 4 // RUN: SCUDO_OPTIONS=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s 5 // RUN: SCUDO_OPTIONS=allocator_may_return_null=1 %run %t calloc 2>&1 6 // RUN: %run %t usable 2>&1 7 8 // Tests for various edge cases related to sizes, notably the maximum size the 9 // allocator can allocate. Tests that an integer overflow in the parameters of 10 // calloc is caught. 11 12 #include <assert.h> 13 #include <malloc.h> 14 #include <stdlib.h> 15 #include <string.h> 16 17 #include <limits> 18 19 int main(int argc, char **argv) 20 { 21 assert(argc == 2); 22 if (!strcmp(argv[1], "malloc")) { 23 // Currently the maximum size the allocator can allocate is 1ULL<<40 bytes. 24 size_t size = std::numeric_limits<size_t>::max(); 25 void *p = malloc(size); 26 if (p) 27 return 1; 28 size = (1ULL << 40) - 16; 29 p = malloc(size); 30 if (p) 31 return 1; 32 } 33 if (!strcmp(argv[1], "calloc")) { 34 // Trigger an overflow in calloc. 35 size_t size = std::numeric_limits<size_t>::max(); 36 void *p = calloc((size / 0x1000) + 1, 0x1000); 37 if (p) 38 return 1; 39 } 40 if (!strcmp(argv[1], "usable")) { 41 // Playing with the actual usable size of a chunk. 42 void *p = malloc(1007); 43 if (!p) 44 return 1; 45 size_t size = malloc_usable_size(p); 46 if (size < 1007) 47 return 1; 48 memset(p, 'A', size); 49 p = realloc(p, 2014); 50 if (!p) 51 return 1; 52 size = malloc_usable_size(p); 53 if (size < 2014) 54 return 1; 55 memset(p, 'B', size); 56 free(p); 57 } 58 return 0; 59 } 60 61 // CHECK: allocator is terminating the process 62