Skip to content

Commit

Permalink
Fix lifetime of ClingMMapper
Browse files Browse the repository at this point in the history
The ClingMMapper must remain available until all ClingMemoryManagers
are destructed, which is typically during shutdown of IncrementalJIT.
This was not the case for the global object MMapperInstance that was
introduced during the upgrade to LLVM 13 because libCling variables
are destructed before running TROOT atexit handlers that shut down
the JIT. In practice, it happened to work but this will change with
the upgrade to LLVM 18 where we see crashes in some tests, potentially
because of upstream commit
llvm/llvm-project@47f5c54

See also commits e0f6c04660 ("Prevent static destruction from ending
DefaultMMapper too early") and 80c14bb948 ("Extend lifetime of
SectionMemoryManager::DefaultMMapper, again") for the same problem
that we previously patched in our copy of LLVM.
  • Loading branch information
hahnjo authored and jenkins committed Aug 27, 2024
1 parent 76f5e99 commit a6f2255
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/Interpreter/IncrementalJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ namespace {
}
};

ClingMMapper MMapperInstance;

// A memory manager for Cling that reserves memory for code and data sections
// to keep them contiguous for the emission of one module. This is required
// for working exception handling support since one .eh_frame section will
Expand Down Expand Up @@ -133,7 +131,7 @@ namespace {
AllocInfo m_RWData;

public:
ClingMemoryManager() : Super(&MMapperInstance) {}
ClingMemoryManager(ClingMMapper& MMapper) : Super(&MMapper) {}

uint8_t* allocateCodeSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID,
Expand Down Expand Up @@ -474,7 +472,10 @@ IncrementalJIT::IncrementalJIT(
return ObjLinkingLayer;
}

auto GetMemMgr = []() { return std::make_unique<ClingMemoryManager>(); };
auto MMapper = std::make_unique<ClingMMapper>();
auto GetMemMgr = [MMapper = std::move(MMapper)]() {
return std::make_unique<ClingMemoryManager>(*MMapper);
};
auto Layer =
std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));

Expand Down

0 comments on commit a6f2255

Please sign in to comment.