Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 1 | //===- MappedBlockStream.cpp - Reads stream data from a PDBFile -----------===// |
| 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/PDB/Raw/MappedBlockStream.h" |
| 11 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 15 | using namespace llvm::pdb; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 16 | |
| 17 | MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const PDBFile &File) : Pdb(File) { |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 18 | if (StreamIdx >= Pdb.getNumStreams()) { |
| 19 | StreamLength = 0; |
| 20 | } else { |
| 21 | StreamLength = Pdb.getStreamByteSize(StreamIdx); |
| 22 | BlockList = Pdb.getStreamBlockList(StreamIdx); |
| 23 | } |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 26 | Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, |
| 27 | ArrayRef<uint8_t> &Buffer) const { |
| 28 | // Make sure we aren't trying to read beyond the end of the stream. |
Zachary Turner | 7dd4259 | 2016-05-27 20:17:33 +0000 | [diff] [blame^] | 29 | if (Size > StreamLength) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 30 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 7dd4259 | 2016-05-27 20:17:33 +0000 | [diff] [blame^] | 31 | if (Offset > StreamLength - Size) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 32 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
| 33 | |
| 34 | if (tryReadContiguously(Offset, Size, Buffer)) |
| 35 | return Error::success(); |
| 36 | |
| 37 | auto CacheIter = CacheMap.find(Offset); |
| 38 | if (CacheIter != CacheMap.end()) { |
| 39 | // In a more general solution, we would need to guarantee that the |
| 40 | // cached allocation is at least the requested size. In practice, since |
| 41 | // these are CodeView / PDB records, we know they are always formatted |
| 42 | // the same way and never change, so we should never be requesting two |
| 43 | // allocations from the same address with different sizes. |
| 44 | Buffer = ArrayRef<uint8_t>(CacheIter->second, Size); |
| 45 | return Error::success(); |
| 46 | } |
| 47 | |
| 48 | // Otherwise allocate a large enough buffer in the pool, memcpy the data |
| 49 | // into it, and return an ArrayRef to that. |
| 50 | uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size); |
| 51 | |
| 52 | if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) |
| 53 | return EC; |
| 54 | CacheMap.insert(std::make_pair(Offset, WriteBuffer)); |
| 55 | Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); |
| 56 | return Error::success(); |
| 57 | } |
| 58 | |
| 59 | bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, |
| 60 | ArrayRef<uint8_t> &Buffer) const { |
| 61 | // Attempt to fulfill the request with a reference directly into the stream. |
| 62 | // This can work even if the request crosses a block boundary, provided that |
| 63 | // all subsequent blocks are contiguous. For example, a 10k read with a 4k |
| 64 | // block size can be filled with a reference if, from the starting offset, |
| 65 | // 3 blocks in a row are contiguous. |
| 66 | uint32_t BlockNum = Offset / Pdb.getBlockSize(); |
| 67 | uint32_t OffsetInBlock = Offset % Pdb.getBlockSize(); |
| 68 | uint32_t BytesFromFirstBlock = |
| 69 | std::min(Size, Pdb.getBlockSize() - OffsetInBlock); |
| 70 | uint32_t NumAdditionalBlocks = |
| 71 | llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) / |
| 72 | Pdb.getBlockSize(); |
| 73 | |
| 74 | uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; |
| 75 | uint32_t E = BlockList[BlockNum]; |
| 76 | for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { |
| 77 | if (BlockList[I + BlockNum] != E) |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | uint32_t FirstBlockAddr = BlockList[BlockNum]; |
| 82 | StringRef Str = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize()); |
| 83 | Str = Str.drop_front(OffsetInBlock); |
| 84 | Buffer = |
| 85 | ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Str.data()), Size); |
| 86 | return true; |
| 87 | } |
| 88 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 89 | Error MappedBlockStream::readBytes(uint32_t Offset, |
| 90 | MutableArrayRef<uint8_t> Buffer) const { |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 91 | uint32_t BlockNum = Offset / Pdb.getBlockSize(); |
| 92 | uint32_t OffsetInBlock = Offset % Pdb.getBlockSize(); |
| 93 | |
| 94 | // Make sure we aren't trying to read beyond the end of the stream. |
| 95 | if (Buffer.size() > StreamLength) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 96 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 97 | if (Offset > StreamLength - Buffer.size()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 98 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 99 | |
| 100 | uint32_t BytesLeft = Buffer.size(); |
| 101 | uint32_t BytesWritten = 0; |
| 102 | uint8_t *WriteBuffer = Buffer.data(); |
| 103 | while (BytesLeft > 0) { |
| 104 | uint32_t StreamBlockAddr = BlockList[BlockNum]; |
| 105 | |
| 106 | StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize()); |
| 107 | |
| 108 | const char *ChunkStart = Data.data() + OffsetInBlock; |
| 109 | uint32_t BytesInChunk = |
| 110 | std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock); |
| 111 | ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); |
| 112 | |
| 113 | BytesWritten += BytesInChunk; |
| 114 | BytesLeft -= BytesInChunk; |
| 115 | ++BlockNum; |
| 116 | OffsetInBlock = 0; |
| 117 | } |
| 118 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 119 | return Error::success(); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 120 | |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 121 | } |