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