1 //===-- combined_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 "combined.h" 13 14 #include <condition_variable> 15 #include <memory> 16 #include <mutex> 17 #include <set> 18 #include <thread> 19 #include <vector> 20 21 static std::mutex Mutex; 22 static std::condition_variable Cv; 23 static bool Ready; 24 25 static constexpr scudo::Chunk::Origin Origin = scudo::Chunk::Origin::Malloc; 26 27 // Fuchsia complains that the function is not used. 28 UNUSED static void disableDebuggerdMaybe() { 29 #if SCUDO_ANDROID 30 // Disable the debuggerd signal handler on Android, without this we can end 31 // up spending a significant amount of time creating tombstones. 32 signal(SIGSEGV, SIG_DFL); 33 #endif 34 } 35 36 template <class AllocatorT> 37 bool isPrimaryAllocation(scudo::uptr Size, scudo::uptr Alignment) { 38 const scudo::uptr MinAlignment = 1UL << SCUDO_MIN_ALIGNMENT_LOG; 39 if (Alignment < MinAlignment) 40 Alignment = MinAlignment; 41 const scudo::uptr NeededSize = 42 scudo::roundUpTo(Size, MinAlignment) + 43 ((Alignment > MinAlignment) ? Alignment : scudo::Chunk::getHeaderSize()); 44 return AllocatorT::PrimaryT::canAllocate(NeededSize); 45 } 46 47 template <class AllocatorT> 48 bool isTaggedAllocation(AllocatorT *Allocator, scudo::uptr Size, 49 scudo::uptr Alignment) { 50 return Allocator->useMemoryTaggingTestOnly() && 51 scudo::systemDetectsMemoryTagFaultsTestOnly() && 52 isPrimaryAllocation<AllocatorT>(Size, Alignment); 53 } 54 55 template <class AllocatorT> 56 void checkMemoryTaggingMaybe(AllocatorT *Allocator, void *P, scudo::uptr Size, 57 scudo::uptr Alignment) { 58 if (!isTaggedAllocation(Allocator, Size, Alignment)) 59 return; 60 61 Size = scudo::roundUpTo(Size, scudo::archMemoryTagGranuleSize()); 62 EXPECT_DEATH( 63 { 64 disableDebuggerdMaybe(); 65 reinterpret_cast<char *>(P)[-1] = 0xaa; 66 }, 67 ""); 68 EXPECT_DEATH( 69 { 70 disableDebuggerdMaybe(); 71 reinterpret_cast<char *>(P)[Size] = 0xaa; 72 }, 73 ""); 74 } 75 76 template <typename Config> struct TestAllocator : scudo::Allocator<Config> { 77 TestAllocator() { 78 this->reset(); 79 this->initThreadMaybe(); 80 } 81 ~TestAllocator() { this->unmapTestOnly(); } 82 }; 83 84 template <class Config> static void testAllocator() { 85 using AllocatorT = TestAllocator<Config>; 86 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 87 88 static scudo::u8 StaticBuffer[scudo::Chunk::getHeaderSize() + 1]; 89 EXPECT_FALSE( 90 Allocator->isOwned(&StaticBuffer[scudo::Chunk::getHeaderSize()])); 91 92 scudo::u8 StackBuffer[scudo::Chunk::getHeaderSize() + 1]; 93 for (scudo::uptr I = 0; I < sizeof(StackBuffer); I++) 94 StackBuffer[I] = 0x42U; 95 EXPECT_FALSE(Allocator->isOwned(&StackBuffer[scudo::Chunk::getHeaderSize()])); 96 for (scudo::uptr I = 0; I < sizeof(StackBuffer); I++) 97 EXPECT_EQ(StackBuffer[I], 0x42U); 98 99 constexpr scudo::uptr MinAlignLog = FIRST_32_SECOND_64(3U, 4U); 100 101 // This allocates and deallocates a bunch of chunks, with a wide range of 102 // sizes and alignments, with a focus on sizes that could trigger weird 103 // behaviors (plus or minus a small delta of a power of two for example). 104 for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) { 105 for (scudo::uptr AlignLog = MinAlignLog; AlignLog <= 16U; AlignLog++) { 106 const scudo::uptr Align = 1U << AlignLog; 107 for (scudo::sptr Delta = -32; Delta <= 32; Delta++) { 108 if (static_cast<scudo::sptr>(1U << SizeLog) + Delta <= 0) 109 continue; 110 const scudo::uptr Size = (1U << SizeLog) + Delta; 111 void *P = Allocator->allocate(Size, Origin, Align); 112 EXPECT_NE(P, nullptr); 113 EXPECT_TRUE(Allocator->isOwned(P)); 114 EXPECT_TRUE(scudo::isAligned(reinterpret_cast<scudo::uptr>(P), Align)); 115 EXPECT_LE(Size, Allocator->getUsableSize(P)); 116 memset(P, 0xaa, Size); 117 checkMemoryTaggingMaybe(Allocator.get(), P, Size, Align); 118 Allocator->deallocate(P, Origin, Size); 119 } 120 } 121 } 122 Allocator->releaseToOS(); 123 124 // Ensure that specifying ZeroContents returns a zero'd out block. 125 for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) { 126 for (scudo::uptr Delta = 0U; Delta <= 4U; Delta++) { 127 const scudo::uptr Size = (1U << SizeLog) + Delta * 128U; 128 void *P = Allocator->allocate(Size, Origin, 1U << MinAlignLog, true); 129 EXPECT_NE(P, nullptr); 130 for (scudo::uptr I = 0; I < Size; I++) 131 ASSERT_EQ((reinterpret_cast<char *>(P))[I], 0); 132 memset(P, 0xaa, Size); 133 Allocator->deallocate(P, Origin, Size); 134 } 135 } 136 Allocator->releaseToOS(); 137 138 // Ensure that specifying ZeroContents returns a zero'd out block. 139 Allocator->setFillContents(scudo::ZeroFill); 140 for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) { 141 for (scudo::uptr Delta = 0U; Delta <= 4U; Delta++) { 142 const scudo::uptr Size = (1U << SizeLog) + Delta * 128U; 143 void *P = Allocator->allocate(Size, Origin, 1U << MinAlignLog, false); 144 EXPECT_NE(P, nullptr); 145 for (scudo::uptr I = 0; I < Size; I++) 146 ASSERT_EQ((reinterpret_cast<char *>(P))[I], 0); 147 memset(P, 0xaa, Size); 148 Allocator->deallocate(P, Origin, Size); 149 } 150 } 151 Allocator->releaseToOS(); 152 153 // Ensure that specifying PatternOrZeroFill returns a pattern or zero filled 154 // block. The primary allocator only produces pattern filled blocks if MTE 155 // is disabled, so we only require pattern filled blocks in that case. 156 Allocator->setFillContents(scudo::PatternOrZeroFill); 157 for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) { 158 for (scudo::uptr Delta = 0U; Delta <= 4U; Delta++) { 159 const scudo::uptr Size = (1U << SizeLog) + Delta * 128U; 160 void *P = Allocator->allocate(Size, Origin, 1U << MinAlignLog, false); 161 EXPECT_NE(P, nullptr); 162 for (scudo::uptr I = 0; I < Size; I++) { 163 unsigned char V = (reinterpret_cast<unsigned char *>(P))[I]; 164 if (isPrimaryAllocation<AllocatorT>(Size, 1U << MinAlignLog) && 165 !Allocator->useMemoryTaggingTestOnly()) 166 ASSERT_EQ(V, scudo::PatternFillByte); 167 else 168 ASSERT_TRUE(V == scudo::PatternFillByte || V == 0); 169 } 170 memset(P, 0xaa, Size); 171 Allocator->deallocate(P, Origin, Size); 172 } 173 } 174 Allocator->releaseToOS(); 175 176 // Verify that a chunk will end up being reused, at some point. 177 const scudo::uptr NeedleSize = 1024U; 178 void *NeedleP = Allocator->allocate(NeedleSize, Origin); 179 Allocator->deallocate(NeedleP, Origin); 180 bool Found = false; 181 for (scudo::uptr I = 0; I < 1024U && !Found; I++) { 182 void *P = Allocator->allocate(NeedleSize, Origin); 183 if (Allocator->untagPointerMaybe(P) == 184 Allocator->untagPointerMaybe(NeedleP)) 185 Found = true; 186 Allocator->deallocate(P, Origin); 187 } 188 EXPECT_TRUE(Found); 189 190 constexpr scudo::uptr MaxSize = Config::Primary::SizeClassMap::MaxSize; 191 192 // Reallocate a large chunk all the way down to a byte, verifying that we 193 // preserve the data in the process. 194 scudo::uptr Size = MaxSize * 2; 195 const scudo::uptr DataSize = 2048U; 196 void *P = Allocator->allocate(Size, Origin); 197 const char Marker = 0xab; 198 memset(P, Marker, scudo::Min(Size, DataSize)); 199 while (Size > 1U) { 200 Size /= 2U; 201 void *NewP = Allocator->reallocate(P, Size); 202 EXPECT_NE(NewP, nullptr); 203 for (scudo::uptr J = 0; J < scudo::Min(Size, DataSize); J++) 204 EXPECT_EQ((reinterpret_cast<char *>(NewP))[J], Marker); 205 P = NewP; 206 } 207 Allocator->deallocate(P, Origin); 208 209 // Check that reallocating a chunk to a slightly smaller or larger size 210 // returns the same chunk. This requires that all the sizes we iterate on use 211 // the same block size, but that should be the case for MaxSize - 64 with our 212 // default class size maps. 213 constexpr scudo::uptr ReallocSize = MaxSize - 64; 214 P = Allocator->allocate(ReallocSize, Origin); 215 memset(P, Marker, ReallocSize); 216 for (scudo::sptr Delta = -32; Delta < 32; Delta += 8) { 217 const scudo::uptr NewSize = ReallocSize + Delta; 218 void *NewP = Allocator->reallocate(P, NewSize); 219 EXPECT_EQ(NewP, P); 220 for (scudo::uptr I = 0; I < ReallocSize - 32; I++) 221 EXPECT_EQ((reinterpret_cast<char *>(NewP))[I], Marker); 222 checkMemoryTaggingMaybe(Allocator.get(), NewP, NewSize, 0); 223 } 224 Allocator->deallocate(P, Origin); 225 226 // Allocates a bunch of chunks, then iterate over all the chunks, ensuring 227 // they are the ones we allocated. This requires the allocator to not have any 228 // other allocated chunk at this point (eg: won't work with the Quarantine). 229 if (!UseQuarantine) { 230 std::vector<void *> V; 231 for (scudo::uptr I = 0; I < 64U; I++) 232 V.push_back(Allocator->allocate(rand() % (MaxSize / 2U), Origin)); 233 Allocator->disable(); 234 Allocator->iterateOverChunks( 235 0U, static_cast<scudo::uptr>(SCUDO_MMAP_RANGE_SIZE - 1), 236 [](uintptr_t Base, size_t Size, void *Arg) { 237 std::vector<void *> *V = reinterpret_cast<std::vector<void *> *>(Arg); 238 void *P = reinterpret_cast<void *>(Base); 239 EXPECT_NE(std::find(V->begin(), V->end(), P), V->end()); 240 }, 241 reinterpret_cast<void *>(&V)); 242 Allocator->enable(); 243 while (!V.empty()) { 244 Allocator->deallocate(V.back(), Origin); 245 V.pop_back(); 246 } 247 } 248 249 Allocator->releaseToOS(); 250 251 if (Allocator->useMemoryTaggingTestOnly() && 252 scudo::systemDetectsMemoryTagFaultsTestOnly()) { 253 // Check that use-after-free is detected. 254 for (scudo::uptr SizeLog = 0U; SizeLog <= 20U; SizeLog++) { 255 const scudo::uptr Size = 1U << SizeLog; 256 if (!isTaggedAllocation(Allocator.get(), Size, 1)) 257 continue; 258 // UAF detection is probabilistic, so we repeat the test up to 256 times 259 // if necessary. With 15 possible tags this means a 1 in 15^256 chance of 260 // a false positive. 261 EXPECT_DEATH( 262 { 263 disableDebuggerdMaybe(); 264 for (unsigned I = 0; I != 256; ++I) { 265 void *P = Allocator->allocate(Size, Origin); 266 Allocator->deallocate(P, Origin); 267 reinterpret_cast<char *>(P)[0] = 0xaa; 268 } 269 }, 270 ""); 271 EXPECT_DEATH( 272 { 273 disableDebuggerdMaybe(); 274 for (unsigned I = 0; I != 256; ++I) { 275 void *P = Allocator->allocate(Size, Origin); 276 Allocator->deallocate(P, Origin); 277 reinterpret_cast<char *>(P)[Size - 1] = 0xaa; 278 } 279 }, 280 ""); 281 } 282 283 // Check that disabling memory tagging works correctly. 284 void *P = Allocator->allocate(2048, Origin); 285 EXPECT_DEATH(reinterpret_cast<char *>(P)[2048] = 0xaa, ""); 286 scudo::disableMemoryTagChecksTestOnly(); 287 Allocator->disableMemoryTagging(); 288 reinterpret_cast<char *>(P)[2048] = 0xaa; 289 Allocator->deallocate(P, Origin); 290 291 P = Allocator->allocate(2048, Origin); 292 EXPECT_EQ(Allocator->untagPointerMaybe(P), P); 293 reinterpret_cast<char *>(P)[2048] = 0xaa; 294 Allocator->deallocate(P, Origin); 295 296 Allocator->releaseToOS(); 297 298 // Disabling memory tag checks may interfere with subsequent tests. 299 // Re-enable them now. 300 scudo::enableMemoryTagChecksTestOnly(); 301 } 302 303 scudo::uptr BufferSize = 8192; 304 std::vector<char> Buffer(BufferSize); 305 scudo::uptr ActualSize = Allocator->getStats(Buffer.data(), BufferSize); 306 while (ActualSize > BufferSize) { 307 BufferSize = ActualSize + 1024; 308 Buffer.resize(BufferSize); 309 ActualSize = Allocator->getStats(Buffer.data(), BufferSize); 310 } 311 std::string Stats(Buffer.begin(), Buffer.end()); 312 // Basic checks on the contents of the statistics output, which also allows us 313 // to verify that we got it all. 314 EXPECT_NE(Stats.find("Stats: SizeClassAllocator"), std::string::npos); 315 EXPECT_NE(Stats.find("Stats: MapAllocator"), std::string::npos); 316 EXPECT_NE(Stats.find("Stats: Quarantine"), std::string::npos); 317 } 318 319 // Test that multiple instantiations of the allocator have not messed up the 320 // process's signal handlers (GWP-ASan used to do this). 321 void testSEGV() { 322 const scudo::uptr Size = 4 * scudo::getPageSizeCached(); 323 scudo::MapPlatformData Data = {}; 324 void *P = scudo::map(nullptr, Size, "testSEGV", MAP_NOACCESS, &Data); 325 EXPECT_NE(P, nullptr); 326 EXPECT_DEATH(memset(P, 0xaa, Size), ""); 327 scudo::unmap(P, Size, UNMAP_ALL, &Data); 328 } 329 330 TEST(ScudoCombinedTest, BasicCombined) { 331 UseQuarantine = false; 332 testAllocator<scudo::AndroidSvelteConfig>(); 333 #if SCUDO_FUCHSIA 334 testAllocator<scudo::FuchsiaConfig>(); 335 #else 336 testAllocator<scudo::DefaultConfig>(); 337 UseQuarantine = true; 338 testAllocator<scudo::AndroidConfig>(); 339 testSEGV(); 340 #endif 341 } 342 343 template <typename AllocatorT> static void stressAllocator(AllocatorT *A) { 344 { 345 std::unique_lock<std::mutex> Lock(Mutex); 346 while (!Ready) 347 Cv.wait(Lock); 348 } 349 std::vector<std::pair<void *, scudo::uptr>> V; 350 for (scudo::uptr I = 0; I < 256U; I++) { 351 const scudo::uptr Size = std::rand() % 4096U; 352 void *P = A->allocate(Size, Origin); 353 // A region could have ran out of memory, resulting in a null P. 354 if (P) 355 V.push_back(std::make_pair(P, Size)); 356 } 357 while (!V.empty()) { 358 auto Pair = V.back(); 359 A->deallocate(Pair.first, Origin, Pair.second); 360 V.pop_back(); 361 } 362 } 363 364 template <class Config> static void testAllocatorThreaded() { 365 Ready = false; 366 using AllocatorT = TestAllocator<Config>; 367 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 368 std::thread Threads[32]; 369 for (scudo::uptr I = 0; I < ARRAY_SIZE(Threads); I++) 370 Threads[I] = std::thread(stressAllocator<AllocatorT>, Allocator.get()); 371 { 372 std::unique_lock<std::mutex> Lock(Mutex); 373 Ready = true; 374 Cv.notify_all(); 375 } 376 for (auto &T : Threads) 377 T.join(); 378 Allocator->releaseToOS(); 379 } 380 381 TEST(ScudoCombinedTest, ThreadedCombined) { 382 UseQuarantine = false; 383 testAllocatorThreaded<scudo::AndroidSvelteConfig>(); 384 #if SCUDO_FUCHSIA 385 testAllocatorThreaded<scudo::FuchsiaConfig>(); 386 #else 387 testAllocatorThreaded<scudo::DefaultConfig>(); 388 UseQuarantine = true; 389 testAllocatorThreaded<scudo::AndroidConfig>(); 390 #endif 391 } 392 393 struct DeathSizeClassConfig { 394 static const scudo::uptr NumBits = 1; 395 static const scudo::uptr MinSizeLog = 10; 396 static const scudo::uptr MidSizeLog = 10; 397 static const scudo::uptr MaxSizeLog = 13; 398 static const scudo::u32 MaxNumCachedHint = 4; 399 static const scudo::uptr MaxBytesCachedLog = 12; 400 }; 401 402 static const scudo::uptr DeathRegionSizeLog = 20U; 403 struct DeathConfig { 404 static const bool MaySupportMemoryTagging = false; 405 406 // Tiny allocator, its Primary only serves chunks of four sizes. 407 using SizeClassMap = scudo::FixedSizeClassMap<DeathSizeClassConfig>; 408 typedef scudo::SizeClassAllocator64<DeathConfig> Primary; 409 static const scudo::uptr PrimaryRegionSizeLog = DeathRegionSizeLog; 410 static const scudo::s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN; 411 static const scudo::s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX; 412 413 typedef scudo::MapAllocatorNoCache SecondaryCache; 414 template <class A> using TSDRegistryT = scudo::TSDRegistrySharedT<A, 1U, 1U>; 415 }; 416 417 TEST(ScudoCombinedTest, DeathCombined) { 418 using AllocatorT = TestAllocator<DeathConfig>; 419 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 420 421 const scudo::uptr Size = 1000U; 422 void *P = Allocator->allocate(Size, Origin); 423 EXPECT_NE(P, nullptr); 424 425 // Invalid sized deallocation. 426 EXPECT_DEATH(Allocator->deallocate(P, Origin, Size + 8U), ""); 427 428 // Misaligned pointer. Potentially unused if EXPECT_DEATH isn't available. 429 UNUSED void *MisalignedP = 430 reinterpret_cast<void *>(reinterpret_cast<scudo::uptr>(P) | 1U); 431 EXPECT_DEATH(Allocator->deallocate(MisalignedP, Origin, Size), ""); 432 EXPECT_DEATH(Allocator->reallocate(MisalignedP, Size * 2U), ""); 433 434 // Header corruption. 435 scudo::u64 *H = 436 reinterpret_cast<scudo::u64 *>(scudo::Chunk::getAtomicHeader(P)); 437 *H ^= 0x42U; 438 EXPECT_DEATH(Allocator->deallocate(P, Origin, Size), ""); 439 *H ^= 0x420042U; 440 EXPECT_DEATH(Allocator->deallocate(P, Origin, Size), ""); 441 *H ^= 0x420000U; 442 443 // Invalid chunk state. 444 Allocator->deallocate(P, Origin, Size); 445 EXPECT_DEATH(Allocator->deallocate(P, Origin, Size), ""); 446 EXPECT_DEATH(Allocator->reallocate(P, Size * 2U), ""); 447 EXPECT_DEATH(Allocator->getUsableSize(P), ""); 448 } 449 450 // Ensure that releaseToOS can be called prior to any other allocator 451 // operation without issue. 452 TEST(ScudoCombinedTest, ReleaseToOS) { 453 using AllocatorT = TestAllocator<DeathConfig>; 454 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 455 456 Allocator->releaseToOS(); 457 } 458 459 // Verify that when a region gets full, the allocator will still manage to 460 // fulfill the allocation through a larger size class. 461 TEST(ScudoCombinedTest, FullRegion) { 462 using AllocatorT = TestAllocator<DeathConfig>; 463 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 464 465 std::vector<void *> V; 466 scudo::uptr FailedAllocationsCount = 0; 467 for (scudo::uptr ClassId = 1U; 468 ClassId <= DeathConfig::SizeClassMap::LargestClassId; ClassId++) { 469 const scudo::uptr Size = 470 DeathConfig::SizeClassMap::getSizeByClassId(ClassId); 471 // Allocate enough to fill all of the regions above this one. 472 const scudo::uptr MaxNumberOfChunks = 473 ((1U << DeathRegionSizeLog) / Size) * 474 (DeathConfig::SizeClassMap::LargestClassId - ClassId + 1); 475 void *P; 476 for (scudo::uptr I = 0; I <= MaxNumberOfChunks; I++) { 477 P = Allocator->allocate(Size - 64U, Origin); 478 if (!P) 479 FailedAllocationsCount++; 480 else 481 V.push_back(P); 482 } 483 while (!V.empty()) { 484 Allocator->deallocate(V.back(), Origin); 485 V.pop_back(); 486 } 487 } 488 EXPECT_EQ(FailedAllocationsCount, 0U); 489 } 490 491 TEST(ScudoCombinedTest, OddEven) { 492 using AllocatorT = TestAllocator<scudo::AndroidConfig>; 493 using SizeClassMap = AllocatorT::PrimaryT::SizeClassMap; 494 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 495 496 if (!Allocator->useMemoryTaggingTestOnly()) 497 return; 498 499 auto CheckOddEven = [](scudo::uptr P1, scudo::uptr P2) { 500 scudo::uptr Tag1 = scudo::extractTag(scudo::loadTag(P1)); 501 scudo::uptr Tag2 = scudo::extractTag(scudo::loadTag(P2)); 502 EXPECT_NE(Tag1 % 2, Tag2 % 2); 503 }; 504 505 for (scudo::uptr ClassId = 1U; ClassId <= SizeClassMap::LargestClassId; 506 ClassId++) { 507 const scudo::uptr Size = SizeClassMap::getSizeByClassId(ClassId); 508 509 std::set<scudo::uptr> Ptrs; 510 bool Found = false; 511 for (unsigned I = 0; I != 65536; ++I) { 512 scudo::uptr P = scudo::untagPointer(reinterpret_cast<scudo::uptr>( 513 Allocator->allocate(Size - scudo::Chunk::getHeaderSize(), Origin))); 514 if (Ptrs.count(P - Size)) { 515 Found = true; 516 CheckOddEven(P, P - Size); 517 break; 518 } 519 if (Ptrs.count(P + Size)) { 520 Found = true; 521 CheckOddEven(P, P + Size); 522 break; 523 } 524 Ptrs.insert(P); 525 } 526 EXPECT_TRUE(Found); 527 } 528 } 529 530 TEST(ScudoCombinedTest, DisableMemInit) { 531 using AllocatorT = TestAllocator<scudo::AndroidConfig>; 532 using SizeClassMap = AllocatorT::PrimaryT::SizeClassMap; 533 auto Allocator = std::unique_ptr<AllocatorT>(new AllocatorT()); 534 535 std::vector<void *> Ptrs(65536, nullptr); 536 537 Allocator->setOption(scudo::Option::ThreadDisableMemInit, 1); 538 539 constexpr scudo::uptr MinAlignLog = FIRST_32_SECOND_64(3U, 4U); 540 541 // Test that if mem-init is disabled on a thread, calloc should still work as 542 // expected. This is tricky to ensure when MTE is enabled, so this test tries 543 // to exercise the relevant code on our MTE path. 544 for (scudo::uptr ClassId = 1U; ClassId <= 8; ClassId++) { 545 const scudo::uptr Size = 546 SizeClassMap::getSizeByClassId(ClassId) - scudo::Chunk::getHeaderSize(); 547 if (Size < 8) 548 continue; 549 for (unsigned I = 0; I != Ptrs.size(); ++I) { 550 Ptrs[I] = Allocator->allocate(Size, Origin); 551 memset(Ptrs[I], 0xaa, Size); 552 } 553 for (unsigned I = 0; I != Ptrs.size(); ++I) 554 Allocator->deallocate(Ptrs[I], Origin, Size); 555 for (unsigned I = 0; I != Ptrs.size(); ++I) { 556 Ptrs[I] = Allocator->allocate(Size - 8, Origin); 557 memset(Ptrs[I], 0xbb, Size - 8); 558 } 559 for (unsigned I = 0; I != Ptrs.size(); ++I) 560 Allocator->deallocate(Ptrs[I], Origin, Size - 8); 561 for (unsigned I = 0; I != Ptrs.size(); ++I) { 562 Ptrs[I] = Allocator->allocate(Size, Origin, 1U << MinAlignLog, true); 563 for (scudo::uptr J = 0; J < Size; ++J) 564 ASSERT_EQ((reinterpret_cast<char *>(Ptrs[I]))[J], 0); 565 } 566 } 567 568 Allocator->setOption(scudo::Option::ThreadDisableMemInit, 0); 569 } 570