29 return EPCIU.getIndirectStubs(NumStubs);
41 Error deallocatePool();
49 unsigned TrampolineSize = 0;
50 unsigned TrampolinesPerPage = 0;
51 std::vector<FinalizedAlloc> TrampolineBlocks;
59 Error deallocateStubs();
73 using StubInfo = std::pair<IndirectStubInfo, JITSymbolFlags>;
85 TrampolineSize =
ABI.getTrampolineSize();
87 (EPC.getPageSize() -
ABI.getPointerSize()) / TrampolineSize;
90Error EPCTrampolinePool::deallocatePool() {
91 std::promise<MSVCPError> DeallocResultP;
92 auto DeallocResultF = DeallocResultP.get_future();
95 std::move(TrampolineBlocks),
96 [&](
Error Err) { DeallocResultP.set_value(std::move(Err)); });
98 return DeallocResultF.get();
101Error EPCTrampolinePool::grow() {
102 using namespace jitlink;
104 assert(AvailableTrampolines.empty() &&
105 "Grow called with trampolines still available");
108 assert(ResolverAddress &&
"Resolver address can not be null");
112 auto Alloc = SimpleSegmentAlloc::Create(
113 EPC.getMemMgr(), EPC.getSymbolStringPool(),
nullptr,
114 {{MemProt::Read | MemProt::Exec, {PageSize, Align(PageSize)}}});
116 return Alloc.takeError();
118 unsigned NumTrampolines = TrampolinesPerPage;
122 SegInfo.WorkingMem.data(), SegInfo.Addr, ResolverAddress, NumTrampolines);
123 for (
unsigned I = 0;
I < NumTrampolines; ++
I)
124 AvailableTrampolines.push_back(SegInfo.Addr + (
I * TrampolineSize));
126 auto FA =
Alloc->finalize();
128 return FA.takeError();
130 TrampolineBlocks.push_back(std::move(*FA));
139 SIM[StubName] = std::make_pair(StubAddr, StubFlags);
140 return createStubs(SIM);
143Error EPCIndirectStubsManager::createStubs(
const StubInitsMap &StubInits) {
144 auto AvailableStubInfos = getIndirectStubs(EPCIU, StubInits.size());
145 if (!AvailableStubInfos)
146 return AvailableStubInfos.takeError();
149 std::lock_guard<std::mutex> Lock(ISMMutex);
151 for (
auto &SI : StubInits) {
152 auto &
A = (*AvailableStubInfos)[ASIdx++];
153 StubInfos[
SI.first()] = std::make_pair(
A,
SI.second.second);
161 std::vector<tpctypes::UInt32Write> PtrUpdates;
162 for (
auto &SI : StubInits)
163 PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress,
164 static_cast<uint32_t>(
SI.second.first.getValue())});
165 return MemAccess.writeUInt32s(PtrUpdates);
169 std::vector<tpctypes::UInt64Write> PtrUpdates;
170 for (
auto &SI : StubInits)
171 PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress,
172 static_cast<uint64_t>(
SI.second.first.getValue())});
173 return MemAccess.writeUInt64s(PtrUpdates);
176 return make_error<StringError>(
"Unsupported pointer size",
182 bool ExportedStubsOnly) {
183 std::lock_guard<std::mutex> Lock(ISMMutex);
184 auto I = StubInfos.find(
Name);
185 if (
I == StubInfos.end())
187 return {
I->second.first.StubAddress,
I->second.second};
191 std::lock_guard<std::mutex> Lock(ISMMutex);
192 auto I = StubInfos.find(
Name);
193 if (
I == StubInfos.end())
195 return {
I->second.first.PointerAddress,
I->second.second};
203 std::lock_guard<std::mutex> Lock(ISMMutex);
204 auto I = StubInfos.find(
Name);
205 if (
I == StubInfos.end())
206 return make_error<StringError>(
"Unknown stub name",
208 PtrAddr =
I->second.first.PointerAddress;
215 return MemAccess.writeUInt32s(PUpdate);
219 return MemAccess.writeUInt64s(PUpdate);
222 return make_error<StringError>(
"Unsupported pointer size",
237 switch (TT.getArch()) {
239 return make_error<StringError>(
240 std::string(
"No EPCIndirectionUtils available for ") + TT.str(),
244 return CreateWithABI<OrcAArch64>(EPC);
247 return CreateWithABI<OrcI386>(EPC);
250 return CreateWithABI<OrcLoongArch64>(EPC);
253 return CreateWithABI<OrcMips32Be>(EPC);
256 return CreateWithABI<OrcMips32Le>(EPC);
260 return CreateWithABI<OrcMips64>(EPC);
263 return CreateWithABI<OrcRiscv64>(EPC);
266 if (TT.getOS() == Triple::OSType::Win32)
267 return CreateWithABI<OrcX86_64_Win32>(EPC);
269 return CreateWithABI<OrcX86_64_SysV>(EPC);
275 auto &MemMgr = EPC.getMemMgr();
276 auto Err = MemMgr.deallocate(std::move(IndirectStubAllocs));
280 static_cast<EPCTrampolinePool &
>(*TP).deallocatePool());
284 joinErrors(std::move(Err), MemMgr.deallocate(std::move(ResolverBlock)));
292 using namespace jitlink;
294 assert(ABI &&
"ABI can not be null");
295 auto ResolverSize = ABI->getResolverCodeSize();
297 auto Alloc = SimpleSegmentAlloc::Create(
298 EPC.getMemMgr(), EPC.getSymbolStringPool(),
nullptr,
299 {{MemProt::Read | MemProt::Exec,
300 {ResolverSize, Align(EPC.getPageSize())}}});
303 return Alloc.takeError();
305 auto SegInfo = Alloc->getSegInfo(MemProt::Read | MemProt::Exec);
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,
334 &getTrampolinePool());
339 std::unique_ptr<ABISupport> ABI)
340 : EPC(EPC), ABI(
std::
move(ABI)) {
341 assert(this->ABI &&
"ABI can not be null");
344 "Stubs larger than one page are not supported");
348EPCIndirectionUtils::getIndirectStubs(
unsigned NumStubs) {
349 using namespace jitlink;
351 std::lock_guard<std::mutex> Lock(EPCUIMutex);
354 if (NumStubs > AvailableIndirectStubs.size()) {
355 auto NumStubsToAllocate = NumStubs;
357 auto StubBytes =
alignTo(NumStubsToAllocate * ABI->getStubSize(),
PageSize);
358 NumStubsToAllocate = StubBytes / ABI->getStubSize();
365 auto Alloc = SimpleSegmentAlloc::Create(
367 {{StubProt, {static_cast<size_t>(StubBytes), Align(PageSize)}},
368 {PtrProt, {static_cast<size_t>(PtrBytes), Align(PageSize)}}});
371 return Alloc.takeError();
373 auto StubSeg =
Alloc->getSegInfo(StubProt);
374 auto PtrSeg =
Alloc->getSegInfo(PtrProt);
376 ABI->writeIndirectStubsBlock(StubSeg.WorkingMem.data(), StubSeg.Addr,
377 PtrSeg.Addr, NumStubsToAllocate);
379 auto FA =
Alloc->finalize();
381 return FA.takeError();
383 IndirectStubAllocs.push_back(std::move(*FA));
385 auto StubExecutorAddr = StubSeg.Addr;
386 auto PtrExecutorAddr = PtrSeg.Addr;
387 for (
unsigned I = 0;
I != NumStubsToAllocate; ++
I) {
388 AvailableIndirectStubs.push_back(
389 IndirectStubInfo(StubExecutorAddr, PtrExecutorAddr));
390 StubExecutorAddr +=
ABI->getStubSize();
391 PtrExecutorAddr +=
ABI->getPointerSize();
395 assert(NumStubs <= AvailableIndirectStubs.size() &&
396 "Sufficient stubs should have been allocated above");
398 IndirectStubInfoVector
Result;
400 Result.push_back(AvailableIndirectStubs.back());
401 AvailableIndirectStubs.pop_back();
409 auto &LCTM = *jitTargetAddressToPointer<LazyCallThroughManager *>(LCTMAddr);
410 std::promise<ExecutorAddr> LandingAddrP;
411 auto LandingAddrF = LandingAddrP.get_future();
412 LCTM.resolveTrampolineLandingAddress(
415 return LandingAddrF.get().getValue();
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
Flags for symbols in the JIT.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
Represents a finalized allocation.
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.
std::unique_ptr< IndirectStubsManager > createIndirectStubsManager()
Create an IndirectStubsManager for the executor process.
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.
ExecutorProcessControl & getExecutorProcessControl() const
Return a reference to the ExecutorProcessControl object.
LazyCallThroughManager & createLazyCallThroughManager(ExecutionSession &ES, ExecutorAddr ErrorHandlerAddr)
Create a LazyCallThroughManager.
Error cleanup()
Release memory for resources held by this instance.
TrampolinePool & getTrampolinePool()
Create a TrampolinePool for the executor process.
static Expected< std::unique_ptr< EPCIndirectionUtils > > Create(ExecutorProcessControl &EPC)
Create based on the ExecutorProcessControl triple.
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.
jitlink::JITLinkMemoryManager & getMemMgr() const
Return a JITLinkMemoryManager for the 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.
MemoryAccess & getMemoryAccess() const
Return a MemoryAccess object for the target process.
Represents a defining location for a JIT symbol.
Base class for managing collections of named indirect stubs.
virtual Error createStub(StringRef StubName, ExecutorAddr StubAddr, JITSymbolFlags StubFlags)=0
Create a single stub with the given name, target address and flags.
virtual ExecutorSymbolDef findStub(StringRef Name, bool ExportedStubsOnly)=0
Find the stub with the given name.
virtual ExecutorSymbolDef findPointer(StringRef Name)=0
Find the implementation-pointer for the stub.
virtual Error updatePointer(StringRef Name, ExecutorAddr NewAddr)=0
Change the value of the implementation pointer for the stub.
virtual Error createStubs(const StubInitsMap &StubInits)=0
Create StubInits.size() stubs with the given names, target addresses, and flags.
Manages a set of 'lazy call-through' trampolines.
Base class for pools of compiler re-entry trampolines.
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.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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.