llvm.org GIT mirror llvm / 11625a0
[MSF] Move MSF unit tests to their own unittest target. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@309895 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 2 years ago
7 changed file(s) with 874 addition(s) and 862 deletion(s). Raw diff Collapse all Expand all
0 add_subdirectory(CodeView)
11 add_subdirectory(DWARF)
2 add_subdirectory(MSF)
23 add_subdirectory(PDB)
0 set(LLVM_LINK_COMPONENTS
1 DebugInfoMSF
2 )
3
4 set(DebugInfoMSFSources
5 MappedBlockStreamTest.cpp
6 MSFBuilderTest.cpp
7 )
8
9 add_llvm_unittest(DebugInfoMSFTests
10 ${DebugInfoMSFSources}
11 )
12
13 target_link_libraries(DebugInfoMSFTests LLVMTestingSupport)
0 //===- MSFBuilderTest.cpp Tests manipulation of MSF stream metadata ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
10 #include "llvm/DebugInfo/MSF/MSFCommon.h"
11 #include "llvm/Testing/Support/Error.h"
12
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace llvm::msf;
17
18 namespace {
19 class MSFBuilderTest : public testing::Test {
20 protected:
21 void initializeSimpleSuperBlock(msf::SuperBlock &SB) {
22 initializeSuperBlock(SB);
23 SB.NumBlocks = 1000;
24 SB.NumDirectoryBytes = 8192;
25 }
26
27 void initializeSuperBlock(msf::SuperBlock &SB) {
28 ::memset(&SB, 0, sizeof(SB));
29
30 ::memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic));
31 SB.FreeBlockMapBlock = 1;
32 SB.BlockMapAddr = 1;
33 SB.BlockSize = 4096;
34 SB.NumDirectoryBytes = 0;
35 SB.NumBlocks = 2; // one for the Super Block, one for the directory
36 }
37
38 BumpPtrAllocator Allocator;
39 };
40 } // namespace
41
42 TEST_F(MSFBuilderTest, ValidateSuperBlockAccept) {
43 // Test that a known good super block passes validation.
44 SuperBlock SB;
45 initializeSuperBlock(SB);
46
47 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
48 }
49
50 TEST_F(MSFBuilderTest, ValidateSuperBlockReject) {
51 // Test that various known problems cause a super block to be rejected.
52 SuperBlock SB;
53 initializeSimpleSuperBlock(SB);
54
55 // Mismatched magic
56 SB.MagicBytes[0] = 8;
57 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
58 initializeSimpleSuperBlock(SB);
59
60 // Block 0 is reserved for super block, can't be occupied by the block map
61 SB.BlockMapAddr = 0;
62 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
63 initializeSimpleSuperBlock(SB);
64
65 // Block sizes have to be powers of 2.
66 SB.BlockSize = 3120;
67 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
68 initializeSimpleSuperBlock(SB);
69
70 // The directory itself has a maximum size.
71 SB.NumDirectoryBytes = SB.BlockSize * SB.BlockSize / 4;
72 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
73 SB.NumDirectoryBytes = SB.NumDirectoryBytes + 4;
74 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
75 }
76
77 TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) {
78 // Test that when assigning a stream to a known list of blocks, the blocks
79 // are correctly marked as used after adding, but no other incorrect blocks
80 // are accidentally marked as used.
81
82 std::vector Blocks = {4, 5, 6, 7, 8, 9, 10, 11, 12};
83 // Allocate some extra blocks at the end so we can verify that they're free
84 // after the initialization.
85 uint32_t NumBlocks = msf::getMinimumBlockCount() + Blocks.size() + 10;
86 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096, NumBlocks);
87 ASSERT_THAT_EXPECTED(ExpectedMsf, Succeeded());
88 auto &Msf = *ExpectedMsf;
89
90 EXPECT_THAT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks),
91 Succeeded());
92
93 for (auto B : Blocks) {
94 EXPECT_FALSE(Msf.isBlockFree(B));
95 }
96
97 uint32_t FreeBlockStart = Blocks.back() + 1;
98 for (uint32_t I = FreeBlockStart; I < NumBlocks; ++I) {
99 EXPECT_TRUE(Msf.isBlockFree(I));
100 }
101 }
102
103 TEST_F(MSFBuilderTest, TestAddStreamNoDirectoryBlockIncrease) {
104 // Test that adding a new stream correctly updates the directory. This only
105 // tests the case where the directory *DOES NOT* grow large enough that it
106 // crosses a Block boundary.
107 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
108 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
109 auto &Msf = *ExpectedMsf;
110
111 auto ExpectedL1 = Msf.build();
112 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
113 MSFLayout &L1 = *ExpectedL1;
114
115 auto OldDirBlocks = L1.DirectoryBlocks;
116 EXPECT_EQ(1U, OldDirBlocks.size());
117
118 auto ExpectedMsf2 = MSFBuilder::create(Allocator, 4096);
119 EXPECT_THAT_EXPECTED(ExpectedMsf2, Succeeded());
120 auto &Msf2 = *ExpectedMsf2;
121
122 EXPECT_THAT_EXPECTED(Msf2.addStream(4000), Succeeded());
123 EXPECT_EQ(1U, Msf2.getNumStreams());
124 EXPECT_EQ(4000U, Msf2.getStreamSize(0));
125 auto Blocks = Msf2.getStreamBlocks(0);
126 EXPECT_EQ(1U, Blocks.size());
127
128 auto ExpectedL2 = Msf2.build();
129 EXPECT_THAT_EXPECTED(ExpectedL2, Succeeded());
130 MSFLayout &L2 = *ExpectedL2;
131 auto NewDirBlocks = L2.DirectoryBlocks;
132 EXPECT_EQ(1U, NewDirBlocks.size());
133 }
134
135 TEST_F(MSFBuilderTest, TestAddStreamWithDirectoryBlockIncrease) {
136 // Test that adding a new stream correctly updates the directory. This only
137 // tests the case where the directory *DOES* grow large enough that it
138 // crosses a Block boundary. This is because the newly added stream occupies
139 // so many Blocks that need to be indexed in the directory that the directory
140 // crosses a Block boundary.
141 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
142 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
143 auto &Msf = *ExpectedMsf;
144
145 EXPECT_THAT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t)),
146 Succeeded());
147
148 auto ExpectedL1 = Msf.build();
149 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
150 MSFLayout &L1 = *ExpectedL1;
151 auto DirBlocks = L1.DirectoryBlocks;
152 EXPECT_EQ(2U, DirBlocks.size());
153 }
154
155 TEST_F(MSFBuilderTest, TestGrowStreamNoBlockIncrease) {
156 // Test growing an existing stream by a value that does not affect the number
157 // of blocks it occupies.
158 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
159 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
160 auto &Msf = *ExpectedMsf;
161
162 EXPECT_THAT_EXPECTED(Msf.addStream(1024), Succeeded());
163 EXPECT_EQ(1024U, Msf.getStreamSize(0));
164 auto OldStreamBlocks = Msf.getStreamBlocks(0);
165 EXPECT_EQ(1U, OldStreamBlocks.size());
166
167 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
168 EXPECT_EQ(2048U, Msf.getStreamSize(0));
169 auto NewStreamBlocks = Msf.getStreamBlocks(0);
170 EXPECT_EQ(1U, NewStreamBlocks.size());
171
172 EXPECT_EQ(OldStreamBlocks, NewStreamBlocks);
173 }
174
175 TEST_F(MSFBuilderTest, TestGrowStreamWithBlockIncrease) {
176 // Test that growing an existing stream to a value large enough that it causes
177 // the need to allocate new Blocks to the stream correctly updates the
178 // stream's
179 // block list.
180 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
181 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
182 auto &Msf = *ExpectedMsf;
183
184 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
185 EXPECT_EQ(2048U, Msf.getStreamSize(0));
186 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
187 EXPECT_EQ(1U, OldStreamBlocks.size());
188
189 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 6144), Succeeded());
190 EXPECT_EQ(6144U, Msf.getStreamSize(0));
191 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
192 EXPECT_EQ(2U, NewStreamBlocks.size());
193
194 EXPECT_EQ(OldStreamBlocks[0], NewStreamBlocks[0]);
195 EXPECT_NE(NewStreamBlocks[0], NewStreamBlocks[1]);
196 }
197
198 TEST_F(MSFBuilderTest, TestShrinkStreamNoBlockDecrease) {
199 // Test that shrinking an existing stream by a value that does not affect the
200 // number of Blocks it occupies makes no changes to stream's block list.
201 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
202 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
203 auto &Msf = *ExpectedMsf;
204
205 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
206 EXPECT_EQ(2048U, Msf.getStreamSize(0));
207 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
208 EXPECT_EQ(1U, OldStreamBlocks.size());
209
210 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 1024), Succeeded());
211 EXPECT_EQ(1024U, Msf.getStreamSize(0));
212 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
213 EXPECT_EQ(1U, NewStreamBlocks.size());
214
215 EXPECT_EQ(OldStreamBlocks, NewStreamBlocks);
216 }
217
218 TEST_F(MSFBuilderTest, TestShrinkStreamWithBlockDecrease) {
219 // Test that shrinking an existing stream to a value large enough that it
220 // causes the need to deallocate new Blocks to the stream correctly updates
221 // the stream's block list.
222 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
223 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
224 auto &Msf = *ExpectedMsf;
225
226 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
227 EXPECT_EQ(6144U, Msf.getStreamSize(0));
228 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
229 EXPECT_EQ(2U, OldStreamBlocks.size());
230
231 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
232 EXPECT_EQ(2048U, Msf.getStreamSize(0));
233 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
234 EXPECT_EQ(1U, NewStreamBlocks.size());
235
236 EXPECT_EQ(OldStreamBlocks[0], NewStreamBlocks[0]);
237 }
238
239 TEST_F(MSFBuilderTest, TestRejectReusedStreamBlock) {
240 // Test that attempting to add a stream and assigning a block that is already
241 // in use by another stream fails.
242 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
243 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
244 auto &Msf = *ExpectedMsf;
245
246 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
247
248 std::vector Blocks = {2, 3};
249 EXPECT_THAT_EXPECTED(Msf.addStream(6144, Blocks), Failed());
250 }
251
252 TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) {
253 // Test that when adding multiple streams, the number of used and free Blocks
254 // allocated to the MSF file are as expected.
255 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
256 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
257 auto &Msf = *ExpectedMsf;
258
259 // one for the super block, one for the directory block map
260 uint32_t NumUsedBlocks = Msf.getNumUsedBlocks();
261 EXPECT_EQ(msf::getMinimumBlockCount(), NumUsedBlocks);
262 EXPECT_EQ(0U, Msf.getNumFreeBlocks());
263
264 const uint32_t StreamSizes[] = {4000, 6193, 189723};
265 for (int I = 0; I < 3; ++I) {
266 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
267 NumUsedBlocks += bytesToBlocks(StreamSizes[I], 4096);
268 EXPECT_EQ(NumUsedBlocks, Msf.getNumUsedBlocks());
269 EXPECT_EQ(0U, Msf.getNumFreeBlocks());
270 }
271 }
272
273 TEST_F(MSFBuilderTest, BuildMsfLayout) {
274 // Test that we can generate an MSFLayout structure from a valid layout
275 // specification.
276 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
277 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
278 auto &Msf = *ExpectedMsf;
279
280 const uint32_t StreamSizes[] = {4000, 6193, 189723};
281 uint32_t ExpectedNumBlocks = msf::getMinimumBlockCount();
282 for (int I = 0; I < 3; ++I) {
283 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
284 ExpectedNumBlocks += bytesToBlocks(StreamSizes[I], 4096);
285 }
286 ++ExpectedNumBlocks; // The directory itself should use 1 block
287
288 auto ExpectedLayout = Msf.build();
289 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
290 MSFLayout &L = *ExpectedLayout;
291 EXPECT_EQ(4096U, L.SB->BlockSize);
292 EXPECT_EQ(ExpectedNumBlocks, L.SB->NumBlocks);
293
294 EXPECT_EQ(1U, L.DirectoryBlocks.size());
295
296 EXPECT_EQ(3U, L.StreamMap.size());
297 EXPECT_EQ(3U, L.StreamSizes.size());
298 for (int I = 0; I < 3; ++I) {
299 EXPECT_EQ(StreamSizes[I], L.StreamSizes[I]);
300 uint32_t ExpectedNumBlocks = bytesToBlocks(StreamSizes[I], 4096);
301 EXPECT_EQ(ExpectedNumBlocks, L.StreamMap[I].size());
302 }
303 }
304
305 TEST_F(MSFBuilderTest, UseDirectoryBlockHint) {
306 Expected ExpectedMsf = MSFBuilder::create(
307 Allocator, 4096, msf::getMinimumBlockCount() + 1, false);
308 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
309 auto &Msf = *ExpectedMsf;
310
311 uint32_t B = msf::getFirstUnreservedBlock();
312 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
313 EXPECT_THAT_EXPECTED(Msf.addStream(2048, {B + 2}), Succeeded());
314
315 auto ExpectedLayout = Msf.build();
316 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
317 MSFLayout &L = *ExpectedLayout;
318 EXPECT_EQ(msf::getMinimumBlockCount() + 2, L.SB->NumBlocks);
319 EXPECT_EQ(1U, L.DirectoryBlocks.size());
320 EXPECT_EQ(1U, L.StreamMap[0].size());
321
322 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
323 EXPECT_EQ(B + 2, L.StreamMap[0].front());
324 }
325
326 TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) {
327 Expected ExpectedMsf =
328 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
329 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
330 auto &Msf = *ExpectedMsf;
331 uint32_t B = msf::getFirstUnreservedBlock();
332 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
333
334 uint32_t Size = 4096 * 4096 / 4;
335 EXPECT_THAT_EXPECTED(Msf.addStream(Size), Succeeded());
336
337 auto ExpectedLayout = Msf.build();
338 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
339 MSFLayout &L = *ExpectedLayout;
340 EXPECT_EQ(2U, L.DirectoryBlocks.size());
341 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
342 }
343
344 TEST_F(MSFBuilderTest, DirectoryBlockHintOverestimated) {
345 Expected ExpectedMsf =
346 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
347 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
348 auto &Msf = *ExpectedMsf;
349
350 uint32_t B = msf::getFirstUnreservedBlock();
351 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2}), Succeeded());
352
353 ASSERT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
354
355 auto ExpectedLayout = Msf.build();
356 ASSERT_THAT_EXPECTED(ExpectedLayout, Succeeded());
357 MSFLayout &L = *ExpectedLayout;
358 EXPECT_EQ(1U, L.DirectoryBlocks.size());
359 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
360 }
0 //===- llvm/unittest/DebugInfo/MSF/MappedBlockStreamTest.cpp --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
10 #include "llvm/DebugInfo/MSF/IMSFFile.h"
11 #include "llvm/DebugInfo/MSF/MSFError.h"
12 #include "llvm/Support/BinaryByteStream.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamRef.h"
15 #include "llvm/Support/BinaryStreamWriter.h"
16 #include "llvm/Testing/Support/Error.h"
17
18 #include "gtest/gtest.h"
19
20 #include
21
22 using namespace llvm;
23 using namespace llvm::msf;
24 using namespace llvm::support;
25
26 namespace {
27
28 static const uint32_t BlocksAry[] = {0, 1, 2, 5, 4, 3, 6, 7, 8, 9};
29 static uint8_t DataAry[] = {'A', 'B', 'C', 'F', 'E', 'D', 'G', 'H', 'I', 'J'};
30
31 class DiscontiguousStream : public WritableBinaryStream {
32 public:
33 DiscontiguousStream(ArrayRef Blocks, MutableArrayRef Data)
34 : Blocks(Blocks.begin(), Blocks.end()), Data(Data.begin(), Data.end()) {}
35
36 uint32_t block_size() const { return 1; }
37 uint32_t block_count() const { return Blocks.size(); }
38
39 endianness getEndian() const override { return little; }
40
41 Error readBytes(uint32_t Offset, uint32_t Size,
42 ArrayRef &Buffer) override {
43 if (auto EC = checkOffset(Offset, Size))
44 return EC;
45 Buffer = Data.slice(Offset, Size);
46 return Error::success();
47 }
48
49 Error readLongestContiguousChunk(uint32_t Offset,
50 ArrayRef &Buffer) override {
51 if (auto EC = checkOffset(Offset, 1))
52 return EC;
53 Buffer = Data.drop_front(Offset);
54 return Error::success();
55 }
56
57 uint32_t getLength() override { return Data.size(); }
58
59 Error writeBytes(uint32_t Offset, ArrayRef SrcData) override {
60 if (auto EC = checkOffset(Offset, SrcData.size()))
61 return EC;
62 ::memcpy(&Data[Offset], SrcData.data(), SrcData.size());
63 return Error::success();
64 }
65 Error commit() override { return Error::success(); }
66
67 MSFStreamLayout layout() const {
68 return MSFStreamLayout{static_cast(Data.size()), Blocks};
69 }
70
71 BumpPtrAllocator Allocator;
72
73 private:
74 std::vector Blocks;
75 MutableArrayRef Data;
76 };
77
78 TEST(MappedBlockStreamTest, NumBlocks) {
79 DiscontiguousStream F(BlocksAry, DataAry);
80 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
81 F.Allocator);
82 EXPECT_EQ(F.block_size(), S->getBlockSize());
83 EXPECT_EQ(F.layout().Blocks.size(), S->getNumBlocks());
84 }
85
86 // Tests that a read which is entirely contained within a single block works
87 // and does not allocate.
88 TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
89 DiscontiguousStream F(BlocksAry, DataAry);
90 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
91 F.Allocator);
92
93 BinaryStreamReader R(*S);
94 BinaryStreamRef SR;
95 EXPECT_THAT_ERROR(R.readStreamRef(SR, 0U), Succeeded());
96 ArrayRef Buffer;
97 EXPECT_THAT_ERROR(SR.readBytes(0U, 1U, Buffer), Failed());
98 EXPECT_THAT_ERROR(R.readStreamRef(SR, 1U), Succeeded());
99 EXPECT_THAT_ERROR(SR.readBytes(1U, 1U, Buffer), Failed());
100 }
101
102 // Tests that a read which outputs into a full destination buffer works and
103 // does not fail due to the length of the output buffer.
104 TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {
105 DiscontiguousStream F(BlocksAry, DataAry);
106 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
107 F.Allocator);
108
109 BinaryStreamReader R(*S);
110 StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
111 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
112 EXPECT_EQ(Str, StringRef("A"));
113 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
114 }
115
116 // Tests that a read which crosses a block boundary, but where the subsequent
117 // blocks are still contiguous in memory to the previous block works and does
118 // not allocate memory.
119 TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {
120 DiscontiguousStream F(BlocksAry, DataAry);
121 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
122 F.Allocator);
123 BinaryStreamReader R(*S);
124 StringRef Str;
125 EXPECT_THAT_ERROR(R.readFixedString(Str, 2), Succeeded());
126 EXPECT_EQ(Str, StringRef("AB"));
127 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
128
129 R.setOffset(6);
130 EXPECT_THAT_ERROR(R.readFixedString(Str, 4), Succeeded());
131 EXPECT_EQ(Str, StringRef("GHIJ"));
132 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
133 }
134
135 // Tests that a read which crosses a block boundary and cannot be referenced
136 // contiguously works and allocates only the precise amount of bytes
137 // requested.
138 TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {
139 DiscontiguousStream F(BlocksAry, DataAry);
140 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
141 F.Allocator);
142 BinaryStreamReader R(*S);
143 StringRef Str;
144 EXPECT_THAT_ERROR(R.readFixedString(Str, 10), Succeeded());
145 EXPECT_EQ(Str, StringRef("ABCDEFGHIJ"));
146 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
147 }
148
149 // Test that an out of bounds read which doesn't cross a block boundary
150 // fails and allocates no memory.
151 TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {
152 DiscontiguousStream F(BlocksAry, DataAry);
153 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
154 F.Allocator);
155 BinaryStreamReader R(*S);
156 StringRef Str;
157
158 R.setOffset(10);
159 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Failed());
160 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
161 }
162
163 // Test that an out of bounds read which crosses a contiguous block boundary
164 // fails and allocates no memory.
165 TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {
166 DiscontiguousStream F(BlocksAry, DataAry);
167 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
168 F.Allocator);
169 BinaryStreamReader R(*S);
170 StringRef Str;
171
172 R.setOffset(6);
173 EXPECT_THAT_ERROR(R.readFixedString(Str, 5), Failed());
174 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
175 }
176
177 // Test that an out of bounds read which crosses a discontiguous block
178 // boundary fails and allocates no memory.
179 TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
180 DiscontiguousStream F(BlocksAry, DataAry);
181 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
182 F.Allocator);
183 BinaryStreamReader R(*S);
184 StringRef Str;
185
186 EXPECT_THAT_ERROR(R.readFixedString(Str, 11), Failed());
187 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
188 }
189
190 // Tests that a read which is entirely contained within a single block but
191 // beyond the end of a StreamRef fails.
192 TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
193 DiscontiguousStream F(BlocksAry, DataAry);
194 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
195 F.Allocator);
196 BinaryStreamReader R(*S);
197 StringRef Str;
198 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
199 EXPECT_EQ(Str, StringRef("A"));
200 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
201 }
202
203 // Tests that a read which is not aligned on the same boundary as a previous
204 // cached request, but which is known to overlap that request, shares the
205 // previous allocation.
206 TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {
207 DiscontiguousStream F(BlocksAry, DataAry);
208 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
209 F.Allocator);
210 BinaryStreamReader R(*S);
211 StringRef Str1;
212 StringRef Str2;
213 EXPECT_THAT_ERROR(R.readFixedString(Str1, 7), Succeeded());
214 EXPECT_EQ(Str1, StringRef("ABCDEFG"));
215 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
216
217 R.setOffset(2);
218 EXPECT_THAT_ERROR(R.readFixedString(Str2, 3), Succeeded());
219 EXPECT_EQ(Str2, StringRef("CDE"));
220 EXPECT_EQ(Str1.data() + 2, Str2.data());
221 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
222 }
223
224 // Tests that a read which is not aligned on the same boundary as a previous
225 // cached request, but which only partially overlaps a previous cached request,
226 // still works correctly and allocates again from the shared pool.
227 TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {
228 DiscontiguousStream F(BlocksAry, DataAry);
229 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
230 F.Allocator);
231 BinaryStreamReader R(*S);
232 StringRef Str1;
233 StringRef Str2;
234 EXPECT_THAT_ERROR(R.readFixedString(Str1, 6), Succeeded());
235 EXPECT_EQ(Str1, StringRef("ABCDEF"));
236 EXPECT_EQ(6U, F.Allocator.getBytesAllocated());
237
238 R.setOffset(4);
239 EXPECT_THAT_ERROR(R.readFixedString(Str2, 4), Succeeded());
240 EXPECT_EQ(Str2, StringRef("EFGH"));
241 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
242 }
243
244 TEST(MappedBlockStreamTest, WriteBeyondEndOfStream) {
245 static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
246 static uint8_t LargeBuffer[] = {'0', '1', '2', '3', '4', '5',
247 '6', '7', '8', '9', 'A'};
248 static uint8_t SmallBuffer[] = {'0', '1', '2'};
249 static_assert(sizeof(LargeBuffer) > sizeof(Data),
250 "LargeBuffer is not big enough");
251
252 DiscontiguousStream F(BlocksAry, Data);
253 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
254 F, F.Allocator);
255 ArrayRef Buffer;
256
257 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(LargeBuffer)), Failed());
258 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(SmallBuffer)),
259 Succeeded());
260 EXPECT_THAT_ERROR(S->writeBytes(7, ArrayRef(SmallBuffer)),
261 Succeeded());
262 EXPECT_THAT_ERROR(S->writeBytes(8, ArrayRef(SmallBuffer)), Failed());
263 }
264
265 TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {
266 static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
267 DiscontiguousStream F(BlocksAry, Data);
268 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
269 F, F.Allocator);
270 ArrayRef Buffer;
271
272 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
273 EXPECT_EQ(Buffer, ArrayRef('A'));
274 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
275 EXPECT_EQ(Buffer, ArrayRef('J'));
276
277 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('J')), Succeeded());
278 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('A')), Succeeded());
279
280 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
281 EXPECT_EQ(Buffer, ArrayRef('J'));
282 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
283 EXPECT_EQ(Buffer, ArrayRef('A'));
284
285 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('A')), Succeeded());
286 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('J')), Succeeded());
287
288 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
289 EXPECT_EQ(Buffer, ArrayRef('A'));
290 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
291 EXPECT_EQ(Buffer, ArrayRef('J'));
292 }
293
294 TEST(MappedBlockStreamTest, TestWriteBytesBreakBoundary) {
295 static uint8_t Data[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
296 static uint8_t TestData[] = {'T', 'E', 'S', 'T', 'I', 'N', 'G', '.'};
297 static uint8_t Expected[] = {'T', 'E', 'S', 'N', 'I',
298 'T', 'G', '.', '0', '0'};
299
300 DiscontiguousStream F(BlocksAry, Data);
301 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
302 F, F.Allocator);
303 ArrayRef Buffer;
304
305 EXPECT_THAT_ERROR(S->writeBytes(0, TestData), Succeeded());
306 // First just compare the memory, then compare the result of reading the
307 // string out.
308 EXPECT_EQ(ArrayRef(Data), ArrayRef(Expected));
309
310 EXPECT_THAT_ERROR(S->readBytes(0, 8, Buffer), Succeeded());
311 EXPECT_EQ(Buffer, ArrayRef(TestData));
312 }
313
314 TEST(MappedBlockStreamTest, TestWriteThenRead) {
315 std::vector DataBytes(10);
316 MutableArrayRef Data(DataBytes);
317 const uint32_t Blocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
318
319 DiscontiguousStream F(Blocks, Data);
320 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
321 F, F.Allocator);
322
323 enum class MyEnum : uint32_t { Val1 = 2908234, Val2 = 120891234 };
324 using support::ulittle32_t;
325
326 uint16_t u16[] = {31468, 0};
327 uint32_t u32[] = {890723408, 0};
328 MyEnum Enum[] = {MyEnum::Val1, MyEnum::Val2};
329 StringRef ZStr[] = {"Zero Str", ""};
330 StringRef FStr[] = {"Fixed Str", ""};
331 uint8_t byteArray0[] = {'1', '2'};
332 uint8_t byteArray1[] = {'0', '0'};
333 ArrayRef byteArrayRef0(byteArray0);
334 ArrayRef byteArrayRef1(byteArray1);
335 ArrayRef byteArray[] = {byteArrayRef0, byteArrayRef1};
336 uint32_t intArr0[] = {890723408, 29082234};
337 uint32_t intArr1[] = {890723408, 29082234};
338 ArrayRef intArray[] = {intArr0, intArr1};
339
340 BinaryStreamReader Reader(*S);
341 BinaryStreamWriter Writer(*S);
342 EXPECT_THAT_ERROR(Writer.writeInteger(u16[0]), Succeeded());
343 EXPECT_THAT_ERROR(Reader.readInteger(u16[1]), Succeeded());
344 EXPECT_EQ(u16[0], u16[1]);
345 EXPECT_EQ(std::vector({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}),
346 DataBytes);
347
348 Reader.setOffset(0);
349 Writer.setOffset(0);
350 ::memset(DataBytes.data(), 0, 10);
351 EXPECT_THAT_ERROR(Writer.writeInteger(u32[0]), Succeeded());
352 EXPECT_THAT_ERROR(Reader.readInteger(u32[1]), Succeeded());
353 EXPECT_EQ(u32[0], u32[1]);
354 EXPECT_EQ(std::vector({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}),
355 DataBytes);
356
357 Reader.setOffset(0);
358 Writer.setOffset(0);
359 ::memset(DataBytes.data(), 0, 10);
360 EXPECT_THAT_ERROR(Writer.writeEnum(Enum[0]), Succeeded());
361 EXPECT_THAT_ERROR(Reader.readEnum(Enum[1]), Succeeded());
362 EXPECT_EQ(Enum[0], Enum[1]);
363 EXPECT_EQ(std::vector({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}),
364 DataBytes);
365
366 Reader.setOffset(0);
367 Writer.setOffset(0);
368 ::memset(DataBytes.data(), 0, 10);
369 EXPECT_THAT_ERROR(Writer.writeCString(ZStr[0]), Succeeded());
370 EXPECT_THAT_ERROR(Reader.readCString(ZStr[1]), Succeeded());
371 EXPECT_EQ(ZStr[0], ZStr[1]);
372 EXPECT_EQ(
373 std::vector({'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}),
374 DataBytes);
375
376 Reader.setOffset(0);
377 Writer.setOffset(0);
378 ::memset(DataBytes.data(), 0, 10);
379 EXPECT_THAT_ERROR(Writer.writeFixedString(FStr[0]), Succeeded());
380 EXPECT_THAT_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()),
381 Succeeded());
382 EXPECT_EQ(FStr[0], FStr[1]);
383 EXPECT_EQ(
384 std::vector({'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}),
385 DataBytes);
386
387 Reader.setOffset(0);
388 Writer.setOffset(0);
389 ::memset(DataBytes.data(), 0, 10);
390 EXPECT_THAT_ERROR(Writer.writeArray(byteArray[0]), Succeeded());
391 EXPECT_THAT_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()),
392 Succeeded());
393 EXPECT_EQ(byteArray[0], byteArray[1]);
394 EXPECT_EQ(std::vector({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}),
395 DataBytes);
396
397 Reader.setOffset(0);
398 Writer.setOffset(0);
399 ::memset(DataBytes.data(), 0, 10);
400 EXPECT_THAT_ERROR(Writer.writeArray(intArray[0]), Succeeded());
401 EXPECT_THAT_ERROR(Reader.readArray(intArray[1], intArray[0].size()),
402 Succeeded());
403 EXPECT_EQ(intArray[0], intArray[1]);
404 }
405
406 TEST(MappedBlockStreamTest, TestWriteContiguousStreamRef) {
407 std::vector DestDataBytes(10);
408 MutableArrayRef DestData(DestDataBytes);
409 const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
410
411 std::vector SrcDataBytes(10);
412 MutableArrayRef SrcData(SrcDataBytes);
413
414 DiscontiguousStream F(DestBlocks, DestData);
415 auto DestStream = WritableMappedBlockStream::createStream(
416 F.block_size(), F.layout(), F, F.Allocator);
417
418 // First write "Test Str" into the source stream.
419 MutableBinaryByteStream SourceStream(SrcData, little);
420 BinaryStreamWriter SourceWriter(SourceStream);
421 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
422 EXPECT_EQ(SrcDataBytes, std::vector(
423 {'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0}));
424
425 // Then write the source stream into the dest stream.
426 BinaryStreamWriter DestWriter(*DestStream);
427 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(SourceStream), Succeeded());
428 EXPECT_EQ(DestDataBytes, std::vector(
429 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
430
431 // Then read the string back out of the dest stream.
432 StringRef Result;
433 BinaryStreamReader DestReader(*DestStream);
434 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
435 EXPECT_EQ(Result, "Test Str");
436 }
437
438 TEST(MappedBlockStreamTest, TestWriteDiscontiguousStreamRef) {
439 std::vector DestDataBytes(10);
440 MutableArrayRef DestData(DestDataBytes);
441 const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
442
443 std::vector SrcDataBytes(10);
444 MutableArrayRef SrcData(SrcDataBytes);
445 const uint32_t SrcBlocks[] = {1, 0, 6, 3, 4, 5, 2, 7, 8, 9};
446
447 DiscontiguousStream DestF(DestBlocks, DestData);
448 DiscontiguousStream SrcF(SrcBlocks, SrcData);
449
450 auto Dest = WritableMappedBlockStream::createStream(
451 DestF.block_size(), DestF.layout(), DestF, DestF.Allocator);
452 auto Src = WritableMappedBlockStream::createStream(
453 SrcF.block_size(), SrcF.layout(), SrcF, SrcF.Allocator);
454
455 // First write "Test Str" into the source stream.
456 BinaryStreamWriter SourceWriter(*Src);
457 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
458 EXPECT_EQ(SrcDataBytes, std::vector(
459 {'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0}));
460
461 // Then write the source stream into the dest stream.
462 BinaryStreamWriter DestWriter(*Dest);
463 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(*Src), Succeeded());
464 EXPECT_EQ(DestDataBytes, std::vector(
465 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
466
467 // Then read the string back out of the dest stream.
468 StringRef Result;
469 BinaryStreamReader DestReader(*Dest);
470 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
471 EXPECT_EQ(Result, "Test Str");
472 }
473
474 TEST(MappedBlockStreamTest, DataLivesAfterStreamDestruction) {
475 std::vector DataBytes(10);
476 MutableArrayRef Data(DataBytes);
477 const uint32_t Blocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
478
479 StringRef Str[] = {"Zero Str", ""};
480
481 DiscontiguousStream F(Blocks, Data);
482 {
483 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
484 F, F.Allocator);
485
486 BinaryStreamReader Reader(*S);
487 BinaryStreamWriter Writer(*S);
488 ::memset(DataBytes.data(), 0, 10);
489 EXPECT_THAT_ERROR(Writer.writeCString(Str[0]), Succeeded());
490 EXPECT_THAT_ERROR(Reader.readCString(Str[1]), Succeeded());
491 EXPECT_EQ(Str[0], Str[1]);
492 }
493
494 EXPECT_EQ(Str[0], Str[1]);
495 }
496
497 } // end anonymous namespace
55
66 set(DebugInfoPDBSources
77 HashTableTest.cpp
8 MappedBlockStreamTest.cpp
98 StringTableBuilderTest.cpp
10 MSFBuilderTest.cpp
119 PDBApiTest.cpp
1210 )
1311
+0
-361
unittests/DebugInfo/PDB/MSFBuilderTest.cpp less more
None //===- MSFBuilderTest.cpp Tests manipulation of MSF stream metadata ------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
10 #include "llvm/DebugInfo/MSF/MSFCommon.h"
11 #include "llvm/Testing/Support/Error.h"
12
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace llvm::msf;
17
18 namespace {
19 class MSFBuilderTest : public testing::Test {
20 protected:
21 void initializeSimpleSuperBlock(msf::SuperBlock &SB) {
22 initializeSuperBlock(SB);
23 SB.NumBlocks = 1000;
24 SB.NumDirectoryBytes = 8192;
25 }
26
27 void initializeSuperBlock(msf::SuperBlock &SB) {
28 ::memset(&SB, 0, sizeof(SB));
29
30 ::memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic));
31 SB.FreeBlockMapBlock = 1;
32 SB.BlockMapAddr = 1;
33 SB.BlockSize = 4096;
34 SB.NumDirectoryBytes = 0;
35 SB.NumBlocks = 2; // one for the Super Block, one for the directory
36 }
37
38 BumpPtrAllocator Allocator;
39 };
40 }
41
42 TEST_F(MSFBuilderTest, ValidateSuperBlockAccept) {
43 // Test that a known good super block passes validation.
44 SuperBlock SB;
45 initializeSuperBlock(SB);
46
47 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
48 }
49
50 TEST_F(MSFBuilderTest, ValidateSuperBlockReject) {
51 // Test that various known problems cause a super block to be rejected.
52 SuperBlock SB;
53 initializeSimpleSuperBlock(SB);
54
55 // Mismatched magic
56 SB.MagicBytes[0] = 8;
57 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
58 initializeSimpleSuperBlock(SB);
59
60 // Block 0 is reserved for super block, can't be occupied by the block map
61 SB.BlockMapAddr = 0;
62 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
63 initializeSimpleSuperBlock(SB);
64
65 // Block sizes have to be powers of 2.
66 SB.BlockSize = 3120;
67 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
68 initializeSimpleSuperBlock(SB);
69
70 // The directory itself has a maximum size.
71 SB.NumDirectoryBytes = SB.BlockSize * SB.BlockSize / 4;
72 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Succeeded());
73 SB.NumDirectoryBytes = SB.NumDirectoryBytes + 4;
74 EXPECT_THAT_ERROR(msf::validateSuperBlock(SB), Failed());
75 }
76
77 TEST_F(MSFBuilderTest, TestUsedBlocksMarkedAsUsed) {
78 // Test that when assigning a stream to a known list of blocks, the blocks
79 // are correctly marked as used after adding, but no other incorrect blocks
80 // are accidentally marked as used.
81
82 std::vector Blocks = {4, 5, 6, 7, 8, 9, 10, 11, 12};
83 // Allocate some extra blocks at the end so we can verify that they're free
84 // after the initialization.
85 uint32_t NumBlocks = msf::getMinimumBlockCount() + Blocks.size() + 10;
86 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096, NumBlocks);
87 ASSERT_THAT_EXPECTED(ExpectedMsf, Succeeded());
88 auto &Msf = *ExpectedMsf;
89
90 EXPECT_THAT_EXPECTED(Msf.addStream(Blocks.size() * 4096, Blocks),
91 Succeeded());
92
93 for (auto B : Blocks) {
94 EXPECT_FALSE(Msf.isBlockFree(B));
95 }
96
97 uint32_t FreeBlockStart = Blocks.back() + 1;
98 for (uint32_t I = FreeBlockStart; I < NumBlocks; ++I) {
99 EXPECT_TRUE(Msf.isBlockFree(I));
100 }
101 }
102
103 TEST_F(MSFBuilderTest, TestAddStreamNoDirectoryBlockIncrease) {
104 // Test that adding a new stream correctly updates the directory. This only
105 // tests the case where the directory *DOES NOT* grow large enough that it
106 // crosses a Block boundary.
107 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
108 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
109 auto &Msf = *ExpectedMsf;
110
111 auto ExpectedL1 = Msf.build();
112 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
113 MSFLayout &L1 = *ExpectedL1;
114
115 auto OldDirBlocks = L1.DirectoryBlocks;
116 EXPECT_EQ(1U, OldDirBlocks.size());
117
118 auto ExpectedMsf2 = MSFBuilder::create(Allocator, 4096);
119 EXPECT_THAT_EXPECTED(ExpectedMsf2, Succeeded());
120 auto &Msf2 = *ExpectedMsf2;
121
122 EXPECT_THAT_EXPECTED(Msf2.addStream(4000), Succeeded());
123 EXPECT_EQ(1U, Msf2.getNumStreams());
124 EXPECT_EQ(4000U, Msf2.getStreamSize(0));
125 auto Blocks = Msf2.getStreamBlocks(0);
126 EXPECT_EQ(1U, Blocks.size());
127
128 auto ExpectedL2 = Msf2.build();
129 EXPECT_THAT_EXPECTED(ExpectedL2, Succeeded());
130 MSFLayout &L2 = *ExpectedL2;
131 auto NewDirBlocks = L2.DirectoryBlocks;
132 EXPECT_EQ(1U, NewDirBlocks.size());
133 }
134
135 TEST_F(MSFBuilderTest, TestAddStreamWithDirectoryBlockIncrease) {
136 // Test that adding a new stream correctly updates the directory. This only
137 // tests the case where the directory *DOES* grow large enough that it
138 // crosses a Block boundary. This is because the newly added stream occupies
139 // so many Blocks that need to be indexed in the directory that the directory
140 // crosses a Block boundary.
141 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
142 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
143 auto &Msf = *ExpectedMsf;
144
145 EXPECT_THAT_EXPECTED(Msf.addStream(4096 * 4096 / sizeof(uint32_t)),
146 Succeeded());
147
148 auto ExpectedL1 = Msf.build();
149 EXPECT_THAT_EXPECTED(ExpectedL1, Succeeded());
150 MSFLayout &L1 = *ExpectedL1;
151 auto DirBlocks = L1.DirectoryBlocks;
152 EXPECT_EQ(2U, DirBlocks.size());
153 }
154
155 TEST_F(MSFBuilderTest, TestGrowStreamNoBlockIncrease) {
156 // Test growing an existing stream by a value that does not affect the number
157 // of blocks it occupies.
158 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
159 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
160 auto &Msf = *ExpectedMsf;
161
162 EXPECT_THAT_EXPECTED(Msf.addStream(1024), Succeeded());
163 EXPECT_EQ(1024U, Msf.getStreamSize(0));
164 auto OldStreamBlocks = Msf.getStreamBlocks(0);
165 EXPECT_EQ(1U, OldStreamBlocks.size());
166
167 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
168 EXPECT_EQ(2048U, Msf.getStreamSize(0));
169 auto NewStreamBlocks = Msf.getStreamBlocks(0);
170 EXPECT_EQ(1U, NewStreamBlocks.size());
171
172 EXPECT_EQ(OldStreamBlocks, NewStreamBlocks);
173 }
174
175 TEST_F(MSFBuilderTest, TestGrowStreamWithBlockIncrease) {
176 // Test that growing an existing stream to a value large enough that it causes
177 // the need to allocate new Blocks to the stream correctly updates the
178 // stream's
179 // block list.
180 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
181 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
182 auto &Msf = *ExpectedMsf;
183
184 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
185 EXPECT_EQ(2048U, Msf.getStreamSize(0));
186 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
187 EXPECT_EQ(1U, OldStreamBlocks.size());
188
189 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 6144), Succeeded());
190 EXPECT_EQ(6144U, Msf.getStreamSize(0));
191 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
192 EXPECT_EQ(2U, NewStreamBlocks.size());
193
194 EXPECT_EQ(OldStreamBlocks[0], NewStreamBlocks[0]);
195 EXPECT_NE(NewStreamBlocks[0], NewStreamBlocks[1]);
196 }
197
198 TEST_F(MSFBuilderTest, TestShrinkStreamNoBlockDecrease) {
199 // Test that shrinking an existing stream by a value that does not affect the
200 // number of Blocks it occupies makes no changes to stream's block list.
201 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
202 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
203 auto &Msf = *ExpectedMsf;
204
205 EXPECT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
206 EXPECT_EQ(2048U, Msf.getStreamSize(0));
207 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
208 EXPECT_EQ(1U, OldStreamBlocks.size());
209
210 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 1024), Succeeded());
211 EXPECT_EQ(1024U, Msf.getStreamSize(0));
212 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
213 EXPECT_EQ(1U, NewStreamBlocks.size());
214
215 EXPECT_EQ(OldStreamBlocks, NewStreamBlocks);
216 }
217
218 TEST_F(MSFBuilderTest, TestShrinkStreamWithBlockDecrease) {
219 // Test that shrinking an existing stream to a value large enough that it
220 // causes the need to deallocate new Blocks to the stream correctly updates
221 // the stream's block list.
222 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
223 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
224 auto &Msf = *ExpectedMsf;
225
226 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
227 EXPECT_EQ(6144U, Msf.getStreamSize(0));
228 std::vector OldStreamBlocks = Msf.getStreamBlocks(0);
229 EXPECT_EQ(2U, OldStreamBlocks.size());
230
231 EXPECT_THAT_ERROR(Msf.setStreamSize(0, 2048), Succeeded());
232 EXPECT_EQ(2048U, Msf.getStreamSize(0));
233 std::vector NewStreamBlocks = Msf.getStreamBlocks(0);
234 EXPECT_EQ(1U, NewStreamBlocks.size());
235
236 EXPECT_EQ(OldStreamBlocks[0], NewStreamBlocks[0]);
237 }
238
239 TEST_F(MSFBuilderTest, TestRejectReusedStreamBlock) {
240 // Test that attempting to add a stream and assigning a block that is already
241 // in use by another stream fails.
242 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
243 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
244 auto &Msf = *ExpectedMsf;
245
246 EXPECT_THAT_EXPECTED(Msf.addStream(6144), Succeeded());
247
248 std::vector Blocks = {2, 3};
249 EXPECT_THAT_EXPECTED(Msf.addStream(6144, Blocks), Failed());
250 }
251
252 TEST_F(MSFBuilderTest, TestBlockCountsWhenAddingStreams) {
253 // Test that when adding multiple streams, the number of used and free Blocks
254 // allocated to the MSF file are as expected.
255 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
256 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
257 auto &Msf = *ExpectedMsf;
258
259 // one for the super block, one for the directory block map
260 uint32_t NumUsedBlocks = Msf.getNumUsedBlocks();
261 EXPECT_EQ(msf::getMinimumBlockCount(), NumUsedBlocks);
262 EXPECT_EQ(0U, Msf.getNumFreeBlocks());
263
264 const uint32_t StreamSizes[] = {4000, 6193, 189723};
265 for (int I = 0; I < 3; ++I) {
266 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
267 NumUsedBlocks += bytesToBlocks(StreamSizes[I], 4096);
268 EXPECT_EQ(NumUsedBlocks, Msf.getNumUsedBlocks());
269 EXPECT_EQ(0U, Msf.getNumFreeBlocks());
270 }
271 }
272
273 TEST_F(MSFBuilderTest, BuildMsfLayout) {
274 // Test that we can generate an MSFLayout structure from a valid layout
275 // specification.
276 auto ExpectedMsf = MSFBuilder::create(Allocator, 4096);
277 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
278 auto &Msf = *ExpectedMsf;
279
280 const uint32_t StreamSizes[] = {4000, 6193, 189723};
281 uint32_t ExpectedNumBlocks = msf::getMinimumBlockCount();
282 for (int I = 0; I < 3; ++I) {
283 EXPECT_THAT_EXPECTED(Msf.addStream(StreamSizes[I]), Succeeded());
284 ExpectedNumBlocks += bytesToBlocks(StreamSizes[I], 4096);
285 }
286 ++ExpectedNumBlocks; // The directory itself should use 1 block
287
288 auto ExpectedLayout = Msf.build();
289 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
290 MSFLayout &L = *ExpectedLayout;
291 EXPECT_EQ(4096U, L.SB->BlockSize);
292 EXPECT_EQ(ExpectedNumBlocks, L.SB->NumBlocks);
293
294 EXPECT_EQ(1U, L.DirectoryBlocks.size());
295
296 EXPECT_EQ(3U, L.StreamMap.size());
297 EXPECT_EQ(3U, L.StreamSizes.size());
298 for (int I = 0; I < 3; ++I) {
299 EXPECT_EQ(StreamSizes[I], L.StreamSizes[I]);
300 uint32_t ExpectedNumBlocks = bytesToBlocks(StreamSizes[I], 4096);
301 EXPECT_EQ(ExpectedNumBlocks, L.StreamMap[I].size());
302 }
303 }
304
305 TEST_F(MSFBuilderTest, UseDirectoryBlockHint) {
306 Expected ExpectedMsf = MSFBuilder::create(
307 Allocator, 4096, msf::getMinimumBlockCount() + 1, false);
308 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
309 auto &Msf = *ExpectedMsf;
310
311 uint32_t B = msf::getFirstUnreservedBlock();
312 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
313 EXPECT_THAT_EXPECTED(Msf.addStream(2048, {B + 2}), Succeeded());
314
315 auto ExpectedLayout = Msf.build();
316 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
317 MSFLayout &L = *ExpectedLayout;
318 EXPECT_EQ(msf::getMinimumBlockCount() + 2, L.SB->NumBlocks);
319 EXPECT_EQ(1U, L.DirectoryBlocks.size());
320 EXPECT_EQ(1U, L.StreamMap[0].size());
321
322 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
323 EXPECT_EQ(B + 2, L.StreamMap[0].front());
324 }
325
326 TEST_F(MSFBuilderTest, DirectoryBlockHintInsufficient) {
327 Expected ExpectedMsf =
328 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
329 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
330 auto &Msf = *ExpectedMsf;
331 uint32_t B = msf::getFirstUnreservedBlock();
332 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1}), Succeeded());
333
334 uint32_t Size = 4096 * 4096 / 4;
335 EXPECT_THAT_EXPECTED(Msf.addStream(Size), Succeeded());
336
337 auto ExpectedLayout = Msf.build();
338 EXPECT_THAT_EXPECTED(ExpectedLayout, Succeeded());
339 MSFLayout &L = *ExpectedLayout;
340 EXPECT_EQ(2U, L.DirectoryBlocks.size());
341 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
342 }
343
344 TEST_F(MSFBuilderTest, DirectoryBlockHintOverestimated) {
345 Expected ExpectedMsf =
346 MSFBuilder::create(Allocator, 4096, msf::getMinimumBlockCount() + 2);
347 EXPECT_THAT_EXPECTED(ExpectedMsf, Succeeded());
348 auto &Msf = *ExpectedMsf;
349
350 uint32_t B = msf::getFirstUnreservedBlock();
351 EXPECT_THAT_ERROR(Msf.setDirectoryBlocksHint({B + 1, B + 2}), Succeeded());
352
353 ASSERT_THAT_EXPECTED(Msf.addStream(2048), Succeeded());
354
355 auto ExpectedLayout = Msf.build();
356 ASSERT_THAT_EXPECTED(ExpectedLayout, Succeeded());
357 MSFLayout &L = *ExpectedLayout;
358 EXPECT_EQ(1U, L.DirectoryBlocks.size());
359 EXPECT_EQ(B + 1, L.DirectoryBlocks[0]);
360 }
+0
-499
unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp less more
None //===- llvm/unittest/DebugInfo/PDB/MappedBlockStreamTest.cpp --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
10 #include "llvm/DebugInfo/MSF/IMSFFile.h"
11 #include "llvm/DebugInfo/MSF/MSFError.h"
12 #include "llvm/Support/BinaryByteStream.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamRef.h"
15 #include "llvm/Support/BinaryStreamWriter.h"
16 #include "llvm/Testing/Support/Error.h"
17
18 #include "gtest/gtest.h"
19
20 #include
21
22 using namespace llvm;
23 using namespace llvm::msf;
24 using namespace llvm::support;
25
26 namespace {
27
28 static const uint32_t BlocksAry[] = {0, 1, 2, 5, 4, 3, 6, 7, 8, 9};
29 static uint8_t DataAry[] = {'A', 'B', 'C', 'F', 'E', 'D', 'G', 'H', 'I', 'J'};
30
31 class DiscontiguousStream : public WritableBinaryStream {
32 public:
33 DiscontiguousStream(ArrayRef Blocks, MutableArrayRef Data)
34 : Blocks(Blocks.begin(), Blocks.end()), Data(Data.begin(), Data.end()) {}
35
36 uint32_t block_size() const { return 1; }
37 uint32_t block_count() const { return Blocks.size(); }
38
39 endianness getEndian() const override { return little; }
40
41 Error readBytes(uint32_t Offset, uint32_t Size,
42 ArrayRef &Buffer) override {
43 if (auto EC = checkOffset(Offset, Size))
44 return EC;
45 Buffer = Data.slice(Offset, Size);
46 return Error::success();
47 }
48
49 Error readLongestContiguousChunk(uint32_t Offset,
50 ArrayRef &Buffer) override {
51 if (auto EC = checkOffset(Offset, 1))
52 return EC;
53 Buffer = Data.drop_front(Offset);
54 return Error::success();
55 }
56
57 uint32_t getLength() override { return Data.size(); }
58
59 Error writeBytes(uint32_t Offset, ArrayRef SrcData) override {
60 if (auto EC = checkOffset(Offset, SrcData.size()))
61 return EC;
62 ::memcpy(&Data[Offset], SrcData.data(), SrcData.size());
63 return Error::success();
64 }
65 Error commit() override { return Error::success(); }
66
67 MSFStreamLayout layout() const {
68 return MSFStreamLayout{static_cast(Data.size()), Blocks};
69 }
70
71 BumpPtrAllocator Allocator;
72
73 private:
74 std::vector Blocks;
75 MutableArrayRef Data;
76 };
77
78 TEST(MappedBlockStreamTest, NumBlocks) {
79 DiscontiguousStream F(BlocksAry, DataAry);
80 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
81 F.Allocator);
82 EXPECT_EQ(F.block_size(), S->getBlockSize());
83 EXPECT_EQ(F.layout().Blocks.size(), S->getNumBlocks());
84
85 }
86
87 // Tests that a read which is entirely contained within a single block works
88 // and does not allocate.
89 TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
90 DiscontiguousStream F(BlocksAry, DataAry);
91 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
92 F.Allocator);
93
94 BinaryStreamReader R(*S);
95 BinaryStreamRef SR;
96 EXPECT_THAT_ERROR(R.readStreamRef(SR, 0U), Succeeded());
97 ArrayRef Buffer;
98 EXPECT_THAT_ERROR(SR.readBytes(0U, 1U, Buffer), Failed());
99 EXPECT_THAT_ERROR(R.readStreamRef(SR, 1U), Succeeded());
100 EXPECT_THAT_ERROR(SR.readBytes(1U, 1U, Buffer), Failed());
101 }
102
103 // Tests that a read which outputs into a full destination buffer works and
104 // does not fail due to the length of the output buffer.
105 TEST(MappedBlockStreamTest, ReadOntoNonEmptyBuffer) {
106 DiscontiguousStream F(BlocksAry, DataAry);
107 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
108 F.Allocator);
109
110 BinaryStreamReader R(*S);
111 StringRef Str = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
112 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
113 EXPECT_EQ(Str, StringRef("A"));
114 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
115 }
116
117 // Tests that a read which crosses a block boundary, but where the subsequent
118 // blocks are still contiguous in memory to the previous block works and does
119 // not allocate memory.
120 TEST(MappedBlockStreamTest, ZeroCopyReadContiguousBreak) {
121 DiscontiguousStream F(BlocksAry, DataAry);
122 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
123 F.Allocator);
124 BinaryStreamReader R(*S);
125 StringRef Str;
126 EXPECT_THAT_ERROR(R.readFixedString(Str, 2), Succeeded());
127 EXPECT_EQ(Str, StringRef("AB"));
128 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
129
130 R.setOffset(6);
131 EXPECT_THAT_ERROR(R.readFixedString(Str, 4), Succeeded());
132 EXPECT_EQ(Str, StringRef("GHIJ"));
133 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
134 }
135
136 // Tests that a read which crosses a block boundary and cannot be referenced
137 // contiguously works and allocates only the precise amount of bytes
138 // requested.
139 TEST(MappedBlockStreamTest, CopyReadNonContiguousBreak) {
140 DiscontiguousStream F(BlocksAry, DataAry);
141 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
142 F.Allocator);
143 BinaryStreamReader R(*S);
144 StringRef Str;
145 EXPECT_THAT_ERROR(R.readFixedString(Str, 10), Succeeded());
146 EXPECT_EQ(Str, StringRef("ABCDEFGHIJ"));
147 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
148 }
149
150 // Test that an out of bounds read which doesn't cross a block boundary
151 // fails and allocates no memory.
152 TEST(MappedBlockStreamTest, InvalidReadSizeNoBreak) {
153 DiscontiguousStream F(BlocksAry, DataAry);
154 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
155 F.Allocator);
156 BinaryStreamReader R(*S);
157 StringRef Str;
158
159 R.setOffset(10);
160 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Failed());
161 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
162 }
163
164 // Test that an out of bounds read which crosses a contiguous block boundary
165 // fails and allocates no memory.
166 TEST(MappedBlockStreamTest, InvalidReadSizeContiguousBreak) {
167 DiscontiguousStream F(BlocksAry, DataAry);
168 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
169 F.Allocator);
170 BinaryStreamReader R(*S);
171 StringRef Str;
172
173 R.setOffset(6);
174 EXPECT_THAT_ERROR(R.readFixedString(Str, 5), Failed());
175 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
176 }
177
178 // Test that an out of bounds read which crosses a discontiguous block
179 // boundary fails and allocates no memory.
180 TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
181 DiscontiguousStream F(BlocksAry, DataAry);
182 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
183 F.Allocator);
184 BinaryStreamReader R(*S);
185 StringRef Str;
186
187 EXPECT_THAT_ERROR(R.readFixedString(Str, 11), Failed());
188 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
189 }
190
191 // Tests that a read which is entirely contained within a single block but
192 // beyond the end of a StreamRef fails.
193 TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
194 DiscontiguousStream F(BlocksAry, DataAry);
195 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
196 F.Allocator);
197 BinaryStreamReader R(*S);
198 StringRef Str;
199 EXPECT_THAT_ERROR(R.readFixedString(Str, 1), Succeeded());
200 EXPECT_EQ(Str, StringRef("A"));
201 EXPECT_EQ(0U, F.Allocator.getBytesAllocated());
202 }
203
204 // Tests that a read which is not aligned on the same boundary as a previous
205 // cached request, but which is known to overlap that request, shares the
206 // previous allocation.
207 TEST(MappedBlockStreamTest, UnalignedOverlappingRead) {
208 DiscontiguousStream F(BlocksAry, DataAry);
209 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
210 F.Allocator);
211 BinaryStreamReader R(*S);
212 StringRef Str1;
213 StringRef Str2;
214 EXPECT_THAT_ERROR(R.readFixedString(Str1, 7), Succeeded());
215 EXPECT_EQ(Str1, StringRef("ABCDEFG"));
216 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
217
218 R.setOffset(2);
219 EXPECT_THAT_ERROR(R.readFixedString(Str2, 3), Succeeded());
220 EXPECT_EQ(Str2, StringRef("CDE"));
221 EXPECT_EQ(Str1.data() + 2, Str2.data());
222 EXPECT_EQ(7U, F.Allocator.getBytesAllocated());
223 }
224
225 // Tests that a read which is not aligned on the same boundary as a previous
226 // cached request, but which only partially overlaps a previous cached request,
227 // still works correctly and allocates again from the shared pool.
228 TEST(MappedBlockStreamTest, UnalignedOverlappingReadFail) {
229 DiscontiguousStream F(BlocksAry, DataAry);
230 auto S = MappedBlockStream::createStream(F.block_size(), F.layout(), F,
231 F.Allocator);
232 BinaryStreamReader R(*S);
233 StringRef Str1;
234 StringRef Str2;
235 EXPECT_THAT_ERROR(R.readFixedString(Str1, 6), Succeeded());
236 EXPECT_EQ(Str1, StringRef("ABCDEF"));
237 EXPECT_EQ(6U, F.Allocator.getBytesAllocated());
238
239 R.setOffset(4);
240 EXPECT_THAT_ERROR(R.readFixedString(Str2, 4), Succeeded());
241 EXPECT_EQ(Str2, StringRef("EFGH"));
242 EXPECT_EQ(10U, F.Allocator.getBytesAllocated());
243 }
244
245 TEST(MappedBlockStreamTest, WriteBeyondEndOfStream) {
246 static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
247 static uint8_t LargeBuffer[] = {'0', '1', '2', '3', '4', '5',
248 '6', '7', '8', '9', 'A'};
249 static uint8_t SmallBuffer[] = {'0', '1', '2'};
250 static_assert(sizeof(LargeBuffer) > sizeof(Data),
251 "LargeBuffer is not big enough");
252
253 DiscontiguousStream F(BlocksAry, Data);
254 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
255 F, F.Allocator);
256 ArrayRef Buffer;
257
258 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(LargeBuffer)), Failed());
259 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef(SmallBuffer)),
260 Succeeded());
261 EXPECT_THAT_ERROR(S->writeBytes(7, ArrayRef(SmallBuffer)),
262 Succeeded());
263 EXPECT_THAT_ERROR(S->writeBytes(8, ArrayRef(SmallBuffer)), Failed());
264 }
265
266 TEST(MappedBlockStreamTest, TestWriteBytesNoBreakBoundary) {
267 static uint8_t Data[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
268 DiscontiguousStream F(BlocksAry, Data);
269 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
270 F, F.Allocator);
271 ArrayRef Buffer;
272
273 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
274 EXPECT_EQ(Buffer, ArrayRef('A'));
275 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
276 EXPECT_EQ(Buffer, ArrayRef('J'));
277
278 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('J')), Succeeded());
279 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('A')), Succeeded());
280
281 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
282 EXPECT_EQ(Buffer, ArrayRef('J'));
283 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
284 EXPECT_EQ(Buffer, ArrayRef('A'));
285
286 EXPECT_THAT_ERROR(S->writeBytes(0, ArrayRef('A')), Succeeded());
287 EXPECT_THAT_ERROR(S->writeBytes(9, ArrayRef('J')), Succeeded());
288
289 EXPECT_THAT_ERROR(S->readBytes(0, 1, Buffer), Succeeded());
290 EXPECT_EQ(Buffer, ArrayRef('A'));
291 EXPECT_THAT_ERROR(S->readBytes(9, 1, Buffer), Succeeded());
292 EXPECT_EQ(Buffer, ArrayRef('J'));
293 }
294
295 TEST(MappedBlockStreamTest, TestWriteBytesBreakBoundary) {
296 static uint8_t Data[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
297 static uint8_t TestData[] = {'T', 'E', 'S', 'T', 'I', 'N', 'G', '.'};
298 static uint8_t Expected[] = {'T', 'E', 'S', 'N', 'I',
299 'T', 'G', '.', '0', '0'};
300
301 DiscontiguousStream F(BlocksAry, Data);
302 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
303 F, F.Allocator);
304 ArrayRef Buffer;
305
306 EXPECT_THAT_ERROR(S->writeBytes(0, TestData), Succeeded());
307 // First just compare the memory, then compare the result of reading the
308 // string out.
309 EXPECT_EQ(ArrayRef(Data), ArrayRef(Expected));
310
311 EXPECT_THAT_ERROR(S->readBytes(0, 8, Buffer), Succeeded());
312 EXPECT_EQ(Buffer, ArrayRef(TestData));
313 }
314
315 TEST(MappedBlockStreamTest, TestWriteThenRead) {
316 std::vector DataBytes(10);
317 MutableArrayRef Data(DataBytes);
318 const uint32_t Blocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
319
320 DiscontiguousStream F(Blocks, Data);
321 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
322 F, F.Allocator);
323
324 enum class MyEnum : uint32_t { Val1 = 2908234, Val2 = 120891234 };
325 using support::ulittle32_t;
326
327 uint16_t u16[] = {31468, 0};
328 uint32_t u32[] = {890723408, 0};
329 MyEnum Enum[] = {MyEnum::Val1, MyEnum::Val2};
330 StringRef ZStr[] = {"Zero Str", ""};
331 StringRef FStr[] = {"Fixed Str", ""};
332 uint8_t byteArray0[] = {'1', '2'};
333 uint8_t byteArray1[] = {'0', '0'};
334 ArrayRef byteArrayRef0(byteArray0);
335 ArrayRef byteArrayRef1(byteArray1);
336 ArrayRef byteArray[] = { byteArrayRef0, byteArrayRef1 };
337 uint32_t intArr0[] = {890723408, 29082234};
338 uint32_t intArr1[] = {890723408, 29082234};
339 ArrayRef intArray[] = {intArr0, intArr1};
340
341 BinaryStreamReader Reader(*S);
342 BinaryStreamWriter Writer(*S);
343 EXPECT_THAT_ERROR(Writer.writeInteger(u16[0]), Succeeded());
344 EXPECT_THAT_ERROR(Reader.readInteger(u16[1]), Succeeded());
345 EXPECT_EQ(u16[0], u16[1]);
346 EXPECT_EQ(std::vector({0, 0x7A, 0xEC, 0, 0, 0, 0, 0, 0, 0}),
347 DataBytes);
348
349 Reader.setOffset(0);
350 Writer.setOffset(0);
351 ::memset(DataBytes.data(), 0, 10);
352 EXPECT_THAT_ERROR(Writer.writeInteger(u32[0]), Succeeded());
353 EXPECT_THAT_ERROR(Reader.readInteger(u32[1]), Succeeded());
354 EXPECT_EQ(u32[0], u32[1]);
355 EXPECT_EQ(std::vector({0x17, 0x5C, 0x50, 0, 0, 0, 0x35, 0, 0, 0}),
356 DataBytes);
357
358 Reader.setOffset(0);
359 Writer.setOffset(0);
360 ::memset(DataBytes.data(), 0, 10);
361 EXPECT_THAT_ERROR(Writer.writeEnum(Enum[0]), Succeeded());
362 EXPECT_THAT_ERROR(Reader.readEnum(Enum[1]), Succeeded());
363 EXPECT_EQ(Enum[0], Enum[1]);
364 EXPECT_EQ(std::vector({0x2C, 0x60, 0x4A, 0, 0, 0, 0, 0, 0, 0}),
365 DataBytes);
366
367 Reader.setOffset(0);
368 Writer.setOffset(0);
369 ::memset(DataBytes.data(), 0, 10);
370 EXPECT_THAT_ERROR(Writer.writeCString(ZStr[0]), Succeeded());
371 EXPECT_THAT_ERROR(Reader.readCString(ZStr[1]), Succeeded());
372 EXPECT_EQ(ZStr[0], ZStr[1]);
373 EXPECT_EQ(
374 std::vector({'r', 'e', 'Z', ' ', 'S', 't', 'o', 'r', 0, 0}),
375 DataBytes);
376
377 Reader.setOffset(0);
378 Writer.setOffset(0);
379 ::memset(DataBytes.data(), 0, 10);
380 EXPECT_THAT_ERROR(Writer.writeFixedString(FStr[0]), Succeeded());
381 EXPECT_THAT_ERROR(Reader.readFixedString(FStr[1], FStr[0].size()),
382 Succeeded());
383 EXPECT_EQ(FStr[0], FStr[1]);
384 EXPECT_EQ(
385 std::vector({'x', 'i', 'F', 'd', ' ', 'S', 'e', 't', 0, 'r'}),
386 DataBytes);
387
388 Reader.setOffset(0);
389 Writer.setOffset(0);
390 ::memset(DataBytes.data(), 0, 10);
391 EXPECT_THAT_ERROR(Writer.writeArray(byteArray[0]), Succeeded());
392 EXPECT_THAT_ERROR(Reader.readArray(byteArray[1], byteArray[0].size()),
393 Succeeded());
394 EXPECT_EQ(byteArray[0], byteArray[1]);
395 EXPECT_EQ(std::vector({0, 0x32, 0x31, 0, 0, 0, 0, 0, 0, 0}),
396 DataBytes);
397
398 Reader.setOffset(0);
399 Writer.setOffset(0);
400 ::memset(DataBytes.data(), 0, 10);
401 EXPECT_THAT_ERROR(Writer.writeArray(intArray[0]), Succeeded());
402 EXPECT_THAT_ERROR(Reader.readArray(intArray[1], intArray[0].size()),
403 Succeeded());
404 EXPECT_EQ(intArray[0], intArray[1]);
405 }
406
407 TEST(MappedBlockStreamTest, TestWriteContiguousStreamRef) {
408 std::vector DestDataBytes(10);
409 MutableArrayRef DestData(DestDataBytes);
410 const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
411
412 std::vector SrcDataBytes(10);
413 MutableArrayRef SrcData(SrcDataBytes);
414
415 DiscontiguousStream F(DestBlocks, DestData);
416 auto DestStream = WritableMappedBlockStream::createStream(
417 F.block_size(), F.layout(), F, F.Allocator);
418
419 // First write "Test Str" into the source stream.
420 MutableBinaryByteStream SourceStream(SrcData, little);
421 BinaryStreamWriter SourceWriter(SourceStream);
422 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
423 EXPECT_EQ(SrcDataBytes, std::vector(
424 {'T', 'e', 's', 't', ' ', 'S', 't', 'r', 0, 0}));
425
426 // Then write the source stream into the dest stream.
427 BinaryStreamWriter DestWriter(*DestStream);
428 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(SourceStream), Succeeded());
429 EXPECT_EQ(DestDataBytes, std::vector(
430 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
431
432 // Then read the string back out of the dest stream.
433 StringRef Result;
434 BinaryStreamReader DestReader(*DestStream);
435 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
436 EXPECT_EQ(Result, "Test Str");
437 }
438
439 TEST(MappedBlockStreamTest, TestWriteDiscontiguousStreamRef) {
440 std::vector DestDataBytes(10);
441 MutableArrayRef DestData(DestDataBytes);
442 const uint32_t DestBlocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
443
444 std::vector SrcDataBytes(10);
445 MutableArrayRef SrcData(SrcDataBytes);
446 const uint32_t SrcBlocks[] = {1, 0, 6, 3, 4, 5, 2, 7, 8, 9};
447
448 DiscontiguousStream DestF(DestBlocks, DestData);
449 DiscontiguousStream SrcF(SrcBlocks, SrcData);
450
451 auto Dest = WritableMappedBlockStream::createStream(
452 DestF.block_size(), DestF.layout(), DestF, DestF.Allocator);
453 auto Src = WritableMappedBlockStream::createStream(
454 SrcF.block_size(), SrcF.layout(), SrcF, SrcF.Allocator);
455
456 // First write "Test Str" into the source stream.
457 BinaryStreamWriter SourceWriter(*Src);
458 EXPECT_THAT_ERROR(SourceWriter.writeCString("Test Str"), Succeeded());
459 EXPECT_EQ(SrcDataBytes, std::vector(
460 {'e', 'T', 't', 't', ' ', 'S', 's', 'r', 0, 0}));
461
462 // Then write the source stream into the dest stream.
463 BinaryStreamWriter DestWriter(*Dest);
464 EXPECT_THAT_ERROR(DestWriter.writeStreamRef(*Src), Succeeded());
465 EXPECT_EQ(DestDataBytes, std::vector(
466 {'s', 'e', 'T', ' ', 'S', 't', 't', 'r', 0, 0}));
467
468 // Then read the string back out of the dest stream.
469 StringRef Result;
470 BinaryStreamReader DestReader(*Dest);
471 EXPECT_THAT_ERROR(DestReader.readCString(Result), Succeeded());
472 EXPECT_EQ(Result, "Test Str");
473 }
474
475 TEST(MappedBlockStreamTest, DataLivesAfterStreamDestruction) {
476 std::vector DataBytes(10);
477 MutableArrayRef Data(DataBytes);
478 const uint32_t Blocks[] = {2, 1, 0, 6, 3, 4, 5, 7, 9, 8};
479
480 StringRef Str[] = {"Zero Str", ""};
481
482 DiscontiguousStream F(Blocks, Data);
483 {
484 auto S = WritableMappedBlockStream::createStream(F.block_size(), F.layout(),
485 F, F.Allocator);
486
487 BinaryStreamReader Reader(*S);
488 BinaryStreamWriter Writer(*S);
489 ::memset(DataBytes.data(), 0, 10);
490 EXPECT_THAT_ERROR(Writer.writeCString(Str[0]), Succeeded());
491 EXPECT_THAT_ERROR(Reader.readCString(Str[1]), Succeeded());
492 EXPECT_EQ(Str[0], Str[1]);
493 }
494
495 EXPECT_EQ(Str[0], Str[1]);
496 }
497
498 } // end anonymous namespace