LLVM  4.0.0
MemoryBuffer.cpp
Go to the documentation of this file.
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
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 // This file implements the MemoryBuffer interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Errno.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
24 #include <cassert>
25 #include <cerrno>
26 #include <cstring>
27 #include <new>
28 #include <sys/types.h>
29 #include <system_error>
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
31 #include <unistd.h>
32 #else
33 #include <io.h>
34 #endif
35 using namespace llvm;
36 
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
40 
42 
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
45 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
46  bool RequiresNullTerminator) {
47  assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
48  "Buffer is not null terminated!");
49  BufferStart = BufStart;
50  BufferEnd = BufEnd;
51 }
52 
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
56 
57 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
58 /// null-terminates it.
59 static void CopyStringRef(char *Memory, StringRef Data) {
60  if (!Data.empty())
61  memcpy(Memory, Data.data(), Data.size());
62  Memory[Data.size()] = 0; // Null terminate string.
63 }
64 
65 namespace {
66 struct NamedBufferAlloc {
67  const Twine &Name;
68  NamedBufferAlloc(const Twine &Name) : Name(Name) {}
69 };
70 }
71 
72 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
73  SmallString<256> NameBuf;
74  StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
75 
76  char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
77  CopyStringRef(Mem + N, NameRef);
78  return Mem;
79 }
80 
81 namespace {
82 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
83 class MemoryBufferMem : public MemoryBuffer {
84 public:
85  MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
86  init(InputData.begin(), InputData.end(), RequiresNullTerminator);
87  }
88 
89  /// Disable sized deallocation for MemoryBufferMem, because it has
90  /// tail-allocated data.
91  void operator delete(void *p) { ::operator delete(p); }
92 
93  StringRef getBufferIdentifier() const override {
94  // The name is stored after the class itself.
95  return StringRef(reinterpret_cast<const char *>(this + 1));
96  }
97 
98  BufferKind getBufferKind() const override {
99  return MemoryBuffer_Malloc;
100  }
101 };
102 }
103 
105 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
106  uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize);
107 
108 std::unique_ptr<MemoryBuffer>
110  bool RequiresNullTerminator) {
111  auto *Ret = new (NamedBufferAlloc(BufferName))
112  MemoryBufferMem(InputData, RequiresNullTerminator);
113  return std::unique_ptr<MemoryBuffer>(Ret);
114 }
115 
116 std::unique_ptr<MemoryBuffer>
117 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
118  return std::unique_ptr<MemoryBuffer>(getMemBuffer(
119  Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
120 }
121 
122 std::unique_ptr<MemoryBuffer>
123 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
124  std::unique_ptr<MemoryBuffer> Buf =
125  getNewUninitMemBuffer(InputData.size(), BufferName);
126  if (!Buf)
127  return nullptr;
128  memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
129  InputData.size());
130  return Buf;
131 }
132 
133 std::unique_ptr<MemoryBuffer>
134 MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
135  // Allocate space for the MemoryBuffer, the data and the name. It is important
136  // that MemoryBuffer and data are aligned so PointerIntPair works with them.
137  // TODO: Is 16-byte alignment enough? We copy small object files with large
138  // alignment expectations into this buffer.
139  SmallString<256> NameBuf;
140  StringRef NameRef = BufferName.toStringRef(NameBuf);
141  size_t AlignedStringLen =
142  alignTo(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
143  size_t RealLen = AlignedStringLen + Size + 1;
144  char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
145  if (!Mem)
146  return nullptr;
147 
148  // The name is stored after the class itself.
149  CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
150 
151  // The buffer begins after the name and must be aligned.
152  char *Buf = Mem + AlignedStringLen;
153  Buf[Size] = 0; // Null terminate buffer.
154 
155  auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
156  return std::unique_ptr<MemoryBuffer>(Ret);
157 }
158 
159 std::unique_ptr<MemoryBuffer>
160 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
161  std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
162  if (!SB)
163  return nullptr;
164  memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
165  return SB;
166 }
167 
169 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize,
170  bool RequiresNullTerminator) {
171  SmallString<256> NameBuf;
172  StringRef NameRef = Filename.toStringRef(NameBuf);
173 
174  if (NameRef == "-")
175  return getSTDIN();
176  return getFile(Filename, FileSize, RequiresNullTerminator);
177 }
178 
180 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
181  uint64_t Offset) {
182  return getFileAux(FilePath, -1, MapSize, Offset, false, false);
183 }
184 
185 
186 //===----------------------------------------------------------------------===//
187 // MemoryBuffer::getFile implementation.
188 //===----------------------------------------------------------------------===//
189 
190 namespace {
191 /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
192 ///
193 /// This handles converting the offset into a legal offset on the platform.
194 class MemoryBufferMMapFile : public MemoryBuffer {
196 
197  static uint64_t getLegalMapOffset(uint64_t Offset) {
198  return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
199  }
200 
201  static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
202  return Len + (Offset - getLegalMapOffset(Offset));
203  }
204 
205  const char *getStart(uint64_t Len, uint64_t Offset) {
206  return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
207  }
208 
209 public:
210  MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
211  uint64_t Offset, std::error_code &EC)
212  : MFR(FD, sys::fs::mapped_file_region::readonly,
213  getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
214  if (!EC) {
215  const char *Start = getStart(Len, Offset);
216  init(Start, Start + Len, RequiresNullTerminator);
217  }
218  }
219 
220  /// Disable sized deallocation for MemoryBufferMMapFile, because it has
221  /// tail-allocated data.
222  void operator delete(void *p) { ::operator delete(p); }
223 
224  StringRef getBufferIdentifier() const override {
225  // The name is stored after the class itself.
226  return StringRef(reinterpret_cast<const char *>(this + 1));
227  }
228 
229  BufferKind getBufferKind() const override {
230  return MemoryBuffer_MMap;
231  }
232 };
233 }
234 
236 getMemoryBufferForStream(int FD, const Twine &BufferName) {
237  const ssize_t ChunkSize = 4096*4;
238  SmallString<ChunkSize> Buffer;
239  ssize_t ReadBytes;
240  // Read into Buffer until we hit EOF.
241  do {
242  Buffer.reserve(Buffer.size() + ChunkSize);
243  ReadBytes = read(FD, Buffer.end(), ChunkSize);
244  if (ReadBytes == -1) {
245  if (errno == EINTR) continue;
246  return std::error_code(errno, std::generic_category());
247  }
248  Buffer.set_size(Buffer.size() + ReadBytes);
249  } while (ReadBytes != 0);
250 
251  return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
252 }
253 
254 
256 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
257  bool RequiresNullTerminator, bool IsVolatileSize) {
258  return getFileAux(Filename, FileSize, FileSize, 0,
259  RequiresNullTerminator, IsVolatileSize);
260 }
261 
263 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
264  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
265  bool IsVolatileSize);
266 
268 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
269  uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) {
270  int FD;
271  std::error_code EC = sys::fs::openFileForRead(Filename, FD);
272  if (EC)
273  return EC;
274 
276  getOpenFileImpl(FD, Filename, FileSize, MapSize, Offset,
277  RequiresNullTerminator, IsVolatileSize);
278  close(FD);
279  return Ret;
280 }
281 
282 static bool shouldUseMmap(int FD,
283  size_t FileSize,
284  size_t MapSize,
285  off_t Offset,
286  bool RequiresNullTerminator,
287  int PageSize,
288  bool IsVolatileSize) {
289  // mmap may leave the buffer without null terminator if the file size changed
290  // by the time the last page is mapped in, so avoid it if the file size is
291  // likely to change.
292  if (IsVolatileSize)
293  return false;
294 
295  // We don't use mmap for small files because this can severely fragment our
296  // address space.
297  if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
298  return false;
299 
300  if (!RequiresNullTerminator)
301  return true;
302 
303 
304  // If we don't know the file size, use fstat to find out. fstat on an open
305  // file descriptor is cheaper than stat on a random path.
306  // FIXME: this chunk of code is duplicated, but it avoids a fstat when
307  // RequiresNullTerminator = false and MapSize != -1.
308  if (FileSize == size_t(-1)) {
310  if (sys::fs::status(FD, Status))
311  return false;
312  FileSize = Status.getSize();
313  }
314 
315  // If we need a null terminator and the end of the map is inside the file,
316  // we cannot use mmap.
317  size_t End = Offset + MapSize;
318  assert(End <= FileSize);
319  if (End != FileSize)
320  return false;
321 
322  // Don't try to map files that are exactly a multiple of the system page size
323  // if we need a null terminator.
324  if ((FileSize & (PageSize -1)) == 0)
325  return false;
326 
327 #if defined(__CYGWIN__)
328  // Don't try to map files that are exactly a multiple of the physical page size
329  // if we need a null terminator.
330  // FIXME: We should reorganize again getPageSize() on Win32.
331  if ((FileSize & (4096 - 1)) == 0)
332  return false;
333 #endif
334 
335  return true;
336 }
337 
339 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
340  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
341  bool IsVolatileSize) {
342  static int PageSize = sys::Process::getPageSize();
343 
344  // Default is to map the full file.
345  if (MapSize == uint64_t(-1)) {
346  // If we don't know the file size, use fstat to find out. fstat on an open
347  // file descriptor is cheaper than stat on a random path.
348  if (FileSize == uint64_t(-1)) {
350  std::error_code EC = sys::fs::status(FD, Status);
351  if (EC)
352  return EC;
353 
354  // If this not a file or a block device (e.g. it's a named pipe
355  // or character device), we can't trust the size. Create the memory
356  // buffer by copying off the stream.
357  sys::fs::file_type Type = Status.type();
358  if (Type != sys::fs::file_type::regular_file &&
360  return getMemoryBufferForStream(FD, Filename);
361 
362  FileSize = Status.getSize();
363  }
364  MapSize = FileSize;
365  }
366 
367  if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
368  PageSize, IsVolatileSize)) {
369  std::error_code EC;
370  std::unique_ptr<MemoryBuffer> Result(
371  new (NamedBufferAlloc(Filename))
372  MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC));
373  if (!EC)
374  return std::move(Result);
375  }
376 
377  std::unique_ptr<MemoryBuffer> Buf =
378  MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
379  if (!Buf) {
380  // Failed to create a buffer. The only way it can fail is if
381  // new(std::nothrow) returns 0.
383  }
384 
385  char *BufPtr = const_cast<char *>(Buf->getBufferStart());
386 
387  size_t BytesLeft = MapSize;
388 #ifndef HAVE_PREAD
389  if (lseek(FD, Offset, SEEK_SET) == -1)
390  return std::error_code(errno, std::generic_category());
391 #endif
392 
393  while (BytesLeft) {
394 #ifdef HAVE_PREAD
395  ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
396 #else
397  ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
398 #endif
399  if (NumRead == -1) {
400  if (errno == EINTR)
401  continue;
402  // Error while reading.
403  return std::error_code(errno, std::generic_category());
404  }
405  if (NumRead == 0) {
406  memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
407  break;
408  }
409  BytesLeft -= NumRead;
410  BufPtr += NumRead;
411  }
412 
413  return std::move(Buf);
414 }
415 
417 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
418  bool RequiresNullTerminator, bool IsVolatileSize) {
419  return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
420  RequiresNullTerminator, IsVolatileSize);
421 }
422 
424 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
425  int64_t Offset) {
426  assert(MapSize != uint64_t(-1));
427  return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
428  /*IsVolatileSize*/ false);
429 }
430 
432  // Read in all of the data from stdin, we cannot mmap stdin.
433  //
434  // FIXME: That isn't necessarily true, we should try to mmap stdin and
435  // fallback if it fails.
437 
438  return getMemoryBufferForStream(0, "<stdin>");
439 }
440 
443  int FD;
444  std::error_code EC = sys::fs::openFileForRead(Filename, FD);
445  if (EC)
446  return EC;
448  getMemoryBufferForStream(FD, Filename);
449  close(FD);
450  return Ret;
451 }
452 
454  StringRef Data = getBuffer();
455  StringRef Identifier = getBufferIdentifier();
456  return MemoryBufferRef(Data, Identifier);
457 }
MemoryBufferRef getMemBufferRef() const
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory, memory that we know is already null terminated.
Represents either an error or a value T.
Definition: ErrorOr.h:68
value_type read(const void *memory)
Read a value of a particular endianness from memory.
Definition: Endian.h:48
std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl< char > *RealPath=nullptr)
This class provides various memory handling functions that manipulate MemoryBlock instances...
Definition: Memory.h:46
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getMemoryBufferForStream(int FD, const Twine &BufferName)
This class represents a memory mapped file.
Definition: FileSystem.h:685
StringRef getBuffer() const
Definition: MemoryBuffer.h:59
static std::unique_ptr< MemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:664
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
void reserve(size_type N)
Definition: SmallVector.h:377
static std::unique_ptr< MemoryBuffer > getNewMemBuffer(size_t Size, StringRef BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
const char * const_data() const
Get a const view of the data.
static bool shouldUseMmap(int FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatileSize)
file_status - Represents the result of a call to stat and friends.
Definition: FileSystem.h:142
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize, uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a MemoryBuffer.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::error_code make_error_code(BitcodeError E)
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096))
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it...
iterator begin() const
Definition: StringRef.h:103
INITIALIZE_PASS(HexagonEarlyIfConversion,"hexagon-eif","Hexagon early if conversion", false, false) bool HexagonEarlyIfConversion MachineBasicBlock * SB
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
uint32_t Offset
static const unsigned End
static unsigned getPageSize()
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:463
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:65
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:130
std::error_code ChangeStdinToBinary()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Definition: SmallVector.h:668
Provides a library for accessing information about this process and other processes on the operating ...
file_type
An enumeration for the file system's view of the type.
Definition: FileSystem.h:55
virtual ~MemoryBuffer()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
#define N
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:171
iterator end() const
Definition: StringRef.h:105
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:125
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
file_type type() const
Definition: FileSystem.h:211
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize, int64_t Offset)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.