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 | template <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(EC: YIn.error(), S: GenerateDiag.getMessage()); |
31 | |
32 | return Error::success(); |
33 | } |
34 | |
35 | TEST(DebugAddrSection, TestParseDebugAddrYAML) { |
36 | StringRef Yaml = R"( |
37 | debug_addr: |
38 | - Format: DWARF64 |
39 | Length: 0x1234 |
40 | Version: 5 |
41 | )" ; |
42 | DWARFYAML::Data Data; |
43 | EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data), Succeeded()); |
44 | } |
45 | |
46 | TEST(DebugAddrSection, TestMissingVersion) { |
47 | StringRef Yaml = R"( |
48 | Format: DWARF64 |
49 | Length: 0x1234 |
50 | )" ; |
51 | DWARFYAML::AddrTableEntry AddrTableEntry; |
52 | EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, AddrTableEntry), |
53 | FailedWithMessage("missing required key 'Version'" )); |
54 | } |
55 | |
56 | TEST(DebugAddrSection, TestUnexpectedKey) { |
57 | StringRef Yaml = R"( |
58 | Format: DWARF64 |
59 | Length: 0x1234 |
60 | Version: 5 |
61 | Blah: unexpected |
62 | )" ; |
63 | DWARFYAML::AddrTableEntry AddrTableEntry; |
64 | EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, AddrTableEntry), |
65 | FailedWithMessage("unknown key 'Blah'" )); |
66 | } |
67 | |
68 | TEST(DebugPubSection, TestDebugPubSection) { |
69 | StringRef Yaml = R"( |
70 | debug_pubnames: |
71 | Length: 0x1234 |
72 | Version: 2 |
73 | UnitOffset: 0x4321 |
74 | UnitSize: 0x00 |
75 | Entries: |
76 | - DieOffset: 0x1234 |
77 | Name: abc |
78 | - DieOffset: 0x4321 |
79 | Name: def |
80 | debug_pubtypes: |
81 | Length: 0x1234 |
82 | Version: 2 |
83 | UnitOffset: 0x4321 |
84 | UnitSize: 0x00 |
85 | Entries: |
86 | - DieOffset: 0x1234 |
87 | Name: abc |
88 | - DieOffset: 0x4321 |
89 | Name: def |
90 | )" ; |
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 | |
113 | TEST(DebugPubSection, TestUnexpectedDescriptor) { |
114 | StringRef Yaml = R"( |
115 | debug_pubnames: |
116 | Length: 0x1234 |
117 | Version: 2 |
118 | UnitOffset: 0x4321 |
119 | UnitSize: 0x00 |
120 | Entries: |
121 | - DieOffset: 0x1234 |
122 | Descriptor: 0x12 |
123 | Name: abcd |
124 | )" ; |
125 | DWARFYAML::Data Data; |
126 | EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data), |
127 | FailedWithMessage("unknown key 'Descriptor'" )); |
128 | } |
129 | |
130 | TEST(DebugGNUPubSection, TestDebugGNUPubSections) { |
131 | StringRef Yaml = R"( |
132 | debug_gnu_pubnames: |
133 | Length: 0x1234 |
134 | Version: 2 |
135 | UnitOffset: 0x4321 |
136 | UnitSize: 0x00 |
137 | Entries: |
138 | - DieOffset: 0x1234 |
139 | Descriptor: 0x12 |
140 | Name: abc |
141 | - DieOffset: 0x4321 |
142 | Descriptor: 0x34 |
143 | Name: def |
144 | debug_gnu_pubtypes: |
145 | Length: 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 | )" ; |
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 | |
183 | TEST(DebugGNUPubSection, TestMissingDescriptor) { |
184 | StringRef Yaml = R"( |
185 | debug_gnu_pubnames: |
186 | Length: 0x1234 |
187 | Version: 2 |
188 | UnitOffset: 0x4321 |
189 | UnitSize: 0x00 |
190 | Entries: |
191 | - DieOffset: 0x1234 |
192 | Name: abcd |
193 | )" ; |
194 | DWARFYAML::Data Data; |
195 | EXPECT_THAT_ERROR(parseDWARFYAML(Yaml, Data), |
196 | FailedWithMessage("missing required key 'Descriptor'" )); |
197 | } |
198 | |