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 reallocarray 2>&1    | FileCheck %s --check-prefix=CHECK-reallocarray
11 // RUN: %env_hwasan_opts=allocator_may_return_null=1     %run %t reallocarray 2>&1
12 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new 2>&1             | FileCheck %s --check-prefix=CHECK-max
13 // RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new 2>&1             | FileCheck %s --check-prefix=CHECK-oom
14 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new max 2>&1         | FileCheck %s --check-prefix=CHECK-max
15 // RUN: %env_hwasan_opts=allocator_may_return_null=1 not %run %t new max 2>&1         | FileCheck %s --check-prefix=CHECK-oom
16 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow 2>&1     | FileCheck %s --check-prefix=CHECK-max
17 // RUN: %env_hwasan_opts=allocator_may_return_null=1     %run %t new-nothrow 2>&1
18 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t new-nothrow max 2>&1 | FileCheck %s --check-prefix=CHECK-max
19 // RUN: %env_hwasan_opts=allocator_may_return_null=1     %run %t new-nothrow max 2>&1
20 // RUN:                                                 %run %t usable 2>&1
21 
22 // Tests for various edge cases related to sizes, notably the maximum size the
23 // allocator can allocate. Tests that an integer overflow in the parameters of
24 // 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 
37 #include "utils.h"
38 
39 int main(int argc, char **argv) {
40   assert(argc <= 3);
41   bool test_size_max = argc == 3 && !untag_strcmp(argv[2], "max");
42 
43   static const size_t kMaxAllowedMallocSize = 1ULL << 40;
44   static const size_t kChunkHeaderSize = 16;
45 
46   size_t MallocSize = test_size_max ? std::numeric_limits<size_t>::max()
47                                     : kMaxAllowedMallocSize;
48 
49   if (!untag_strcmp(argv[1], "malloc")) {
50     void *p = malloc(MallocSize);
51     assert(!p);
52     p = malloc(kMaxAllowedMallocSize - kChunkHeaderSize);
53     assert(!p);
54   } else if (!untag_strcmp(argv[1], "calloc")) {
55     // Trigger an overflow in calloc.
56     size_t size = std::numeric_limits<size_t>::max();
57     void *p = calloc((size / 0x1000) + 1, 0x1000);
58     assert(!p);
59   } else if (!untag_strcmp(argv[1], "reallocarray")) {
60     // Trigger an overflow in reallocarray.
61     size_t size = std::numeric_limits<size_t>::max();
62     void *p = __sanitizer_reallocarray(nullptr, (size / 0x1000) + 1, 0x1000);
63     assert(!p);
64   } else if (!untag_strcmp(argv[1], "new")) {
65     void *p = operator new(MallocSize);
66     assert(!p);
67   } else if (!untag_strcmp(argv[1], "new-nothrow")) {
68     void *p = operator new(MallocSize, std::nothrow);
69     assert(!p);
70   } else if (!untag_strcmp(argv[1], "usable")) {
71     // Playing with the actual usable size of a chunk.
72     void *p = malloc(1007);
73     assert(p);
74     size_t size = __sanitizer_get_allocated_size(p);
75     assert(size >= 1007);
76     memset(p, 'A', size);
77     p = realloc(p, 2014);
78     assert(p);
79     size = __sanitizer_get_allocated_size(p);
80     assert(size >= 2014);
81     memset(p, 'B', size);
82     free(p);
83   } else {
84     assert(0);
85   }
86 
87   return 0;
88 }
89 
90 // CHECK-max: {{ERROR: HWAddressSanitizer: requested allocation size .* exceeds maximum supported size}}
91 // CHECK-oom: ERROR: HWAddressSanitizer: allocator is out of memory
92 // CHECK-calloc: ERROR: HWAddressSanitizer: calloc parameters overflow
93 // CHECK-reallocarray: ERROR: HWAddressSanitizer: reallocarray parameters overflow
94