diff options
author | Greg Clayton <clayborg@gmail.com> | 2019-09-17 17:46:13 +0000 |
---|---|---|
committer | Greg Clayton <clayborg@gmail.com> | 2019-09-17 17:46:13 +0000 |
commit | c6b156cbb83a714d662b83a17692f6426afc3fc8 (patch) | |
tree | a107eb8212a3131edfd14a00e1aaa84cebe7091d /llvm/unittests/DebugInfo | |
parent | 5801e6257627378e414d1a0d6be68aa71f67ef28 (diff) | |
download | bcm5719-llvm-c6b156cbb83a714d662b83a17692f6426afc3fc8.tar.gz bcm5719-llvm-c6b156cbb83a714d662b83a17692f6426afc3fc8.zip |
GSYM: Add the llvm::gsym::Header header class with tests
This patch adds the llvm::gsym::Header class which appears at the start of a stand alone GSYM file, or in the first bytes of the GSYM data in a GSYM section within a file. Added encode and decode methods with full error handling and full tests.
Differential Revision: https://reviews.llvm.org/D67666
llvm-svn: 372149
Diffstat (limited to 'llvm/unittests/DebugInfo')
-rw-r--r-- | llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp b/llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp index 26fcaa4dcd3..ba543e02735 100644 --- a/llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp +++ b/llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallString.h" +#include "llvm/DebugInfo/GSYM/Header.h" #include "llvm/DebugInfo/GSYM/FileEntry.h" #include "llvm/DebugInfo/GSYM/FileWriter.h" #include "llvm/DebugInfo/GSYM/FunctionInfo.h" @@ -944,3 +945,104 @@ TEST(GSYMTest, TestLineTableEncodeErrors) { LT.encode(FW, BaseAddr)); LT.clear(); } + +static void TestHeaderEncodeError(const Header &H, + std::string ExpectedErrorMsg) { + const support::endianness ByteOrder = llvm::support::little; + SmallString<512> Str; + raw_svector_ostream OutStrm(Str); + FileWriter FW(OutStrm, ByteOrder); + llvm::Error Err = H.encode(FW); + checkError(ExpectedErrorMsg, std::move(Err)); +} + +static void TestHeaderDecodeError(std::string Bytes, + std::string ExpectedErrorMsg) { + const support::endianness ByteOrder = llvm::support::little; + uint8_t AddressSize = 4; + DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); + llvm::Expected<Header> Decoded = Header::decode(Data); + // Make sure decoding fails. + ASSERT_FALSE((bool)Decoded); + // Make sure decoded object is the same as the one we encoded. + checkError(ExpectedErrorMsg, Decoded.takeError()); +} + +// Populate a GSYM header with valid values. +static void InitHeader(Header &H) { + H.Magic = GSYM_MAGIC; + H.Version = GSYM_VERSION; + H.AddrOffSize = 4; + H.UUIDSize = 16; + H.BaseAddress = 0x1000; + H.NumAddresses = 1; + H.StrtabOffset= 0x2000; + H.StrtabSize = 0x1000; + for (size_t i=0; i<GSYM_MAX_UUID_SIZE; ++i) { + if (i < H.UUIDSize) + H.UUID[i] = i; + else + H.UUID[i] = 0; + } +} + +TEST(GSYMTest, TestHeaderEncodeErrors) { + Header H; + InitHeader(H); + H.Magic = 12; + TestHeaderEncodeError(H, "invalid GSYM magic 0x0000000c"); + InitHeader(H); + H.Version = 12; + TestHeaderEncodeError(H, "unsupported GSYM version 12"); + InitHeader(H); + H.AddrOffSize = 12; + TestHeaderEncodeError(H, "invalid address offset size 12"); + InitHeader(H); + H.UUIDSize = 128; + TestHeaderEncodeError(H, "invalid UUID size 128"); +} + +TEST(GSYMTest, TestHeaderDecodeErrors) { + const llvm::support::endianness ByteOrder = llvm::support::little; + SmallString<512> Str; + raw_svector_ostream OutStrm(Str); + FileWriter FW(OutStrm, ByteOrder); + Header H; + InitHeader(H); + llvm::Error Err = H.encode(FW); + ASSERT_FALSE(Err); + FW.fixup32(12, offsetof(Header, Magic)); + TestHeaderDecodeError(OutStrm.str(), "invalid GSYM magic 0x0000000c"); + FW.fixup32(GSYM_MAGIC, offsetof(Header, Magic)); + FW.fixup32(12, offsetof(Header, Version)); + TestHeaderDecodeError(OutStrm.str(), "unsupported GSYM version 12"); + FW.fixup32(GSYM_VERSION, offsetof(Header, Version)); + FW.fixup32(12, offsetof(Header, AddrOffSize)); + TestHeaderDecodeError(OutStrm.str(), "invalid address offset size 12"); + FW.fixup32(4, offsetof(Header, AddrOffSize)); + FW.fixup32(128, offsetof(Header, UUIDSize)); + TestHeaderDecodeError(OutStrm.str(), "invalid UUID size 128"); +} + +static void TestHeaderEncodeDecode(const Header &H, + support::endianness ByteOrder) { + uint8_t AddressSize = 4; + SmallString<512> Str; + raw_svector_ostream OutStrm(Str); + FileWriter FW(OutStrm, ByteOrder); + llvm::Error Err = H.encode(FW); + ASSERT_FALSE(Err); + std::string Bytes(OutStrm.str()); + DataExtractor Data(Bytes, ByteOrder == llvm::support::little, AddressSize); + llvm::Expected<Header> Decoded = Header::decode(Data); + // Make sure decoding succeeded. + ASSERT_TRUE((bool)Decoded); + EXPECT_EQ(H, Decoded.get()); + +} +TEST(GSYMTest, TestHeaderEncodeDecode) { + Header H; + InitHeader(H); + TestHeaderEncodeDecode(H, llvm::support::little); + TestHeaderEncodeDecode(H, llvm::support::big); +} |