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 #include "absl/container/node_hash_map.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace base_internal {
29 namespace {
30 
31 // This test doesn't use gtest since it needs to test that everything
32 // works before main().
33 #define TEST_ASSERT(x)                                           \
34   if (!(x)) {                                                    \
35     printf("TEST_ASSERT(%s) FAILED ON LINE %d\n", #x, __LINE__); \
36     abort();                                                     \
37   }
38 
39 // a block of memory obtained from the allocator
40 struct BlockDesc {
41   char *ptr;      // pointer to memory
42   int len;        // number of bytes
43   int fill;       // filled with data starting with this
44 };
45 
46 // Check that the pattern placed in the block d
47 // by RandomizeBlockDesc is still there.
CheckBlockDesc(const BlockDesc & d)48 static void CheckBlockDesc(const BlockDesc &d) {
49   for (int i = 0; i != d.len; i++) {
50     TEST_ASSERT((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
51   }
52 }
53 
54 // Fill the block "*d" with a pattern
55 // starting with a random byte.
RandomizeBlockDesc(BlockDesc * d)56 static void RandomizeBlockDesc(BlockDesc *d) {
57   d->fill = rand() & 0xff;
58   for (int i = 0; i != d->len; i++) {
59     d->ptr[i] = (d->fill + i) & 0xff;
60   }
61 }
62 
63 // Use to indicate to the malloc hooks that
64 // this calls is from LowLevelAlloc.
65 static bool using_low_level_alloc = false;
66 
67 // n times, toss a coin, and based on the outcome
68 // either allocate a new block or deallocate an old block.
69 // New blocks are placed in a std::unordered_map with a random key
70 // and initialized with RandomizeBlockDesc().
71 // If keys conflict, the older block is freed.
72 // Old blocks are always checked with CheckBlockDesc()
73 // before being freed.  At the end of the run,
74 // all remaining allocated blocks are freed.
75 // If use_new_arena is true, use a fresh arena, and then delete it.
76 // If call_malloc_hook is true and user_arena is true,
77 // allocations and deallocations are reported via the MallocHook
78 // interface.
Test(bool use_new_arena,bool call_malloc_hook,int n)79 static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
80   typedef absl::node_hash_map<int, BlockDesc> AllocMap;
81   AllocMap allocated;
82   AllocMap::iterator it;
83   BlockDesc block_desc;
84   int rnd;
85   LowLevelAlloc::Arena *arena = 0;
86   if (use_new_arena) {
87     int32_t flags = call_malloc_hook ? LowLevelAlloc::kCallMallocHook : 0;
88     arena = LowLevelAlloc::NewArena(flags);
89   }
90   for (int i = 0; i != n; i++) {
91     if (i != 0 && i % 10000 == 0) {
92       printf(".");
93       fflush(stdout);
94     }
95 
96     switch (rand() & 1) {      // toss a coin
97     case 0:     // coin came up heads: add a block
98       using_low_level_alloc = true;
99       block_desc.len = rand() & 0x3fff;
100       block_desc.ptr =
101         reinterpret_cast<char *>(
102                         arena == 0
103                         ? LowLevelAlloc::Alloc(block_desc.len)
104                         : LowLevelAlloc::AllocWithArena(block_desc.len, arena));
105       using_low_level_alloc = false;
106       RandomizeBlockDesc(&block_desc);
107       rnd = rand();
108       it = allocated.find(rnd);
109       if (it != allocated.end()) {
110         CheckBlockDesc(it->second);
111         using_low_level_alloc = true;
112         LowLevelAlloc::Free(it->second.ptr);
113         using_low_level_alloc = false;
114         it->second = block_desc;
115       } else {
116         allocated[rnd] = block_desc;
117       }
118       break;
119     case 1:     // coin came up tails: remove a block
120       it = allocated.begin();
121       if (it != allocated.end()) {
122         CheckBlockDesc(it->second);
123         using_low_level_alloc = true;
124         LowLevelAlloc::Free(it->second.ptr);
125         using_low_level_alloc = false;
126         allocated.erase(it);
127       }
128       break;
129     }
130   }
131   // remove all remaining blocks
132   while ((it = allocated.begin()) != allocated.end()) {
133     CheckBlockDesc(it->second);
134     using_low_level_alloc = true;
135     LowLevelAlloc::Free(it->second.ptr);
136     using_low_level_alloc = false;
137     allocated.erase(it);
138   }
139   if (use_new_arena) {
140     TEST_ASSERT(LowLevelAlloc::DeleteArena(arena));
141   }
142 }
143 
144 // LowLevelAlloc is designed to be safe to call before main().
145 static struct BeforeMain {
BeforeMainabsl::base_internal::__anond87b53fa0111::BeforeMain146   BeforeMain() {
147     Test(false, false, 50000);
148     Test(true, false, 50000);
149     Test(true, true, 50000);
150   }
151 } before_main;
152 
153 }  // namespace
154 }  // namespace base_internal
155 ABSL_NAMESPACE_END
156 }  // namespace absl
157 
main(int argc,char * argv[])158 int main(int argc, char *argv[]) {
159   // The actual test runs in the global constructor of `before_main`.
160   printf("PASS\n");
161   return 0;
162 }
163