@@ -122,18 +122,35 @@ bool mapSecondary(const Options &Options, uptr CommitBase, uptr CommitSize,
122
122
Flags |= MAP_RESIZABLE;
123
123
Flags |= MAP_ALLOWNOMEM;
124
124
125
- const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * getPageSizeCached ();
125
+ const uptr PageSize = getPageSizeCached ();
126
+ const uptr MaxUnusedCacheBytes = MaxUnusedCachePages * PageSize;
126
127
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
+ }
136
149
}
150
+
151
+ const uptr RemapFlags =
152
+ (useMemoryTagging<Config>(Options) ? MAP_MEMTAG : 0 ) | Flags;
153
+ return MemMap.remap (CommitBase, CommitSize, " scudo:secondary" , RemapFlags);
137
154
}
138
155
139
156
// Template specialization to avoid producing zero-length array
0 commit comments