1 // RUN: %clangxx_hwasan %s -lstdc++ -o %t 2 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-max 3 // RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t malloc 2>&1 4 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc max 2>&1 | FileCheck %s --check-prefix=CHECK-max 5 // RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t malloc max 2>&1 6 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-calloc 7 // RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t calloc 2>&1 8 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new 2>&1 | FileCheck %s --check-prefix=CHECK-max 9 // RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new 2>&1 | FileCheck %s --check-prefix=CHECK-oom 10 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new max 2>&1 | FileCheck %s --check-prefix=CHECK-max 11 // RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new max 2>&1 | FileCheck %s --check-prefix=CHECK-oom 12 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow 2>&1 | FileCheck %s --check-prefix=CHECK-max 13 // RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t new-nothrow 2>&1 14 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow max 2>&1 | FileCheck %s --check-prefix=CHECK-max 15 // RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t new-nothrow max 2>&1 16 // RUN: %run %t usable 2>&1 17 18 // Tests for various edge cases related to sizes, notably the maximum size the 19 // allocator can allocate. Tests that an integer overflow in the parameters of 20 // calloc is caught. 21 22 #include <assert.h> 23 #include <malloc.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #include <limits> 28 #include <new> 29 30 #include <sanitizer/allocator_interface.h> 31 32 int main(int argc, char **argv) { 33 assert(argc <= 3); 34 bool test_size_max = argc == 3 && !strcmp(argv[2], "max"); 35 36 static const size_t kMaxAllowedMallocSize = 1ULL << 40; 37 static const size_t kChunkHeaderSize = 16; 38 39 size_t MallocSize = test_size_max ? std::numeric_limits<size_t>::max() 40 : kMaxAllowedMallocSize; 41 42 if (!strcmp(argv[1], "malloc")) { 43 void *p = malloc(MallocSize); 44 assert(!p); 45 p = malloc(kMaxAllowedMallocSize - kChunkHeaderSize); 46 assert(!p); 47 } else if (!strcmp(argv[1], "calloc")) { 48 // Trigger an overflow in calloc. 49 size_t size = std::numeric_limits<size_t>::max(); 50 void *p = calloc((size / 0x1000) + 1, 0x1000); 51 assert(!p); 52 } else if (!strcmp(argv[1], "new")) { 53 void *p = operator new(MallocSize); 54 assert(!p); 55 } else if (!strcmp(argv[1], "new-nothrow")) { 56 void *p = operator new(MallocSize, std::nothrow); 57 assert(!p); 58 } else if (!strcmp(argv[1], "usable")) { 59 // Playing with the actual usable size of a chunk. 60 void *p = malloc(1007); 61 assert(p); 62 size_t size = __sanitizer_get_allocated_size(p); 63 assert(size >= 1007); 64 memset(p, 'A', size); 65 p = realloc(p, 2014); 66 assert(p); 67 size = __sanitizer_get_allocated_size(p); 68 assert(size >= 2014); 69 memset(p, 'B', size); 70 free(p); 71 } else { 72 assert(0); 73 } 74 75 return 0; 76 } 77 78 // CHECK-max: {{ERROR: HWAddressSanitizer: requested allocation size .* exceeds maximum supported size}} 79 // CHECK-oom: ERROR: HWAddressSanitizer: allocator is out of memory 80 // CHECK-calloc: ERROR: HWAddressSanitizer: calloc parameters overflow 81