Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 1 | //===- StreamingMemoryObject.cpp - Streamable data interface -------------===// |
| 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/Support/StreamingMemoryObject.h" |
| 11 | #include <cassert> |
| 12 | #include <cstddef> |
| 13 | #include <cstring> |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class RawMemoryObject : public MemoryObject { |
| 19 | public: |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 20 | RawMemoryObject(const unsigned char *Start, const unsigned char *End) |
| 21 | : FirstChar(Start), LastChar(End) { |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 22 | assert(LastChar >= FirstChar && "Invalid start/end range"); |
| 23 | } |
| 24 | |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 25 | uint64_t getExtent() const override { return LastChar - FirstChar; } |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 26 | uint64_t readBytes(uint8_t *Buf, uint64_t Size, |
| 27 | uint64_t Address) const override; |
| 28 | const uint8_t *getPointer(uint64_t address, uint64_t size) const override; |
| 29 | bool isValidAddress(uint64_t address) const override { |
| 30 | return validAddress(address); |
| 31 | } |
| 32 | |
| 33 | private: |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 34 | const uint8_t *const FirstChar; |
| 35 | const uint8_t *const LastChar; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 36 | |
| 37 | // These are implemented as inline functions here to avoid multiple virtual |
| 38 | // calls per public function |
| 39 | bool validAddress(uint64_t address) const { |
| 40 | return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar; |
| 41 | } |
| 42 | |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 43 | RawMemoryObject(const RawMemoryObject &) = delete; |
| 44 | void operator=(const RawMemoryObject &) = delete; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size, |
| 48 | uint64_t Address) const { |
| 49 | uint64_t BufferSize = LastChar - FirstChar; |
| 50 | if (Address >= BufferSize) |
| 51 | return 0; |
| 52 | |
| 53 | uint64_t End = Address + Size; |
| 54 | if (End > BufferSize) |
| 55 | End = BufferSize; |
| 56 | |
| 57 | assert(static_cast<int64_t>(End - Address) >= 0); |
| 58 | Size = End - Address; |
| 59 | memcpy(Buf, Address + FirstChar, Size); |
| 60 | return Size; |
| 61 | } |
| 62 | |
| 63 | const uint8_t *RawMemoryObject::getPointer(uint64_t address, |
| 64 | uint64_t size) const { |
| 65 | return FirstChar + address; |
| 66 | } |
| 67 | } // anonymous namespace |
| 68 | |
| 69 | namespace llvm { |
| 70 | // If the bitcode has a header, then its size is known, and we don't have to |
| 71 | // block until we actually want to read it. |
| 72 | bool StreamingMemoryObject::isValidAddress(uint64_t address) const { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 73 | if (ObjectSize && address < ObjectSize) |
| 74 | return true; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 75 | return fetchToPos(address); |
| 76 | } |
| 77 | |
| 78 | uint64_t StreamingMemoryObject::getExtent() const { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 79 | if (ObjectSize) |
| 80 | return ObjectSize; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 81 | size_t pos = BytesRead + kChunkSize; |
| 82 | // keep fetching until we run out of bytes |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 83 | while (fetchToPos(pos)) |
| 84 | pos += kChunkSize; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 85 | return ObjectSize; |
| 86 | } |
| 87 | |
| 88 | uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size, |
| 89 | uint64_t Address) const { |
| 90 | fetchToPos(Address + Size - 1); |
| 91 | // Note: For wrapped bitcode files will set ObjectSize after the |
| 92 | // first call to fetchToPos. In such cases, ObjectSize can be |
| 93 | // smaller than BytesRead. |
| 94 | size_t MaxAddress = |
| 95 | (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead; |
| 96 | if (Address >= MaxAddress) |
| 97 | return 0; |
| 98 | |
| 99 | uint64_t End = Address + Size; |
| 100 | if (End > MaxAddress) |
| 101 | End = MaxAddress; |
| 102 | assert(End >= Address); |
| 103 | Size = End - Address; |
| 104 | memcpy(Buf, &Bytes[Address + BytesSkipped], Size); |
| 105 | return Size; |
| 106 | } |
| 107 | |
| 108 | const uint8_t *StreamingMemoryObject::getPointer(uint64_t Address, |
| 109 | uint64_t Size) const { |
| 110 | fetchToPos(Address + Size - 1); |
| 111 | return &Bytes[Address + BytesSkipped]; |
| 112 | } |
| 113 | |
| 114 | bool StreamingMemoryObject::dropLeadingBytes(size_t s) { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 115 | if (BytesRead < s) |
| 116 | return true; |
Jim Stichnoth | d722ff2 | 2016-12-26 20:10:07 -0800 | [diff] [blame] | 117 | BytesSkipped = s; |
| 118 | BytesRead -= s; |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | void StreamingMemoryObject::setKnownObjectSize(size_t size) { |
| 123 | ObjectSize = size; |
| 124 | Bytes.reserve(size); |
| 125 | if (ObjectSize <= BytesRead) |
| 126 | EOFReached = true; |
| 127 | } |
| 128 | |
| 129 | MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start, |
| 130 | const unsigned char *End) { |
| 131 | return new RawMemoryObject(Start, End); |
| 132 | } |
| 133 | |
| 134 | StreamingMemoryObject::StreamingMemoryObject( |
| 135 | std::unique_ptr<DataStreamer> Streamer) |
| 136 | : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0), |
| 137 | BytesSkipped(0), ObjectSize(0), EOFReached(false) { |
| 138 | BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize); |
| 139 | } |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 140 | } // namespace llvm |