Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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 | |
Dynamic Tools Team | 09e6d48 | 2019-11-26 18:18:14 -0800 | [diff] [blame] | 9 | #include "tests/scudo_unit_test.h" |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 10 | |
Peter Collingbourne | 7488a17 | 2020-12-14 13:57:59 -0800 | [diff] [blame] | 11 | #include "allocator_config.h" |
Dynamic Tools Team | 09e6d48 | 2019-11-26 18:18:14 -0800 | [diff] [blame] | 12 | #include "secondary.h" |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 13 | |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include <condition_variable> |
| 17 | #include <mutex> |
Dynamic Tools Team | d2740c5 | 2019-11-19 13:58:06 -0800 | [diff] [blame] | 18 | #include <random> |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 19 | #include <thread> |
Dynamic Tools Team | 09e6d48 | 2019-11-26 18:18:14 -0800 | [diff] [blame] | 20 | #include <vector> |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 21 | |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 22 | template <typename Config> static void testSecondaryBasic(void) { |
| 23 | using SecondaryT = scudo::MapAllocator<Config>; |
| 24 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 25 | scudo::GlobalStats S; |
| 26 | S.init(); |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 27 | std::unique_ptr<SecondaryT> L(new SecondaryT); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 28 | L->init(&S); |
| 29 | const scudo::uptr Size = 1U << 16; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 30 | void *P = L->allocate(scudo::Options{}, Size); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 31 | EXPECT_NE(P, nullptr); |
| 32 | memset(P, 'A', Size); |
Dynamic Tools Team | d29271f | 2019-10-31 10:31:49 -0700 | [diff] [blame] | 33 | EXPECT_GE(SecondaryT::getBlockSize(P), Size); |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 34 | L->deallocate(scudo::Options{}, P); |
Mitch Phillips | a99b779 | 2021-05-20 10:56:47 -0700 | [diff] [blame] | 35 | |
Dynamic Tools Team | c5d5abc | 2020-01-27 14:03:21 -0800 | [diff] [blame] | 36 | // If the Secondary can't cache that pointer, it will be unmapped. |
Vitaly Buka | 5cb9636 | 2021-05-20 16:17:25 -0700 | [diff] [blame] | 37 | if (!L->canCache(Size)) { |
| 38 | EXPECT_DEATH( |
| 39 | { |
| 40 | // Repeat few time to avoid missing crash if it's mmaped by unrelated |
| 41 | // code. |
| 42 | for (int i = 0; i < 10; ++i) { |
| 43 | P = L->allocate(scudo::Options{}, Size); |
| 44 | L->deallocate(scudo::Options{}, P); |
| 45 | memset(P, 'A', Size); |
| 46 | } |
| 47 | }, |
| 48 | ""); |
Vitaly Buka | 5cb9636 | 2021-05-20 16:17:25 -0700 | [diff] [blame] | 49 | } |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 50 | |
| 51 | const scudo::uptr Align = 1U << 16; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 52 | P = L->allocate(scudo::Options{}, Size + Align, Align); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 53 | EXPECT_NE(P, nullptr); |
| 54 | void *AlignedP = reinterpret_cast<void *>( |
| 55 | scudo::roundUpTo(reinterpret_cast<scudo::uptr>(P), Align)); |
| 56 | memset(AlignedP, 'A', Size); |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 57 | L->deallocate(scudo::Options{}, P); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 58 | |
| 59 | std::vector<void *> V; |
| 60 | for (scudo::uptr I = 0; I < 32U; I++) |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 61 | V.push_back(L->allocate(scudo::Options{}, Size)); |
Dynamic Tools Team | d2740c5 | 2019-11-19 13:58:06 -0800 | [diff] [blame] | 62 | std::shuffle(V.begin(), V.end(), std::mt19937(std::random_device()())); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 63 | while (!V.empty()) { |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 64 | L->deallocate(scudo::Options{}, V.back()); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 65 | V.pop_back(); |
| 66 | } |
Kostya Kortchinsky | 53aea5c | 2021-06-03 12:11:05 -0700 | [diff] [blame^] | 67 | scudo::ScopedString Str; |
Dynamic Tools Team | 3e8c65b | 2019-10-18 20:00:32 +0000 | [diff] [blame] | 68 | L->getStats(&Str); |
| 69 | Str.output(); |
Mitch Phillips | da86a54 | 2021-05-24 16:08:57 -0700 | [diff] [blame] | 70 | L->unmapTestOnly(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 73 | struct NoCacheConfig { |
| 74 | typedef scudo::MapAllocatorNoCache SecondaryCache; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 75 | static const bool MaySupportMemoryTagging = false; |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Peter Collingbourne | 7488a17 | 2020-12-14 13:57:59 -0800 | [diff] [blame] | 78 | struct TestConfig { |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 79 | typedef scudo::MapAllocatorCache<TestConfig> SecondaryCache; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 80 | static const bool MaySupportMemoryTagging = false; |
Peter Collingbourne | 7488a17 | 2020-12-14 13:57:59 -0800 | [diff] [blame] | 81 | static const scudo::u32 SecondaryCacheEntriesArraySize = 128U; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 82 | static const scudo::u32 SecondaryCacheQuarantineSize = 0U; |
Peter Collingbourne | 7488a17 | 2020-12-14 13:57:59 -0800 | [diff] [blame] | 83 | static const scudo::u32 SecondaryCacheDefaultMaxEntriesCount = 64U; |
| 84 | static const scudo::uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 20; |
| 85 | static const scudo::s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN; |
| 86 | static const scudo::s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX; |
| 87 | }; |
| 88 | |
Dynamic Tools Team | d29271f | 2019-10-31 10:31:49 -0700 | [diff] [blame] | 89 | TEST(ScudoSecondaryTest, SecondaryBasic) { |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 90 | testSecondaryBasic<NoCacheConfig>(); |
| 91 | testSecondaryBasic<scudo::DefaultConfig>(); |
| 92 | testSecondaryBasic<TestConfig>(); |
Dynamic Tools Team | d29271f | 2019-10-31 10:31:49 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Peter Collingbourne | 4edf4ef | 2020-12-15 15:32:32 -0800 | [diff] [blame] | 95 | using LargeAllocator = scudo::MapAllocator<scudo::DefaultConfig>; |
Dynamic Tools Team | d29271f | 2019-10-31 10:31:49 -0700 | [diff] [blame] | 96 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 97 | // This exercises a variety of combinations of size and alignment for the |
| 98 | // MapAllocator. The size computation done here mimic the ones done by the |
| 99 | // combined allocator. |
| 100 | TEST(ScudoSecondaryTest, SecondaryCombinations) { |
| 101 | constexpr scudo::uptr MinAlign = FIRST_32_SECOND_64(8, 16); |
| 102 | constexpr scudo::uptr HeaderSize = scudo::roundUpTo(8, MinAlign); |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 103 | std::unique_ptr<LargeAllocator> L(new LargeAllocator); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 104 | L->init(nullptr); |
| 105 | for (scudo::uptr SizeLog = 0; SizeLog <= 20; SizeLog++) { |
| 106 | for (scudo::uptr AlignLog = FIRST_32_SECOND_64(3, 4); AlignLog <= 16; |
| 107 | AlignLog++) { |
| 108 | const scudo::uptr Align = 1U << AlignLog; |
| 109 | for (scudo::sptr Delta = -128; Delta <= 128; Delta += 8) { |
| 110 | if (static_cast<scudo::sptr>(1U << SizeLog) + Delta <= 0) |
| 111 | continue; |
| 112 | const scudo::uptr UserSize = |
| 113 | scudo::roundUpTo((1U << SizeLog) + Delta, MinAlign); |
| 114 | const scudo::uptr Size = |
| 115 | HeaderSize + UserSize + (Align > MinAlign ? Align - HeaderSize : 0); |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 116 | void *P = L->allocate(scudo::Options{}, Size, Align); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 117 | EXPECT_NE(P, nullptr); |
| 118 | void *AlignedP = reinterpret_cast<void *>( |
| 119 | scudo::roundUpTo(reinterpret_cast<scudo::uptr>(P), Align)); |
| 120 | memset(AlignedP, 0xff, UserSize); |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 121 | L->deallocate(scudo::Options{}, P); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
Kostya Kortchinsky | 53aea5c | 2021-06-03 12:11:05 -0700 | [diff] [blame^] | 125 | scudo::ScopedString Str; |
Dynamic Tools Team | 3e8c65b | 2019-10-18 20:00:32 +0000 | [diff] [blame] | 126 | L->getStats(&Str); |
| 127 | Str.output(); |
Mitch Phillips | da86a54 | 2021-05-24 16:08:57 -0700 | [diff] [blame] | 128 | L->unmapTestOnly(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST(ScudoSecondaryTest, SecondaryIterate) { |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 132 | std::unique_ptr<LargeAllocator> L(new LargeAllocator); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 133 | L->init(nullptr); |
| 134 | std::vector<void *> V; |
| 135 | const scudo::uptr PageSize = scudo::getPageSizeCached(); |
| 136 | for (scudo::uptr I = 0; I < 32U; I++) |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 137 | V.push_back(L->allocate(scudo::Options{}, (std::rand() % 16) * PageSize)); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 138 | auto Lambda = [V](scudo::uptr Block) { |
| 139 | EXPECT_NE(std::find(V.begin(), V.end(), reinterpret_cast<void *>(Block)), |
| 140 | V.end()); |
| 141 | }; |
| 142 | L->disable(); |
| 143 | L->iterateOverBlocks(Lambda); |
| 144 | L->enable(); |
| 145 | while (!V.empty()) { |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 146 | L->deallocate(scudo::Options{}, V.back()); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 147 | V.pop_back(); |
| 148 | } |
Kostya Kortchinsky | 53aea5c | 2021-06-03 12:11:05 -0700 | [diff] [blame^] | 149 | scudo::ScopedString Str; |
Dynamic Tools Team | 3e8c65b | 2019-10-18 20:00:32 +0000 | [diff] [blame] | 150 | L->getStats(&Str); |
| 151 | Str.output(); |
Mitch Phillips | da86a54 | 2021-05-24 16:08:57 -0700 | [diff] [blame] | 152 | L->unmapTestOnly(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 155 | TEST(ScudoSecondaryTest, SecondaryOptions) { |
| 156 | std::unique_ptr<LargeAllocator> L(new LargeAllocator); |
| 157 | L->init(nullptr); |
| 158 | // Attempt to set a maximum number of entries higher than the array size. |
| 159 | EXPECT_FALSE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4096U)); |
| 160 | // A negative number will be cast to a scudo::u32, and fail. |
| 161 | EXPECT_FALSE(L->setOption(scudo::Option::MaxCacheEntriesCount, -1)); |
| 162 | if (L->canCache(0U)) { |
| 163 | // Various valid combinations. |
| 164 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4U)); |
| 165 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 20)); |
| 166 | EXPECT_TRUE(L->canCache(1UL << 18)); |
| 167 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 17)); |
| 168 | EXPECT_FALSE(L->canCache(1UL << 18)); |
| 169 | EXPECT_TRUE(L->canCache(1UL << 16)); |
| 170 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 0U)); |
| 171 | EXPECT_FALSE(L->canCache(1UL << 16)); |
| 172 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntriesCount, 4U)); |
| 173 | EXPECT_TRUE(L->setOption(scudo::Option::MaxCacheEntrySize, 1UL << 20)); |
| 174 | EXPECT_TRUE(L->canCache(1UL << 16)); |
| 175 | } |
Mitch Phillips | da86a54 | 2021-05-24 16:08:57 -0700 | [diff] [blame] | 176 | L->unmapTestOnly(); |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 179 | static std::mutex Mutex; |
| 180 | static std::condition_variable Cv; |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 181 | static bool Ready; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 182 | |
Dynamic Tools Team | d29271f | 2019-10-31 10:31:49 -0700 | [diff] [blame] | 183 | static void performAllocations(LargeAllocator *L) { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 184 | std::vector<void *> V; |
| 185 | const scudo::uptr PageSize = scudo::getPageSizeCached(); |
| 186 | { |
| 187 | std::unique_lock<std::mutex> Lock(Mutex); |
| 188 | while (!Ready) |
| 189 | Cv.wait(Lock); |
| 190 | } |
Dynamic Tools Team | fa69c70 | 2020-02-05 09:58:52 -0800 | [diff] [blame] | 191 | for (scudo::uptr I = 0; I < 128U; I++) { |
| 192 | // Deallocate 75% of the blocks. |
| 193 | const bool Deallocate = (rand() & 3) != 0; |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 194 | void *P = L->allocate(scudo::Options{}, (std::rand() % 16) * PageSize); |
Dynamic Tools Team | fa69c70 | 2020-02-05 09:58:52 -0800 | [diff] [blame] | 195 | if (Deallocate) |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 196 | L->deallocate(scudo::Options{}, P); |
Dynamic Tools Team | fa69c70 | 2020-02-05 09:58:52 -0800 | [diff] [blame] | 197 | else |
| 198 | V.push_back(P); |
| 199 | } |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 200 | while (!V.empty()) { |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 201 | L->deallocate(scudo::Options{}, V.back()); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 202 | V.pop_back(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | TEST(ScudoSecondaryTest, SecondaryThreadsRace) { |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 207 | Ready = false; |
| 208 | std::unique_ptr<LargeAllocator> L(new LargeAllocator); |
Dynamic Tools Team | fa69c70 | 2020-02-05 09:58:52 -0800 | [diff] [blame] | 209 | L->init(nullptr, /*ReleaseToOsInterval=*/0); |
| 210 | std::thread Threads[16]; |
| 211 | for (scudo::uptr I = 0; I < ARRAY_SIZE(Threads); I++) |
Kostya Kortchinsky | c72ca56 | 2020-07-27 09:13:42 -0700 | [diff] [blame] | 212 | Threads[I] = std::thread(performAllocations, L.get()); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 213 | { |
| 214 | std::unique_lock<std::mutex> Lock(Mutex); |
| 215 | Ready = true; |
| 216 | Cv.notify_all(); |
| 217 | } |
| 218 | for (auto &T : Threads) |
| 219 | T.join(); |
Kostya Kortchinsky | 53aea5c | 2021-06-03 12:11:05 -0700 | [diff] [blame^] | 220 | scudo::ScopedString Str; |
Dynamic Tools Team | 3e8c65b | 2019-10-18 20:00:32 +0000 | [diff] [blame] | 221 | L->getStats(&Str); |
| 222 | Str.output(); |
Mitch Phillips | da86a54 | 2021-05-24 16:08:57 -0700 | [diff] [blame] | 223 | L->unmapTestOnly(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 224 | } |