30 return EPCIU.getIndirectStubs(NumStubs);
41 EPCTrampolinePool(EPCIndirectionUtils &EPCIU);
42 Error deallocatePool();
45 Error grow()
override;
47 using FinalizedAlloc = jitlink::JITLinkMemoryManager::FinalizedAlloc;
49 EPCIndirectionUtils &EPCIU;
50 unsigned TrampolineSize = 0;
51 unsigned TrampolinesPerPage = 0;
52 std::vector<FinalizedAlloc> TrampolineBlocks;
58 EPCIndirectStubsManager(EPCIndirectionUtils &EPCIU) : EPCIU(EPCIU) {}
60 Error createStub(StringRef StubName, ExecutorAddr StubAddr,
61 JITSymbolFlags StubFlags)
override;
63 Error createStubs(
const StubInitsMap &StubInits)
override;
65 ExecutorSymbolDef findStub(StringRef Name,
bool ExportedStubsOnly)
override;
67 ExecutorSymbolDef findPointer(StringRef Name)
override;
69 Error updatePointer(StringRef Name, ExecutorAddr NewAddr)
override;
72 using StubInfo = std::pair<IndirectStubInfo, JITSymbolFlags>;
75 EPCIndirectionUtils &EPCIU;
76 StringMap<StubInfo> StubInfos;
84 TrampolineSize =
ABI.getTrampolineSize();
86 (EPC.getPageSize() -
ABI.getPointerSize()) / TrampolineSize;
89Error EPCTrampolinePool::deallocatePool() {
90 std::promise<MSVCPError> DeallocResultP;
91 auto DeallocResultF = DeallocResultP.get_future();
94 DeallocResultP.set_value(std::move(Err));
97 return DeallocResultF.get();
100Error EPCTrampolinePool::grow() {
101 using namespace jitlink;
103 assert(AvailableTrampolines.empty() &&
104 "Grow called with trampolines still available");
107 assert(ResolverAddress &&
"Resolver address can not be null");
111 auto Alloc = SimpleSegmentAlloc::Create(
112 EPCIU.
getMemManager(), EPC.getSymbolStringPool(), EPC.getTargetTriple(),
113 nullptr, {{MemProt::Read | MemProt::Exec, {PageSize, Align(PageSize)}}});
115 return Alloc.takeError();
117 unsigned NumTrampolines = TrampolinesPerPage;
121 SegInfo.WorkingMem.data(), SegInfo.Addr, ResolverAddress, NumTrampolines);
122 for (
unsigned I = 0;
I < NumTrampolines; ++
I)
123 AvailableTrampolines.push_back(SegInfo.Addr + (
I * TrampolineSize));
125 auto FA =
Alloc->finalize();
127 return FA.takeError();
129 TrampolineBlocks.push_back(std::move(*FA));
134Error EPCIndirectStubsManager::createStub(StringRef StubName,
136 JITSymbolFlags StubFlags) {
138 SIM[StubName] = std::make_pair(StubAddr, StubFlags);
139 return createStubs(SIM);
142Error EPCIndirectStubsManager::createStubs(
const StubInitsMap &StubInits) {
143 auto AvailableStubInfos = getIndirectStubs(EPCIU, StubInits.size());
144 if (!AvailableStubInfos)
145 return AvailableStubInfos.takeError();
148 std::lock_guard<std::mutex> Lock(ISMMutex);
150 for (
auto &SI : StubInits) {
151 auto &
A = (*AvailableStubInfos)[ASIdx++];
152 StubInfos[
SI.first()] = std::make_pair(
A,
SI.second.second);
160 std::vector<tpctypes::UInt32Write> PtrUpdates;
161 for (
auto &SI : StubInits)
162 PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress,
163 static_cast<uint32_t
>(
SI.second.first.getValue())});
164 return MemAccess.writeUInt32s(PtrUpdates);
168 std::vector<tpctypes::UInt64Write> PtrUpdates;
169 for (
auto &SI : StubInits)
170 PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress,
171 SI.second.first.getValue()});
172 return MemAccess.writeUInt64s(PtrUpdates);
181 bool ExportedStubsOnly) {
182 std::lock_guard<std::mutex> Lock(ISMMutex);
183 auto I = StubInfos.find(Name);
184 if (
I == StubInfos.end())
186 return {
I->second.first.StubAddress,
I->second.second};
190 std::lock_guard<std::mutex> Lock(ISMMutex);
191 auto I = StubInfos.find(Name);
192 if (
I == StubInfos.end())
194 return {
I->second.first.PointerAddress,
I->second.second};
197Error EPCIndirectStubsManager::updatePointer(StringRef Name,
202 std::lock_guard<std::mutex> Lock(ISMMutex);
203 auto I = StubInfos.find(Name);
204 if (
I == StubInfos.end())
207 PtrAddr =
I->second.first.PointerAddress;
214 return MemAccess.writeUInt32s(PUpdate);
218 return MemAccess.writeUInt64s(PUpdate);
233Expected<std::unique_ptr<EPCIndirectionUtils>>
237 const auto &TT = EPC.getTargetTriple();
238 switch (TT.getArch()) {
241 std::string(
"No EPCIndirectionUtils available for ") + TT.str(),
276 auto Err = MemMgr.deallocate(std::move(IndirectStubAllocs));
280 static_cast<EPCTrampolinePool &
>(*TP).deallocatePool());
284 joinErrors(std::move(Err), MemMgr.deallocate(std::move(ResolverBlock)));
294 assert(ABI &&
"ABI can not be null");
295 auto ResolverSize = ABI->getResolverCodeSize();
297 auto Alloc = SimpleSegmentAlloc::Create(
298 MemMgr, EPC.getSymbolStringPool(), EPC.getTargetTriple(),
nullptr,
299 {{MemProt::Read | MemProt::Exec,
300 {ResolverSize, Align(EPC.getPageSize())}}});
303 return Alloc.takeError();
306 ResolverBlockAddr = SegInfo.Addr;
307 ABI->writeResolverCode(SegInfo.WorkingMem.data(), ResolverBlockAddr,
308 ReentryFnAddr, ReentryCtxAddr);
310 auto FA =
Alloc->finalize();
312 return FA.takeError();
314 ResolverBlock = std::move(*FA);
315 return ResolverBlockAddr;
318std::unique_ptr<IndirectStubsManager>
320 return std::make_unique<EPCIndirectStubsManager>(*
this);
325 TP = std::make_unique<EPCTrampolinePool>(*
this);
332 "createLazyCallThroughManager can not have been called before");
333 LCTM = std::make_unique<LazyCallThroughManager>(ES, ErrorHandlerAddr,
341 std::unique_ptr<ABISupport> ABI)
342 : EPC(EPC), MemMgr(MemMgr), MemAccess(MemAccess), ABI(
std::
move(ABI)) {
343 assert(this->ABI &&
"ABI can not be null");
346 "Stubs larger than one page are not supported");
350EPCIndirectionUtils::getIndirectStubs(
unsigned NumStubs) {
353 std::lock_guard<std::mutex> Lock(EPCUIMutex);
356 if (NumStubs > AvailableIndirectStubs.size()) {
357 auto NumStubsToAllocate = NumStubs;
359 auto StubBytes =
alignTo(NumStubsToAllocate * ABI->getStubSize(),
PageSize);
360 NumStubsToAllocate = StubBytes / ABI->getStubSize();
367 auto Alloc = SimpleSegmentAlloc::Create(
369 {{StubProt, {static_cast<size_t>(StubBytes), Align(PageSize)}},
370 {PtrProt, {static_cast<size_t>(PtrBytes), Align(PageSize)}}});
373 return Alloc.takeError();
375 auto StubSeg =
Alloc->getSegInfo(StubProt);
376 auto PtrSeg =
Alloc->getSegInfo(PtrProt);
378 ABI->writeIndirectStubsBlock(StubSeg.WorkingMem.data(), StubSeg.Addr,
379 PtrSeg.Addr, NumStubsToAllocate);
381 auto FA =
Alloc->finalize();
383 return FA.takeError();
385 IndirectStubAllocs.push_back(std::move(*FA));
387 auto StubExecutorAddr = StubSeg.Addr;
388 auto PtrExecutorAddr = PtrSeg.Addr;
389 for (
unsigned I = 0;
I != NumStubsToAllocate; ++
I) {
390 AvailableIndirectStubs.push_back(
391 IndirectStubInfo(StubExecutorAddr, PtrExecutorAddr));
392 StubExecutorAddr +=
ABI->getStubSize();
393 PtrExecutorAddr +=
ABI->getPointerSize();
397 assert(NumStubs <= AvailableIndirectStubs.size() &&
398 "Sufficient stubs should have been allocated above");
400 IndirectStubInfoVector
Result;
402 Result.push_back(AvailableIndirectStubs.back());
403 AvailableIndirectStubs.pop_back();
412 std::promise<ExecutorAddr> LandingAddrP;
413 auto LandingAddrF = LandingAddrP.get_future();
414 LCTM.resolveTrampolineLandingAddress(
416 [&](
ExecutorAddr Addr) { LandingAddrP.set_value(Addr); });
417 return LandingAddrF.get().getValue();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Manages allocations of JIT memory.
virtual void deallocate(std::vector< FinalizedAlloc > Allocs, OnDeallocatedFunction OnDeallocated)=0
Deallocate a list of allocation objects.
EPCIndirectionUtils::IndirectStubInfo IndirectStubInfo
static Expected< IndirectStubInfoVector > getIndirectStubs(EPCIndirectionUtils &EPCIU, unsigned NumStubs)
EPCIndirectionUtils::IndirectStubInfoVector IndirectStubInfoVector
virtual void writeTrampolines(char *TrampolineBlockWorkingMem, ExecutorAddr TrampolineBlockTragetAddr, ExecutorAddr ResolverAddr, unsigned NumTrampolines) const =0
unsigned getPointerSize() const
Provides ExecutorProcessControl based indirect stubs, trampoline pool and lazy call through manager.
LLVM_ABI std::unique_ptr< IndirectStubsManager > createIndirectStubsManager()
Create an IndirectStubsManager for the executor process.
LLVM_ABI Expected< ExecutorAddr > writeResolverBlock(ExecutorAddr ReentryFnAddr, ExecutorAddr ReentryCtxAddr)
Write resolver code to the executor process and return its address.
LazyCallThroughManager & getLazyCallThroughManager()
Create a LazyCallThroughManager for the executor process.
MemoryAccess & getMemoryAccess() const
Return a reference to the MemoryAccess object for this instance.
ExecutorProcessControl & getExecutorProcessControl() const
Return a reference to the ExecutorProcessControl object.
jitlink::JITLinkMemoryManager & getMemManager() const
Return a reference to the JITLinkMemoryManager object for this instance.
static LLVM_ABI Expected< std::unique_ptr< EPCIndirectionUtils > > Create(ExecutorProcessControl &EPC, jitlink::JITLinkMemoryManager &MemMgr, MemoryAccess &MemAccess)
Create based on the ExecutorProcessControl triple.
LLVM_ABI LazyCallThroughManager & createLazyCallThroughManager(ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LazyCallThroughManager.
LLVM_ABI Error cleanup()
Release memory for resources held by this instance.
LLVM_ABI TrampolinePool & getTrampolinePool()
Create a TrampolinePool for the executor process.
static std::unique_ptr< EPCIndirectionUtils > CreateWithABI(ExecutorProcessControl &EPC, jitlink::JITLinkMemoryManager &MemMgr, MemoryAccess &MemAccess)
Create using the given ABI class.
ABISupport & getABISupport() const
Return a reference to the ABISupport object for this instance.
ExecutorAddr getResolverBlockAddress() const
Returns the address of the Resolver block.
An ExecutionSession represents a running JIT program.
Represents an address in the executor process.
uint64_t getValue() const
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
ExecutorProcessControl supports interaction with a JIT target process.
const Triple & getTargetTriple() const
Return the Triple for the target process.
std::shared_ptr< SymbolStringPool > getSymbolStringPool() const
Return a shared pointer to the SymbolStringPool for this instance.
unsigned getPageSize() const
Get the page size for the target process.
Represents a defining location for a JIT symbol.
Base class for managing collections of named indirect stubs.
Manages a set of 'lazy call-through' trampolines.
APIs for manipulating memory in the target process.
Base class for pools of compiler re-entry trampolines.
UIntWrite< uint64_t > UInt64Write
Describes a write to a uint64_t.
UIntWrite< uint32_t > UInt32Write
Describes a write to a uint32_t.
LLVM_ABI Error setUpInProcessLCTMReentryViaEPCIU(EPCIndirectionUtils &EPCIU)
This will call writeResolver on the given EPCIndirectionUtils instance to set up re-entry via a funct...
static JITTargetAddress reentry(JITTargetAddress LCTMAddr, JITTargetAddress TrampolineAddr)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
T jitTargetAddressToPointer(JITTargetAddress Addr)
Convert a JITTargetAddress to a pointer.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.