xref: /llvm-project/llvm/unittests/DebugInfo/DWARF/DWARFListTableTest.cpp (revision 89fab98e884f05076bbd420d95b5de3596f5452c)
1 //===- DWARFListTableTest.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/DWARFListTable.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
15 namespace {
16 
TEST(DWARFListTableHeader,TruncatedLength)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,
21                                /*AddrSize=*/4);
22   DWARFListTableHeader Header(/*SectionName=*/".debug_rnglists",
23                               /*ListTypeString=*/"range");
24   uint64_t Offset = 0;
25   EXPECT_THAT_ERROR(
26       Header.extract(Extractor, &Offset),
27       FailedWithMessage(
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);
34 }
35 
TEST(DWARFListTableHeader,TruncatedLengthDWARF64)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,
42                                /*AddrSize=*/4);
43   DWARFListTableHeader Header(/*SectionName=*/".debug_rnglists",
44                               /*ListTypeString=*/"range");
45   uint64_t Offset = 0;
46   EXPECT_THAT_ERROR(
47       Header.extract(Extractor, &Offset),
48       FailedWithMessage(
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);
55 }
56 
TEST(DWARFListTableHeader,TruncatedHeader)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,
62                                /*AddrSize=*/4);
63   DWARFListTableHeader Header(/*SectionName=*/".debug_rnglists",
64                               /*ListTypeString=*/"range");
65   uint64_t Offset = 0;
66   EXPECT_THAT_ERROR(
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);
74 }
75 
TEST(DWARFListTableHeader,OffsetEntryCount)76 TEST(DWARFListTableHeader, OffsetEntryCount) {
77   static const char SecData[] = "\x10\x00\x00\x00" // Length
78                                 "\x05\x00"         // Version
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,
89                                /*AddrSize=*/4);
90   DWARFListTableHeader Header(/*SectionName=*/".debug_rnglists",
91                               /*ListTypeString=*/"range");
92   uint64_t Offset = 0;
93   EXPECT_FALSE(!!Header.extract(Extractor, &Offset));
94   std::optional<uint64_t> Offset0 = Header.getOffsetEntry(Extractor, 0);
95   EXPECT_TRUE(!!Offset0);
96   EXPECT_EQ(Offset0, uint64_t(4));
97   std::optional<uint64_t> Offset1 = Header.getOffsetEntry(Extractor, 1);
98   EXPECT_FALSE(!!Offset1);
99   EXPECT_EQ(Header.length(), sizeof(SecData) - 1);
100 }
101 
102 } // end anonymous namespace
103