198 lines · cpp
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-exception6//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 17using namespace llvm;18 19template <class T> static Error parseDWARFYAML(StringRef Yaml, T &Data) {20 SMDiagnostic GenerateDiag;21 yaml::Input YIn(22 Yaml, /*Ctxt=*/nullptr,23 [](const SMDiagnostic &Diag, void *DiagContext) {24 *static_cast<SMDiagnostic *>(DiagContext) = Diag;25 },26 &GenerateDiag);27 28 YIn >> Data;29 if (YIn.error())30 return createStringError(YIn.error(), GenerateDiag.getMessage());31 32 return Error::success();33}34 35TEST(DebugAddrSection, TestParseDebugAddrYAML) {36 StringRef Yaml = R"(37debug_addr:38 - Format: DWARF6439 Length: 0x123440 Version: 541)";42 DWARFYAML::Data Data;43 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data), Succeeded());44}45 46TEST(DebugAddrSection, TestMissingVersion) {47 StringRef Yaml = R"(48Format: DWARF6449Length: 0x123450)";51 DWARFYAML::AddrTableEntry AddrTableEntry;52 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, AddrTableEntry),53 FailedWithMessage("missing required key 'Version'"));54}55 56TEST(DebugAddrSection, TestUnexpectedKey) {57 StringRef Yaml = R"(58Format: DWARF6459Length: 0x123460Version: 561Blah: unexpected62)";63 DWARFYAML::AddrTableEntry AddrTableEntry;64 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, AddrTableEntry),65 FailedWithMessage("unknown key 'Blah'"));66}67 68TEST(DebugPubSection, TestDebugPubSection) {69 StringRef Yaml = R"(70debug_pubnames:71 Length: 0x123472 Version: 273 UnitOffset: 0x432174 UnitSize: 0x0075 Entries:76 - DieOffset: 0x123477 Name: abc78 - DieOffset: 0x432179 Name: def80debug_pubtypes:81 Length: 0x123482 Version: 283 UnitOffset: 0x432184 UnitSize: 0x0085 Entries:86 - DieOffset: 0x123487 Name: abc88 - DieOffset: 0x432189 Name: def90)";91 DWARFYAML::Data Data;92 ASSERT_THAT_ERROR(parseDWARFYAML(Yaml, Data), Succeeded());93 94 ASSERT_TRUE(Data.PubNames.has_value());95 DWARFYAML::PubSection PubNames = *Data.PubNames;96 97 ASSERT_EQ(PubNames.Entries.size(), 2u);98 EXPECT_EQ((uint32_t)PubNames.Entries[0].DieOffset, 0x1234u);99 EXPECT_EQ(PubNames.Entries[0].Name, "abc");100 EXPECT_EQ((uint32_t)PubNames.Entries[1].DieOffset, 0x4321u);101 EXPECT_EQ(PubNames.Entries[1].Name, "def");102 103 ASSERT_TRUE(Data.PubTypes.has_value());104 DWARFYAML::PubSection PubTypes = *Data.PubTypes;105 106 ASSERT_EQ(PubTypes.Entries.size(), 2u);107 EXPECT_EQ((uint32_t)PubTypes.Entries[0].DieOffset, 0x1234u);108 EXPECT_EQ(PubTypes.Entries[0].Name, "abc");109 EXPECT_EQ((uint32_t)PubTypes.Entries[1].DieOffset, 0x4321u);110 EXPECT_EQ(PubTypes.Entries[1].Name, "def");111}112 113TEST(DebugPubSection, TestUnexpectedDescriptor) {114 StringRef Yaml = R"(115debug_pubnames:116 Length: 0x1234117 Version: 2118 UnitOffset: 0x4321119 UnitSize: 0x00120 Entries:121 - DieOffset: 0x1234122 Descriptor: 0x12123 Name: abcd124)";125 DWARFYAML::Data Data;126 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data),127 FailedWithMessage("unknown key 'Descriptor'"));128}129 130TEST(DebugGNUPubSection, TestDebugGNUPubSections) {131 StringRef Yaml = R"(132debug_gnu_pubnames:133 Length: 0x1234134 Version: 2135 UnitOffset: 0x4321136 UnitSize: 0x00137 Entries:138 - DieOffset: 0x1234139 Descriptor: 0x12140 Name: abc141 - DieOffset: 0x4321142 Descriptor: 0x34143 Name: def144debug_gnu_pubtypes:145 Length: 0x1234146 Version: 2147 UnitOffset: 0x4321148 UnitSize: 0x00149 Entries:150 - DieOffset: 0x1234151 Descriptor: 0x12152 Name: abc153 - DieOffset: 0x4321154 Descriptor: 0x34155 Name: def156)";157 DWARFYAML::Data Data;158 ASSERT_THAT_ERROR(parseDWARFYAML(Yaml, Data), Succeeded());159 160 ASSERT_TRUE(Data.GNUPubNames.has_value());161 DWARFYAML::PubSection GNUPubNames = *Data.GNUPubNames;162 163 ASSERT_EQ(GNUPubNames.Entries.size(), 2u);164 EXPECT_EQ((uint32_t)GNUPubNames.Entries[0].DieOffset, 0x1234u);165 EXPECT_EQ((uint8_t)GNUPubNames.Entries[0].Descriptor, 0x12);166 EXPECT_EQ(GNUPubNames.Entries[0].Name, "abc");167 EXPECT_EQ((uint32_t)GNUPubNames.Entries[1].DieOffset, 0x4321u);168 EXPECT_EQ((uint8_t)GNUPubNames.Entries[1].Descriptor, 0x34);169 EXPECT_EQ(GNUPubNames.Entries[1].Name, "def");170 171 ASSERT_TRUE(Data.GNUPubTypes.has_value());172 DWARFYAML::PubSection GNUPubTypes = *Data.GNUPubTypes;173 174 ASSERT_EQ(GNUPubTypes.Entries.size(), 2u);175 EXPECT_EQ((uint32_t)GNUPubTypes.Entries[0].DieOffset, 0x1234u);176 EXPECT_EQ((uint8_t)GNUPubTypes.Entries[0].Descriptor, 0x12);177 EXPECT_EQ(GNUPubTypes.Entries[0].Name, "abc");178 EXPECT_EQ((uint32_t)GNUPubTypes.Entries[1].DieOffset, 0x4321u);179 EXPECT_EQ((uint8_t)GNUPubTypes.Entries[1].Descriptor, 0x34);180 EXPECT_EQ(GNUPubTypes.Entries[1].Name, "def");181}182 183TEST(DebugGNUPubSection, TestMissingDescriptor) {184 StringRef Yaml = R"(185debug_gnu_pubnames:186 Length: 0x1234187 Version: 2188 UnitOffset: 0x4321189 UnitSize: 0x00190 Entries:191 - DieOffset: 0x1234192 Name: abcd193)";194 DWARFYAML::Data Data;195 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data),196 FailedWithMessage("missing required key 'Descriptor'"));197}198