LLVM API Documentation

StreamableMemoryObject.h
Go to the documentation of this file.
00001 //===- StreamableMemoryObject.h - Streamable data interface -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 
00011 #ifndef LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
00012 #define LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
00013 
00014 #include "llvm/Support/Compiler.h"
00015 #include "llvm/Support/DataStream.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include "llvm/Support/MemoryObject.h"
00018 #include <cassert>
00019 #include <memory>
00020 #include <vector>
00021 
00022 namespace llvm {
00023 
00024 /// StreamableMemoryObject - Interface to data which might be streamed.
00025 /// Streamability has 2 important implications/restrictions. First, the data
00026 /// might not yet exist in memory when the request is made. This just means
00027 /// that readByte/readBytes might have to block or do some work to get it.
00028 /// More significantly, the exact size of the object might not be known until
00029 /// it has all been fetched. This means that to return the right result,
00030 /// getExtent must also wait for all the data to arrive; therefore it should
00031 /// not be called on objects which are actually streamed (this would defeat
00032 /// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
00033 /// used to test addresses without knowing the exact size of the stream.
00034 /// Finally, getPointer can be used instead of readBytes to avoid extra copying.
00035 class StreamableMemoryObject : public MemoryObject {
00036  public:
00037   /// Destructor      - Override as necessary.
00038   virtual ~StreamableMemoryObject();
00039 
00040   /// getBase         - Returns the lowest valid address in the region.
00041   ///
00042   /// @result         - The lowest valid address.
00043   uint64_t getBase() const override = 0;
00044 
00045   /// getExtent       - Returns the size of the region in bytes.  (The region is
00046   ///                   contiguous, so the highest valid address of the region
00047   ///                   is getBase() + getExtent() - 1).
00048   ///                   May block until all bytes in the stream have been read
00049   ///
00050   /// @result         - The size of the region.
00051   uint64_t getExtent() const override = 0;
00052 
00053   /// readByte        - Tries to read a single byte from the region.
00054   ///                   May block until (address - base) bytes have been read
00055   /// @param address  - The address of the byte, in the same space as getBase().
00056   /// @param ptr      - A pointer to a byte to be filled in.  Must be non-NULL.
00057   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
00058   ///                   bounds violation or an implementation-specific error.
00059   int readByte(uint64_t address, uint8_t *ptr) const override = 0;
00060 
00061   /// readBytes       - Tries to read a contiguous range of bytes from the
00062   ///                   region, up to the end of the region.
00063   ///                   May block until (address - base + size) bytes have
00064   ///                   been read. Additionally, StreamableMemoryObjects will
00065   ///                   not do partial reads - if size bytes cannot be read,
00066   ///                   readBytes will fail.
00067   ///
00068   /// @param address  - The address of the first byte, in the same space as
00069   ///                   getBase().
00070   /// @param size     - The number of bytes to copy.
00071   /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL
00072   ///                   and large enough to hold size bytes.
00073   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
00074   ///                   bounds violation or an implementation-specific error.
00075   int readBytes(uint64_t address, uint64_t size,
00076                 uint8_t *buf) const override = 0;
00077 
00078   /// getPointer  - Ensures that the requested data is in memory, and returns
00079   ///               A pointer to it. More efficient than using readBytes if the
00080   ///               data is already in memory.
00081   ///               May block until (address - base + size) bytes have been read
00082   /// @param address - address of the byte, in the same space as getBase()
00083   /// @param size    - amount of data that must be available on return
00084   /// @result        - valid pointer to the requested data
00085   virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
00086 
00087   /// isValidAddress - Returns true if the address is within the object
00088   ///                  (i.e. between base and base + extent - 1 inclusive)
00089   ///                  May block until (address - base) bytes have been read
00090   /// @param address - address of the byte, in the same space as getBase()
00091   /// @result        - true if the address may be read with readByte()
00092   virtual bool isValidAddress(uint64_t address) const = 0;
00093 
00094   /// isObjectEnd    - Returns true if the address is one past the end of the
00095   ///                  object (i.e. if it is equal to base + extent)
00096   ///                  May block until (address - base) bytes have been read
00097   /// @param address - address of the byte, in the same space as getBase()
00098   /// @result        - true if the address is equal to base + extent
00099   virtual bool isObjectEnd(uint64_t address) const = 0;
00100 };
00101 
00102 /// StreamingMemoryObject - interface to data which is actually streamed from
00103 /// a DataStreamer. In addition to inherited members, it has the
00104 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
00105 /// to non-streamed objects.
00106 class StreamingMemoryObject : public StreamableMemoryObject {
00107 public:
00108   StreamingMemoryObject(DataStreamer *streamer);
00109   uint64_t getBase() const override { return 0; }
00110   uint64_t getExtent() const override;
00111   int readByte(uint64_t address, uint8_t *ptr) const override;
00112   int readBytes(uint64_t address, uint64_t size,
00113                 uint8_t *buf) const override;
00114   const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
00115     // This could be fixed by ensuring the bytes are fetched and making a copy,
00116     // requiring that the bitcode size be known, or otherwise ensuring that
00117     // the memory doesn't go away/get reallocated, but it's
00118     // not currently necessary. Users that need the pointer don't stream.
00119     llvm_unreachable("getPointer in streaming memory objects not allowed");
00120     return nullptr;
00121   }
00122   bool isValidAddress(uint64_t address) const override;
00123   bool isObjectEnd(uint64_t address) const override;
00124 
00125   /// Drop s bytes from the front of the stream, pushing the positions of the
00126   /// remaining bytes down by s. This is used to skip past the bitcode header,
00127   /// since we don't know a priori if it's present, and we can't put bytes
00128   /// back into the stream once we've read them.
00129   bool dropLeadingBytes(size_t s);
00130 
00131   /// If the data object size is known in advance, many of the operations can
00132   /// be made more efficient, so this method should be called before reading
00133   /// starts (although it can be called anytime).
00134   void setKnownObjectSize(size_t size);
00135 
00136 private:
00137   const static uint32_t kChunkSize = 4096 * 4;
00138   mutable std::vector<unsigned char> Bytes;
00139   std::unique_ptr<DataStreamer> Streamer;
00140   mutable size_t BytesRead;   // Bytes read from stream
00141   size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
00142   mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
00143   mutable bool EOFReached;
00144 
00145   // Fetch enough bytes such that Pos can be read or EOF is reached
00146   // (i.e. BytesRead > Pos). Return true if Pos can be read.
00147   // Unlike most of the functions in BitcodeReader, returns true on success.
00148   // Most of the requests will be small, but we fetch at kChunkSize bytes
00149   // at a time to avoid making too many potentially expensive GetBytes calls
00150   bool fetchToPos(size_t Pos) const {
00151     if (EOFReached) return Pos < ObjectSize;
00152     while (Pos >= BytesRead) {
00153       Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
00154       size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
00155                                         kChunkSize);
00156       BytesRead += bytes;
00157       if (bytes < kChunkSize) {
00158         assert((!ObjectSize || BytesRead >= Pos) &&
00159                "Unexpected short read fetching bitcode");
00160         if (BytesRead <= Pos) { // reached EOF/ran out of bytes
00161           ObjectSize = BytesRead;
00162           EOFReached = true;
00163           return false;
00164         }
00165       }
00166     }
00167     return true;
00168   }
00169 
00170   StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
00171   void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
00172 };
00173 
00174 StreamableMemoryObject *getNonStreamedMemoryObject(
00175     const unsigned char *Start, const unsigned char *End);
00176 
00177 }
00178 #endif  // STREAMABLEMEMORYOBJECT_H_