1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the MemoryBuffer interface. 10 // 11 //===----------------------------------------------------------------------===// 12 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" 24 #include <cassert> 25 #include <cerrno> 26 #include <cstring> 27 #include <new> 28 #include <sys/types.h> 29 #include <system_error> 30 #if !defined(_MSC_VER) && !defined(__MINGW32__) 31 #include <unistd.h> 32 #else 33 #include <io.h> 34 #endif 35 using namespace llvm; 36 37 //===----------------------------------------------------------------------===// 38 // MemoryBuffer implementation itself. 39 //===----------------------------------------------------------------------===// 40 41 MemoryBuffer::~MemoryBuffer() { } 42 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; 50 BufferEnd = BufEnd; 51 } 52 53 //===----------------------------------------------------------------------===// 54 // MemoryBufferMem implementation. 55 //===----------------------------------------------------------------------===// 56 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) { 60 if (!Data.empty()) 61 memcpy(Memory, Data.data(), Data.size()); 62 Memory[Data.size()] = 0; // Null terminate string. 63 } 64 65 namespace { 66 struct NamedBufferAlloc { 67 const Twine &Name; 68 NamedBufferAlloc(const Twine &Name) : Name(Name) {} 69 }; 70 } 71 72 void *operator new(size_t N, const NamedBufferAlloc &Alloc) { 73 SmallString<256> NameBuf; 74 StringRef NameRef = Alloc.Name.toStringRef(NameBuf); 75 76 char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1)); 77 CopyStringRef(Mem + N, NameRef); 78 return Mem; 79 } 80 81 namespace { 82 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory. 83 template<typename MB> 84 class MemoryBufferMem : public MB { 85 public: 86 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) { 87 MemoryBuffer::init(InputData.begin(), InputData.end(), 88 RequiresNullTerminator); 89 } 90 91 /// Disable sized deallocation for MemoryBufferMem, because it has 92 /// tail-allocated data. 93 void operator delete(void *p) { ::operator delete(p); } 94 95 StringRef getBufferIdentifier() const override { 96 // The name is stored after the class itself. 97 return StringRef(reinterpret_cast<const char *>(this + 1)); 98 } 99 100 MemoryBuffer::BufferKind getBufferKind() const override { 101 return MemoryBuffer::MemoryBuffer_Malloc; 102 } 103 }; 104 } 105 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); 110 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); 117 } 118 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)); 123 } 124 125 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 126 getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) { 127 auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName); 128 if (!Buf) 129 return make_error_code(errc::not_enough_memory); 130 memcpy(Buf->getBufferStart(), InputData.data(), InputData.size()); 131 return std::move(Buf); 132 } 133 134 std::unique_ptr<MemoryBuffer> 135 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) { 136 auto Buf = getMemBufferCopyImpl(InputData, BufferName); 137 if (Buf) 138 return std::move(*Buf); 139 return nullptr; 140 } 141 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); 147 148 if (NameRef == "-") 149 return getSTDIN(); 150 return getFile(Filename, FileSize, RequiresNullTerminator); 151 } 152 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, 157 IsVolatile); 158 } 159 160 //===----------------------------------------------------------------------===// 161 // MemoryBuffer::getFile implementation. 162 //===----------------------------------------------------------------------===// 163 164 namespace { 165 166 template <typename MB> 167 constexpr sys::fs::mapped_file_region::mapmode Mapmode = 168 sys::fs::mapped_file_region::readonly; 169 template <> 170 constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> = 171 sys::fs::mapped_file_region::readonly; 172 template <> 173 constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> = 174 sys::fs::mapped_file_region::priv; 175 template <> 176 constexpr sys::fs::mapped_file_region::mapmode 177 Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite; 178 179 /// Memory maps a file descriptor using sys::fs::mapped_file_region. 180 /// 181 /// This handles converting the offset into a legal offset on the platform. 182 template<typename MB> 183 class MemoryBufferMMapFile : public MB { 184 sys::fs::mapped_file_region MFR; 185 186 static uint64_t getLegalMapOffset(uint64_t Offset) { 187 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1); 188 } 189 190 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) { 191 return Len + (Offset - getLegalMapOffset(Offset)); 192 } 193 194 const char *getStart(uint64_t Len, uint64_t Offset) { 195 return MFR.const_data() + (Offset - getLegalMapOffset(Offset)); 196 } 197 198 public: 199 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len, 200 uint64_t Offset, std::error_code &EC) 201 : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset), 202 getLegalMapOffset(Offset), EC) { 203 if (!EC) { 204 const char *Start = getStart(Len, Offset); 205 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator); 206 } 207 } 208 209 /// Disable sized deallocation for MemoryBufferMMapFile, because it has 210 /// tail-allocated data. 211 void operator delete(void *p) { ::operator delete(p); } 212 213 StringRef getBufferIdentifier() const override { 214 // The name is stored after the class itself. 215 return StringRef(reinterpret_cast<const char *>(this + 1)); 216 } 217 218 MemoryBuffer::BufferKind getBufferKind() const override { 219 return MemoryBuffer::MemoryBuffer_MMap; 220 } 221 }; 222 } 223 224 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 225 getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName) { 226 const ssize_t ChunkSize = 4096*4; 227 SmallString<ChunkSize> Buffer; 228 // Read into Buffer until we hit EOF. 229 for (;;) { 230 Buffer.reserve(Buffer.size() + ChunkSize); 231 Expected<size_t> ReadBytes = sys::fs::readNativeFile( 232 FD, makeMutableArrayRef(Buffer.end(), ChunkSize)); 233 if (!ReadBytes) 234 return errorToErrorCode(ReadBytes.takeError()); 235 if (*ReadBytes == 0) 236 break; 237 Buffer.set_size(Buffer.size() + *ReadBytes); 238 } 239 240 return getMemBufferCopyImpl(Buffer, BufferName); 241 } 242 243 244 ErrorOr<std::unique_ptr<MemoryBuffer>> 245 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize, 246 bool RequiresNullTerminator, bool IsVolatile) { 247 return getFileAux<MemoryBuffer>(Filename, FileSize, FileSize, 0, 248 RequiresNullTerminator, IsVolatile); 249 } 250 251 template <typename MB> 252 static ErrorOr<std::unique_ptr<MB>> 253 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, 254 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, 255 bool IsVolatile); 256 257 template <typename MB> 258 static ErrorOr<std::unique_ptr<MB>> 259 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize, 260 uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile) { 261 Expected<sys::fs::file_t> FDOrErr = 262 sys::fs::openNativeFileForRead(Filename, sys::fs::OF_None); 263 if (!FDOrErr) 264 return errorToErrorCode(FDOrErr.takeError()); 265 sys::fs::file_t FD = *FDOrErr; 266 auto Ret = getOpenFileImpl<MB>(FD, Filename, FileSize, MapSize, Offset, 267 RequiresNullTerminator, IsVolatile); 268 sys::fs::closeFile(FD); 269 return Ret; 270 } 271 272 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 273 WritableMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize, 274 bool IsVolatile) { 275 return getFileAux<WritableMemoryBuffer>(Filename, FileSize, FileSize, 0, 276 /*RequiresNullTerminator*/ false, 277 IsVolatile); 278 } 279 280 ErrorOr<std::unique_ptr<WritableMemoryBuffer>> 281 WritableMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize, 282 uint64_t Offset, bool IsVolatile) { 283 return getFileAux<WritableMemoryBuffer>(Filename, -1, MapSize, Offset, false, 284 IsVolatile); 285 } 286 287 std::unique_ptr<WritableMemoryBuffer> 288 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) { 289 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>; 290 // Allocate space for the MemoryBuffer, the data and the name. It is important 291 // that MemoryBuffer and data are aligned so PointerIntPair works with them. 292 // TODO: Is 16-byte alignment enough? We copy small object files with large 293 // alignment expectations into this buffer. 294 SmallString<256> NameBuf; 295 StringRef NameRef = BufferName.toStringRef(NameBuf); 296 size_t AlignedStringLen = alignTo(sizeof(MemBuffer) + NameRef.size() + 1, 16); 297 size_t RealLen = AlignedStringLen + Size + 1; 298 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow)); 299 if (!Mem) 300 return nullptr; 301 302 // The name is stored after the class itself. 303 CopyStringRef(Mem + sizeof(MemBuffer), NameRef); 304 305 // The buffer begins after the name and must be aligned. 306 char *Buf = Mem + AlignedStringLen; 307 Buf[Size] = 0; // Null terminate buffer. 308 309 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true); 310 return std::unique_ptr<WritableMemoryBuffer>(Ret); 311 } 312 313 std::unique_ptr<WritableMemoryBuffer> 314 WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) { 315 auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName); 316 if (!SB) 317 return nullptr; 318 memset(SB->getBufferStart(), 0, Size); 319 return SB; 320 } 321 322 static bool shouldUseMmap(sys::fs::file_t FD, 323 size_t FileSize, 324 size_t MapSize, 325 off_t Offset, 326 bool RequiresNullTerminator, 327 int PageSize, 328 bool IsVolatile) { 329 // mmap may leave the buffer without null terminator if the file size changed 330 // by the time the last page is mapped in, so avoid it if the file size is 331 // likely to change. 332 if (IsVolatile && RequiresNullTerminator) 333 return false; 334 335 // We don't use mmap for small files because this can severely fragment our 336 // address space. 337 if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize) 338 return false; 339 340 if (!RequiresNullTerminator) 341 return true; 342 343 // If we don't know the file size, use fstat to find out. fstat on an open 344 // file descriptor is cheaper than stat on a random path. 345 // FIXME: this chunk of code is duplicated, but it avoids a fstat when 346 // RequiresNullTerminator = false and MapSize != -1. 347 if (FileSize == size_t(-1)) { 348 sys::fs::file_status Status; 349 if (sys::fs::status(FD, Status)) 350 return false; 351 FileSize = Status.getSize(); 352 } 353 354 // If we need a null terminator and the end of the map is inside the file, 355 // we cannot use mmap. 356 size_t End = Offset + MapSize; 357 assert(End <= FileSize); 358 if (End != FileSize) 359 return false; 360 361 // Don't try to map files that are exactly a multiple of the system page size 362 // if we need a null terminator. 363 if ((FileSize & (PageSize -1)) == 0) 364 return false; 365 366 #if defined(__CYGWIN__) 367 // Don't try to map files that are exactly a multiple of the physical page size 368 // if we need a null terminator. 369 // FIXME: We should reorganize again getPageSize() on Win32. 370 if ((FileSize & (4096 - 1)) == 0) 371 return false; 372 #endif 373 374 return true; 375 } 376 377 static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 378 getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, 379 uint64_t Offset) { 380 Expected<sys::fs::file_t> FDOrErr = sys::fs::openNativeFileForReadWrite( 381 Filename, sys::fs::CD_OpenExisting, sys::fs::OF_None); 382 if (!FDOrErr) 383 return errorToErrorCode(FDOrErr.takeError()); 384 sys::fs::file_t FD = *FDOrErr; 385 386 // Default is to map the full file. 387 if (MapSize == uint64_t(-1)) { 388 // If we don't know the file size, use fstat to find out. fstat on an open 389 // file descriptor is cheaper than stat on a random path. 390 if (FileSize == uint64_t(-1)) { 391 sys::fs::file_status Status; 392 std::error_code EC = sys::fs::status(FD, Status); 393 if (EC) 394 return EC; 395 396 // If this not a file or a block device (e.g. it's a named pipe 397 // or character device), we can't mmap it, so error out. 398 sys::fs::file_type Type = Status.type(); 399 if (Type != sys::fs::file_type::regular_file && 400 Type != sys::fs::file_type::block_file) 401 return make_error_code(errc::invalid_argument); 402 403 FileSize = Status.getSize(); 404 } 405 MapSize = FileSize; 406 } 407 408 std::error_code EC; 409 std::unique_ptr<WriteThroughMemoryBuffer> Result( 410 new (NamedBufferAlloc(Filename)) 411 MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize, 412 Offset, EC)); 413 if (EC) 414 return EC; 415 return std::move(Result); 416 } 417 418 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 419 WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) { 420 return getReadWriteFile(Filename, FileSize, FileSize, 0); 421 } 422 423 /// Map a subrange of the specified file as a WritableMemoryBuffer. 424 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>> 425 WriteThroughMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize, 426 uint64_t Offset) { 427 return getReadWriteFile(Filename, -1, MapSize, Offset); 428 } 429 430 template <typename MB> 431 static ErrorOr<std::unique_ptr<MB>> 432 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, 433 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, 434 bool IsVolatile) { 435 static int PageSize = sys::Process::getPageSizeEstimate(); 436 437 // Default is to map the full file. 438 if (MapSize == uint64_t(-1)) { 439 // If we don't know the file size, use fstat to find out. fstat on an open 440 // file descriptor is cheaper than stat on a random path. 441 if (FileSize == uint64_t(-1)) { 442 sys::fs::file_status Status; 443 std::error_code EC = sys::fs::status(FD, Status); 444 if (EC) 445 return EC; 446 447 // If this not a file or a block device (e.g. it's a named pipe 448 // or character device), we can't trust the size. Create the memory 449 // buffer by copying off the stream. 450 sys::fs::file_type Type = Status.type(); 451 if (Type != sys::fs::file_type::regular_file && 452 Type != sys::fs::file_type::block_file) 453 return getMemoryBufferForStream(FD, Filename); 454 455 FileSize = Status.getSize(); 456 } 457 MapSize = FileSize; 458 } 459 460 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, 461 PageSize, IsVolatile)) { 462 std::error_code EC; 463 std::unique_ptr<MB> Result( 464 new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>( 465 RequiresNullTerminator, FD, MapSize, Offset, EC)); 466 if (!EC) 467 return std::move(Result); 468 } 469 470 auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename); 471 if (!Buf) { 472 // Failed to create a buffer. The only way it can fail is if 473 // new(std::nothrow) returns 0. 474 return make_error_code(errc::not_enough_memory); 475 } 476 477 // Read until EOF, zero-initialize the rest. 478 MutableArrayRef<char> ToRead = Buf->getBuffer(); 479 while (!ToRead.empty()) { 480 Expected<size_t> ReadBytes = 481 sys::fs::readNativeFileSlice(FD, ToRead, Offset); 482 if (!ReadBytes) 483 return errorToErrorCode(ReadBytes.takeError()); 484 if (*ReadBytes == 0) { 485 std::memset(ToRead.data(), 0, ToRead.size()); 486 break; 487 } 488 ToRead = ToRead.drop_front(*ReadBytes); 489 Offset += *ReadBytes; 490 } 491 492 return std::move(Buf); 493 } 494 495 ErrorOr<std::unique_ptr<MemoryBuffer>> 496 MemoryBuffer::getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, 497 bool RequiresNullTerminator, bool IsVolatile) { 498 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0, 499 RequiresNullTerminator, IsVolatile); 500 } 501 502 ErrorOr<std::unique_ptr<MemoryBuffer>> 503 MemoryBuffer::getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, 504 int64_t Offset, bool IsVolatile) { 505 assert(MapSize != uint64_t(-1)); 506 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false, 507 IsVolatile); 508 } 509 510 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() { 511 // Read in all of the data from stdin, we cannot mmap stdin. 512 // 513 // FIXME: That isn't necessarily true, we should try to mmap stdin and 514 // fallback if it fails. 515 sys::ChangeStdinToBinary(); 516 517 return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>"); 518 } 519 520 ErrorOr<std::unique_ptr<MemoryBuffer>> 521 MemoryBuffer::getFileAsStream(const Twine &Filename) { 522 Expected<sys::fs::file_t> FDOrErr = 523 sys::fs::openNativeFileForRead(Filename, sys::fs::OF_None); 524 if (!FDOrErr) 525 return errorToErrorCode(FDOrErr.takeError()); 526 sys::fs::file_t FD = *FDOrErr; 527 ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = 528 getMemoryBufferForStream(FD, Filename); 529 sys::fs::closeFile(FD); 530 return Ret; 531 } 532 533 MemoryBufferRef MemoryBuffer::getMemBufferRef() const { 534 StringRef Data = getBuffer(); 535 StringRef Identifier = getBufferIdentifier(); 536 return MemoryBufferRef(Data, Identifier); 537 } 538 539 SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer() {} 540