1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/base/internal/low_level_alloc.h"
16
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <thread> // NOLINT(build/c++11)
21 #include <unordered_map>
22 #include <utility>
23
24 namespace absl {
25 ABSL_NAMESPACE_BEGIN
26 namespace base_internal {
27 namespace {
28
29 // This test doesn't use gtest since it needs to test that everything
30 // works before main().
31 #define TEST_ASSERT(x) \
32 if (!(x)) { \
33 printf("TEST_ASSERT(%s) FAILED ON LINE %d\n", #x, __LINE__); \
34 abort(); \
35 }
36
37 // a block of memory obtained from the allocator
38 struct BlockDesc {
39 char *ptr; // pointer to memory
40 int len; // number of bytes
41 int fill; // filled with data starting with this
42 };
43
44 // Check that the pattern placed in the block d
45 // by RandomizeBlockDesc is still there.
CheckBlockDesc(const BlockDesc & d)46 static void CheckBlockDesc(const BlockDesc &d) {
47 for (int i = 0; i != d.len; i++) {
48 TEST_ASSERT((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
49 }
50 }
51
52 // Fill the block "*d" with a pattern
53 // starting with a random byte.
RandomizeBlockDesc(BlockDesc * d)54 static void RandomizeBlockDesc(BlockDesc *d) {
55 d->fill = rand() & 0xff;
56 for (int i = 0; i != d->len; i++) {
57 d->ptr[i] = (d->fill + i) & 0xff;
58 }
59 }
60
61 // Use to indicate to the malloc hooks that
62 // this calls is from LowLevelAlloc.
63 static bool using_low_level_alloc = false;
64
65 // n times, toss a coin, and based on the outcome
66 // either allocate a new block or deallocate an old block.
67 // New blocks are placed in a std::unordered_map with a random key
68 // and initialized with RandomizeBlockDesc().
69 // If keys conflict, the older block is freed.
70 // Old blocks are always checked with CheckBlockDesc()
71 // before being freed. At the end of the run,
72 // all remaining allocated blocks are freed.
73 // If use_new_arena is true, use a fresh arena, and then delete it.
74 // If call_malloc_hook is true and user_arena is true,
75 // allocations and deallocations are reported via the MallocHook
76 // interface.
Test(bool use_new_arena,bool call_malloc_hook,int n)77 static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
78 typedef std::unordered_map<int, BlockDesc> AllocMap;
79 AllocMap allocated;
80 AllocMap::iterator it;
81 BlockDesc block_desc;
82 int rnd;
83 LowLevelAlloc::Arena *arena = 0;
84 if (use_new_arena) {
85 int32_t flags = call_malloc_hook ? LowLevelAlloc::kCallMallocHook : 0;
86 arena = LowLevelAlloc::NewArena(flags);
87 }
88 for (int i = 0; i != n; i++) {
89 if (i != 0 && i % 10000 == 0) {
90 printf(".");
91 fflush(stdout);
92 }
93
94 switch (rand() & 1) { // toss a coin
95 case 0: // coin came up heads: add a block
96 using_low_level_alloc = true;
97 block_desc.len = rand() & 0x3fff;
98 block_desc.ptr =
99 reinterpret_cast<char *>(
100 arena == 0
101 ? LowLevelAlloc::Alloc(block_desc.len)
102 : LowLevelAlloc::AllocWithArena(block_desc.len, arena));
103 using_low_level_alloc = false;
104 RandomizeBlockDesc(&block_desc);
105 rnd = rand();
106 it = allocated.find(rnd);
107 if (it != allocated.end()) {
108 CheckBlockDesc(it->second);
109 using_low_level_alloc = true;
110 LowLevelAlloc::Free(it->second.ptr);
111 using_low_level_alloc = false;
112 it->second = block_desc;
113 } else {
114 allocated[rnd] = block_desc;
115 }
116 break;
117 case 1: // coin came up tails: remove a block
118 it = allocated.begin();
119 if (it != allocated.end()) {
120 CheckBlockDesc(it->second);
121 using_low_level_alloc = true;
122 LowLevelAlloc::Free(it->second.ptr);
123 using_low_level_alloc = false;
124 allocated.erase(it);
125 }
126 break;
127 }
128 }
129 // remove all remaining blocks
130 while ((it = allocated.begin()) != allocated.end()) {
131 CheckBlockDesc(it->second);
132 using_low_level_alloc = true;
133 LowLevelAlloc::Free(it->second.ptr);
134 using_low_level_alloc = false;
135 allocated.erase(it);
136 }
137 if (use_new_arena) {
138 TEST_ASSERT(LowLevelAlloc::DeleteArena(arena));
139 }
140 }
141
142 // LowLevelAlloc is designed to be safe to call before main().
143 static struct BeforeMain {
BeforeMainabsl::base_internal::__anon567d4a750111::BeforeMain144 BeforeMain() {
145 Test(false, false, 50000);
146 Test(true, false, 50000);
147 Test(true, true, 50000);
148 }
149 } before_main;
150
151 } // namespace
152 } // namespace base_internal
153 ABSL_NAMESPACE_END
154 } // namespace absl
155
main(int argc,char * argv[])156 int main(int argc, char *argv[]) {
157 // The actual test runs in the global constructor of `before_main`.
158 printf("PASS\n");
159 return 0;
160 }
161