Skip to content

Commit

Permalink
[scudo] Enabled MAP_ALLOWNOMEM for all platforms
Browse files Browse the repository at this point in the history
Back-ported from https://r.android.com/2591905.

Reviewed By: Chia-hungDuan

Differential Revision: https://reviews.llvm.org/D152888
  • Loading branch information
marcone authored and ChiaHungDuan committed Jul 25, 2023
1 parent b31771c commit bf89531
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions compiler-rt/lib/scudo/standalone/secondary.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,22 @@ template <typename Config> class MapAllocatorNoCache {
static const uptr MaxUnusedCachePages = 4U;

template <typename Config>
void mapSecondary(Options Options, uptr CommitBase, uptr CommitSize,
bool mapSecondary(Options Options, uptr CommitBase, uptr CommitSize,
uptr AllocPos, uptr Flags, MemMapT &MemMap) {
Flags |= MAP_RESIZABLE;
Flags |= MAP_ALLOWNOMEM;

const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached();
if (useMemoryTagging<Config>(Options) && CommitSize > MaxUnusedCacheBytes) {
const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",
MAP_RESIZABLE | MAP_MEMTAG | Flags);
MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
"scudo:secondary", MAP_RESIZABLE | Flags);
return MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",
MAP_MEMTAG | Flags) &&
MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
"scudo:secondary", Flags);
} else {
const uptr RemapFlags =
MAP_RESIZABLE | (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) |
Flags;
MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
(useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
return MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
}
}

Expand Down Expand Up @@ -295,8 +297,7 @@ template <typename Config> class MapAllocatorCache {
} else if (Entry.BlockBegin < NewBlockBegin) {
storeTags(Entry.BlockBegin, NewBlockBegin);
} else {
storeTags(untagPointer(NewBlockBegin),
untagPointer(Entry.BlockBegin));
storeTags(untagPointer(NewBlockBegin), untagPointer(Entry.BlockBegin));
}
}
(*H)->CommitBase = Entry.CommitBase;
Expand Down Expand Up @@ -592,7 +593,11 @@ void *MapAllocator<Config>::allocate(Options Options, uptr Size, uptr Alignment,

const uptr CommitSize = MapEnd - PageSize - CommitBase;
const uptr AllocPos = roundDown(CommitBase + CommitSize - Size, Alignment);
mapSecondary<Config>(Options, CommitBase, CommitSize, AllocPos, 0, MemMap);
if (!mapSecondary<Config>(Options, CommitBase, CommitSize, AllocPos, 0,
MemMap)) {
MemMap.unmap(MemMap.getBase(), MemMap.getCapacity());
return nullptr;
}
const uptr HeaderPos =
AllocPos - Chunk::getHeaderSize() - LargeBlock::getHeaderSize();
LargeBlock::Header *H = reinterpret_cast<LargeBlock::Header *>(
Expand Down

0 comments on commit bf89531

Please sign in to comment.