1 // Test the behavior of malloc/calloc/realloc when the allocation size is huge. 2 // By default (allocator_may_return_null=0) the process should crash. 3 // With allocator_may_return_null=1 the allocator should return 0. 4 // 5 // RUN: %clangxx_asan -O0 %s -o %t 6 // RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 7 // RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 8 // RUN: %env_asan_opts=allocator_may_return_null=1 %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mNULL 9 // RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cCRASH 10 // RUN: %env_asan_opts=allocator_may_return_null=1 %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cNULL 11 // RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coCRASH 12 // RUN: %env_asan_opts=allocator_may_return_null=1 %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coNULL 13 // RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rCRASH 14 // RUN: %env_asan_opts=allocator_may_return_null=1 %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rNULL 15 // RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrCRASH 16 // RUN: %env_asan_opts=allocator_may_return_null=1 %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrNULL 17 18 #include <limits.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <stdio.h> 22 #include <assert.h> 23 #include <limits> 24 int main(int argc, char **argv) { 25 // Disable stderr buffering. Needed on Windows. 26 setvbuf(stderr, NULL, _IONBF, 0); 27 28 volatile size_t size = std::numeric_limits<size_t>::max() - 10000; 29 assert(argc == 2); 30 void *x = 0; 31 if (!strcmp(argv[1], "malloc")) { 32 fprintf(stderr, "malloc:\n"); 33 x = malloc(size); 34 } 35 if (!strcmp(argv[1], "calloc")) { 36 fprintf(stderr, "calloc:\n"); 37 x = calloc(size / 4, 4); 38 } 39 40 if (!strcmp(argv[1], "calloc-overflow")) { 41 fprintf(stderr, "calloc-overflow:\n"); 42 volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); 43 size_t kArraySize = 4096; 44 volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; 45 x = calloc(kArraySize, kArraySize2); 46 } 47 48 if (!strcmp(argv[1], "realloc")) { 49 fprintf(stderr, "realloc:\n"); 50 x = realloc(0, size); 51 } 52 if (!strcmp(argv[1], "realloc-after-malloc")) { 53 fprintf(stderr, "realloc-after-malloc:\n"); 54 char *t = (char*)malloc(100); 55 *t = 42; 56 x = realloc(t, size); 57 assert(*t == 42); 58 free(t); 59 } 60 // The NULL pointer is printed differently on different systems, while (long)0 61 // is always the same. 62 fprintf(stderr, "x: %lx\n", (long)x); 63 free(x); 64 return x != 0; 65 } 66 // CHECK-mCRASH: malloc: 67 // CHECK-mCRASH: AddressSanitizer's allocator is terminating the process 68 // CHECK-cCRASH: calloc: 69 // CHECK-cCRASH: AddressSanitizer's allocator is terminating the process 70 // CHECK-coCRASH: calloc-overflow: 71 // CHECK-coCRASH: AddressSanitizer's allocator is terminating the process 72 // CHECK-rCRASH: realloc: 73 // CHECK-rCRASH: AddressSanitizer's allocator is terminating the process 74 // CHECK-mrCRASH: realloc-after-malloc: 75 // CHECK-mrCRASH: AddressSanitizer's allocator is terminating the process 76 77 // CHECK-mNULL: malloc: 78 // CHECK-mNULL: x: 0 79 // CHECK-cNULL: calloc: 80 // CHECK-cNULL: x: 0 81 // CHECK-coNULL: calloc-overflow: 82 // CHECK-coNULL: x: 0 83 // CHECK-rNULL: realloc: 84 // CHECK-rNULL: x: 0 85 // CHECK-mrNULL: realloc-after-malloc: 86 // CHECK-mrNULL: x: 0 87