1 //===- llvm/unittest/DebugInfo/DWARFDebugArangeSetTest.cpp-----------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
11 #include "llvm/Testing/Support/Error.h"
12 #include "gtest/gtest.h"
18 struct WarningHandler
{
19 ~WarningHandler() { EXPECT_THAT_ERROR(std::move(Err
), Succeeded()); }
21 void operator()(Error E
) { Err
= joinErrors(std::move(Err
), std::move(E
)); }
23 Error
getWarning() { return std::move(Err
); }
25 Error Err
= Error::success();
28 template <size_t SecSize
>
29 void ExpectExtractError(const char (&SecDataRaw
)[SecSize
],
30 const char *ErrorMessage
) {
31 DWARFDataExtractor
Extractor(StringRef(SecDataRaw
, SecSize
- 1),
32 /* IsLittleEndian = */ true,
33 /* AddressSize = */ 4);
34 DWARFDebugArangeSet Set
;
36 WarningHandler Warnings
;
37 Error E
= Set
.extract(Extractor
, &Offset
, Warnings
);
38 ASSERT_TRUE(E
.operator bool());
39 EXPECT_STREQ(ErrorMessage
, toString(std::move(E
)).c_str());
42 TEST(DWARFDebugArangeSet
, LengthExceedsSectionSize
) {
43 static const char DebugArangesSecRaw
[] =
44 "\x15\x00\x00\x00" // The length exceeds the section boundaries
46 "\x00\x00\x00\x00" // Debug Info Offset
47 "\x04" // Address Size
48 "\x00" // Segment Selector Size
49 "\x00\x00\x00\x00" // Padding
50 "\x00\x00\x00\x00" // Termination tuple
54 "the length of address range table at offset 0x0 exceeds section size");
57 TEST(DWARFDebugArangeSet
, LengthExceedsSectionSizeDWARF64
) {
58 static const char DebugArangesSecRaw
[] =
59 "\xff\xff\xff\xff" // DWARF64 mark
60 "\x15\x00\x00\x00\x00\x00\x00\x00" // The length exceeds the section
63 "\x00\x00\x00\x00\x00\x00\x00\x00" // Debug Info Offset
64 "\x04" // Address Size
65 "\x00" // Segment Selector Size
67 "\x00\x00\x00\x00" // Termination tuple
71 "the length of address range table at offset 0x0 exceeds section size");
74 TEST(DWARFDebugArangeSet
, UnsupportedAddressSize
) {
75 static const char DebugArangesSecRaw
[] =
76 "\x0c\x00\x00\x00" // Length
78 "\x00\x00\x00\x00" // Debug Info Offset
79 "\x03" // Address Size (not supported)
80 "\x00" // Segment Selector Size
82 "\x00\x00\x00\x00"; // Termination tuple
85 "address range table at offset 0x0 has unsupported address size: 3 "
86 "(supported are 2, 4, 8)");
89 TEST(DWARFDebugArangeSet
, UnsupportedSegmentSelectorSize
) {
90 static const char DebugArangesSecRaw
[] =
91 "\x14\x00\x00\x00" // Length
93 "\x00\x00\x00\x00" // Debug Info Offset
94 "\x04" // Address Size
95 "\x04" // Segment Selector Size (not supported)
97 "\x00\x00\x00\x00" // Termination tuple
102 "non-zero segment selector size in address range table at offset 0x0 "
106 TEST(DWARFDebugArangeSet
, NoTerminationEntry
) {
107 static const char DebugArangesSecRaw
[] =
108 "\x14\x00\x00\x00" // Length
109 "\x02\x00" // Version
110 "\x00\x00\x00\x00" // Debug Info Offset
111 "\x04" // Address Size
112 "\x00" // Segment Selector Size
113 "\x00\x00\x00\x00" // Padding
114 "\x00\x00\x00\x00" // Entry: Address
115 "\x01\x00\x00\x00" // Length
116 ; // No termination tuple
119 "address range table at offset 0x0 is not terminated by null entry");
122 TEST(DWARFDebugArangeSet
, ReservedUnitLength
) {
123 // Note: 12 is the minimum length to pass the basic check for the size of
124 // the section. 1 will be automatically subtracted in ExpectExtractError().
125 static const char DebugArangesSecRaw
[12 + 1] =
126 "\xf0\xff\xff\xff"; // Reserved unit length value
127 ExpectExtractError(DebugArangesSecRaw
,
128 "parsing address ranges table at offset 0x0: unsupported "
129 "reserved unit length of value 0xfffffff0");
132 TEST(DWARFDebugArangeSet
, SectionTooShort
) {
133 // Note: 1 will be automatically subtracted in ExpectExtractError().
134 static const char DebugArangesSecRaw
[11 + 1] = {0};
135 ExpectExtractError(DebugArangesSecRaw
,
136 "parsing address ranges table at offset 0x0: unexpected "
137 "end of data at offset 0xb while reading [0xb, 0xc)");
140 TEST(DWARFDebugArangeSet
, SectionTooShortDWARF64
) {
141 // Note: 1 will be automatically subtracted in ExpectExtractError().
142 static const char DebugArangesSecRaw
[23 + 1] =
143 "\xff\xff\xff\xff"; // DWARF64 mark
144 ExpectExtractError(DebugArangesSecRaw
,
145 "parsing address ranges table at offset 0x0: unexpected "
146 "end of data at offset 0x17 while reading [0x17, 0x18)");
149 TEST(DWARFDebugArangeSet
, NoSpaceForEntries
) {
150 static const char DebugArangesSecRaw
[] =
151 "\x0c\x00\x00\x00" // Length
152 "\x02\x00" // Version
153 "\x00\x00\x00\x00" // Debug Info Offset
154 "\x04" // Address Size
155 "\x00" // Segment Selector Size
156 "\x00\x00\x00\x00" // Padding
160 "address range table at offset 0x0 has an insufficient length "
161 "to contain any entries");
164 TEST(DWARFDebugArangeSet
, UnevenLength
) {
165 static const char DebugArangesSecRaw
[] =
166 "\x1b\x00\x00\x00" // Length (not a multiple of tuple size)
167 "\x02\x00" // Version
168 "\x00\x00\x00\x00" // Debug Info Offset
169 "\x04" // Address Size
170 "\x00" // Segment Selector Size
171 "\x00\x00\x00\x00" // Padding
172 "\x00\x00\x00\x00" // Entry: Address
173 "\x01\x00\x00\x00" // Length
174 "\x00\x00\x00\x00" // Termination tuple
178 "address range table at offset 0x0 has length that is not a multiple "
179 "of the tuple size");
182 TEST(DWARFDebugArangeSet
, ZeroAddressEntry
) {
183 static const char DebugArangesSecRaw
[] =
184 "\x1c\x00\x00\x00" // Length
185 "\x02\x00" // Version
186 "\x00\x00\x00\x00" // Debug Info Offset
187 "\x04" // Address Size
188 "\x00" // Segment Selector Size
189 "\x00\x00\x00\x00" // Padding
190 "\x00\x00\x00\x00" // Entry1: Address
191 "\x01\x00\x00\x00" // Length
192 "\x00\x00\x00\x00" // Termination tuple
194 DWARFDataExtractor
Extractor(
195 StringRef(DebugArangesSecRaw
, sizeof(DebugArangesSecRaw
) - 1),
196 /*IsLittleEndian=*/true,
198 DWARFDebugArangeSet Set
;
200 ASSERT_THAT_ERROR(Set
.extract(Extractor
, &Offset
, WarningHandler()),
202 auto Range
= Set
.descriptors();
203 auto Iter
= Range
.begin();
204 ASSERT_EQ(std::distance(Iter
, Range
.end()), 1);
205 EXPECT_EQ(Iter
->Address
, 0u);
206 EXPECT_EQ(Iter
->Length
, 1u);
209 TEST(DWARFDebugArangeSet
, ZeroLengthEntry
) {
210 static const char DebugArangesSecRaw
[] =
211 "\x1c\x00\x00\x00" // Length
212 "\x02\x00" // Version
213 "\x00\x00\x00\x00" // Debug Info Offset
214 "\x04" // Address Size
215 "\x00" // Segment Selector Size
216 "\x00\x00\x00\x00" // Padding
217 "\x01\x00\x00\x00" // Entry1: Address
218 "\x00\x00\x00\x00" // Length
219 "\x00\x00\x00\x00" // Termination tuple
221 DWARFDataExtractor
Extractor(
222 StringRef(DebugArangesSecRaw
, sizeof(DebugArangesSecRaw
) - 1),
223 /*IsLittleEndian=*/true,
225 DWARFDebugArangeSet Set
;
227 ASSERT_THAT_ERROR(Set
.extract(Extractor
, &Offset
, WarningHandler()),
229 auto Range
= Set
.descriptors();
230 auto Iter
= Range
.begin();
231 ASSERT_EQ(std::distance(Iter
, Range
.end()), 1);
232 EXPECT_EQ(Iter
->Address
, 1u);
233 EXPECT_EQ(Iter
->Length
, 0u);
236 TEST(DWARFDebugArangesSet
, PrematureTerminator
) {
237 static const char DebugArangesSecRaw
[] =
238 "\x24\x00\x00\x00" // Length
239 "\x02\x00" // Version
240 "\x00\x00\x00\x00" // Debug Info Offset
241 "\x04" // Address Size
242 "\x00" // Segment Selector Size
243 "\x00\x00\x00\x00" // Padding
244 "\x00\x00\x00\x00" // Entry1: Premature
245 "\x00\x00\x00\x00" // terminator
246 "\x01\x00\x00\x00" // Entry2: Address
247 "\x01\x00\x00\x00" // Length
248 "\x00\x00\x00\x00" // Termination tuple
250 DWARFDataExtractor
Extractor(
251 StringRef(DebugArangesSecRaw
, sizeof(DebugArangesSecRaw
) - 1),
252 /*IsLittleEndian=*/true,
254 DWARFDebugArangeSet Set
;
256 WarningHandler Warnings
;
257 ASSERT_THAT_ERROR(Set
.extract(Extractor
, &Offset
, Warnings
), Succeeded());
258 auto Range
= Set
.descriptors();
259 auto Iter
= Range
.begin();
260 ASSERT_EQ(std::distance(Iter
, Range
.end()), 2);
261 EXPECT_EQ(Iter
->Address
, 0u);
262 EXPECT_EQ(Iter
->Length
, 0u);
264 EXPECT_EQ(Iter
->Address
, 1u);
265 EXPECT_EQ(Iter
->Length
, 1u);
267 Warnings
.getWarning(),
268 FailedWithMessage("address range table at offset 0x0 has a premature "
269 "terminator entry at offset 0x10"));
272 } // end anonymous namespace