1 //===- DWARFYAMLTest.cpp - Tests for DWARFYAML.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 "llvm/ObjectYAML/DWARFYAML.h" 10 #include "llvm/ObjectYAML/DWARFEmitter.h" 11 #include "llvm/Support/Error.h" 12 #include "llvm/Support/SourceMgr.h" 13 #include "llvm/Support/YAMLTraits.h" 14 #include "llvm/Testing/Support/Error.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 19 static Expected<DWARFYAML::Data> parseDWARFYAML(StringRef Yaml, 20 bool IsLittleEndian = false, 21 bool Is64bit = true) { 22 DWARFYAML::Data Data; 23 Data.IsLittleEndian = IsLittleEndian; 24 Data.Is64bit = Is64bit; 25 26 SMDiagnostic GenerateDiag; 27 yaml::Input YIn( 28 Yaml, /*Ctxt=*/nullptr, 29 [](const SMDiagnostic &Diag, void *DiagContext) { 30 *static_cast<SMDiagnostic *>(DiagContext) = Diag; 31 }, 32 &GenerateDiag); 33 34 YIn >> Data; 35 if (YIn.error()) 36 return createStringError(YIn.error(), GenerateDiag.getMessage()); 37 38 return Data; 39 } 40 41 TEST(DebugAddrSection, TestParseDebugAddrYAML) { 42 StringRef Yaml = R"( 43 debug_addr: 44 - Format: DWARF64 45 Length: 0x1234 46 Version: 5 47 )"; 48 auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml); 49 EXPECT_THAT_EXPECTED(SectionsOrErr, Succeeded()); 50 } 51 52 TEST(DebugAddrSection, TestMissingVersion) { 53 StringRef Yaml = R"( 54 debug_addr: 55 - Format: DWARF64 56 Length: 0x1234 57 )"; 58 auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml); 59 EXPECT_THAT_ERROR(SectionsOrErr.takeError(), 60 FailedWithMessage("missing required key 'Version'")); 61 } 62 63 TEST(DebugAddrSection, TestUnexpectedKey) { 64 StringRef Yaml = R"( 65 debug_addr: 66 - Format: DWARF64 67 Length: 0x1234 68 Version: 5 69 Blah: unexpected 70 )"; 71 auto SectionsOrErr = DWARFYAML::emitDebugSections(Yaml); 72 EXPECT_THAT_ERROR(SectionsOrErr.takeError(), 73 FailedWithMessage("unknown key 'Blah'")); 74 } 75 76 TEST(DebugPubSection, TestDebugPubSection) { 77 StringRef Yaml = R"( 78 debug_pubnames: 79 Length: 80 TotalLength: 0x1234 81 Version: 2 82 UnitOffset: 0x4321 83 UnitSize: 0x00 84 Entries: 85 - DieOffset: 0x1234 86 Name: abc 87 - DieOffset: 0x4321 88 Name: def 89 debug_pubtypes: 90 Length: 91 TotalLength: 0x1234 92 Version: 2 93 UnitOffset: 0x4321 94 UnitSize: 0x00 95 Entries: 96 - DieOffset: 0x1234 97 Name: abc 98 - DieOffset: 0x4321 99 Name: def 100 )"; 101 auto DWARFOrErr = parseDWARFYAML(Yaml); 102 ASSERT_THAT_EXPECTED(DWARFOrErr, Succeeded()); 103 104 ASSERT_TRUE(DWARFOrErr->PubNames.hasValue()); 105 DWARFYAML::PubSection PubNames = DWARFOrErr->PubNames.getValue(); 106 107 ASSERT_EQ(PubNames.Entries.size(), 2u); 108 EXPECT_EQ((uint32_t)PubNames.Entries[0].DieOffset, 0x1234u); 109 EXPECT_EQ(PubNames.Entries[0].Name, "abc"); 110 EXPECT_EQ((uint32_t)PubNames.Entries[1].DieOffset, 0x4321u); 111 EXPECT_EQ(PubNames.Entries[1].Name, "def"); 112 113 ASSERT_TRUE(DWARFOrErr->PubTypes.hasValue()); 114 DWARFYAML::PubSection PubTypes = DWARFOrErr->PubTypes.getValue(); 115 116 ASSERT_EQ(PubTypes.Entries.size(), 2u); 117 EXPECT_EQ((uint32_t)PubTypes.Entries[0].DieOffset, 0x1234u); 118 EXPECT_EQ(PubTypes.Entries[0].Name, "abc"); 119 EXPECT_EQ((uint32_t)PubTypes.Entries[1].DieOffset, 0x4321u); 120 EXPECT_EQ(PubTypes.Entries[1].Name, "def"); 121 } 122 123 TEST(DebugPubSection, TestUnexpectedDescriptor) { 124 StringRef Yaml = R"( 125 debug_pubnames: 126 Length: 127 TotalLength: 0x1234 128 Version: 2 129 UnitOffset: 0x4321 130 UnitSize: 0x00 131 Entries: 132 - DieOffset: 0x1234 133 Descriptor: 0x12 134 Name: abcd 135 )"; 136 auto DWARFOrErr = parseDWARFYAML(Yaml); 137 EXPECT_THAT_ERROR(DWARFOrErr.takeError(), 138 FailedWithMessage("unknown key 'Descriptor'")); 139 } 140 141 TEST(DebugGNUPubSection, TestDebugGNUPubSections) { 142 StringRef Yaml = R"( 143 debug_gnu_pubnames: 144 Length: 145 TotalLength: 0x1234 146 Version: 2 147 UnitOffset: 0x4321 148 UnitSize: 0x00 149 Entries: 150 - DieOffset: 0x1234 151 Descriptor: 0x12 152 Name: abc 153 - DieOffset: 0x4321 154 Descriptor: 0x34 155 Name: def 156 debug_gnu_pubtypes: 157 Length: 158 TotalLength: 0x1234 159 Version: 2 160 UnitOffset: 0x4321 161 UnitSize: 0x00 162 Entries: 163 - DieOffset: 0x1234 164 Descriptor: 0x12 165 Name: abc 166 - DieOffset: 0x4321 167 Descriptor: 0x34 168 Name: def 169 )"; 170 auto DWARFOrErr = parseDWARFYAML(Yaml); 171 ASSERT_THAT_EXPECTED(DWARFOrErr, Succeeded()); 172 173 ASSERT_TRUE(DWARFOrErr->GNUPubNames.hasValue()); 174 DWARFYAML::PubSection GNUPubNames = DWARFOrErr->GNUPubNames.getValue(); 175 176 ASSERT_EQ(GNUPubNames.Entries.size(), 2u); 177 EXPECT_EQ((uint32_t)GNUPubNames.Entries[0].DieOffset, 0x1234u); 178 EXPECT_EQ((uint8_t)GNUPubNames.Entries[0].Descriptor, 0x12); 179 EXPECT_EQ(GNUPubNames.Entries[0].Name, "abc"); 180 EXPECT_EQ((uint32_t)GNUPubNames.Entries[1].DieOffset, 0x4321u); 181 EXPECT_EQ((uint8_t)GNUPubNames.Entries[1].Descriptor, 0x34); 182 EXPECT_EQ(GNUPubNames.Entries[1].Name, "def"); 183 184 ASSERT_TRUE(DWARFOrErr->GNUPubTypes.hasValue()); 185 DWARFYAML::PubSection GNUPubTypes = DWARFOrErr->GNUPubTypes.getValue(); 186 187 ASSERT_EQ(GNUPubTypes.Entries.size(), 2u); 188 EXPECT_EQ((uint32_t)GNUPubTypes.Entries[0].DieOffset, 0x1234u); 189 EXPECT_EQ((uint8_t)GNUPubTypes.Entries[0].Descriptor, 0x12); 190 EXPECT_EQ(GNUPubTypes.Entries[0].Name, "abc"); 191 EXPECT_EQ((uint32_t)GNUPubTypes.Entries[1].DieOffset, 0x4321u); 192 EXPECT_EQ((uint8_t)GNUPubTypes.Entries[1].Descriptor, 0x34); 193 EXPECT_EQ(GNUPubTypes.Entries[1].Name, "def"); 194 } 195 196 TEST(DebugGNUPubSection, TestMissingDescriptor) { 197 StringRef Yaml = R"( 198 debug_gnu_pubnames: 199 Length: 200 TotalLength: 0x1234 201 Version: 2 202 UnitOffset: 0x4321 203 UnitSize: 0x00 204 Entries: 205 - DieOffset: 0x1234 206 Name: abcd 207 )"; 208 auto DWARFOrErr = parseDWARFYAML(Yaml); 209 EXPECT_THAT_ERROR(DWARFOrErr.takeError(), 210 FailedWithMessage("missing required key 'Descriptor'")); 211 } 212