1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
//===- llvm/unittest/DebugInfo/DWARFDebugAbbrevTest.cpp -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace dwarf;
enum OrderKind : bool { InOrder, OutOfOrder };
void writeValidAbbreviationDeclarations(raw_ostream &OS, uint32_t FirstCode,
OrderKind Order) {
encodeULEB128(FirstCode, OS);
encodeULEB128(DW_TAG_compile_unit, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_yes);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(DW_FORM_strp, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
uint32_t SecondCode =
Order == OrderKind::InOrder ? FirstCode + 1 : FirstCode - 1;
encodeULEB128(SecondCode, OS);
encodeULEB128(DW_TAG_subprogram, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(DW_FORM_strp, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
}
void writeMalformedULEB128Value(raw_ostream &OS) {
OS << static_cast<uint8_t>(0x80);
}
void writeULEB128LargerThan64Bits(raw_ostream &OS) {
static constexpr llvm::StringRef LargeULEB128 =
"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01";
OS << LargeULEB128;
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccess) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 5;
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
encodeULEB128(0, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(AbbrevSet.extract(Data, &Offset), Succeeded());
// The Abbreviation Declarations are in order and contiguous, so we want to
// make sure that FirstAbbrCode was correctly set.
EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), FirstCode);
const DWARFAbbreviationDeclaration *Abbrev5 =
AbbrevSet.getAbbreviationDeclaration(FirstCode);
ASSERT_TRUE(Abbrev5);
EXPECT_EQ(Abbrev5->getTag(), DW_TAG_compile_unit);
EXPECT_TRUE(Abbrev5->hasChildren());
EXPECT_EQ(Abbrev5->getNumAttributes(), 1u);
const DWARFAbbreviationDeclaration *Abbrev6 =
AbbrevSet.getAbbreviationDeclaration(FirstCode + 1);
ASSERT_TRUE(Abbrev6);
EXPECT_EQ(Abbrev6->getTag(), DW_TAG_subprogram);
EXPECT_FALSE(Abbrev6->hasChildren());
EXPECT_EQ(Abbrev6->getNumAttributes(), 1u);
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetExtractSuccessOutOfOrder) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 2;
writeValidAbbreviationDeclarations(OS, FirstCode, OutOfOrder);
encodeULEB128(0, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(AbbrevSet.extract(Data, &Offset), Succeeded());
// The declarations are out of order, ensure that FirstAbbrCode is UINT32_MAX.
EXPECT_EQ(AbbrevSet.getFirstAbbrCode(), UINT32_MAX);
const DWARFAbbreviationDeclaration *Abbrev2 =
AbbrevSet.getAbbreviationDeclaration(FirstCode);
ASSERT_TRUE(Abbrev2);
EXPECT_EQ(Abbrev2->getTag(), DW_TAG_compile_unit);
EXPECT_TRUE(Abbrev2->hasChildren());
EXPECT_EQ(Abbrev2->getNumAttributes(), 1u);
const DWARFAbbreviationDeclaration *Abbrev1 =
AbbrevSet.getAbbreviationDeclaration(FirstCode - 1);
ASSERT_TRUE(Abbrev1);
EXPECT_EQ(Abbrev1->getTag(), DW_TAG_subprogram);
EXPECT_FALSE(Abbrev1->hasChildren());
EXPECT_EQ(Abbrev1->getNumAttributes(), 1u);
}
TEST(DWARFDebugAbbrevTest, DWARFAbbreviationDeclSetCodeExtractionError) {
SmallString<64> RawData;
// Check for malformed ULEB128.
{
raw_svector_ostream OS(RawData);
writeMalformedULEB128Value(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
"malformed uleb128, extends past end"));
EXPECT_EQ(Offset, 0u);
}
RawData.clear();
// Check for ULEB128 too large to fit into a uin64_t.
{
raw_svector_ostream OS(RawData);
writeULEB128LargerThan64Bits(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
"uleb128 too big for uint64"));
EXPECT_EQ(Offset, 0u);
}
}
TEST(DWARFDebugAbbrevTest, DWARFAbbreviationDeclSetTagExtractionError) {
SmallString<64> RawData;
const uint32_t Code = 1;
// Check for malformed ULEB128.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
writeMalformedULEB128Value(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
"malformed uleb128, extends past end"));
// Only the code was extracted correctly.
EXPECT_EQ(Offset, 1u);
}
RawData.clear();
// Check for ULEB128 too large to fit into a uint64_t.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
writeULEB128LargerThan64Bits(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
"uleb128 too big for uint64"));
// Only the code was extracted correctly.
EXPECT_EQ(Offset, 1u);
}
}
TEST(DWARFDebugAbbrevTest, DWARFAbbreviationDeclSetChildExtractionError) {
SmallString<64> RawData;
const uint32_t Code = 1;
const dwarf::Tag Tag = DW_TAG_compile_unit;
// We want to make sure that we fail if we reach the end of the stream before
// reading the 'children' byte.
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
encodeULEB128(Tag, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage(
"unexpected end of data at offset 0x2 while reading [0x2, 0x3)"));
// The code and the tag were extracted correctly.
EXPECT_EQ(Offset, 2u);
}
TEST(DWARFDebugAbbrevTest, DWARFAbbreviationDeclSetAttributeExtractionError) {
SmallString<64> RawData;
const uint32_t Code = 1;
const dwarf::Tag Tag = DW_TAG_compile_unit;
const uint8_t Children = DW_CHILDREN_yes;
// Check for malformed ULEB128.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
encodeULEB128(Tag, OS);
OS << Children;
writeMalformedULEB128Value(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000003: "
"malformed uleb128, extends past end"));
// The code, tag, and child byte were extracted correctly.
EXPECT_EQ(Offset, 3u);
}
RawData.clear();
// Check for ULEB128 too large to fit into a uint64_t.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
encodeULEB128(Tag, OS);
OS << Children;
writeULEB128LargerThan64Bits(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000003: "
"uleb128 too big for uint64"));
// The code, tag, and child byte were extracted correctly.
EXPECT_EQ(Offset, 3u);
}
}
TEST(DWARFDebugAbbrevTest, DWARFAbbreviationDeclSetFormExtractionError) {
SmallString<64> RawData;
const uint32_t Code = 1;
const dwarf::Tag Tag = DW_TAG_compile_unit;
const uint8_t Children = DW_CHILDREN_yes;
const dwarf::Attribute Attr = DW_AT_name;
// Check for malformed ULEB128.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
encodeULEB128(Tag, OS);
OS << Children;
encodeULEB128(Attr, OS);
writeMalformedULEB128Value(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000004: "
"malformed uleb128, extends past end"));
// The code, tag, child byte, and first attribute were extracted correctly.
EXPECT_EQ(Offset, 4u);
}
RawData.clear();
// Check for ULEB128 too large to fit into a uint64_t.
{
raw_svector_ostream OS(RawData);
encodeULEB128(Code, OS);
encodeULEB128(Tag, OS);
OS << Children;
encodeULEB128(Attr, OS);
writeULEB128LargerThan64Bits(OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
ASSERT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("unable to decode LEB128 at offset 0x00000004: "
"uleb128 too big for uint64"));
// The code, tag, child byte, and first attribute were extracted correctly.
EXPECT_EQ(Offset, 4u);
}
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetInvalidTag) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 1;
// First, we're going to manually add good data.
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
// Afterwards, we're going to write an Abbreviation Decl manually with an
// invalid tag.
encodeULEB128(FirstCode + 2, OS);
encodeULEB128(0, OS); // Invalid Tag
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(DW_FORM_strp, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
EXPECT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage("abbreviation declaration requires a non-null tag"));
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetInvalidAttrValidForm) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 120;
// First, we're going to manually add good data.
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
// Afterwards, we're going to write an Abbreviation Decl manually with an
// invalid attribute but valid form.
encodeULEB128(FirstCode - 5, OS);
encodeULEB128(DW_TAG_compile_unit, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(0, OS); // Invalid attribute followed by an invalid form.
encodeULEB128(DW_FORM_strp, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
EXPECT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage(
"malformed abbreviation declaration attribute. Either the "
"attribute or the form is zero while the other is not"));
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetValidAttrInvalidForm) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 120;
// First, we're going to manually add good data.
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
// Afterwards, we're going to write an Abbreviation Decl manually with a
// valid attribute but invalid form.
encodeULEB128(FirstCode - 5, OS);
encodeULEB128(DW_TAG_compile_unit, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(0, OS); // Invalid form after a valid attribute.
encodeULEB128(0, OS);
encodeULEB128(0, OS);
encodeULEB128(0, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
EXPECT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage(
"malformed abbreviation declaration attribute. Either the "
"attribute or the form is zero while the other is not"));
}
TEST(DWARFDebugAbbrevTest, DWARFAbbrevDeclSetMissingTerminator) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
uint32_t FirstCode = 120;
// First, we're going to manually add good data.
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
// Afterwards, we're going to write an Abbreviation Decl manually without a
// termintating sequence.
encodeULEB128(FirstCode + 7, OS);
encodeULEB128(DW_TAG_compile_unit, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(DW_FORM_strp, OS);
uint64_t Offset = 0;
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFAbbreviationDeclarationSet AbbrevSet;
EXPECT_THAT_ERROR(
AbbrevSet.extract(Data, &Offset),
FailedWithMessage(
"abbreviation declaration attribute list was not terminated with a "
"null entry"));
}
TEST(DWARFDebugAbbrevTest, DWARFDebugAbbrevParseError) {
SmallString<64> RawData;
raw_svector_ostream OS(RawData);
const uint32_t FirstCode = 70;
// First, we're going to manually add good data.
writeValidAbbreviationDeclarations(OS, FirstCode, InOrder);
// Afterwards, we're going to write an Abbreviation Decl manually without a
// termintating sequence.
encodeULEB128(FirstCode - 1, OS);
encodeULEB128(DW_TAG_compile_unit, OS);
OS << static_cast<uint8_t>(DW_CHILDREN_no);
encodeULEB128(DW_AT_name, OS);
encodeULEB128(DW_FORM_strp, OS);
// The specific error should percolate up to the DWARFDebugAbbrev::parse().
DataExtractor Data(RawData, sys::IsLittleEndianHost, sizeof(uint64_t));
DWARFDebugAbbrev DebugAbbrev(Data);
EXPECT_THAT_ERROR(
DebugAbbrev.parse(),
FailedWithMessage(
"abbreviation declaration attribute list was not terminated with a "
"null entry"));
}
|