|
| 1 | +//===- llvm/unittest/DebugInfo/DWARFDebugAbbrevTest.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/DWARFDebugAbbrev.h" |
| 10 | +#include "llvm/BinaryFormat/Dwarf.h" |
| 11 | +#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" |
| 12 | +#include "llvm/Support/DataExtractor.h" |
| 13 | +#include "llvm/Support/LEB128.h" |
| 14 | +#include "llvm/Support/MemoryBuffer.h" |
| 15 | +#include "llvm/Testing/Support/Error.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +using namespace llvm; |
| 19 | +using namespace dwarf; |
| 20 | + |
| 21 | +enum Order : bool { InOrder, OutOfOrder }; |
| 22 | + |
| 23 | +void WriteAbbreviationDeclarations(raw_ostream &OS, uint32_t FirstCode, |
| 24 | + Order Ord) { |
| 25 | + encodeULEB128(FirstCode, OS); |
| 26 | + encodeULEB128(DW_TAG_compile_unit, OS); |
| 27 | + OS << static_cast<uint8_t>(DW_CHILDREN_yes); |
| 28 | + encodeULEB128(DW_AT_name, OS); |
| 29 | + encodeULEB128(DW_FORM_strp, OS); |
| 30 | + encodeULEB128(0, OS); |
| 31 | + encodeULEB128(0, OS); |
| 32 | + |
| 33 | + uint32_t SecondCode = Ord == Order::InOrder ? FirstCode + 1 : FirstCode - 1; |
| 34 | + |
| 35 | + encodeULEB128(SecondCode, OS); |
| 36 | + encodeULEB128(DW_TAG_subprogram, OS); |
| 37 | + OS << static_cast<uint8_t>(DW_CHILDREN_no); |
| 38 | + encodeULEB128(DW_AT_name, OS); |
| 39 | + encodeULEB128(DW_FORM_strp, OS); |
| 40 | + encodeULEB128(0, OS); |
| 41 | + encodeULEB128(0, OS); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccess) { |
| 45 | + SmallString<64> RawData; |
| 46 | + raw_svector_ostream OS(RawData); |
| 47 | + uint32_t FirstCode = 5; |
| 48 | + |
| 49 | + WriteAbbreviationDeclarations(OS, FirstCode, InOrder); |
| 50 | + encodeULEB128(0, OS); |
| 51 | + |
| 52 | + uint64_t Offset = 0; |
| 53 | + DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t)); |
| 54 | + DWARFAbbreviationDeclarationSet AbbrevSet; |
| 55 | + const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset); |
| 56 | + EXPECT_TRUE(DataWasExtracted); |
| 57 | + // The Abbreviation Declarations are in order and contiguous, so we want to |
| 58 | + // make sure that FirstAbbrCode was correctly set |
| 59 | + EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), FirstCode); |
| 60 | + |
| 61 | + const DWARFAbbreviationDeclaration *Abbrev5 = |
| 62 | + AbbrevSet.getAbbreviationDeclaration(FirstCode); |
| 63 | + EXPECT_TRUE(Abbrev5); |
| 64 | + EXPECT_EQ(Abbrev5->getTag(), DW_TAG_compile_unit); |
| 65 | + EXPECT_TRUE(Abbrev5->hasChildren()); |
| 66 | + EXPECT_EQ(Abbrev5->getNumAttributes(), 1u); |
| 67 | + |
| 68 | + const DWARFAbbreviationDeclaration *Abbrev6 = |
| 69 | + AbbrevSet.getAbbreviationDeclaration(FirstCode + 1); |
| 70 | + EXPECT_TRUE(Abbrev6); |
| 71 | + EXPECT_EQ(Abbrev6->getTag(), DW_TAG_subprogram); |
| 72 | + EXPECT_FALSE(Abbrev6->hasChildren()); |
| 73 | + EXPECT_EQ(Abbrev6->getNumAttributes(), 1u); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccessOutOfOrder) { |
| 77 | + SmallString<64> RawData; |
| 78 | + raw_svector_ostream OS(RawData); |
| 79 | + uint32_t FirstCode = 2; |
| 80 | + |
| 81 | + WriteAbbreviationDeclarations(OS, FirstCode, OutOfOrder); |
| 82 | + encodeULEB128(0, OS); |
| 83 | + |
| 84 | + uint64_t Offset = 0; |
| 85 | + DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t)); |
| 86 | + DWARFAbbreviationDeclarationSet AbbrevSet; |
| 87 | + const bool DataWasExtracted = AbbrevSet.extract(Data, &Offset); |
| 88 | + EXPECT_TRUE(DataWasExtracted); |
| 89 | + // The declarations are out of order, ensure that FirstAbbrCode is UINT32_MAX |
| 90 | + EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), UINT32_MAX); |
| 91 | + |
| 92 | + const DWARFAbbreviationDeclaration *Abbrev2 = |
| 93 | + AbbrevSet.getAbbreviationDeclaration(FirstCode); |
| 94 | + EXPECT_TRUE(Abbrev2); |
| 95 | + EXPECT_EQ(Abbrev2->getTag(), DW_TAG_compile_unit); |
| 96 | + EXPECT_TRUE(Abbrev2->hasChildren()); |
| 97 | + EXPECT_EQ(Abbrev2->getNumAttributes(), 1u); |
| 98 | + |
| 99 | + const DWARFAbbreviationDeclaration *Abbrev1 = |
| 100 | + AbbrevSet.getAbbreviationDeclaration(FirstCode - 1); |
| 101 | + EXPECT_TRUE(Abbrev1); |
| 102 | + EXPECT_EQ(Abbrev1->getTag(), DW_TAG_subprogram); |
| 103 | + EXPECT_FALSE(Abbrev1->hasChildren()); |
| 104 | + EXPECT_EQ(Abbrev1->getNumAttributes(), 1u); |
| 105 | +} |
0 commit comments