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(GetWithError({}), 45 FailedWithMessage("unexpected end of data")); 46 EXPECT_EQ(GetWithoutError({}), ErrorResult); 47 48 // Not long enough for the U32 field. 49 EXPECT_THAT_EXPECTED(GetWithError({0x00, 0x01, 0x02}), 50 FailedWithMessage("unexpected end of data")); 51 EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02}), ErrorResult); 52 53 EXPECT_THAT_EXPECTED( 54 GetWithError({0x00, 0x01, 0x02, 0x03}), 55 HasValue(std::make_tuple(0x00010203, dwarf::DWARF32, 4))); 56 EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02, 0x03}), 57 std::make_tuple(0x00010203, dwarf::DWARF32, 4)); 58 59 // Zeroes are not an error, but without the Error object it is hard to tell 60 // them apart from a failed read. 61 EXPECT_THAT_EXPECTED( 62 GetWithError({0x00, 0x00, 0x00, 0x00}), 63 HasValue(std::make_tuple(0x00000000, dwarf::DWARF32, 4))); 64 EXPECT_EQ(GetWithoutError({0x00, 0x00, 0x00, 0x00}), 65 std::make_tuple(0x00000000, dwarf::DWARF32, 4)); 66 67 // Smallest invalid value. 68 EXPECT_THAT_EXPECTED( 69 GetWithError({0xff, 0xff, 0xff, 0xf0}), 70 FailedWithMessage( 71 "unsupported reserved unit length of value 0xfffffff0")); 72 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xf0}), ErrorResult); 73 74 // DWARF64 marker without the subsequent length field. 75 EXPECT_THAT_EXPECTED(GetWithError({0xff, 0xff, 0xff, 0xff}), 76 FailedWithMessage("unexpected end of data")); 77 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff}), ErrorResult); 78 79 // Not enough data for the U64 length. 80 EXPECT_THAT_EXPECTED( 81 GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}), 82 FailedWithMessage("unexpected end of data")); 83 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}), 84 ErrorResult); 85 86 EXPECT_THAT_EXPECTED( 87 GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 88 0x06, 0x07}), 89 HasValue(std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12))); 90 EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 91 0x04, 0x05, 0x06, 0x07}), 92 std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12)); 93 } 94 95 } // namespace 96