[lldb/DWARF] Fix a crash parsing invalid dwarf (pr49678)

If the debug info is missing the terminating null die, we would crash
when trying to access the nonexisting children/siblings. This was
discovered because the test case for D98619 accidentaly produced such
input.
This commit is contained in:
Pavel Labath 2021-03-27 21:21:14 +01:00
parent 1e511bb1be
commit 2ecf928153
2 changed files with 42 additions and 0 deletions

View File

@ -236,6 +236,11 @@ void DWARFUnit::ExtractDIEsRWLocked() {
}
if (!m_die_array.empty()) {
// The last die cannot have children (if it did, it wouldn't be the last one).
// This only makes a difference for malformed dwarf that does not have a
// terminating null die.
m_die_array.back().SetHasChildren(false);
if (m_first_die) {
// Only needed for the assertion.
m_first_die.SetHasChildren(m_die_array.front().HasChildren());

View File

@ -47,3 +47,40 @@ DWARF:
ASSERT_NE(die_first, nullptr);
EXPECT_TRUE(die_first->IsNULL());
}
TEST(DWARFUnitTest, MissingSentinel) {
// Make sure we don't crash if the debug info is missing a null DIE sentinel.
const char *yamldata = R"(
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_386
DWARF:
debug_abbrev:
- Table:
- Code: 0x00000001
Tag: DW_TAG_compile_unit
Children: DW_CHILDREN_yes
Attributes:
- Attribute: DW_AT_language
Form: DW_FORM_data2
debug_info:
- Version: 4
AddrSize: 8
Entries:
- AbbrCode: 0x00000001
Values:
- Value: 0x000000000000000C
)";
YAMLModuleTester t(yamldata);
ASSERT_TRUE((bool)t.GetDwarfUnit());
DWARFUnit *unit = t.GetDwarfUnit();
const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
ASSERT_NE(die_first, nullptr);
EXPECT_EQ(die_first->GetFirstChild(), nullptr);
EXPECT_EQ(die_first->GetSibling(), nullptr);
}