From 3e2a34116833ec0a01cc2c3d4353834aef7a693a Mon Sep 17 00:00:00 2001 From: Konstantin Mandrika Date: Thu, 30 Jun 2022 14:29:08 -0400 Subject: [PATCH] Handle abbrev entities being out of order. There are cases where the debug_abbrev entities are not sequential, for example, in Xamarin system dlls. This change gracefully handles such a case. Change-Id: Ib270393d3cf9fd18efd99d15d0fba4f96748188a Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/3738879 Reviewed-by: Joshua Peraza --- src/common/dwarf/dwarf2reader.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/common/dwarf/dwarf2reader.cc b/src/common/dwarf/dwarf2reader.cc index bf6758d8..11854b4b 100644 --- a/src/common/dwarf/dwarf2reader.cc +++ b/src/common/dwarf/dwarf2reader.cc @@ -129,10 +129,13 @@ void CompilationUnit::ReadAbbrevs() { const uint64_t abbrev_length = iter->second.second - header_.abbrev_offset; #endif + uint64_t highest_number = 0; + while (1) { CompilationUnit::Abbrev abbrev; size_t len; const uint64_t number = reader_->ReadUnsignedLEB128(abbrevptr, &len); + highest_number = std::max(highest_number, number); if (number == 0) break; @@ -170,9 +173,17 @@ void CompilationUnit::ReadAbbrevs() { value); abbrev.attributes.push_back(abbrev_attr); } - assert(abbrev.number == abbrevs_->size()); abbrevs_->push_back(abbrev); } + + // Account of cases where entries are out of order. + std::sort(abbrevs_->begin(), abbrevs_->end(), + [](const CompilationUnit::Abbrev& lhs, const CompilationUnit::Abbrev& rhs) { + return lhs.number < rhs.number; + }); + + // Ensure that there are no missing sections. + assert(abbrevs_->size() == highest_number + 1); } // Skips a single DIE's attributes.