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