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//===----------------------------------------------------------------------===//
16#include "llvm/Config/config.h"
31#include <system_error>
32#if !defined(_MSC_VER) && !defined(__MINGW32__)
40//===----------------------------------------------------------------------===//
41// MemoryBuffer implementation itself.
42//===----------------------------------------------------------------------===//
46/// init - Initialize this MemoryBuffer as a reference to externally allocated
47/// memory, memory that we know is already null terminated.
49 bool RequiresNullTerminator) {
50 assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
51 "Buffer is not null terminated!");
52 BufferStart = BufStart;
56//===----------------------------------------------------------------------===//
57// MemoryBufferMem implementation.
58//===----------------------------------------------------------------------===//
60/// CopyStringRef - Copies contents of a StringRef into a block of memory and
61/// null-terminates it.
65 Memory[
Data.size()] = 0;
// Null terminate string.
69struct NamedBufferAlloc {
71 NamedBufferAlloc(
const Twine &Name) :
Name(
Name) {}
75 void *
operator new(
size_t N,
const NamedBufferAlloc &
Alloc) {
79 // We use malloc() and manually handle it returning null instead of calling
80 // operator new because we need all uses of NamedBufferAlloc to be
81 // deallocated with a call to free() due to needing to use malloc() in
82 // WritableMemoryBuffer::getNewUninitMemBuffer() to work around the out-of-
83 // memory handler installed by default in LLVM. See operator delete() member
84 // functions within this file for the paired call to free().
86 static_cast<char *
>(std::malloc(
N +
sizeof(
size_t) + NameRef.
size() + 1));
89 *
reinterpret_cast<size_t *
>(Mem +
N) = NameRef.
size();
95/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
97class MemoryBufferMem :
public MB {
99 MemoryBufferMem(StringRef InputData,
bool RequiresNullTerminator) {
101 RequiresNullTerminator);
104 /// Disable sized deallocation for MemoryBufferMem, because it has
105 /// tail-allocated data.
106 void operator delete(
void *
p) { std::free(p); }
108 StringRef getBufferIdentifier()
const override {
109 // The name is stored after the class itself.
110 return StringRef(
reinterpret_cast<const char *
>(
this + 1) +
sizeof(
size_t),
111 *
reinterpret_cast<const size_t *
>(
this + 1));
120template <
typename MB>
123 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile,
124 std::optional<Align> Alignment);
126std::unique_ptr<MemoryBuffer>
128 bool RequiresNullTerminator) {
129 auto *Ret =
new (NamedBufferAlloc(BufferName))
130 MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
131 return std::unique_ptr<MemoryBuffer>(Ret);
134std::unique_ptr<MemoryBuffer>
137 Ref.getBuffer(),
Ref.getBufferIdentifier(), RequiresNullTerminator));
146 // Calling memcpy with null src/dst is UB, and an empty StringRef is
147 // represented with {nullptr, 0}.
149 return std::move(Buf);
152std::unique_ptr<MemoryBuffer>
156 return std::move(*Buf);
162 bool RequiresNullTerminator,
163 std::optional<Align> Alignment) {
165 StringRef NameRef = Filename.toStringRef(NameBuf);
169 return getFile(Filename, IsText, RequiresNullTerminator,
170 /*IsVolatile=*/false, Alignment);
176 std::optional<Align> Alignment) {
178 /*RequiresNullTerminator=*/false, IsVolatile,
182//===----------------------------------------------------------------------===//
183// MemoryBuffer::getFile implementation.
184//===----------------------------------------------------------------------===//
188template <
typename MB>
201/// Memory maps a file descriptor using sys::fs::mapped_file_region.
203/// This handles converting the offset into a legal offset on the platform.
205class MemoryBufferMMapFile :
public MB {
216 const char *getStart(uint64_t Len, uint64_t
Offset) {
221 MemoryBufferMMapFile(
bool RequiresNullTerminator,
sys::fs::file_t FD, uint64_t Len,
222 uint64_t
Offset, std::error_code &EC)
223 : MFR(FD, Mapmode<MB>, getLegalMapSize(
Len,
Offset),
231 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
232 /// tail-allocated data.
233 void operator delete(
void *
p) { std::free(p); }
235 StringRef getBufferIdentifier()
const override {
236 // The name is stored after the class itself.
237 return StringRef(
reinterpret_cast<const char *
>(
this + 1) +
sizeof(
size_t),
238 *
reinterpret_cast<const size_t *
>(
this + 1));
245 void dontNeedIfMmap()
override { MFR.
dontNeed(); }
259 bool RequiresNullTerminator,
bool IsVolatile,
260 std::optional<Align> Alignment) {
262 IsText, RequiresNullTerminator, IsVolatile,
266template <
typename MB>
270 bool IsVolatile, std::optional<Align> Alignment);
272template <
typename MB>
275 bool IsText,
bool RequiresNullTerminator,
bool IsVolatile,
276 std::optional<Align> Alignment) {
283 RequiresNullTerminator, IsVolatile, Alignment);
290 std::optional<Align> Alignment) {
292 Filename,
/*MapSize=*/-1,
/*Offset=*/0,
/*IsText=*/false,
293 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
299 std::optional<Align> Alignment) {
301 Filename, MapSize,
Offset,
/*IsText=*/false,
302 /*RequiresNullTerminator=*/false, IsVolatile, Alignment);
305std::unique_ptr<WritableMemoryBuffer>
307 const Twine &BufferName,
308 std::optional<Align> Alignment) {
309 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
311 // Use 16-byte alignment if no alignment is specified.
312 Align BufAlign = Alignment.value_or(
Align(16));
314 // Allocate space for the MemoryBuffer, the data and the name. It is important
315 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
319 size_t StringLen =
sizeof(MemBuffer) +
sizeof(
size_t) + NameRef.
size() + 1;
320 size_t RealLen = StringLen +
Size + 1 + BufAlign.
value();
321 if (RealLen <=
Size)
// Check for rollover.
323 // We use a call to malloc() rather than a call to a non-throwing operator
324 // new() because LLVM unconditionally installs an out of memory new handler
325 // when exceptions are disabled. This new handler intentionally crashes to
326 // aid with debugging, but that makes non-throwing new calls unhelpful.
327 // See MemoryBufferMem::operator delete() for the paired call to free(), and
328 // llvm::install_out_of_memory_new_handler() for the installation of the
329 // custom new handler.
330 char *Mem =
static_cast<char *
>(std::malloc(RealLen));
334 // The name is stored after the class itself.
335 *
reinterpret_cast<size_t *
>(Mem +
sizeof(MemBuffer)) = NameRef.
size();
336 CopyStringRef(Mem +
sizeof(MemBuffer) +
sizeof(
size_t), NameRef);
338 // The buffer begins after the name and must be aligned.
339 char *Buf = (
char *)
alignAddr(Mem + StringLen, BufAlign);
340 Buf[
Size] = 0;
// Null terminate buffer.
343 return std::unique_ptr<WritableMemoryBuffer>(Ret);
346std::unique_ptr<WritableMemoryBuffer>
351 memset(SB->getBufferStart(), 0,
Size);
359 bool RequiresNullTerminator,
363 // zOS Enhanced ASCII auto convert does not support mmap.
367 // mmap may leave the buffer without null terminator if the file size changed
368 // by the time the last page is mapped in, so avoid it if the file size is
370 if (IsVolatile && RequiresNullTerminator)
373 // We don't use mmap for small files because this can severely fragment our
375 if (MapSize < 4 * 4096 || MapSize < (
unsigned)
PageSize)
378 if (!RequiresNullTerminator)
381 // If we don't know the file size, use fstat to find out. fstat on an open
382 // file descriptor is cheaper than stat on a random path.
383 // FIXME: this chunk of code is duplicated, but it avoids a fstat when
384 // RequiresNullTerminator = false and MapSize != -1.
385 if (FileSize ==
size_t(-1)) {
389 FileSize =
Status.getSize();
392 // If we need a null terminator and the end of the map is inside the file,
393 // we cannot use mmap.
394 size_t End =
Offset + MapSize;
399 // Don't try to map files that are exactly a multiple of the system page size
400 // if we need a null terminator.
401 if ((FileSize & (
PageSize -1)) == 0)
404#if defined(__CYGWIN__)
405 // Don't try to map files that are exactly a multiple of the physical page size
406 // if we need a null terminator.
407 // FIXME: We should reorganize again getPageSize() on Win32.
408 if ((FileSize & (4096 - 1)) == 0)
424 // Default is to map the full file.
426 // If we don't know the file size, use fstat to find out. fstat on an open
427 // file descriptor is cheaper than stat on a random path.
434 // If this not a file or a block device (e.g. it's a named pipe
435 // or character device), we can't mmap it, so error out.
441 FileSize =
Status.getSize();
447 std::unique_ptr<WriteThroughMemoryBuffer> Result(
448 new (NamedBufferAlloc(Filename))
449 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(
false, FD, MapSize,
453 return std::move(Result);
461/// Map a subrange of the specified file as a WritableMemoryBuffer.
468template <
typename MB>
472 bool IsVolatile, std::optional<Align> Alignment) {
475 // Default is to map the full file.
477 // If we don't know the file size, use fstat to find out. fstat on an open
478 // file descriptor is cheaper than stat on a random path.
485 // If this not a file or a block device (e.g. it's a named pipe
486 // or character device), we can't trust the size. Create the memory
487 // buffer by copying off the stream.
493 FileSize =
Status.getSize();
501 std::unique_ptr<MB> Result(
502 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
503 RequiresNullTerminator, FD, MapSize,
Offset, EC));
505 // On at least Linux, and possibly on other systems, mmap may return pages
506 // from the page cache that are not properly filled with trailing zeroes,
507 // if some prior user of the page wrote non-zero bytes. Detect this and
508 // don't use mmap in that case.
509 if (!RequiresNullTerminator || *Result->getBufferEnd() ==
'0円')
510 return std::move(Result);
515 ErrorOr<bool> NeedsConversion = needConversion(Filename, FD);
516 if (std::error_code EC = NeedsConversion.
getError())
518 // File size may increase due to EBCDIC -> UTF-8 conversion, therefore we
519 // cannot trust the file size and we create the memory buffer by copying
521 // Note: This only works with the assumption of reading a full file (i.e,
522 // Offset == 0 and MapSize == FileSize). Reading a file slice does not work.
523 if (*NeedsConversion &&
Offset == 0 && MapSize == FileSize)
530 // Failed to create a buffer. The only way it can fail is if
531 // new(std::nothrow) returns 0.
535 // Read until EOF, zero-initialize the rest.
537 while (!ToRead.
empty()) {
542 if (*ReadBytes == 0) {
543 std::memset(ToRead.
data(), 0, ToRead.
size());
550 return std::move(Buf);
555 uint64_t FileSize,
bool RequiresNullTerminator,
556 bool IsVolatile, std::optional<Align> Alignment) {
558 RequiresNullTerminator, IsVolatile,
564 bool IsVolatile, std::optional<Align> Alignment) {
567 IsVolatile, Alignment);
571 // Read in all of the data from stdin, we cannot mmap stdin.
573 // FIXME: That isn't necessarily true, we should try to mmap stdin and
574 // fallback if it fails.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Prepare AGPR Alloc
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
static bool shouldUseMmap(sys::fs::file_t FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsText, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
static ErrorOr< std::unique_ptr< MB > > getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatile, std::optional< Align > Alignment)
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it.
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
Provides a library for accessing information about this process and other processes on the operating ...
This file defines the SmallString class.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
BufferKind
The kind of memory backing used to support the MemoryBuffer.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
StringRef getBuffer() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
MemoryBufferRef getMemBufferRef() const
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
~SmallVectorMemoryBuffer() override
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static LLVM_ABI ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
static LLVM_ABI std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="", std::optional< Align > Alignment=std::nullopt)
Allocate a new MemoryBuffer of the specified size that is not initialized.
static LLVM_ABI ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a WritableMemoryBuffer.
static LLVM_ABI ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
static LLVM_ABI ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a ReadWriteMemoryBuffer.
This class provides various memory handling functions that manipulate MemoryBlock instances.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Represents the result of a call to sys::fs::status().
This class represents a memory mapped file.
static LLVM_ABI int alignment()
@ priv
May modify via data, but changes are lost on destruction.
@ readonly
May only access map via const_data as read only.
@ readwrite
May access map via data and modify it. Written to path.
LLVM_ABI const char * const_data() const
Get a const view of the data.
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl< char > &Buffer, ssize_t ChunkSize=DefaultReadChunkSize)
Reads from FileHandle until EOF, appending to Buffer in chunks of size ChunkSize.
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
file_type
An enumeration for the file system's view of the type.
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
LLVM_ABI Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
Expected< file_t > openNativeFileForReadWrite(const Twine &Name, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI file_t getStdinHandle()
Return an open handle to standard in.
LLVM_ABI std::error_code ChangeStdinMode(fs::OpenFlags Flags)
This is an optimization pass for GlobalISel generic memory operations.
std::error_code make_error_code(BitcodeError E)
@ Ref
The access may reference the value stored in memory.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
OutputIt copy(R &&Range, OutputIt Out)
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.