120
|
1 //===- MappedBlockStream.cpp - Reads stream data from an MSF file ---------===//
|
|
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 "llvm/DebugInfo/MSF/MappedBlockStream.h"
|
|
11
|
|
12 #include "llvm/DebugInfo/MSF/IMSFFile.h"
|
|
13 #include "llvm/DebugInfo/MSF/MSFCommon.h"
|
|
14 #include "llvm/DebugInfo/MSF/MSFError.h"
|
|
15 #include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
|
|
16
|
|
17 using namespace llvm;
|
|
18 using namespace llvm::msf;
|
|
19
|
|
20 namespace {
|
|
21 template <typename Base> class MappedBlockStreamImpl : public Base {
|
|
22 public:
|
|
23 template <typename... Args>
|
|
24 MappedBlockStreamImpl(Args &&... Params)
|
|
25 : Base(std::forward<Args>(Params)...) {}
|
|
26 };
|
|
27 }
|
|
28
|
|
29 static void initializeFpmStreamLayout(const MSFLayout &Layout,
|
|
30 MSFStreamLayout &FpmLayout) {
|
|
31 uint32_t NumFpmIntervals = msf::getNumFpmIntervals(Layout);
|
|
32 support::ulittle32_t FpmBlock = Layout.SB->FreeBlockMapBlock;
|
|
33 assert(FpmBlock == 1 || FpmBlock == 2);
|
|
34 while (NumFpmIntervals > 0) {
|
|
35 FpmLayout.Blocks.push_back(FpmBlock);
|
|
36 FpmBlock += msf::getFpmIntervalLength(Layout);
|
|
37 --NumFpmIntervals;
|
|
38 }
|
|
39 FpmLayout.Length = msf::getFullFpmByteSize(Layout);
|
|
40 }
|
|
41
|
|
42 typedef std::pair<uint32_t, uint32_t> Interval;
|
|
43 static Interval intersect(const Interval &I1, const Interval &I2) {
|
|
44 return std::make_pair(std::max(I1.first, I2.first),
|
|
45 std::min(I1.second, I2.second));
|
|
46 }
|
|
47
|
|
48 MappedBlockStream::MappedBlockStream(uint32_t BlockSize, uint32_t NumBlocks,
|
|
49 const MSFStreamLayout &Layout,
|
|
50 const ReadableStream &MsfData)
|
|
51 : BlockSize(BlockSize), NumBlocks(NumBlocks), StreamLayout(Layout),
|
|
52 MsfData(MsfData) {}
|
|
53
|
|
54 std::unique_ptr<MappedBlockStream>
|
|
55 MappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
|
|
56 const MSFStreamLayout &Layout,
|
|
57 const ReadableStream &MsfData) {
|
|
58 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
|
|
59 BlockSize, NumBlocks, Layout, MsfData);
|
|
60 }
|
|
61
|
|
62 std::unique_ptr<MappedBlockStream>
|
|
63 MappedBlockStream::createIndexedStream(const MSFLayout &Layout,
|
|
64 const ReadableStream &MsfData,
|
|
65 uint32_t StreamIndex) {
|
|
66 MSFStreamLayout SL;
|
|
67 SL.Blocks = Layout.StreamMap[StreamIndex];
|
|
68 SL.Length = Layout.StreamSizes[StreamIndex];
|
|
69 return llvm::make_unique<MappedBlockStreamImpl<MappedBlockStream>>(
|
|
70 Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
71 }
|
|
72
|
|
73 std::unique_ptr<MappedBlockStream>
|
|
74 MappedBlockStream::createDirectoryStream(const MSFLayout &Layout,
|
|
75 const ReadableStream &MsfData) {
|
|
76 MSFStreamLayout SL;
|
|
77 SL.Blocks = Layout.DirectoryBlocks;
|
|
78 SL.Length = Layout.SB->NumDirectoryBytes;
|
|
79 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
80 }
|
|
81
|
|
82 std::unique_ptr<MappedBlockStream>
|
|
83 MappedBlockStream::createFpmStream(const MSFLayout &Layout,
|
|
84 const ReadableStream &MsfData) {
|
|
85 MSFStreamLayout SL;
|
|
86 initializeFpmStreamLayout(Layout, SL);
|
|
87 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
88 }
|
|
89
|
|
90 Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
|
|
91 ArrayRef<uint8_t> &Buffer) const {
|
|
92 // Make sure we aren't trying to read beyond the end of the stream.
|
|
93 if (Size > StreamLayout.Length)
|
|
94 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
95 if (Offset > StreamLayout.Length - Size)
|
|
96 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
97
|
|
98 if (tryReadContiguously(Offset, Size, Buffer))
|
|
99 return Error::success();
|
|
100
|
|
101 auto CacheIter = CacheMap.find(Offset);
|
|
102 if (CacheIter != CacheMap.end()) {
|
|
103 // Try to find an alloc that was large enough for this request.
|
|
104 for (auto &Entry : CacheIter->second) {
|
|
105 if (Entry.size() >= Size) {
|
|
106 Buffer = Entry.slice(0, Size);
|
|
107 return Error::success();
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111
|
|
112 // We couldn't find a buffer that started at the correct offset (the most
|
|
113 // common scenario). Try to see if there is a buffer that starts at some
|
|
114 // other offset but overlaps the desired range.
|
|
115 for (auto &CacheItem : CacheMap) {
|
|
116 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
|
|
117
|
|
118 // We already checked this one on the fast path above.
|
|
119 if (CacheItem.first == Offset)
|
|
120 continue;
|
|
121 // If the initial extent of the cached item is beyond the ending extent
|
|
122 // of the request, there is no overlap.
|
|
123 if (CacheItem.first >= Offset + Size)
|
|
124 continue;
|
|
125
|
|
126 // We really only have to check the last item in the list, since we append
|
|
127 // in order of increasing length.
|
|
128 if (CacheItem.second.empty())
|
|
129 continue;
|
|
130
|
|
131 auto CachedAlloc = CacheItem.second.back();
|
|
132 // If the initial extent of the request is beyond the ending extent of
|
|
133 // the cached item, there is no overlap.
|
|
134 Interval CachedExtent =
|
|
135 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
|
|
136 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
|
|
137 continue;
|
|
138
|
|
139 Interval Intersection = intersect(CachedExtent, RequestExtent);
|
|
140 // Only use this if the entire request extent is contained in the cached
|
|
141 // extent.
|
|
142 if (Intersection != RequestExtent)
|
|
143 continue;
|
|
144
|
|
145 uint32_t CacheRangeOffset =
|
|
146 AbsoluteDifference(CachedExtent.first, Intersection.first);
|
|
147 Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
|
|
148 return Error::success();
|
|
149 }
|
|
150
|
|
151 // Otherwise allocate a large enough buffer in the pool, memcpy the data
|
|
152 // into it, and return an ArrayRef to that. Do not touch existing pool
|
|
153 // allocations, as existing clients may be holding a pointer which must
|
|
154 // not be invalidated.
|
|
155 uint8_t *WriteBuffer = static_cast<uint8_t *>(Pool.Allocate(Size, 8));
|
|
156 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
|
|
157 return EC;
|
|
158
|
|
159 if (CacheIter != CacheMap.end()) {
|
|
160 CacheIter->second.emplace_back(WriteBuffer, Size);
|
|
161 } else {
|
|
162 std::vector<CacheEntry> List;
|
|
163 List.emplace_back(WriteBuffer, Size);
|
|
164 CacheMap.insert(std::make_pair(Offset, List));
|
|
165 }
|
|
166 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
|
|
167 return Error::success();
|
|
168 }
|
|
169
|
|
170 Error MappedBlockStream::readLongestContiguousChunk(
|
|
171 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
|
|
172 // Make sure we aren't trying to read beyond the end of the stream.
|
|
173 if (Offset >= StreamLayout.Length)
|
|
174 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
175 uint32_t First = Offset / BlockSize;
|
|
176 uint32_t Last = First;
|
|
177
|
|
178 while (Last < NumBlocks - 1) {
|
|
179 if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1)
|
|
180 break;
|
|
181 ++Last;
|
|
182 }
|
|
183
|
|
184 uint32_t OffsetInFirstBlock = Offset % BlockSize;
|
|
185 uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock;
|
|
186 uint32_t BlockSpan = Last - First + 1;
|
|
187 uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize;
|
|
188
|
|
189 ArrayRef<uint8_t> BlockData;
|
|
190 uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize);
|
|
191 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData))
|
|
192 return EC;
|
|
193
|
|
194 BlockData = BlockData.drop_front(OffsetInFirstBlock);
|
|
195 Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan);
|
|
196 return Error::success();
|
|
197 }
|
|
198
|
|
199 uint32_t MappedBlockStream::getLength() const { return StreamLayout.Length; }
|
|
200
|
|
201 bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
|
|
202 ArrayRef<uint8_t> &Buffer) const {
|
|
203 if (Size == 0) {
|
|
204 Buffer = ArrayRef<uint8_t>();
|
|
205 return true;
|
|
206 }
|
|
207 // Attempt to fulfill the request with a reference directly into the stream.
|
|
208 // This can work even if the request crosses a block boundary, provided that
|
|
209 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
|
|
210 // block size can be filled with a reference if, from the starting offset,
|
|
211 // 3 blocks in a row are contiguous.
|
|
212 uint32_t BlockNum = Offset / BlockSize;
|
|
213 uint32_t OffsetInBlock = Offset % BlockSize;
|
|
214 uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock);
|
|
215 uint32_t NumAdditionalBlocks =
|
|
216 llvm::alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize;
|
|
217
|
|
218 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
|
|
219 uint32_t E = StreamLayout.Blocks[BlockNum];
|
|
220 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
|
|
221 if (StreamLayout.Blocks[I + BlockNum] != E)
|
|
222 return false;
|
|
223 }
|
|
224
|
|
225 // Read out the entire block where the requested offset starts. Then drop
|
|
226 // bytes from the beginning so that the actual starting byte lines up with
|
|
227 // the requested starting byte. Then, since we know this is a contiguous
|
|
228 // cross-block span, explicitly resize the ArrayRef to cover the entire
|
|
229 // request length.
|
|
230 ArrayRef<uint8_t> BlockData;
|
|
231 uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum];
|
|
232 uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize);
|
|
233 if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) {
|
|
234 consumeError(std::move(EC));
|
|
235 return false;
|
|
236 }
|
|
237 BlockData = BlockData.drop_front(OffsetInBlock);
|
|
238 Buffer = ArrayRef<uint8_t>(BlockData.data(), Size);
|
|
239 return true;
|
|
240 }
|
|
241
|
|
242 Error MappedBlockStream::readBytes(uint32_t Offset,
|
|
243 MutableArrayRef<uint8_t> Buffer) const {
|
|
244 uint32_t BlockNum = Offset / BlockSize;
|
|
245 uint32_t OffsetInBlock = Offset % BlockSize;
|
|
246
|
|
247 // Make sure we aren't trying to read beyond the end of the stream.
|
|
248 if (Buffer.size() > StreamLayout.Length)
|
|
249 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
250 if (Offset > StreamLayout.Length - Buffer.size())
|
|
251 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
252
|
|
253 uint32_t BytesLeft = Buffer.size();
|
|
254 uint32_t BytesWritten = 0;
|
|
255 uint8_t *WriteBuffer = Buffer.data();
|
|
256 while (BytesLeft > 0) {
|
|
257 uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum];
|
|
258
|
|
259 ArrayRef<uint8_t> BlockData;
|
|
260 uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize);
|
|
261 if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData))
|
|
262 return EC;
|
|
263
|
|
264 const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock;
|
|
265 uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock);
|
|
266 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
|
|
267
|
|
268 BytesWritten += BytesInChunk;
|
|
269 BytesLeft -= BytesInChunk;
|
|
270 ++BlockNum;
|
|
271 OffsetInBlock = 0;
|
|
272 }
|
|
273
|
|
274 return Error::success();
|
|
275 }
|
|
276
|
|
277 uint32_t MappedBlockStream::getNumBytesCopied() const {
|
|
278 return static_cast<uint32_t>(Pool.getBytesAllocated());
|
|
279 }
|
|
280
|
|
281 void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); }
|
|
282
|
|
283 void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset,
|
|
284 ArrayRef<uint8_t> Data) const {
|
|
285 // If this write overlapped a read which previously came from the pool,
|
|
286 // someone may still be holding a pointer to that alloc which is now invalid.
|
|
287 // Compute the overlapping range and update the cache entry, so any
|
|
288 // outstanding buffers are automatically updated.
|
|
289 for (const auto &MapEntry : CacheMap) {
|
|
290 // If the end of the written extent precedes the beginning of the cached
|
|
291 // extent, ignore this map entry.
|
|
292 if (Offset + Data.size() < MapEntry.first)
|
|
293 continue;
|
|
294 for (const auto &Alloc : MapEntry.second) {
|
|
295 // If the end of the cached extent precedes the beginning of the written
|
|
296 // extent, ignore this alloc.
|
|
297 if (MapEntry.first + Alloc.size() < Offset)
|
|
298 continue;
|
|
299
|
|
300 // If we get here, they are guaranteed to overlap.
|
|
301 Interval WriteInterval = std::make_pair(Offset, Offset + Data.size());
|
|
302 Interval CachedInterval =
|
|
303 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
|
|
304 // If they overlap, we need to write the new data into the overlapping
|
|
305 // range.
|
|
306 auto Intersection = intersect(WriteInterval, CachedInterval);
|
|
307 assert(Intersection.first <= Intersection.second);
|
|
308
|
|
309 uint32_t Length = Intersection.second - Intersection.first;
|
|
310 uint32_t SrcOffset =
|
|
311 AbsoluteDifference(WriteInterval.first, Intersection.first);
|
|
312 uint32_t DestOffset =
|
|
313 AbsoluteDifference(CachedInterval.first, Intersection.first);
|
|
314 ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length);
|
|
315 }
|
|
316 }
|
|
317 }
|
|
318
|
|
319 WritableMappedBlockStream::WritableMappedBlockStream(
|
|
320 uint32_t BlockSize, uint32_t NumBlocks, const MSFStreamLayout &Layout,
|
|
321 const WritableStream &MsfData)
|
|
322 : ReadInterface(BlockSize, NumBlocks, Layout, MsfData),
|
|
323 WriteInterface(MsfData) {}
|
|
324
|
|
325 std::unique_ptr<WritableMappedBlockStream>
|
|
326 WritableMappedBlockStream::createStream(uint32_t BlockSize, uint32_t NumBlocks,
|
|
327 const MSFStreamLayout &Layout,
|
|
328 const WritableStream &MsfData) {
|
|
329 return llvm::make_unique<MappedBlockStreamImpl<WritableMappedBlockStream>>(
|
|
330 BlockSize, NumBlocks, Layout, MsfData);
|
|
331 }
|
|
332
|
|
333 std::unique_ptr<WritableMappedBlockStream>
|
|
334 WritableMappedBlockStream::createIndexedStream(const MSFLayout &Layout,
|
|
335 const WritableStream &MsfData,
|
|
336 uint32_t StreamIndex) {
|
|
337 MSFStreamLayout SL;
|
|
338 SL.Blocks = Layout.StreamMap[StreamIndex];
|
|
339 SL.Length = Layout.StreamSizes[StreamIndex];
|
|
340 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
341 }
|
|
342
|
|
343 std::unique_ptr<WritableMappedBlockStream>
|
|
344 WritableMappedBlockStream::createDirectoryStream(
|
|
345 const MSFLayout &Layout, const WritableStream &MsfData) {
|
|
346 MSFStreamLayout SL;
|
|
347 SL.Blocks = Layout.DirectoryBlocks;
|
|
348 SL.Length = Layout.SB->NumDirectoryBytes;
|
|
349 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
350 }
|
|
351
|
|
352 std::unique_ptr<WritableMappedBlockStream>
|
|
353 WritableMappedBlockStream::createFpmStream(const MSFLayout &Layout,
|
|
354 const WritableStream &MsfData) {
|
|
355 MSFStreamLayout SL;
|
|
356 initializeFpmStreamLayout(Layout, SL);
|
|
357 return createStream(Layout.SB->BlockSize, Layout.SB->NumBlocks, SL, MsfData);
|
|
358 }
|
|
359
|
|
360 Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
|
|
361 ArrayRef<uint8_t> &Buffer) const {
|
|
362 return ReadInterface.readBytes(Offset, Size, Buffer);
|
|
363 }
|
|
364
|
|
365 Error WritableMappedBlockStream::readLongestContiguousChunk(
|
|
366 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
|
|
367 return ReadInterface.readLongestContiguousChunk(Offset, Buffer);
|
|
368 }
|
|
369
|
|
370 uint32_t WritableMappedBlockStream::getLength() const {
|
|
371 return ReadInterface.getLength();
|
|
372 }
|
|
373
|
|
374 Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
|
|
375 ArrayRef<uint8_t> Buffer) const {
|
|
376 // Make sure we aren't trying to write beyond the end of the stream.
|
|
377 if (Buffer.size() > getStreamLength())
|
|
378 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
379
|
|
380 if (Offset > getStreamLayout().Length - Buffer.size())
|
|
381 return make_error<MSFError>(msf_error_code::insufficient_buffer);
|
|
382
|
|
383 uint32_t BlockNum = Offset / getBlockSize();
|
|
384 uint32_t OffsetInBlock = Offset % getBlockSize();
|
|
385
|
|
386 uint32_t BytesLeft = Buffer.size();
|
|
387 uint32_t BytesWritten = 0;
|
|
388 while (BytesLeft > 0) {
|
|
389 uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum];
|
|
390 uint32_t BytesToWriteInChunk =
|
|
391 std::min(BytesLeft, getBlockSize() - OffsetInBlock);
|
|
392
|
|
393 const uint8_t *Chunk = Buffer.data() + BytesWritten;
|
|
394 ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
|
|
395 uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize());
|
|
396 MsfOffset += OffsetInBlock;
|
|
397 if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData))
|
|
398 return EC;
|
|
399
|
|
400 BytesLeft -= BytesToWriteInChunk;
|
|
401 BytesWritten += BytesToWriteInChunk;
|
|
402 ++BlockNum;
|
|
403 OffsetInBlock = 0;
|
|
404 }
|
|
405
|
|
406 ReadInterface.fixCacheAfterWrite(Offset, Buffer);
|
|
407
|
|
408 return Error::success();
|
|
409 }
|
|
410
|
|
411 Error WritableMappedBlockStream::commit() const {
|
|
412 return WriteInterface.commit();
|
|
413 }
|