Skip to content

Commit 8c94004

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 691b12a commit 8c94004

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,30 @@ 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+
DCHECK_LT(TaggedSize, CommitSize);
136+
return MemMap.remap(CommitBase, TaggedSize, "scudo:secondary",
137+
MAP_MEMTAG | Flags) &&
138+
MemMap.remap(AllocPos, CommitSize - TaggedSize, "scudo:secondary",
139+
Flags);
140+
} else {
141+
const uptr RemapFlags =
142+
(useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
143+
return MemMap.remap(CommitBase, CommitSize, "scudo:secondary",
144+
RemapFlags);
145+
}
146+
}
147+
148+
const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * PageSize;
126149
if (useMemoryTagging<Config>(Options) && CommitSize > MaxUnusedCacheBytes) {
127150
const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
128151
return MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",

0 commit comments

Comments
 (0)