Skip to content

Commit 1e7547e

Browse files
marconeahomescu
authored andcommitted
[scudo] Avoid splitting unaligned allocations on Trusty
Split allocations around the pointer returned by malloc on Trusty. Avoid splitting completely if that pointer is not page-aligned.
1 parent 5784bf8 commit 1e7547e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

compiler-rt/lib/scudo/standalone/secondary.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,29 @@ bool mapSecondary(const Options &Options, uptr CommitBase, uptr CommitSize,
122122
Flags |= MAP_RESIZABLE;
123123
Flags |= MAP_ALLOWNOMEM;
124124

125-
const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached();
125+
const uptr PageSize = getPageSizeCached();
126+
if (SCUDO_TRUSTY) {
127+
/*
128+
* On Trusty we need AllocPos to be usable for shared memory, which cannot
129+
* cross multiple mappings. This means we need to split around AllocPos
130+
* and not over it. We can only do this if the address is page-aligned.
131+
*/
132+
const uptr TaggedSize = AllocPos - CommitBase;
133+
if (useMemoryTagging<Config>(Options) && isAligned(TaggedSize, PageSize)) {
134+
DCHECK_GT(TaggedSize, 0);
135+
return MemMap.remap(CommitBase, TaggedSize, "scudo:secondary",
136+
MAP_MEMTAG | Flags) &&
137+
MemMap.remap(AllocPos, CommitSize - TaggedSize, "scudo:secondary",
138+
Flags);
139+
} else {
140+
const uptr RemapFlags =
141+
(useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
142+
return MemMap.remap(CommitBase, CommitSize, "scudo:secondary",
143+
RemapFlags);
144+
}
145+
}
146+
147+
const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * PageSize;
126148
if (useMemoryTagging<Config>(Options) && CommitSize > MaxUnusedCacheBytes) {
127149
const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
128150
return MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",

0 commit comments

Comments
 (0)