1 //===- llvm/unittest/DebugInfo/DWARFDebugArangeSetTest.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/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
16 template <size_t SecSize>
17 void ExpectExtractError(const char (&SecDataRaw)[SecSize],
18                         const char *ErrorMessage) {
19   DWARFDataExtractor Extractor(StringRef(SecDataRaw, SecSize - 1),
20                                /* IsLittleEndian = */ true,
21                                /* AddressSize = */ 4);
22   DWARFDebugArangeSet Set;
23   uint64_t Offset = 0;
24   Error E = Set.extract(Extractor, &Offset);
25   ASSERT_TRUE(E.operator bool());
26   EXPECT_STREQ(ErrorMessage, toString(std::move(E)).c_str());
27 }
28 
29 TEST(DWARFDebugArangeSet, LengthExceedsSectionSize) {
30   static const char DebugArangesSecRaw[] =
31       "\x15\x00\x00\x00" // The length exceeds the section boundaries
32       "\x02\x00"         // Version
33       "\x00\x00\x00\x00" // Debug Info Offset
34       "\x04"             // Address Size
35       "\x00"             // Segment Selector Size
36       "\x00\x00\x00\x00" // Padding
37       "\x00\x00\x00\x00" // Termination tuple
38       "\x00\x00\x00\x00";
39   ExpectExtractError(
40       DebugArangesSecRaw,
41       "the length of address range table at offset 0x0 exceeds section size");
42 }
43 
44 TEST(DWARFDebugArangeSet, LengthExceedsSectionSizeDWARF64) {
45   static const char DebugArangesSecRaw[] =
46       "\xff\xff\xff\xff"                 // DWARF64 mark
47       "\x15\x00\x00\x00\x00\x00\x00\x00" // The length exceeds the section
48                                          // boundaries
49       "\x02\x00"                         // Version
50       "\x00\x00\x00\x00\x00\x00\x00\x00" // Debug Info Offset
51       "\x04"                             // Address Size
52       "\x00"                             // Segment Selector Size
53                                          // No padding
54       "\x00\x00\x00\x00"                 // Termination tuple
55       "\x00\x00\x00\x00";
56   ExpectExtractError(
57       DebugArangesSecRaw,
58       "the length of address range table at offset 0x0 exceeds section size");
59 }
60 
61 TEST(DWARFDebugArangeSet, UnsupportedAddressSize) {
62   static const char DebugArangesSecRaw[] =
63       "\x0c\x00\x00\x00"  // Length
64       "\x02\x00"          // Version
65       "\x00\x00\x00\x00"  // Debug Info Offset
66       "\x02"              // Address Size (not supported)
67       "\x00"              // Segment Selector Size
68                           // No padding
69       "\x00\x00\x00\x00"; // Termination tuple
70   ExpectExtractError(
71       DebugArangesSecRaw,
72       "address range table at offset 0x0 has unsupported address size: 2 "
73       "(4 and 8 supported)");
74 }
75 
76 TEST(DWARFDebugArangeSet, UnsupportedSegmentSelectorSize) {
77   static const char DebugArangesSecRaw[] =
78       "\x14\x00\x00\x00" // Length
79       "\x02\x00"         // Version
80       "\x00\x00\x00\x00" // Debug Info Offset
81       "\x04"             // Address Size
82       "\x04"             // Segment Selector Size (not supported)
83                          // No padding
84       "\x00\x00\x00\x00" // Termination tuple
85       "\x00\x00\x00\x00"
86       "\x00\x00\x00\x00";
87   ExpectExtractError(
88       DebugArangesSecRaw,
89       "non-zero segment selector size in address range table at offset 0x0 "
90       "is not supported");
91 }
92 
93 TEST(DWARFDebugArangeSet, NoTerminationEntry) {
94   static const char DebugArangesSecRaw[] =
95       "\x14\x00\x00\x00" // Length
96       "\x02\x00"         // Version
97       "\x00\x00\x00\x00" // Debug Info Offset
98       "\x04"             // Address Size
99       "\x00"             // Segment Selector Size
100       "\x00\x00\x00\x00" // Padding
101       "\x00\x00\x00\x00" // Entry: Address
102       "\x01\x00\x00\x00" //        Length
103       ;                  // No termination tuple
104   ExpectExtractError(
105       DebugArangesSecRaw,
106       "address range table at offset 0x0 is not terminated by null entry");
107 }
108 
109 TEST(DWARFDebugArangeSet, ReservedUnitLength) {
110   // Note: 12 is the minimum length to pass the basic check for the size of
111   // the section. 1 will be automatically subtracted in ExpectExtractError().
112   static const char DebugArangesSecRaw[12 + 1] =
113       "\xf0\xff\xff\xff"; // Reserved unit length value
114   ExpectExtractError(DebugArangesSecRaw,
115                      "parsing address ranges table at offset 0x0: unsupported "
116                      "reserved unit length of value 0xfffffff0");
117 }
118 
119 TEST(DWARFDebugArangeSet, SectionTooShort) {
120   // Note: 1 will be automatically subtracted in ExpectExtractError().
121   static const char DebugArangesSecRaw[11 + 1] = {0};
122   ExpectExtractError(DebugArangesSecRaw,
123                      "parsing address ranges table at offset 0x0: unexpected "
124                      "end of data at offset 0xb while reading [0xb, 0xc)");
125 }
126 
127 TEST(DWARFDebugArangeSet, SectionTooShortDWARF64) {
128   // Note: 1 will be automatically subtracted in ExpectExtractError().
129   static const char DebugArangesSecRaw[23 + 1] =
130       "\xff\xff\xff\xff"; // DWARF64 mark
131   ExpectExtractError(DebugArangesSecRaw,
132                      "parsing address ranges table at offset 0x0: unexpected "
133                      "end of data at offset 0x17 while reading [0x17, 0x18)");
134 }
135 
136 TEST(DWARFDebugArangeSet, NoSpaceForEntries) {
137   static const char DebugArangesSecRaw[] =
138       "\x0c\x00\x00\x00" // Length
139       "\x02\x00"         // Version
140       "\x00\x00\x00\x00" // Debug Info Offset
141       "\x04"             // Address Size
142       "\x00"             // Segment Selector Size
143       "\x00\x00\x00\x00" // Padding
144       ;                  // No entries
145   ExpectExtractError(
146       DebugArangesSecRaw,
147       "address range table at offset 0x0 has an insufficient length "
148       "to contain any entries");
149 }
150 
151 TEST(DWARFDebugArangeSet, UnevenLength) {
152   static const char DebugArangesSecRaw[] =
153       "\x1b\x00\x00\x00" // Length (not a multiple of tuple size)
154       "\x02\x00"         // Version
155       "\x00\x00\x00\x00" // Debug Info Offset
156       "\x04"             // Address Size
157       "\x00"             // Segment Selector Size
158       "\x00\x00\x00\x00" // Padding
159       "\x00\x00\x00\x00" // Entry: Address
160       "\x01\x00\x00\x00" //        Length
161       "\x00\x00\x00\x00" // Termination tuple
162       "\x00\x00\x00\x00";
163   ExpectExtractError(
164       DebugArangesSecRaw,
165       "address range table at offset 0x0 has length that is not a multiple "
166       "of the tuple size");
167 }
168 
169 TEST(DWARFDebugArangeSet, ZeroLengthEntry) {
170   static const char DebugArangesSecRaw[] =
171       "\x24\x00\x00\x00" // Length
172       "\x02\x00"         // Version
173       "\x00\x00\x00\x00" // Debug Info Offset
174       "\x04"             // Address Size
175       "\x00"             // Segment Selector Size
176       "\x00\x00\x00\x00" // Padding
177       "\x00\x00\x00\x00" // Entry1: Address
178       "\x01\x00\x00\x00" //         Length
179       "\x01\x00\x00\x00" // Entry2: Address
180       "\x00\x00\x00\x00" //         Length (invalid)
181       "\x00\x00\x00\x00" // Termination tuple
182       "\x00\x00\x00\x00";
183   ExpectExtractError(
184       DebugArangesSecRaw,
185       "address range table at offset 0x0 has an invalid tuple (length = 0) "
186       "at offset 0x18");
187 }
188 
189 } // end anonymous namespace
190