90 lines · cpp
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 || %clangxx_hwasan %s -o %t4// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-max5// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t malloc 2>&16// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t malloc max 2>&1 | FileCheck %s --check-prefix=CHECK-max7// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t malloc max 2>&18// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-calloc9// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t calloc 2>&110// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t reallocarray 2>&1 | FileCheck %s --check-prefix=CHECK-reallocarray11// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t reallocarray 2>&112// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new 2>&1 | FileCheck %s --check-prefix=CHECK-max13// RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new 2>&1 | FileCheck %s --check-prefix=CHECK-oom14// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new max 2>&1 | FileCheck %s --check-prefix=CHECK-max15// RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new max 2>&1 | FileCheck %s --check-prefix=CHECK-oom16// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow 2>&1 | FileCheck %s --check-prefix=CHECK-max17// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t new-nothrow 2>&118// RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow max 2>&1 | FileCheck %s --check-prefix=CHECK-max19// RUN: %env_hwasan_opts=allocator_may_return_null=1 %run %t new-nothrow max 2>&120// RUN: %run %t usable 2>&121 22// Tests for various edge cases related to sizes, notably the maximum size the23// allocator can allocate. Tests that an integer overflow in the parameters of24// calloc is caught.25 26#include <assert.h>27#include <malloc.h>28#include <stdlib.h>29#include <string.h>30 31#include <limits>32#include <new>33 34#include <sanitizer/allocator_interface.h>35#include <sanitizer/hwasan_interface.h>36 37int main(int argc, char **argv) {38 assert(argc <= 3);39 bool test_size_max = argc == 3 && !strcmp(argv[2], "max");40 41 static const size_t kMaxAllowedMallocSize = 1ULL << 40;42 static const size_t kChunkHeaderSize = 16;43 44 size_t MallocSize = test_size_max ? std::numeric_limits<size_t>::max()45 : (kMaxAllowedMallocSize + 1);46 47 if (!strcmp(argv[1], "malloc")) {48 void *p = malloc(MallocSize);49 assert(!p);50 } else if (!strcmp(argv[1], "calloc")) {51 // Trigger an overflow in calloc.52 size_t size = std::numeric_limits<size_t>::max();53 void *p = calloc((size / 0x1000) + 1, 0x1000);54 assert(!p);55 } else if (!strcmp(argv[1], "reallocarray")) {56 // Trigger an overflow in reallocarray.57 size_t size = std::numeric_limits<size_t>::max();58 void *p = __sanitizer_reallocarray(nullptr, (size / 0x1000) + 1, 0x1000);59 assert(!p);60 } else if (!strcmp(argv[1], "new")) {61 void *p = operator new(MallocSize);62 assert(!p);63 } else if (!strcmp(argv[1], "new-nothrow")) {64 void *p = operator new(MallocSize, std::nothrow);65 assert(!p);66 } else if (!strcmp(argv[1], "usable")) {67 // Playing with the actual usable size of a chunk.68 void *p = malloc(1007);69 assert(p);70 size_t size = __sanitizer_get_allocated_size(p);71 assert(size >= 1007);72 memset(p, 'A', size);73 p = realloc(p, 2014);74 assert(p);75 size = __sanitizer_get_allocated_size(p);76 assert(size >= 2014);77 memset(p, 'B', size);78 free(p);79 } else {80 assert(0);81 }82 83 return 0;84}85 86// CHECK-max: {{ERROR: HWAddressSanitizer: requested allocation size .* exceeds maximum supported size}}87// CHECK-oom: ERROR: HWAddressSanitizer: out of memory: allocator is trying to allocate88// CHECK-calloc: ERROR: HWAddressSanitizer: calloc parameters overflow89// CHECK-reallocarray: ERROR: HWAddressSanitizer: reallocarray parameters overflow90