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