mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-27 14:24:40 +00:00
Fixing container/pointer bug in remote-lli found by ASan
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191976 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -36,8 +36,10 @@ allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID,
|
|||||||
// heap storage is sufficient here, but we're using mapped memory to work
|
// heap storage is sufficient here, but we're using mapped memory to work
|
||||||
// around a bug in MCJIT.
|
// around a bug in MCJIT.
|
||||||
sys::MemoryBlock Block = allocateSection(Size);
|
sys::MemoryBlock Block = allocateSection(Size);
|
||||||
|
// AllocatedSections will own this memory.
|
||||||
AllocatedSections.push_back( Allocation(Block, Alignment, true) );
|
AllocatedSections.push_back( Allocation(Block, Alignment, true) );
|
||||||
UnmappedSections.push_back( &AllocatedSections.back() );
|
// UnmappedSections has the same information but does not own the memory.
|
||||||
|
UnmappedSections.push_back( Allocation(Block, Alignment, true) );
|
||||||
return (uint8_t*)Block.base();
|
return (uint8_t*)Block.base();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +52,10 @@ allocateDataSection(uintptr_t Size, unsigned Alignment,
|
|||||||
// heap storage is sufficient here, but we're using mapped memory to work
|
// heap storage is sufficient here, but we're using mapped memory to work
|
||||||
// around a bug in MCJIT.
|
// around a bug in MCJIT.
|
||||||
sys::MemoryBlock Block = allocateSection(Size);
|
sys::MemoryBlock Block = allocateSection(Size);
|
||||||
|
// AllocatedSections will own this memory.
|
||||||
AllocatedSections.push_back( Allocation(Block, Alignment, false) );
|
AllocatedSections.push_back( Allocation(Block, Alignment, false) );
|
||||||
UnmappedSections.push_back( &AllocatedSections.back() );
|
// UnmappedSections has the same information but does not own the memory.
|
||||||
|
UnmappedSections.push_back( Allocation(Block, Alignment, false) );
|
||||||
return (uint8_t*)Block.base();
|
return (uint8_t*)Block.base();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,43 +90,39 @@ void RemoteMemoryManager::notifyObjectLoaded(ExecutionEngine *EE,
|
|||||||
// all the data sections.
|
// all the data sections.
|
||||||
uint64_t CurOffset = 0;
|
uint64_t CurOffset = 0;
|
||||||
unsigned MaxAlign = Target->getPageAlignment();
|
unsigned MaxAlign = Target->getPageAlignment();
|
||||||
SmallVector<std::pair<const Allocation*, uint64_t>, 16> Offsets;
|
SmallVector<std::pair<Allocation&, uint64_t>, 16> Offsets;
|
||||||
unsigned NumSections = UnmappedSections.size();
|
unsigned NumSections = UnmappedSections.size();
|
||||||
// We're going to go through the list twice to separate code and data, but
|
// We're going to go through the list twice to separate code and data, but
|
||||||
// it's a very small list, so that's OK.
|
// it's a very small list, so that's OK.
|
||||||
for (size_t i = 0, e = NumSections; i != e; ++i) {
|
for (size_t i = 0, e = NumSections; i != e; ++i) {
|
||||||
const Allocation *Section = UnmappedSections[i];
|
Allocation &Section = UnmappedSections[i];
|
||||||
assert(Section);
|
if (Section.IsCode) {
|
||||||
if (Section->IsCode) {
|
unsigned Size = Section.MB.size();
|
||||||
unsigned Size = Section->MB.size();
|
unsigned Align = Section.Alignment;
|
||||||
unsigned Align = Section->Alignment;
|
|
||||||
DEBUG(dbgs() << "code region: size " << Size
|
DEBUG(dbgs() << "code region: size " << Size
|
||||||
<< ", alignment " << Align << "\n");
|
<< ", alignment " << Align << "\n");
|
||||||
// Align the current offset up to whatever is needed for the next
|
// Align the current offset up to whatever is needed for the next
|
||||||
// section.
|
// section.
|
||||||
CurOffset = (CurOffset + Align - 1) / Align * Align;
|
CurOffset = (CurOffset + Align - 1) / Align * Align;
|
||||||
// Save off the address of the new section and allocate its space.
|
// Save off the address of the new section and allocate its space.
|
||||||
Offsets.push_back(std::pair<const Allocation*,uint64_t>(Section,
|
Offsets.push_back(std::pair<Allocation&,uint64_t>(Section, CurOffset));
|
||||||
CurOffset));
|
|
||||||
CurOffset += Size;
|
CurOffset += Size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Adjust to keep code and data aligned on seperate pages.
|
// Adjust to keep code and data aligned on seperate pages.
|
||||||
CurOffset = (CurOffset + MaxAlign - 1) / MaxAlign * MaxAlign;
|
CurOffset = (CurOffset + MaxAlign - 1) / MaxAlign * MaxAlign;
|
||||||
for (size_t i = 0, e = NumSections; i != e; ++i) {
|
for (size_t i = 0, e = NumSections; i != e; ++i) {
|
||||||
const Allocation *Section = UnmappedSections[i];
|
Allocation &Section = UnmappedSections[i];
|
||||||
assert(Section);
|
if (!Section.IsCode) {
|
||||||
if (!Section->IsCode) {
|
unsigned Size = Section.MB.size();
|
||||||
unsigned Size = Section->MB.size();
|
unsigned Align = Section.Alignment;
|
||||||
unsigned Align = Section->Alignment;
|
|
||||||
DEBUG(dbgs() << "data region: size " << Size
|
DEBUG(dbgs() << "data region: size " << Size
|
||||||
<< ", alignment " << Align << "\n");
|
<< ", alignment " << Align << "\n");
|
||||||
// Align the current offset up to whatever is needed for the next
|
// Align the current offset up to whatever is needed for the next
|
||||||
// section.
|
// section.
|
||||||
CurOffset = (CurOffset + Align - 1) / Align * Align;
|
CurOffset = (CurOffset + Align - 1) / Align * Align;
|
||||||
// Save off the address of the new section and allocate its space.
|
// Save off the address of the new section and allocate its space.
|
||||||
Offsets.push_back(std::pair<const Allocation*,uint64_t>(Section,
|
Offsets.push_back(std::pair<Allocation&,uint64_t>(Section, CurOffset));
|
||||||
CurOffset));
|
|
||||||
CurOffset += Size;
|
CurOffset += Size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,9 +136,9 @@ void RemoteMemoryManager::notifyObjectLoaded(ExecutionEngine *EE,
|
|||||||
// copies of the sections.
|
// copies of the sections.
|
||||||
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
|
for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
|
||||||
uint64_t Addr = RemoteAddr + Offsets[i].second;
|
uint64_t Addr = RemoteAddr + Offsets[i].second;
|
||||||
EE->mapSectionAddress(const_cast<void*>(Offsets[i].first->MB.base()), Addr);
|
EE->mapSectionAddress(const_cast<void*>(Offsets[i].first.MB.base()), Addr);
|
||||||
|
|
||||||
DEBUG(dbgs() << " Mapping local: " << Offsets[i].first->MB.base()
|
DEBUG(dbgs() << " Mapping local: " << Offsets[i].first.MB.base()
|
||||||
<< " to remote: 0x" << format("%llx", Addr) << "\n");
|
<< " to remote: 0x" << format("%llx", Addr) << "\n");
|
||||||
|
|
||||||
MappedSections[Addr] = Offsets[i].first;
|
MappedSections[Addr] = Offsets[i].first;
|
||||||
@ -149,20 +149,20 @@ void RemoteMemoryManager::notifyObjectLoaded(ExecutionEngine *EE,
|
|||||||
|
|
||||||
bool RemoteMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
bool RemoteMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
||||||
// FIXME: Make this function thread safe.
|
// FIXME: Make this function thread safe.
|
||||||
for (DenseMap<uint64_t, const Allocation*>::iterator
|
for (DenseMap<uint64_t, Allocation>::iterator
|
||||||
I = MappedSections.begin(), E = MappedSections.end();
|
I = MappedSections.begin(), E = MappedSections.end();
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
uint64_t RemoteAddr = I->first;
|
uint64_t RemoteAddr = I->first;
|
||||||
const Allocation *Section = I->second;
|
const Allocation &Section = I->second;
|
||||||
if (Section->IsCode) {
|
if (Section.IsCode) {
|
||||||
Target->loadCode(RemoteAddr, Section->MB.base(), Section->MB.size());
|
Target->loadCode(RemoteAddr, Section.MB.base(), Section.MB.size());
|
||||||
|
|
||||||
DEBUG(dbgs() << " loading code: " << Section->MB.base()
|
DEBUG(dbgs() << " loading code: " << Section.MB.base()
|
||||||
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
|
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
|
||||||
} else {
|
} else {
|
||||||
Target->loadData(RemoteAddr, Section->MB.base(), Section->MB.size());
|
Target->loadData(RemoteAddr, Section.MB.base(), Section.MB.size());
|
||||||
|
|
||||||
DEBUG(dbgs() << " loading data: " << Section->MB.base()
|
DEBUG(dbgs() << " loading data: " << Section.MB.base()
|
||||||
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
|
<< " to remote: 0x" << format("%llx", RemoteAddr) << "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ class RemoteMemoryManager : public JITMemoryManager {
|
|||||||
public:
|
public:
|
||||||
// Notice that this structure takes ownership of the memory allocated.
|
// Notice that this structure takes ownership of the memory allocated.
|
||||||
struct Allocation {
|
struct Allocation {
|
||||||
|
Allocation() {}
|
||||||
Allocation(sys::MemoryBlock mb, unsigned a, bool code)
|
Allocation(sys::MemoryBlock mb, unsigned a, bool code)
|
||||||
: MB(mb), Alignment(a), IsCode(code) {}
|
: MB(mb), Alignment(a), IsCode(code) {}
|
||||||
|
|
||||||
@ -48,11 +49,11 @@ private:
|
|||||||
// have allocated locally but have not yet remapped for the remote target.
|
// have allocated locally but have not yet remapped for the remote target.
|
||||||
// When we receive notification of a completed module load, we will map
|
// When we receive notification of a completed module load, we will map
|
||||||
// these sections into the remote target.
|
// these sections into the remote target.
|
||||||
SmallVector<const Allocation *, 2> UnmappedSections;
|
SmallVector<Allocation, 2> UnmappedSections;
|
||||||
|
|
||||||
// This map tracks the sections we have remapped for the remote target
|
// This map tracks the sections we have remapped for the remote target
|
||||||
// but have not yet copied to the target.
|
// but have not yet copied to the target.
|
||||||
DenseMap<uint64_t, const Allocation *> MappedSections;
|
DenseMap<uint64_t, Allocation> MappedSections;
|
||||||
|
|
||||||
// FIXME: This is part of a work around to keep sections near one another
|
// FIXME: This is part of a work around to keep sections near one another
|
||||||
// when MCJIT performs relocations after code emission but before
|
// when MCJIT performs relocations after code emission but before
|
||||||
|
Reference in New Issue
Block a user