1 //===- DWARFListTableTest.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/DWARFListTable.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
17 TEST(DWARFListTableHeader
, TruncatedLength
) {
18 static const char SecData
[] = "\x33\x22\x11"; // Truncated DWARF32 length
19 DWARFDataExtractor
Extractor(StringRef(SecData
, sizeof(SecData
) - 1),
20 /*isLittleEndian=*/true,
22 DWARFListTableHeader
Header(/*SectionName=*/".debug_rnglists",
23 /*ListTypeString=*/"range");
26 Header
.extract(Extractor
, &Offset
),
28 "parsing .debug_rnglists table at offset 0x0: unexpected end of data "
29 "at offset 0x3 while reading [0x0, 0x4)"));
30 // length() is expected to return 0 to indicate that the unit length field
31 // can not be parsed and so we can not, for example, skip the current set
32 // to continue parsing from the next one.
33 EXPECT_EQ(Header
.length(), 0u);
36 TEST(DWARFListTableHeader
, TruncatedLengthDWARF64
) {
37 static const char SecData
[] =
38 "\xff\xff\xff\xff" // DWARF64 mark
39 "\x55\x44\x33\x22\x11"; // Truncated DWARF64 length
40 DWARFDataExtractor
Extractor(StringRef(SecData
, sizeof(SecData
) - 1),
41 /*isLittleEndian=*/true,
43 DWARFListTableHeader
Header(/*SectionName=*/".debug_rnglists",
44 /*ListTypeString=*/"range");
47 Header
.extract(Extractor
, &Offset
),
49 "parsing .debug_rnglists table at offset 0x0: unexpected end of data "
50 "at offset 0x9 while reading [0x4, 0xc)"));
51 // length() is expected to return 0 to indicate that the unit length field
52 // can not be parsed and so we can not, for example, skip the current set
53 // to continue parsing from the next one.
54 EXPECT_EQ(Header
.length(), 0u);
57 TEST(DWARFListTableHeader
, TruncatedHeader
) {
58 static const char SecData
[] = "\x02\x00\x00\x00" // Length
59 "\x05\x00"; // Version
60 DWARFDataExtractor
Extractor(StringRef(SecData
, sizeof(SecData
) - 1),
61 /*isLittleEndian=*/true,
63 DWARFListTableHeader
Header(/*SectionName=*/".debug_rnglists",
64 /*ListTypeString=*/"range");
67 Header
.extract(Extractor
, &Offset
),
68 FailedWithMessage(".debug_rnglists table at offset 0x0 has too small "
69 "length (0x6) to contain a complete header"));
70 // length() is expected to return the full length of the set if the unit
71 // length field is read, even if an error occurred during the parsing,
72 // to allow skipping the current set and continue parsing from the next one.
73 EXPECT_EQ(Header
.length(), 6u);
76 TEST(DWARFListTableHeader
, OffsetEntryCount
) {
77 static const char SecData
[] = "\x10\x00\x00\x00" // Length
79 "\x08" // Address size
80 "\x00" // Segment selector size
81 "\x01\x00\x00\x00" // Offset entry count
82 "\x04\x00\x00\x00" // offset[0]
83 "\x04" // DW_RLE_offset_pair
84 "\x01" // ULEB128 starting offset
85 "\x02" // ULEB128 ending offset
86 "\x00"; // DW_RLE_end_of_list
87 DWARFDataExtractor
Extractor(StringRef(SecData
, sizeof(SecData
) - 1),
88 /*isLittleEndian=*/true,
90 DWARFListTableHeader
Header(/*SectionName=*/".debug_rnglists",
91 /*ListTypeString=*/"range");
93 EXPECT_FALSE(!!Header
.extract(Extractor
, &Offset
));
94 Optional
<uint64_t> Offset0
= Header
.getOffsetEntry(Extractor
, 0);
95 EXPECT_TRUE(!!Offset0
);
96 EXPECT_EQ(Offset0
, uint64_t(4));
97 Optional
<uint64_t> Offset1
= Header
.getOffsetEntry(Extractor
, 1);
98 EXPECT_FALSE(!!Offset1
);
99 EXPECT_EQ(Header
.length(), sizeof(SecData
) - 1);
102 } // end anonymous namespace