Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/DebugInfo/PDB/MappedBlockStreamTest.cpp --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <unordered_map> |
| 11 | |
| 12 | #include "llvm/DebugInfo/CodeView/StreamReader.h" |
| 13 | #include "llvm/DebugInfo/CodeView/StreamRef.h" |
| 14 | #include "llvm/DebugInfo/PDB/Raw/IPDBFile.h" |
| 15 | #include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::codeview; |
| 20 | using namespace llvm::pdb; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | #define EXPECT_NO_ERROR(Err) \ |
| 25 | { \ |
Zachary Turner | d665a7f | 2016-06-01 18:18:55 +0000 | [diff] [blame] | 26 | auto E = Err; \ |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 27 | EXPECT_FALSE(static_cast<bool>(E)); \ |
| 28 | if (E) \ |
| 29 | consumeError(std::move(E)); \ |
| 30 | } |
| 31 | |
| 32 | #define EXPECT_ERROR(Err) \ |
| 33 | { \ |
Zachary Turner | d665a7f | 2016-06-01 18:18:55 +0000 | [diff] [blame] | 34 | auto E = Err; \ |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 35 | EXPECT_TRUE(static_cast<bool>(E)); \ |
| 36 | if (E) \ |
| 37 | consumeError(std::move(E)); \ |
| 38 | } |
| 39 | |
NAKAMURA Takumi | 47d1e37 | 2016-06-01 14:26:54 +0000 | [diff] [blame] | 40 | static const uint32_t BlocksAry[] = {0, 1, 2, 5, 4, 3, 6, 7, 8, 9}; |
| 41 | static const char DataAry[] = {'A', 'B', 'C', 'F', 'E', |
| 42 | 'D', 'G', 'H', 'I', 'J'}; |
| 43 | |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 44 | class DiscontiguousFile : public IPDBFile { |
| 45 | public: |
| 46 | DiscontiguousFile() |
NAKAMURA Takumi | f021d26 | 2016-06-01 22:59:06 +0000 | [diff] [blame] | 47 | : Blocks(std::begin(BlocksAry), std::end(BlocksAry)), |
NAKAMURA Takumi | 7f48be0 | 2016-06-01 23:03:46 +0000 | [diff] [blame] | 48 | Data(std::begin(DataAry), std::end(DataAry)) {} |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 49 | |
| 50 | virtual uint32_t getBlockSize() const override { return 1; } |
| 51 | virtual uint32_t getBlockCount() const override { return 10; } |
| 52 | virtual uint32_t getNumStreams() const override { return 1; } |
| 53 | virtual uint32_t getStreamByteSize(uint32_t StreamIndex) const override { |
| 54 | return getBlockCount() * getBlockSize(); |
| 55 | } |
| 56 | virtual ArrayRef<uint32_t> |
| 57 | getStreamBlockList(uint32_t StreamIndex) const override { |
| 58 | if (StreamIndex != 0) |
| 59 | return ArrayRef<uint32_t>(); |
| 60 | return Blocks; |
| 61 | } |
| 62 | virtual StringRef getBlockData(uint32_t BlockIndex, |
| 63 | uint32_t NumBytes) const override { |
| 64 | return StringRef(&Data[BlockIndex], NumBytes); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | std::vector<uint32_t> Blocks; |
| 69 | std::vector<char> Data; |
| 70 | }; |
| 71 | |
| 72 | // Tests that a read which is entirely contained within a single block works |
| 73 | // and does not allocate. |
David Majnemer | b6aa875 | 2016-06-01 18:13:06 +0000 | [diff] [blame] | 74 | TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) { |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 75 | DiscontiguousFile F; |
| 76 | MappedBlockStream S(0, F); |
| 77 | StreamReader R(S); |
David Majnemer | b6aa875 | 2016-06-01 18:13:06 +0000 | [diff] [blame] | 78 | StreamRef SR; |
| 79 | EXPECT_NO_ERROR(R.readStreamRef(SR, 0U)); |
| 80 | ArrayRef<uint8_t> Buffer; |
| 81 | EXPECT_ERROR(SR.readBytes(0U, 1U, Buffer)); |
David Majnemer | 8c79db1 | 2016-06-02 06:21:44 +0000 | [diff] [blame^] | 82 | EXPECT_NO_ERROR(R.readStreamRef(SR, 1U)); |
| 83 | EXPECT_ERROR(SR.readBytes(1U, 1U, Buffer)); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Tests that a read which outputs into a full destination buffer works and |
| 87 | // does not fail due to the length of the output buffer. |
| 88 | TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) { |
| 89 | DiscontiguousFile F; |
| 90 | MappedBlockStream S(0, F); |
| 91 | StreamReader R(S); |
| 92 | StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; |
| 93 | EXPECT_NO_ERROR(R.readFixedString(Str, 1)); |
| 94 | EXPECT_EQ(Str, StringRef("A")); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 95 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Tests that a read which crosses a block boundary, but where the subsequent |
| 99 | // blocks are still contiguous in memory to the previous block works and does |
| 100 | // not allocate memory. |
| 101 | TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) { |
| 102 | DiscontiguousFile F; |
| 103 | MappedBlockStream S(0, F); |
| 104 | StreamReader R(S); |
| 105 | StringRef Str; |
| 106 | EXPECT_NO_ERROR(R.readFixedString(Str, 2)); |
| 107 | EXPECT_EQ(Str, StringRef("AB")); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 108 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 109 | |
| 110 | R.setOffset(6); |
| 111 | EXPECT_NO_ERROR(R.readFixedString(Str, 4)); |
| 112 | EXPECT_EQ(Str, StringRef("GHIJ")); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 113 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Tests that a read which crosses a block boundary and cannot be referenced |
| 117 | // contiguously works and allocates only the precise amount of bytes |
| 118 | // requested. |
| 119 | TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) { |
| 120 | DiscontiguousFile F; |
| 121 | MappedBlockStream S(0, F); |
| 122 | StreamReader R(S); |
| 123 | StringRef Str; |
| 124 | EXPECT_NO_ERROR(R.readFixedString(Str, 10)); |
| 125 | EXPECT_EQ(Str, StringRef("ABCDEFGHIJ")); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 126 | EXPECT_EQ(10U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Test that an out of bounds read which doesn't cross a block boundary |
| 130 | // fails and allocates no memory. |
| 131 | TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) { |
| 132 | DiscontiguousFile F; |
| 133 | MappedBlockStream S(0, F); |
| 134 | StreamReader R(S); |
| 135 | StringRef Str; |
| 136 | |
| 137 | R.setOffset(10); |
| 138 | EXPECT_ERROR(R.readFixedString(Str, 1)); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 139 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Test that an out of bounds read which crosses a contiguous block boundary |
| 143 | // fails and allocates no memory. |
| 144 | TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) { |
| 145 | DiscontiguousFile F; |
| 146 | MappedBlockStream S(0, F); |
| 147 | StreamReader R(S); |
| 148 | StringRef Str; |
| 149 | |
| 150 | R.setOffset(6); |
| 151 | EXPECT_ERROR(R.readFixedString(Str, 5)); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 152 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Test that an out of bounds read which crosses a discontiguous block |
| 156 | // boundary fails and allocates no memory. |
| 157 | TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) { |
| 158 | DiscontiguousFile F; |
| 159 | MappedBlockStream S(0, F); |
| 160 | StreamReader R(S); |
| 161 | StringRef Str; |
| 162 | |
| 163 | EXPECT_ERROR(R.readFixedString(Str, 11)); |
David Majnemer | c011305 | 2016-06-01 18:13:02 +0000 | [diff] [blame] | 164 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 165 | } |
| 166 | |
David Majnemer | b6aa875 | 2016-06-01 18:13:06 +0000 | [diff] [blame] | 167 | // Tests that a read which is entirely contained within a single block but |
| 168 | // beyond the end of a StreamRef fails. |
| 169 | TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) { |
| 170 | DiscontiguousFile F; |
| 171 | MappedBlockStream S(0, F); |
| 172 | StreamReader R(S); |
| 173 | StringRef Str; |
| 174 | EXPECT_NO_ERROR(R.readFixedString(Str, 1)); |
| 175 | EXPECT_EQ(Str, StringRef("A")); |
| 176 | EXPECT_EQ(0U, S.getNumBytesCopied()); |
| 177 | } |
| 178 | |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame] | 179 | } // end anonymous namespace |