#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
struct WarningHandler {
~WarningHandler() { EXPECT_THAT_ERROR(std::move(Err), Succeeded()); }
void operator()(Error E) { Err = joinErrors(std::move(Err), std::move(E)); }
Error getWarning() { return std::move(Err); }
Error Err = Error::success();
};
template <size_t SecSize>
void ExpectExtractError(const char (&SecDataRaw)[SecSize],
const char *ErrorMessage) {
DWARFDataExtractor Extractor(StringRef(SecDataRaw, SecSize - 1),
true,
4);
DWARFDebugArangeSet Set;
uint64_t Offset = 0;
WarningHandler Warnings;
Error E = Set.extract(Extractor, &Offset, Warnings);
ASSERT_TRUE(E.operator bool());
EXPECT_STREQ(ErrorMessage, toString(std::move(E)).c_str());
}
TEST(DWARFDebugArangeSet, LengthExceedsSectionSize) {
static const char DebugArangesSecRaw[] =
"\x15\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
ExpectExtractError(
DebugArangesSecRaw,
"the length of address range table at offset 0x0 exceeds section size");
}
TEST(DWARFDebugArangeSet, LengthExceedsSectionSizeDWARF64) {
static const char DebugArangesSecRaw[] =
"\xff\xff\xff\xff" "\x15\x00\x00\x00\x00\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
ExpectExtractError(
DebugArangesSecRaw,
"the length of address range table at offset 0x0 exceeds section size");
}
TEST(DWARFDebugArangeSet, UnsupportedAddressSize) {
static const char DebugArangesSecRaw[] =
"\x0c\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x03" "\x00" "\x00\x00\x00\x00"; ExpectExtractError(
DebugArangesSecRaw,
"address range table at offset 0x0 has unsupported address size: 3 "
"(supported are 2, 4, 8)");
}
TEST(DWARFDebugArangeSet, UnsupportedSegmentSelectorSize) {
static const char DebugArangesSecRaw[] =
"\x14\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x04" "\x00\x00\x00\x00" "\x00\x00\x00\x00"
"\x00\x00\x00\x00";
ExpectExtractError(
DebugArangesSecRaw,
"non-zero segment selector size in address range table at offset 0x0 "
"is not supported");
}
TEST(DWARFDebugArangeSet, NoTerminationEntry) {
static const char DebugArangesSecRaw[] =
"\x14\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x01\x00\x00\x00" ; ExpectExtractError(
DebugArangesSecRaw,
"address range table at offset 0x0 is not terminated by null entry");
}
TEST(DWARFDebugArangeSet, ReservedUnitLength) {
static const char DebugArangesSecRaw[12 + 1] =
"\xf0\xff\xff\xff"; ExpectExtractError(DebugArangesSecRaw,
"parsing address ranges table at offset 0x0: unsupported "
"reserved unit length of value 0xfffffff0");
}
TEST(DWARFDebugArangeSet, SectionTooShort) {
static const char DebugArangesSecRaw[11 + 1] = {0};
ExpectExtractError(DebugArangesSecRaw,
"parsing address ranges table at offset 0x0: unexpected "
"end of data at offset 0xb while reading [0xb, 0xc)");
}
TEST(DWARFDebugArangeSet, SectionTooShortDWARF64) {
static const char DebugArangesSecRaw[23 + 1] =
"\xff\xff\xff\xff"; ExpectExtractError(DebugArangesSecRaw,
"parsing address ranges table at offset 0x0: unexpected "
"end of data at offset 0x17 while reading [0x17, 0x18)");
}
TEST(DWARFDebugArangeSet, NoSpaceForEntries) {
static const char DebugArangesSecRaw[] =
"\x0c\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" ; ExpectExtractError(
DebugArangesSecRaw,
"address range table at offset 0x0 has an insufficient length "
"to contain any entries");
}
TEST(DWARFDebugArangeSet, UnevenLength) {
static const char DebugArangesSecRaw[] =
"\x1b\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x01\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
ExpectExtractError(
DebugArangesSecRaw,
"address range table at offset 0x0 has length that is not a multiple "
"of the tuple size");
}
TEST(DWARFDebugArangeSet, ZeroAddressEntry) {
static const char DebugArangesSecRaw[] =
"\x1c\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x01\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
DWARFDataExtractor Extractor(
StringRef(DebugArangesSecRaw, sizeof(DebugArangesSecRaw) - 1),
true,
4);
DWARFDebugArangeSet Set;
uint64_t Offset = 0;
ASSERT_THAT_ERROR(Set.extract(Extractor, &Offset, WarningHandler()),
Succeeded());
auto Range = Set.descriptors();
auto Iter = Range.begin();
ASSERT_EQ(std::distance(Iter, Range.end()), 1);
EXPECT_EQ(Iter->Address, 0u);
EXPECT_EQ(Iter->Length, 1u);
}
TEST(DWARFDebugArangeSet, ZeroLengthEntry) {
static const char DebugArangesSecRaw[] =
"\x1c\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x01\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
DWARFDataExtractor Extractor(
StringRef(DebugArangesSecRaw, sizeof(DebugArangesSecRaw) - 1),
true,
4);
DWARFDebugArangeSet Set;
uint64_t Offset = 0;
ASSERT_THAT_ERROR(Set.extract(Extractor, &Offset, WarningHandler()),
Succeeded());
auto Range = Set.descriptors();
auto Iter = Range.begin();
ASSERT_EQ(std::distance(Iter, Range.end()), 1);
EXPECT_EQ(Iter->Address, 1u);
EXPECT_EQ(Iter->Length, 0u);
}
TEST(DWARFDebugArangesSet, PrematureTerminator) {
static const char DebugArangesSecRaw[] =
"\x24\x00\x00\x00" "\x02\x00" "\x00\x00\x00\x00" "\x04" "\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00" "\x01\x00\x00\x00" "\x01\x00\x00\x00" "\x00\x00\x00\x00" "\x00\x00\x00\x00";
DWARFDataExtractor Extractor(
StringRef(DebugArangesSecRaw, sizeof(DebugArangesSecRaw) - 1),
true,
4);
DWARFDebugArangeSet Set;
uint64_t Offset = 0;
WarningHandler Warnings;
ASSERT_THAT_ERROR(Set.extract(Extractor, &Offset, Warnings), Succeeded());
auto Range = Set.descriptors();
auto Iter = Range.begin();
ASSERT_EQ(std::distance(Iter, Range.end()), 2);
EXPECT_EQ(Iter->Address, 0u);
EXPECT_EQ(Iter->Length, 0u);
++Iter;
EXPECT_EQ(Iter->Address, 1u);
EXPECT_EQ(Iter->Length, 1u);
EXPECT_THAT_ERROR(
Warnings.getWarning(),
FailedWithMessage("address range table at offset 0x0 has a premature "
"terminator entry at offset 0x10"));
}
}