llvm.org GIT mirror llvm / 90dcff7
<rdar://problem/5917641> use getMemBufferCopy if supplied buffer is not already zero terminated git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50880 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Kledzik 12 years ago
2 changed file(s) with 20 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
2222 #include "llvm/Support/MemoryBuffer.h"
2323 #include "llvm/Support/MathExtras.h"
2424 #include "llvm/System/Path.h"
25 #include "llvm/System/Process.h"
2526 #include "llvm/Target/TargetMachine.h"
2627 #include "llvm/Target/TargetMachineRegistry.h"
2728 #include "llvm/Target/TargetAsmInfo.h"
4546 bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
4647 const char* triplePrefix)
4748 {
48 MemoryBuffer* buffer = MemoryBuffer::getMemBuffer((char*)mem,
49 (char*)mem+length);
49 MemoryBuffer* buffer = makeBuffer(mem, length);
5050 if ( buffer == NULL )
5151 return false;
5252 return isTargetMatch(buffer, triplePrefix);
9090 return makeLTOModule(buffer.get(), errMsg);
9191 }
9292
93
94 MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
95 {
96 // MemoryBuffer requires the byte past end of the buffer to be a zero.
97 // We might get lucky and already be that way, otherwise make a copy.
98 // Also if next byte is on a different page, don't assume it is readable.
99 const char* startPtr = (char*)mem;
100 const char* endPtr = startPtr+length;
101 if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
102 || (*endPtr != 0) )
103 return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
104 else
105 return MemoryBuffer::getMemBuffer(startPtr, endPtr);
106 }
107
108
93109 LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
94110 std::string& errMsg)
95111 {
96 OwningPtr buffer(MemoryBuffer::getMemBuffer((char*)mem,
97 (char*)mem+length));
112 OwningPtr buffer(makeBuffer(mem, length));
98113 if ( !buffer )
99114 return NULL;
100115 return makeLTOModule(buffer.get(), errMsg);
8080
8181 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
8282 std::string& errMsg);
83 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
8384
8485 typedef llvm::StringMap StringSet;
8586