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
computeCRC32(u32 Crc,uptr Value,uptr * Array,uptr ArraySize)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 {
getAtomicHeader(void * Ptr)74 static inline AtomicPackedHeader *getAtomicHeader(void *Ptr) {
75 return reinterpret_cast<AtomicPackedHeader *>(reinterpret_cast<uptr>(Ptr) -
76 getHeaderSize());
77 }
78 static inline
getConstAtomicHeader(const void * Ptr)79 const AtomicPackedHeader *getConstAtomicHeader(const void *Ptr) {
80 return reinterpret_cast<const AtomicPackedHeader *>(
81 reinterpret_cast<uptr>(Ptr) - getHeaderSize());
82 }
83
isAligned(const void * Ptr)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.
getBackendPtr(const void * Ptr,UnpackedHeader * 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.
getUsableSize(const void * Ptr,UnpackedHeader * Header)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.
getSize(const void * Ptr,UnpackedHeader * Header)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.
computeChecksum(const void * Ptr,UnpackedHeader * 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.
isValid(const void * Ptr)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
loadHeader(const void * Ptr,UnpackedHeader * NewUnpackedHeader)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.
storeHeader(void * Ptr,UnpackedHeader * NewUnpackedHeader)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.
compareExchangeHeader(void * Ptr,UnpackedHeader * NewUnpackedHeader,UnpackedHeader * OldUnpackedHeader)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 {
QuarantineCallback__scudo::QuarantineCallback177 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.
Recycle__scudo::QuarantineCallback182 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.
Allocate__scudo::QuarantineCallback200 void *Allocate(uptr Size) {
201 const uptr BatchClassId = SizeClassMap::ClassID(sizeof(QuarantineBatch));
202 return getBackend().allocatePrimary(Cache_, BatchClassId);
203 }
204
Deallocate__scudo::QuarantineCallback205 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
getQuarantineCache(ScudoTSD * TSD)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
Allocator__scudo::Allocator246 explicit Allocator(LinkerInitialized)
247 : Quarantine(LINKER_INITIALIZED) {}
248
249 NOINLINE void performSanityChecks();
250
init__scudo::Allocator251 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.
isValidPointer__scudo::Allocator290 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.
allocate__scudo::Allocator302 void *allocate(uptr Size, uptr Alignment, AllocType Type,
303 bool ForceZeroContents = false) NO_THREAD_SAFETY_ANALYSIS {
304 initThreadMaybe();
305
306 if (UNLIKELY(Alignment > MaxAlignment)) {
307 if (AllocatorMayReturnNull())
308 return nullptr;
309 reportAllocationAlignmentTooBig(Alignment, MaxAlignment);
310 }
311 if (UNLIKELY(Alignment < MinAlignment))
312 Alignment = MinAlignment;
313
314 #ifdef GWP_ASAN_HOOKS
315 if (UNLIKELY(GuardedAlloc.shouldSample())) {
316 if (void *Ptr = GuardedAlloc.allocate(Size, Alignment)) {
317 if (SCUDO_CAN_USE_HOOKS && &__sanitizer_malloc_hook)
318 __sanitizer_malloc_hook(Ptr, Size);
319 return Ptr;
320 }
321 }
322 #endif // GWP_ASAN_HOOKS
323
324 const uptr NeededSize = RoundUpTo(Size ? Size : 1, MinAlignment) +
325 Chunk::getHeaderSize();
326 const uptr AlignedSize = (Alignment > MinAlignment) ?
327 NeededSize + (Alignment - Chunk::getHeaderSize()) : NeededSize;
328 if (UNLIKELY(Size >= MaxAllowedMallocSize) ||
329 UNLIKELY(AlignedSize >= MaxAllowedMallocSize)) {
330 if (AllocatorMayReturnNull())
331 return nullptr;
332 reportAllocationSizeTooBig(Size, AlignedSize, MaxAllowedMallocSize);
333 }
334
335 if (CheckRssLimit && UNLIKELY(isRssLimitExceeded())) {
336 if (AllocatorMayReturnNull())
337 return nullptr;
338 reportRssLimitExceeded();
339 }
340
341 // Primary and Secondary backed allocations have a different treatment. We
342 // deal with alignment requirements of Primary serviced allocations here,
343 // but the Secondary will take care of its own alignment needs.
344 void *BackendPtr;
345 uptr BackendSize;
346 u8 ClassId;
347 if (PrimaryT::CanAllocate(AlignedSize, MinAlignment)) {
348 BackendSize = AlignedSize;
349 ClassId = SizeClassMap::ClassID(BackendSize);
350 bool UnlockRequired;
351 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired);
352 BackendPtr = Backend.allocatePrimary(&TSD->Cache, ClassId);
353 if (UnlockRequired)
354 TSD->unlock();
355 } else {
356 BackendSize = NeededSize;
357 ClassId = 0;
358 BackendPtr = Backend.allocateSecondary(BackendSize, Alignment);
359 }
360 if (UNLIKELY(!BackendPtr)) {
361 SetAllocatorOutOfMemory();
362 if (AllocatorMayReturnNull())
363 return nullptr;
364 reportOutOfMemory(Size);
365 }
366
367 // If requested, we will zero out the entire contents of the returned chunk.
368 if ((ForceZeroContents || ZeroContents) && ClassId)
369 memset(BackendPtr, 0, PrimaryT::ClassIdToSize(ClassId));
370
371 UnpackedHeader Header = {};
372 uptr UserPtr = reinterpret_cast<uptr>(BackendPtr) + Chunk::getHeaderSize();
373 if (UNLIKELY(!IsAligned(UserPtr, Alignment))) {
374 // Since the Secondary takes care of alignment, a non-aligned pointer
375 // means it is from the Primary. It is also the only case where the offset
376 // field of the header would be non-zero.
377 DCHECK(ClassId);
378 const uptr AlignedUserPtr = RoundUpTo(UserPtr, Alignment);
379 Header.Offset = (AlignedUserPtr - UserPtr) >> MinAlignmentLog;
380 UserPtr = AlignedUserPtr;
381 }
382 DCHECK_LE(UserPtr + Size, reinterpret_cast<uptr>(BackendPtr) + BackendSize);
383 Header.State = ChunkAllocated;
384 Header.AllocType = Type;
385 if (ClassId) {
386 Header.ClassId = ClassId;
387 Header.SizeOrUnusedBytes = Size;
388 } else {
389 // The secondary fits the allocations to a page, so the amount of unused
390 // bytes is the difference between the end of the user allocation and the
391 // next page boundary.
392 const uptr PageSize = GetPageSizeCached();
393 const uptr TrailingBytes = (UserPtr + Size) & (PageSize - 1);
394 if (TrailingBytes)
395 Header.SizeOrUnusedBytes = PageSize - TrailingBytes;
396 }
397 void *Ptr = reinterpret_cast<void *>(UserPtr);
398 Chunk::storeHeader(Ptr, &Header);
399 if (SCUDO_CAN_USE_HOOKS && &__sanitizer_malloc_hook)
400 __sanitizer_malloc_hook(Ptr, Size);
401 return Ptr;
402 }
403
404 // Place a chunk in the quarantine or directly deallocate it in the event of
405 // a zero-sized quarantine, or if the size of the chunk is greater than the
406 // quarantine chunk size threshold.
quarantineOrDeallocateChunk__scudo::Allocator407 void quarantineOrDeallocateChunk(void *Ptr, UnpackedHeader *Header,
408 uptr Size) NO_THREAD_SAFETY_ANALYSIS {
409 const bool BypassQuarantine = !Size || (Size > QuarantineChunksUpToSize);
410 if (BypassQuarantine) {
411 UnpackedHeader NewHeader = *Header;
412 NewHeader.State = ChunkAvailable;
413 Chunk::compareExchangeHeader(Ptr, &NewHeader, Header);
414 void *BackendPtr = Chunk::getBackendPtr(Ptr, Header);
415 if (Header->ClassId) {
416 bool UnlockRequired;
417 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired);
418 getBackend().deallocatePrimary(&TSD->Cache, BackendPtr,
419 Header->ClassId);
420 if (UnlockRequired)
421 TSD->unlock();
422 } else {
423 getBackend().deallocateSecondary(BackendPtr);
424 }
425 } else {
426 // If a small memory amount was allocated with a larger alignment, we want
427 // to take that into account. Otherwise the Quarantine would be filled
428 // with tiny chunks, taking a lot of VA memory. This is an approximation
429 // of the usable size, that allows us to not call
430 // GetActuallyAllocatedSize.
431 const uptr EstimatedSize = Size + (Header->Offset << MinAlignmentLog);
432 UnpackedHeader NewHeader = *Header;
433 NewHeader.State = ChunkQuarantine;
434 Chunk::compareExchangeHeader(Ptr, &NewHeader, Header);
435 bool UnlockRequired;
436 ScudoTSD *TSD = getTSDAndLock(&UnlockRequired);
437 Quarantine.Put(getQuarantineCache(TSD), QuarantineCallback(&TSD->Cache),
438 Ptr, EstimatedSize);
439 if (UnlockRequired)
440 TSD->unlock();
441 }
442 }
443
444 // Deallocates a Chunk, which means either adding it to the quarantine or
445 // directly returning it to the backend if criteria are met.
deallocate__scudo::Allocator446 void deallocate(void *Ptr, uptr DeleteSize, uptr DeleteAlignment,
447 AllocType Type) {
448 // For a deallocation, we only ensure minimal initialization, meaning thread
449 // local data will be left uninitialized for now (when using ELF TLS). The
450 // fallback cache will be used instead. This is a workaround for a situation
451 // where the only heap operation performed in a thread would be a free past
452 // the TLS destructors, ending up in initialized thread specific data never
453 // being destroyed properly. Any other heap operation will do a full init.
454 initThreadMaybe(/*MinimalInit=*/true);
455 if (SCUDO_CAN_USE_HOOKS && &__sanitizer_free_hook)
456 __sanitizer_free_hook(Ptr);
457 if (UNLIKELY(!Ptr))
458 return;
459
460 #ifdef GWP_ASAN_HOOKS
461 if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr))) {
462 GuardedAlloc.deallocate(Ptr);
463 return;
464 }
465 #endif // GWP_ASAN_HOOKS
466
467 if (UNLIKELY(!Chunk::isAligned(Ptr)))
468 dieWithMessage("misaligned pointer when deallocating address %p\n", Ptr);
469 UnpackedHeader Header;
470 Chunk::loadHeader(Ptr, &Header);
471 if (UNLIKELY(Header.State != ChunkAllocated))
472 dieWithMessage("invalid chunk state when deallocating address %p\n", Ptr);
473 if (DeallocationTypeMismatch) {
474 // The deallocation type has to match the allocation one.
475 if (Header.AllocType != Type) {
476 // With the exception of memalign'd Chunks, that can be still be free'd.
477 if (Header.AllocType != FromMemalign || Type != FromMalloc)
478 dieWithMessage("allocation type mismatch when deallocating address "
479 "%p\n", Ptr);
480 }
481 }
482 const uptr Size = Chunk::getSize(Ptr, &Header);
483 if (DeleteSizeMismatch) {
484 if (DeleteSize && DeleteSize != Size)
485 dieWithMessage("invalid sized delete when deallocating address %p\n",
486 Ptr);
487 }
488 (void)DeleteAlignment; // TODO(kostyak): verify that the alignment matches.
489 quarantineOrDeallocateChunk(Ptr, &Header, Size);
490 }
491
492 // Reallocates a chunk. We can save on a new allocation if the new requested
493 // size still fits in the chunk.
reallocate__scudo::Allocator494 void *reallocate(void *OldPtr, uptr NewSize) {
495 initThreadMaybe();
496
497 #ifdef GWP_ASAN_HOOKS
498 if (UNLIKELY(GuardedAlloc.pointerIsMine(OldPtr))) {
499 size_t OldSize = GuardedAlloc.getSize(OldPtr);
500 void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc);
501 if (NewPtr)
502 memcpy(NewPtr, OldPtr, (NewSize < OldSize) ? NewSize : OldSize);
503 GuardedAlloc.deallocate(OldPtr);
504 return NewPtr;
505 }
506 #endif // GWP_ASAN_HOOKS
507
508 if (UNLIKELY(!Chunk::isAligned(OldPtr)))
509 dieWithMessage("misaligned address when reallocating address %p\n",
510 OldPtr);
511 UnpackedHeader OldHeader;
512 Chunk::loadHeader(OldPtr, &OldHeader);
513 if (UNLIKELY(OldHeader.State != ChunkAllocated))
514 dieWithMessage("invalid chunk state when reallocating address %p\n",
515 OldPtr);
516 if (DeallocationTypeMismatch) {
517 if (UNLIKELY(OldHeader.AllocType != FromMalloc))
518 dieWithMessage("allocation type mismatch when reallocating address "
519 "%p\n", OldPtr);
520 }
521 const uptr UsableSize = Chunk::getUsableSize(OldPtr, &OldHeader);
522 // The new size still fits in the current chunk, and the size difference
523 // is reasonable.
524 if (NewSize <= UsableSize &&
525 (UsableSize - NewSize) < (SizeClassMap::kMaxSize / 2)) {
526 UnpackedHeader NewHeader = OldHeader;
527 NewHeader.SizeOrUnusedBytes =
528 OldHeader.ClassId ? NewSize : UsableSize - NewSize;
529 Chunk::compareExchangeHeader(OldPtr, &NewHeader, &OldHeader);
530 return OldPtr;
531 }
532 // Otherwise, we have to allocate a new chunk and copy the contents of the
533 // old one.
534 void *NewPtr = allocate(NewSize, MinAlignment, FromMalloc);
535 if (NewPtr) {
536 const uptr OldSize = OldHeader.ClassId ? OldHeader.SizeOrUnusedBytes :
537 UsableSize - OldHeader.SizeOrUnusedBytes;
538 memcpy(NewPtr, OldPtr, Min(NewSize, UsableSize));
539 quarantineOrDeallocateChunk(OldPtr, &OldHeader, OldSize);
540 }
541 return NewPtr;
542 }
543
544 // Helper function that returns the actual usable size of a chunk.
getUsableSize__scudo::Allocator545 uptr getUsableSize(const void *Ptr) {
546 initThreadMaybe();
547 if (UNLIKELY(!Ptr))
548 return 0;
549
550 #ifdef GWP_ASAN_HOOKS
551 if (UNLIKELY(GuardedAlloc.pointerIsMine(Ptr)))
552 return GuardedAlloc.getSize(Ptr);
553 #endif // GWP_ASAN_HOOKS
554
555 UnpackedHeader Header;
556 Chunk::loadHeader(Ptr, &Header);
557 // Getting the usable size of a chunk only makes sense if it's allocated.
558 if (UNLIKELY(Header.State != ChunkAllocated))
559 dieWithMessage("invalid chunk state when sizing address %p\n", Ptr);
560 return Chunk::getUsableSize(Ptr, &Header);
561 }
562
calloc__scudo::Allocator563 void *calloc(uptr NMemB, uptr Size) {
564 initThreadMaybe();
565 if (UNLIKELY(CheckForCallocOverflow(NMemB, Size))) {
566 if (AllocatorMayReturnNull())
567 return nullptr;
568 reportCallocOverflow(NMemB, Size);
569 }
570 return allocate(NMemB * Size, MinAlignment, FromMalloc, true);
571 }
572
commitBack__scudo::Allocator573 void commitBack(ScudoTSD *TSD) {
574 Quarantine.Drain(getQuarantineCache(TSD), QuarantineCallback(&TSD->Cache));
575 Backend.destroyCache(&TSD->Cache);
576 }
577
getStats__scudo::Allocator578 uptr getStats(AllocatorStat StatType) {
579 initThreadMaybe();
580 uptr stats[AllocatorStatCount];
581 Backend.getStats(stats);
582 return stats[StatType];
583 }
584
canReturnNull__scudo::Allocator585 bool canReturnNull() {
586 initThreadMaybe();
587 return AllocatorMayReturnNull();
588 }
589
setRssLimit__scudo::Allocator590 void setRssLimit(uptr LimitMb, bool HardLimit) {
591 if (HardLimit)
592 HardRssLimitMb = LimitMb;
593 else
594 SoftRssLimitMb = LimitMb;
595 CheckRssLimit = HardRssLimitMb || SoftRssLimitMb;
596 }
597
printStats__scudo::Allocator598 void printStats() {
599 initThreadMaybe();
600 Backend.printStats();
601 }
602 };
603
performSanityChecks()604 NOINLINE void Allocator::performSanityChecks() {
605 // Verify that the header offset field can hold the maximum offset. In the
606 // case of the Secondary allocator, it takes care of alignment and the
607 // offset will always be 0. In the case of the Primary, the worst case
608 // scenario happens in the last size class, when the backend allocation
609 // would already be aligned on the requested alignment, which would happen
610 // to be the maximum alignment that would fit in that size class. As a
611 // result, the maximum offset will be at most the maximum alignment for the
612 // last size class minus the header size, in multiples of MinAlignment.
613 UnpackedHeader Header = {};
614 const uptr MaxPrimaryAlignment =
615 1 << MostSignificantSetBitIndex(SizeClassMap::kMaxSize - MinAlignment);
616 const uptr MaxOffset =
617 (MaxPrimaryAlignment - Chunk::getHeaderSize()) >> MinAlignmentLog;
618 Header.Offset = MaxOffset;
619 if (Header.Offset != MaxOffset)
620 dieWithMessage("maximum possible offset doesn't fit in header\n");
621 // Verify that we can fit the maximum size or amount of unused bytes in the
622 // header. Given that the Secondary fits the allocation to a page, the worst
623 // case scenario happens in the Primary. It will depend on the second to
624 // last and last class sizes, as well as the dynamic base for the Primary.
625 // The following is an over-approximation that works for our needs.
626 const uptr MaxSizeOrUnusedBytes = SizeClassMap::kMaxSize - 1;
627 Header.SizeOrUnusedBytes = MaxSizeOrUnusedBytes;
628 if (Header.SizeOrUnusedBytes != MaxSizeOrUnusedBytes)
629 dieWithMessage("maximum possible unused bytes doesn't fit in header\n");
630
631 const uptr LargestClassId = SizeClassMap::kLargestClassID;
632 Header.ClassId = LargestClassId;
633 if (Header.ClassId != LargestClassId)
634 dieWithMessage("largest class ID doesn't fit in header\n");
635 }
636
637 // Opportunistic RSS limit check. This will update the RSS limit status, if
638 // it can, every 250ms, otherwise it will just return the current one.
isRssLimitExceeded()639 NOINLINE bool Allocator::isRssLimitExceeded() {
640 u64 LastCheck = atomic_load_relaxed(&RssLastCheckedAtNS);
641 const u64 CurrentCheck = MonotonicNanoTime();
642 if (LIKELY(CurrentCheck < LastCheck + (250ULL * 1000000ULL)))
643 return atomic_load_relaxed(&RssLimitExceeded);
644 if (!atomic_compare_exchange_weak(&RssLastCheckedAtNS, &LastCheck,
645 CurrentCheck, memory_order_relaxed))
646 return atomic_load_relaxed(&RssLimitExceeded);
647 // TODO(kostyak): We currently use sanitizer_common's GetRSS which reads the
648 // RSS from /proc/self/statm by default. We might want to
649 // call getrusage directly, even if it's less accurate.
650 const uptr CurrentRssMb = GetRSS() >> 20;
651 if (HardRssLimitMb && UNLIKELY(HardRssLimitMb < CurrentRssMb))
652 dieWithMessage("hard RSS limit exhausted (%zdMb vs %zdMb)\n",
653 HardRssLimitMb, CurrentRssMb);
654 if (SoftRssLimitMb) {
655 if (atomic_load_relaxed(&RssLimitExceeded)) {
656 if (CurrentRssMb <= SoftRssLimitMb)
657 atomic_store_relaxed(&RssLimitExceeded, false);
658 } else {
659 if (CurrentRssMb > SoftRssLimitMb) {
660 atomic_store_relaxed(&RssLimitExceeded, true);
661 Printf("Scudo INFO: soft RSS limit exhausted (%zdMb vs %zdMb)\n",
662 SoftRssLimitMb, CurrentRssMb);
663 }
664 }
665 }
666 return atomic_load_relaxed(&RssLimitExceeded);
667 }
668
669 static Allocator Instance(LINKER_INITIALIZED);
670
getBackend()671 static BackendT &getBackend() {
672 return Instance.Backend;
673 }
674
initScudo()675 void initScudo() {
676 Instance.init();
677 #ifdef GWP_ASAN_HOOKS
678 gwp_asan::options::initOptions(__sanitizer::GetEnv("GWP_ASAN_OPTIONS"),
679 Printf);
680 gwp_asan::options::Options &Opts = gwp_asan::options::getOptions();
681 Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction();
682 GuardedAlloc.init(Opts);
683
684 if (Opts.InstallSignalHandlers)
685 gwp_asan::segv_handler::installSignalHandlers(
686 &GuardedAlloc, __sanitizer::Printf,
687 gwp_asan::backtrace::getPrintBacktraceFunction(),
688 gwp_asan::backtrace::getSegvBacktraceFunction());
689 #endif // GWP_ASAN_HOOKS
690 }
691
init()692 void ScudoTSD::init() {
693 getBackend().initCache(&Cache);
694 memset(QuarantineCachePlaceHolder, 0, sizeof(QuarantineCachePlaceHolder));
695 }
696
commitBack()697 void ScudoTSD::commitBack() {
698 Instance.commitBack(this);
699 }
700
scudoAllocate(uptr Size,uptr Alignment,AllocType Type)701 void *scudoAllocate(uptr Size, uptr Alignment, AllocType Type) {
702 if (Alignment && UNLIKELY(!IsPowerOfTwo(Alignment))) {
703 errno = EINVAL;
704 if (Instance.canReturnNull())
705 return nullptr;
706 reportAllocationAlignmentNotPowerOfTwo(Alignment);
707 }
708 return SetErrnoOnNull(Instance.allocate(Size, Alignment, Type));
709 }
710
scudoDeallocate(void * Ptr,uptr Size,uptr Alignment,AllocType Type)711 void scudoDeallocate(void *Ptr, uptr Size, uptr Alignment, AllocType Type) {
712 Instance.deallocate(Ptr, Size, Alignment, Type);
713 }
714
scudoRealloc(void * Ptr,uptr Size)715 void *scudoRealloc(void *Ptr, uptr Size) {
716 if (!Ptr)
717 return SetErrnoOnNull(Instance.allocate(Size, MinAlignment, FromMalloc));
718 if (Size == 0) {
719 Instance.deallocate(Ptr, 0, 0, FromMalloc);
720 return nullptr;
721 }
722 return SetErrnoOnNull(Instance.reallocate(Ptr, Size));
723 }
724
scudoCalloc(uptr NMemB,uptr Size)725 void *scudoCalloc(uptr NMemB, uptr Size) {
726 return SetErrnoOnNull(Instance.calloc(NMemB, Size));
727 }
728
scudoValloc(uptr Size)729 void *scudoValloc(uptr Size) {
730 return SetErrnoOnNull(
731 Instance.allocate(Size, GetPageSizeCached(), FromMemalign));
732 }
733
scudoPvalloc(uptr Size)734 void *scudoPvalloc(uptr Size) {
735 const uptr PageSize = GetPageSizeCached();
736 if (UNLIKELY(CheckForPvallocOverflow(Size, PageSize))) {
737 errno = ENOMEM;
738 if (Instance.canReturnNull())
739 return nullptr;
740 reportPvallocOverflow(Size);
741 }
742 // pvalloc(0) should allocate one page.
743 Size = Size ? RoundUpTo(Size, PageSize) : PageSize;
744 return SetErrnoOnNull(Instance.allocate(Size, PageSize, FromMemalign));
745 }
746
scudoPosixMemalign(void ** MemPtr,uptr Alignment,uptr Size)747 int scudoPosixMemalign(void **MemPtr, uptr Alignment, uptr Size) {
748 if (UNLIKELY(!CheckPosixMemalignAlignment(Alignment))) {
749 if (!Instance.canReturnNull())
750 reportInvalidPosixMemalignAlignment(Alignment);
751 return EINVAL;
752 }
753 void *Ptr = Instance.allocate(Size, Alignment, FromMemalign);
754 if (UNLIKELY(!Ptr))
755 return ENOMEM;
756 *MemPtr = Ptr;
757 return 0;
758 }
759
scudoAlignedAlloc(uptr Alignment,uptr Size)760 void *scudoAlignedAlloc(uptr Alignment, uptr Size) {
761 if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(Alignment, Size))) {
762 errno = EINVAL;
763 if (Instance.canReturnNull())
764 return nullptr;
765 reportInvalidAlignedAllocAlignment(Size, Alignment);
766 }
767 return SetErrnoOnNull(Instance.allocate(Size, Alignment, FromMalloc));
768 }
769
scudoMallocUsableSize(void * Ptr)770 uptr scudoMallocUsableSize(void *Ptr) {
771 return Instance.getUsableSize(Ptr);
772 }
773
774 } // namespace __scudo
775
776 using namespace __scudo;
777
778 // MallocExtension helper functions
779
__sanitizer_get_current_allocated_bytes()780 uptr __sanitizer_get_current_allocated_bytes() {
781 return Instance.getStats(AllocatorStatAllocated);
782 }
783
__sanitizer_get_heap_size()784 uptr __sanitizer_get_heap_size() {
785 return Instance.getStats(AllocatorStatMapped);
786 }
787
__sanitizer_get_free_bytes()788 uptr __sanitizer_get_free_bytes() {
789 return 1;
790 }
791
__sanitizer_get_unmapped_bytes()792 uptr __sanitizer_get_unmapped_bytes() {
793 return 1;
794 }
795
__sanitizer_get_estimated_allocated_size(uptr Size)796 uptr __sanitizer_get_estimated_allocated_size(uptr Size) {
797 return Size;
798 }
799
__sanitizer_get_ownership(const void * Ptr)800 int __sanitizer_get_ownership(const void *Ptr) {
801 return Instance.isValidPointer(Ptr);
802 }
803
__sanitizer_get_allocated_size(const void * Ptr)804 uptr __sanitizer_get_allocated_size(const void *Ptr) {
805 return Instance.getUsableSize(Ptr);
806 }
807
808 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_malloc_hook,void * Ptr,uptr Size)809 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook,
810 void *Ptr, uptr Size) {
811 (void)Ptr;
812 (void)Size;
813 }
814
SANITIZER_INTERFACE_WEAK_DEF(void,__sanitizer_free_hook,void * Ptr)815 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_free_hook, void *Ptr) {
816 (void)Ptr;
817 }
818 #endif
819
820 // Interface functions
821
__scudo_set_rss_limit(uptr LimitMb,s32 HardLimit)822 void __scudo_set_rss_limit(uptr LimitMb, s32 HardLimit) {
823 if (!SCUDO_CAN_USE_PUBLIC_INTERFACE)
824 return;
825 Instance.setRssLimit(LimitMb, !!HardLimit);
826 }
827
__scudo_print_stats()828 void __scudo_print_stats() {
829 Instance.printStats();
830 }
831