blob: cdfcd90c96cbaa6d226b251f9289d2cebb28c67c [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// Test the behavior of malloc/calloc/realloc when the allocation size is huge.
2// By default (allocator_may_return_null=0) the process should crash.
3// With allocator_may_return_null=1 the allocator should return 0.
4//
5// RUN: %clangxx_asan -O0 %s -o %t
6// RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -08007// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
8// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mNULL
9// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cCRASH
10// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t calloc 2>&1 | FileCheck %s --check-prefix=CHECK-cNULL
11// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coCRASH
12// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t calloc-overflow 2>&1 | FileCheck %s --check-prefix=CHECK-coNULL
13// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rCRASH
14// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t realloc 2>&1 | FileCheck %s --check-prefix=CHECK-rNULL
15// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrCRASH
16// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t realloc-after-malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mrNULL
Stephen Hines2d1fdb22014-05-28 23:58:16 -070017
18#include <limits.h>
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <assert.h>
23#include <limits>
24int main(int argc, char **argv) {
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080025 // Disable stderr buffering. Needed on Windows.
26 setvbuf(stderr, NULL, _IONBF, 0);
27
Stephen Hines2d1fdb22014-05-28 23:58:16 -070028 volatile size_t size = std::numeric_limits<size_t>::max() - 10000;
29 assert(argc == 2);
Stephen Hines86277eb2015-03-23 12:06:32 -070030 void *x = 0;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070031 if (!strcmp(argv[1], "malloc")) {
32 fprintf(stderr, "malloc:\n");
Stephen Hines86277eb2015-03-23 12:06:32 -070033 x = malloc(size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034 }
35 if (!strcmp(argv[1], "calloc")) {
36 fprintf(stderr, "calloc:\n");
Stephen Hines86277eb2015-03-23 12:06:32 -070037 x = calloc(size / 4, 4);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070038 }
39
40 if (!strcmp(argv[1], "calloc-overflow")) {
41 fprintf(stderr, "calloc-overflow:\n");
42 volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max();
43 size_t kArraySize = 4096;
44 volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
Stephen Hines86277eb2015-03-23 12:06:32 -070045 x = calloc(kArraySize, kArraySize2);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070046 }
47
48 if (!strcmp(argv[1], "realloc")) {
49 fprintf(stderr, "realloc:\n");
Stephen Hines86277eb2015-03-23 12:06:32 -070050 x = realloc(0, size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070051 }
52 if (!strcmp(argv[1], "realloc-after-malloc")) {
53 fprintf(stderr, "realloc-after-malloc:\n");
54 char *t = (char*)malloc(100);
55 *t = 42;
Stephen Hines86277eb2015-03-23 12:06:32 -070056 x = realloc(t, size);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070057 assert(*t == 42);
58 free(t);
59 }
60 // The NULL pointer is printed differently on different systems, while (long)0
61 // is always the same.
62 fprintf(stderr, "x: %lx\n", (long)x);
63 free(x);
64 return x != 0;
65}
66// CHECK-mCRASH: malloc:
67// CHECK-mCRASH: AddressSanitizer's allocator is terminating the process
68// CHECK-cCRASH: calloc:
69// CHECK-cCRASH: AddressSanitizer's allocator is terminating the process
70// CHECK-coCRASH: calloc-overflow:
71// CHECK-coCRASH: AddressSanitizer's allocator is terminating the process
72// CHECK-rCRASH: realloc:
73// CHECK-rCRASH: AddressSanitizer's allocator is terminating the process
74// CHECK-mrCRASH: realloc-after-malloc:
75// CHECK-mrCRASH: AddressSanitizer's allocator is terminating the process
76
77// CHECK-mNULL: malloc:
78// CHECK-mNULL: x: 0
79// CHECK-cNULL: calloc:
80// CHECK-cNULL: x: 0
81// CHECK-coNULL: calloc-overflow:
82// CHECK-coNULL: x: 0
83// CHECK-rNULL: realloc:
84// CHECK-rNULL: x: 0
85// CHECK-mrNULL: realloc-after-malloc:
86// CHECK-mrNULL: x: 0