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