1 //===-- secondary_test.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "tests/scudo_unit_test.h"
10 
11 #include "allocator_config.h"
12 #include "secondary.h"
13 
14 #include <stdio.h>
15 
16 #include <condition_variable>
17 #include <mutex>
18 #include <random>
19 #include <thread>
20 #include <vector>
21 
22 template <typename Config> static void testSecondaryBasic(void) {
23   using SecondaryT = scudo::MapAllocator<Config>;
24 
25   scudo::GlobalStats S;
26   S.init();
27   std::unique_ptr<SecondaryT> L(new SecondaryT);
28   L->init(&S);
29   const scudo::uptr Size = 1U << 16;
30   void *P = L->allocate(Size);
31   EXPECT_NE(P, nullptr);
32   memset(P, 'A', Size);
33   EXPECT_GE(SecondaryT::getBlockSize(P), Size);
34   L->deallocate(P);
35   // If the Secondary can't cache that pointer, it will be unmapped.
36   if (!L->canCache(Size))
37     EXPECT_DEATH(memset(P, 'A', Size), "");
38 
39   const scudo::uptr Align = 1U << 16;
40   P = L->allocate(Size + Align, Align);
41   EXPECT_NE(P, nullptr);
42   void *AlignedP = reinterpret_cast<void *>(
43       scudo::roundUpTo(reinterpret_cast<scudo::uptr>(P), Align));
44   memset(AlignedP, 'A', Size);
45   L->deallocate(P);
46 
47   std::vector<void *> V;
48   for (scudo::uptr I = 0; I < 32U; I++)
49     V.push_back(L->allocate(Size));
50   std::shuffle(V.begin(), V.end(), std::mt19937(std::random_device()()));
51   while (!V.empty()) {
52     L->deallocate(V.back());
53     V.pop_back();
54   }
55   scudo::ScopedString Str(1024);
56   L->getStats(&Str);
57   Str.output();
58 }
59 
60 struct NoCacheConfig {
61   typedef scudo::MapAllocatorNoCache SecondaryCache;
62 };
63 
64 struct TestConfig {
65   typedef scudo::MapAllocatorCache<TestConfig> SecondaryCache;
66   static const scudo::u32 SecondaryCacheEntriesArraySize = 128U;
67   static const scudo::u32 SecondaryCacheDefaultMaxEntriesCount = 64U;
68   static const scudo::uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 20;
69   static const scudo::s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN;
70   static const scudo::s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX;
71 };
72 
73 TEST(ScudoSecondaryTest, SecondaryBasic) {
74   testSecondaryBasic<NoCacheConfig>();
75   testSecondaryBasic<scudo::DefaultConfig>();
76   testSecondaryBasic<TestConfig>();
77 }
78 
79 using LargeAllocator = scudo::MapAllocator<scudo::DefaultConfig>;
80 
81 // This exercises a variety of combinations of size and alignment for the
82 // MapAllocator. The size computation done here mimic the ones done by the
83 // combined allocator.
84 TEST(ScudoSecondaryTest, SecondaryCombinations) {
85   constexpr scudo::uptr MinAlign = FIRST_32_SECOND_64(8, 16);
86   constexpr scudo::uptr HeaderSize = scudo::roundUpTo(8, MinAlign);
87   std::unique_ptr<LargeAllocator> L(new LargeAllocator);
88   L->init(nullptr);
89   for (scudo::uptr SizeLog = 0; SizeLog <= 20; SizeLog++) {
90     for (scudo::uptr AlignLog = FIRST_32_SECOND_64(3, 4); AlignLog <= 16;
91          AlignLog++) {
92       const scudo::uptr Align = 1U << AlignLog;
93       for (scudo::sptr Delta = -128; Delta <= 128; Delta += 8) {
94         if (static_cast<scudo::sptr>(1U << SizeLog) + Delta <= 0)
95           continue;
96         const scudo::uptr UserSize =
97             scudo::roundUpTo((1U << SizeLog) + Delta, MinAlign);
98         const scudo::uptr Size =
99             HeaderSize + UserSize + (Align > MinAlign ? Align - HeaderSize : 0);
100         void *P = L->allocate(Size, Align);
101         EXPECT_NE(P, nullptr);
102         void *AlignedP = reinterpret_cast<void *>(
103             scudo::roundUpTo(reinterpret_cast<scudo::uptr>(P), Align));
104         memset(AlignedP, 0xff, UserSize);
105         L->deallocate(P);
106       }
107     }
108   }
109   scudo::ScopedString Str(1024);
110   L->getStats(&Str);
111   Str.output();
112 }
113 
114 TEST(ScudoSecondaryTest, SecondaryIterate) {
115   std::unique_ptr<LargeAllocator> L(new LargeAllocator);
116   L->init(nullptr);
117   std::vector<void *> V;
118   const scudo::uptr PageSize = scudo::getPageSizeCached();
119   for (scudo::uptr I = 0; I < 32U; I++)
120     V.push_back(L->allocate((std::rand() % 16) * PageSize));
121   auto Lambda = [V](scudo::uptr Block) {
122     EXPECT_NE(std::find(V.begin(), V.end(), reinterpret_cast<void *>(Block)),
123               V.end());
124   };
125   L->disable();
126   L->iterateOverBlocks(Lambda);
127   L->enable();
128   while (!V.empty()) {
129     L->deallocate(V.back());
130     V.pop_back();
131   }
132   scudo::ScopedString Str(1024);
133   L->getStats(&Str);
134   Str.output();
135 }
136 
137 TEST(ScudoSecondaryTest, SecondaryOptions) {
138   std::unique_ptr<LargeAllocator> L(new LargeAllocator);
139   L->init(nullptr);
140   // Attempt to set a maximum number of entries higher than the array size.
141   EXPECT_FALSE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4096U));
142   // A negative number will be cast to a scudo::u32, and fail.
143   EXPECT_FALSE(L->setOption(scudo::Option::MaxCacheEntriesCount, -1));
144   if (L->canCache(0U)) {
145     // Various valid combinations.
146     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4U));
147     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 20));
148     EXPECT_TRUE(L->canCache(1UL << 18));
149     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 17));
150     EXPECT_FALSE(L->canCache(1UL << 18));
151     EXPECT_TRUE(L->canCache(1UL << 16));
152     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 0U));
153     EXPECT_FALSE(L->canCache(1UL << 16));
154     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4U));
155     EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 20));
156     EXPECT_TRUE(L->canCache(1UL << 16));
157   }
158 }
159 
160 static std::mutex Mutex;
161 static std::condition_variable Cv;
162 static bool Ready;
163 
164 static void performAllocations(LargeAllocator *L) {
165   std::vector<void *> V;
166   const scudo::uptr PageSize = scudo::getPageSizeCached();
167   {
168     std::unique_lock<std::mutex> Lock(Mutex);
169     while (!Ready)
170       Cv.wait(Lock);
171   }
172   for (scudo::uptr I = 0; I < 128U; I++) {
173     // Deallocate 75% of the blocks.
174     const bool Deallocate = (rand() & 3) != 0;
175     void *P = L->allocate((std::rand() % 16) * PageSize);
176     if (Deallocate)
177       L->deallocate(P);
178     else
179       V.push_back(P);
180   }
181   while (!V.empty()) {
182     L->deallocate(V.back());
183     V.pop_back();
184   }
185 }
186 
187 TEST(ScudoSecondaryTest, SecondaryThreadsRace) {
188   Ready = false;
189   std::unique_ptr<LargeAllocator> L(new LargeAllocator);
190   L->init(nullptr, /*ReleaseToOsInterval=*/0);
191   std::thread Threads[16];
192   for (scudo::uptr I = 0; I < ARRAY_SIZE(Threads); I++)
193     Threads[I] = std::thread(performAllocations, L.get());
194   {
195     std::unique_lock<std::mutex> Lock(Mutex);
196     Ready = true;
197     Cv.notify_all();
198   }
199   for (auto &T : Threads)
200     T.join();
201   scudo::ScopedString Str(1024);
202   L->getStats(&Str);
203   Str.output();
204 }
205