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
~MemoryBuffer()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.
init(const char * BufStart,const char * BufEnd,bool RequiresNullTerminator)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.
CopyStringRef(char * Memory,StringRef Data)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;
NamedBufferAlloc__anon5f7fd5650111::NamedBufferAlloc68 NamedBufferAlloc(const Twine &Name) : Name(Name) {}
69 };
70 }
71
operator new(size_t N,const NamedBufferAlloc & Alloc)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:
MemoryBufferMem(StringRef InputData,bool RequiresNullTerminator)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.
operator delete(void * p)93 void operator delete(void *p) { ::operator delete(p); }
94
getBufferIdentifier() const95 StringRef getBufferIdentifier() const override {
96 // The name is stored after the class itself.
97 return StringRef(reinterpret_cast<const char *>(this + 1));
98 }
99
getBufferKind() const100 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>
getMemBuffer(StringRef InputData,StringRef BufferName,bool RequiresNullTerminator)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>
getMemBuffer(MemoryBufferRef Ref,bool RequiresNullTerminator)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>>
getMemBufferCopyImpl(StringRef InputData,const Twine & BufferName)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>
getMemBufferCopy(StringRef InputData,const Twine & BufferName)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>>
getFileOrSTDIN(const Twine & Filename,int64_t FileSize,bool RequiresNullTerminator)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>>
getFileSlice(const Twine & FilePath,uint64_t MapSize,uint64_t Offset,bool IsVolatile)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 #if 0 // XXX BINARYEN
165 namespace {
166 /// Memory maps a file descriptor using sys::fs::mapped_file_region.
167 ///
168 /// This handles converting the offset into a legal offset on the platform.
169 template<typename MB>
170 class MemoryBufferMMapFile : public MB {
171 sys::fs::mapped_file_region MFR;
172
173 static uint64_t getLegalMapOffset(uint64_t Offset) {
174 return Offset & ~(sys::fs::mapped_file_region::alignment() - 1);
175 }
176
177 static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
178 return Len + (Offset - getLegalMapOffset(Offset));
179 }
180
181 const char *getStart(uint64_t Len, uint64_t Offset) {
182 return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
183 }
184
185 public:
186 MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
187 uint64_t Offset, std::error_code &EC)
188 : MFR(FD, MB::Mapmode, getLegalMapSize(Len, Offset),
189 getLegalMapOffset(Offset), EC) {
190 if (!EC) {
191 const char *Start = getStart(Len, Offset);
192 MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
193 }
194 }
195
196 /// Disable sized deallocation for MemoryBufferMMapFile, because it has
197 /// tail-allocated data.
198 void operator delete(void *p) { ::operator delete(p); }
199
200 StringRef getBufferIdentifier() const override {
201 // The name is stored after the class itself.
202 return StringRef(reinterpret_cast<const char *>(this + 1));
203 }
204
205 MemoryBuffer::BufferKind getBufferKind() const override {
206 return MemoryBuffer::MemoryBuffer_MMap;
207 }
208 };
209 }
210 #endif
211
212 static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getMemoryBufferForStream(sys::fs::file_t FD,const Twine & BufferName)213 getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName) {
214 llvm_unreachable("getMemoryBufferForStream");
215 }
216
217
218 ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine & Filename,int64_t FileSize,bool RequiresNullTerminator,bool IsVolatile)219 MemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
220 bool RequiresNullTerminator, bool IsVolatile) {
221 return getFileAux<MemoryBuffer>(Filename, FileSize, FileSize, 0,
222 RequiresNullTerminator, IsVolatile);
223 }
224
225 template <typename MB>
226 static ErrorOr<std::unique_ptr<MB>>
227 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
228 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
229 bool IsVolatile);
230
231 template <typename MB>
232 static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine & Filename,int64_t FileSize,uint64_t MapSize,uint64_t Offset,bool RequiresNullTerminator,bool IsVolatile)233 getFileAux(const Twine &Filename, int64_t FileSize, uint64_t MapSize,
234 uint64_t Offset, bool RequiresNullTerminator, bool IsVolatile) {
235 llvm_unreachable("getFileAux");
236 }
237
238 ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine & Filename,int64_t FileSize,bool IsVolatile)239 WritableMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize,
240 bool IsVolatile) {
241 return getFileAux<WritableMemoryBuffer>(Filename, FileSize, FileSize, 0,
242 /*RequiresNullTerminator*/ false,
243 IsVolatile);
244 }
245
246 ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFileSlice(const Twine & Filename,uint64_t MapSize,uint64_t Offset,bool IsVolatile)247 WritableMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize,
248 uint64_t Offset, bool IsVolatile) {
249 return getFileAux<WritableMemoryBuffer>(Filename, -1, MapSize, Offset, false,
250 IsVolatile);
251 }
252
253 std::unique_ptr<WritableMemoryBuffer>
getNewUninitMemBuffer(size_t Size,const Twine & BufferName)254 WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size, const Twine &BufferName) {
255 using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
256 // Allocate space for the MemoryBuffer, the data and the name. It is important
257 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
258 // TODO: Is 16-byte alignment enough? We copy small object files with large
259 // alignment expectations into this buffer.
260 SmallString<256> NameBuf;
261 StringRef NameRef = BufferName.toStringRef(NameBuf);
262 size_t AlignedStringLen = alignTo(sizeof(MemBuffer) + NameRef.size() + 1, 16);
263 size_t RealLen = AlignedStringLen + Size + 1;
264 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
265 if (!Mem)
266 return nullptr;
267
268 // The name is stored after the class itself.
269 CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
270
271 // The buffer begins after the name and must be aligned.
272 char *Buf = Mem + AlignedStringLen;
273 Buf[Size] = 0; // Null terminate buffer.
274
275 auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
276 return std::unique_ptr<WritableMemoryBuffer>(Ret);
277 }
278
279 std::unique_ptr<WritableMemoryBuffer>
getNewMemBuffer(size_t Size,const Twine & BufferName)280 WritableMemoryBuffer::getNewMemBuffer(size_t Size, const Twine &BufferName) {
281 auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
282 if (!SB)
283 return nullptr;
284 memset(SB->getBufferStart(), 0, Size);
285 return SB;
286 }
287
shouldUseMmap(sys::fs::file_t FD,size_t FileSize,size_t MapSize,off_t Offset,bool RequiresNullTerminator,int PageSize,bool IsVolatile)288 static bool shouldUseMmap(sys::fs::file_t FD,
289 size_t FileSize,
290 size_t MapSize,
291 off_t Offset,
292 bool RequiresNullTerminator,
293 int PageSize,
294 bool IsVolatile) {
295 // XXX BINARYEn
296 return false;
297 }
298
299 static ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
getReadWriteFile(const Twine & Filename,uint64_t FileSize,uint64_t MapSize,uint64_t Offset)300 getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
301 uint64_t Offset) {
302 llvm_unreachable("getReadWriteFile");
303 }
304
305 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
getFile(const Twine & Filename,int64_t FileSize)306 WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
307 return getReadWriteFile(Filename, FileSize, FileSize, 0);
308 }
309
310 /// Map a subrange of the specified file as a WritableMemoryBuffer.
311 ErrorOr<std::unique_ptr<WriteThroughMemoryBuffer>>
getFileSlice(const Twine & Filename,uint64_t MapSize,uint64_t Offset)312 WriteThroughMemoryBuffer::getFileSlice(const Twine &Filename, uint64_t MapSize,
313 uint64_t Offset) {
314 return getReadWriteFile(Filename, -1, MapSize, Offset);
315 }
316
317 template <typename MB>
318 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)319 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
320 uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
321 bool IsVolatile) {
322 llvm_unreachable("getOpenFileImpl");
323 }
324
325 ErrorOr<std::unique_ptr<MemoryBuffer>>
getOpenFile(sys::fs::file_t FD,const Twine & Filename,uint64_t FileSize,bool RequiresNullTerminator,bool IsVolatile)326 MemoryBuffer::getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
327 bool RequiresNullTerminator, bool IsVolatile) {
328 return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
329 RequiresNullTerminator, IsVolatile);
330 }
331
332 ErrorOr<std::unique_ptr<MemoryBuffer>>
getOpenFileSlice(sys::fs::file_t FD,const Twine & Filename,uint64_t MapSize,int64_t Offset,bool IsVolatile)333 MemoryBuffer::getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
334 int64_t Offset, bool IsVolatile) {
335 assert(MapSize != uint64_t(-1));
336 return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
337 IsVolatile);
338 }
339
getSTDIN()340 ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
341 llvm_unreachable("getSTDIN");
342 }
343
344 ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileAsStream(const Twine & Filename)345 MemoryBuffer::getFileAsStream(const Twine &Filename) {
346 llvm_unreachable("getFileAsStream");
347 }
348
getMemBufferRef() const349 MemoryBufferRef MemoryBuffer::getMemBufferRef() const {
350 StringRef Data = getBuffer();
351 StringRef Identifier = getBufferIdentifier();
352 return MemoryBufferRef(Data, Identifier);
353 }
354
~SmallVectorMemoryBuffer()355 SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer() {}
356