Skip to content

Commit 0a6aec2

Browse files
committed
[NFC][lsan] Change Mac root regions scan
1 parent a0b0bf3 commit 0a6aec2

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

compiler-rt/lib/lsan/lsan_common.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,11 @@ static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
527527

528528
# endif // SANITIZER_FUCHSIA
529529

530-
void ScanRootRegion(Frontier *frontier, const Region &root_region,
531-
uptr region_begin, uptr region_end, bool is_readable) {
530+
bool HasRootRegions() { return !root_regions.empty(); }
531+
532+
static void ScanRootRegion(Frontier *frontier, const Region &root_region,
533+
uptr region_begin, uptr region_end,
534+
bool is_readable) {
532535
uptr intersection_begin = Max(root_region.begin, region_begin);
533536
uptr intersection_end = Min(region_end, root_region.end);
534537
if (intersection_begin >= intersection_end)
@@ -542,6 +545,16 @@ void ScanRootRegion(Frontier *frontier, const Region &root_region,
542545
kReachable);
543546
}
544547

548+
void ScanRootRegions(Frontier *frontier,
549+
const InternalMmapVectorNoCtor<Region> &mapped_regions) {
550+
if (!flags()->use_root_regions || mapped_regions.empty())
551+
return;
552+
553+
for (const auto &m : mapped_regions)
554+
for (const auto &r : root_regions)
555+
ScanRootRegion(frontier, r, m.begin, m.end, true);
556+
}
557+
545558
static void ProcessRootRegion(Frontier *frontier, const Region &root_region) {
546559
MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
547560
MemoryMappedSegment segment;

compiler-rt/lib/lsan/lsan_common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ struct CheckForLeaksParam {
256256
bool success = false;
257257
};
258258

259-
InternalMmapVectorNoCtor<Region> const *GetRootRegions();
260-
void ScanRootRegion(Frontier *frontier, const Region &region, uptr region_begin,
261-
uptr region_end, bool is_readable);
259+
bool HasRootRegions();
260+
void ScanRootRegions(Frontier *frontier,
261+
const InternalMmapVectorNoCtor<Region> &region);
262262
// Run stoptheworld while holding any platform-specific locks, as well as the
263263
// allocator and thread registry locks.
264264
void LockStuffAndStopTheWorld(StopTheWorldCallback callback,

compiler-rt/lib/lsan/lsan_common_mac.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ void ProcessPlatformSpecificAllocations(Frontier *frontier) {
165165
vm_address_t address = 0;
166166
kern_return_t err = KERN_SUCCESS;
167167

168-
InternalMmapVectorNoCtor<Region> const *root_regions = GetRootRegions();
168+
InternalMmapVectorNoCtor<Region> mapped_regions;
169+
bool use_root_regions = flags()->use_root_regions && HasRootRegions();
169170

170171
RegionScanState scan_state;
171172
while (err == KERN_SUCCESS) {
@@ -203,8 +204,7 @@ void ProcessPlatformSpecificAllocations(Frontier *frontier) {
203204

204205
// Recursing over the full memory map is very slow, break out
205206
// early if we don't need the full iteration.
206-
if (scan_state.seen_regions == SeenRegion::All &&
207-
!(flags()->use_root_regions && root_regions->size() > 0)) {
207+
if (scan_state.seen_regions == SeenRegion::All && !use_root_regions) {
208208
break;
209209
}
210210

@@ -215,15 +215,12 @@ void ProcessPlatformSpecificAllocations(Frontier *frontier) {
215215
//
216216
// TODO(fjricci) - remove this once sanitizer_procmaps_mac has the same
217217
// behavior as sanitizer_procmaps_linux and traverses all memory regions
218-
if (flags()->use_root_regions) {
219-
for (uptr i = 0; i < root_regions->size(); i++) {
220-
ScanRootRegion(frontier, (*root_regions)[i], address, end_address,
221-
info.protection & kProtectionRead);
222-
}
223-
}
218+
if (use_root_regions && (info.protection & kProtectionRead))
219+
mapped_regions.push_back({address, end_address});
224220

225221
address = end_address;
226222
}
223+
ScanRootRegions(frontier, mapped_regions);
227224
}
228225

229226
// On darwin, we can intercept _exit gracefully, and return a failing exit code

0 commit comments

Comments
 (0)