1 //===-- DWARFUnitTest.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
10 #include "TestingSupport/Symbol/YAMLModuleTester.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 TEST(DWARFUnitTest, NullUnitDie) {
18   // Make sure we don't crash parsing a null unit DIE.
19   const char *yamldata = R"(
20 --- !ELF
21 FileHeader:
22   Class:   ELFCLASS64
23   Data:    ELFDATA2LSB
24   Type:    ET_EXEC
25   Machine: EM_386
26 DWARF:
27   debug_abbrev:
28     - Table:
29         - Code:            0x00000001
30           Tag:             DW_TAG_compile_unit
31           Children:        DW_CHILDREN_yes
32           Attributes:
33             - Attribute:       DW_AT_language
34               Form:            DW_FORM_data2
35   debug_info:
36     - Version:         4
37       AddrSize:        8
38       Entries:
39         - AbbrCode:        0x00000000
40 )";
41 
42   YAMLModuleTester t(yamldata);
43   ASSERT_TRUE((bool)t.GetDwarfUnit());
44 
45   DWARFUnit *unit = t.GetDwarfUnit();
46   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
47   ASSERT_NE(die_first, nullptr);
48   EXPECT_TRUE(die_first->IsNULL());
49 }
50 
51 TEST(DWARFUnitTest, MissingSentinel) {
52   // Make sure we don't crash if the debug info is missing a null DIE sentinel.
53   const char *yamldata = R"(
54 --- !ELF
55 FileHeader:
56   Class:   ELFCLASS64
57   Data:    ELFDATA2LSB
58   Type:    ET_EXEC
59   Machine: EM_386
60 DWARF:
61   debug_abbrev:
62     - Table:
63         - Code:            0x00000001
64           Tag:             DW_TAG_compile_unit
65           Children:        DW_CHILDREN_yes
66           Attributes:
67             - Attribute:       DW_AT_language
68               Form:            DW_FORM_data2
69   debug_info:
70     - Version:         4
71       AddrSize:        8
72       Entries:
73         - AbbrCode:        0x00000001
74           Values:
75             - Value:           0x000000000000000C
76 )";
77 
78   YAMLModuleTester t(yamldata);
79   ASSERT_TRUE((bool)t.GetDwarfUnit());
80 
81   DWARFUnit *unit = t.GetDwarfUnit();
82   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
83   ASSERT_NE(die_first, nullptr);
84   EXPECT_EQ(die_first->GetFirstChild(), nullptr);
85   EXPECT_EQ(die_first->GetSibling(), nullptr);
86 }
87