blob: ff220075f224de5041b4c03f407fd815ac442c72 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Michael Chastain
33//
34// This is a unit test for large allocations in malloc and friends.
35// "Large" means "so large that they overflow the address space".
36// For 32 bits, this means allocations near 2^32 bytes and 2^31 bytes.
37// For 64 bits, this means allocations near 2^64 bytes and 2^63 bytes.
38
39#include <stddef.h> // for size_t, NULL
40#include <stdlib.h> // for malloc, free, realloc
41#include <stdio.h>
42#include <set> // for set, etc
43
44#include "base/logging.h" // for operator<<, CHECK, etc
45
46using std::set;
47
48// Alloc a size that should always fail.
49
50void TryAllocExpectFail(size_t size) {
51 void* p1 = malloc(size);
52 CHECK(p1 == NULL);
53
54 void* p2 = malloc(1);
55 CHECK(p2 != NULL);
56
57 void* p3 = realloc(p2, size);
58 CHECK(p3 == NULL);
59
60 free(p2);
61}
62
63// Alloc a size that might work and might fail.
64// If it does work, touch some pages.
65
66void TryAllocMightFail(size_t size) {
67 unsigned char* p = static_cast<unsigned char*>(malloc(size));
68 if ( p != NULL ) {
69 unsigned char volatile* vp = p; // prevent optimizations
70 static const size_t kPoints = 1024;
71
72 for ( size_t i = 0; i < kPoints; ++i ) {
73 vp[i * (size / kPoints)] = static_cast<unsigned char>(i);
74 }
75
76 for ( size_t i = 0; i < kPoints; ++i ) {
77 CHECK(vp[i * (size / kPoints)] == static_cast<unsigned char>(i));
78 }
79
80 vp[size-1] = 'M';
81 CHECK(vp[size-1] == 'M');
82 }
83
84 free(p);
85}
86
87int main (int argc, char** argv) {
88 // Allocate some 0-byte objects. They better be unique.
89 // 0 bytes is not large but it exercises some paths related to
90 // large-allocation code.
91 {
92 static const int kZeroTimes = 1024;
93 printf("Test malloc(0) x %d\n", kZeroTimes);
94 set<char*> p_set;
95 for ( int i = 0; i < kZeroTimes; ++i ) {
96 char* p = new char;
97 CHECK(p != NULL);
98 CHECK(p_set.find(p) == p_set.end());
99 p_set.insert(p_set.end(), p);
100 }
101 // Just leak the memory.
102 }
103
104 // Grab some memory so that some later allocations are guaranteed to fail.
105 printf("Test small malloc\n");
106 void* p_small = malloc(4*1048576);
107 CHECK(p_small != NULL);
108
109 // Test sizes up near the maximum size_t.
110 // These allocations test the wrap-around code.
111 printf("Test malloc(0 - N)\n");
112 const size_t zero = 0;
113 static const size_t kMinusNTimes = 16384;
114 for ( size_t i = 1; i < kMinusNTimes; ++i ) {
115 TryAllocExpectFail(zero - i);
116 }
117
118 // Test sizes a bit smaller.
119 // The small malloc above guarantees that all these return NULL.
120 printf("Test malloc(0 - 1048576 - N)\n");
121 static const size_t kMinusMBMinusNTimes = 16384;
122 for ( size_t i = 0; i < kMinusMBMinusNTimes; ++i) {
123 TryAllocExpectFail(zero - 1048576 - i);
124 }
125
126 // Test sizes at half of size_t.
127 // These might or might not fail to allocate.
128 printf("Test malloc(max/2 +- N)\n");
129 static const size_t kHalfPlusMinusTimes = 64;
130 const size_t half = (zero - 2) / 2 + 1;
131 for ( size_t i = 0; i < kHalfPlusMinusTimes; ++i) {
132 TryAllocMightFail(half - i);
133 TryAllocMightFail(half + i);
134 }
135
136 printf("PASS\n");
137 return 0;
138}