@@ -56,6 +56,52 @@ static void NextSectionLoad(LoadedModule *module, MemoryMappedSegmentData *data,
5656 sc->sectname );
5757}
5858
59+ static bool VerifyMemoryMapping (MemoryMappingLayout *mapping) {
60+ InternalMmapVector<LoadedModule> modules;
61+ modules.reserve (128 ); // matches DumpProcessMap
62+ mapping->DumpListOfModules (&modules);
63+
64+ InternalMmapVector<LoadedModule::AddressRange> segments;
65+ for (uptr i = 0 ; i < modules.size (); ++i) {
66+ for (auto &range : modules[i].ranges ()) {
67+ segments.push_back (range);
68+ }
69+ }
70+
71+ // Verify that none of the segments overlap:
72+ // 1. Sort the segments by the start address
73+ // 2. Check that every segment starts after the previous one ends.
74+ Sort (segments.data (), segments.size (),
75+ [](LoadedModule::AddressRange& a, LoadedModule::AddressRange& b) {
76+ return a.beg < b.beg ;
77+ });
78+
79+ // To avoid spam, we only print the report message once-per-process.
80+ static bool invalid_module_map_reported = false ;
81+ bool well_formed = true ;
82+
83+ for (size_t i = 1 ; i < segments.size (); i++) {
84+ uptr cur_start = segments[i].beg ;
85+ uptr prev_end = segments[i - 1 ].end ;
86+ if (cur_start < prev_end) {
87+ well_formed = false ;
88+ VReport (2 , " Overlapping mappings: %s start = %p, %s end = %p\n " ,
89+ segments[i].name , (void *)cur_start, segments[i-1 ].name , (void *)prev_end);
90+ if (!invalid_module_map_reported) {
91+ Report (
92+ " WARN: Invalid dyld module map detected. This is most likely a bug "
93+ " in the sanitizer.\n " );
94+ Report (" WARN: Backtraces may be unreliable.\n " );
95+ invalid_module_map_reported = true ;
96+ }
97+ }
98+ }
99+
100+ mapping->Reset ();
101+ return well_formed;
102+ }
103+
104+
59105void MemoryMappedSegment::AddAddressRanges (LoadedModule *module ) {
60106 // Don't iterate over sections when the caller hasn't set up the
61107 // data pointer, when there are no sections, or when the segment
@@ -81,7 +127,7 @@ void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) {
81127
82128MemoryMappingLayout::MemoryMappingLayout (bool cache_enabled) {
83129 Reset ();
84- Verify ( );
130+ VerifyMemoryMapping ( this );
85131}
86132
87133MemoryMappingLayout::~MemoryMappingLayout () {
@@ -443,56 +489,6 @@ bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
443489 return false ;
444490}
445491
446- // NOTE: Verify expects to be called immediately after Reset(), since otherwise
447- // it may miss some mappings. Verify will Reset() the layout after verification.
448- bool MemoryMappingLayout::Verify () {
449- InternalMmapVector<char > module_name (kMaxPathLength );
450- MemoryMappedSegment segment (module_name.data (), module_name.size ());
451- MemoryMappedSegmentData data;
452- segment.data_ = &data;
453-
454- InternalMmapVector<MemoryMappedSegment> segments;
455- while (Next (&segment)) {
456- // skip the __PAGEZERO segment, its vmsize is 0
457- if (segment.filename [0 ] == ' \0 ' || (segment.start == segment.end ))
458- continue ;
459-
460- segments.push_back (segment);
461- }
462-
463- // Verify that none of the segments overlap:
464- // 1. Sort the segments by the start address
465- // 2. Check that every segment starts after the previous one ends.
466- Sort (segments.data (), segments.size (),
467- [](MemoryMappedSegment& a, MemoryMappedSegment& b) {
468- return a.start < b.start ;
469- });
470-
471- // To avoid spam, we only print the report message once-per-process.
472- static bool invalid_module_map_reported = false ;
473- bool well_formed = true ;
474-
475- for (size_t i = 1 ; i < segments.size (); i++) {
476- uptr cur_start = segments[i].start ;
477- uptr prev_end = segments[i - 1 ].end ;
478- if (cur_start < prev_end) {
479- well_formed = false ;
480- VReport (2 , " Overlapping mappings: cur_start = %p prev_end = %p\n " ,
481- (void *)cur_start, (void *)prev_end);
482- if (!invalid_module_map_reported) {
483- Report (
484- " WARN: Invalid dyld module map detected. This is most likely a bug "
485- " in the sanitizer.\n " );
486- Report (" WARN: Backtraces may be unreliable.\n " );
487- invalid_module_map_reported = true ;
488- }
489- }
490- }
491-
492- Reset ();
493- return well_formed;
494- }
495-
496492void MemoryMappingLayout::DumpListOfModules (
497493 InternalMmapVectorNoCtor<LoadedModule> *modules) {
498494 Reset ();
0 commit comments