frames.
RuntimeDyld was previously responsible for tracking allocated EH frames, but it
makes more sense to have the RuntimeDyld::MemoryManager track them (since the
frames are allocated through the memory manager, and written to memory owned by
the memory manager). This patch moves the frame tracking into
RTDyldMemoryManager, and changes the deregisterFrames method on
RuntimeDyld::MemoryManager from:
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size);
to:
void deregisterEHFrames();
Separating this responsibility will allow ORC to continue to throw the
RuntimeDyld instances away post-link (saving a few dozen bytes per lazy
function) while properly deregistering frames when modules are unloaded.
This patch also updates ORC to call deregisterEHFrames when modules are
unloaded. This fixes a bug where an exception that tears down the JIT can then
unwind through dangling EH frames that have been deallocated but not
deregistered, resulting in UB.
For people using SectionMemoryManager this should be pretty much a no-op. For
people with custom allocators that override registerEHFrames/deregisterEHFrames,
you will now be responsible for tracking allocated EH frames.
Reviewed in https://reviews.llvm.org/D32829
llvm-svn: 302589
return nullptr;
}
+ void removeModulesFromBaseLayer(BaseLayerT &BaseLayer) {
+ for (auto &BLH : BaseLayerHandles)
+ BaseLayer.removeModuleSet(BLH);
+ }
+
std::unique_ptr<JITSymbolResolver> ExternalSymbolResolver;
std::unique_ptr<ResourceOwner<RuntimeDyld::MemoryManager>> MemMgr;
std::unique_ptr<IndirectStubsMgrT> StubsMgr;
CreateIndirectStubsManager(std::move(CreateIndirectStubsManager)),
CloneStubsIntoPartitions(CloneStubsIntoPartitions) {}
+ ~CompileOnDemandLayer() {
+ while (!LogicalDylibs.empty())
+ removeModuleSet(LogicalDylibs.begin());
+ }
+
/// @brief Add a module to the compile-on-demand layer.
template <typename ModuleSetT, typename MemoryManagerPtrT,
typename SymbolResolverPtrT>
/// This will remove all modules in the layers below that were derived from
/// the module represented by H.
void removeModuleSet(ModuleSetHandleT H) {
+ H->removeModulesFromBaseLayer(BaseLayer);
LogicalDylibs.erase(H);
}
return 0;
}
+ LD.BaseLayerHandles.push_back(PartH);
+
return CalledAddr;
}
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
size_t Size) override {
- UnfinalizedEHFrames.push_back(
- std::make_pair(LoadAddr, static_cast<uint32_t>(Size)));
+ UnfinalizedEHFrames.push_back({LoadAddr, Size});
}
- void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
- size_t Size) override {
- auto Err = Client.deregisterEHFrames(LoadAddr, Size);
- // FIXME: Add error poll.
- assert(!Err && "Failed to register remote EH frames.");
- (void)Err;
+ void deregisterEHFrames() override {
+ for (auto &Frame : RegisteredEHFrames) {
+ auto Err = Client.deregisterEHFrames(Frame.Addr, Frame.Size);
+ // FIXME: Add error poll.
+ assert(!Err && "Failed to register remote EH frames.");
+ (void)Err;
+ }
}
void notifyObjectLoaded(RuntimeDyld &Dyld,
Unfinalized.clear();
for (auto &EHFrame : UnfinalizedEHFrames) {
- if (auto Err = Client.registerEHFrames(EHFrame.first, EHFrame.second)) {
+ if (auto Err = Client.registerEHFrames(EHFrame.Addr, EHFrame.Size)) {
// FIXME: Replace this once finalizeMemory can return an Error.
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
if (ErrMsg) {
return false;
}
}
- UnfinalizedEHFrames.clear();
+ RegisteredEHFrames = std::move(UnfinalizedEHFrames);
+ UnfinalizedEHFrames = {};
return false;
}
ResourceIdMgr::ResourceId Id;
std::vector<ObjectAllocs> Unmapped;
std::vector<ObjectAllocs> Unfinalized;
- std::vector<std::pair<uint64_t, uint32_t>> UnfinalizedEHFrames;
+
+ struct EHFrame {
+ JITTargetAddress Addr;
+ uint64_t Size;
+ };
+ std::vector<EHFrame> UnfinalizedEHFrames;
+ std::vector<EHFrame> RegisteredEHFrames;
};
/// Remote indirect stubs manager.
buildInitialSymbolTable(PFC->Objects);
}
+ ~ConcreteLinkedObjectSet() override {
+ MemMgr->deregisterEHFrames();
+ }
+
void setHandle(ObjSetHandleT H) {
PFC->Handle = H;
}
/// Deregister EH frames in the current proces.
static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size);
- void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override {
- registerEHFramesInProcess(Addr, Size);
- }
-
- void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override {
- deregisterEHFramesInProcess(Addr, Size);
- }
+ void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
+ void deregisterEHFrames() override;
/// This method returns the address of the specified function or variable in
/// the current process.
/// MCJIT or RuntimeDyld. Use getSymbolAddress instead.
virtual void *getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure = true);
+
+private:
+ struct EHFrame {
+ uint8_t *Addr;
+ size_t Size;
+ };
+ std::vector<EHFrame> EHFrames;
};
// Create wrappers for C Binding types (see CBindingWrapping.h).
/// be the case for local execution) these two values will be the same.
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
size_t Size) = 0;
- virtual void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
- size_t Size) = 0;
+ virtual void deregisterEHFrames() = 0;
/// This method is called when object loading is complete and section page
/// permissions can be applied. It is up to the memory manager implementation
return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
}
- void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
- size_t Size) override {
- return ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
+ void deregisterEHFrames() override {
+ return ClientMM->deregisterEHFrames();
}
void notifyObjectLoaded(RuntimeDyld &RTDyld,
#endif
+void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
+ size_t Size) {
+ registerEHFramesInProcess(Addr, Size);
+ EHFrames.push_back({Addr, Size});
+}
+
+void RTDyldMemoryManager::deregisterEHFrames() {
+ for (auto &Frame : EHFrames)
+ deregisterEHFramesInProcess(Frame.Addr, Frame.Size);
+ EHFrames.clear();
+}
+
static int jit_noop() {
return 0;
}
void RuntimeDyldImpl::registerEHFrames() {}
-void RuntimeDyldImpl::deregisterEHFrames() {}
+void RuntimeDyldImpl::deregisterEHFrames() {
+ MemMgr.deregisterEHFrames();
+}
#ifndef NDEBUG
static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
size_t EHFrameSize = Sections[EHFrameSID].getSize();
MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
- RegisteredEHFrameSections.push_back(EHFrameSID);
}
UnregisteredEHFrameSections.clear();
}
-void RuntimeDyldELF::deregisterEHFrames() {
- for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
- SID EHFrameSID = RegisteredEHFrameSections[i];
- uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
- uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
- size_t EHFrameSize = Sections[EHFrameSID].getSize();
- MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
- }
- RegisteredEHFrameSections.clear();
-}
-
std::unique_ptr<RuntimeDyldELF>
llvm::RuntimeDyldELF::create(Triple::ArchType Arch,
RuntimeDyld::MemoryManager &MemMgr,
// in a table until we receive a request to register all unregistered
// EH frame sections with the memory manager.
SmallVector<SID, 2> UnregisteredEHFrameSections;
- SmallVector<SID, 2> RegisteredEHFrameSections;
// Map between GOT relocation value and corresponding GOT offset
std::map<RelocationValueRef, uint64_t> GOTOffsetMap;
StubMap &Stubs) override;
bool isCompatibleFile(const object::ObjectFile &Obj) const override;
void registerEHFrames() override;
- void deregisterEHFrames() override;
Error finalizeLoad(const ObjectFile &Obj,
ObjSectionToIDMap &SectionMap) override;
};
virtual void registerEHFrames();
- virtual void deregisterEHFrames();
+ void deregisterEHFrames();
virtual Error finalizeLoad(const ObjectFile &ObjImg,
ObjSectionToIDMap &SectionMap) {
}
void registerEHFrames() override {}
- void deregisterEHFrames() override {}
};
}
}
void registerEHFrames() override {}
- void deregisterEHFrames() override {}
};
}
}
UnregisteredEHFrameSections.clear();
}
- void deregisterEHFrames() override {
- // Stub
- }
Error finalizeLoad(const ObjectFile &Obj,
ObjSectionToIDMap &SectionMap) override {
// Look for and record the EH frame section IDs.
MemMgr->registerEHFrames(Addr, LoadAddr, Size);
}
- void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
- size_t Size) override {
- MemMgr->deregisterEHFrames(Addr, LoadAddr, Size);
+ void deregisterEHFrames() override {
+ MemMgr->deregisterEHFrames();
}
bool finalizeMemory(std::string *ErrMsg = nullptr) override {
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
size_t Size) override {}
- void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
- size_t Size) override {}
+ void deregisterEHFrames() override {}
void preallocateSlab(uint64_t Size) {
std::string Err;
return nullptr;
}
void registerEHFrames(uint8_t *, uint64_t, size_t) override {}
- void deregisterEHFrames(uint8_t *, uint64_t, size_t) override {}
+ void deregisterEHFrames() override {}
bool finalizeMemory(std::string *) override { return false; }
};
Objs.push_back(OwningObj.getBinary());
bool DebugSectionSeen = false;
- SectionMemoryManagerWrapper SMMW(DebugSectionSeen);
+ auto SMMW =
+ std::make_shared<SectionMemoryManagerWrapper>(DebugSectionSeen);
auto Resolver =
createLambdaResolver(
[](const std::string &Name) {
{
// Test with ProcessAllSections = false (the default).
- auto H = ObjLayer.addObjectSet(Objs, &SMMW, &*Resolver);
+ auto H = ObjLayer.addObjectSet(Objs, SMMW, &*Resolver);
ObjLayer.emitAndFinalize(H);
EXPECT_EQ(DebugSectionSeen, false)
<< "Unexpected debug info section";
{
// Test with ProcessAllSections = true.
ObjLayer.setProcessAllSections(true);
- auto H = ObjLayer.addObjectSet(Objs, &SMMW, &*Resolver);
+ auto H = ObjLayer.addObjectSet(Objs, SMMW, &*Resolver);
ObjLayer.emitAndFinalize(H);
EXPECT_EQ(DebugSectionSeen, true)
<< "Expected debug info section not seen";
return JITSymbol(nullptr);
});
- SectionMemoryManagerWrapper SMMW;
- ObjLayer.addObjectSet(std::move(Obj1Set), &SMMW, &*Resolver);
- auto H = ObjLayer.addObjectSet(std::move(Obj2Set), &SMMW, &*Resolver);
+ auto SMMW = std::make_shared<SectionMemoryManagerWrapper>();
+ ObjLayer.addObjectSet(std::move(Obj1Set), SMMW, &*Resolver);
+ auto H = ObjLayer.addObjectSet(std::move(Obj2Set), SMMW, &*Resolver);
ObjLayer.emitAndFinalize(H);
-
+ ObjLayer.removeObjectSet(H);
+
// Finalization of module 2 should trigger finalization of module 1.
// Verify that finalize on SMMW is only called once.
- EXPECT_EQ(SMMW.FinalizationCount, 1)
+ EXPECT_EQ(SMMW->FinalizationCount, 1)
<< "Extra call to finalize";
}
std::vector<object::ObjectFile*> Obj2Set;
Obj2Set.push_back(Obj2.getBinary());
- SectionMemoryManagerWrapper SMMW;
+ auto SMMW = std::make_shared<SectionMemoryManagerWrapper>();
NullResolver NR;
- auto H = ObjLayer.addObjectSet(std::move(Obj1Set), &SMMW, &NR);
- ObjLayer.addObjectSet(std::move(Obj2Set), &SMMW, &NR);
+ auto H = ObjLayer.addObjectSet(std::move(Obj1Set), SMMW, &NR);
+ ObjLayer.addObjectSet(std::move(Obj2Set), SMMW, &NR);
ObjLayer.emitAndFinalize(H);
-
+ ObjLayer.removeObjectSet(H);
+
// Only one call to needsToReserveAllocationSpace should have been made.
- EXPECT_EQ(SMMW.NeedsToReserveAllocationSpaceCount, 1)
+ EXPECT_EQ(SMMW->NeedsToReserveAllocationSpaceCount, 1)
<< "More than one call to needsToReserveAllocationSpace "
"(multiple unrelated objects loaded prior to finalization)";
}