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_msan -O0 %s -o %t 6 // RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 7 // RUN: MSAN_OPTIONS=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH 8 // RUN: MSAN_OPTIONS=allocator_may_return_null=1 %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mNULL 9 // RUN: MSAN_OPTIONS=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cCRASH 10 // RUN: MSAN_OPTIONS=allocator_may_return_null=1 %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cNULL 11 // RUN: MSAN_OPTIONS=allocator_may_return_null=0 not %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coCRASH 12 // RUN: MSAN_OPTIONS=allocator_may_return_null=1 %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coNULL 13 // RUN: MSAN_OPTIONS=allocator_may_return_null=0 not %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rCRASH 14 // RUN: MSAN_OPTIONS=allocator_may_return_null=1 %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rNULL 15 // RUN: MSAN_OPTIONS=allocator_may_return_null=0 not %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrCRASH 16 // RUN: MSAN_OPTIONS=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 volatile size_t size = std::numeric_limits<size_t>::max() - 10000; 26 assert(argc == 2); 27 char *x = 0; 28 if (!strcmp(argv[1], "malloc")) { 29 fprintf(stderr, "malloc:\n"); 30 x = (char*)malloc(size); 31 } 32 if (!strcmp(argv[1], "calloc")) { 33 fprintf(stderr, "calloc:\n"); 34 x = (char*)calloc(size / 4, 4); 35 } 36 37 if (!strcmp(argv[1], "calloc-overflow")) { 38 fprintf(stderr, "calloc-overflow:\n"); 39 volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max(); 40 size_t kArraySize = 4096; 41 volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10; 42 x = (char*)calloc(kArraySize, kArraySize2); 43 } 44 45 if (!strcmp(argv[1], "realloc")) { 46 fprintf(stderr, "realloc:\n"); 47 x = (char*)realloc(0, size); 48 } 49 if (!strcmp(argv[1], "realloc-after-malloc")) { 50 fprintf(stderr, "realloc-after-malloc:\n"); 51 char *t = (char*)malloc(100); 52 *t = 42; 53 x = (char*)realloc(t, size); 54 assert(*t == 42); 55 } 56 // The NULL pointer is printed differently on different systems, while (long)0 57 // is always the same. 58 fprintf(stderr, "x: %lx\n", (long)x); 59 return x != 0; 60 } 61 // CHECK-mCRASH: malloc: 62 // CHECK-mCRASH: MemorySanitizer's allocator is terminating the process 63 // CHECK-cCRASH: calloc: 64 // CHECK-cCRASH: MemorySanitizer's allocator is terminating the process 65 // CHECK-coCRASH: calloc-overflow: 66 // CHECK-coCRASH: MemorySanitizer's allocator is terminating the process 67 // CHECK-rCRASH: realloc: 68 // CHECK-rCRASH: MemorySanitizer's allocator is terminating the process 69 // CHECK-mrCRASH: realloc-after-malloc: 70 // CHECK-mrCRASH: MemorySanitizer's allocator is terminating the process 71 72 // CHECK-mNULL: malloc: 73 // CHECK-mNULL: x: 0 74 // CHECK-cNULL: calloc: 75 // CHECK-cNULL: x: 0 76 // CHECK-coNULL: calloc-overflow: 77 // CHECK-coNULL: x: 0 78 // CHECK-rNULL: realloc: 79 // CHECK-rNULL: x: 0 80 // CHECK-mrNULL: realloc-after-malloc: 81 // CHECK-mrNULL: x: 0 82