1 //===- DWARFDataExtractorTest.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/DWARFDataExtractor.h" 10 #include "llvm/Testing/Support/Error.h" 11 #include "gtest/gtest.h" 12 13 using namespace llvm; 14 15 namespace { 16 17 TEST(DWARFDataExtractorTest, getInitialLength) { 18 auto GetWithError = [](ArrayRef<uint8_t> Bytes) 19 -> Expected<std::tuple<uint64_t, dwarf::DwarfFormat, uint64_t>> { 20 DWARFDataExtractor Data(Bytes, /*IsLittleEndian=*/false, /*AddressSize=*/8); 21 DWARFDataExtractor::Cursor C(0); 22 uint64_t Length; 23 dwarf::DwarfFormat Format; 24 std::tie(Length, Format) = Data.getInitialLength(C); 25 if (C) 26 return std::make_tuple(Length, Format, C.tell()); 27 28 EXPECT_EQ(Length, 0u); 29 EXPECT_EQ(Format, dwarf::DWARF32); 30 EXPECT_EQ(C.tell(), 0u); 31 return C.takeError(); 32 }; 33 auto GetWithoutError = [](ArrayRef<uint8_t> Bytes) { 34 DWARFDataExtractor Data(Bytes, /*IsLittleEndian=*/false, /*AddressSize=*/8); 35 uint64_t Offset = 0; 36 uint64_t Length; 37 dwarf::DwarfFormat Format; 38 std::tie(Length, Format) = Data.getInitialLength(&Offset); 39 return std::make_tuple(Length, Format, Offset); 40 }; 41 auto ErrorResult = std::make_tuple(0, dwarf::DWARF32, 0); 42 43 // Empty data. 44 EXPECT_THAT_EXPECTED( 45 GetWithError({}), 46 FailedWithMessage("unexpected end of data at offset 0x0")); 47 EXPECT_EQ(GetWithoutError({}), ErrorResult); 48 49 // Not long enough for the U32 field. 50 EXPECT_THAT_EXPECTED( 51 GetWithError({0x00, 0x01, 0x02}), 52 FailedWithMessage("unexpected end of data at offset 0x0")); 53 EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02}), ErrorResult); 54 55 EXPECT_THAT_EXPECTED( 56 GetWithError({0x00, 0x01, 0x02, 0x03}), 57 HasValue(std::make_tuple(0x00010203, dwarf::DWARF32, 4))); 58 EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02, 0x03}), 59 std::make_tuple(0x00010203, dwarf::DWARF32, 4)); 60 61 // Zeroes are not an error, but without the Error object it is hard to tell 62 // them apart from a failed read. 63 EXPECT_THAT_EXPECTED( 64 GetWithError({0x00, 0x00, 0x00, 0x00}), 65 HasValue(std::make_tuple(0x00000000, dwarf::DWARF32, 4))); 66 EXPECT_EQ(GetWithoutError({0x00, 0x00, 0x00, 0x00}), 67 std::make_tuple(0x00000000, dwarf::DWARF32, 4)); 68 69 // Smallest invalid value. 70 EXPECT_THAT_EXPECTED( 71 GetWithError({0xff, 0xff, 0xff, 0xf0}), 72 FailedWithMessage( 73 "unsupported reserved unit length of value 0xfffffff0")); 74 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xf0}), ErrorResult); 75 76 // DWARF64 marker without the subsequent length field. 77 EXPECT_THAT_EXPECTED( 78 GetWithError({0xff, 0xff, 0xff, 0xff}), 79 FailedWithMessage("unexpected end of data at offset 0x4")); 80 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff}), ErrorResult); 81 82 // Not enough data for the U64 length. 83 EXPECT_THAT_EXPECTED( 84 GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}), 85 FailedWithMessage("unexpected end of data at offset 0x4")); 86 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}), 87 ErrorResult); 88 89 EXPECT_THAT_EXPECTED( 90 GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 91 0x06, 0x07}), 92 HasValue(std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12))); 93 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 94 0x04, 0x05, 0x06, 0x07}), 95 std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12)); 96 } 97 98 } // namespace 99