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