Skip to content

Commit 7dba407

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 a25da1a commit 7dba407

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,35 @@ 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+
const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * PageSize;
126127
if (useMemoryTagging<Config>(Options) && CommitSize > MaxUnusedCacheBytes) {
127-
const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
128-
return MemMap.remap(CommitBase, UntaggedPos - CommitBase, "scudo:secondary",
129-
MAP_MEMTAG | Flags) &&
130-
MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
131-
"scudo:secondary", Flags);
132-
} else {
133-
const uptr RemapFlags =
134-
(useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
135-
return MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
128+
if (SCUDO_TRUSTY) {
129+
/*
130+
* On Trusty we need AllocPos to be usable for memrefs, which cannot
131+
* cross multiple mappings. This means we need to split around AllocPos
132+
* and not over it. We can only do this if the address is page-aligned.
133+
*/
134+
const uptr TaggedSize = AllocPos - CommitBase;
135+
if (TaggedSize != 0 && isAligned(TaggedSize, PageSize)) {
136+
return MemMap.remap(CommitBase, TaggedSize, "scudo:secondary",
137+
MAP_MEMTAG | Flags) &&
138+
MemMap.remap(AllocPos, CommitSize - TaggedSize,
139+
"scudo:secondary", Flags);
140+
}
141+
/* We could not split, so fall through to the normal code path */
142+
} else {
143+
const uptr UntaggedPos = Max(AllocPos, CommitBase + MaxUnusedCacheBytes);
144+
return MemMap.remap(CommitBase, UntaggedPos - CommitBase,
145+
"scudo:secondary", MAP_MEMTAG | Flags) &&
146+
MemMap.remap(UntaggedPos, CommitBase + CommitSize - UntaggedPos,
147+
"scudo:secondary", Flags);
148+
}
136149
}
150+
151+
const uptr RemapFlags =
152+
(useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0) | Flags;
153+
return MemMap.remap(CommitBase, CommitSize, "scudo:secondary", RemapFlags);
137154
}
138155

139156
// Template specialization to avoid producing zero-length array

0 commit comments

Comments
 (0)