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, Flags: %s",
237 Entry.CommitBase, Entry.CommitBase + Entry.CommitSize,
238 Entry.CommitSize, Entry.Time == 0 ? " [R]" : "",
239 Entry.Flags & CachedBlock::NoAccess ? "NoAccess" : "None");
240 const s64 ResidentPages =
241 Entry.MemMap.getResidentPages(From: Entry.CommitBase, Size: Entry.CommitSize);
242
243 if (ResidentPages >= 0) {
244 Str->append(Format: ", Resident Pages: %" PRId64 "/%zu", ResidentPages,
245 Entry.CommitSize / getPageSizeCached());
246 }
247 Str->append(Format: "\n");
248 }
249 }
250
251 // Ensure the default maximum specified fits the array.
252 static_assert(Config::getDefaultMaxEntriesCount() <=
253 Config::getEntriesArraySize(),
254 "");
255 // Ensure the cache entry array size fits in the LRU list Next and Prev
256 // index fields
257 static_assert(Config::getEntriesArraySize() <= CachedBlock::CacheIndexMax,
258 "Cache entry array is too large to be indexed.");
259
260 void init(s32 ReleaseToOsInterval) NO_THREAD_SAFETY_ANALYSIS {
261 DCHECK_EQ(LRUEntries.size(), 0U);
262 setOption(O: Option::MaxCacheEntriesCount,
263 Value: static_cast<sptr>(Config::getDefaultMaxEntriesCount()));
264 setOption(O: Option::MaxCacheEntrySize,
265 Value: static_cast<sptr>(Config::getDefaultMaxEntrySize()));
266 // The default value in the cache config has the higher priority.
267 if (Config::getDefaultReleaseToOsIntervalMs() != INT32_MIN)
268 ReleaseToOsInterval = Config::getDefaultReleaseToOsIntervalMs();
269 setOption(O: Option::ReleaseInterval, Value: static_cast<sptr>(ReleaseToOsInterval));
270
271 LRUEntries.clear();
272 LRUEntries.init(Base: Entries, BaseSize: sizeof(Entries));
273 OldestPresentEntry = nullptr;
274
275 AvailEntries.clear();
276 AvailEntries.init(Base: Entries, BaseSize: sizeof(Entries));
277 for (u32 I = 0; I < Config::getEntriesArraySize(); I++)
278 AvailEntries.push_back(X: &Entries[I]);
279 }
280
281 void store(const Options &Options, uptr CommitBase, uptr CommitSize,
282 uptr BlockBegin, MemMapT MemMap) EXCLUDES(Mutex) {
283 DCHECK(canCache(CommitSize));
284
285 const s32 Interval = atomic_load_relaxed(A: &ReleaseToOsIntervalMs);
286 u64 Time;
287 CachedBlock Entry;
288
289 Entry.CommitBase = CommitBase;
290 Entry.CommitSize = CommitSize;
291 Entry.BlockBegin = BlockBegin;
292 Entry.MemMap = MemMap;
293 Entry.Time = UINT64_MAX;
294 Entry.Flags = CachedBlock::None;
295
296 bool MemoryTaggingEnabled = useMemoryTagging<Config>(Options);
297 if (MemoryTaggingEnabled) {
298 if (Interval == 0 && !SCUDO_FUCHSIA) {
299 // Release the memory and make it inaccessible at the same time by
300 // creating a new MAP_NOACCESS mapping on top of the existing mapping.
301 // Fuchsia does not support replacing mappings by creating a new mapping
302 // on top so we just do the two syscalls there.
303 Entry.Time = 0;
304 mapSecondary<Config>(Options, Entry.CommitBase, Entry.CommitSize,
305 Entry.CommitBase, MAP_NOACCESS, Entry.MemMap);
306 } else {
307 Entry.MemMap.setMemoryPermission(Addr: Entry.CommitBase, Size: Entry.CommitSize,
308 MAP_NOACCESS);
309 }
310 Entry.Flags = CachedBlock::NoAccess;
311 }
312
313 // Usually only one entry will be evicted from the cache.
314 // Only in the rare event that the cache shrinks in real-time
315 // due to a decrease in the configurable value MaxEntriesCount
316 // will more than one cache entry be evicted.
317 // The vector is used to save the MemMaps of evicted entries so
318 // that the unmap call can be performed outside the lock
319 Vector<MemMapT, 1U> EvictionMemMaps;
320
321 do {
322 ScopedLock L(Mutex);
323
324 // Time must be computed under the lock to ensure
325 // that the LRU cache remains sorted with respect to
326 // time in a multithreaded environment
327 Time = getMonotonicTimeFast();
328 if (Entry.Time != 0)
329 Entry.Time = Time;
330
331 if (MemoryTaggingEnabled && !useMemoryTagging<Config>(Options)) {
332 // If we get here then memory tagging was disabled in between when we
333 // read Options and when we locked Mutex. We can't insert our entry into
334 // the quarantine or the cache because the permissions would be wrong so
335 // just unmap it.
336 unmapCallBack(Entry.MemMap);
337 break;
338 }
339
340 if (!Config::getQuarantineDisabled() && Config::getQuarantineSize()) {
341 QuarantinePos =
342 (QuarantinePos + 1) % Max(Config::getQuarantineSize(), 1u);
343 if (!Quarantine[QuarantinePos].isValid()) {
344 Quarantine[QuarantinePos] = Entry;
345 return;
346 }
347 CachedBlock PrevEntry = Quarantine[QuarantinePos];
348 Quarantine[QuarantinePos] = Entry;
349 Entry = PrevEntry;
350 }
351
352 // All excess entries are evicted from the cache. Note that when
353 // `MaxEntriesCount` is zero, cache storing shouldn't happen and it's
354 // guarded by the `DCHECK(canCache(CommitSize))` above. As a result, we
355 // won't try to pop `LRUEntries` when it's empty.
356 while (LRUEntries.size() >= atomic_load_relaxed(A: &MaxEntriesCount)) {
357 // Save MemMaps of evicted entries to perform unmap outside of lock
358 CachedBlock *Entry = LRUEntries.back();
359 EvictionMemMaps.push_back(Element: Entry->MemMap);
360 remove(Entry);
361 }
362
363 insert(Entry);
364 } while (0);
365
366 for (MemMapT &EvictMemMap : EvictionMemMaps)
367 unmapCallBack(EvictMemMap);
368
369 if (Interval >= 0) {
370 // It is very likely that multiple threads trying to do a release at the
371 // same time will not actually release any extra elements. Therefore,
372 // let any other thread continue, skipping the release.
373 if (Mutex.tryLock()) {
374 SCUDO_SCOPED_TRACE(
375 GetSecondaryReleaseToOSTraceName(ReleaseToOS::Normal));
376
377 releaseOlderThan(ReleaseTime: Time - static_cast<u64>(Interval) * 1000000);
378 Mutex.unlock();
379 } else
380 atomic_fetch_add(A: &ReleaseToOsSkips, V: 1U, MO: memory_order_relaxed);
381 }
382 }
383
384 CachedBlock retrieve(uptr MaxAllowedFragmentedPages, uptr Size,
385 uptr Alignment, uptr HeadersSize, uptr &EntryHeaderPos)
386 EXCLUDES(Mutex) {
387 const uptr PageSize = getPageSizeCached();
388 // 10% of the requested size proved to be the optimal choice for
389 // retrieving cached blocks after testing several options.
390 constexpr u32 FragmentedBytesDivisor = 10;
391 CachedBlock Entry;
392 EntryHeaderPos = 0;
393 {
394 ScopedLock L(Mutex);
395 CallsToRetrieve++;
396 if (LRUEntries.size() == 0)
397 return {};
398 CachedBlock *RetrievedEntry = nullptr;
399 uptr MinDiff = UINTPTR_MAX;
400
401 // Since allocation sizes don't always match cached memory chunk sizes
402 // we allow some memory to be unused (called fragmented bytes). The
403 // amount of unused bytes is exactly EntryHeaderPos - CommitBase.
404 //
405 // CommitBase CommitBase + CommitSize
406 // V V
407 // +---+------------+-----------------+---+
408 // | | | | |
409 // +---+------------+-----------------+---+
410 // ^ ^ ^
411 // Guard EntryHeaderPos Guard-page-end
412 // page-begin
413 //
414 // [EntryHeaderPos, CommitBase + CommitSize) contains the user data as
415 // well as the header metadata. If EntryHeaderPos - CommitBase exceeds
416 // MaxAllowedFragmentedPages * PageSize, the cached memory chunk is
417 // not considered valid for retrieval.
418 for (CachedBlock &Entry : LRUEntries) {
419 const uptr CommitBase = Entry.CommitBase;
420 const uptr CommitSize = Entry.CommitSize;
421 const uptr AllocPos =
422 roundDown(X: CommitBase + CommitSize - Size, Boundary: Alignment);
423 const uptr HeaderPos = AllocPos - HeadersSize;
424 const uptr MaxAllowedFragmentedBytes =
425 MaxAllowedFragmentedPages * PageSize;
426 if (HeaderPos > CommitBase + CommitSize)
427 continue;
428 // TODO: Remove AllocPos > CommitBase + MaxAllowedFragmentedBytes
429 // and replace with Diff > MaxAllowedFragmentedBytes
430 if (HeaderPos < CommitBase ||
431 AllocPos > CommitBase + MaxAllowedFragmentedBytes) {
432 continue;
433 }
434
435 const uptr Diff = roundDown(X: HeaderPos, Boundary: PageSize) - CommitBase;
436
437 // Keep track of the smallest cached block
438 // that is greater than (AllocSize + HeaderSize)
439 if (Diff >= MinDiff)
440 continue;
441
442 MinDiff = Diff;
443 RetrievedEntry = &Entry;
444 EntryHeaderPos = HeaderPos;
445
446 // Immediately use a cached block if its size is close enough to the
447 // requested size
448 const uptr OptimalFitThesholdBytes =
449 (CommitBase + CommitSize - HeaderPos) / FragmentedBytesDivisor;
450 if (Diff <= OptimalFitThesholdBytes)
451 break;
452 }
453
454 if (RetrievedEntry != nullptr) {
455 Entry = *RetrievedEntry;
456 remove(Entry: RetrievedEntry);
457 SuccessfulRetrieves++;
458 }
459 }
460
461 // The difference between the retrieved memory chunk and the request
462 // size is at most MaxAllowedFragmentedPages
463 //
464 // +- MaxAllowedFragmentedPages * PageSize -+
465 // +--------------------------+-------------+
466 // | | |
467 // +--------------------------+-------------+
468 // \ Bytes to be released / ^
469 // |
470 // (may or may not be committed)
471 //
472 // The maximum number of bytes released to the OS is capped by
473 // MaxReleasedCachePages
474 //
475 // TODO : Consider making MaxReleasedCachePages configurable since
476 // the release to OS API can vary across systems.
477 if (Entry.Time != 0) {
478 const uptr FragmentedBytes =
479 roundDown(X: EntryHeaderPos, Boundary: PageSize) - Entry.CommitBase;
480 const uptr MaxUnreleasedCacheBytes = MaxUnreleasedCachePages * PageSize;
481 if (FragmentedBytes > MaxUnreleasedCacheBytes) {
482 const uptr MaxReleasedCacheBytes =
483 CachedBlock::MaxReleasedCachePages * PageSize;
484 uptr BytesToRelease =
485 roundUp(X: Min<uptr>(A: MaxReleasedCacheBytes,
486 B: FragmentedBytes - MaxUnreleasedCacheBytes),
487 Boundary: PageSize);
488 Entry.MemMap.releaseAndZeroPagesToOS(From: Entry.CommitBase, Size: BytesToRelease);
489 }
490 }
491
492 return Entry;
493 }
494
495 bool canCache(uptr Size) {
496 return atomic_load_relaxed(A: &MaxEntriesCount) != 0U &&
497 Size <= atomic_load_relaxed(A: &MaxEntrySize);
498 }
499
500 bool setOption(Option O, sptr Value) {
501 if (O == Option::ReleaseInterval) {
502 const s32 Interval = Max(
503 Min(static_cast<s32>(Value), Config::getMaxReleaseToOsIntervalMs()),
504 Config::getMinReleaseToOsIntervalMs());
505 atomic_store_relaxed(A: &ReleaseToOsIntervalMs, V: Interval);
506 return true;
507 }
508 if (O == Option::MaxCacheEntriesCount) {
509 if (Value < 0)
510 return false;
511 atomic_store_relaxed(
512 &MaxEntriesCount,
513 Min<u32>(static_cast<u32>(Value), Config::getEntriesArraySize()));
514 return true;
515 }
516 if (O == Option::MaxCacheEntrySize) {
517 atomic_store_relaxed(A: &MaxEntrySize, V: static_cast<uptr>(Value));
518 return true;
519 }
520 // Not supported by the Secondary Cache, but not an error either.
521 return true;
522 }
523
524 void releaseToOS([[maybe_unused]] ReleaseToOS ReleaseType) EXCLUDES(Mutex) {
525 SCUDO_SCOPED_TRACE(GetSecondaryReleaseToOSTraceName(ReleaseType));
526
527 if (ReleaseType == ReleaseToOS::ForceFast) {
528 // Never wait for the lock, always move on if there is already
529 // a release operation in progress.
530 if (Mutex.tryLock()) {
531 releaseOlderThan(UINT64_MAX);
532 Mutex.unlock();
533 }
534 } else {
535 // Since this is a request to release everything, always wait for the
536 // lock so that we guarantee all entries are released after this call.
537 ScopedLock L(Mutex);
538 releaseOlderThan(UINT64_MAX);
539 }
540 }
541
542 void disableMemoryTagging() EXCLUDES(Mutex) {
543 if (Config::getQuarantineDisabled())
544 return;
545
546 ScopedLock L(Mutex);
547 for (u32 I = 0; I != Config::getQuarantineSize(); ++I) {
548 if (Quarantine[I].isValid()) {
549 MemMapT &MemMap = Quarantine[I].MemMap;
550 unmapCallBack(MemMap);
551 Quarantine[I].invalidate();
552 }
553 }
554 QuarantinePos = -1U;
555 }
556
557 void disable() NO_THREAD_SAFETY_ANALYSIS { Mutex.lock(); }
558
559 void enable() NO_THREAD_SAFETY_ANALYSIS { Mutex.unlock(); }
560
561 void unmapTestOnly() { empty(); }
562
563 void releaseOlderThanTestOnly(u64 ReleaseTime) {
564 ScopedLock L(Mutex);
565 releaseOlderThan(ReleaseTime);
566 }
567
568private:
569 void insert(const CachedBlock &Entry) REQUIRES(Mutex) {
570 CachedBlock *AvailEntry = AvailEntries.front();
571 AvailEntries.pop_front();
572
573 *AvailEntry = Entry;
574 LRUEntries.push_front(X: AvailEntry);
575 if (OldestPresentEntry == nullptr && AvailEntry->Time != 0)
576 OldestPresentEntry = AvailEntry;
577 }
578
579 void remove(CachedBlock *Entry) REQUIRES(Mutex) {
580 DCHECK(Entry->isValid());
581 if (OldestPresentEntry == Entry) {
582 OldestPresentEntry = LRUEntries.getPrev(X: Entry);
583 DCHECK(OldestPresentEntry == nullptr || OldestPresentEntry->Time != 0);
584 }
585 LRUEntries.remove(X: Entry);
586 Entry->invalidate();
587 AvailEntries.push_front(X: Entry);
588 }
589
590 void empty() {
591 MemMapT MapInfo[Config::getEntriesArraySize()];
592 uptr N = 0;
593 {
594 ScopedLock L(Mutex);
595
596 for (CachedBlock &Entry : LRUEntries)
597 MapInfo[N++] = Entry.MemMap;
598 LRUEntries.clear();
599 OldestPresentEntry = nullptr;
600 }
601 for (uptr I = 0; I < N; I++) {
602 MemMapT &MemMap = MapInfo[I];
603 unmapCallBack(MemMap);
604 }
605 }
606
607 void releaseOlderThan(u64 ReleaseTime) REQUIRES(Mutex) {
608 SCUDO_SCOPED_TRACE(GetSecondaryReleaseOlderThanTraceName());
609
610 if (!Config::getQuarantineDisabled()) {
611 for (uptr I = 0; I < Config::getQuarantineSize(); I++) {
612 auto &Entry = Quarantine[I];
613 if (!Entry.isValid() || Entry.Time == 0 || Entry.Time > ReleaseTime)
614 continue;
615 Entry.MemMap.releaseAndZeroPagesToOS(Entry.CommitBase,
616 Entry.CommitSize);
617 Entry.Time = 0;
618 }
619 }
620
621 for (CachedBlock *Entry = OldestPresentEntry; Entry != nullptr;
622 Entry = LRUEntries.getPrev(X: Entry)) {
623 DCHECK(Entry->isValid());
624 DCHECK(Entry->Time != 0);
625
626 if (Entry->Time > ReleaseTime) {
627 // All entries are newer than this, so no need to keep scanning.
628 OldestPresentEntry = Entry;
629 return;
630 }
631
632 Entry->MemMap.releaseAndZeroPagesToOS(From: Entry->CommitBase,
633 Size: Entry->CommitSize);
634 Entry->Time = 0;
635 }
636 OldestPresentEntry = nullptr;
637 }
638
639 HybridMutex Mutex;
640 u32 QuarantinePos GUARDED_BY(Mutex) = 0;
641 atomic_u32 MaxEntriesCount = {};
642 atomic_uptr MaxEntrySize = {};
643 atomic_s32 ReleaseToOsIntervalMs = {};
644 u32 CallsToRetrieve GUARDED_BY(Mutex) = 0;
645 u32 SuccessfulRetrieves GUARDED_BY(Mutex) = 0;
646 atomic_uptr ReleaseToOsSkips = {};
647
648 CachedBlock Entries[Config::getEntriesArraySize()] GUARDED_BY(Mutex) = {};
649 NonZeroLengthArray<CachedBlock, Config::getQuarantineSize()>
650 Quarantine GUARDED_BY(Mutex) = {};
651
652 // The oldest entry in the LRUEntries that has Time non-zero.
653 CachedBlock *OldestPresentEntry GUARDED_BY(Mutex) = nullptr;
654 // Cached blocks stored in LRU order
655 DoublyLinkedList<CachedBlock> LRUEntries GUARDED_BY(Mutex);
656 // The unused Entries
657 SinglyLinkedList<CachedBlock> AvailEntries GUARDED_BY(Mutex);
658};
659
660template <typename Config> class MapAllocator {
661public:
662 void init(GlobalStats *S,
663 s32 ReleaseToOsInterval = -1) NO_THREAD_SAFETY_ANALYSIS {
664 DCHECK_EQ(AllocatedBytes, 0U);
665 DCHECK_EQ(FreedBytes, 0U);
666 Cache.init(ReleaseToOsInterval);
667 Stats.init();
668 if (LIKELY(S))
669 S->link(S: &Stats);
670 }
671
672 void *allocate(const Options &Options, uptr Size, uptr AlignmentHint = 0,
673 uptr *BlockEnd = nullptr,
674 FillContentsMode FillContents = NoFill);
675
676 void deallocate(const Options &Options, void *Ptr);
677
678 void *tryAllocateFromCache(const Options &Options, uptr Size, uptr Alignment,
679 uptr *BlockEndPtr, FillContentsMode FillContents);
680
681 static uptr getBlockEnd(void *Ptr) {
682 auto *B = LargeBlock::getHeader<Config>(Ptr);
683 return B->CommitBase + B->CommitSize;
684 }
685
686 static uptr getBlockSize(void *Ptr) {
687 return getBlockEnd(Ptr) - reinterpret_cast<uptr>(Ptr);
688 }
689
690 static uptr getGuardPageSize() {
691 if (Config::getEnableGuardPages())
692 return getPageSizeCached();
693 return 0U;
694 }
695
696 static constexpr uptr getHeadersSize() {
697 return Chunk::getHeaderSize() + LargeBlock::getHeaderSize();
698 }
699
700 void disable() NO_THREAD_SAFETY_ANALYSIS {
701 Mutex.lock();
702 Cache.disable();
703 }
704
705 void enable() NO_THREAD_SAFETY_ANALYSIS {
706 Cache.enable();
707 Mutex.unlock();
708 }
709
710 template <typename F> void iterateOverBlocks(F Callback) const {
711 Mutex.assertHeld();
712
713 for (const auto &H : InUseBlocks) {
714 uptr Ptr = reinterpret_cast<uptr>(&H) + LargeBlock::getHeaderSize();
715 if (allocatorSupportsMemoryTagging<Config>())
716 Ptr = untagPointer(Ptr);
717 Callback(Ptr);
718 }
719 }
720
721 bool canCache(uptr Size) { return Cache.canCache(Size); }
722
723 bool setOption(Option O, sptr Value) { return Cache.setOption(O, Value); }
724
725 void releaseToOS(ReleaseToOS ReleaseType) { Cache.releaseToOS(ReleaseType); }
726
727 void disableMemoryTagging() { Cache.disableMemoryTagging(); }
728
729 void unmapTestOnly() { Cache.unmapTestOnly(); }
730
731 void getStats(ScopedString *Str);
732
733private:
734 typename Config::template CacheT<typename Config::CacheConfig> Cache;
735
736 mutable HybridMutex Mutex;
737 DoublyLinkedList<LargeBlock::Header> InUseBlocks GUARDED_BY(Mutex);
738 uptr AllocatedBytes GUARDED_BY(Mutex) = 0;
739 uptr FreedBytes GUARDED_BY(Mutex) = 0;
740 uptr FragmentedBytes GUARDED_BY(Mutex) = 0;
741 uptr LargestSize GUARDED_BY(Mutex) = 0;
742 u32 NumberOfAllocs GUARDED_BY(Mutex) = 0;
743 u32 NumberOfFrees GUARDED_BY(Mutex) = 0;
744 LocalStats Stats GUARDED_BY(Mutex);
745};
746
747template <typename Config>
748void *
749MapAllocator<Config>::tryAllocateFromCache(const Options &Options, uptr Size,
750 uptr Alignment, uptr *BlockEndPtr,
751 FillContentsMode FillContents) {
752 CachedBlock Entry;
753 uptr EntryHeaderPos;
754 uptr MaxAllowedFragmentedPages = MaxUnreleasedCachePages;
755
756 if (LIKELY(!useMemoryTagging<Config>(Options))) {
757 MaxAllowedFragmentedPages += CachedBlock::MaxReleasedCachePages;
758 } else {
759 // TODO: Enable MaxReleasedCachePages may result in pages for an entry being
760 // partially released and it erases the tag of those pages as well. To
761 // support this feature for MTE, we need to tag those pages again.
762 DCHECK_EQ(MaxAllowedFragmentedPages, MaxUnreleasedCachePages);
763 }
764
765 Entry = Cache.retrieve(MaxAllowedFragmentedPages, Size, Alignment,
766 getHeadersSize(), EntryHeaderPos);
767 if (!Entry.isValid())
768 return nullptr;
769
770 LargeBlock::Header *H = reinterpret_cast<LargeBlock::Header *>(
771 LargeBlock::addHeaderTag<Config>(EntryHeaderPos));
772 bool Zeroed = Entry.Time == 0;
773
774 if (UNLIKELY(Entry.Flags & CachedBlock::NoAccess)) {
775 // NOTE: Flags set to 0 actually restores read-write.
776 Entry.MemMap.setMemoryPermission(Addr: Entry.CommitBase, Size: Entry.CommitSize,
777 /*Flags=*/Flags: 0);
778 }
779
780 if (useMemoryTagging<Config>(Options)) {
781 uptr NewBlockBegin = reinterpret_cast<uptr>(H + 1);
782 if (Zeroed || (Entry.BlockBegin < NewBlockBegin)) {
783 storeTags(Begin: reinterpret_cast<uptr>(H), End: NewBlockBegin);
784 } else {
785 storeTags(Begin: untagPointer(Ptr: NewBlockBegin), End: untagPointer(Ptr: Entry.BlockBegin));
786 storeTags(Begin: reinterpret_cast<uptr>(H), End: NewBlockBegin);
787 }
788 }
789
790 H->CommitBase = Entry.CommitBase;
791 H->CommitSize = Entry.CommitSize;
792 H->MemMap = Entry.MemMap;
793
794 const uptr BlockEnd = H->CommitBase + H->CommitSize;
795 if (BlockEndPtr)
796 *BlockEndPtr = BlockEnd;
797 uptr HInt = reinterpret_cast<uptr>(H);
798 if (allocatorSupportsMemoryTagging<Config>())
799 HInt = untagPointer(Ptr: HInt);
800 const uptr PtrInt = HInt + LargeBlock::getHeaderSize();
801 void *Ptr = reinterpret_cast<void *>(PtrInt);
802 if (FillContents && !Zeroed)
803 memset(s: Ptr, c: FillContents == ZeroFill ? 0 : PatternFillByte,
804 n: BlockEnd - PtrInt);
805 {
806 ScopedLock L(Mutex);
807 InUseBlocks.push_back(X: H);
808 AllocatedBytes += H->CommitSize;
809 FragmentedBytes += H->MemMap.getCapacity() - H->CommitSize;
810 NumberOfAllocs++;
811 Stats.add(I: StatAllocated, V: H->CommitSize);
812 Stats.add(I: StatMapped, V: H->MemMap.getCapacity());
813 }
814 return Ptr;
815}
816// As with the Primary, the size passed to this function includes any desired
817// alignment, so that the frontend can align the user allocation. The hint
818// parameter allows us to unmap spurious memory when dealing with larger
819// (greater than a page) alignments on 32-bit platforms.
820// Due to the sparsity of address space available on those platforms, requesting
821// an allocation from the Secondary with a large alignment would end up wasting
822// VA space (even though we are not committing the whole thing), hence the need
823// to trim off some of the reserved space.
824// For allocations requested with an alignment greater than or equal to a page,
825// the committed memory will amount to something close to Size - AlignmentHint
826// (pending rounding and headers).
827template <typename Config>
828void *MapAllocator<Config>::allocate(const Options &Options, uptr Size,
829 uptr Alignment, uptr *BlockEndPtr,
830 FillContentsMode FillContents) {
831 if (Options.get(Opt: OptionBit::AddLargeAllocationSlack))
832 Size += 1UL << SCUDO_MIN_ALIGNMENT_LOG;
833 Alignment = Max(A: Alignment, B: uptr(1U) << SCUDO_MIN_ALIGNMENT_LOG);
834 const uptr PageSize = getPageSizeCached();
835
836 // Note that cached blocks may have aligned address already. Thus we simply
837 // pass the required size (`Size` + `getHeadersSize()`) to do cache look up.
838 const uptr MinNeededSizeForCache = roundUp(X: Size + getHeadersSize(), Boundary: PageSize);
839
840 if (Alignment < PageSize && Cache.canCache(MinNeededSizeForCache)) {
841 void *Ptr = tryAllocateFromCache(Options, Size, Alignment, BlockEndPtr,
842 FillContents);
843 if (Ptr != nullptr)
844 return Ptr;
845 }
846
847 uptr RoundedSize =
848 roundUp(X: roundUp(X: Size, Boundary: Alignment) + getHeadersSize(), Boundary: PageSize);
849 if (UNLIKELY(Alignment > PageSize))
850 RoundedSize += Alignment - PageSize;
851
852 ReservedMemoryT ReservedMemory;
853 const uptr MapSize = RoundedSize + 2 * getGuardPageSize();
854 if (UNLIKELY(!ReservedMemory.create(/*Addr=*/0U, MapSize, nullptr,
855 MAP_ALLOWNOMEM))) {
856 return nullptr;
857 }
858
859 // Take the entire ownership of reserved region.
860 MemMapT MemMap = ReservedMemory.dispatch(Addr: ReservedMemory.getBase(),
861 Size: ReservedMemory.getCapacity());
862 uptr MapBase = MemMap.getBase();
863 uptr CommitBase = MapBase + getGuardPageSize();
864 uptr MapEnd = MapBase + MapSize;
865
866 // In the unlikely event of alignments larger than a page, adjust the amount
867 // of memory we want to commit, and trim the extra memory.
868 if (UNLIKELY(Alignment >= PageSize)) {
869 // For alignments greater than or equal to a page, the user pointer (eg:
870 // the pointer that is returned by the C or C++ allocation APIs) ends up
871 // on a page boundary , and our headers will live in the preceding page.
872 CommitBase =
873 roundUp(X: MapBase + getGuardPageSize() + 1, Boundary: Alignment) - PageSize;
874 // We only trim the extra memory on 32-bit platforms: 64-bit platforms
875 // are less constrained memory wise, and that saves us two syscalls.
876 if (SCUDO_WORDSIZE == 32U) {
877 const uptr NewMapBase = CommitBase - getGuardPageSize();
878 DCHECK_GE(NewMapBase, MapBase);
879 if (NewMapBase != MapBase) {
880 MemMap.unmap(Addr: MapBase, Size: NewMapBase - MapBase);
881 MapBase = NewMapBase;
882 }
883 // CommitBase is past the first guard page, but this computation needs
884 // to include a page where the header lives.
885 const uptr NewMapEnd =
886 CommitBase + PageSize + roundUp(X: Size, Boundary: PageSize) + getGuardPageSize();
887 DCHECK_LE(NewMapEnd, MapEnd);
888 if (NewMapEnd != MapEnd) {
889 MemMap.unmap(Addr: NewMapEnd, Size: MapEnd - NewMapEnd);
890 MapEnd = NewMapEnd;
891 }
892 }
893 }
894
895 const uptr CommitSize = MapEnd - getGuardPageSize() - CommitBase;
896 const uptr AllocPos = roundDown(X: CommitBase + CommitSize - Size, Boundary: Alignment);
897 if (!mapSecondary<Config>(Options, CommitBase, CommitSize, AllocPos, 0,
898 MemMap)) {
899 unmap(MemMap);
900 return nullptr;
901 }
902 const uptr HeaderPos = AllocPos - getHeadersSize();
903 // Make sure that the header is not in the guard page or before the base.
904 DCHECK_GE(HeaderPos, MapBase + getGuardPageSize());
905 LargeBlock::Header *H = reinterpret_cast<LargeBlock::Header *>(
906 LargeBlock::addHeaderTag<Config>(HeaderPos));
907 if (useMemoryTagging<Config>(Options))
908 storeTags(Begin: reinterpret_cast<uptr>(H), End: reinterpret_cast<uptr>(H + 1));
909 H->CommitBase = CommitBase;
910 H->CommitSize = CommitSize;
911 H->MemMap = MemMap;
912 if (BlockEndPtr)
913 *BlockEndPtr = CommitBase + CommitSize;
914 {
915 ScopedLock L(Mutex);
916 InUseBlocks.push_back(X: H);
917 AllocatedBytes += CommitSize;
918 FragmentedBytes += H->MemMap.getCapacity() - CommitSize;
919 if (LargestSize < CommitSize)
920 LargestSize = CommitSize;
921 NumberOfAllocs++;
922 Stats.add(I: StatAllocated, V: CommitSize);
923 Stats.add(I: StatMapped, V: H->MemMap.getCapacity());
924 }
925 return reinterpret_cast<void *>(HeaderPos + LargeBlock::getHeaderSize());
926}
927
928template <typename Config>
929void MapAllocator<Config>::deallocate(const Options &Options, void *Ptr)
930 EXCLUDES(Mutex) {
931 LargeBlock::Header *H = LargeBlock::getHeader<Config>(Ptr);
932 const uptr CommitSize = H->CommitSize;
933 {
934 ScopedLock L(Mutex);
935 InUseBlocks.remove(X: H);
936 FreedBytes += CommitSize;
937 FragmentedBytes -= H->MemMap.getCapacity() - CommitSize;
938 NumberOfFrees++;
939 Stats.sub(I: StatAllocated, V: CommitSize);
940 Stats.sub(I: StatMapped, V: H->MemMap.getCapacity());
941 }
942
943 if (Cache.canCache(H->CommitSize)) {
944 Cache.store(Options, H->CommitBase, H->CommitSize,
945 reinterpret_cast<uptr>(H + 1), H->MemMap);
946 } else {
947 // Note that the `H->MemMap` is stored on the pages managed by itself. Take
948 // over the ownership before unmap() so that any operation along with
949 // unmap() won't touch inaccessible pages.
950 MemMapT MemMap = H->MemMap;
951 unmap(MemMap);
952 }
953}
954
955template <typename Config>
956void MapAllocator<Config>::getStats(ScopedString *Str) EXCLUDES(Mutex) {
957 ScopedLock L(Mutex);
958 Str->append(Format: "Stats: MapAllocator: allocated %u times (%zuK), freed %u times "
959 "(%zuK), remains %u (%zuK) max %zuM, Fragmented %zuK\n",
960 NumberOfAllocs, AllocatedBytes >> 10, NumberOfFrees,
961 FreedBytes >> 10, NumberOfAllocs - NumberOfFrees,
962 (AllocatedBytes - FreedBytes) >> 10, LargestSize >> 20,
963 FragmentedBytes >> 10);
964 Cache.getStats(Str);
965}
966
967} // namespace scudo
968
969#endif // SCUDO_SECONDARY_H_
970