From 7096b08cd76fde000873f4ed936f358afb79176e Mon Sep 17 00:00:00 2001 From: Francis Ricci Date: Mon, 17 Jul 2017 23:03:03 +0000 Subject: [PATCH] Only scan global sections containing data in LSan on darwin Summary: __DATA segments on Darwin contain a large number of separate sections, most of which cannot actually contain pointers, and contain const values or objc metadata. Only scanning sections which can contain pointers greatly improves performance. On a medium-sized (~4000 files) internal project, I saw a speedup of about 50% in standalone LSan's execution time (50% improvement in the time spent running LSan, not the total program time). Reviewers: kcc, kubamracek, alekseyshl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D35432 llvm-svn: 308231 --- compiler-rt/lib/lsan/lsan_common_mac.cc | 15 +++++++++++---- .../lib/sanitizer_common/sanitizer_common.cc | 5 +++-- .../lib/sanitizer_common/sanitizer_common.h | 12 +++++++++--- .../lib/sanitizer_common/sanitizer_procmaps.h | 1 + .../sanitizer_common/sanitizer_procmaps_mac.cc | 6 ++++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/compiler-rt/lib/lsan/lsan_common_mac.cc b/compiler-rt/lib/lsan/lsan_common_mac.cc index f87c6b7e0425..c0fa36fd969c 100644 --- a/compiler-rt/lib/lsan/lsan_common_mac.cc +++ b/compiler-rt/lib/lsan/lsan_common_mac.cc @@ -92,8 +92,15 @@ LoadedModule *GetLinker() { return nullptr; } // required on Darwin. void InitializePlatformSpecificModules() {} +// Sections which may contain global variables +static const char *kGlobalVarSecNames[] = { + "__DATA", "__bss", "__common", "__data", + "__objc_data", "__objc_opt_rw", "__objc_opt_ptrs"}; + // Scans global variables for heap pointers. void ProcessGlobalRegions(Frontier *frontier) { + for (auto name : kGlobalVarSecNames) CHECK(ARRAY_SIZE(name) < kMaxSegName); + MemoryMappingLayout memory_mapping(false); InternalMmapVector modules(/*initial_capacity*/ 128); memory_mapping.DumpListOfModules(&modules); @@ -104,10 +111,10 @@ void ProcessGlobalRegions(Frontier *frontier) { for (const __sanitizer::LoadedModule::AddressRange &range : modules[i].ranges()) { - // Sections storing global variables are writable and non-executable - if (range.executable || !range.writable) continue; - - ScanGlobalRange(range.beg, range.end, frontier); + for (auto name : kGlobalVarSecNames) { + if (!internal_strcmp(range.name, name)) + ScanGlobalRange(range.beg, range.end, frontier); + } } } } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc index 7e6f8fce76d2..3868d546929f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc @@ -285,9 +285,10 @@ void LoadedModule::clear() { } void LoadedModule::addAddressRange(uptr beg, uptr end, bool executable, - bool writable) { + bool writable, const char *name) { void *mem = InternalAlloc(sizeof(AddressRange)); - AddressRange *r = new(mem) AddressRange(beg, end, executable, writable); + AddressRange *r = + new(mem) AddressRange(beg, end, executable, writable, name); ranges_.push_back(r); if (executable && end > max_executable_address_) max_executable_address_ = end; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 89aae579856a..f684271d4dd5 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -702,6 +702,7 @@ inline const char *ModuleArchToString(ModuleArch arch) { } const uptr kModuleUUIDSize = 16; +const uptr kMaxSegName = 16; // Represents a binary loaded into virtual memory (e.g. this can be an // executable or a shared object). @@ -720,7 +721,8 @@ class LoadedModule { void set(const char *module_name, uptr base_address, ModuleArch arch, u8 uuid[kModuleUUIDSize], bool instrumented); void clear(); - void addAddressRange(uptr beg, uptr end, bool executable, bool writable); + void addAddressRange(uptr beg, uptr end, bool executable, bool writable, + const char *name = nullptr); bool containsAddress(uptr address) const; const char *full_name() const { return full_name_; } @@ -736,13 +738,17 @@ class LoadedModule { uptr end; bool executable; bool writable; + char name[kMaxSegName]; - AddressRange(uptr beg, uptr end, bool executable, bool writable) + AddressRange(uptr beg, uptr end, bool executable, bool writable, + const char *name) : next(nullptr), beg(beg), end(end), executable(executable), - writable(writable) {} + writable(writable) { + internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name)); + } }; const IntrusiveList &ranges() const { return ranges_; } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h b/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h index 76685661cc64..50753c4e607b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h @@ -58,6 +58,7 @@ class MemoryMappedSegment { u8 uuid[kModuleUUIDSize]; #if SANITIZER_MAC + char name[kMaxSegName]; private: friend class MemoryMappingLayout; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cc index 411bbd39dc9c..277d2b194d24 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_mac.cc @@ -42,12 +42,13 @@ void MemoryMappedSegment::NextSectionLoad(LoadedModule *module) { uptr sec_start = sc->addr + base_virt_addr_; uptr sec_end = sec_start + sc->size; - module->addAddressRange(sec_start, sec_end, IsExecutable(), IsWritable()); + module->addAddressRange(sec_start, sec_end, IsExecutable(), IsWritable(), + sc->sectname); } void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) { if (!nsects_) { - module->addAddressRange(start, end, IsExecutable(), IsWritable()); + module->addAddressRange(start, end, IsExecutable(), IsWritable(), name); return; } @@ -199,6 +200,7 @@ bool MemoryMappingLayout::NextSegmentLoad(MemoryMappedSegment *segment) { : _dyld_get_image_name(current_image_); internal_strncpy(segment->filename, src, segment->filename_size); } + internal_strncpy(segment->name, sc->segname, ARRAY_SIZE(segment->name)); segment->arch = current_arch_; internal_memcpy(segment->uuid, current_uuid_, kModuleUUIDSize); return true;