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 
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Errno.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MathExtras.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 
~MemoryBuffer()41 MemoryBuffer::~MemoryBuffer() { }
42 
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
init(const char * BufStart,const char * BufEnd,bool RequiresNullTerminator)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.
CopyStringRef(char * Memory,StringRef Data)59 static void CopyStringRef(char *Memory, StringRef Data) {
60   memcpy(Memory, Data.data(), Data.size());
61   Memory[Data.size()] = 0; // Null terminate string.
62 }
63 
64 namespace {
65 struct NamedBufferAlloc {
66   const Twine &Name;
NamedBufferAlloc__anon5fae06310111::NamedBufferAlloc67   NamedBufferAlloc(const Twine &Name) : Name(Name) {}
68 };
69 }
70 
operator new(size_t N,const NamedBufferAlloc & Alloc)71 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
72   SmallString<256> NameBuf;
73   StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
74 
75   char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
76   CopyStringRef(Mem + N, NameRef);
77   return Mem;
78 }
79 
80 namespace {
81 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
82 class MemoryBufferMem : public MemoryBuffer {
83 public:
MemoryBufferMem(StringRef InputData,bool RequiresNullTerminator)84   MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
85     init(InputData.begin(), InputData.end(), RequiresNullTerminator);
86   }
87 
getBufferIdentifier() const88   const char *getBufferIdentifier() const override {
89      // The name is stored after the class itself.
90     return reinterpret_cast<const char*>(this + 1);
91   }
92 
getBufferKind() const93   BufferKind getBufferKind() const override {
94     return MemoryBuffer_Malloc;
95   }
96 };
97 }
98 
99 static ErrorOr<std::unique_ptr<MemoryBuffer>>
100 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
101            uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize);
102 
103 std::unique_ptr<MemoryBuffer>
getMemBuffer(StringRef InputData,StringRef BufferName,bool RequiresNullTerminator)104 MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
105                            bool RequiresNullTerminator) {
106   auto *Ret = new (NamedBufferAlloc(BufferName))
107       MemoryBufferMem(InputData, RequiresNullTerminator);
108   return std::unique_ptr<MemoryBuffer>(Ret);
109 }
110 
111 std::unique_ptr<MemoryBuffer>
getMemBuffer(MemoryBufferRef Ref,bool RequiresNullTerminator)112 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
113   return std::unique_ptr<MemoryBuffer>(getMemBuffer(
114       Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
115 }
116 
117 std::unique_ptr<MemoryBuffer>
getMemBufferCopy(StringRef InputData,const Twine & BufferName)118 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
119   std::unique_ptr<MemoryBuffer> Buf =
120       getNewUninitMemBuffer(InputData.size(), BufferName);
121   if (!Buf)
122     return nullptr;
123   memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
124          InputData.size());
125   return Buf;
126 }
127 
128 std::unique_ptr<MemoryBuffer>
getNewUninitMemBuffer(size_t Size,const Twine & BufferName)129 MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
130   // Allocate space for the MemoryBuffer, the data and the name. It is important
131   // that MemoryBuffer and data are aligned so PointerIntPair works with them.
132   // TODO: Is 16-byte alignment enough?  We copy small object files with large
133   // alignment expectations into this buffer.
134   SmallString<256> NameBuf;
135   StringRef NameRef = BufferName.toStringRef(NameBuf);
136   size_t AlignedStringLen =
137       RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
138   size_t RealLen = AlignedStringLen + Size + 1;
139   char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
140   if (!Mem)
141     return nullptr;
142 
143   // The name is stored after the class itself.
144   CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
145 
146   // The buffer begins after the name and must be aligned.
147   char *Buf = Mem + AlignedStringLen;
148   Buf[Size] = 0; // Null terminate buffer.
149 
150   auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
151   return std::unique_ptr<MemoryBuffer>(Ret);
152 }
153 
154 std::unique_ptr<MemoryBuffer>
getNewMemBuffer(size_t Size,StringRef BufferName)155 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
156   std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
157   if (!SB)
158     return nullptr;
159   memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
160   return SB;
161 }
162 
163 ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine & Filename,int64_t FileSize)164 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) {
165   SmallString<256> NameBuf;
166   StringRef NameRef = Filename.toStringRef(NameBuf);
167 
168   if (NameRef == "-")
169     return getSTDIN();
170   return getFile(Filename, FileSize);
171 }
172 
173 ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileSlice(const Twine & FilePath,uint64_t MapSize,uint64_t Offset)174 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
175                            uint64_t Offset) {
176   return getFileAux(FilePath, -1, MapSize, Offset, false, false);
177 }
178 
179 
180 //===----------------------------------------------------------------------===//
181 // MemoryBuffer::getFile implementation.
182 //===----------------------------------------------------------------------===//
183 
184 namespace {
185 /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
186 ///
187 /// This handles converting the offset into a legal offset on the platform.
188 class MemoryBufferMMapFile : public MemoryBuffer {
189   sys::fs::mapped_file_region MFR;
190 
getLegalMapOffset(uint64_t Offset)191   static uint64_t getLegalMapOffset(uint64_t Offset) {
192     return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
193   }
194 
getLegalMapSize(uint64_t Len,uint64_t Offset)195   static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
196     return Len + (Offset - getLegalMapOffset(Offset));
197   }
198 
getStart(uint64_t Len,uint64_t Offset)199   const char *getStart(uint64_t Len, uint64_t Offset) {
200     return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
201   }
202 
203 public:
MemoryBufferMMapFile(bool RequiresNullTerminator,int FD,uint64_t Len,uint64_t Offset,std::error_code & EC)204   MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
205                        uint64_t Offset, std::error_code &EC)
206       : MFR(FD, sys::fs::mapped_file_region::readonly,
207             getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
208     if (!EC) {
209       const char *Start = getStart(Len, Offset);
210       init(Start, Start + Len, RequiresNullTerminator);
211     }
212   }
213 
getBufferIdentifier() const214   const char *getBufferIdentifier() const override {
215     // The name is stored after the class itself.
216     return reinterpret_cast<const char *>(this + 1);
217   }
218 
getBufferKind() const219   BufferKind getBufferKind() const override {
220     return MemoryBuffer_MMap;
221   }
222 };
223 }
224 
225 static ErrorOr<std::unique_ptr<MemoryBuffer>>
getMemoryBufferForStream(int FD,const Twine & BufferName)226 getMemoryBufferForStream(int FD, const Twine &BufferName) {
227   const ssize_t ChunkSize = 4096*4;
228   SmallString<ChunkSize> Buffer;
229   ssize_t ReadBytes;
230   // Read into Buffer until we hit EOF.
231   do {
232     Buffer.reserve(Buffer.size() + ChunkSize);
233     ReadBytes = read(FD, Buffer.end(), ChunkSize);
234     if (ReadBytes == -1) {
235       if (errno == EINTR) continue;
236       return std::error_code(errno, std::generic_category());
237     }
238     Buffer.set_size(Buffer.size() + ReadBytes);
239   } while (ReadBytes != 0);
240 
241   return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
242 }
243 
244 
245 ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine & Filename,int64_t FileSize,bool RequiresNullTerminator,bool IsVolatileSize)246 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
247                       bool RequiresNullTerminator, bool IsVolatileSize) {
248   return getFileAux(Filename, FileSize, FileSize, 0,
249                     RequiresNullTerminator, IsVolatileSize);
250 }
251 
252 static ErrorOr<std::unique_ptr<MemoryBuffer>>
253 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
254                 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
255                 bool IsVolatileSize);
256 
257 static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileAux(const Twine & Filename,int64_t FileSize,uint64_t MapSize,uint64_t Offset,bool RequiresNullTerminator,bool IsVolatileSize)258 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
259            uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) {
260   int FD;
261   std::error_code EC = sys::fs::openFileForRead(Filename, FD);
262   if (EC)
263     return EC;
264 
265   ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
266       getOpenFileImpl(FD, Filename, FileSize, MapSize, Offset,
267                       RequiresNullTerminator, IsVolatileSize);
268   close(FD);
269   return Ret;
270 }
271 
shouldUseMmap(int FD,size_t FileSize,size_t MapSize,off_t Offset,bool RequiresNullTerminator,int PageSize,bool IsVolatileSize)272 static bool shouldUseMmap(int FD,
273                           size_t FileSize,
274                           size_t MapSize,
275                           off_t Offset,
276                           bool RequiresNullTerminator,
277                           int PageSize,
278                           bool IsVolatileSize) {
279   // mmap may leave the buffer without null terminator if the file size changed
280   // by the time the last page is mapped in, so avoid it if the file size is
281   // likely to change.
282   if (IsVolatileSize)
283     return false;
284 
285   // We don't use mmap for small files because this can severely fragment our
286   // address space.
287   if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
288     return false;
289 
290   if (!RequiresNullTerminator)
291     return true;
292 
293 
294   // If we don't know the file size, use fstat to find out.  fstat on an open
295   // file descriptor is cheaper than stat on a random path.
296   // FIXME: this chunk of code is duplicated, but it avoids a fstat when
297   // RequiresNullTerminator = false and MapSize != -1.
298   if (FileSize == size_t(-1)) {
299     sys::fs::file_status Status;
300     if (sys::fs::status(FD, Status))
301       return false;
302     FileSize = Status.getSize();
303   }
304 
305   // If we need a null terminator and the end of the map is inside the file,
306   // we cannot use mmap.
307   size_t End = Offset + MapSize;
308   assert(End <= FileSize);
309   if (End != FileSize)
310     return false;
311 
312   // Don't try to map files that are exactly a multiple of the system page size
313   // if we need a null terminator.
314   if ((FileSize & (PageSize -1)) == 0)
315     return false;
316 
317 #if defined(__CYGWIN__)
318   // Don't try to map files that are exactly a multiple of the physical page size
319   // if we need a null terminator.
320   // FIXME: We should reorganize again getPageSize() on Win32.
321   if ((FileSize & (4096 - 1)) == 0)
322     return false;
323 #endif
324 
325   return true;
326 }
327 
328 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)329 getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
330                 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
331                 bool IsVolatileSize) {
332   static int PageSize = sys::Process::getPageSize();
333 
334   // Default is to map the full file.
335   if (MapSize == uint64_t(-1)) {
336     // If we don't know the file size, use fstat to find out.  fstat on an open
337     // file descriptor is cheaper than stat on a random path.
338     if (FileSize == uint64_t(-1)) {
339       sys::fs::file_status Status;
340       std::error_code EC = sys::fs::status(FD, Status);
341       if (EC)
342         return EC;
343 
344       // If this not a file or a block device (e.g. it's a named pipe
345       // or character device), we can't trust the size. Create the memory
346       // buffer by copying off the stream.
347       sys::fs::file_type Type = Status.type();
348       if (Type != sys::fs::file_type::regular_file &&
349           Type != sys::fs::file_type::block_file)
350         return getMemoryBufferForStream(FD, Filename);
351 
352       FileSize = Status.getSize();
353     }
354     MapSize = FileSize;
355   }
356 
357   if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
358                     PageSize, IsVolatileSize)) {
359     std::error_code EC;
360     std::unique_ptr<MemoryBuffer> Result(
361         new (NamedBufferAlloc(Filename))
362         MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC));
363     if (!EC)
364       return std::move(Result);
365   }
366 
367   std::unique_ptr<MemoryBuffer> Buf =
368       MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
369   if (!Buf) {
370     // Failed to create a buffer. The only way it can fail is if
371     // new(std::nothrow) returns 0.
372     return make_error_code(errc::not_enough_memory);
373   }
374 
375   char *BufPtr = const_cast<char *>(Buf->getBufferStart());
376 
377   size_t BytesLeft = MapSize;
378 #ifndef HAVE_PREAD
379   if (lseek(FD, Offset, SEEK_SET) == -1)
380     return std::error_code(errno, std::generic_category());
381 #endif
382 
383   while (BytesLeft) {
384 #ifdef HAVE_PREAD
385     ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
386 #else
387     ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
388 #endif
389     if (NumRead == -1) {
390       if (errno == EINTR)
391         continue;
392       // Error while reading.
393       return std::error_code(errno, std::generic_category());
394     }
395     if (NumRead == 0) {
396       memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
397       break;
398     }
399     BytesLeft -= NumRead;
400     BufPtr += NumRead;
401   }
402 
403   return std::move(Buf);
404 }
405 
406 ErrorOr<std::unique_ptr<MemoryBuffer>>
getOpenFile(int FD,const Twine & Filename,uint64_t FileSize,bool RequiresNullTerminator,bool IsVolatileSize)407 MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
408                           bool RequiresNullTerminator, bool IsVolatileSize) {
409   return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
410                          RequiresNullTerminator, IsVolatileSize);
411 }
412 
413 ErrorOr<std::unique_ptr<MemoryBuffer>>
getOpenFileSlice(int FD,const Twine & Filename,uint64_t MapSize,int64_t Offset)414 MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
415                                int64_t Offset) {
416   assert(MapSize != uint64_t(-1));
417   return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
418                          /*IsVolatileSize*/ false);
419 }
420 
getSTDIN()421 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
422   // Read in all of the data from stdin, we cannot mmap stdin.
423   //
424   // FIXME: That isn't necessarily true, we should try to mmap stdin and
425   // fallback if it fails.
426   sys::ChangeStdinToBinary();
427 
428   return getMemoryBufferForStream(0, "<stdin>");
429 }
430 
getMemBufferRef() const431 MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
432   StringRef Data = getBuffer();
433   StringRef Identifier = getBufferIdentifier();
434   return MemoryBufferRef(Data, Identifier);
435 }
436