MemoryBuffer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the MemoryBuffer interface.
  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 <cassert>
  24. #include <cerrno>
  25. #include <cstring>
  26. #include <new>
  27. #include <sys/types.h>
  28. #include <system_error>
  29. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  30. #include <unistd.h>
  31. #else
  32. #include <io.h>
  33. #endif
  34. using namespace llvm;
  35. //===----------------------------------------------------------------------===//
  36. // MemoryBuffer implementation itself.
  37. //===----------------------------------------------------------------------===//
  38. MemoryBuffer::~MemoryBuffer() { }
  39. /// init - Initialize this MemoryBuffer as a reference to externally allocated
  40. /// memory, memory that we know is already null terminated.
  41. void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
  42. bool RequiresNullTerminator) {
  43. assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
  44. "Buffer is not null terminated!");
  45. BufferStart = BufStart;
  46. BufferEnd = BufEnd;
  47. }
  48. //===----------------------------------------------------------------------===//
  49. // MemoryBufferMem implementation.
  50. //===----------------------------------------------------------------------===//
  51. /// CopyStringRef - Copies contents of a StringRef into a block of memory and
  52. /// null-terminates it.
  53. static void CopyStringRef(_Out_cap_x_(Data.size()+1) char *Memory, StringRef Data) {
  54. if (!Data.empty())
  55. memcpy(Memory, Data.data(), Data.size());
  56. Memory[Data.size()] = 0; // Null terminate string.
  57. }
  58. namespace {
  59. struct NamedBufferAlloc {
  60. const Twine &Name;
  61. NamedBufferAlloc(const Twine &Name) : Name(Name) {}
  62. };
  63. }
  64. void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
  65. SmallString<256> NameBuf;
  66. StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
  67. char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
  68. CopyStringRef(Mem + N, NameRef);
  69. return Mem;
  70. }
  71. namespace {
  72. /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
  73. class MemoryBufferMem : public MemoryBuffer {
  74. public:
  75. MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
  76. init(InputData.begin(), InputData.end(), RequiresNullTerminator);
  77. }
  78. const char *getBufferIdentifier() const override {
  79. // The name is stored after the class itself.
  80. return reinterpret_cast<const char*>(this + 1);
  81. }
  82. BufferKind getBufferKind() const override {
  83. return MemoryBuffer_Malloc;
  84. }
  85. };
  86. }
  87. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  88. getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
  89. uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize);
  90. std::unique_ptr<MemoryBuffer>
  91. MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
  92. bool RequiresNullTerminator) {
  93. auto *Ret = new (NamedBufferAlloc(BufferName))
  94. MemoryBufferMem(InputData, RequiresNullTerminator);
  95. return std::unique_ptr<MemoryBuffer>(Ret);
  96. }
  97. std::unique_ptr<MemoryBuffer>
  98. MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
  99. return std::unique_ptr<MemoryBuffer>(getMemBuffer(
  100. Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
  101. }
  102. std::unique_ptr<MemoryBuffer>
  103. MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
  104. std::unique_ptr<MemoryBuffer> Buf =
  105. getNewUninitMemBuffer(InputData.size(), BufferName);
  106. if (!Buf)
  107. return nullptr;
  108. memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
  109. InputData.size());
  110. return Buf;
  111. }
  112. std::unique_ptr<MemoryBuffer>
  113. MemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
  114. // Allocate space for the MemoryBuffer, the data and the name. It is important
  115. // that MemoryBuffer and data are aligned so PointerIntPair works with them.
  116. // TODO: Is 16-byte alignment enough? We copy small object files with large
  117. // alignment expectations into this buffer.
  118. SmallString<256> NameBuf;
  119. StringRef NameRef = BufferName.toStringRef(NameBuf);
  120. size_t AlignedStringLen =
  121. RoundUpToAlignment(sizeof(MemoryBufferMem) + NameRef.size() + 1, 16);
  122. size_t RealLen = AlignedStringLen + Size + 1;
  123. char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
  124. if (!Mem)
  125. return nullptr;
  126. // The name is stored after the class itself.
  127. CopyStringRef(Mem + sizeof(MemoryBufferMem), NameRef);
  128. // The buffer begins after the name and must be aligned.
  129. char *Buf = Mem + AlignedStringLen;
  130. Buf[Size] = 0; // Null terminate buffer.
  131. auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
  132. return std::unique_ptr<MemoryBuffer>(Ret);
  133. }
  134. std::unique_ptr<MemoryBuffer>
  135. MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
  136. std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
  137. if (!SB)
  138. return nullptr;
  139. memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
  140. return SB;
  141. }
  142. ErrorOr<std::unique_ptr<MemoryBuffer>>
  143. MemoryBuffer::getFileOrSTDIN(const Twine &Filename, int64_t FileSize) {
  144. SmallString<256> NameBuf;
  145. StringRef NameRef = Filename.toStringRef(NameBuf);
  146. if (NameRef == "-")
  147. return getSTDIN();
  148. return getFile(Filename, FileSize);
  149. }
  150. ErrorOr<std::unique_ptr<MemoryBuffer>>
  151. MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
  152. uint64_t Offset) {
  153. return getFileAux(FilePath, -1, MapSize, Offset, false, false);
  154. }
  155. //===----------------------------------------------------------------------===//
  156. // MemoryBuffer::getFile implementation.
  157. //===----------------------------------------------------------------------===//
  158. namespace {
  159. /// \brief Memory maps a file descriptor using sys::fs::mapped_file_region.
  160. ///
  161. /// This handles converting the offset into a legal offset on the platform.
  162. class MemoryBufferMMapFile : public MemoryBuffer {
  163. sys::fs::mapped_file_region MFR;
  164. static uint64_t getLegalMapOffset(uint64_t Offset) {
  165. return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
  166. }
  167. static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
  168. return Len + (Offset - getLegalMapOffset(Offset));
  169. }
  170. const char *getStart(uint64_t Len, uint64_t Offset) {
  171. return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
  172. }
  173. public:
  174. MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
  175. uint64_t Offset, std::error_code &EC)
  176. : MFR(FD, sys::fs::mapped_file_region::readonly,
  177. getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
  178. if (!EC) {
  179. const char *Start = getStart(Len, Offset);
  180. init(Start, Start + Len, RequiresNullTerminator);
  181. }
  182. }
  183. const char *getBufferIdentifier() const override {
  184. // The name is stored after the class itself.
  185. return reinterpret_cast<const char *>(this + 1);
  186. }
  187. BufferKind getBufferKind() const override {
  188. return MemoryBuffer_MMap;
  189. }
  190. };
  191. }
  192. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  193. getMemoryBufferForStream(int FD, const Twine &BufferName) {
  194. const ssize_t ChunkSize = 4096*4;
  195. const ssize_t InitialChunkSize = 4096*2; // HLSL Change - be more conservative on how much stack space we start with
  196. SmallString<InitialChunkSize> Buffer;
  197. ssize_t ReadBytes;
  198. // Read into Buffer until we hit EOF.
  199. do {
  200. Buffer.reserve(Buffer.size() + ChunkSize);
  201. ReadBytes = llvm::sys::fs::msf_read(FD, Buffer.end(), ChunkSize);
  202. if (ReadBytes == -1) {
  203. if (errno == EINTR) continue;
  204. return std::error_code(errno, std::generic_category());
  205. }
  206. Buffer.set_size(Buffer.size() + ReadBytes);
  207. } while (ReadBytes != 0);
  208. return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
  209. }
  210. ErrorOr<std::unique_ptr<MemoryBuffer>>
  211. MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
  212. bool RequiresNullTerminator, bool IsVolatileSize) {
  213. return getFileAux(Filename, FileSize, FileSize, 0,
  214. RequiresNullTerminator, IsVolatileSize);
  215. }
  216. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  217. getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
  218. uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
  219. bool IsVolatileSize);
  220. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  221. getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
  222. uint64_t Offset, bool RequiresNullTerminator, bool IsVolatileSize) {
  223. int FD;
  224. std::error_code EC = sys::fs::openFileForRead(Filename, FD);
  225. if (EC)
  226. return EC;
  227. ErrorOr<std::unique_ptr<MemoryBuffer>> Ret =
  228. getOpenFileImpl(FD, Filename, FileSize, MapSize, Offset,
  229. RequiresNullTerminator, IsVolatileSize);
  230. llvm::sys::fs::msf_close(FD); // HLSL Change - use msf_close
  231. return Ret;
  232. }
  233. static bool shouldUseMmap(int FD,
  234. size_t FileSize,
  235. size_t MapSize,
  236. off_t Offset,
  237. bool RequiresNullTerminator,
  238. int PageSize,
  239. bool IsVolatileSize) {
  240. // mmap may leave the buffer without null terminator if the file size changed
  241. // by the time the last page is mapped in, so avoid it if the file size is
  242. // likely to change.
  243. if (IsVolatileSize)
  244. return false;
  245. // We don't use mmap for small files because this can severely fragment our
  246. // address space.
  247. if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
  248. return false;
  249. if (!RequiresNullTerminator)
  250. return true;
  251. // If we don't know the file size, use fstat to find out. fstat on an open
  252. // file descriptor is cheaper than stat on a random path.
  253. // FIXME: this chunk of code is duplicated, but it avoids a fstat when
  254. // RequiresNullTerminator = false and MapSize != -1.
  255. if (FileSize == size_t(-1)) {
  256. sys::fs::file_status Status;
  257. if (sys::fs::status(FD, Status))
  258. return false;
  259. FileSize = Status.getSize();
  260. }
  261. // If we need a null terminator and the end of the map is inside the file,
  262. // we cannot use mmap.
  263. size_t End = Offset + MapSize;
  264. assert(End <= FileSize);
  265. if (End != FileSize)
  266. return false;
  267. // Don't try to map files that are exactly a multiple of the system page size
  268. // if we need a null terminator.
  269. if ((FileSize & (PageSize -1)) == 0)
  270. return false;
  271. #if defined(__CYGWIN__)
  272. // Don't try to map files that are exactly a multiple of the physical page size
  273. // if we need a null terminator.
  274. // FIXME: We should reorganize again getPageSize() on Win32.
  275. if ((FileSize & (4096 - 1)) == 0)
  276. return false;
  277. #endif
  278. return true;
  279. }
  280. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  281. getOpenFileImpl(int FD, const Twine &Filename, uint64_t FileSize,
  282. uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
  283. bool IsVolatileSize) {
  284. static int PageSize = sys::Process::getPageSize();
  285. // Default is to map the full file.
  286. if (MapSize == uint64_t(-1)) {
  287. // If we don't know the file size, use fstat to find out. fstat on an open
  288. // file descriptor is cheaper than stat on a random path.
  289. if (FileSize == uint64_t(-1)) {
  290. sys::fs::file_status Status;
  291. std::error_code EC = sys::fs::status(FD, Status);
  292. if (EC)
  293. return EC;
  294. // If this not a file or a block device (e.g. it's a named pipe
  295. // or character device), we can't trust the size. Create the memory
  296. // buffer by copying off the stream.
  297. sys::fs::file_type Type = Status.type();
  298. if (Type != sys::fs::file_type::regular_file &&
  299. Type != sys::fs::file_type::block_file)
  300. return getMemoryBufferForStream(FD, Filename);
  301. FileSize = Status.getSize();
  302. }
  303. MapSize = FileSize;
  304. }
  305. if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
  306. PageSize, IsVolatileSize)) {
  307. std::error_code EC;
  308. std::unique_ptr<MemoryBuffer> Result(
  309. new (NamedBufferAlloc(Filename))
  310. MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC));
  311. if (!EC)
  312. return std::move(Result);
  313. }
  314. std::unique_ptr<MemoryBuffer> Buf =
  315. MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
  316. if (!Buf) {
  317. // Failed to create a buffer. The only way it can fail is if
  318. // new(std::nothrow) returns 0.
  319. return make_error_code(errc::not_enough_memory);
  320. }
  321. char *BufPtr = const_cast<char *>(Buf->getBufferStart());
  322. size_t BytesLeft = MapSize;
  323. #undef HAVE_PREAD // HLSL Change - pread bypasses needed layers
  324. #ifndef HAVE_PREAD
  325. if (llvm::sys::fs::msf_lseek(FD, Offset, SEEK_SET) == -1) // HLSL Change - use msf_lseek
  326. return std::error_code(errno, std::generic_category());
  327. #endif
  328. while (BytesLeft) {
  329. #ifdef HAVE_PREAD
  330. ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset);
  331. #else
  332. ssize_t NumRead = ::llvm::sys::fs::msf_read(FD, BufPtr, BytesLeft);
  333. #endif
  334. if (NumRead == -1) {
  335. if (errno == EINTR)
  336. continue;
  337. // Error while reading.
  338. return std::error_code(errno, std::generic_category());
  339. }
  340. if (NumRead == 0) {
  341. memset(BufPtr, 0, BytesLeft); // zero-initialize rest of the buffer.
  342. break;
  343. }
  344. BytesLeft -= NumRead;
  345. BufPtr += NumRead;
  346. }
  347. return std::move(Buf);
  348. }
  349. ErrorOr<std::unique_ptr<MemoryBuffer>>
  350. MemoryBuffer::getOpenFile(int FD, const Twine &Filename, uint64_t FileSize,
  351. bool RequiresNullTerminator, bool IsVolatileSize) {
  352. return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0,
  353. RequiresNullTerminator, IsVolatileSize);
  354. }
  355. ErrorOr<std::unique_ptr<MemoryBuffer>>
  356. MemoryBuffer::getOpenFileSlice(int FD, const Twine &Filename, uint64_t MapSize,
  357. int64_t Offset) {
  358. assert(MapSize != uint64_t(-1));
  359. return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false,
  360. /*IsVolatileSize*/ false);
  361. }
  362. ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
  363. // Read in all of the data from stdin, we cannot mmap stdin.
  364. //
  365. // FIXME: That isn't necessarily true, we should try to mmap stdin and
  366. // fallback if it fails.
  367. sys::ChangeStdinToBinary();
  368. return getMemoryBufferForStream(0, "<stdin>");
  369. }
  370. MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
  371. StringRef Data = getBuffer();
  372. StringRef Identifier = getBufferIdentifier();
  373. return MemoryBufferRef(Data, Identifier);
  374. }