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