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 DataExtractor 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( 115 DebugArangesSecRaw, 116 "address range table at offset 0x0 has unsupported reserved unit length " 117 "of value 0xfffffff0"); 118 } 119 120 TEST(DWARFDebugArangeSet, SectionTooShort) { 121 // Note: 1 will be automatically subtracted in ExpectExtractError(). 122 static const char DebugArangesSecRaw[11 + 1] = {0}; 123 ExpectExtractError( 124 DebugArangesSecRaw, 125 "section is not large enough to contain an address range table " 126 "at offset 0x0"); 127 } 128 129 TEST(DWARFDebugArangeSet, SectionTooShortDWARF64) { 130 // Note: 1 will be automatically subtracted in ExpectExtractError(). 131 static const char DebugArangesSecRaw[23 + 1] = 132 "\xff\xff\xff\xff"; // DWARF64 mark 133 ExpectExtractError( 134 DebugArangesSecRaw, 135 "section is not large enough to contain a DWARF64 address range table " 136 "at offset 0x0"); 137 } 138 139 TEST(DWARFDebugArangeSet, NoSpaceForEntries) { 140 static const char DebugArangesSecRaw[] = 141 "\x0c\x00\x00\x00" // Length 142 "\x02\x00" // Version 143 "\x00\x00\x00\x00" // Debug Info Offset 144 "\x04" // Address Size 145 "\x00" // Segment Selector Size 146 "\x00\x00\x00\x00" // Padding 147 ; // No entries 148 ExpectExtractError( 149 DebugArangesSecRaw, 150 "address range table at offset 0x0 has an insufficient length " 151 "to contain any entries"); 152 } 153 154 TEST(DWARFDebugArangeSet, UnevenLength) { 155 static const char DebugArangesSecRaw[] = 156 "\x1b\x00\x00\x00" // Length (not a multiple of tuple size) 157 "\x02\x00" // Version 158 "\x00\x00\x00\x00" // Debug Info Offset 159 "\x04" // Address Size 160 "\x00" // Segment Selector Size 161 "\x00\x00\x00\x00" // Padding 162 "\x00\x00\x00\x00" // Entry: Address 163 "\x01\x00\x00\x00" // Length 164 "\x00\x00\x00\x00" // Termination tuple 165 "\x00\x00\x00\x00"; 166 ExpectExtractError( 167 DebugArangesSecRaw, 168 "address range table at offset 0x0 has length that is not a multiple " 169 "of the tuple size"); 170 } 171 172 TEST(DWARFDebugArangeSet, ZeroLengthEntry) { 173 static const char DebugArangesSecRaw[] = 174 "\x24\x00\x00\x00" // Length 175 "\x02\x00" // Version 176 "\x00\x00\x00\x00" // Debug Info Offset 177 "\x04" // Address Size 178 "\x00" // Segment Selector Size 179 "\x00\x00\x00\x00" // Padding 180 "\x00\x00\x00\x00" // Entry1: Address 181 "\x01\x00\x00\x00" // Length 182 "\x01\x00\x00\x00" // Entry2: Address 183 "\x00\x00\x00\x00" // Length (invalid) 184 "\x00\x00\x00\x00" // Termination tuple 185 "\x00\x00\x00\x00"; 186 ExpectExtractError( 187 DebugArangesSecRaw, 188 "address range table at offset 0x0 has an invalid tuple (length = 0) " 189 "at offset 0x18"); 190 } 191 192 } // end anonymous namespace 193