diff --git a/compiler-rt/lib/scudo/standalone/secondary.h b/compiler-rt/lib/scudo/standalone/secondary.h --- a/compiler-rt/lib/scudo/standalone/secondary.h +++ b/compiler-rt/lib/scudo/standalone/secondary.h @@ -86,6 +86,25 @@ static const uptr MaxUnusedCachePages = 4U; +template +void mapSecondary(Options Options, uptr CommitBase, uptr CommitSize, + uptr AllocPos, uptr Flags, MapPlatformData *Data) { + const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached(); + if (useMemoryTagging(Options) && CommitSize > MaxUnusedCacheBytes) { + const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes); + map(reinterpret_cast(CommitBase), UntaggedPos - CommitBase, + "scudo:secondary", MAP_RESIZABLE | MAP_MEMTAG | Flags, Data); + map(reinterpret_cast(UntaggedPos), + CommitBase + CommitSize - UntaggedPos, "scudo:secondary", + MAP_RESIZABLE | Flags, Data); + } else { + map(reinterpret_cast(CommitBase), CommitSize, "scudo:secondary", + MAP_RESIZABLE | (useMemoryTagging(Options) ? MAP_MEMTAG : 0) | + Flags, + Data); + } +} + template class MapAllocatorCache { public: // Ensure the default maximum specified fits the array. @@ -129,9 +148,8 @@ // Fuchsia does not support replacing mappings by creating a new mapping // on top so we just do the two syscalls there. Entry.Time = 0; - map(reinterpret_cast(Entry.CommitBase), Entry.CommitSize, - "scudo:secondary", MAP_RESIZABLE | MAP_NOACCESS | MAP_MEMTAG, - &Entry.Data); + mapSecondary(Options, Entry.CommitBase, Entry.CommitSize, + Entry.CommitBase, MAP_NOACCESS, &Entry.Data); } else { setMemoryPermission(Entry.CommitBase, Entry.CommitSize, MAP_NOACCESS, &Entry.Data); @@ -530,19 +548,7 @@ const uptr CommitSize = MapEnd - PageSize - CommitBase; const uptr AllocPos = roundDownTo(CommitBase + CommitSize - Size, Alignment); - const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached(); - if (useMemoryTagging(Options) && CommitSize > MaxUnusedCacheBytes) { - const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes); - map(reinterpret_cast(CommitBase), UntaggedPos - CommitBase, - "scudo:secondary", MAP_RESIZABLE | MAP_MEMTAG, &Data); - map(reinterpret_cast(UntaggedPos), - CommitBase + CommitSize - UntaggedPos, "scudo:secondary", MAP_RESIZABLE, - &Data); - } else { - map(reinterpret_cast(CommitBase), CommitSize, "scudo:secondary", - MAP_RESIZABLE | (useMemoryTagging(Options) ? MAP_MEMTAG : 0), - &Data); - } + mapSecondary(Options, CommitBase, CommitSize, AllocPos, 0, &Data); const uptr HeaderPos = AllocPos - Chunk::getHeaderSize() - LargeBlock::getHeaderSize(); LargeBlock::Header *H = reinterpret_cast(