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