1 //===-- scudo_allocator.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// Scudo Hardened Allocator implementation. 11 /// It uses the sanitizer_common allocator as a base and aims at mitigating 12 /// heap corruption vulnerabilities. It provides a checksum-guarded chunk 13 /// header, a delayed free list, and additional sanity checks. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #include "scudo_allocator.h" 18 #include "scudo_crc32.h" 19 #include "scudo_tls.h" 20 #include "scudo_utils.h" 21 22 #include "sanitizer_common/sanitizer_allocator_interface.h" 23 #include "sanitizer_common/sanitizer_quarantine.h" 24 25 #include <limits.h> 26 #include <pthread.h> 27 #include <string.h> 28 29 namespace __scudo { 30 31 // Global static cookie, initialized at start-up. 32 static uptr Cookie; 33 34 // We default to software CRC32 if the alternatives are not supported, either 35 // at compilation or at runtime. 36 static atomic_uint8_t HashAlgorithm = { CRC32Software }; 37 38 INLINE u32 computeCRC32(uptr Crc, uptr Value, uptr *Array, uptr ArraySize) { 39 // If the hardware CRC32 feature is defined here, it was enabled everywhere, 40 // as opposed to only for scudo_crc32.cpp. This means that other hardware 41 // specific instructions were likely emitted at other places, and as a 42 // result there is no reason to not use it here. 43 #if defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) 44 Crc = CRC32_INTRINSIC(Crc, Value); 45 for (uptr i = 0; i < ArraySize; i++) 46 Crc = CRC32_INTRINSIC(Crc, Array[i]); 47 return Crc; 48 #else 49 if (atomic_load_relaxed(&HashAlgorithm) == CRC32Hardware) { 50 Crc = computeHardwareCRC32(Crc, Value); 51 for (uptr i = 0; i < ArraySize; i++) 52 Crc = computeHardwareCRC32(Crc, Array[i]); 53 return Crc; 54 } 55 Crc = computeSoftwareCRC32(Crc, Value); 56 for (uptr i = 0; i < ArraySize; i++) 57 Crc = computeSoftwareCRC32(Crc, Array[i]); 58 return Crc; 59 #endif // defined(__SSE4_2__) || defined(__ARM_FEATURE_CRC32) 60 } 61 62 static ScudoBackendAllocator &getBackendAllocator(); 63 64 struct ScudoChunk : UnpackedHeader { 65 // We can't use the offset member of the chunk itself, as we would double 66 // fetch it without any warranty that it wouldn't have been tampered. To 67 // prevent this, we work with a local copy of the header. 68 void *getAllocBeg(UnpackedHeader *Header) { 69 return reinterpret_cast<void *>( 70 reinterpret_cast<uptr>(this) - (Header->Offset << MinAlignmentLog)); 71 } 72 73 // Returns the usable size for a chunk, meaning the amount of bytes from the 74 // beginning of the user data to the end of the backend allocated chunk. 75 uptr getUsableSize(UnpackedHeader *Header) { 76 uptr Size = 77 getBackendAllocator().GetActuallyAllocatedSize(getAllocBeg(Header), 78 Header->FromPrimary); 79 if (Size == 0) 80 return 0; 81 return Size - AlignedChunkHeaderSize - (Header->Offset << MinAlignmentLog); 82 } 83 84 // Compute the checksum of the Chunk pointer and its ChunkHeader. 85 u16 computeChecksum(UnpackedHeader *Header) const { 86 UnpackedHeader ZeroChecksumHeader = *Header; 87 ZeroChecksumHeader.Checksum = 0; 88 uptr HeaderHolder[sizeof(UnpackedHeader) / sizeof(uptr)]; 89 memcpy(&HeaderHolder, &ZeroChecksumHeader, sizeof(HeaderHolder)); 90 u32 Crc = computeCRC32(Cookie, reinterpret_cast<uptr>(this), HeaderHolder, 91 ARRAY_SIZE(HeaderHolder)); 92 return static_cast<u16>(Crc); 93 } 94 95 // Checks the validity of a chunk by verifying its checksum. It doesn't 96 // incur termination in the event of an invalid chunk. 97 bool isValid() { 98 UnpackedHeader NewUnpackedHeader; 99 const AtomicPackedHeader *AtomicHeader = 100 reinterpret_cast<const AtomicPackedHeader *>(this); 101 PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); 102 NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader); 103 return (NewUnpackedHeader.Checksum == computeChecksum(&NewUnpackedHeader)); 104 } 105 106 // Nulls out a chunk header. When returning the chunk to the backend, there 107 // is no need to store a valid ChunkAvailable header, as this would be 108 // computationally expensive. Zeroing out serves the same purpose by making 109 // the header invalid. In the extremely rare event where 0 would be a valid 110 // checksum for the chunk, the state of the chunk is ChunkAvailable anyway. 111 COMPILER_CHECK(ChunkAvailable == 0); 112 void eraseHeader() { 113 PackedHeader NullPackedHeader = 0; 114 AtomicPackedHeader *AtomicHeader = 115 reinterpret_cast<AtomicPackedHeader *>(this); 116 atomic_store_relaxed(AtomicHeader, NullPackedHeader); 117 } 118 119 // Loads and unpacks the header, verifying the checksum in the process. 120 void loadHeader(UnpackedHeader *NewUnpackedHeader) const { 121 const AtomicPackedHeader *AtomicHeader = 122 reinterpret_cast<const AtomicPackedHeader *>(this); 123 PackedHeader NewPackedHeader = atomic_load_relaxed(AtomicHeader); 124 *NewUnpackedHeader = bit_cast<UnpackedHeader>(NewPackedHeader); 125 if (UNLIKELY(NewUnpackedHeader->Checksum != 126 computeChecksum(NewUnpackedHeader))) { 127 dieWithMessage("ERROR: corrupted chunk header at address %p\n", this); 128 } 129 } 130 131 // Packs and stores the header, computing the checksum in the process. 132 void storeHeader(UnpackedHeader *NewUnpackedHeader) { 133 NewUnpackedHeader->Checksum = computeChecksum(NewUnpackedHeader); 134 PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); 135 AtomicPackedHeader *AtomicHeader = 136 reinterpret_cast<AtomicPackedHeader *>(this); 137 atomic_store_relaxed(AtomicHeader, NewPackedHeader); 138 } 139 140 // Packs and stores the header, computing the checksum in the process. We 141 // compare the current header with the expected provided one to ensure that 142 // we are not being raced by a corruption occurring in another thread. 143 void compareExchangeHeader(UnpackedHeader *NewUnpackedHeader, 144 UnpackedHeader *OldUnpackedHeader) { 145 NewUnpackedHeader->Checksum = computeChecksum(NewUnpackedHeader); 146 PackedHeader NewPackedHeader = bit_cast<PackedHeader>(*NewUnpackedHeader); 147 PackedHeader OldPackedHeader = bit_cast<PackedHeader>(*OldUnpackedHeader); 148 AtomicPackedHeader *AtomicHeader = 149 reinterpret_cast<AtomicPackedHeader *>(this); 150 if (UNLIKELY(!atomic_compare_exchange_strong(AtomicHeader, 151 &OldPackedHeader, 152 NewPackedHeader, 153 memory_order_relaxed))) { 154 dieWithMessage("ERROR: race on chunk header at address %p\n", this); 155 } 156 } 157 }; 158 159 ScudoChunk *getScudoChunk(uptr UserBeg) { 160 return reinterpret_cast<ScudoChunk *>(UserBeg - AlignedChunkHeaderSize); 161 } 162 163 struct AllocatorOptions { 164 u32 QuarantineSizeMb; 165 u32 ThreadLocalQuarantineSizeKb; 166 bool MayReturnNull; 167 s32 ReleaseToOSIntervalMs; 168 bool DeallocationTypeMismatch; 169 bool DeleteSizeMismatch; 170 bool ZeroContents; 171 172 void setFrom(const Flags *f, const CommonFlags *cf); 173 void copyTo(Flags *f, CommonFlags *cf) const; 174 }; 175 176 void AllocatorOptions::setFrom(const Flags *f, const CommonFlags *cf) { 177 MayReturnNull = cf->allocator_may_return_null; 178 ReleaseToOSIntervalMs = cf->allocator_release_to_os_interval_ms; 179 QuarantineSizeMb = f->QuarantineSizeMb; 180 ThreadLocalQuarantineSizeKb = f->ThreadLocalQuarantineSizeKb; 181 DeallocationTypeMismatch = f->DeallocationTypeMismatch; 182 DeleteSizeMismatch = f->DeleteSizeMismatch; 183 ZeroContents = f->ZeroContents; 184 } 185 186 void AllocatorOptions::copyTo(Flags *f, CommonFlags *cf) const { 187 cf->allocator_may_return_null = MayReturnNull; 188 cf->allocator_release_to_os_interval_ms = ReleaseToOSIntervalMs; 189 f->QuarantineSizeMb = QuarantineSizeMb; 190 f->ThreadLocalQuarantineSizeKb = ThreadLocalQuarantineSizeKb; 191 f->DeallocationTypeMismatch = DeallocationTypeMismatch; 192 f->DeleteSizeMismatch = DeleteSizeMismatch; 193 f->ZeroContents = ZeroContents; 194 } 195 196 static void initScudoInternal(const AllocatorOptions &Options); 197 198 static bool ScudoInitIsRunning = false; 199 200 void initScudo() { 201 SanitizerToolName = "Scudo"; 202 CHECK(!ScudoInitIsRunning && "Scudo init calls itself!"); 203 ScudoInitIsRunning = true; 204 205 // Check if hardware CRC32 is supported in the binary and by the platform, if 206 // so, opt for the CRC32 hardware version of the checksum. 207 if (computeHardwareCRC32 && testCPUFeature(CRC32CPUFeature)) 208 atomic_store_relaxed(&HashAlgorithm, CRC32Hardware); 209 210 initFlags(); 211 212 AllocatorOptions Options; 213 Options.setFrom(getFlags(), common_flags()); 214 initScudoInternal(Options); 215 216 // TODO(kostyak): determine if MaybeStartBackgroudThread could be of some use. 217 218 ScudoInitIsRunning = false; 219 } 220 221 struct QuarantineCallback { 222 explicit QuarantineCallback(AllocatorCache *Cache) 223 : Cache_(Cache) {} 224 225 // Chunk recycling function, returns a quarantined chunk to the backend, 226 // first making sure it hasn't been tampered with. 227 void Recycle(ScudoChunk *Chunk) { 228 UnpackedHeader Header; 229 Chunk->loadHeader(&Header); 230 if (UNLIKELY(Header.State != ChunkQuarantine)) { 231 dieWithMessage("ERROR: invalid chunk state when recycling address %p\n", 232 Chunk); 233 } 234 Chunk->eraseHeader(); 235 void *Ptr = Chunk->getAllocBeg(&Header); 236 getBackendAllocator().Deallocate(Cache_, Ptr, Header.FromPrimary); 237 } 238 239 // Internal quarantine allocation and deallocation functions. We first check 240 // that the batches are indeed serviced by the Primary. 241 // TODO(kostyak): figure out the best way to protect the batches. 242 COMPILER_CHECK(sizeof(QuarantineBatch) < SizeClassMap::kMaxSize); 243 void *Allocate(uptr Size) { 244 return getBackendAllocator().Allocate(Cache_, Size, MinAlignment, true); 245 } 246 247 void Deallocate(void *Ptr) { 248 getBackendAllocator().Deallocate(Cache_, Ptr, true); 249 } 250 251 AllocatorCache *Cache_; 252 }; 253 254 typedef Quarantine<QuarantineCallback, ScudoChunk> ScudoQuarantine; 255 typedef ScudoQuarantine::Cache ScudoQuarantineCache; 256 COMPILER_CHECK(sizeof(ScudoQuarantineCache) <= 257 sizeof(ScudoThreadContext::QuarantineCachePlaceHolder)); 258 259 AllocatorCache *getAllocatorCache(ScudoThreadContext *ThreadContext) { 260 return &ThreadContext->Cache; 261 } 262 263 ScudoQuarantineCache *getQuarantineCache(ScudoThreadContext *ThreadContext) { 264 return reinterpret_cast< 265 ScudoQuarantineCache *>(ThreadContext->QuarantineCachePlaceHolder); 266 } 267 268 Xorshift128Plus *getPrng(ScudoThreadContext *ThreadContext) { 269 return &ThreadContext->Prng; 270 } 271 272 struct ScudoAllocator { 273 static const uptr MaxAllowedMallocSize = 274 FIRST_32_SECOND_64(2UL << 30, 1ULL << 40); 275 276 ScudoBackendAllocator BackendAllocator; 277 ScudoQuarantine AllocatorQuarantine; 278 279 // The fallback caches are used when the thread local caches have been 280 // 'detroyed' on thread tear-down. They are protected by a Mutex as they can 281 // be accessed by different threads. 282 StaticSpinMutex FallbackMutex; 283 AllocatorCache FallbackAllocatorCache; 284 ScudoQuarantineCache FallbackQuarantineCache; 285 Xorshift128Plus FallbackPrng; 286 287 bool DeallocationTypeMismatch; 288 bool ZeroContents; 289 bool DeleteSizeMismatch; 290 291 explicit ScudoAllocator(LinkerInitialized) 292 : AllocatorQuarantine(LINKER_INITIALIZED), 293 FallbackQuarantineCache(LINKER_INITIALIZED) {} 294 295 void init(const AllocatorOptions &Options) { 296 // Verify that the header offset field can hold the maximum offset. In the 297 // case of the Secondary allocator, it takes care of alignment and the 298 // offset will always be 0. In the case of the Primary, the worst case 299 // scenario happens in the last size class, when the backend allocation 300 // would already be aligned on the requested alignment, which would happen 301 // to be the maximum alignment that would fit in that size class. As a 302 // result, the maximum offset will be at most the maximum alignment for the 303 // last size class minus the header size, in multiples of MinAlignment. 304 UnpackedHeader Header = {}; 305 uptr MaxPrimaryAlignment = 1 << MostSignificantSetBitIndex( 306 SizeClassMap::kMaxSize - MinAlignment); 307 uptr MaxOffset = (MaxPrimaryAlignment - AlignedChunkHeaderSize) >> 308 MinAlignmentLog; 309 Header.Offset = MaxOffset; 310 if (Header.Offset != MaxOffset) { 311 dieWithMessage("ERROR: the maximum possible offset doesn't fit in the " 312 "header\n"); 313 } 314 // Verify that we can fit the maximum size or amount of unused bytes in the 315 // header. Given that the Secondary fits the allocation to a page, the worst 316 // case scenario happens in the Primary. It will depend on the second to 317 // last and last class sizes, as well as the dynamic base for the Primary. 318 // The following is an over-approximation that works for our needs. 319 uptr MaxSizeOrUnusedBytes = SizeClassMap::kMaxSize - 1; 320 Header.SizeOrUnusedBytes = MaxSizeOrUnusedBytes; 321 if (Header.SizeOrUnusedBytes != MaxSizeOrUnusedBytes) { 322 dieWithMessage("ERROR: the maximum possible unused bytes doesn't fit in " 323 "the header\n"); 324 } 325 326 DeallocationTypeMismatch = Options.DeallocationTypeMismatch; 327 DeleteSizeMismatch = Options.DeleteSizeMismatch; 328 ZeroContents = Options.ZeroContents; 329 BackendAllocator.Init(Options.MayReturnNull, Options.ReleaseToOSIntervalMs); 330 AllocatorQuarantine.Init( 331 static_cast<uptr>(Options.QuarantineSizeMb) << 20, 332 static_cast<uptr>(Options.ThreadLocalQuarantineSizeKb) << 10); 333 BackendAllocator.InitCache(&FallbackAllocatorCache); 334 FallbackPrng.initFromURandom(); 335 Cookie = FallbackPrng.getNext(); 336 } 337 338 // Helper function that checks for a valid Scudo chunk. nullptr isn't. 339 bool isValidPointer(const void *UserPtr) { 340 initThreadMaybe(); 341 if (!UserPtr) 342 return false; 343 uptr UserBeg = reinterpret_cast<uptr>(UserPtr); 344 if (!IsAligned(UserBeg, MinAlignment)) 345 return false; 346 return getScudoChunk(UserBeg)->isValid(); 347 } 348 349 // Allocates a chunk. 350 void *allocate(uptr Size, uptr Alignment, AllocType Type, 351 bool ForceZeroContents = false) { 352 initThreadMaybe(); 353 if (UNLIKELY(!IsPowerOfTwo(Alignment))) { 354 dieWithMessage("ERROR: alignment is not a power of 2\n"); 355 } 356 if (Alignment > MaxAlignment) 357 return BackendAllocator.ReturnNullOrDieOnBadRequest(); 358 if (Alignment < MinAlignment) 359 Alignment = MinAlignment; 360 if (Size >= MaxAllowedMallocSize) 361 return BackendAllocator.ReturnNullOrDieOnBadRequest(); 362 if (Size == 0) 363 Size = 1; 364 365 uptr NeededSize = RoundUpTo(Size, MinAlignment) + AlignedChunkHeaderSize; 366 uptr AlignedSize = (Alignment > MinAlignment) ? 367 NeededSize + (Alignment - AlignedChunkHeaderSize) : NeededSize; 368 if (AlignedSize >= MaxAllowedMallocSize) 369 return BackendAllocator.ReturnNullOrDieOnBadRequest(); 370 371 // Primary and Secondary backed allocations have a different treatment. We 372 // deal with alignment requirements of Primary serviced allocations here, 373 // but the Secondary will take care of its own alignment needs. 374 bool FromPrimary = PrimaryAllocator::CanAllocate(AlignedSize, MinAlignment); 375 376 void *Ptr; 377 uptr Salt; 378 uptr AllocationSize = FromPrimary ? AlignedSize : NeededSize; 379 uptr AllocationAlignment = FromPrimary ? MinAlignment : Alignment; 380 ScudoThreadContext *ThreadContext = getThreadContextAndLock(); 381 if (LIKELY(ThreadContext)) { 382 Salt = getPrng(ThreadContext)->getNext(); 383 Ptr = BackendAllocator.Allocate(getAllocatorCache(ThreadContext), 384 AllocationSize, AllocationAlignment, 385 FromPrimary); 386 ThreadContext->unlock(); 387 } else { 388 SpinMutexLock l(&FallbackMutex); 389 Salt = FallbackPrng.getNext(); 390 Ptr = BackendAllocator.Allocate(&FallbackAllocatorCache, AllocationSize, 391 AllocationAlignment, FromPrimary); 392 } 393 if (!Ptr) 394 return BackendAllocator.ReturnNullOrDieOnOOM(); 395 396 // If requested, we will zero out the entire contents of the returned chunk. 397 if ((ForceZeroContents || ZeroContents) && FromPrimary) 398 memset(Ptr, 0, 399 BackendAllocator.GetActuallyAllocatedSize(Ptr, FromPrimary)); 400 401 UnpackedHeader Header = {}; 402 uptr AllocBeg = reinterpret_cast<uptr>(Ptr); 403 uptr UserBeg = AllocBeg + AlignedChunkHeaderSize; 404 if (!IsAligned(UserBeg, Alignment)) { 405 // Since the Secondary takes care of alignment, a non-aligned pointer 406 // means it is from the Primary. It is also the only case where the offset 407 // field of the header would be non-zero. 408 CHECK(FromPrimary); 409 UserBeg = RoundUpTo(UserBeg, Alignment); 410 uptr Offset = UserBeg - AlignedChunkHeaderSize - AllocBeg; 411 Header.Offset = Offset >> MinAlignmentLog; 412 } 413 CHECK_LE(UserBeg + Size, AllocBeg + AllocationSize); 414 Header.State = ChunkAllocated; 415 Header.AllocType = Type; 416 if (FromPrimary) { 417 Header.FromPrimary = FromPrimary; 418 Header.SizeOrUnusedBytes = Size; 419 } else { 420 // The secondary fits the allocations to a page, so the amount of unused 421 // bytes is the difference between the end of the user allocation and the 422 // next page boundary. 423 uptr PageSize = GetPageSizeCached(); 424 uptr TrailingBytes = (UserBeg + Size) & (PageSize - 1); 425 if (TrailingBytes) 426 Header.SizeOrUnusedBytes = PageSize - TrailingBytes; 427 } 428 Header.Salt = static_cast<u8>(Salt); 429 getScudoChunk(UserBeg)->storeHeader(&Header); 430 void *UserPtr = reinterpret_cast<void *>(UserBeg); 431 // if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(UserPtr, Size); 432 return UserPtr; 433 } 434 435 // Place a chunk in the quarantine. In the event of a zero-sized quarantine, 436 // we directly deallocate the chunk, otherwise the flow would lead to the 437 // chunk being loaded (and checked) twice, and stored (and checksummed) once, 438 // with no additional security value. 439 void quarantineOrDeallocateChunk(ScudoChunk *Chunk, UnpackedHeader *Header, 440 uptr Size) { 441 bool FromPrimary = Header->FromPrimary; 442 bool BypassQuarantine = (AllocatorQuarantine.GetCacheSize() == 0); 443 if (BypassQuarantine) { 444 Chunk->eraseHeader(); 445 void *Ptr = Chunk->getAllocBeg(Header); 446 ScudoThreadContext *ThreadContext = getThreadContextAndLock(); 447 if (LIKELY(ThreadContext)) { 448 getBackendAllocator().Deallocate(getAllocatorCache(ThreadContext), Ptr, 449 FromPrimary); 450 ThreadContext->unlock(); 451 } else { 452 SpinMutexLock Lock(&FallbackMutex); 453 getBackendAllocator().Deallocate(&FallbackAllocatorCache, Ptr, 454 FromPrimary); 455 } 456 } else { 457 UnpackedHeader NewHeader = *Header; 458 NewHeader.State = ChunkQuarantine; 459 Chunk->compareExchangeHeader(&NewHeader, Header); 460 ScudoThreadContext *ThreadContext = getThreadContextAndLock(); 461 if (LIKELY(ThreadContext)) { 462 AllocatorQuarantine.Put(getQuarantineCache(ThreadContext), 463 QuarantineCallback( 464 getAllocatorCache(ThreadContext)), 465 Chunk, Size); 466 ThreadContext->unlock(); 467 } else { 468 SpinMutexLock l(&FallbackMutex); 469 AllocatorQuarantine.Put(&FallbackQuarantineCache, 470 QuarantineCallback(&FallbackAllocatorCache), 471 Chunk, Size); 472 } 473 } 474 } 475 476 // Deallocates a Chunk, which means adding it to the delayed free list (or 477 // Quarantine). 478 void deallocate(void *UserPtr, uptr DeleteSize, AllocType Type) { 479 initThreadMaybe(); 480 // if (&__sanitizer_free_hook) __sanitizer_free_hook(UserPtr); 481 if (!UserPtr) 482 return; 483 uptr UserBeg = reinterpret_cast<uptr>(UserPtr); 484 if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { 485 dieWithMessage("ERROR: attempted to deallocate a chunk not properly " 486 "aligned at address %p\n", UserPtr); 487 } 488 ScudoChunk *Chunk = getScudoChunk(UserBeg); 489 UnpackedHeader OldHeader; 490 Chunk->loadHeader(&OldHeader); 491 if (UNLIKELY(OldHeader.State != ChunkAllocated)) { 492 dieWithMessage("ERROR: invalid chunk state when deallocating address " 493 "%p\n", UserPtr); 494 } 495 if (DeallocationTypeMismatch) { 496 // The deallocation type has to match the allocation one. 497 if (OldHeader.AllocType != Type) { 498 // With the exception of memalign'd Chunks, that can be still be free'd. 499 if (OldHeader.AllocType != FromMemalign || Type != FromMalloc) { 500 dieWithMessage("ERROR: allocation type mismatch on address %p\n", 501 UserPtr); 502 } 503 } 504 } 505 uptr Size = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : 506 Chunk->getUsableSize(&OldHeader) - OldHeader.SizeOrUnusedBytes; 507 if (DeleteSizeMismatch) { 508 if (DeleteSize && DeleteSize != Size) { 509 dieWithMessage("ERROR: invalid sized delete on chunk at address %p\n", 510 UserPtr); 511 } 512 } 513 514 // If a small memory amount was allocated with a larger alignment, we want 515 // to take that into account. Otherwise the Quarantine would be filled with 516 // tiny chunks, taking a lot of VA memory. This is an approximation of the 517 // usable size, that allows us to not call GetActuallyAllocatedSize. 518 uptr LiableSize = Size + (OldHeader.Offset << MinAlignment); 519 quarantineOrDeallocateChunk(Chunk, &OldHeader, LiableSize); 520 } 521 522 // Reallocates a chunk. We can save on a new allocation if the new requested 523 // size still fits in the chunk. 524 void *reallocate(void *OldPtr, uptr NewSize) { 525 initThreadMaybe(); 526 uptr UserBeg = reinterpret_cast<uptr>(OldPtr); 527 if (UNLIKELY(!IsAligned(UserBeg, MinAlignment))) { 528 dieWithMessage("ERROR: attempted to reallocate a chunk not properly " 529 "aligned at address %p\n", OldPtr); 530 } 531 ScudoChunk *Chunk = getScudoChunk(UserBeg); 532 UnpackedHeader OldHeader; 533 Chunk->loadHeader(&OldHeader); 534 if (UNLIKELY(OldHeader.State != ChunkAllocated)) { 535 dieWithMessage("ERROR: invalid chunk state when reallocating address " 536 "%p\n", OldPtr); 537 } 538 if (UNLIKELY(OldHeader.AllocType != FromMalloc)) { 539 dieWithMessage("ERROR: invalid chunk type when reallocating address %p\n", 540 OldPtr); 541 } 542 uptr UsableSize = Chunk->getUsableSize(&OldHeader); 543 // The new size still fits in the current chunk, and the size difference 544 // is reasonable. 545 if (NewSize <= UsableSize && 546 (UsableSize - NewSize) < (SizeClassMap::kMaxSize / 2)) { 547 UnpackedHeader NewHeader = OldHeader; 548 NewHeader.SizeOrUnusedBytes = 549 OldHeader.FromPrimary ? NewSize : UsableSize - NewSize; 550 Chunk->compareExchangeHeader(&NewHeader, &OldHeader); 551 return OldPtr; 552 } 553 // Otherwise, we have to allocate a new chunk and copy the contents of the 554 // old one. 555 void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc); 556 if (NewPtr) { 557 uptr OldSize = OldHeader.FromPrimary ? OldHeader.SizeOrUnusedBytes : 558 UsableSize - OldHeader.SizeOrUnusedBytes; 559 memcpy(NewPtr, OldPtr, Min(NewSize, OldSize)); 560 quarantineOrDeallocateChunk(Chunk, &OldHeader, UsableSize); 561 } 562 return NewPtr; 563 } 564 565 // Helper function that returns the actual usable size of a chunk. 566 uptr getUsableSize(const void *Ptr) { 567 initThreadMaybe(); 568 if (!Ptr) 569 return 0; 570 uptr UserBeg = reinterpret_cast<uptr>(Ptr); 571 ScudoChunk *Chunk = getScudoChunk(UserBeg); 572 UnpackedHeader Header; 573 Chunk->loadHeader(&Header); 574 // Getting the usable size of a chunk only makes sense if it's allocated. 575 if (UNLIKELY(Header.State != ChunkAllocated)) { 576 dieWithMessage("ERROR: invalid chunk state when sizing address %p\n", 577 Ptr); 578 } 579 return Chunk->getUsableSize(&Header); 580 } 581 582 void *calloc(uptr NMemB, uptr Size) { 583 initThreadMaybe(); 584 uptr Total = NMemB * Size; 585 if (Size != 0 && Total / Size != NMemB) // Overflow check 586 return BackendAllocator.ReturnNullOrDieOnBadRequest(); 587 return allocate(Total, MinAlignment, FromMalloc, true); 588 } 589 590 void commitBack(ScudoThreadContext *ThreadContext) { 591 AllocatorCache *Cache = getAllocatorCache(ThreadContext); 592 AllocatorQuarantine.Drain(getQuarantineCache(ThreadContext), 593 QuarantineCallback(Cache)); 594 BackendAllocator.DestroyCache(Cache); 595 } 596 597 uptr getStats(AllocatorStat StatType) { 598 initThreadMaybe(); 599 uptr stats[AllocatorStatCount]; 600 BackendAllocator.GetStats(stats); 601 return stats[StatType]; 602 } 603 }; 604 605 static ScudoAllocator Instance(LINKER_INITIALIZED); 606 607 static ScudoBackendAllocator &getBackendAllocator() { 608 return Instance.BackendAllocator; 609 } 610 611 static void initScudoInternal(const AllocatorOptions &Options) { 612 Instance.init(Options); 613 } 614 615 void ScudoThreadContext::init() { 616 getBackendAllocator().InitCache(&Cache); 617 Prng.initFromURandom(); 618 memset(QuarantineCachePlaceHolder, 0, sizeof(QuarantineCachePlaceHolder)); 619 } 620 621 void ScudoThreadContext::commitBack() { 622 Instance.commitBack(this); 623 } 624 625 void *scudoMalloc(uptr Size, AllocType Type) { 626 return Instance.allocate(Size, MinAlignment, Type); 627 } 628 629 void scudoFree(void *Ptr, AllocType Type) { 630 Instance.deallocate(Ptr, 0, Type); 631 } 632 633 void scudoSizedFree(void *Ptr, uptr Size, AllocType Type) { 634 Instance.deallocate(Ptr, Size, Type); 635 } 636 637 void *scudoRealloc(void *Ptr, uptr Size) { 638 if (!Ptr) 639 return Instance.allocate(Size, MinAlignment, FromMalloc); 640 if (Size == 0) { 641 Instance.deallocate(Ptr, 0, FromMalloc); 642 return nullptr; 643 } 644 return Instance.reallocate(Ptr, Size); 645 } 646 647 void *scudoCalloc(uptr NMemB, uptr Size) { 648 return Instance.calloc(NMemB, Size); 649 } 650 651 void *scudoValloc(uptr Size) { 652 return Instance.allocate(Size, GetPageSizeCached(), FromMemalign); 653 } 654 655 void *scudoMemalign(uptr Alignment, uptr Size) { 656 return Instance.allocate(Size, Alignment, FromMemalign); 657 } 658 659 void *scudoPvalloc(uptr Size) { 660 uptr PageSize = GetPageSizeCached(); 661 Size = RoundUpTo(Size, PageSize); 662 if (Size == 0) { 663 // pvalloc(0) should allocate one page. 664 Size = PageSize; 665 } 666 return Instance.allocate(Size, PageSize, FromMemalign); 667 } 668 669 int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) { 670 *MemPtr = Instance.allocate(Size, Alignment, FromMemalign); 671 return 0; 672 } 673 674 void *scudoAlignedAlloc(uptr Alignment, uptr Size) { 675 // size must be a multiple of the alignment. To avoid a division, we first 676 // make sure that alignment is a power of 2. 677 CHECK(IsPowerOfTwo(Alignment)); 678 CHECK_EQ((Size & (Alignment - 1)), 0); 679 return Instance.allocate(Size, Alignment, FromMalloc); 680 } 681 682 uptr scudoMallocUsableSize(void *Ptr) { 683 return Instance.getUsableSize(Ptr); 684 } 685 686 } // namespace __scudo 687 688 using namespace __scudo; 689 690 // MallocExtension helper functions 691 692 uptr __sanitizer_get_current_allocated_bytes() { 693 return Instance.getStats(AllocatorStatAllocated); 694 } 695 696 uptr __sanitizer_get_heap_size() { 697 return Instance.getStats(AllocatorStatMapped); 698 } 699 700 uptr __sanitizer_get_free_bytes() { 701 return 1; 702 } 703 704 uptr __sanitizer_get_unmapped_bytes() { 705 return 1; 706 } 707 708 uptr __sanitizer_get_estimated_allocated_size(uptr size) { 709 return size; 710 } 711 712 int __sanitizer_get_ownership(const void *Ptr) { 713 return Instance.isValidPointer(Ptr); 714 } 715 716 uptr __sanitizer_get_allocated_size(const void *Ptr) { 717 return Instance.getUsableSize(Ptr); 718 } 719