1 //===-- scudo_allocator.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 /// Scudo Hardened Allocator implementation. 10 /// It uses the sanitizer_common allocator as a base and aims at mitigating 11 /// heap corruption vulnerabilities. It provides a checksum-guarded chunk 12 /// header, a delayed free list, and additional sanity checks. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "scudo_allocator.h" 17 #include "scudo_crc32.h" 18 #include "scudo_errors.h" 19 #include "scudo_flags.h" 20 #include "scudo_interface_internal.h" 21 #include "scudo_tsd.h" 22 #include "scudo_utils.h" 23 24 #include "sanitizer_common/sanitizer_allocator_checks.h" 25 #include "sanitizer_common/sanitizer_allocator_interface.h" 26 #include "sanitizer_common/sanitizer_quarantine.h" 27 28 #ifdef GWP_ASAN_HOOKS 29 # include "gwp_asan/guarded_pool_allocator.h" 30 # include "gwp_asan/optional/backtrace.h" 31 # include "gwp_asan/optional/options_parser.h" 32 #include "gwp_asan/optional/segv_handler.h" 33 #endif // GWP_ASAN_HOOKS 34 35 #include <errno.h> 36 #include <string.h> 37 38 namespace __scudo { 39 40 // Global static cookie, initialized at start-up. 41 static u32 Cookie; 42 43 // We default to software CRC32 if the alternatives are not supported, either 44 // at compilation or at runtime. 45 static atomic_uint8_t HashAlgorithm = { CRC32Software }; 46 47 inline u32 computeCRC32(u32 Crc, uptr Value, uptr *Array, uptr ArraySize) { 48 // If the hardware CRC32 feature is defined here, it was enabled everywhere, 49 // as opposed to only for scudo_crc32.cpp. This means that other hardware 50 // specific instructions were likely emitted at other places, and as a 51 // result there is no reason to not use it here. 52 #if defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) 53 Crc = CRC32_INTRINSIC(Crc, Value); 54 for (uptr i = 0; i < ArraySize; i++) 55 Crc = CRC32_INTRINSIC(Crc, Array[i]); 56 return Crc; 57 #else 58 if (atomic_load_relaxed(&HashAlgorithm) == CRC32Hardware) { 59 Crc = computeHardwareCRC32(Crc, Value); 60 for (uptr i = 0; i < ArraySize; i++) 61 Crc = computeHardwareCRC32(Crc, Array[i]); 62 return Crc; 63 } 64 Crc = computeSoftwareCRC32(Crc, Value); 65 for (uptr i = 0; i < ArraySize; i++) 66 Crc = computeSoftwareCRC32(Crc, Array[i]); 67 return Crc; 68 #endif // defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) 69 } 70 71 static BackendT &getBackend(); 72 73 namespace Chunk { 74 static inline AtomicPackedHeader *getAtomicHeader(void *Ptr) { 75 return reinterpret_cast<AtomicPackedHeader *>(reinterpret_cast<uptr>(Ptr) - 76 getHeaderSize()); 77 } 78 static inline 79 const AtomicPackedHeader *getConstAtomicHeader(const void *Ptr) { 80 return reinterpret_cast<const AtomicPackedHeader *>( 81 reinterpret_cast<uptr>(Ptr) - getHeaderSize()); 82 } 83 84 static inline bool isAligned(const void *Ptr) { 85 return IsAligned(reinterpret_cast<uptr>(Ptr), MinAlignment); 86 } 87 88 // We can't use the offset member of the chunk itself, as we would double 89 // fetch it without any warranty that it wouldn't have been tampered. To 90 // prevent this, we work with a local copy of the header. 91 static inline void *getBackendPtr(const void *Ptr, UnpackedHeader *Header) { 92 return reinterpret_cast<void *>(reinterpret_cast<uptr>(Ptr) - 93 getHeaderSize() - (Header->Offset << MinAlignmentLog)); 94 } 95 96 // Returns the usable size for a chunk, meaning the amount of bytes from the 97 // beginning of the user data to the end of the backend allocated chunk. 98 static inline uptr getUsableSize(const void *Ptr, UnpackedHeader *Header) { 99 const uptr ClassId = Header->ClassId; 100 if (ClassId) 101 return PrimaryT::ClassIdToSize(ClassId) - getHeaderSize() - 102 (Header->Offset << MinAlignmentLog); 103 return SecondaryT::GetActuallyAllocatedSize( 104 getBackendPtr(Ptr, Header)) - getHeaderSize(); 105 } 106 107 // Returns the size the user requested when allocating the chunk. 108 static inline uptr getSize(const void *Ptr, UnpackedHeader *Header) { 109 const uptr SizeOrUnusedBytes = Header->SizeOrUnusedBytes; 110 if (Header->ClassId) 111 return SizeOrUnusedBytes; 112 return SecondaryT::GetActuallyAllocatedSize( 113 getBackendPtr(Ptr, Header)) - getHeaderSize() - SizeOrUnusedBytes; 114 } 115 116 // Compute the checksum of the chunk pointer and its header. 117 static inline u16 computeChecksum(const void *Ptr, UnpackedHeader *Header) { 118 UnpackedHeader ZeroChecksumHeader = *Header; 119 ZeroChecksumHeader.Checksum = 0; 120 uptr HeaderHolder[sizeof(UnpackedHeader) / sizeof(uptr)]; 121 memcpy(&HeaderHolder, &ZeroChecksumHeader, sizeof(HeaderHolder)); 122 const u32 Crc = computeCRC32(Cookie, reinterpret_cast<uptr>(Ptr), 123 HeaderHolder, ARRAY_SIZE(HeaderHolder)); 124 return static_cast<u16>(Crc); 125 } 126 127 // Checks the validity of a chunk by verifying its checksum. It doesn't 128 // incur termination in the event of an invalid chunk. 129 static inline bool isValid(const void *Ptr) { 130 PackedHeader NewPackedHeader = 131 atomic_load_relaxed(getConstAtomicHeader(Ptr)); 132 UnpackedHeader NewUnpackedHeader = 133 bit_cast<UnpackedHeader>(NewPackedHeader); 134 return (NewUnpackedHeader.Checksum == 135 computeChecksum(Ptr, &NewUnpackedHeader)); 136 } 137 138 // Ensure that ChunkAvailable is 0, so that if a 0 checksum is ever valid 139 // for a fully nulled out header, its state will be available anyway. 140 COMPILER_CHECK(ChunkAvailable == 0); 141 142 // Loads and unpacks the header, verifying the checksum in the process. 143 static inline 144 void loadHeader(const void *Ptr, UnpackedHeader *NewUnpackedHeader) { 145 PackedHeader NewPackedHeader = 146 atomic_load_relaxed(getConstAtomicHeader(Ptr)); 147 *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader); 148 if (UNLIKELY(NewUnpackedHeader->Checksum != 149 computeChecksum(Ptr, NewUnpackedHeader))) 150 dieWithMessage("corrupted chunk header at address %p\n", Ptr); 151 } 152 153 // Packs and stores the header, computing the checksum in the process. 154 static inline void storeHeader(void *Ptr, UnpackedHeader *NewUnpackedHeader) { 155 NewUnpackedHeader->Checksum = computeChecksum(Ptr, NewUnpackedHeader); 156 PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); 157 atomic_store_relaxed(getAtomicHeader(Ptr), NewPackedHeader); 158 } 159 160 // Packs and stores the header, computing the checksum in the process. We 161 // compare the current header with the expected provided one to ensure that 162 // we are not being raced by a corruption occurring in another thread. 163 static inline void compareExchangeHeader(void *Ptr, 164 UnpackedHeader *NewUnpackedHeader, 165 UnpackedHeader *OldUnpackedHeader) { 166 NewUnpackedHeader->Checksum = computeChecksum(Ptr, NewUnpackedHeader); 167 PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); 168 PackedHeader OldPackedHeader = bit_cast<PackedHeader>(*OldUnpackedHeader); 169 if (UNLIKELY(!atomic_compare_exchange_strong( 170 getAtomicHeader(Ptr), &OldPackedHeader, NewPackedHeader, 171 memory_order_relaxed))) 172 dieWithMessage("race on chunk header at address %p\n", Ptr); 173 } 174 } // namespace Chunk 175 176 struct QuarantineCallback { 177 explicit QuarantineCallback(AllocatorCacheT *Cache) 178 : Cache_(Cache) {} 179 180 // Chunk recycling function, returns a quarantined chunk to the backend, 181 // first making sure it hasn't been tampered with. 182 void Recycle(void *Ptr) { 183 UnpackedHeader Header; 184 Chunk::loadHeader(Ptr, &Header); 185 if (UNLIKELY(Header.State != ChunkQuarantine)) 186 dieWithMessage("invalid chunk state when recycling address %p\n", Ptr); 187 UnpackedHeader NewHeader = Header; 188 NewHeader.State = ChunkAvailable; 189 Chunk::compareExchangeHeader(Ptr, &NewHeader, &Header); 190 void *BackendPtr = Chunk::getBackendPtr(Ptr, &Header); 191 if (Header.ClassId) 192 getBackend().deallocatePrimary(Cache_, BackendPtr, Header.ClassId); 193 else 194 getBackend().deallocateSecondary(BackendPtr); 195 } 196 197 // Internal quarantine allocation and deallocation functions. We first check 198 // that the batches are indeed serviced by the Primary. 199 // TODO(kostyak): figure out the best way to protect the batches. 200 void *Allocate(uptr Size) { 201 const uptr BatchClassId = SizeClassMap::ClassID(sizeof(QuarantineBatch)); 202 return getBackend().allocatePrimary(Cache_, BatchClassId); 203 } 204 205 void Deallocate(void *Ptr) { 206 const uptr BatchClassId = SizeClassMap::ClassID(sizeof(QuarantineBatch)); 207 getBackend().deallocatePrimary(Cache_, Ptr, BatchClassId); 208 } 209 210 AllocatorCacheT *Cache_; 211 COMPILER_CHECK(sizeof(QuarantineBatch) < SizeClassMap::kMaxSize); 212 }; 213 214 typedef Quarantine<QuarantineCallback, void> QuarantineT; 215 typedef QuarantineT::Cache QuarantineCacheT; 216 COMPILER_CHECK(sizeof(QuarantineCacheT) <= 217 sizeof(ScudoTSD::QuarantineCachePlaceHolder)); 218 219 QuarantineCacheT *getQuarantineCache(ScudoTSD *TSD) { 220 return reinterpret_cast<QuarantineCacheT *>(TSD->QuarantineCachePlaceHolder); 221 } 222 223 #ifdef GWP_ASAN_HOOKS 224 static gwp_asan::GuardedPoolAllocator GuardedAlloc; 225 #endif // GWP_ASAN_HOOKS 226 227 struct Allocator { 228 static const uptr MaxAllowedMallocSize = 229 FIRST_32_SECOND_64(2UL << 30, 1ULL << 40); 230 231 BackendT Backend; 232 QuarantineT Quarantine; 233 234 u32 QuarantineChunksUpToSize; 235 236 bool DeallocationTypeMismatch; 237 bool ZeroContents; 238 bool DeleteSizeMismatch; 239 240 bool CheckRssLimit; 241 uptr HardRssLimitMb; 242 uptr SoftRssLimitMb; 243 atomic_uint8_t RssLimitExceeded; 244 atomic_uint64_t RssLastCheckedAtNS; 245 246 explicit Allocator(LinkerInitialized) 247 : Quarantine(LINKER_INITIALIZED) {} 248 249 NOINLINE void performSanityChecks(); 250 251 void init() { 252 SanitizerToolName = "Scudo"; 253 PrimaryAllocatorName = "ScudoPrimary"; 254 SecondaryAllocatorName = "ScudoSecondary"; 255 256 initFlags(); 257 258 performSanityChecks(); 259 260 // Check if hardware CRC32 is supported in the binary and by the platform, 261 // if so, opt for the CRC32 hardware version of the checksum. 262 if (&computeHardwareCRC32 && hasHardwareCRC32()) 263 atomic_store_relaxed(&HashAlgorithm, CRC32Hardware); 264 265 SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null); 266 Backend.init(common_flags()->allocator_release_to_os_interval_ms); 267 HardRssLimitMb = common_flags()->hard_rss_limit_mb; 268 SoftRssLimitMb = common_flags()->soft_rss_limit_mb; 269 Quarantine.Init( 270 static_cast<uptr>(getFlags()->QuarantineSizeKb) << 10, 271 static_cast<uptr>(getFlags()->ThreadLocalQuarantineSizeKb) << 10); 272 QuarantineChunksUpToSize = (Quarantine.GetCacheSize() == 0) ? 0 : 273 getFlags()->QuarantineChunksUpToSize; 274 DeallocationTypeMismatch = getFlags()->DeallocationTypeMismatch; 275 DeleteSizeMismatch = getFlags()->DeleteSizeMismatch; 276 ZeroContents = getFlags()->ZeroContents; 277 278 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&Cookie), sizeof(Cookie), 279 /*blocking=*/false))) { 280 Cookie = static_cast<u32>((NanoTime() >> 12) ^ 281 (reinterpret_cast<uptr>(this) >> 4)); 282 } 283 284 CheckRssLimit = HardRssLimitMb || SoftRssLimitMb; 285 if (CheckRssLimit) 286 atomic_store_relaxed(&RssLastCheckedAtNS, MonotonicNanoTime()); 287 } 288 289 // Helper function that checks for a valid Scudo chunk. nullptr isn't. 290 bool isValidPointer(const void *Ptr) { 291 initThreadMaybe(); 292 if (UNLIKELY(!Ptr)) 293 return false; 294 if (!Chunk::isAligned(Ptr)) 295 return false; 296 return Chunk::isValid(Ptr); 297 } 298 299 NOINLINE bool isRssLimitExceeded(); 300 301 // Allocates a chunk. 302 void *allocate(uptr Size, uptr Alignment, AllocType Type, 303 bool ForceZeroContents = false) { 304 initThreadMaybe(); 305 306 #ifdef GWP_ASAN_HOOKS 307 if (UNLIKELY(GuardedAlloc.shouldSample())) { 308 if (void *Ptr = GuardedAlloc.allocate(Size)) 309 return Ptr; 310 } 311 #endif // GWP_ASAN_HOOKS 312 313 if (UNLIKELY(Alignment > MaxAlignment)) { 314 if (AllocatorMayReturnNull()) 315 return nullptr; 316 reportAllocationAlignmentTooBig(Alignment, MaxAlignment); 317 } 318 if (UNLIKELY(Alignment < MinAlignment)) 319 Alignment = MinAlignment; 320 321 const uptr NeededSize = RoundUpTo(Size ? Size : 1, MinAlignment) + 322 Chunk::getHeaderSize(); 323 const uptr AlignedSize = (Alignment > MinAlignment) ? 324 NeededSize + (Alignment - Chunk::getHeaderSize()) : NeededSize; 325 if (UNLIKELY(Size >= MaxAllowedMallocSize) || 326 UNLIKELY(AlignedSize >= MaxAllowedMallocSize)) { 327 if (AllocatorMayReturnNull()) 328 return nullptr; 329 reportAllocationSizeTooBig(Size, AlignedSize, MaxAllowedMallocSize); 330 } 331 332 if (CheckRssLimit && UNLIKELY(isRssLimitExceeded())) { 333 if (AllocatorMayReturnNull()) 334 return nullptr; 335 reportRssLimitExceeded(); 336 } 337 338 // Primary and Secondary backed allocations have a different treatment. We 339 // deal with alignment requirements of Primary serviced allocations here, 340 // but the Secondary will take care of its own alignment needs. 341 void *BackendPtr; 342 uptr BackendSize; 343 u8 ClassId; 344 if (PrimaryT::CanAllocate(AlignedSize, MinAlignment)) { 345 BackendSize = AlignedSize; 346 ClassId = SizeClassMap::ClassID(BackendSize); 347 bool UnlockRequired; 348 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired); 349 BackendPtr = Backend.allocatePrimary(&TSD->Cache, ClassId); 350 if (UnlockRequired) 351 TSD->unlock(); 352 } else { 353 BackendSize = NeededSize; 354 ClassId = 0; 355 BackendPtr = Backend.allocateSecondary(BackendSize, Alignment); 356 } 357 if (UNLIKELY(!BackendPtr)) { 358 SetAllocatorOutOfMemory(); 359 if (AllocatorMayReturnNull()) 360 return nullptr; 361 reportOutOfMemory(Size); 362 } 363 364 // If requested, we will zero out the entire contents of the returned chunk. 365 if ((ForceZeroContents || ZeroContents) && ClassId) 366 memset(BackendPtr, 0, PrimaryT::ClassIdToSize(ClassId)); 367 368 UnpackedHeader Header = {}; 369 uptr UserPtr = reinterpret_cast<uptr>(BackendPtr) + Chunk::getHeaderSize(); 370 if (UNLIKELY(!IsAligned(UserPtr, Alignment))) { 371 // Since the Secondary takes care of alignment, a non-aligned pointer 372 // means it is from the Primary. It is also the only case where the offset 373 // field of the header would be non-zero. 374 DCHECK(ClassId); 375 const uptr AlignedUserPtr = RoundUpTo(UserPtr, Alignment); 376 Header.Offset = (AlignedUserPtr - UserPtr) >> MinAlignmentLog; 377 UserPtr = AlignedUserPtr; 378 } 379 DCHECK_LE(UserPtr + Size, reinterpret_cast<uptr>(BackendPtr) + BackendSize); 380 Header.State = ChunkAllocated; 381 Header.AllocType = Type; 382 if (ClassId) { 383 Header.ClassId = ClassId; 384 Header.SizeOrUnusedBytes = Size; 385 } else { 386 // The secondary fits the allocations to a page, so the amount of unused 387 // bytes is the difference between the end of the user allocation and the 388 // next page boundary. 389 const uptr PageSize = GetPageSizeCached(); 390 const uptr TrailingBytes = (UserPtr + Size) & (PageSize - 1); 391 if (TrailingBytes) 392 Header.SizeOrUnusedBytes = PageSize - TrailingBytes; 393 } 394 void *Ptr = reinterpret_cast<void *>(UserPtr); 395 Chunk::storeHeader(Ptr, &Header); 396 if (SCUDO_CAN_USE_HOOKS && &__sanitizer_malloc_hook) 397 __sanitizer_malloc_hook(Ptr, Size); 398 return Ptr; 399 } 400 401 // Place a chunk in the quarantine or directly deallocate it in the event of 402 // a zero-sized quarantine, or if the size of the chunk is greater than the 403 // quarantine chunk size threshold. 404 void quarantineOrDeallocateChunk(void *Ptr, UnpackedHeader *Header, 405 uptr Size) { 406 const bool BypassQuarantine = !Size || (Size > QuarantineChunksUpToSize); 407 if (BypassQuarantine) { 408 UnpackedHeader NewHeader = *Header; 409 NewHeader.State = ChunkAvailable; 410 Chunk::compareExchangeHeader(Ptr, &NewHeader, Header); 411 void *BackendPtr = Chunk::getBackendPtr(Ptr, Header); 412 if (Header->ClassId) { 413 bool UnlockRequired; 414 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired); 415 getBackend().deallocatePrimary(&TSD->Cache, BackendPtr, 416 Header->ClassId); 417 if (UnlockRequired) 418 TSD->unlock(); 419 } else { 420 getBackend().deallocateSecondary(BackendPtr); 421 } 422 } else { 423 // If a small memory amount was allocated with a larger alignment, we want 424 // to take that into account. Otherwise the Quarantine would be filled 425 // with tiny chunks, taking a lot of VA memory. This is an approximation 426 // of the usable size, that allows us to not call 427 // GetActuallyAllocatedSize. 428 const uptr EstimatedSize = Size + (Header->Offset << MinAlignmentLog); 429 UnpackedHeader NewHeader = *Header; 430 NewHeader.State = ChunkQuarantine; 431 Chunk::compareExchangeHeader(Ptr, &NewHeader, Header); 432 bool UnlockRequired; 433 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired); 434 Quarantine.Put(getQuarantineCache(TSD), QuarantineCallback(&TSD->Cache), 435 Ptr, EstimatedSize); 436 if (UnlockRequired) 437 TSD->unlock(); 438 } 439 } 440 441 // Deallocates a Chunk, which means either adding it to the quarantine or 442 // directly returning it to the backend if criteria are met. 443 void deallocate(void *Ptr, uptr DeleteSize, uptr DeleteAlignment, 444 AllocType Type) { 445 // For a deallocation, we only ensure minimal initialization, meaning thread 446 // local data will be left uninitialized for now (when using ELF TLS). The 447 // fallback cache will be used instead. This is a workaround for a situation 448 // where the only heap operation performed in a thread would be a free past 449 // the TLS destructors, ending up in initialized thread specific data never 450 // being destroyed properly. Any other heap operation will do a full init. 451 initThreadMaybe(/*MinimalInit=*/true); 452 if (SCUDO_CAN_USE_HOOKS && &__sanitizer_free_hook) 453 __sanitizer_free_hook(Ptr); 454 if (UNLIKELY(!Ptr)) 455 return; 456 457 #ifdef GWP_ASAN_HOOKS 458 if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) { 459 GuardedAlloc.deallocate(Ptr); 460 return; 461 } 462 #endif // GWP_ASAN_HOOKS 463 464 if (UNLIKELY(!Chunk::isAligned(Ptr))) 465 dieWithMessage("misaligned pointer when deallocating address %p\n", Ptr); 466 UnpackedHeader Header; 467 Chunk::loadHeader(Ptr, &Header); 468 if (UNLIKELY(Header.State != ChunkAllocated)) 469 dieWithMessage("invalid chunk state when deallocating address %p\n", Ptr); 470 if (DeallocationTypeMismatch) { 471 // The deallocation type has to match the allocation one. 472 if (Header.AllocType != Type) { 473 // With the exception of memalign'd Chunks, that can be still be free'd. 474 if (Header.AllocType != FromMemalign || Type != FromMalloc) 475 dieWithMessage("allocation type mismatch when deallocating address " 476 "%p\n", Ptr); 477 } 478 } 479 const uptr Size = Chunk::getSize(Ptr, &Header); 480 if (DeleteSizeMismatch) { 481 if (DeleteSize && DeleteSize != Size) 482 dieWithMessage("invalid sized delete when deallocating address %p\n", 483 Ptr); 484 } 485 (void)DeleteAlignment; // TODO(kostyak): verify that the alignment matches. 486 quarantineOrDeallocateChunk(Ptr, &Header, Size); 487 } 488 489 // Reallocates a chunk. We can save on a new allocation if the new requested 490 // size still fits in the chunk. 491 void *reallocate(void *OldPtr, uptr NewSize) { 492 initThreadMaybe(); 493 494 #ifdef GWP_ASAN_HOOKS 495 if (UNLIKELY(GuardedAlloc.pointerIsMine(OldPtr))) { 496 size_t OldSize = GuardedAlloc.getSize(OldPtr); 497 void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); 498 if (NewPtr) 499 memcpy(NewPtr, OldPtr, (NewSize < OldSize) ? NewSize : OldSize); 500 GuardedAlloc.deallocate(OldPtr); 501 return NewPtr; 502 } 503 #endif // GWP_ASAN_HOOKS 504 505 if (UNLIKELY(!Chunk::isAligned(OldPtr))) 506 dieWithMessage("misaligned address when reallocating address %p\n", 507 OldPtr); 508 UnpackedHeader OldHeader; 509 Chunk::loadHeader(OldPtr, &OldHeader); 510 if (UNLIKELY(OldHeader.State != ChunkAllocated)) 511 dieWithMessage("invalid chunk state when reallocating address %p\n", 512 OldPtr); 513 if (DeallocationTypeMismatch) { 514 if (UNLIKELY(OldHeader.AllocType != FromMalloc)) 515 dieWithMessage("allocation type mismatch when reallocating address " 516 "%p\n", OldPtr); 517 } 518 const uptr UsableSize = Chunk::getUsableSize(OldPtr, &OldHeader); 519 // The new size still fits in the current chunk, and the size difference 520 // is reasonable. 521 if (NewSize <= UsableSize && 522 (UsableSize - NewSize) < (SizeClassMap::kMaxSize / 2)) { 523 UnpackedHeader NewHeader = OldHeader; 524 NewHeader.SizeOrUnusedBytes = 525 OldHeader.ClassId ? NewSize : UsableSize - NewSize; 526 Chunk::compareExchangeHeader(OldPtr, &NewHeader, &OldHeader); 527 return OldPtr; 528 } 529 // Otherwise, we have to allocate a new chunk and copy the contents of the 530 // old one. 531 void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); 532 if (NewPtr) { 533 const uptr OldSize = OldHeader.ClassId ? OldHeader.SizeOrUnusedBytes : 534 UsableSize - OldHeader.SizeOrUnusedBytes; 535 memcpy(NewPtr, OldPtr, Min(NewSize, UsableSize)); 536 quarantineOrDeallocateChunk(OldPtr, &OldHeader, OldSize); 537 } 538 return NewPtr; 539 } 540 541 // Helper function that returns the actual usable size of a chunk. 542 uptr getUsableSize(const void *Ptr) { 543 initThreadMaybe(); 544 if (UNLIKELY(!Ptr)) 545 return 0; 546 547 #ifdef GWP_ASAN_HOOKS 548 if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) 549 return GuardedAlloc.getSize(Ptr); 550 #endif // GWP_ASAN_HOOKS 551 552 UnpackedHeader Header; 553 Chunk::loadHeader(Ptr, &Header); 554 // Getting the usable size of a chunk only makes sense if it's allocated. 555 if (UNLIKELY(Header.State != ChunkAllocated)) 556 dieWithMessage("invalid chunk state when sizing address %p\n", Ptr); 557 return Chunk::getUsableSize(Ptr, &Header); 558 } 559 560 void *calloc(uptr NMemB, uptr Size) { 561 initThreadMaybe(); 562 if (UNLIKELY(CheckForCallocOverflow(NMemB, Size))) { 563 if (AllocatorMayReturnNull()) 564 return nullptr; 565 reportCallocOverflow(NMemB, Size); 566 } 567 return allocate(NMemB * Size, MinAlignment, FromMalloc, true); 568 } 569 570 void commitBack(ScudoTSD *TSD) { 571 Quarantine.Drain(getQuarantineCache(TSD), QuarantineCallback(&TSD->Cache)); 572 Backend.destroyCache(&TSD->Cache); 573 } 574 575 uptr getStats(AllocatorStat StatType) { 576 initThreadMaybe(); 577 uptr stats[AllocatorStatCount]; 578 Backend.getStats(stats); 579 return stats[StatType]; 580 } 581 582 bool canReturnNull() { 583 initThreadMaybe(); 584 return AllocatorMayReturnNull(); 585 } 586 587 void setRssLimit(uptr LimitMb, bool HardLimit) { 588 if (HardLimit) 589 HardRssLimitMb = LimitMb; 590 else 591 SoftRssLimitMb = LimitMb; 592 CheckRssLimit = HardRssLimitMb || SoftRssLimitMb; 593 } 594 595 void printStats() { 596 initThreadMaybe(); 597 Backend.printStats(); 598 } 599 }; 600 601 NOINLINE void Allocator::performSanityChecks() { 602 // Verify that the header offset field can hold the maximum offset. In the 603 // case of the Secondary allocator, it takes care of alignment and the 604 // offset will always be 0. In the case of the Primary, the worst case 605 // scenario happens in the last size class, when the backend allocation 606 // would already be aligned on the requested alignment, which would happen 607 // to be the maximum alignment that would fit in that size class. As a 608 // result, the maximum offset will be at most the maximum alignment for the 609 // last size class minus the header size, in multiples of MinAlignment. 610 UnpackedHeader Header = {}; 611 const uptr MaxPrimaryAlignment = 612 1 << MostSignificantSetBitIndex(SizeClassMap::kMaxSize - MinAlignment); 613 const uptr MaxOffset = 614 (MaxPrimaryAlignment - Chunk::getHeaderSize()) >> MinAlignmentLog; 615 Header.Offset = MaxOffset; 616 if (Header.Offset != MaxOffset) 617 dieWithMessage("maximum possible offset doesn't fit in header\n"); 618 // Verify that we can fit the maximum size or amount of unused bytes in the 619 // header. Given that the Secondary fits the allocation to a page, the worst 620 // case scenario happens in the Primary. It will depend on the second to 621 // last and last class sizes, as well as the dynamic base for the Primary. 622 // The following is an over-approximation that works for our needs. 623 const uptr MaxSizeOrUnusedBytes = SizeClassMap::kMaxSize - 1; 624 Header.SizeOrUnusedBytes = MaxSizeOrUnusedBytes; 625 if (Header.SizeOrUnusedBytes != MaxSizeOrUnusedBytes) 626 dieWithMessage("maximum possible unused bytes doesn't fit in header\n"); 627 628 const uptr LargestClassId = SizeClassMap::kLargestClassID; 629 Header.ClassId = LargestClassId; 630 if (Header.ClassId != LargestClassId) 631 dieWithMessage("largest class ID doesn't fit in header\n"); 632 } 633 634 // Opportunistic RSS limit check. This will update the RSS limit status, if 635 // it can, every 250ms, otherwise it will just return the current one. 636 NOINLINE bool Allocator::isRssLimitExceeded() { 637 u64 LastCheck = atomic_load_relaxed(&RssLastCheckedAtNS); 638 const u64 CurrentCheck = MonotonicNanoTime(); 639 if (LIKELY(CurrentCheck < LastCheck + (250ULL * 1000000ULL))) 640 return atomic_load_relaxed(&RssLimitExceeded); 641 if (!atomic_compare_exchange_weak(&RssLastCheckedAtNS, &LastCheck, 642 CurrentCheck, memory_order_relaxed)) 643 return atomic_load_relaxed(&RssLimitExceeded); 644 // TODO(kostyak): We currently use sanitizer_common's GetRSS which reads the 645 // RSS from /proc/self/statm by default. We might want to 646 // call getrusage directly, even if it's less accurate. 647 const uptr CurrentRssMb = GetRSS() >> 20; 648 if (HardRssLimitMb && UNLIKELY(HardRssLimitMb < CurrentRssMb)) 649 dieWithMessage("hard RSS limit exhausted (%zdMb vs %zdMb)\n", 650 HardRssLimitMb, CurrentRssMb); 651 if (SoftRssLimitMb) { 652 if (atomic_load_relaxed(&RssLimitExceeded)) { 653 if (CurrentRssMb <= SoftRssLimitMb) 654 atomic_store_relaxed(&RssLimitExceeded, false); 655 } else { 656 if (CurrentRssMb > SoftRssLimitMb) { 657 atomic_store_relaxed(&RssLimitExceeded, true); 658 Printf("Scudo INFO: soft RSS limit exhausted (%zdMb vs %zdMb)\n", 659 SoftRssLimitMb, CurrentRssMb); 660 } 661 } 662 } 663 return atomic_load_relaxed(&RssLimitExceeded); 664 } 665 666 static Allocator Instance(LINKER_INITIALIZED); 667 668 static BackendT &getBackend() { 669 return Instance.Backend; 670 } 671 672 void initScudo() { 673 Instance.init(); 674 #ifdef GWP_ASAN_HOOKS 675 gwp_asan::options::initOptions(__sanitizer::GetEnv("GWP_ASAN_OPTIONS"), 676 Printf); 677 gwp_asan::options::Options &Opts = gwp_asan::options::getOptions(); 678 Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction(); 679 GuardedAlloc.init(Opts); 680 681 if (Opts.InstallSignalHandlers) 682 gwp_asan::segv_handler::installSignalHandlers( 683 &GuardedAlloc, __sanitizer::Printf, 684 gwp_asan::backtrace::getPrintBacktraceFunction(), 685 gwp_asan::backtrace::getSegvBacktraceFunction()); 686 #endif // GWP_ASAN_HOOKS 687 } 688 689 void ScudoTSD::init() { 690 getBackend().initCache(&Cache); 691 memset(QuarantineCachePlaceHolder, 0, sizeof(QuarantineCachePlaceHolder)); 692 } 693 694 void ScudoTSD::commitBack() { 695 Instance.commitBack(this); 696 } 697 698 void *scudoAllocate(uptr Size, uptr Alignment, AllocType Type) { 699 if (Alignment && UNLIKELY(!IsPowerOfTwo(Alignment))) { 700 errno = EINVAL; 701 if (Instance.canReturnNull()) 702 return nullptr; 703 reportAllocationAlignmentNotPowerOfTwo(Alignment); 704 } 705 return SetErrnoOnNull(Instance.allocate(Size, Alignment, Type)); 706 } 707 708 void scudoDeallocate(void *Ptr, uptr Size, uptr Alignment, AllocType Type) { 709 Instance.deallocate(Ptr, Size, Alignment, Type); 710 } 711 712 void *scudoRealloc(void *Ptr, uptr Size) { 713 if (!Ptr) 714 return SetErrnoOnNull(Instance.allocate(Size, MinAlignment, FromMalloc)); 715 if (Size == 0) { 716 Instance.deallocate(Ptr, 0, 0, FromMalloc); 717 return nullptr; 718 } 719 return SetErrnoOnNull(Instance.reallocate(Ptr, Size)); 720 } 721 722 void *scudoCalloc(uptr NMemB, uptr Size) { 723 return SetErrnoOnNull(Instance.calloc(NMemB, Size)); 724 } 725 726 void *scudoValloc(uptr Size) { 727 return SetErrnoOnNull( 728 Instance.allocate(Size, GetPageSizeCached(), FromMemalign)); 729 } 730 731 void *scudoPvalloc(uptr Size) { 732 const uptr PageSize = GetPageSizeCached(); 733 if (UNLIKELY(CheckForPvallocOverflow(Size, PageSize))) { 734 errno = ENOMEM; 735 if (Instance.canReturnNull()) 736 return nullptr; 737 reportPvallocOverflow(Size); 738 } 739 // pvalloc(0) should allocate one page. 740 Size = Size ? RoundUpTo(Size, PageSize) : PageSize; 741 return SetErrnoOnNull(Instance.allocate(Size, PageSize, FromMemalign)); 742 } 743 744 int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) { 745 if (UNLIKELY(!CheckPosixMemalignAlignment(Alignment))) { 746 if (!Instance.canReturnNull()) 747 reportInvalidPosixMemalignAlignment(Alignment); 748 return EINVAL; 749 } 750 void *Ptr = Instance.allocate(Size, Alignment, FromMemalign); 751 if (UNLIKELY(!Ptr)) 752 return ENOMEM; 753 *MemPtr = Ptr; 754 return 0; 755 } 756 757 void *scudoAlignedAlloc(uptr Alignment, uptr Size) { 758 if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(Alignment, Size))) { 759 errno = EINVAL; 760 if (Instance.canReturnNull()) 761 return nullptr; 762 reportInvalidAlignedAllocAlignment(Size, Alignment); 763 } 764 return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMalloc)); 765 } 766 767 uptr scudoMallocUsableSize(void *Ptr) { 768 return Instance.getUsableSize(Ptr); 769 } 770 771 } // namespace __scudo 772 773 using namespace __scudo; 774 775 // MallocExtension helper functions 776 777 uptr __sanitizer_get_current_allocated_bytes() { 778 return Instance.getStats(AllocatorStatAllocated); 779 } 780 781 uptr __sanitizer_get_heap_size() { 782 return Instance.getStats(AllocatorStatMapped); 783 } 784 785 uptr __sanitizer_get_free_bytes() { 786 return 1; 787 } 788 789 uptr __sanitizer_get_unmapped_bytes() { 790 return 1; 791 } 792 793 uptr __sanitizer_get_estimated_allocated_size(uptr Size) { 794 return Size; 795 } 796 797 int __sanitizer_get_ownership(const void *Ptr) { 798 return Instance.isValidPointer(Ptr); 799 } 800 801 uptr __sanitizer_get_allocated_size(const void *Ptr) { 802 return Instance.getUsableSize(Ptr); 803 } 804 805 #if !SANITIZER_SUPPORTS_WEAK_HOOKS 806 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook, 807 void *Ptr, uptr Size) { 808 (void)Ptr; 809 (void)Size; 810 } 811 812 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_free_hook, void *Ptr) { 813 (void)Ptr; 814 } 815 #endif 816 817 // Interface functions 818 819 void __scudo_set_rss_limit(uptr LimitMb, s32 HardLimit) { 820 if (!SCUDO_CAN_USE_PUBLIC_INTERFACE) 821 return; 822 Instance.setRssLimit(LimitMb, !!HardLimit); 823 } 824 825 void __scudo_print_stats() { 826 Instance.printStats(); 827 } 828