2017-05-04 00:02:29 +08:00
|
|
|
//===- DWARFVerifier.cpp --------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFSection.h"
|
2017-06-14 08:17:55 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
|
2017-05-04 00:02:29 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace dwarf;
|
|
|
|
using namespace object;
|
|
|
|
|
2017-07-14 07:25:24 +08:00
|
|
|
bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
|
2017-07-18 09:00:26 +08:00
|
|
|
uint32_t *Offset, unsigned UnitIndex,
|
|
|
|
uint8_t &UnitType, bool &isUnitDWARF64) {
|
2017-07-14 07:25:24 +08:00
|
|
|
uint32_t AbbrOffset, Length;
|
2017-07-18 09:00:26 +08:00
|
|
|
uint8_t AddrSize = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
uint16_t Version;
|
|
|
|
bool Success = true;
|
|
|
|
|
|
|
|
bool ValidLength = false;
|
|
|
|
bool ValidVersion = false;
|
|
|
|
bool ValidAddrSize = false;
|
|
|
|
bool ValidType = true;
|
|
|
|
bool ValidAbbrevOffset = true;
|
|
|
|
|
|
|
|
uint32_t OffsetStart = *Offset;
|
|
|
|
Length = DebugInfoData.getU32(Offset);
|
|
|
|
if (Length == UINT32_MAX) {
|
|
|
|
isUnitDWARF64 = true;
|
|
|
|
OS << format(
|
|
|
|
"Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
|
|
|
|
UnitIndex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Version = DebugInfoData.getU16(Offset);
|
|
|
|
|
|
|
|
if (Version >= 5) {
|
|
|
|
UnitType = DebugInfoData.getU8(Offset);
|
|
|
|
AddrSize = DebugInfoData.getU8(Offset);
|
|
|
|
AbbrOffset = DebugInfoData.getU32(Offset);
|
|
|
|
ValidType = DWARFUnit::isValidUnitType(UnitType);
|
|
|
|
} else {
|
2017-07-18 09:00:26 +08:00
|
|
|
UnitType = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
AbbrOffset = DebugInfoData.getU32(Offset);
|
|
|
|
AddrSize = DebugInfoData.getU8(Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
|
|
|
|
ValidAbbrevOffset = false;
|
|
|
|
|
|
|
|
ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
|
|
|
|
ValidVersion = DWARFContext::isSupportedVersion(Version);
|
|
|
|
ValidAddrSize = AddrSize == 4 || AddrSize == 8;
|
|
|
|
if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
|
|
|
|
!ValidType) {
|
|
|
|
Success = false;
|
|
|
|
OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
|
|
|
|
if (!ValidLength)
|
|
|
|
OS << "\tError: The length for this unit is too "
|
|
|
|
"large for the .debug_info provided.\n";
|
|
|
|
if (!ValidVersion)
|
|
|
|
OS << "\tError: The 16 bit unit header version is not valid.\n";
|
|
|
|
if (!ValidType)
|
|
|
|
OS << "\tError: The unit type encoding is not valid.\n";
|
|
|
|
if (!ValidAbbrevOffset)
|
|
|
|
OS << "\tError: The offset into the .debug_abbrev section is "
|
|
|
|
"not valid.\n";
|
|
|
|
if (!ValidAddrSize)
|
|
|
|
OS << "\tError: The address size is unsupported.\n";
|
|
|
|
}
|
|
|
|
*Offset = OffsetStart + Length + 4;
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
|
|
|
|
uint32_t NumUnitErrors = 0;
|
|
|
|
unsigned NumDies = Unit.getNumDIEs();
|
|
|
|
for (unsigned I = 0; I < NumDies; ++I) {
|
|
|
|
auto Die = Unit.getDIEAtIndex(I);
|
|
|
|
if (Die.getTag() == DW_TAG_null)
|
|
|
|
continue;
|
2017-07-25 05:04:11 +08:00
|
|
|
NumUnitErrors += verifyDieRanges(Die);
|
2017-07-18 09:00:26 +08:00
|
|
|
for (auto AttrValue : Die.attributes()) {
|
|
|
|
NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
|
|
|
|
NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NumUnitErrors == 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 08:51:32 +08:00
|
|
|
unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
|
2017-07-20 10:06:52 +08:00
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (Abbrev) {
|
|
|
|
const DWARFAbbreviationDeclarationSet *AbbrDecls =
|
|
|
|
Abbrev->getAbbreviationDeclarationSet(0);
|
|
|
|
for (auto AbbrDecl : *AbbrDecls) {
|
|
|
|
SmallDenseSet<uint16_t> AttributeSet;
|
|
|
|
for (auto Attribute : AbbrDecl.attributes()) {
|
|
|
|
auto Result = AttributeSet.insert(Attribute.Attr);
|
|
|
|
if (!Result.second) {
|
2017-07-21 08:51:32 +08:00
|
|
|
OS << "Error: Abbreviation declaration contains multiple "
|
|
|
|
<< AttributeString(Attribute.Attr) << " attributes.\n";
|
|
|
|
AbbrDecl.dump(OS);
|
2017-07-20 10:06:52 +08:00
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 08:51:32 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::handleDebugAbbrev() {
|
|
|
|
OS << "Verifying .debug_abbrev...\n";
|
|
|
|
|
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
|
|
|
bool noDebugAbbrev = DObj.getAbbrevSection().empty();
|
|
|
|
bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
|
|
|
|
|
|
|
|
if (noDebugAbbrev && noDebugAbbrevDWO) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (!noDebugAbbrev)
|
|
|
|
NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
|
|
|
|
|
|
|
|
if (!noDebugAbbrevDWO)
|
|
|
|
NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
|
2017-07-20 10:06:52 +08:00
|
|
|
return NumErrors == 0;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
bool DWARFVerifier::handleDebugInfo() {
|
2017-07-14 07:25:24 +08:00
|
|
|
OS << "Verifying .debug_info Unit Header Chain...\n";
|
|
|
|
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
|
|
|
DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
|
|
|
|
DCtx.isLittleEndian(), 0);
|
2017-07-18 09:00:26 +08:00
|
|
|
uint32_t NumDebugInfoErrors = 0;
|
|
|
|
uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
|
|
|
|
uint8_t UnitType = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
bool isUnitDWARF64 = false;
|
2017-07-18 09:00:26 +08:00
|
|
|
bool isHeaderChainValid = true;
|
2017-07-14 07:25:24 +08:00
|
|
|
bool hasDIE = DebugInfoData.isValidOffset(Offset);
|
|
|
|
while (hasDIE) {
|
2017-07-18 09:00:26 +08:00
|
|
|
OffsetStart = Offset;
|
|
|
|
if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
|
|
|
|
isUnitDWARF64)) {
|
|
|
|
isHeaderChainValid = false;
|
2017-07-14 07:25:24 +08:00
|
|
|
if (isUnitDWARF64)
|
|
|
|
break;
|
2017-07-18 09:00:26 +08:00
|
|
|
} else {
|
|
|
|
std::unique_ptr<DWARFUnit> Unit;
|
|
|
|
switch (UnitType) {
|
|
|
|
case dwarf::DW_UT_type:
|
|
|
|
case dwarf::DW_UT_split_type: {
|
|
|
|
DWARFUnitSection<DWARFTypeUnit> TUSection{};
|
|
|
|
Unit.reset(new DWARFTypeUnit(
|
2017-07-20 06:27:28 +08:00
|
|
|
DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
|
|
|
|
&DObj.getRangeSection(), DObj.getStringSection(),
|
|
|
|
DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
|
|
|
|
DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
|
2017-07-18 09:00:26 +08:00
|
|
|
nullptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case dwarf::DW_UT_skeleton:
|
|
|
|
case dwarf::DW_UT_split_compile:
|
|
|
|
case dwarf::DW_UT_compile:
|
|
|
|
case dwarf::DW_UT_partial:
|
|
|
|
// UnitType = 0 means that we are
|
|
|
|
// verifying a compile unit in DWARF v4.
|
|
|
|
case 0: {
|
|
|
|
DWARFUnitSection<DWARFCompileUnit> CUSection{};
|
|
|
|
Unit.reset(new DWARFCompileUnit(
|
2017-07-20 06:27:28 +08:00
|
|
|
DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
|
|
|
|
&DObj.getRangeSection(), DObj.getStringSection(),
|
|
|
|
DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
|
|
|
|
DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
|
2017-07-18 09:00:26 +08:00
|
|
|
nullptr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { llvm_unreachable("Invalid UnitType."); }
|
|
|
|
}
|
|
|
|
Unit->extract(DebugInfoData, &OffsetStart);
|
|
|
|
if (!verifyUnitContents(*Unit))
|
|
|
|
++NumDebugInfoErrors;
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
|
|
|
hasDIE = DebugInfoData.isValidOffset(Offset);
|
|
|
|
++UnitIdx;
|
|
|
|
}
|
|
|
|
if (UnitIdx == 0 && !hasDIE) {
|
|
|
|
OS << "Warning: .debug_info is empty.\n";
|
2017-07-18 09:00:26 +08:00
|
|
|
isHeaderChainValid = true;
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
NumDebugInfoErrors += verifyDebugInfoReferences();
|
|
|
|
return (isHeaderChainValid && NumDebugInfoErrors == 0);
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 05:04:11 +08:00
|
|
|
unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die) {
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
for (auto Range : Die.getAddressRanges()) {
|
|
|
|
if (Range.LowPC >= Range.HighPC) {
|
|
|
|
++NumErrors;
|
2017-07-25 13:03:17 +08:00
|
|
|
OS << format("error: Invalid address range [0x%08" PRIx64
|
|
|
|
" - 0x%08" PRIx64 "].\n",
|
2017-07-25 05:04:11 +08:00
|
|
|
Range.LowPC, Range.HighPC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
|
|
|
|
DWARFAttribute &AttrValue) {
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-05-04 02:25:46 +08:00
|
|
|
const auto Attr = AttrValue.Attr;
|
|
|
|
switch (Attr) {
|
|
|
|
case DW_AT_ranges:
|
|
|
|
// Make sure the offset in the DW_AT_ranges attribute is valid.
|
|
|
|
if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
|
2017-07-20 06:27:28 +08:00
|
|
|
if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
|
|
|
|
"bounds:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DW_AT_stmt_list:
|
|
|
|
// Make sure the offset in the DW_AT_stmt_list attribute is valid.
|
|
|
|
if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
|
2017-07-20 06:27:28 +08:00
|
|
|
if (*SectionOffset >= DObj.getLineSection().Data.size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
|
|
|
|
"bounds: "
|
2017-08-07 23:37:57 +08:00
|
|
|
<< format("0x%08" PRIx64, *SectionOffset) << "\n";
|
2017-05-04 02:25:46 +08:00
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
break;
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
|
|
|
|
DWARFAttribute &AttrValue) {
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-05-04 02:25:46 +08:00
|
|
|
const auto Form = AttrValue.Value.getForm();
|
|
|
|
switch (Form) {
|
|
|
|
case DW_FORM_ref1:
|
|
|
|
case DW_FORM_ref2:
|
|
|
|
case DW_FORM_ref4:
|
|
|
|
case DW_FORM_ref8:
|
|
|
|
case DW_FORM_ref_udata: {
|
|
|
|
// Verify all CU relative references are valid CU offsets.
|
|
|
|
Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
|
|
|
|
assert(RefVal);
|
|
|
|
if (RefVal) {
|
|
|
|
auto DieCU = Die.getDwarfUnit();
|
|
|
|
auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
|
|
|
|
auto CUOffset = AttrValue.Value.getRawUValue();
|
|
|
|
if (CUOffset >= CUSize) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: " << FormEncodingString(Form) << " CU offset "
|
2017-08-07 23:37:57 +08:00
|
|
|
<< format("0x%08" PRIx64, CUOffset)
|
2017-05-04 02:25:46 +08:00
|
|
|
<< " is invalid (must be less than CU size of "
|
|
|
|
<< format("0x%08" PRIx32, CUSize) << "):\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
} else {
|
|
|
|
// Valid reference, but we will verify it points to an actual
|
|
|
|
// DIE later.
|
|
|
|
ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DW_FORM_ref_addr: {
|
|
|
|
// Verify all absolute DIE references have valid offsets in the
|
|
|
|
// .debug_info section.
|
|
|
|
Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
|
|
|
|
assert(RefVal);
|
|
|
|
if (RefVal) {
|
2017-07-20 06:27:28 +08:00
|
|
|
if (*RefVal >= DObj.getInfoSection().Data.size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
|
|
|
|
"bounds:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
} else {
|
|
|
|
// Valid reference, but we will verify it points to an actual
|
|
|
|
// DIE later.
|
|
|
|
ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DW_FORM_strp: {
|
|
|
|
auto SecOffset = AttrValue.Value.getAsSectionOffset();
|
|
|
|
assert(SecOffset); // DW_FORM_strp is a section offset.
|
2017-07-20 06:27:28 +08:00
|
|
|
if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
break;
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoReferences() {
|
2017-05-04 00:02:29 +08:00
|
|
|
// Take all references and make sure they point to an actual DIE by
|
|
|
|
// getting the DIE by offset and emitting an error
|
|
|
|
OS << "Verifying .debug_info references...\n";
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-05-04 00:02:29 +08:00
|
|
|
for (auto Pair : ReferenceToDIEOffsets) {
|
|
|
|
auto Die = DCtx.getDIEForOffset(Pair.first);
|
|
|
|
if (Die)
|
|
|
|
continue;
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-05-04 00:02:29 +08:00
|
|
|
OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
|
|
|
|
<< ". Offset is in between DIEs:\n";
|
|
|
|
for (auto Offset : Pair.second) {
|
|
|
|
auto ReferencingDie = DCtx.getDIEForOffset(Offset);
|
|
|
|
ReferencingDie.dump(OS, 0);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
void DWARFVerifier::verifyDebugLineStmtOffsets() {
|
2017-05-04 00:02:29 +08:00
|
|
|
std::map<uint64_t, DWARFDie> StmtListToDie;
|
|
|
|
for (const auto &CU : DCtx.compile_units()) {
|
2017-05-04 02:25:46 +08:00
|
|
|
auto Die = CU->getUnitDIE();
|
2017-05-04 00:02:29 +08:00
|
|
|
// Get the attribute value as a section offset. No need to produce an
|
|
|
|
// error here if the encoding isn't correct because we validate this in
|
|
|
|
// the .debug_info verifier.
|
2017-05-04 02:25:46 +08:00
|
|
|
auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
|
2017-05-04 00:02:29 +08:00
|
|
|
if (!StmtSectionOffset)
|
|
|
|
continue;
|
|
|
|
const uint32_t LineTableOffset = *StmtSectionOffset;
|
2017-05-04 02:25:46 +08:00
|
|
|
auto LineTable = DCtx.getLineTableForUnit(CU.get());
|
2017-07-20 06:27:28 +08:00
|
|
|
if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
|
2017-05-04 02:25:46 +08:00
|
|
|
if (!LineTable) {
|
|
|
|
++NumDebugLineErrors;
|
|
|
|
OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
|
|
|
|
<< "] was not able to be parsed for CU:\n";
|
|
|
|
Die.dump(OS, 0);
|
|
|
|
OS << '\n';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't get a valid line table back if the offset is wrong.
|
|
|
|
assert(LineTable == nullptr);
|
2017-05-04 00:02:29 +08:00
|
|
|
// Skip this line table as it isn't valid. No need to create an error
|
|
|
|
// here because we validate this in the .debug_info verifier.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto Iter = StmtListToDie.find(LineTableOffset);
|
|
|
|
if (Iter != StmtListToDie.end()) {
|
|
|
|
++NumDebugLineErrors;
|
|
|
|
OS << "error: two compile unit DIEs, "
|
|
|
|
<< format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
|
2017-05-04 02:25:46 +08:00
|
|
|
<< format("0x%08" PRIx32, Die.getOffset())
|
2017-05-04 00:02:29 +08:00
|
|
|
<< ", have the same DW_AT_stmt_list section offset:\n";
|
|
|
|
Iter->second.dump(OS, 0);
|
2017-05-04 02:25:46 +08:00
|
|
|
Die.dump(OS, 0);
|
2017-05-04 00:02:29 +08:00
|
|
|
OS << '\n';
|
|
|
|
// Already verified this line table before, no need to do it again.
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
StmtListToDie[LineTableOffset] = Die;
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
void DWARFVerifier::verifyDebugLineRows() {
|
|
|
|
for (const auto &CU : DCtx.compile_units()) {
|
|
|
|
auto Die = CU->getUnitDIE();
|
2017-05-04 00:02:29 +08:00
|
|
|
auto LineTable = DCtx.getLineTableForUnit(CU.get());
|
2017-05-04 02:25:46 +08:00
|
|
|
// If there is no line table we will have created an error in the
|
|
|
|
// .debug_info verifier or in verifyDebugLineStmtOffsets().
|
|
|
|
if (!LineTable)
|
2017-05-04 00:02:29 +08:00
|
|
|
continue;
|
|
|
|
uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
|
|
|
|
uint64_t PrevAddress = 0;
|
|
|
|
uint32_t RowIndex = 0;
|
|
|
|
for (const auto &Row : LineTable->Rows) {
|
|
|
|
if (Row.Address < PrevAddress) {
|
|
|
|
++NumDebugLineErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: .debug_line["
|
2017-08-07 23:37:57 +08:00
|
|
|
<< format("0x%08" PRIx64,
|
2017-05-04 02:25:46 +08:00
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
2017-05-04 00:02:29 +08:00
|
|
|
<< "] row[" << RowIndex
|
|
|
|
<< "] decreases in address from previous row:\n";
|
|
|
|
|
|
|
|
DWARFDebugLine::Row::dumpTableHeader(OS);
|
|
|
|
if (RowIndex > 0)
|
|
|
|
LineTable->Rows[RowIndex - 1].dump(OS);
|
|
|
|
Row.dump(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Row.File > MaxFileIndex) {
|
|
|
|
++NumDebugLineErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
OS << "error: .debug_line["
|
2017-08-07 23:37:57 +08:00
|
|
|
<< format("0x%08" PRIx64,
|
2017-05-04 02:25:46 +08:00
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
2017-05-04 00:02:29 +08:00
|
|
|
<< "][" << RowIndex << "] has invalid file index " << Row.File
|
|
|
|
<< " (valid values are [1," << MaxFileIndex << "]):\n";
|
|
|
|
DWARFDebugLine::Row::dumpTableHeader(OS);
|
|
|
|
Row.dump(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
if (Row.EndSequence)
|
|
|
|
PrevAddress = 0;
|
|
|
|
else
|
|
|
|
PrevAddress = Row.Address;
|
|
|
|
++RowIndex;
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::handleDebugLine() {
|
|
|
|
NumDebugLineErrors = 0;
|
|
|
|
OS << "Verifying .debug_line...\n";
|
|
|
|
verifyDebugLineStmtOffsets();
|
|
|
|
verifyDebugLineRows();
|
2017-05-04 00:02:29 +08:00
|
|
|
return NumDebugLineErrors == 0;
|
|
|
|
}
|
2017-06-14 08:17:55 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
unsigned DWARFVerifier::verifyAccelTable(const DWARFSection *AccelSection,
|
|
|
|
DataExtractor *StrData,
|
|
|
|
const char *SectionName) {
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
|
|
|
|
DCtx.isLittleEndian(), 0);
|
|
|
|
DWARFAcceleratorTable AccelTable(AccelSectionData, *StrData);
|
2017-06-14 08:17:55 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
OS << "Verifying " << SectionName << "...\n";
|
|
|
|
// Verify that the fixed part of the header is not too short.
|
2017-06-17 06:03:21 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
|
|
|
|
OS << "\terror: Section is too small to fit a section header.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Verify that the section is not too short.
|
|
|
|
if (!AccelTable.extract()) {
|
|
|
|
OS << "\terror: Section is smaller than size described in section header.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2017-06-30 04:13:05 +08:00
|
|
|
// Verify that all buckets have a valid hash index or are empty.
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t NumBuckets = AccelTable.getNumBuckets();
|
|
|
|
uint32_t NumHashes = AccelTable.getNumHashes();
|
2017-06-14 08:17:55 +08:00
|
|
|
|
|
|
|
uint32_t BucketsOffset =
|
2017-07-26 08:52:31 +08:00
|
|
|
AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
|
2017-06-30 04:13:05 +08:00
|
|
|
uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
|
|
|
|
uint32_t OffsetsBase = HashesBase + NumHashes * 4;
|
2017-06-14 08:17:55 +08:00
|
|
|
for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
|
2017-06-14 08:17:55 +08:00
|
|
|
if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
|
2017-07-26 08:52:31 +08:00
|
|
|
OS << format("\terror: Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
|
2017-06-30 04:13:05 +08:00
|
|
|
HashIdx);
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-06-14 08:17:55 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
|
2017-06-30 04:13:05 +08:00
|
|
|
if (NumAtoms == 0) {
|
2017-07-26 08:52:31 +08:00
|
|
|
OS << "\terror: no atoms; failed to read HashData.\n";
|
|
|
|
return 1;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!AccelTable.validateForms()) {
|
|
|
|
OS << "\terror: unsupported form; failed to read HashData.\n";
|
|
|
|
return 1;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
|
|
|
|
uint32_t HashOffset = HashesBase + 4 * HashIdx;
|
|
|
|
uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t Hash = AccelSectionData.getU32(&HashOffset);
|
|
|
|
uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
|
|
|
|
if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
|
|
|
|
sizeof(uint64_t))) {
|
|
|
|
OS << format("\terror: Hash[%d] has invalid HashData offset: 0x%08x.\n",
|
2017-06-30 04:13:05 +08:00
|
|
|
HashIdx, HashDataOffset);
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t StrpOffset;
|
|
|
|
uint32_t StringOffset;
|
|
|
|
uint32_t StringCount = 0;
|
2017-08-01 02:01:16 +08:00
|
|
|
unsigned Offset;
|
|
|
|
unsigned Tag;
|
2017-07-26 08:52:31 +08:00
|
|
|
while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
|
2017-06-30 04:13:05 +08:00
|
|
|
const uint32_t NumHashDataObjects =
|
2017-07-26 08:52:31 +08:00
|
|
|
AccelSectionData.getU32(&HashDataOffset);
|
2017-06-30 04:13:05 +08:00
|
|
|
for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
|
|
|
|
++HashDataIdx) {
|
2017-08-01 02:01:16 +08:00
|
|
|
std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
|
|
|
|
auto Die = DCtx.getDIEForOffset(Offset);
|
|
|
|
if (!Die) {
|
2017-06-30 04:13:05 +08:00
|
|
|
const uint32_t BucketIdx =
|
|
|
|
NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
|
|
|
|
StringOffset = StrpOffset;
|
2017-07-26 08:52:31 +08:00
|
|
|
const char *Name = StrData->getCStr(&StringOffset);
|
2017-06-30 04:13:05 +08:00
|
|
|
if (!Name)
|
|
|
|
Name = "<NULL>";
|
|
|
|
|
|
|
|
OS << format(
|
2017-07-26 08:52:31 +08:00
|
|
|
"\terror: %s Bucket[%d] Hash[%d] = 0x%08x "
|
2017-06-30 04:13:05 +08:00
|
|
|
"Str[%u] = 0x%08x "
|
|
|
|
"DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
|
2017-07-26 08:52:31 +08:00
|
|
|
SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
|
2017-08-01 02:01:16 +08:00
|
|
|
HashDataIdx, Offset, Name);
|
2017-06-30 04:13:05 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-08-01 02:01:16 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
|
|
|
|
OS << "\terror: Tag " << dwarf::TagString(Tag)
|
|
|
|
<< " in accelerator table does not match Tag "
|
|
|
|
<< dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
|
|
|
|
<< "].\n";
|
|
|
|
++NumErrors;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++StringCount;
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::handleAccelTables() {
|
|
|
|
const DWARFObject &D = DCtx.getDWARFObj();
|
|
|
|
DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (!D.getAppleNamesSection().Data.empty())
|
|
|
|
NumErrors +=
|
|
|
|
verifyAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
|
|
|
|
if (!D.getAppleTypesSection().Data.empty())
|
|
|
|
NumErrors +=
|
|
|
|
verifyAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
|
|
|
|
if (!D.getAppleNamespacesSection().Data.empty())
|
|
|
|
NumErrors += verifyAccelTable(&D.getAppleNamespacesSection(), &StrData,
|
|
|
|
".apple_namespaces");
|
|
|
|
if (!D.getAppleObjCSection().Data.empty())
|
|
|
|
NumErrors +=
|
|
|
|
verifyAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
|
|
|
|
return NumErrors == 0;
|
2017-06-14 08:17:55 +08:00
|
|
|
}
|