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/STLExtras.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Alignment.h"
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/Process.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/SmallVectorMemoryBuffer.h"
30 #include <sys/types.h>
31 #include <system_error>
32 #if !defined(_MSC_VER) && !defined(__MINGW32__)
39 #include "llvm/Support/AutoConvert.h"
43 //===----------------------------------------------------------------------===//
44 // MemoryBuffer implementation itself.
45 //===----------------------------------------------------------------------===//
47 MemoryBuffer::~MemoryBuffer() = default;
49 /// init - Initialize this MemoryBuffer as a reference to externally allocated
50 /// memory, memory that we know is already null terminated.
51 void MemoryBuffer::init(const char *BufStart
, const char *BufEnd
,
52 bool RequiresNullTerminator
) {
53 assert((!RequiresNullTerminator
|| BufEnd
[0] == 0) &&
54 "Buffer is not null terminated!");
55 BufferStart
= BufStart
;
59 //===----------------------------------------------------------------------===//
60 // MemoryBufferMem implementation.
61 //===----------------------------------------------------------------------===//
63 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
64 /// null-terminates it.
65 static void CopyStringRef(char *Memory
, StringRef Data
) {
67 memcpy(Memory
, Data
.data(), Data
.size());
68 Memory
[Data
.size()] = 0; // Null terminate string.
72 struct NamedBufferAlloc
{
74 NamedBufferAlloc(const Twine
&Name
) : Name(Name
) {}
78 void *operator new(size_t N
, const NamedBufferAlloc
&Alloc
) {
79 SmallString
<256> NameBuf
;
80 StringRef NameRef
= Alloc
.Name
.toStringRef(NameBuf
);
82 char *Mem
= static_cast<char *>(operator new(N
+ sizeof(size_t) +
84 *reinterpret_cast<size_t *>(Mem
+ N
) = NameRef
.size();
85 CopyStringRef(Mem
+ N
+ sizeof(size_t), NameRef
);
90 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
92 class MemoryBufferMem
: public MB
{
94 MemoryBufferMem(StringRef InputData
, bool RequiresNullTerminator
) {
95 MemoryBuffer::init(InputData
.begin(), InputData
.end(),
96 RequiresNullTerminator
);
99 /// Disable sized deallocation for MemoryBufferMem, because it has
100 /// tail-allocated data.
101 void operator delete(void *p
) { ::operator delete(p
); }
103 StringRef
getBufferIdentifier() const override
{
104 // The name is stored after the class itself.
105 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
106 *reinterpret_cast<const size_t *>(this + 1));
109 MemoryBuffer::BufferKind
getBufferKind() const override
{
110 return MemoryBuffer::MemoryBuffer_Malloc
;
115 template <typename MB
>
116 static ErrorOr
<std::unique_ptr
<MB
>>
117 getFileAux(const Twine
&Filename
, uint64_t MapSize
, uint64_t Offset
,
118 bool IsText
, bool RequiresNullTerminator
, bool IsVolatile
,
119 std::optional
<Align
> Alignment
);
121 std::unique_ptr
<MemoryBuffer
>
122 MemoryBuffer::getMemBuffer(StringRef InputData
, StringRef BufferName
,
123 bool RequiresNullTerminator
) {
124 auto *Ret
= new (NamedBufferAlloc(BufferName
))
125 MemoryBufferMem
<MemoryBuffer
>(InputData
, RequiresNullTerminator
);
126 return std::unique_ptr
<MemoryBuffer
>(Ret
);
129 std::unique_ptr
<MemoryBuffer
>
130 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref
, bool RequiresNullTerminator
) {
131 return std::unique_ptr
<MemoryBuffer
>(getMemBuffer(
132 Ref
.getBuffer(), Ref
.getBufferIdentifier(), RequiresNullTerminator
));
135 static ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
136 getMemBufferCopyImpl(StringRef InputData
, const Twine
&BufferName
) {
138 WritableMemoryBuffer::getNewUninitMemBuffer(InputData
.size(), BufferName
);
140 return make_error_code(errc::not_enough_memory
);
141 // Calling memcpy with null src/dst is UB, and an empty StringRef is
142 // represented with {nullptr, 0}.
143 llvm::copy(InputData
, Buf
->getBufferStart());
144 return std::move(Buf
);
147 std::unique_ptr
<MemoryBuffer
>
148 MemoryBuffer::getMemBufferCopy(StringRef InputData
, const Twine
&BufferName
) {
149 auto Buf
= getMemBufferCopyImpl(InputData
, BufferName
);
151 return std::move(*Buf
);
155 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
156 MemoryBuffer::getFileOrSTDIN(const Twine
&Filename
, bool IsText
,
157 bool RequiresNullTerminator
,
158 std::optional
<Align
> Alignment
) {
159 SmallString
<256> NameBuf
;
160 StringRef NameRef
= Filename
.toStringRef(NameBuf
);
164 return getFile(Filename
, IsText
, RequiresNullTerminator
,
165 /*IsVolatile=*/false, Alignment
);
168 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
169 MemoryBuffer::getFileSlice(const Twine
&FilePath
, uint64_t MapSize
,
170 uint64_t Offset
, bool IsVolatile
,
171 std::optional
<Align
> Alignment
) {
172 return getFileAux
<MemoryBuffer
>(FilePath
, MapSize
, Offset
, /*IsText=*/false,
173 /*RequiresNullTerminator=*/false, IsVolatile
,
177 //===----------------------------------------------------------------------===//
178 // MemoryBuffer::getFile implementation.
179 //===----------------------------------------------------------------------===//
183 template <typename MB
>
184 constexpr sys::fs::mapped_file_region::mapmode Mapmode
=
185 sys::fs::mapped_file_region::readonly
;
187 constexpr sys::fs::mapped_file_region::mapmode Mapmode
<MemoryBuffer
> =
188 sys::fs::mapped_file_region::readonly
;
190 constexpr sys::fs::mapped_file_region::mapmode Mapmode
<WritableMemoryBuffer
> =
191 sys::fs::mapped_file_region::priv
;
193 constexpr sys::fs::mapped_file_region::mapmode
194 Mapmode
<WriteThroughMemoryBuffer
> = sys::fs::mapped_file_region::readwrite
;
196 /// Memory maps a file descriptor using sys::fs::mapped_file_region.
198 /// This handles converting the offset into a legal offset on the platform.
199 template<typename MB
>
200 class MemoryBufferMMapFile
: public MB
{
201 sys::fs::mapped_file_region MFR
;
203 static uint64_t getLegalMapOffset(uint64_t Offset
) {
204 return Offset
& ~(sys::fs::mapped_file_region::alignment() - 1);
207 static uint64_t getLegalMapSize(uint64_t Len
, uint64_t Offset
) {
208 return Len
+ (Offset
- getLegalMapOffset(Offset
));
211 const char *getStart(uint64_t Len
, uint64_t Offset
) {
212 return MFR
.const_data() + (Offset
- getLegalMapOffset(Offset
));
216 MemoryBufferMMapFile(bool RequiresNullTerminator
, sys::fs::file_t FD
, uint64_t Len
,
217 uint64_t Offset
, std::error_code
&EC
)
218 : MFR(FD
, Mapmode
<MB
>, getLegalMapSize(Len
, Offset
),
219 getLegalMapOffset(Offset
), EC
) {
221 const char *Start
= getStart(Len
, Offset
);
222 MemoryBuffer::init(Start
, Start
+ Len
, RequiresNullTerminator
);
226 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
227 /// tail-allocated data.
228 void operator delete(void *p
) { ::operator delete(p
); }
230 StringRef
getBufferIdentifier() const override
{
231 // The name is stored after the class itself.
232 return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
233 *reinterpret_cast<const size_t *>(this + 1));
236 MemoryBuffer::BufferKind
getBufferKind() const override
{
237 return MemoryBuffer::MemoryBuffer_MMap
;
240 void dontNeedIfMmap() override
{ MFR
.dontNeed(); }
244 static ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
245 getMemoryBufferForStream(sys::fs::file_t FD
, const Twine
&BufferName
) {
246 SmallString
<sys::fs::DefaultReadChunkSize
> Buffer
;
247 if (Error E
= sys::fs::readNativeFileToEOF(FD
, Buffer
))
248 return errorToErrorCode(std::move(E
));
249 return getMemBufferCopyImpl(Buffer
, BufferName
);
252 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
253 MemoryBuffer::getFile(const Twine
&Filename
, bool IsText
,
254 bool RequiresNullTerminator
, bool IsVolatile
,
255 std::optional
<Align
> Alignment
) {
256 return getFileAux
<MemoryBuffer
>(Filename
, /*MapSize=*/-1, /*Offset=*/0,
257 IsText
, RequiresNullTerminator
, IsVolatile
,
261 template <typename MB
>
262 static ErrorOr
<std::unique_ptr
<MB
>>
263 getOpenFileImpl(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t FileSize
,
264 uint64_t MapSize
, int64_t Offset
, bool RequiresNullTerminator
,
265 bool IsVolatile
, std::optional
<Align
> Alignment
);
267 template <typename MB
>
268 static ErrorOr
<std::unique_ptr
<MB
>>
269 getFileAux(const Twine
&Filename
, uint64_t MapSize
, uint64_t Offset
,
270 bool IsText
, bool RequiresNullTerminator
, bool IsVolatile
,
271 std::optional
<Align
> Alignment
) {
272 Expected
<sys::fs::file_t
> FDOrErr
= sys::fs::openNativeFileForRead(
273 Filename
, IsText
? sys::fs::OF_TextWithCRLF
: sys::fs::OF_None
);
275 return errorToErrorCode(FDOrErr
.takeError());
276 sys::fs::file_t FD
= *FDOrErr
;
277 auto Ret
= getOpenFileImpl
<MB
>(FD
, Filename
, /*FileSize=*/-1, MapSize
, Offset
,
278 RequiresNullTerminator
, IsVolatile
, Alignment
);
279 sys::fs::closeFile(FD
);
283 ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
284 WritableMemoryBuffer::getFile(const Twine
&Filename
, bool IsVolatile
,
285 std::optional
<Align
> Alignment
) {
286 return getFileAux
<WritableMemoryBuffer
>(
287 Filename
, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
288 /*RequiresNullTerminator=*/false, IsVolatile
, Alignment
);
291 ErrorOr
<std::unique_ptr
<WritableMemoryBuffer
>>
292 WritableMemoryBuffer::getFileSlice(const Twine
&Filename
, uint64_t MapSize
,
293 uint64_t Offset
, bool IsVolatile
,
294 std::optional
<Align
> Alignment
) {
295 return getFileAux
<WritableMemoryBuffer
>(
296 Filename
, MapSize
, Offset
, /*IsText=*/false,
297 /*RequiresNullTerminator=*/false, IsVolatile
, Alignment
);
300 std::unique_ptr
<WritableMemoryBuffer
>
301 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size
,
302 const Twine
&BufferName
,
303 std::optional
<Align
> Alignment
) {
304 using MemBuffer
= MemoryBufferMem
<WritableMemoryBuffer
>;
306 // Use 16-byte alignment if no alignment is specified.
307 Align BufAlign
= Alignment
.value_or(Align(16));
309 // Allocate space for the MemoryBuffer, the data and the name. It is important
310 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
311 SmallString
<256> NameBuf
;
312 StringRef NameRef
= BufferName
.toStringRef(NameBuf
);
314 size_t StringLen
= sizeof(MemBuffer
) + sizeof(size_t) + NameRef
.size() + 1;
315 size_t RealLen
= StringLen
+ Size
+ 1 + BufAlign
.value();
316 if (RealLen
<= Size
) // Check for rollover.
318 char *Mem
= static_cast<char*>(operator new(RealLen
, std::nothrow
));
322 // The name is stored after the class itself.
323 *reinterpret_cast<size_t *>(Mem
+ sizeof(MemBuffer
)) = NameRef
.size();
324 CopyStringRef(Mem
+ sizeof(MemBuffer
) + sizeof(size_t), NameRef
);
326 // The buffer begins after the name and must be aligned.
327 char *Buf
= (char *)alignAddr(Mem
+ StringLen
, BufAlign
);
328 Buf
[Size
] = 0; // Null terminate buffer.
330 auto *Ret
= new (Mem
) MemBuffer(StringRef(Buf
, Size
), true);
331 return std::unique_ptr
<WritableMemoryBuffer
>(Ret
);
334 std::unique_ptr
<WritableMemoryBuffer
>
335 WritableMemoryBuffer::getNewMemBuffer(size_t Size
, const Twine
&BufferName
) {
336 auto SB
= WritableMemoryBuffer::getNewUninitMemBuffer(Size
, BufferName
);
339 memset(SB
->getBufferStart(), 0, Size
);
343 static bool shouldUseMmap(sys::fs::file_t FD
,
347 bool RequiresNullTerminator
,
350 // mmap may leave the buffer without null terminator if the file size changed
351 // by the time the last page is mapped in, so avoid it if the file size is
353 if (IsVolatile
&& RequiresNullTerminator
)
356 // We don't use mmap for small files because this can severely fragment our
358 if (MapSize
< 4 * 4096 || MapSize
< (unsigned)PageSize
)
361 if (!RequiresNullTerminator
)
364 // If we don't know the file size, use fstat to find out. fstat on an open
365 // file descriptor is cheaper than stat on a random path.
366 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
367 // RequiresNullTerminator = false and MapSize != -1.
368 if (FileSize
== size_t(-1)) {
369 sys::fs::file_status Status
;
370 if (sys::fs::status(FD
, Status
))
372 FileSize
= Status
.getSize();
375 // If we need a null terminator and the end of the map is inside the file,
376 // we cannot use mmap.
377 size_t End
= Offset
+ MapSize
;
378 assert(End
<= FileSize
);
382 // Don't try to map files that are exactly a multiple of the system page size
383 // if we need a null terminator.
384 if ((FileSize
& (PageSize
-1)) == 0)
387 #if defined(__CYGWIN__)
388 // Don't try to map files that are exactly a multiple of the physical page size
389 // if we need a null terminator.
390 // FIXME: We should reorganize again getPageSize() on Win32.
391 if ((FileSize
& (4096 - 1)) == 0)
398 static ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
399 getReadWriteFile(const Twine
&Filename
, uint64_t FileSize
, uint64_t MapSize
,
401 Expected
<sys::fs::file_t
> FDOrErr
= sys::fs::openNativeFileForReadWrite(
402 Filename
, sys::fs::CD_OpenExisting
, sys::fs::OF_None
);
404 return errorToErrorCode(FDOrErr
.takeError());
405 sys::fs::file_t FD
= *FDOrErr
;
407 // Default is to map the full file.
408 if (MapSize
== uint64_t(-1)) {
409 // If we don't know the file size, use fstat to find out. fstat on an open
410 // file descriptor is cheaper than stat on a random path.
411 if (FileSize
== uint64_t(-1)) {
412 sys::fs::file_status Status
;
413 std::error_code EC
= sys::fs::status(FD
, Status
);
417 // If this not a file or a block device (e.g. it's a named pipe
418 // or character device), we can't mmap it, so error out.
419 sys::fs::file_type Type
= Status
.type();
420 if (Type
!= sys::fs::file_type::regular_file
&&
421 Type
!= sys::fs::file_type::block_file
)
422 return make_error_code(errc::invalid_argument
);
424 FileSize
= Status
.getSize();
430 std::unique_ptr
<WriteThroughMemoryBuffer
> Result(
431 new (NamedBufferAlloc(Filename
))
432 MemoryBufferMMapFile
<WriteThroughMemoryBuffer
>(false, FD
, MapSize
,
436 return std::move(Result
);
439 ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
440 WriteThroughMemoryBuffer::getFile(const Twine
&Filename
, int64_t FileSize
) {
441 return getReadWriteFile(Filename
, FileSize
, FileSize
, 0);
444 /// Map a subrange of the specified file as a WritableMemoryBuffer.
445 ErrorOr
<std::unique_ptr
<WriteThroughMemoryBuffer
>>
446 WriteThroughMemoryBuffer::getFileSlice(const Twine
&Filename
, uint64_t MapSize
,
448 return getReadWriteFile(Filename
, -1, MapSize
, Offset
);
451 template <typename MB
>
452 static ErrorOr
<std::unique_ptr
<MB
>>
453 getOpenFileImpl(sys::fs::file_t FD
, const Twine
&Filename
, uint64_t FileSize
,
454 uint64_t MapSize
, int64_t Offset
, bool RequiresNullTerminator
,
455 bool IsVolatile
, std::optional
<Align
> Alignment
) {
456 static int PageSize
= sys::Process::getPageSizeEstimate();
458 // Default is to map the full file.
459 if (MapSize
== uint64_t(-1)) {
460 // If we don't know the file size, use fstat to find out. fstat on an open
461 // file descriptor is cheaper than stat on a random path.
462 if (FileSize
== uint64_t(-1)) {
463 sys::fs::file_status Status
;
464 std::error_code EC
= sys::fs::status(FD
, Status
);
468 // If this not a file or a block device (e.g. it's a named pipe
469 // or character device), we can't trust the size. Create the memory
470 // buffer by copying off the stream.
471 sys::fs::file_type Type
= Status
.type();
472 if (Type
!= sys::fs::file_type::regular_file
&&
473 Type
!= sys::fs::file_type::block_file
)
474 return getMemoryBufferForStream(FD
, Filename
);
476 FileSize
= Status
.getSize();
481 if (shouldUseMmap(FD
, FileSize
, MapSize
, Offset
, RequiresNullTerminator
,
482 PageSize
, IsVolatile
)) {
484 std::unique_ptr
<MB
> Result(
485 new (NamedBufferAlloc(Filename
)) MemoryBufferMMapFile
<MB
>(
486 RequiresNullTerminator
, FD
, MapSize
, Offset
, EC
));
488 return std::move(Result
);
492 // Set codepage auto-conversion for z/OS.
493 if (auto EC
= llvm::enableAutoConversion(FD
))
498 WritableMemoryBuffer::getNewUninitMemBuffer(MapSize
, Filename
, Alignment
);
500 // Failed to create a buffer. The only way it can fail is if
501 // new(std::nothrow) returns 0.
502 return make_error_code(errc::not_enough_memory
);
505 // Read until EOF, zero-initialize the rest.
506 MutableArrayRef
<char> ToRead
= Buf
->getBuffer();
507 while (!ToRead
.empty()) {
508 Expected
<size_t> ReadBytes
=
509 sys::fs::readNativeFileSlice(FD
, ToRead
, Offset
);
511 return errorToErrorCode(ReadBytes
.takeError());
512 if (*ReadBytes
== 0) {
513 std::memset(ToRead
.data(), 0, ToRead
.size());
516 ToRead
= ToRead
.drop_front(*ReadBytes
);
517 Offset
+= *ReadBytes
;
520 return std::move(Buf
);
523 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
524 MemoryBuffer::getOpenFile(sys::fs::file_t FD
, const Twine
&Filename
,
525 uint64_t FileSize
, bool RequiresNullTerminator
,
526 bool IsVolatile
, std::optional
<Align
> Alignment
) {
527 return getOpenFileImpl
<MemoryBuffer
>(FD
, Filename
, FileSize
, FileSize
, 0,
528 RequiresNullTerminator
, IsVolatile
,
532 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MemoryBuffer::getOpenFileSlice(
533 sys::fs::file_t FD
, const Twine
&Filename
, uint64_t MapSize
, int64_t Offset
,
534 bool IsVolatile
, std::optional
<Align
> Alignment
) {
535 assert(MapSize
!= uint64_t(-1));
536 return getOpenFileImpl
<MemoryBuffer
>(FD
, Filename
, -1, MapSize
, Offset
, false,
537 IsVolatile
, Alignment
);
540 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MemoryBuffer::getSTDIN() {
541 // Read in all of the data from stdin, we cannot mmap stdin.
543 // FIXME: That isn't necessarily true, we should try to mmap stdin and
544 // fallback if it fails.
545 sys::ChangeStdinMode(sys::fs::OF_Text
);
547 return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>");
550 ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
551 MemoryBuffer::getFileAsStream(const Twine
&Filename
) {
552 Expected
<sys::fs::file_t
> FDOrErr
=
553 sys::fs::openNativeFileForRead(Filename
, sys::fs::OF_None
);
555 return errorToErrorCode(FDOrErr
.takeError());
556 sys::fs::file_t FD
= *FDOrErr
;
557 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> Ret
=
558 getMemoryBufferForStream(FD
, Filename
);
559 sys::fs::closeFile(FD
);
563 MemoryBufferRef
MemoryBuffer::getMemBufferRef() const {
564 StringRef Data
= getBuffer();
565 StringRef Identifier
= getBufferIdentifier();
566 return MemoryBufferRef(Data
, Identifier
);
569 SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer() = default;