Skip to content

Commit 07c19c3

Browse files
ndrewhc-rhodes
authored andcommitted
[compiler-rt] [Darwin] Move macOS ASAN reservation above 512G (#191039)
On macOS, the first 512G may contain platform-specific reservations. To ensure compatibility with these reservations, this changes ASAN to always map shadow memory above 512G on macOS. rdar://174252720 (cherry picked from commit 857a98e)
1 parent fa56327 commit 07c19c3

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,25 @@ uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
12881288
return shadow_start;
12891289
}
12901290

1291+
// Returns a list of ranges which must be covered by shadow memory,
1292+
// and cannot overlap with any fixed mappings made by a sanitizer.
1293+
// This can ensure that the sanitizer runtime does not map over
1294+
// platform-reserved regions.
1295+
void GetAppReservedRanges(InternalMmapVector<ReservedRange>& ranges) {
1296+
ranges.clear();
1297+
1298+
# if SANITIZER_OSX
1299+
// On macOS, the first 512GB are platform-reserved (some of which
1300+
// may also be available to applications).
1301+
ranges.push_back({0x1000UL, 0x8000000000UL});
1302+
# endif
1303+
1304+
VReport(2, "App ranges:\n");
1305+
for (auto& [range_start, range_end] : ranges) {
1306+
VReport(2, " [%p, %p]\n", range_start, range_end);
1307+
}
1308+
}
1309+
12911310
uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
12921311
uptr num_aliases, uptr ring_buffer_size) {
12931312
CHECK(false && "HWASan aliasing is unimplemented on Mac");
@@ -1300,6 +1319,16 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
13001319
const mach_vm_address_t max_vm_address = GetMaxVirtualAddress() + 1;
13011320
mach_vm_address_t address = GAP_SEARCH_START_ADDRESS;
13021321
mach_vm_address_t free_begin = GAP_SEARCH_START_ADDRESS;
1322+
1323+
// Restrict the search to be after any reserved ranges
1324+
InternalMmapVector<ReservedRange> app_ranges;
1325+
GetAppReservedRanges(app_ranges);
1326+
1327+
for (auto& [range_start, range_end] : app_ranges) {
1328+
address = Max(address, (mach_vm_address_t)range_end);
1329+
free_begin = Max(free_begin, (mach_vm_address_t)range_end);
1330+
}
1331+
13031332
kern_return_t kr = KERN_SUCCESS;
13041333
if (largest_gap_found) *largest_gap_found = 0;
13051334
if (max_occupied_addr) *max_occupied_addr = 0;

compiler-rt/lib/sanitizer_common/sanitizer_mac.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ struct DarwinKernelVersion : VersionBase<DarwinKernelVersion> {
5858
DarwinKernelVersion(u16 major, u16 minor) : VersionBase(major, minor) {}
5959
};
6060

61+
struct ReservedRange {
62+
uptr beg, end;
63+
};
64+
6165
MacosVersion GetMacosAlignedVersion();
6266
DarwinKernelVersion GetDarwinKernelVersion();
67+
void GetAppReservedRanges(InternalMmapVector<ReservedRange>& ranges);
6368

6469
char **GetEnviron();
6570

0 commit comments

Comments
 (0)