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 14 #include "llvm/Support/MemoryBuffer.h" 15 #include "llvm/ADT/OwningPtr.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/Config/config.h" 18 #include "llvm/Support/MathExtras.h" 19 #include "llvm/Support/Errno.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/Process.h" 22 #include "llvm/Support/Program.h" 23 #include "llvm/Support/system_error.h" 24 #include <cassert> 25 #include <cstdio> 26 #include <cstring> 27 #include <cerrno> 28 #include <new> 29 #include <sys/types.h> 30 #include <sys/stat.h> 31 #if !defined(_MSC_VER) && !defined(__MINGW32__) 32 #include <unistd.h> 33 #else 34 #include <io.h> 35 #endif 36 #include <fcntl.h> 37 using namespace llvm; 38 39 //===----------------------------------------------------------------------===// 40 // MemoryBuffer implementation itself. 41 //===----------------------------------------------------------------------===// 42 43 MemoryBuffer::~MemoryBuffer() { } 44 45 /// init - Initialize this MemoryBuffer as a reference to externally allocated 46 /// memory, memory that we know is already null terminated. 47 void MemoryBuffer::init(const char *BufStart, const char *BufEnd, 48 bool RequiresNullTerminator) { 49 assert((!RequiresNullTerminator || BufEnd[0] == 0) && 50 "Buffer is not null terminated!"); 51 BufferStart = BufStart; 52 BufferEnd = BufEnd; 53 } 54 55 //===----------------------------------------------------------------------===// 56 // MemoryBufferMem implementation. 57 //===----------------------------------------------------------------------===// 58 59 /// CopyStringRef - Copies contents of a StringRef into a block of memory and 60 /// null-terminates it. 61 static void CopyStringRef(char *Memory, StringRef Data) { 62 memcpy(Memory, Data.data(), Data.size()); 63 Memory[Data.size()] = 0; // Null terminate string. 64 } 65 66 /// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it. 67 template <typename T> 68 static T *GetNamedBuffer(StringRef Buffer, StringRef Name, 69 bool RequiresNullTerminator) { 70 char *Mem = static_cast<char*>(operator new(sizeof(T) + Name.size() + 1)); 71 CopyStringRef(Mem + sizeof(T), Name); 72 return new (Mem) T(Buffer, RequiresNullTerminator); 73 } 74 75 namespace { 76 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory. 77 class MemoryBufferMem : public MemoryBuffer { 78 public: 79 MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) { 80 init(InputData.begin(), InputData.end(), RequiresNullTerminator); 81 } 82 83 virtual const char *getBufferIdentifier() const { 84 // The name is stored after the class itself. 85 return reinterpret_cast<const char*>(this + 1); 86 } 87 88 virtual BufferKind getBufferKind() const { 89 return MemoryBuffer_Malloc; 90 } 91 }; 92 } 93 94 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note 95 /// that InputData must be a null terminated if RequiresNullTerminator is true! 96 MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData, 97 StringRef BufferName, 98 bool RequiresNullTerminator) { 99 return GetNamedBuffer<MemoryBufferMem>(InputData, BufferName, 100 RequiresNullTerminator); 101 } 102 103 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, 104 /// copying the contents and taking ownership of it. This has no requirements 105 /// on EndPtr[0]. 106 MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData, 107 StringRef BufferName) { 108 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName); 109 if (!Buf) return 0; 110 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(), 111 InputData.size()); 112 return Buf; 113 } 114 115 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size 116 /// that is not initialized. Note that the caller should initialize the 117 /// memory allocated by this method. The memory is owned by the MemoryBuffer 118 /// object. 119 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size, 120 StringRef BufferName) { 121 // Allocate space for the MemoryBuffer, the data and the name. It is important 122 // that MemoryBuffer and data are aligned so PointerIntPair works with them. 123 size_t AlignedStringLen = 124 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 125 sizeof(void*)); // TODO: Is sizeof(void*) enough? 126 size_t RealLen = AlignedStringLen + Size + 1; 127 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow)); 128 if (!Mem) return 0; 129 130 // The name is stored after the class itself. 131 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName); 132 133 // The buffer begins after the name and must be aligned. 134 char *Buf = Mem + AlignedStringLen; 135 Buf[Size] = 0; // Null terminate buffer. 136 137 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true); 138 } 139 140 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that 141 /// is completely initialized to zeros. Note that the caller should 142 /// initialize the memory allocated by this method. The memory is owned by 143 /// the MemoryBuffer object. 144 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) { 145 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); 146 if (!SB) return 0; 147 memset(const_cast<char*>(SB->getBufferStart()), 0, Size); 148 return SB; 149 } 150 151 152 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin 153 /// if the Filename is "-". If an error occurs, this returns null and fills 154 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN) 155 /// returns an empty buffer. 156 error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename, 157 OwningPtr<MemoryBuffer> &result, 158 int64_t FileSize) { 159 if (Filename == "-") 160 return getSTDIN(result); 161 return getFile(Filename, result, FileSize); 162 } 163 164 error_code MemoryBuffer::getFileOrSTDIN(const char *Filename, 165 OwningPtr<MemoryBuffer> &result, 166 int64_t FileSize) { 167 if (strcmp(Filename, "-") == 0) 168 return getSTDIN(result); 169 return getFile(Filename, result, FileSize); 170 } 171 172 //===----------------------------------------------------------------------===// 173 // MemoryBuffer::getFile implementation. 174 //===----------------------------------------------------------------------===// 175 176 namespace { 177 /// MemoryBufferMMapFile - This represents a file that was mapped in with the 178 /// sys::Path::MapInFilePages method. When destroyed, it calls the 179 /// sys::Path::UnMapFilePages method. 180 class MemoryBufferMMapFile : public MemoryBufferMem { 181 public: 182 MemoryBufferMMapFile(StringRef Buffer, bool RequiresNullTerminator) 183 : MemoryBufferMem(Buffer, RequiresNullTerminator) { } 184 185 ~MemoryBufferMMapFile() { 186 static int PageSize = sys::Process::GetPageSize(); 187 188 uintptr_t Start = reinterpret_cast<uintptr_t>(getBufferStart()); 189 size_t Size = getBufferSize(); 190 uintptr_t RealStart = Start & ~(PageSize - 1); 191 size_t RealSize = Size + (Start - RealStart); 192 193 sys::Path::UnMapFilePages(reinterpret_cast<const char*>(RealStart), 194 RealSize); 195 } 196 197 virtual BufferKind getBufferKind() const { 198 return MemoryBuffer_MMap; 199 } 200 }; 201 } 202 203 error_code MemoryBuffer::getFile(StringRef Filename, 204 OwningPtr<MemoryBuffer> &result, 205 int64_t FileSize, 206 bool RequiresNullTerminator) { 207 // Ensure the path is null terminated. 208 SmallString<256> PathBuf(Filename.begin(), Filename.end()); 209 return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize, 210 RequiresNullTerminator); 211 } 212 213 error_code MemoryBuffer::getFile(const char *Filename, 214 OwningPtr<MemoryBuffer> &result, 215 int64_t FileSize, 216 bool RequiresNullTerminator) { 217 int OpenFlags = O_RDONLY; 218 #ifdef O_BINARY 219 OpenFlags |= O_BINARY; // Open input file in binary mode on win32. 220 #endif 221 int FD = ::open(Filename, OpenFlags); 222 if (FD == -1) 223 return error_code(errno, posix_category()); 224 225 error_code ret = getOpenFile(FD, Filename, result, FileSize, FileSize, 226 0, RequiresNullTerminator); 227 close(FD); 228 return ret; 229 } 230 231 static bool shouldUseMmap(int FD, 232 size_t FileSize, 233 size_t MapSize, 234 off_t Offset, 235 bool RequiresNullTerminator, 236 int PageSize) { 237 // We don't use mmap for small files because this can severely fragment our 238 // address space. 239 if (MapSize < 4096*4) 240 return false; 241 242 if (!RequiresNullTerminator) 243 return true; 244 245 246 // If we don't know the file size, use fstat to find out. fstat on an open 247 // file descriptor is cheaper than stat on a random path. 248 // FIXME: this chunk of code is duplicated, but it avoids a fstat when 249 // RequiresNullTerminator = false and MapSize != -1. 250 if (FileSize == size_t(-1)) { 251 struct stat FileInfo; 252 // TODO: This should use fstat64 when available. 253 if (fstat(FD, &FileInfo) == -1) { 254 return error_code(errno, posix_category()); 255 } 256 FileSize = FileInfo.st_size; 257 } 258 259 // If we need a null terminator and the end of the map is inside the file, 260 // we cannot use mmap. 261 size_t End = Offset + MapSize; 262 assert(End <= FileSize); 263 if (End != FileSize) 264 return false; 265 266 // Don't try to map files that are exactly a multiple of the system page size 267 // if we need a null terminator. 268 if ((FileSize & (PageSize -1)) == 0) 269 return false; 270 271 return true; 272 } 273 274 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, 275 OwningPtr<MemoryBuffer> &result, 276 uint64_t FileSize, uint64_t MapSize, 277 int64_t Offset, 278 bool RequiresNullTerminator) { 279 static int PageSize = sys::Process::GetPageSize(); 280 281 // Default is to map the full file. 282 if (MapSize == uint64_t(-1)) { 283 // If we don't know the file size, use fstat to find out. fstat on an open 284 // file descriptor is cheaper than stat on a random path. 285 if (FileSize == uint64_t(-1)) { 286 struct stat FileInfo; 287 // TODO: This should use fstat64 when available. 288 if (fstat(FD, &FileInfo) == -1) { 289 return error_code(errno, posix_category()); 290 } 291 FileSize = FileInfo.st_size; 292 } 293 MapSize = FileSize; 294 } 295 296 if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, 297 PageSize)) { 298 off_t RealMapOffset = Offset & ~(PageSize - 1); 299 off_t Delta = Offset - RealMapOffset; 300 size_t RealMapSize = MapSize + Delta; 301 302 if (const char *Pages = sys::Path::MapInFilePages(FD, 303 RealMapSize, 304 RealMapOffset)) { 305 result.reset(GetNamedBuffer<MemoryBufferMMapFile>( 306 StringRef(Pages + Delta, MapSize), Filename, RequiresNullTerminator)); 307 308 if (RequiresNullTerminator && result->getBufferEnd()[0] != '\0') { 309 // There could be a racing issue that resulted in the file being larger 310 // than the FileSize passed by the caller. We already have an assertion 311 // for this in MemoryBuffer::init() but have a runtime guarantee that 312 // the buffer will be null-terminated here, so do a copy that adds a 313 // null-terminator. 314 result.reset(MemoryBuffer::getMemBufferCopy(result->getBuffer(), 315 Filename)); 316 } 317 return error_code::success(); 318 } 319 } 320 321 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename); 322 if (!Buf) { 323 // Failed to create a buffer. The only way it can fail is if 324 // new(std::nothrow) returns 0. 325 return make_error_code(errc::not_enough_memory); 326 } 327 328 OwningPtr<MemoryBuffer> SB(Buf); 329 char *BufPtr = const_cast<char*>(SB->getBufferStart()); 330 331 size_t BytesLeft = MapSize; 332 #ifndef HAVE_PREAD 333 if (lseek(FD, Offset, SEEK_SET) == -1) 334 return error_code(errno, posix_category()); 335 #endif 336 337 while (BytesLeft) { 338 #ifdef HAVE_PREAD 339 ssize_t NumRead = ::pread(FD, BufPtr, BytesLeft, MapSize-BytesLeft+Offset); 340 #else 341 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft); 342 #endif 343 if (NumRead == -1) { 344 if (errno == EINTR) 345 continue; 346 // Error while reading. 347 return error_code(errno, posix_category()); 348 } 349 if (NumRead == 0) { 350 assert(0 && "We got inaccurate FileSize value or fstat reported an " 351 "invalid file size."); 352 *BufPtr = '\0'; // null-terminate at the actual size. 353 break; 354 } 355 BytesLeft -= NumRead; 356 BufPtr += NumRead; 357 } 358 359 result.swap(SB); 360 return error_code::success(); 361 } 362 363 //===----------------------------------------------------------------------===// 364 // MemoryBuffer::getSTDIN implementation. 365 //===----------------------------------------------------------------------===// 366 367 error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) { 368 // Read in all of the data from stdin, we cannot mmap stdin. 369 // 370 // FIXME: That isn't necessarily true, we should try to mmap stdin and 371 // fallback if it fails. 372 sys::Program::ChangeStdinToBinary(); 373 374 const ssize_t ChunkSize = 4096*4; 375 SmallString<ChunkSize> Buffer; 376 ssize_t ReadBytes; 377 // Read into Buffer until we hit EOF. 378 do { 379 Buffer.reserve(Buffer.size() + ChunkSize); 380 ReadBytes = read(0, Buffer.end(), ChunkSize); 381 if (ReadBytes == -1) { 382 if (errno == EINTR) continue; 383 return error_code(errno, posix_category()); 384 } 385 Buffer.set_size(Buffer.size() + ReadBytes); 386 } while (ReadBytes != 0); 387 388 result.reset(getMemBufferCopy(Buffer, "<stdin>")); 389 return error_code::success(); 390 } 391