1//===-- secondary.h ---------------------------------------------*- 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#ifndef SCUDO_SECONDARY_H_
10#define SCUDO_SECONDARY_H_
11
12#ifndef __STDC_FORMAT_MACROS
13// Ensure PRId64 macro is available
14#define __STDC_FORMAT_MACROS 1
15#endif
16#include <inttypes.h>
17
18#include "chunk.h"
19#include "common.h"
20#include "list.h"
21#include "mem_map.h"
22#include "memtag.h"
23#include "mutex.h"
24#include "options.h"
25#include "stats.h"
26#include "string_utils.h"
27#include "thread_annotations.h"
28#include "tracing.h"
29#include "vector.h"
30
31namespace scudo {
32
33// This allocator wraps the platform allocation primitives, and as such is on
34// the slower side and should preferably be used for larger sized allocations.
35// Blocks allocated will be preceded and followed by a guard page, and hold
36// their own header that is not checksummed: the guard pages and the Combined
37// header should be enough for our purpose.
38
39namespace LargeBlock {
40
41struct alignas(Max<uptr>(A: archSupportsMemoryTagging()
42 ? archMemoryTagGranuleSize()
43 : 1,
44 B: 1U << SCUDO_MIN_ALIGNMENT_LOG)) Header {
45 LargeBlock::Header *Prev;
46 LargeBlock::Header *Next;
47 uptr CommitBase;
48 uptr CommitSize;
49 MemMapT MemMap;
50};
51
52static_assert(sizeof(Header) % (1U << SCUDO_MIN_ALIGNMENT_LOG) == 0, "");
53static_assert(!archSupportsMemoryTagging() ||
54 sizeof(Header) % archMemoryTagGranuleSize() == 0,
55 "");
56
57constexpr uptr getHeaderSize() { return sizeof(Header); }
58
59template <typename Config> static uptr addHeaderTag(uptr Ptr) {
60 if (allocatorSupportsMemoryTagging<Config>())
61 return addFixedTag(Ptr, Tag: 1);
62 return Ptr;
63}
64
65template <typename Config> static Header *getHeader(uptr Ptr) {
66 return reinterpret_cast<Header *>(addHeaderTag<Config>(Ptr)) - 1;
67}
68
69template <typename Config> static Header *getHeader(const void *Ptr) {
70 return getHeader<Config>(reinterpret_cast<uptr>(Ptr));
71}
72
73} // namespace LargeBlock
74
75static inline void unmap(MemMapT &MemMap) { MemMap.unmap(); }
76
77namespace {
78
79struct CachedBlock {
80 static constexpr u16 CacheIndexMax = UINT16_MAX;
81 static constexpr u16 EndOfListVal = CacheIndexMax;
82
83 // We allow a certain amount of fragmentation and part of the fragmented bytes
84 // will be released by `releaseAndZeroPagesToOS()`. This increases the chance
85 // of cache hit rate and reduces the overhead to the RSS at the same time. See
86 // more details in the `MapAllocatorCache::retrieve()` section.
87 //
88 // We arrived at this default value after noticing that mapping in larger
89 // memory regions performs better than releasing memory and forcing a cache
90 // hit. According to the data, it suggests that beyond 4 pages, the release
91 // execution time is longer than the map execution time. In this way,
92 // the default is dependent on the platform.
93 static constexpr uptr MaxReleasedCachePages = 4U;
94
95 uptr CommitBase = 0;
96 uptr CommitSize = 0;
97 uptr BlockBegin = 0;
98 MemMapT MemMap = {};
99 u64 Time = 0;
100 u16 Next = 0;
101 u16 Prev = 0;
102
103 enum CacheFlags : u16 {
104 None = 0,
105 NoAccess = 0x1,
106 };
107 CacheFlags Flags = CachedBlock::None;
108
109 bool isValid() { return CommitBase != 0; }
110
111 void invalidate() { CommitBase = 0; }
112};
113} // namespace
114
115template <typename Config> class MapAllocatorNoCache {
116public:
117 void init(UNUSED s32 ReleaseToOsInterval) {}
118 CachedBlock retrieve(UNUSED uptr MaxAllowedFragmentedBytes, UNUSED uptr Size,
119 UNUSED uptr Alignment, UNUSED uptr HeadersSize,
120 UNUSED uptr &EntryHeaderPos) {
121 return {};
122 }
123 void store(UNUSED Options Options, UNUSED uptr CommitBase,
124 UNUSED uptr CommitSize, UNUSED uptr BlockBegin,
125 UNUSED MemMapT MemMap) {
126 // This should never be called since canCache always returns false.
127 UNREACHABLE(
128 "It is not valid to call store on MapAllocatorNoCache objects.");
129 }
130
131 bool canCache(UNUSED uptr Size) { return false; }
132 void disable() {}
133 void enable() {}
134 void releaseToOS(ReleaseToOS) {}
135 void disableMemoryTagging() {}
136 void unmapTestOnly() {}
137 bool setOption(Option O, UNUSED sptr Value) {
138 if (O == Option::ReleaseInterval || O == Option::MaxCacheEntriesCount ||
139 O == Option::MaxCacheEntrySize)
140 return false;
141 // Not supported by the Secondary Cache, but not an error either.
142 return true;
143 }
144
145 void getStats(UNUSED ScopedString *Str) {
146 Str->append(Format: "Secondary Cache Disabled\n");
147 }
148};
149
150static const uptr MaxUnreleasedCachePages = 4U;
151
152template <typename Config>
153bool mapSecondary(const Options &Options, uptr CommitBase, uptr CommitSize,
154 uptr AllocPos, uptr Flags, MemMapT &MemMap) {
155 Flags |= MAP_RESIZABLE;
156 Flags |= MAP_ALLOWNOMEM;
157
158 const uptr PageSize = getPageSizeCached();
159 if (SCUDO_TRUSTY) {
160 /*
161 * On Trusty we need AllocPos to be usable for shared memory, which cannot
162 * cross multiple mappings. This means we need to split around AllocPos
163 * and not over it. We can only do this if the address is page-aligned.
164 */
165 const uptr TaggedSize = AllocPos - CommitBase;
166 if (useMemoryTagging<Config>(Options) && isAligned(X: TaggedSize, Alignment: PageSize)) {
167 DCHECK_GT(TaggedSize, 0);
168 return MemMap.remap(Addr: CommitBase, Size: TaggedSize, Name: "scudo:secondary",
169 MAP_MEMTAG | Flags) &&
170 MemMap.remap(Addr: AllocPos, Size: CommitSize - TaggedSize, Name: "scudo:secondary",
171 Flags);
172 } else {
173 const uptr RemapFlags =
174 (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
175 return MemMap.remap(Addr: CommitBase, Size: CommitSize, Name: "scudo:secondary",
176 Flags: RemapFlags);
177 }
178 }
179
180 const uptr MaxUnreleasedCacheBytes = MaxUnreleasedCachePages * PageSize;
181 if (useMemoryTagging<Config>(Options) &&
182 CommitSize > MaxUnreleasedCacheBytes) {
183 const uptr UntaggedPos =
184 Max(A: AllocPos, B: CommitBase + MaxUnreleasedCacheBytes);
185 return MemMap.remap(Addr: CommitBase, Size: UntaggedPos - CommitBase, Name: "scudo:secondary",
186 MAP_MEMTAG | Flags) &&
187 MemMap.remap(Addr: UntaggedPos, Size: CommitBase + CommitSize - UntaggedPos,
188 Name: "scudo:secondary", Flags);
189 } else {
190 const uptr RemapFlags =
191 (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
192 return MemMap.remap(Addr: CommitBase, Size: CommitSize, Name: "scudo:secondary", Flags: RemapFlags);
193 }
194}
195
196// Template specialization to avoid producing zero-length array
197template <typename T, size_t Size> class NonZeroLengthArray {
198public:
199 T &operator[](uptr Idx) { return values[Idx]; }
200
201private:
202 T values[Size];
203};
204template <typename T> class NonZeroLengthArray<T, 0> {
205public:
206 T &operator[](uptr UNUSED Idx) { UNREACHABLE("Unsupported!"); }
207};
208
209// The default unmap callback is simply scudo::unmap.
210// In testing, a different unmap callback is used to
211// record information about unmaps in the cache
212template <typename Config, void (*unmapCallBack)(MemMapT &) = unmap>
213class MapAllocatorCache {
214public:
215 void getStats(ScopedString *Str) {
216 ScopedLock L(Mutex);
217 uptr Integral;
218 uptr Fractional;
219 computePercentage(Numerator: SuccessfulRetrieves, Denominator: CallsToRetrieve, Integral: &Integral,
220 Fractional: &Fractional);
221 const s32 Interval = atomic_load_relaxed(A: &ReleaseToOsIntervalMs);
222 Str->append(Format: "Stats: MapAllocatorCache: EntriesCount: %zu, "
223 "MaxEntriesCount: %u, MaxEntrySize: %zu, ReleaseToOsSkips: "
224 "%zu, ReleaseToOsIntervalMs = %d\n",
225 LRUEntries.size(), atomic_load_relaxed(A: &MaxEntriesCount),
226 atomic_load_relaxed(A: &MaxEntrySize),
227 atomic_load_relaxed(A: &ReleaseToOsSkips),
228 Interval >= 0 ? Interval : -1);
229 Str->append(Format: "Stats: CacheRetrievalStats: SuccessRate: %u/%u "
230 "(%zu.%02zu%%)\n",
231 SuccessfulRetrieves, CallsToRetrieve, Integral, Fractional);
232 Str->append(Format: "Cache Entry Info (Most Recent -> Least Recent):\n");
233
234 for (CachedBlock &Entry : LRUEntries) {
235 Str->append(Format: " StartBlockAddress: 0x%zx, EndBlockAddress: 0x%zx, "
236 "BlockSize: %zu%s",
237 Entry.CommitBase, Entry.CommitBase + Entry.CommitSize,
238 Entry.CommitSize, Entry.Time == 0 ? " [R]" : "");
239#if SCUDO_LINUX
240 // getResidentPages only works on linux systems currently.
241 Str->append(Format: ", Resident Pages: %" PRId64 "/%zu\n",
242 getResidentPages(BaseAddress: Entry.CommitBase, Size: Entry.CommitSize),
243 Entry.CommitSize / getPageSizeCached());
244#else
245 Str->append("\n");
246#endif
247 }
248 }
249
250 // Ensure the default maximum specified fits the array.
251 static_assert(Config::getDefaultMaxEntriesCount() <=
252 Config::getEntriesArraySize(),
253 "");
254 // Ensure the cache entry array size fits in the LRU list Next and Prev
255 // index fields
256 static_assert(Config::getEntriesArraySize() <= CachedBlock::CacheIndexMax,
257 "Cache entry array is too large to be indexed.");
258
259 void init(s32 ReleaseToOsInterval) NO_THREAD_SAFETY_ANALYSIS {
260 DCHECK_EQ(LRUEntries.size(), 0U);
261 setOption(O: Option::MaxCacheEntriesCount,
262 Value: static_cast<sptr>(Config::getDefaultMaxEntriesCount()));
263 setOption(O: Option::MaxCacheEntrySize,
264 Value: static_cast<sptr>(Config::getDefaultMaxEntrySize()));
265 // The default value in the cache config has the higher priority.
266 if (Config::getDefaultReleaseToOsIntervalMs() != INT32_MIN)
267 ReleaseToOsInterval = Config::getDefaultReleaseToOsIntervalMs();
268 setOption(O: Option::ReleaseInterval, Value: static_cast<sptr>(ReleaseToOsInterval));
269
270 LRUEntries.clear();
271 LRUEntries.init(Base: Entries, BaseSize: sizeof(Entries));
272 OldestPresentEntry = nullptr;
273
274 AvailEntries.clear();
275 AvailEntries.init(Base: Entries, BaseSize: sizeof(Entries));
276 for (u32 I = 0; I < Config::getEntriesArraySize(); I++)
277 AvailEntries.push_back(X: &Entries[I]);
278 }
279
280 void store(const Options &Options, uptr CommitBase, uptr CommitSize,
281 uptr BlockBegin, MemMapT MemMap) EXCLUDES(Mutex) {
282 DCHECK(canCache(CommitSize));
283
284 const s32 Interval = atomic_load_relaxed(A: &ReleaseToOsIntervalMs);
285 u64 Time;
286 CachedBlock Entry;
287
288 Entry.CommitBase = CommitBase;
289 Entry.CommitSize = CommitSize;
290 Entry.BlockBegin = BlockBegin;
291 Entry.MemMap = MemMap;
292 Entry.Time = UINT64_MAX;
293 Entry.Flags = CachedBlock::None;
294
295 bool MemoryTaggingEnabled = useMemoryTagging<Config>(Options);
296 if (MemoryTaggingEnabled) {
297 if (Interval == 0 && !SCUDO_FUCHSIA) {
298 // Release the memory and make it inaccessible at the same time by
299 // creating a new MAP_NOACCESS mapping on top of the existing mapping.
300 // Fuchsia does not support replacing mappings by creating a new mapping
301 // on top so we just do the two syscalls there.
302 Entry.Time = 0;
303 mapSecondary<Config>(Options, Entry.CommitBase, Entry.CommitSize,
304 Entry.CommitBase, MAP_NOACCESS, Entry.MemMap);
305 } else {
306 Entry.MemMap.setMemoryPermission(Addr: Entry.CommitBase, Size: Entry.CommitSize,
307 MAP_NOACCESS);
308 }
309 Entry.Flags = CachedBlock::NoAccess;
310 }
311
312 // Usually only one entry will be evicted from the cache.
313 // Only in the rare event that the cache shrinks in real-time
314 // due to a decrease in the configurable value MaxEntriesCount
315 // will more than one cache entry be evicted.
316 // The vector is used to save the MemMaps of evicted entries so
317 // that the unmap call can be performed outside the lock
318 Vector<MemMapT, 1U> EvictionMemMaps;
319
320 do {
321 ScopedLock L(Mutex);
322
323 // Time must be computed under the lock to ensure
324 // that the LRU cache remains sorted with respect to
325 // time in a multithreaded environment
326 Time = getMonotonicTimeFast();
327 if (Entry.Time != 0)
328 Entry.Time = Time;
329
330 if (MemoryTaggingEnabled && !useMemoryTagging<Config>(Options)) {
331 // If we get here then memory tagging was disabled in between when we
332 // read Options and when we locked Mutex. We can't insert our entry into
333 // the quarantine or the cache because the permissions would be wrong so
334 // just unmap it.
335 unmapCallBack(Entry.MemMap);
336 break;
337 }
338
339 if (!Config::getQuarantineDisabled() && Config::getQuarantineSize()) {
340 QuarantinePos =
341 (QuarantinePos + 1) % Max(Config::getQuarantineSize(), 1u);
342 if (!Quarantine[QuarantinePos].isValid()) {
343 Quarantine[QuarantinePos] = Entry;
344 return;
345 }
346 CachedBlock PrevEntry = Quarantine[QuarantinePos];
347 Quarantine[QuarantinePos] = Entry;
348 Entry = PrevEntry;
349 }
350
351 // All excess entries are evicted from the cache. Note that when
352 // `MaxEntriesCount` is zero, cache storing shouldn't happen and it's
353 // guarded by the `DCHECK(canCache(CommitSize))` above. As a result, we
354 // won't try to pop `LRUEntries` when it's empty.
355 while (LRUEntries.size() >= atomic_load_relaxed(A: &MaxEntriesCount)) {
356 // Save MemMaps of evicted entries to perform unmap outside of lock
357 CachedBlock *Entry = LRUEntries.back();
358 EvictionMemMaps.push_back(Element: Entry->MemMap);
359 remove(Entry);
360 }
361
362 insert(Entry);
363 } while (0);
364
365 for (MemMapT &EvictMemMap : EvictionMemMaps)
366 unmapCallBack(EvictMemMap);
367
368 if (Interval >= 0) {
369 // It is very likely that multiple threads trying to do a release at the
370 // same time will not actually release any extra elements. Therefore,
371 // let any other thread continue, skipping the release.
372 if (Mutex.tryLock()) {
373 SCUDO_SCOPED_TRACE(
374 GetSecondaryReleaseToOSTraceName(ReleaseToOS::Normal));
375
376 releaseOlderThan(ReleaseTime: Time - static_cast<u64>(Interval) * 1000000);
377 Mutex.unlock();
378 } else
379 atomic_fetch_add(A: &ReleaseToOsSkips, V: 1U, MO: memory_order_relaxed);
380 }
381 }
382
383 CachedBlock retrieve(uptr MaxAllowedFragmentedPages, uptr Size,
384 uptr Alignment, uptr HeadersSize, uptr &EntryHeaderPos)
385 EXCLUDES(Mutex) {
386 const uptr PageSize = getPageSizeCached();
387 // 10% of the requested size proved to be the optimal choice for
388 // retrieving cached blocks after testing several options.
389 constexpr u32 FragmentedBytesDivisor = 10;
390 CachedBlock Entry;
391 EntryHeaderPos = 0;
392 {
393 ScopedLock L(Mutex);
394 CallsToRetrieve++;
395 if (LRUEntries.size() == 0)
396 return {};
397 CachedBlock *RetrievedEntry = nullptr;
398 uptr MinDiff = UINTPTR_MAX;
399
400 // Since allocation sizes don't always match cached memory chunk sizes
401 // we allow some memory to be unused (called fragmented bytes). The
402 // amount of unused bytes is exactly EntryHeaderPos - CommitBase.
403 //
404 // CommitBase CommitBase + CommitSize
405 // V V
406 // +---+------------+-----------------+---+
407 // | | | | |
408 // +---+------------+-----------------+---+
409 // ^ ^ ^
410 // Guard EntryHeaderPos Guard-page-end
411 // page-begin
412 //
413 // [EntryHeaderPos, CommitBase + CommitSize) contains the user data as
414 // well as the header metadata. If EntryHeaderPos - CommitBase exceeds
415 // MaxAllowedFragmentedPages * PageSize, the cached memory chunk is
416 // not considered valid for retrieval.
417 for (CachedBlock &Entry : LRUEntries) {
418 const uptr CommitBase = Entry.CommitBase;
419 const uptr CommitSize = Entry.CommitSize;
420 const uptr AllocPos =
421 roundDown(X: CommitBase + CommitSize - Size, Boundary: Alignment);
422 const uptr HeaderPos = AllocPos - HeadersSize;
423 const uptr MaxAllowedFragmentedBytes =
424 MaxAllowedFragmentedPages * PageSize;
425 if (HeaderPos > CommitBase + CommitSize)
426 continue;
427 // TODO: Remove AllocPos > CommitBase + MaxAllowedFragmentedBytes
428 // and replace with Diff > MaxAllowedFragmentedBytes
429 if (HeaderPos < CommitBase ||
430 AllocPos > CommitBase + MaxAllowedFragmentedBytes) {
431 continue;
432 }
433
434 const uptr Diff = roundDown(X: HeaderPos, Boundary: PageSize) - CommitBase;
435
436 // Keep track of the smallest cached block
437 // that is greater than (AllocSize + HeaderSize)
438 if (Diff >= MinDiff)
439 continue;
440
441 MinDiff = Diff;
442 RetrievedEntry = &Entry;
443 EntryHeaderPos = HeaderPos;
444
445 // Immediately use a cached block if its size is close enough to the
446 // requested size
447 const uptr OptimalFitThesholdBytes =
448 (CommitBase + CommitSize - HeaderPos) / FragmentedBytesDivisor;
449 if (Diff <= OptimalFitThesholdBytes)
450 break;
451 }
452
453 if (RetrievedEntry != nullptr) {
454 Entry = *RetrievedEntry;
455 remove(Entry: RetrievedEntry);
456 SuccessfulRetrieves++;
457 }
458 }
459
460 // The difference between the retrieved memory chunk and the request
461 // size is at most MaxAllowedFragmentedPages
462 //
463 // +- MaxAllowedFragmentedPages * PageSize -+
464 // +--------------------------+-------------+
465 // | | |
466 // +--------------------------+-------------+
467 // \ Bytes to be released / ^
468 // |
469 // (may or may not be committed)
470 //
471 // The maximum number of bytes released to the OS is capped by
472 // MaxReleasedCachePages
473 //
474 // TODO : Consider making MaxReleasedCachePages configurable since
475 // the release to OS API can vary across systems.
476 if (Entry.Time != 0) {
477 const uptr FragmentedBytes =
478 roundDown(X: EntryHeaderPos, Boundary: PageSize) - Entry.CommitBase;
479 const uptr MaxUnreleasedCacheBytes = MaxUnreleasedCachePages * PageSize;
480 if (FragmentedBytes > MaxUnreleasedCacheBytes) {
481 const uptr MaxReleasedCacheBytes =
482 CachedBlock::MaxReleasedCachePages * PageSize;
483 uptr BytesToRelease =
484 roundUp(X: Min<uptr>(A: MaxReleasedCacheBytes,
485 B: FragmentedBytes - MaxUnreleasedCacheBytes),
486 Boundary: PageSize);
487 Entry.MemMap.releaseAndZeroPagesToOS(From: Entry.CommitBase, Size: BytesToRelease);
488 }
489 }
490
491 return Entry;
492 }
493
494 bool canCache(uptr Size) {
495 return atomic_load_relaxed(A: &MaxEntriesCount) != 0U &&
496 Size <= atomic_load_relaxed(A: &MaxEntrySize);
497 }
498
499 bool setOption(Option O, sptr Value) {
500 if (O == Option::ReleaseInterval) {
501 const s32 Interval = Max(
502 Min(static_cast<s32>(Value), Config::getMaxReleaseToOsIntervalMs()),
503 Config::getMinReleaseToOsIntervalMs());
504 atomic_store_relaxed(A: &ReleaseToOsIntervalMs, V: Interval);
505 return true;
506 }
507 if (O == Option::MaxCacheEntriesCount) {
508 if (Value < 0)
509 return false;
510 atomic_store_relaxed(
511 &MaxEntriesCount,
512 Min<u32>(static_cast<u32>(Value), Config::getEntriesArraySize()));
513 return true;
514 }
515 if (O == Option::MaxCacheEntrySize) {
516 atomic_store_relaxed(A: &MaxEntrySize, V: static_cast<uptr>(Value));
517 return true;
518 }
519 // Not supported by the Secondary Cache, but not an error either.
520 return true;
521 }
522
523 void releaseToOS([[maybe_unused]] ReleaseToOS ReleaseType) EXCLUDES(Mutex) {
524 SCUDO_SCOPED_TRACE(GetSecondaryReleaseToOSTraceName(ReleaseType));
525
526 if (ReleaseType == ReleaseToOS::ForceFast) {
527 // Never wait for the lock, always move on if there is already
528 // a release operation in progress.
529 if (Mutex.tryLock()) {
530 releaseOlderThan(UINT64_MAX);
531 Mutex.unlock();
532 }
533 } else {
534 // Since this is a request to release everything, always wait for the
535 // lock so that we guarantee all entries are released after this call.
536 ScopedLock L(Mutex);
537 releaseOlderThan(UINT64_MAX);
538 }
539 }
540
541 void disableMemoryTagging() EXCLUDES(Mutex) {
542 if (Config::getQuarantineDisabled())
543 return;
544
545 ScopedLock L(Mutex);
546 for (u32 I = 0; I != Config::getQuarantineSize(); ++I) {
547 if (Quarantine[I].isValid()) {
548 MemMapT &MemMap = Quarantine[I].MemMap;
549 unmapCallBack(MemMap);
550 Quarantine[I].invalidate();
551 }
552 }
553 QuarantinePos = -1U;
554 }
555
556 void disable() NO_THREAD_SAFETY_ANALYSIS { Mutex.lock(); }
557
558 void enable() NO_THREAD_SAFETY_ANALYSIS { Mutex.unlock(); }
559
560 void unmapTestOnly() { empty(); }
561
562 void releaseOlderThanTestOnly(u64 ReleaseTime) {
563 ScopedLock L(Mutex);
564 releaseOlderThan(ReleaseTime);
565 }
566
567private:
568 void insert(const CachedBlock &Entry) REQUIRES(Mutex) {
569 CachedBlock *AvailEntry = AvailEntries.front();
570 AvailEntries.pop_front();
571
572 *AvailEntry = Entry;
573 LRUEntries.push_front(X: AvailEntry);
574 if (OldestPresentEntry == nullptr && AvailEntry->Time != 0)
575 OldestPresentEntry = AvailEntry;
576 }
577
578 void remove(CachedBlock *Entry) REQUIRES(Mutex) {
579 DCHECK(Entry->isValid());
580 if (OldestPresentEntry == Entry) {
581 OldestPresentEntry = LRUEntries.getPrev(X: Entry);
582 DCHECK(OldestPresentEntry == nullptr || OldestPresentEntry->Time != 0);
583 }
584 LRUEntries.remove(X: Entry);
585 Entry->invalidate();
586 AvailEntries.push_front(X: Entry);
587 }
588
589 void empty() {
590 MemMapT MapInfo[Config::getEntriesArraySize()];
591 uptr N = 0;
592 {
593 ScopedLock L(Mutex);
594
595 for (CachedBlock &Entry : LRUEntries)
596 MapInfo[N++] = Entry.MemMap;
597 LRUEntries.clear();
598 OldestPresentEntry = nullptr;
599 }
600 for (uptr I = 0; I < N; I++) {
601 MemMapT &MemMap = MapInfo[I];
602 unmapCallBack(MemMap);
603 }
604 }
605
606 void releaseOlderThan(u64 ReleaseTime) REQUIRES(Mutex) {
607 SCUDO_SCOPED_TRACE(GetSecondaryReleaseOlderThanTraceName());
608
609 if (!Config::getQuarantineDisabled()) {
610 for (uptr I = 0; I < Config::getQuarantineSize(); I++) {
611 auto &Entry = Quarantine[I];
612 if (!Entry.isValid() || Entry.Time == 0 || Entry.Time > ReleaseTime)
613 continue;
614 Entry.MemMap.releaseAndZeroPagesToOS(Entry.CommitBase,
615 Entry.CommitSize);
616 Entry.Time = 0;
617 }
618 }
619
620 for (CachedBlock *Entry = OldestPresentEntry; Entry != nullptr;
621 Entry = LRUEntries.getPrev(X: Entry)) {
622 DCHECK(Entry->isValid());
623 DCHECK(Entry->Time != 0);
624
625 if (Entry->Time > ReleaseTime) {
626 // All entries are newer than this, so no need to keep scanning.
627 OldestPresentEntry = Entry;
628 return;
629 }
630
631 Entry->MemMap.releaseAndZeroPagesToOS(From: Entry->CommitBase,
632 Size: Entry->CommitSize);
633 Entry->Time = 0;
634 }
635 OldestPresentEntry = nullptr;
636 }
637
638 HybridMutex Mutex;
639 u32 QuarantinePos GUARDED_BY(Mutex) = 0;
640 atomic_u32 MaxEntriesCount = {};
641 atomic_uptr MaxEntrySize = {};
642 atomic_s32 ReleaseToOsIntervalMs = {};
643 u32 CallsToRetrieve GUARDED_BY(Mutex) = 0;
644 u32 SuccessfulRetrieves GUARDED_BY(Mutex) = 0;
645 atomic_uptr ReleaseToOsSkips = {};
646
647 CachedBlock Entries[Config::getEntriesArraySize()] GUARDED_BY(Mutex) = {};
648 NonZeroLengthArray<CachedBlock, Config::getQuarantineSize()>
649 Quarantine GUARDED_BY(Mutex) = {};
650
651 // The oldest entry in the LRUEntries that has Time non-zero.
652 CachedBlock *OldestPresentEntry GUARDED_BY(Mutex) = nullptr;
653 // Cached blocks stored in LRU order
654 DoublyLinkedList<CachedBlock> LRUEntries GUARDED_BY(Mutex);
655 // The unused Entries
656 SinglyLinkedList<CachedBlock> AvailEntries GUARDED_BY(Mutex);
657};
658
659template <typename Config> class MapAllocator {
660public:
661 void init(GlobalStats *S,
662 s32 ReleaseToOsInterval = -1) NO_THREAD_SAFETY_ANALYSIS {
663 DCHECK_EQ(AllocatedBytes, 0U);
664 DCHECK_EQ(FreedBytes, 0U);
665 Cache.init(ReleaseToOsInterval);
666 Stats.init();
667 if (LIKELY(S))
668 S->link(S: &Stats);
669 }
670
671 void *allocate(const Options &Options, uptr Size, uptr AlignmentHint = 0,
672 uptr *BlockEnd = nullptr,
673 FillContentsMode FillContents = NoFill);
674
675 void deallocate(const Options &Options, void *Ptr);
676
677 void *tryAllocateFromCache(const Options &Options, uptr Size, uptr Alignment,
678 uptr *BlockEndPtr, FillContentsMode FillContents);
679
680 static uptr getBlockEnd(void *Ptr) {
681 auto *B = LargeBlock::getHeader<Config>(Ptr);
682 return B->CommitBase + B->CommitSize;
683 }
684
685 static uptr getBlockSize(void *Ptr) {
686 return getBlockEnd(Ptr) - reinterpret_cast<uptr>(Ptr);
687 }
688
689 static uptr getGuardPageSize() {
690 if (Config::getEnableGuardPages())
691 return getPageSizeCached();
692 return 0U;
693 }
694
695 static constexpr uptr getHeadersSize() {
696 return Chunk::getHeaderSize() + LargeBlock::getHeaderSize();
697 }
698
699 void disable() NO_THREAD_SAFETY_ANALYSIS {
700 Mutex.lock();
701 Cache.disable();
702 }
703
704 void enable() NO_THREAD_SAFETY_ANALYSIS {
705 Cache.enable();
706 Mutex.unlock();
707 }
708
709 template <typename F> void iterateOverBlocks(F Callback) const {
710 Mutex.assertHeld();
711
712 for (const auto &H : InUseBlocks) {
713 uptr Ptr = reinterpret_cast<uptr>(&H) + LargeBlock::getHeaderSize();
714 if (allocatorSupportsMemoryTagging<Config>())
715 Ptr = untagPointer(Ptr);
716 Callback(Ptr);
717 }
718 }
719
720 bool canCache(uptr Size) { return Cache.canCache(Size); }
721
722 bool setOption(Option O, sptr Value) { return Cache.setOption(O, Value); }
723
724 void releaseToOS(ReleaseToOS ReleaseType) { Cache.releaseToOS(ReleaseType); }
725
726 void disableMemoryTagging() { Cache.disableMemoryTagging(); }
727
728 void unmapTestOnly() { Cache.unmapTestOnly(); }
729
730 void getStats(ScopedString *Str);
731
732private:
733 typename Config::template CacheT<typename Config::CacheConfig> Cache;
734
735 mutable HybridMutex Mutex;
736 DoublyLinkedList<LargeBlock::Header> InUseBlocks GUARDED_BY(Mutex);
737 uptr AllocatedBytes GUARDED_BY(Mutex) = 0;
738 uptr FreedBytes GUARDED_BY(Mutex) = 0;
739 uptr FragmentedBytes GUARDED_BY(Mutex) = 0;
740 uptr LargestSize GUARDED_BY(Mutex) = 0;
741 u32 NumberOfAllocs GUARDED_BY(Mutex) = 0;
742 u32 NumberOfFrees GUARDED_BY(Mutex) = 0;
743 LocalStats Stats GUARDED_BY(Mutex);
744};
745
746template <typename Config>
747void *
748MapAllocator<Config>::tryAllocateFromCache(const Options &Options, uptr Size,
749 uptr Alignment, uptr *BlockEndPtr,
750 FillContentsMode FillContents) {
751 CachedBlock Entry;
752 uptr EntryHeaderPos;
753 uptr MaxAllowedFragmentedPages = MaxUnreleasedCachePages;
754
755 if (LIKELY(!useMemoryTagging<Config>(Options))) {
756 MaxAllowedFragmentedPages += CachedBlock::MaxReleasedCachePages;
757 } else {
758 // TODO: Enable MaxReleasedCachePages may result in pages for an entry being
759 // partially released and it erases the tag of those pages as well. To
760 // support this feature for MTE, we need to tag those pages again.
761 DCHECK_EQ(MaxAllowedFragmentedPages, MaxUnreleasedCachePages);
762 }
763
764 Entry = Cache.retrieve(MaxAllowedFragmentedPages, Size, Alignment,
765 getHeadersSize(), EntryHeaderPos);
766 if (!Entry.isValid())
767 return nullptr;
768
769 LargeBlock::Header *H = reinterpret_cast<LargeBlock::Header *>(
770 LargeBlock::addHeaderTag<Config>(EntryHeaderPos));
771 bool Zeroed = Entry.Time == 0;
772
773 if (UNLIKELY(Entry.Flags & CachedBlock::NoAccess)) {
774 // NOTE: Flags set to 0 actually restores read-write.
775 Entry.MemMap.setMemoryPermission(Addr: Entry.CommitBase, Size: Entry.CommitSize,
776 /*Flags=*/Flags: 0);
777 }
778
779 if (useMemoryTagging<Config>(Options)) {
780 uptr NewBlockBegin = reinterpret_cast<uptr>(H + 1);
781 if (Zeroed) {
782 storeTags(LargeBlock::addHeaderTag<Config>(Entry.CommitBase),
783 NewBlockBegin);
784 } else if (Entry.BlockBegin < NewBlockBegin) {
785 storeTags(Begin: Entry.BlockBegin, End: NewBlockBegin);
786 } else {
787 storeTags(Begin: untagPointer(Ptr: NewBlockBegin), End: untagPointer(Ptr: Entry.BlockBegin));
788 }
789 }
790
791 H->CommitBase = Entry.CommitBase;
792 H->CommitSize = Entry.CommitSize;
793 H->MemMap = Entry.MemMap;
794
795 const uptr BlockEnd = H->CommitBase + H->CommitSize;
796 if (BlockEndPtr)
797 *BlockEndPtr = BlockEnd;
798 uptr HInt = reinterpret_cast<uptr>(H);
799 if (allocatorSupportsMemoryTagging<Config>())
800 HInt = untagPointer(Ptr: HInt);
801 const uptr PtrInt = HInt + LargeBlock::getHeaderSize();
802 void *Ptr = reinterpret_cast<void *>(PtrInt);
803 if (FillContents && !Zeroed)
804 memset(s: Ptr, c: FillContents == ZeroFill ? 0 : PatternFillByte,
805 n: BlockEnd - PtrInt);
806 {
807 ScopedLock L(Mutex);
808 InUseBlocks.push_back(X: H);
809 AllocatedBytes += H->CommitSize;
810 FragmentedBytes += H->MemMap.getCapacity() - H->CommitSize;
811 NumberOfAllocs++;
812 Stats.add(I: StatAllocated, V: H->CommitSize);
813 Stats.add(I: StatMapped, V: H->MemMap.getCapacity());
814 }
815 return Ptr;
816}
817// As with the Primary, the size passed to this function includes any desired
818// alignment, so that the frontend can align the user allocation. The hint
819// parameter allows us to unmap spurious memory when dealing with larger
820// (greater than a page) alignments on 32-bit platforms.
821// Due to the sparsity of address space available on those platforms, requesting
822// an allocation from the Secondary with a large alignment would end up wasting
823// VA space (even though we are not committing the whole thing), hence the need
824// to trim off some of the reserved space.
825// For allocations requested with an alignment greater than or equal to a page,
826// the committed memory will amount to something close to Size - AlignmentHint
827// (pending rounding and headers).
828template <typename Config>
829void *MapAllocator<Config>::allocate(const Options &Options, uptr Size,
830 uptr Alignment, uptr *BlockEndPtr,
831 FillContentsMode FillContents) {
832 if (Options.get(Opt: OptionBit::AddLargeAllocationSlack))
833 Size += 1UL << SCUDO_MIN_ALIGNMENT_LOG;
834 Alignment = Max(A: Alignment, B: uptr(1U) << SCUDO_MIN_ALIGNMENT_LOG);
835 const uptr PageSize = getPageSizeCached();
836
837 // Note that cached blocks may have aligned address already. Thus we simply
838 // pass the required size (`Size` + `getHeadersSize()`) to do cache look up.
839 const uptr MinNeededSizeForCache = roundUp(X: Size + getHeadersSize(), Boundary: PageSize);
840
841 if (Alignment < PageSize && Cache.canCache(MinNeededSizeForCache)) {
842 void *Ptr = tryAllocateFromCache(Options, Size, Alignment, BlockEndPtr,
843 FillContents);
844 if (Ptr != nullptr)
845 return Ptr;
846 }
847
848 uptr RoundedSize =
849 roundUp(X: roundUp(X: Size, Boundary: Alignment) + getHeadersSize(), Boundary: PageSize);
850 if (UNLIKELY(Alignment > PageSize))
851 RoundedSize += Alignment - PageSize;
852
853 ReservedMemoryT ReservedMemory;
854 const uptr MapSize = RoundedSize + 2 * getGuardPageSize();
855 if (UNLIKELY(!ReservedMemory.create(/*Addr=*/0U, MapSize, nullptr,
856 MAP_ALLOWNOMEM))) {
857 return nullptr;
858 }
859
860 // Take the entire ownership of reserved region.
861 MemMapT MemMap = ReservedMemory.dispatch(Addr: ReservedMemory.getBase(),
862 Size: ReservedMemory.getCapacity());
863 uptr MapBase = MemMap.getBase();
864 uptr CommitBase = MapBase + getGuardPageSize();
865 uptr MapEnd = MapBase + MapSize;
866
867 // In the unlikely event of alignments larger than a page, adjust the amount
868 // of memory we want to commit, and trim the extra memory.
869 if (UNLIKELY(Alignment >= PageSize)) {
870 // For alignments greater than or equal to a page, the user pointer (eg:
871 // the pointer that is returned by the C or C++ allocation APIs) ends up
872 // on a page boundary , and our headers will live in the preceding page.
873 CommitBase =
874 roundUp(X: MapBase + getGuardPageSize() + 1, Boundary: Alignment) - PageSize;
875 // We only trim the extra memory on 32-bit platforms: 64-bit platforms
876 // are less constrained memory wise, and that saves us two syscalls.
877 if (SCUDO_WORDSIZE == 32U) {
878 const uptr NewMapBase = CommitBase - getGuardPageSize();
879 DCHECK_GE(NewMapBase, MapBase);
880 if (NewMapBase != MapBase) {
881 MemMap.unmap(Addr: MapBase, Size: NewMapBase - MapBase);
882 MapBase = NewMapBase;
883 }
884 // CommitBase is past the first guard page, but this computation needs
885 // to include a page where the header lives.
886 const uptr NewMapEnd =
887 CommitBase + PageSize + roundUp(X: Size, Boundary: PageSize) + getGuardPageSize();
888 DCHECK_LE(NewMapEnd, MapEnd);
889 if (NewMapEnd != MapEnd) {
890 MemMap.unmap(Addr: NewMapEnd, Size: MapEnd - NewMapEnd);
891 MapEnd = NewMapEnd;
892 }
893 }
894 }
895
896 const uptr CommitSize = MapEnd - getGuardPageSize() - CommitBase;
897 const uptr AllocPos = roundDown(X: CommitBase + CommitSize - Size, Boundary: Alignment);
898 if (!mapSecondary<Config>(Options, CommitBase, CommitSize, AllocPos, 0,
899 MemMap)) {
900 unmap(MemMap);
901 return nullptr;
902 }
903 const uptr HeaderPos = AllocPos - getHeadersSize();
904 // Make sure that the header is not in the guard page or before the base.
905 DCHECK_GE(HeaderPos, MapBase + getGuardPageSize());
906 LargeBlock::Header *H = reinterpret_cast<LargeBlock::Header *>(
907 LargeBlock::addHeaderTag<Config>(HeaderPos));
908 if (useMemoryTagging<Config>(Options))
909 storeTags(LargeBlock::addHeaderTag<Config>(CommitBase),
910 reinterpret_cast<uptr>(H + 1));
911 H->CommitBase = CommitBase;
912 H->CommitSize = CommitSize;
913 H->MemMap = MemMap;
914 if (BlockEndPtr)
915 *BlockEndPtr = CommitBase + CommitSize;
916 {
917 ScopedLock L(Mutex);
918 InUseBlocks.push_back(X: H);
919 AllocatedBytes += CommitSize;
920 FragmentedBytes += H->MemMap.getCapacity() - CommitSize;
921 if (LargestSize < CommitSize)
922 LargestSize = CommitSize;
923 NumberOfAllocs++;
924 Stats.add(I: StatAllocated, V: CommitSize);
925 Stats.add(I: StatMapped, V: H->MemMap.getCapacity());
926 }
927 return reinterpret_cast<void *>(HeaderPos + LargeBlock::getHeaderSize());
928}
929
930template <typename Config>
931void MapAllocator<Config>::deallocate(const Options &Options, void *Ptr)
932 EXCLUDES(Mutex) {
933 LargeBlock::Header *H = LargeBlock::getHeader<Config>(Ptr);
934 const uptr CommitSize = H->CommitSize;
935 {
936 ScopedLock L(Mutex);
937 InUseBlocks.remove(X: H);
938 FreedBytes += CommitSize;
939 FragmentedBytes -= H->MemMap.getCapacity() - CommitSize;
940 NumberOfFrees++;
941 Stats.sub(I: StatAllocated, V: CommitSize);
942 Stats.sub(I: StatMapped, V: H->MemMap.getCapacity());
943 }
944
945 if (Cache.canCache(H->CommitSize)) {
946 Cache.store(Options, H->CommitBase, H->CommitSize,
947 reinterpret_cast<uptr>(H + 1), H->MemMap);
948 } else {
949 // Note that the `H->MemMap` is stored on the pages managed by itself. Take
950 // over the ownership before unmap() so that any operation along with
951 // unmap() won't touch inaccessible pages.
952 MemMapT MemMap = H->MemMap;
953 unmap(MemMap);
954 }
955}
956
957template <typename Config>
958void MapAllocator<Config>::getStats(ScopedString *Str) EXCLUDES(Mutex) {
959 ScopedLock L(Mutex);
960 Str->append(Format: "Stats: MapAllocator: allocated %u times (%zuK), freed %u times "
961 "(%zuK), remains %u (%zuK) max %zuM, Fragmented %zuK\n",
962 NumberOfAllocs, AllocatedBytes >> 10, NumberOfFrees,
963 FreedBytes >> 10, NumberOfAllocs - NumberOfFrees,
964 (AllocatedBytes - FreedBytes) >> 10, LargestSize >> 20,
965 FragmentedBytes >> 10);
966 Cache.getStats(Str);
967}
968
969} // namespace scudo
970
971#endif // SCUDO_SECONDARY_H_
972