1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the MemoryBuffer interface.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Errc.h"
17 #include "llvm/Support/Errno.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/SmallVectorMemoryBuffer.h"
28 #include <sys/types.h>
29 #include <system_error>
30 #if !defined(_MSC_VER) && !defined(__MINGW32__)
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
41 MemoryBuffer::~MemoryBuffer() { }
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
;
53 //===----------------------------------------------------------------------===//
54 // MemoryBufferMem implementation.
55 //===----------------------------------------------------------------------===//
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
) {
61 memcpy(Memory
, Data
.data(), Data
.size());
62 Memory
[Data
.size()] = 0; // Null terminate string.
66 struct NamedBufferAlloc
{
68 NamedBufferAlloc(const Twine
&Name
) : Name(Name
) {}
72 void *operator new(size_t N
, const NamedBufferAlloc
&Alloc
) {
73 SmallString
<256> NameBuf
;
74 StringRef NameRef
= Alloc
.Name
.toStringRef(NameBuf
);
76 char *Mem
= static_cast<char *>(operator new(N
+ NameRef
.size() + 1));
77 CopyStringRef(Mem
+ N
, NameRef
);
82 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
84 class MemoryBufferMem
: public MB
{
86 MemoryBufferMem(StringRef InputData
, bool RequiresNullTerminator
) {
87 MemoryBuffer::init(InputData
.begin(), InputData
.end(),
88 RequiresNullTerminator
);
91 /// Disable sized deallocation for MemoryBufferMem, because it has
92 /// tail-allocated data.
93 void operator delete(void *p
) { ::operator delete(p
); }
95 StringRef
getBufferIdentifier() const override
{
96 // The name is stored after the class itself.
97 return StringRef(reinterpret_cast<const char *>(this + 1));
100 MemoryBuffer::BufferKind
getBufferKind() const override
{
101 return MemoryBuffer::MemoryBuffer_Malloc
;
106 template <typename MB
>
107 static ErrorOr
<std::unique_ptr
<MB
>>
108 getFileAux(const Twine
&Filename
, int64_t FileSize
, uint64_t MapSize
,
109 uint64_t Offset
, bool RequiresNullTerminator
, bool IsVolatile
);
111 std::unique_ptr
<MemoryBuffer
>
112 MemoryBuffer::getMemBuffer(StringRef InputData
, StringRef BufferName
,
113 bool RequiresNullTerminator
) {
114 auto *Ret
= new (NamedBufferAlloc(BufferName
))
115 MemoryBufferMem
<MemoryBuffer
>(InputData
, RequiresNullTerminator
);
116 return std::unique_ptr
<MemoryBuffer
>(Ret
);
119 std::unique_ptr
<MemoryBuffer
>
120 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref
, bool RequiresNullTerminator
) {
121 return std::unique_ptr
<MemoryBuffer
>(getMemBuffer(
122 Ref
.getBuffer(), Ref
.getBufferIdentifier(), RequiresNullTerminator
));
125 static ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
126 getMemBufferCopyImpl(StringRef InputData
, const Twine
&BufferName
) {
127 auto Buf
= WritableMemoryBuffer::getNewUninitMemBuffer(InputData
.size(), BufferName
);
129 return make_error_code(errc::not_enough_memory
);
130 memcpy(Buf
->getBufferStart(), InputData
.data(), InputData
.size());
131 return std::move(Buf
);
134 std::unique_ptr
<MemoryBuffer
>
135 MemoryBuffer::getMemBufferCopy(StringRef InputData
, const Twine
&BufferName
) {
136 auto Buf
= getMemBufferCopyImpl(InputData
, BufferName
);
138 return std::move(*Buf
);
142 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
143 MemoryBuffer::getFileOrSTDIN(const Twine
&Filename
, int64_t FileSize
,
144 bool RequiresNullTerminator
) {
145 SmallString
<256> NameBuf
;
146 StringRef NameRef
= Filename
.toStringRef(NameBuf
);
150 return getFile(Filename
, FileSize
, RequiresNullTerminator
);
153 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
154 MemoryBuffer::getFileSlice(const Twine
&FilePath
, uint64_t MapSize
,
155 uint64_t Offset
, bool IsVolatile
) {
156 return getFileAux
<MemoryBuffer
>(FilePath
, -1, MapSize
, Offset
, false,
160 //===----------------------------------------------------------------------===//
161 // MemoryBuffer::getFile implementation.
162 //===----------------------------------------------------------------------===//
165 /// Memory maps a file descriptor using sys::fs::mapped_file_region.
167 /// This handles converting the offset into a legal offset on the platform.
168 template<typename MB
>
169 class MemoryBufferMMapFile
: public MB
{
170 sys::fs::mapped_file_region MFR
;
172 static uint64_t getLegalMapOffset(uint64_t Offset
) {
173 return Offset
& ~(sys::fs::mapped_file_region::alignment() - 1);
176 static uint64_t getLegalMapSize(uint64_t Len
, uint64_t Offset
) {
177 return Len
+ (Offset
- getLegalMapOffset(Offset
));
180 const char *getStart(uint64_t Len
, uint64_t Offset
) {
181 return MFR
.const_data() + (Offset
- getLegalMapOffset(Offset
));
185 MemoryBufferMMapFile(bool RequiresNullTerminator
, sys::fs::file_t FD
, uint64_t Len
,
186 uint64_t Offset
, std::error_code
&EC
)
187 : MFR(FD
, MB::Mapmode
, getLegalMapSize(Len
, Offset
),
188 getLegalMapOffset(Offset
), EC
) {
190 const char *Start
= getStart(Len
, Offset
);
191 MemoryBuffer::init(Start
, Start
+ Len
, RequiresNullTerminator
);
195 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
196 /// tail-allocated data.
197 void operator delete(void *p
) { ::operator delete(p
); }
199 StringRef
getBufferIdentifier() const override
{
200 // The name is stored after the class itself.
201 return StringRef(reinterpret_cast<const char *>(this + 1));
204 MemoryBuffer::BufferKind
getBufferKind() const override
{
205 return MemoryBuffer::MemoryBuffer_MMap
;
210 static ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
211 getMemoryBufferForStream(sys::fs::file_t FD
, const Twine
&BufferName
) {
212 const ssize_t ChunkSize
= 4096*4;
213 SmallString
<ChunkSize
> Buffer
;
214 // Read into Buffer until we hit EOF.
216 Buffer
.reserve(Buffer
.size() + ChunkSize
);
217 Expected
<size_t> ReadBytes
= sys::fs::readNativeFile(
218 FD
, makeMutableArrayRef(Buffer
.end(), ChunkSize
));
220 return errorToErrorCode(ReadBytes
.takeError());
223 Buffer
.set_size(Buffer
.size() + *ReadBytes
);
226 return getMemBufferCopyImpl(Buffer
, BufferName
);
230 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
231 MemoryBuffer::getFile(const Twine
&Filename
, int64_t FileSize
,
232 bool RequiresNullTerminator
, bool IsVolatile
) {
233 return getFileAux
<MemoryBuffer
>(Filename
, FileSize
, FileSize
, 0,
234 RequiresNullTerminator
, IsVolatile
);
237 template <typename MB
>
238 static ErrorOr
<std::unique_ptr
<MB
>>
239 getOpenFileImpl(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t FileSize
,
240 uint64_t MapSize
, int64_t Offset
, bool RequiresNullTerminator
,
243 template <typename MB
>
244 static ErrorOr
<std::unique_ptr
<MB
>>
245 getFileAux(const Twine
&Filename
, int64_t FileSize
, uint64_t MapSize
,
246 uint64_t Offset
, bool RequiresNullTerminator
, bool IsVolatile
) {
247 Expected
<sys::fs::file_t
> FDOrErr
=
248 sys::fs::openNativeFileForRead(Filename
, sys::fs::OF_None
);
250 return errorToErrorCode(FDOrErr
.takeError());
251 sys::fs::file_t FD
= *FDOrErr
;
252 auto Ret
= getOpenFileImpl
<MB
>(FD
, Filename
, FileSize
, MapSize
, Offset
,
253 RequiresNullTerminator
, IsVolatile
);
254 sys::fs::closeFile(FD
);
258 ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
259 WritableMemoryBuffer::getFile(const Twine
&Filename
, int64_t FileSize
,
261 return getFileAux
<WritableMemoryBuffer
>(Filename
, FileSize
, FileSize
, 0,
262 /*RequiresNullTerminator*/ false,
266 ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
267 WritableMemoryBuffer::getFileSlice(const Twine
&Filename
, uint64_t MapSize
,
268 uint64_t Offset
, bool IsVolatile
) {
269 return getFileAux
<WritableMemoryBuffer
>(Filename
, -1, MapSize
, Offset
, false,
273 std::unique_ptr
<WritableMemoryBuffer
>
274 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size
, const Twine
&BufferName
) {
275 using MemBuffer
= MemoryBufferMem
<WritableMemoryBuffer
>;
276 // Allocate space for the MemoryBuffer, the data and the name. It is important
277 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
278 // TODO: Is 16-byte alignment enough? We copy small object files with large
279 // alignment expectations into this buffer.
280 SmallString
<256> NameBuf
;
281 StringRef NameRef
= BufferName
.toStringRef(NameBuf
);
282 size_t AlignedStringLen
= alignTo(sizeof(MemBuffer
) + NameRef
.size() + 1, 16);
283 size_t RealLen
= AlignedStringLen
+ Size
+ 1;
284 char *Mem
= static_cast<char*>(operator new(RealLen
, std::nothrow
));
288 // The name is stored after the class itself.
289 CopyStringRef(Mem
+ sizeof(MemBuffer
), NameRef
);
291 // The buffer begins after the name and must be aligned.
292 char *Buf
= Mem
+ AlignedStringLen
;
293 Buf
[Size
] = 0; // Null terminate buffer.
295 auto *Ret
= new (Mem
) MemBuffer(StringRef(Buf
, Size
), true);
296 return std::unique_ptr
<WritableMemoryBuffer
>(Ret
);
299 std::unique_ptr
<WritableMemoryBuffer
>
300 WritableMemoryBuffer::getNewMemBuffer(size_t Size
, const Twine
&BufferName
) {
301 auto SB
= WritableMemoryBuffer::getNewUninitMemBuffer(Size
, BufferName
);
304 memset(SB
->getBufferStart(), 0, Size
);
308 static bool shouldUseMmap(sys::fs::file_t FD
,
312 bool RequiresNullTerminator
,
315 // mmap may leave the buffer without null terminator if the file size changed
316 // by the time the last page is mapped in, so avoid it if the file size is
321 // We don't use mmap for small files because this can severely fragment our
323 if (MapSize
< 4 * 4096 || MapSize
< (unsigned)PageSize
)
326 if (!RequiresNullTerminator
)
329 // If we don't know the file size, use fstat to find out. fstat on an open
330 // file descriptor is cheaper than stat on a random path.
331 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
332 // RequiresNullTerminator = false and MapSize != -1.
333 if (FileSize
== size_t(-1)) {
334 sys::fs::file_status Status
;
335 if (sys::fs::status(FD
, Status
))
337 FileSize
= Status
.getSize();
340 // If we need a null terminator and the end of the map is inside the file,
341 // we cannot use mmap.
342 size_t End
= Offset
+ MapSize
;
343 assert(End
<= FileSize
);
347 // Don't try to map files that are exactly a multiple of the system page size
348 // if we need a null terminator.
349 if ((FileSize
& (PageSize
-1)) == 0)
352 #if defined(__CYGWIN__)
353 // Don't try to map files that are exactly a multiple of the physical page size
354 // if we need a null terminator.
355 // FIXME: We should reorganize again getPageSize() on Win32.
356 if ((FileSize
& (4096 - 1)) == 0)
363 static ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
364 getReadWriteFile(const Twine
&Filename
, uint64_t FileSize
, uint64_t MapSize
,
366 Expected
<sys::fs::file_t
> FDOrErr
= sys::fs::openNativeFileForReadWrite(
367 Filename
, sys::fs::CD_OpenExisting
, sys::fs::OF_None
);
369 return errorToErrorCode(FDOrErr
.takeError());
370 sys::fs::file_t FD
= *FDOrErr
;
372 // Default is to map the full file.
373 if (MapSize
== uint64_t(-1)) {
374 // If we don't know the file size, use fstat to find out. fstat on an open
375 // file descriptor is cheaper than stat on a random path.
376 if (FileSize
== uint64_t(-1)) {
377 sys::fs::file_status Status
;
378 std::error_code EC
= sys::fs::status(FD
, Status
);
382 // If this not a file or a block device (e.g. it's a named pipe
383 // or character device), we can't mmap it, so error out.
384 sys::fs::file_type Type
= Status
.type();
385 if (Type
!= sys::fs::file_type::regular_file
&&
386 Type
!= sys::fs::file_type::block_file
)
387 return make_error_code(errc::invalid_argument
);
389 FileSize
= Status
.getSize();
395 std::unique_ptr
<WriteThroughMemoryBuffer
> Result(
396 new (NamedBufferAlloc(Filename
))
397 MemoryBufferMMapFile
<WriteThroughMemoryBuffer
>(false, FD
, MapSize
,
401 return std::move(Result
);
404 ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
405 WriteThroughMemoryBuffer::getFile(const Twine
&Filename
, int64_t FileSize
) {
406 return getReadWriteFile(Filename
, FileSize
, FileSize
, 0);
409 /// Map a subrange of the specified file as a WritableMemoryBuffer.
410 ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
411 WriteThroughMemoryBuffer::getFileSlice(const Twine
&Filename
, uint64_t MapSize
,
413 return getReadWriteFile(Filename
, -1, MapSize
, Offset
);
416 template <typename MB
>
417 static ErrorOr
<std::unique_ptr
<MB
>>
418 getOpenFileImpl(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t FileSize
,
419 uint64_t MapSize
, int64_t Offset
, bool RequiresNullTerminator
,
421 static int PageSize
= sys::Process::getPageSizeEstimate();
423 // Default is to map the full file.
424 if (MapSize
== uint64_t(-1)) {
425 // If we don't know the file size, use fstat to find out. fstat on an open
426 // file descriptor is cheaper than stat on a random path.
427 if (FileSize
== uint64_t(-1)) {
428 sys::fs::file_status Status
;
429 std::error_code EC
= sys::fs::status(FD
, Status
);
433 // If this not a file or a block device (e.g. it's a named pipe
434 // or character device), we can't trust the size. Create the memory
435 // buffer by copying off the stream.
436 sys::fs::file_type Type
= Status
.type();
437 if (Type
!= sys::fs::file_type::regular_file
&&
438 Type
!= sys::fs::file_type::block_file
)
439 return getMemoryBufferForStream(FD
, Filename
);
441 FileSize
= Status
.getSize();
446 if (shouldUseMmap(FD
, FileSize
, MapSize
, Offset
, RequiresNullTerminator
,
447 PageSize
, IsVolatile
)) {
449 std::unique_ptr
<MB
> Result(
450 new (NamedBufferAlloc(Filename
)) MemoryBufferMMapFile
<MB
>(
451 RequiresNullTerminator
, FD
, MapSize
, Offset
, EC
));
453 return std::move(Result
);
456 auto Buf
= WritableMemoryBuffer::getNewUninitMemBuffer(MapSize
, Filename
);
458 // Failed to create a buffer. The only way it can fail is if
459 // new(std::nothrow) returns 0.
460 return make_error_code(errc::not_enough_memory
);
463 // Read until EOF, zero-initialize the rest.
464 MutableArrayRef
<char> ToRead
= Buf
->getBuffer();
465 while (!ToRead
.empty()) {
466 Expected
<size_t> ReadBytes
=
467 sys::fs::readNativeFileSlice(FD
, ToRead
, Offset
);
469 return errorToErrorCode(ReadBytes
.takeError());
470 if (*ReadBytes
== 0) {
471 std::memset(ToRead
.data(), 0, ToRead
.size());
474 ToRead
= ToRead
.drop_front(*ReadBytes
);
475 Offset
+= *ReadBytes
;
478 return std::move(Buf
);
481 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
482 MemoryBuffer::getOpenFile(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t FileSize
,
483 bool RequiresNullTerminator
, bool IsVolatile
) {
484 return getOpenFileImpl
<MemoryBuffer
>(FD
, Filename
, FileSize
, FileSize
, 0,
485 RequiresNullTerminator
, IsVolatile
);
488 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
489 MemoryBuffer::getOpenFileSlice(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t MapSize
,
490 int64_t Offset
, bool IsVolatile
) {
491 assert(MapSize
!= uint64_t(-1));
492 return getOpenFileImpl
<MemoryBuffer
>(FD
, Filename
, -1, MapSize
, Offset
, false,
496 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MemoryBuffer::getSTDIN() {
497 // Read in all of the data from stdin, we cannot mmap stdin.
499 // FIXME: That isn't necessarily true, we should try to mmap stdin and
500 // fallback if it fails.
501 sys::ChangeStdinToBinary();
503 return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>");
506 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
507 MemoryBuffer::getFileAsStream(const Twine
&Filename
) {
508 Expected
<sys::fs::file_t
> FDOrErr
=
509 sys::fs::openNativeFileForRead(Filename
, sys::fs::OF_None
);
511 return errorToErrorCode(FDOrErr
.takeError());
512 sys::fs::file_t FD
= *FDOrErr
;
513 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Ret
=
514 getMemoryBufferForStream(FD
, Filename
);
515 sys::fs::closeFile(FD
);
519 MemoryBufferRef
MemoryBuffer::getMemBufferRef() const {
520 StringRef Data
= getBuffer();
521 StringRef Identifier
= getBufferIdentifier();
522 return MemoryBufferRef(Data
, Identifier
);
525 SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer() {}