1//===- StreamableMemoryObject.h - Streamable data interface -----*- C++ -*-===//
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
11#ifndef LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
12#define LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
13
14#include "llvm/ADT/OwningPtr.h"
15#include "llvm/Support/Compiler.h"
16#include "llvm/Support/DataStream.h"
17#include "llvm/Support/MemoryObject.h"
18#include <vector>
19
20namespace llvm {
21
22/// StreamableMemoryObject - Interface to data which might be streamed.
23/// Streamability has 2 important implications/restrictions. First, the data
24/// might not yet exist in memory when the request is made. This just means
25/// that readByte/readBytes might have to block or do some work to get it.
26/// More significantly, the exact size of the object might not be known until
27/// it has all been fetched. This means that to return the right result,
28/// getExtent must also wait for all the data to arrive; therefore it should
29/// not be called on objects which are actually streamed (this would defeat
30/// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
31/// used to test addresses without knowing the exact size of the stream.
32/// Finally, getPointer can be used instead of readBytes to avoid extra copying.
33class StreamableMemoryObject : public MemoryObject {
34 public:
35  /// Destructor      - Override as necessary.
36  virtual ~StreamableMemoryObject();
37
38  /// getBase         - Returns the lowest valid address in the region.
39  ///
40  /// @result         - The lowest valid address.
41  virtual uint64_t getBase() const LLVM_OVERRIDE = 0;
42
43  /// getExtent       - Returns the size of the region in bytes.  (The region is
44  ///                   contiguous, so the highest valid address of the region
45  ///                   is getBase() + getExtent() - 1).
46  ///                   May block until all bytes in the stream have been read
47  ///
48  /// @result         - The size of the region.
49  virtual uint64_t getExtent() const LLVM_OVERRIDE = 0;
50
51  /// readByte        - Tries to read a single byte from the region.
52  ///                   May block until (address - base) bytes have been read
53  /// @param address  - The address of the byte, in the same space as getBase().
54  /// @param ptr      - A pointer to a byte to be filled in.  Must be non-NULL.
55  /// @result         - 0 if successful; -1 if not.  Failure may be due to a
56  ///                   bounds violation or an implementation-specific error.
57  virtual int readByte(uint64_t address, uint8_t *ptr) const LLVM_OVERRIDE = 0;
58
59  /// readBytes       - Tries to read a contiguous range of bytes from the
60  ///                   region, up to the end of the region.
61  ///                   May block until (address - base + size) bytes have
62  ///                   been read. Additionally, StreamableMemoryObjects will
63  ///                   not do partial reads - if size bytes cannot be read,
64  ///                   readBytes will fail.
65  ///
66  /// @param address  - The address of the first byte, in the same space as
67  ///                   getBase().
68  /// @param size     - The number of bytes to copy.
69  /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL
70  ///                   and large enough to hold size bytes.
71  /// @result         - 0 if successful; -1 if not.  Failure may be due to a
72  ///                   bounds violation or an implementation-specific error.
73  virtual int readBytes(uint64_t address,
74                        uint64_t size,
75                        uint8_t *buf) const LLVM_OVERRIDE = 0;
76
77  /// getPointer  - Ensures that the requested data is in memory, and returns
78  ///               A pointer to it. More efficient than using readBytes if the
79  ///               data is already in memory.
80  ///               May block until (address - base + size) bytes have been read
81  /// @param address - address of the byte, in the same space as getBase()
82  /// @param size    - amount of data that must be available on return
83  /// @result        - valid pointer to the requested data
84  virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
85
86  /// isValidAddress - Returns true if the address is within the object
87  ///                  (i.e. between base and base + extent - 1 inclusive)
88  ///                  May block until (address - base) bytes have been read
89  /// @param address - address of the byte, in the same space as getBase()
90  /// @result        - true if the address may be read with readByte()
91  virtual bool isValidAddress(uint64_t address) const = 0;
92
93  /// isObjectEnd    - Returns true if the address is one past the end of the
94  ///                  object (i.e. if it is equal to base + extent)
95  ///                  May block until (address - base) bytes have been read
96  /// @param address - address of the byte, in the same space as getBase()
97  /// @result        - true if the address is equal to base + extent
98  virtual bool isObjectEnd(uint64_t address) const = 0;
99};
100
101/// StreamingMemoryObject - interface to data which is actually streamed from
102/// a DataStreamer. In addition to inherited members, it has the
103/// dropLeadingBytes and setKnownObjectSize methods which are not applicable
104/// to non-streamed objects.
105class StreamingMemoryObject : public StreamableMemoryObject {
106public:
107  StreamingMemoryObject(DataStreamer *streamer);
108  virtual uint64_t getBase() const LLVM_OVERRIDE { return 0; }
109  virtual uint64_t getExtent() const LLVM_OVERRIDE;
110  virtual int readByte(uint64_t address, uint8_t *ptr) const LLVM_OVERRIDE;
111  virtual int readBytes(uint64_t address,
112                        uint64_t size,
113                        uint8_t *buf) const LLVM_OVERRIDE;
114  virtual const uint8_t *getPointer(uint64_t address,
115                                    uint64_t size) const LLVM_OVERRIDE {
116    // This could be fixed by ensuring the bytes are fetched and making a copy,
117    // requiring that the bitcode size be known, or otherwise ensuring that
118    // the memory doesn't go away/get reallocated, but it's
119    // not currently necessary. Users that need the pointer don't stream.
120    assert(0 && "getPointer in streaming memory objects not allowed");
121    return NULL;
122  }
123  virtual bool isValidAddress(uint64_t address) const LLVM_OVERRIDE;
124  virtual bool isObjectEnd(uint64_t address) const LLVM_OVERRIDE;
125
126  /// Drop s bytes from the front of the stream, pushing the positions of the
127  /// remaining bytes down by s. This is used to skip past the bitcode header,
128  /// since we don't know a priori if it's present, and we can't put bytes
129  /// back into the stream once we've read them.
130  bool dropLeadingBytes(size_t s);
131
132  /// If the data object size is known in advance, many of the operations can
133  /// be made more efficient, so this method should be called before reading
134  /// starts (although it can be called anytime).
135  void setKnownObjectSize(size_t size);
136
137private:
138  const static uint32_t kChunkSize = 4096 * 4;
139  mutable std::vector<unsigned char> Bytes;
140  OwningPtr<DataStreamer> Streamer;
141  mutable size_t BytesRead;   // Bytes read from stream
142  size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
143  mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
144  mutable bool EOFReached;
145
146  // Fetch enough bytes such that Pos can be read or EOF is reached
147  // (i.e. BytesRead > Pos). Return true if Pos can be read.
148  // Unlike most of the functions in BitcodeReader, returns true on success.
149  // Most of the requests will be small, but we fetch at kChunkSize bytes
150  // at a time to avoid making too many potentially expensive GetBytes calls
151  bool fetchToPos(size_t Pos) const {
152    if (EOFReached) return Pos < ObjectSize;
153    while (Pos >= BytesRead) {
154      Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
155      size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
156                                        kChunkSize);
157      BytesRead += bytes;
158      if (bytes < kChunkSize) {
159        if (ObjectSize && BytesRead < Pos)
160          assert(0 && "Unexpected short read fetching bitcode");
161        if (BytesRead <= Pos) { // reached EOF/ran out of bytes
162          ObjectSize = BytesRead;
163          EOFReached = true;
164          return false;
165        }
166      }
167    }
168    return true;
169  }
170
171  StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
172  void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
173};
174
175StreamableMemoryObject *getNonStreamedMemoryObject(
176    const unsigned char *Start, const unsigned char *End);
177
178}
179#endif  // STREAMABLEMEMORYOBJECT_H_
180