brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.7 KiB · 1aedd74 Raw
103 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/DebugInfo/DWARF/DWARFListTable.h"10#include "llvm/Testing/Support/Error.h"11#include "gtest/gtest.h"12 13using namespace llvm;14 15namespace {16 17TEST(DWARFListTableHeader, TruncatedLength) {18  static const char SecData[] = "\x33\x22\x11"; // Truncated DWARF32 length19  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 field31  // can not be parsed and so we can not, for example, skip the current set32  // to continue parsing from the next one.33  EXPECT_EQ(Header.length(), 0u);34}35 36TEST(DWARFListTableHeader, TruncatedLengthDWARF64) {37  static const char SecData[] =38      "\xff\xff\xff\xff"      // DWARF64 mark39      "\x55\x44\x33\x22\x11"; // Truncated DWARF64 length40  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 field52  // can not be parsed and so we can not, for example, skip the current set53  // to continue parsing from the next one.54  EXPECT_EQ(Header.length(), 0u);55}56 57TEST(DWARFListTableHeader, TruncatedHeader) {58  static const char SecData[] = "\x02\x00\x00\x00" // Length59                                "\x05\x00";        // Version60  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 unit71  // 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 76TEST(DWARFListTableHeader, OffsetEntryCount) {77  static const char SecData[] = "\x10\x00\x00\x00" // Length78                                "\x05\x00"         // Version79                                "\x08"             // Address size80                                "\x00"             // Segment selector size81                                "\x01\x00\x00\x00" // Offset entry count82                                "\x04\x00\x00\x00" // offset[0]83                                "\x04"             // DW_RLE_offset_pair84                                "\x01"             // ULEB128 starting offset85                                "\x02"             // ULEB128 ending offset86                                "\x00";            // DW_RLE_end_of_list87  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 namespace103