<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
This commit is contained in:
Nick Kledzik 2008-05-09 01:09:59 +00:00
parent 018f771398
commit 90dcff7190
2 changed files with 20 additions and 4 deletions

View File

@ -23,6 +23,7 @@
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
#include "llvm/System/Path.h" #include "llvm/System/Path.h"
#include "llvm/System/Process.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetMachineRegistry.h" #include "llvm/Target/TargetMachineRegistry.h"
#include "llvm/Target/TargetAsmInfo.h" #include "llvm/Target/TargetAsmInfo.h"
@ -46,8 +47,7 @@ bool LTOModule::isBitcodeFile(const char* path)
bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length, bool LTOModule::isBitcodeFileForTarget(const void* mem, size_t length,
const char* triplePrefix) const char* triplePrefix)
{ {
MemoryBuffer* buffer = MemoryBuffer::getMemBuffer((char*)mem, MemoryBuffer* buffer = makeBuffer(mem, length);
(char*)mem+length);
if ( buffer == NULL ) if ( buffer == NULL )
return false; return false;
return isTargetMatch(buffer, triplePrefix); return isTargetMatch(buffer, triplePrefix);
@ -91,11 +91,26 @@ LTOModule* LTOModule::makeLTOModule(const char* path, std::string& errMsg)
return makeLTOModule(buffer.get(), errMsg); return makeLTOModule(buffer.get(), errMsg);
} }
MemoryBuffer* LTOModule::makeBuffer(const void* mem, size_t length)
{
// MemoryBuffer requires the byte past end of the buffer to be a zero.
// We might get lucky and already be that way, otherwise make a copy.
// Also if next byte is on a different page, don't assume it is readable.
const char* startPtr = (char*)mem;
const char* endPtr = startPtr+length;
if ( (((uintptr_t)endPtr & (sys::Process::GetPageSize()-1)) == 0)
|| (*endPtr != 0) )
return MemoryBuffer::getMemBufferCopy(startPtr, endPtr);
else
return MemoryBuffer::getMemBuffer(startPtr, endPtr);
}
LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length, LTOModule* LTOModule::makeLTOModule(const void* mem, size_t length,
std::string& errMsg) std::string& errMsg)
{ {
OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getMemBuffer((char*)mem, OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
(char*)mem+length));
if ( !buffer ) if ( !buffer )
return NULL; return NULL;
return makeLTOModule(buffer.get(), errMsg); return makeLTOModule(buffer.get(), errMsg);

View File

@ -81,6 +81,7 @@ private:
static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer, static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
std::string& errMsg); std::string& errMsg);
static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
typedef llvm::StringMap<uint8_t> StringSet; typedef llvm::StringMap<uint8_t> StringSet;