llvm.org GIT mirror llvm / 63d6d75
Fix a bug in MappedBlockStream. It was using the number of blocks of the entire PDB file as the number of blocks of each stream that was created. This was only an issue in the readLongestContiguousChunk function, which was never called prior. This bug surfaced when I updated an algorithm to use this function and the algorithm broke. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303916 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
3 changed file(s) with 47 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
4242 friend class WritableMappedBlockStream;
4343 public:
4444 static std::unique_ptr
45 createStream(uint32_t BlockSize, uint32_t NumBlocks,
46 const MSFStreamLayout &Layout, BinaryStreamRef MsfData);
45 createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
46 BinaryStreamRef MsfData);
4747
4848 static std::unique_ptr
4949 createIndexedStream(const MSFLayout &Layout, BinaryStreamRef MsfData,
7373 void invalidateCache();
7474
7575 uint32_t getBlockSize() const { return BlockSize; }
76 uint32_t getNumBlocks() const { return NumBlocks; }
76 uint32_t getNumBlocks() const { return StreamLayout.Blocks.size(); }
7777 uint32_t getStreamLength() const { return StreamLayout.Length; }
7878
7979 protected:
80 MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
81 const MSFStreamLayout &StreamLayout,
80 MappedBlockStream(uint32_t BlockSize, const MSFStreamLayout &StreamLayout,
8281 BinaryStreamRef MsfData);
8382
8483 private:
9089 ArrayRef &Buffer);
9190
9291 const uint32_t BlockSize;
93 const uint32_t NumBlocks;
9492 const MSFStreamLayout StreamLayout;
9593 BinaryStreamRef MsfData;
9694
102100 class WritableMappedBlockStream : public WritableBinaryStream {
103101 public:
104102 static std::unique_ptr
105 createStream(uint32_t BlockSize, uint32_t NumBlocks,
106 const MSFStreamLayout &Layout, WritableBinaryStreamRef MsfData);
103 createStream(uint32_t BlockSize, const MSFStreamLayout &Layout,
104 WritableBinaryStreamRef MsfData);
107105
108106 static std::unique_ptr
109107 createIndexedStream(const MSFLayout &Layout, WritableBinaryStreamRef MsfData,
138136 uint32_t getStreamLength() const { return ReadInterface.getStreamLength(); }
139137
140138 protected:
141 WritableMappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
139 WritableMappedBlockStream(uint32_t BlockSize,
142140 const MSFStreamLayout &StreamLayout,
143141 WritableBinaryStreamRef MsfData);
144142
4444 std::min(I1.second, I2.second));
4545 }
4646
47 MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
47 MappedBlockStream::MappedBlockStream(uint32_t BlockSize,
4848 const MSFStreamLayout &Layout,
4949 BinaryStreamRef MsfData)
50 : BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout),
51 MsfData(MsfData) {}
50 : BlockSize(BlockSize), StreamLayout(Layout), MsfData(MsfData) {}
5251
5352 std::unique_ptr
54 MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
53 MappedBlockStream::createStream(uint32_t BlockSize,
5554 const MSFStreamLayout &Layout,
5655 BinaryStreamRef MsfData) {
5756 return llvm::make_unique>(
58 BlockSize, NumBlocks, Layout, MsfData);
57 BlockSize, Layout, MsfData);
5958 }
6059
6160 std::unique_ptr MappedBlockStream::createIndexedStream(
6564 SL.Blocks = Layout.StreamMap[StreamIndex];
6665 SL.Length = Layout.StreamSizes[StreamIndex];
6766 return llvm::make_unique>(
68 Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
67 Layout.SB->BlockSize, SL, MsfData);
6968 }
7069
7170 std::unique_ptr
7473 MSFStreamLayout SL;
7574 SL.Blocks = Layout.DirectoryBlocks;
7675 SL.Length = Layout.SB->NumDirectoryBytes;
77 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
76 return createStream(Layout.SB->BlockSize, SL, MsfData);
7877 }
7978
8079 std::unique_ptr
8281 BinaryStreamRef MsfData) {
8382 MSFStreamLayout SL;
8483 initializeFpmStreamLayout(Layout, SL);
85 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
84 return createStream(Layout.SB->BlockSize, SL, MsfData);
8685 }
8786
8887 Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
172171 uint32_t First = Offset / BlockSize;
173172 uint32_t Last = First;
174173
175 while (Last < NumBlocks - 1) {
174 while (Last < getNumBlocks() - 1) {
176175 if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
177176 break;
178177 ++Last;
312311 }
313312
314313 WritableMappedBlockStream::WritableMappedBlockStream(
315 uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
314 uint32_t BlockSize, const MSFStreamLayout &Layout,
316315 WritableBinaryStreamRef MsfData)
317 : ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
318 WriteInterface(MsfData) {}
316 : ReadInterface(BlockSize, Layout, MsfData), WriteInterface(MsfData) {}
319317
320318 std::unique_ptr
321 WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
319 WritableMappedBlockStream::createStream(uint32_t BlockSize,
322320 const MSFStreamLayout &Layout,
323321 WritableBinaryStreamRef MsfData) {
324322 return llvm::make_unique>(
325 BlockSize, NumBlocks, Layout, MsfData);
323 BlockSize, Layout, MsfData);
326324 }
327325
328326 std::unique_ptr
333331 MSFStreamLayout SL;
334332 SL.Blocks = Layout.StreamMap[StreamIndex];
335333 SL.Length = Layout.StreamSizes[StreamIndex];
336 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
334 return createStream(Layout.SB->BlockSize, SL, MsfData);
337335 }
338336
339337 std::unique_ptr
342340 MSFStreamLayout SL;
343341 SL.Blocks = Layout.DirectoryBlocks;
344342 SL.Length = Layout.SB->NumDirectoryBytes;
345 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
343 return createStream(Layout.SB->BlockSize, SL, MsfData);
346344 }
347345
348346 std::unique_ptr
350348 WritableBinaryStreamRef MsfData) {
351349 MSFStreamLayout SL;
352350 initializeFpmStreamLayout(Layout, SL);
353 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
351 return createStream(Layout.SB->BlockSize, SL, MsfData);
354352 }
355353
356354 Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
7474 MutableArrayRef Data;
7575 };
7676
77 TEST(MappedBlockStreamTest, NumBlocks) {
78 DiscontiguousStream F(BlocksAry, DataAry);
79 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
80 EXPECT_EQ(F.block_size(), S->getBlockSize());
81 EXPECT_EQ(F.layout().Blocks.size(), S->getNumBlocks());
82
83 }
84
7785 // Tests that a read which is entirely contained within a single block works
7886 // and does not allocate.
7987 TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
8088 DiscontiguousStream F(BlocksAry, DataAry);
81 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
82 F.layout(), F);
89 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
8390
8491 BinaryStreamReader R(*S);
8592 BinaryStreamRef SR;
94101 // does not fail due to the length of the output buffer.
95102 TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {
96103 DiscontiguousStream F(BlocksAry, DataAry);
97 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
98 F.layout(), F);
104 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
99105
100106 BinaryStreamReader R(*S);
101107 StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
109115 // not allocate memory.
110116 TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {
111117 DiscontiguousStream F(BlocksAry, DataAry);
112 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
118 auto S = MappedBlockStream::createStream(F.block_size(),
113119 F.layout(), F);
114120 BinaryStreamReader R(*S);
115121 StringRef Str;
128134 // requested.
129135 TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {
130136 DiscontiguousStream F(BlocksAry, DataAry);
131 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
132 F.layout(), F);
137 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
133138 BinaryStreamReader R(*S);
134139 StringRef Str;
135140 EXPECT_NO_ERROR(R.readFixedString(Str, 10));
141146 // fails and allocates no memory.
142147 TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {
143148 DiscontiguousStream F(BlocksAry, DataAry);
144 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
145 F.layout(), F);
149 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
146150 BinaryStreamReader R(*S);
147151 StringRef Str;
148152
155159 // fails and allocates no memory.
156160 TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {
157161 DiscontiguousStream F(BlocksAry, DataAry);
158 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
159 F.layout(), F);
162 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
160163 BinaryStreamReader R(*S);
161164 StringRef Str;
162165
169172 // boundary fails and allocates no memory.
170173 TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
171174 DiscontiguousStream F(BlocksAry, DataAry);
172 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
173 F.layout(), F);
175 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
174176 BinaryStreamReader R(*S);
175177 StringRef Str;
176178
182184 // beyond the end of a StreamRef fails.
183185 TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
184186 DiscontiguousStream F(BlocksAry, DataAry);
185 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
186 F.layout(), F);
187 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
187188 BinaryStreamReader R(*S);
188189 StringRef Str;
189190 EXPECT_NO_ERROR(R.readFixedString(Str, 1));
196197 // previous allocation.
197198 TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {
198199 DiscontiguousStream F(BlocksAry, DataAry);
199 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
200 F.layout(), F);
200 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
201201 BinaryStreamReader R(*S);
202202 StringRef Str1;
203203 StringRef Str2;
217217 // still works correctly and allocates again from the shared pool.
218218 TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {
219219 DiscontiguousStream F(BlocksAry, DataAry);
220 auto S = MappedBlockStream::createStream(F.block_size(), F.block_count(),
221 F.layout(), F);
220 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F);
222221 BinaryStreamReader R(*S);
223222 StringRef Str1;
224223 StringRef Str2;
242241
243242 DiscontiguousStream F(BlocksAry, Data);
244243 auto S = WritableMappedBlockStream::createStream(
245 F.block_size(), F.block_count(), F.layout(), F);
244 F.block_size(), F.layout(), F);
246245 ArrayRef Buffer;
247246
248247 EXPECT_ERROR(S->writeBytes(0, ArrayRef(LargeBuffer)));
255254 static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
256255 DiscontiguousStream F(BlocksAry, Data);
257256 auto S = WritableMappedBlockStream::createStream(
258 F.block_size(), F.block_count(), F.layout(), F);
257 F.block_size(), F.layout(), F);
259258 ArrayRef Buffer;
260259
261260 EXPECT_NO_ERROR(S->readBytes(0, 1, Buffer));
288287
289288 DiscontiguousStream F(BlocksAry, Data);
290289 auto S = WritableMappedBlockStream::createStream(
291 F.block_size(), F.block_count(), F.layout(), F);
290 F.block_size(), F.layout(), F);
292291 ArrayRef Buffer;
293292
294293 EXPECT_NO_ERROR(S->writeBytes(0, TestData));
307306
308307 DiscontiguousStream F(Blocks, Data);
309308 auto S = WritableMappedBlockStream::createStream(
310 F.block_size(), F.block_count(), F.layout(), F);
309 F.block_size(), F.layout(), F);
311310
312311 enum class MyEnum : uint32_t { Val1 = 2908234, Val2 = 120891234 };
313312 using support::ulittle32_t;
399398
400399 DiscontiguousStream F(DestBlocks, DestData);
401400 auto DestStream = WritableMappedBlockStream::createStream(
402 F.block_size(), F.block_count(), F.layout(), F);
401 F.block_size(), F.layout(), F);
403402
404403 // First write "Test Str" into the source stream.
405404 MutableBinaryByteStream SourceStream(SrcData, little);
434433 DiscontiguousStream SrcF(SrcBlocks, SrcData);
435434
436435 auto Dest = WritableMappedBlockStream::createStream(
437 DestF.block_size(), DestF.block_count(), DestF.layout(), DestF);
436 DestF.block_size(), DestF.layout(), DestF);
438437 auto Src = WritableMappedBlockStream::createStream(
439 SrcF.block_size(), SrcF.block_count(), SrcF.layout(), SrcF);
438 SrcF.block_size(), SrcF.layout(), SrcF);
440439
441440 // First write "Test Str" into the source stream.
442441 BinaryStreamWriter SourceWriter(*Src);