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