blob: 02b8569e2a6d6b863bdd6b1d15d041a077a7df5f [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.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * 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.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// 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
Brian Silverman20350ac2021-11-17 18:19:55 -080045#include "gperftools/tcmalloc.h"
46#include "tests/testutil.h"
Austin Schuh745610d2015-09-06 18:19:50 -070047
48using std::set;
49
50// Alloc a size that should always fail.
51
52void TryAllocExpectFail(size_t size) {
Brian Silverman20350ac2021-11-17 18:19:55 -080053 void* p1 = noopt(malloc(size));
Austin Schuh745610d2015-09-06 18:19:50 -070054 CHECK(p1 == NULL);
55
Brian Silverman20350ac2021-11-17 18:19:55 -080056 void* p2 = noopt(malloc(1));
Austin Schuh745610d2015-09-06 18:19:50 -070057 CHECK(p2 != NULL);
58
Brian Silverman20350ac2021-11-17 18:19:55 -080059 void* p3 = noopt(realloc(p2, size));
Austin Schuh745610d2015-09-06 18:19:50 -070060 CHECK(p3 == NULL);
61
62 free(p2);
63}
64
65// Alloc a size that might work and might fail.
66// If it does work, touch some pages.
67
68void TryAllocMightFail(size_t size) {
Brian Silverman20350ac2021-11-17 18:19:55 -080069 unsigned char* p = static_cast<unsigned char*>(noopt(malloc(size)));
70 if (p != NULL) {
Austin Schuh745610d2015-09-06 18:19:50 -070071 static const size_t kPoints = 1024;
72
73 for ( size_t i = 0; i < kPoints; ++i ) {
Brian Silverman20350ac2021-11-17 18:19:55 -080074 p[i * (size / kPoints)] = static_cast<unsigned char>(i);
Austin Schuh745610d2015-09-06 18:19:50 -070075 }
76
77 for ( size_t i = 0; i < kPoints; ++i ) {
Brian Silverman20350ac2021-11-17 18:19:55 -080078 CHECK(p[i * (size / kPoints)] == static_cast<unsigned char>(i));
Austin Schuh745610d2015-09-06 18:19:50 -070079 }
80
Brian Silverman20350ac2021-11-17 18:19:55 -080081 p[size-1] = 'M';
82 CHECK(p[size-1] == 'M');
Austin Schuh745610d2015-09-06 18:19:50 -070083 }
84
Brian Silverman20350ac2021-11-17 18:19:55 -080085 free(noopt(p));
Austin Schuh745610d2015-09-06 18:19:50 -070086}
87
88int main (int argc, char** argv) {
89 // Allocate some 0-byte objects. They better be unique.
90 // 0 bytes is not large but it exercises some paths related to
91 // large-allocation code.
92 {
93 static const int kZeroTimes = 1024;
94 printf("Test malloc(0) x %d\n", kZeroTimes);
95 set<char*> p_set;
96 for ( int i = 0; i < kZeroTimes; ++i ) {
97 char* p = new char;
98 CHECK(p != NULL);
99 CHECK(p_set.find(p) == p_set.end());
100 p_set.insert(p_set.end(), p);
101 }
102 // Just leak the memory.
103 }
104
105 // Grab some memory so that some later allocations are guaranteed to fail.
106 printf("Test small malloc\n");
Brian Silverman20350ac2021-11-17 18:19:55 -0800107 void* p_small = noopt(malloc(4*1048576));
Austin Schuh745610d2015-09-06 18:19:50 -0700108 CHECK(p_small != NULL);
109
110 // Test sizes up near the maximum size_t.
111 // These allocations test the wrap-around code.
112 printf("Test malloc(0 - N)\n");
113 const size_t zero = 0;
114 static const size_t kMinusNTimes = 16384;
115 for ( size_t i = 1; i < kMinusNTimes; ++i ) {
116 TryAllocExpectFail(zero - i);
117 }
118
119 // Test sizes a bit smaller.
120 // The small malloc above guarantees that all these return NULL.
121 printf("Test malloc(0 - 1048576 - N)\n");
122 static const size_t kMinusMBMinusNTimes = 16384;
123 for ( size_t i = 0; i < kMinusMBMinusNTimes; ++i) {
124 TryAllocExpectFail(zero - 1048576 - i);
125 }
126
127 // Test sizes at half of size_t.
128 // These might or might not fail to allocate.
129 printf("Test malloc(max/2 +- N)\n");
130 static const size_t kHalfPlusMinusTimes = 64;
131 const size_t half = (zero - 2) / 2 + 1;
132 for ( size_t i = 0; i < kHalfPlusMinusTimes; ++i) {
133 TryAllocMightFail(half - i);
134 TryAllocMightFail(half + i);
135 }
136
137 printf("PASS\n");
138 return 0;
139}