| 1 | //===---------- LazyReexports.cpp - Utilities for lazy reexports ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ExecutionEngine/Orc/LazyReexports.h" |
| 10 | |
| 11 | #include "llvm/ExecutionEngine/Orc/BindCallControllerHandler.h" |
| 12 | #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" |
| 13 | #include "llvm/ExecutionEngine/Orc/OrcABISupport.h" |
| 14 | #include "llvm/ExecutionEngine/Orc/SPSHandlerSpec.h" |
| 15 | #include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h" |
| 16 | #include "llvm/TargetParser/Triple.h" |
| 17 | |
| 18 | #define DEBUG_TYPE "orc" |
| 19 | |
| 20 | namespace llvm { |
| 21 | namespace orc { |
| 22 | |
| 23 | LazyCallThroughManager::LazyCallThroughManager(ExecutionSession &ES, |
| 24 | ExecutorAddr ErrorHandlerAddr, |
| 25 | TrampolinePool *TP) |
| 26 | : ES(ES), ErrorHandlerAddr(ErrorHandlerAddr), TP(TP) {} |
| 27 | |
| 28 | Expected<ExecutorAddr> LazyCallThroughManager::getCallThroughTrampoline( |
| 29 | JITDylib &SourceJD, SymbolStringPtr SymbolName, |
| 30 | NotifyResolvedFunction NotifyResolved) { |
| 31 | assert(TP && "TrampolinePool not set" ); |
| 32 | |
| 33 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
| 34 | auto Trampoline = TP->getTrampoline(); |
| 35 | |
| 36 | if (!Trampoline) |
| 37 | return Trampoline.takeError(); |
| 38 | |
| 39 | Reexports[*Trampoline] = ReexportsEntry{.SourceJD: &SourceJD, .SymbolName: std::move(SymbolName)}; |
| 40 | Notifiers[*Trampoline] = std::move(NotifyResolved); |
| 41 | return *Trampoline; |
| 42 | } |
| 43 | |
| 44 | ExecutorAddr LazyCallThroughManager::reportCallThroughError(Error Err) { |
| 45 | ES.reportError(Err: std::move(Err)); |
| 46 | return ErrorHandlerAddr; |
| 47 | } |
| 48 | |
| 49 | Expected<LazyCallThroughManager::ReexportsEntry> |
| 50 | LazyCallThroughManager::findReexport(ExecutorAddr TrampolineAddr) { |
| 51 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
| 52 | auto I = Reexports.find(x: TrampolineAddr); |
| 53 | if (I == Reexports.end()) |
| 54 | return createStringError(EC: inconvertibleErrorCode(), |
| 55 | S: "Missing reexport for trampoline address %p" + |
| 56 | formatv(Fmt: "{0:x}" , Vals&: TrampolineAddr)); |
| 57 | return I->second; |
| 58 | } |
| 59 | |
| 60 | Error LazyCallThroughManager::notifyResolved(ExecutorAddr TrampolineAddr, |
| 61 | ExecutorAddr ResolvedAddr) { |
| 62 | NotifyResolvedFunction NotifyResolved; |
| 63 | { |
| 64 | std::lock_guard<std::mutex> Lock(LCTMMutex); |
| 65 | auto I = Notifiers.find(x: TrampolineAddr); |
| 66 | if (I != Notifiers.end()) { |
| 67 | NotifyResolved = std::move(I->second); |
| 68 | Notifiers.erase(position: I); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return NotifyResolved ? NotifyResolved(ResolvedAddr) : Error::success(); |
| 73 | } |
| 74 | |
| 75 | void LazyCallThroughManager::resolveTrampolineLandingAddress( |
| 76 | ExecutorAddr TrampolineAddr, |
| 77 | NotifyLandingResolvedFunction NotifyLandingResolved) { |
| 78 | |
| 79 | auto Entry = findReexport(TrampolineAddr); |
| 80 | if (!Entry) |
| 81 | return NotifyLandingResolved(reportCallThroughError(Err: Entry.takeError())); |
| 82 | |
| 83 | // Declaring SLS and the callback outside of the call to ES.lookup is a |
| 84 | // workaround to fix build failures on AIX and on z/OS platforms. |
| 85 | SymbolLookupSet SLS({Entry->SymbolName}); |
| 86 | auto Callback = [this, TrampolineAddr, SymbolName = Entry->SymbolName, |
| 87 | NotifyLandingResolved = std::move(NotifyLandingResolved)]( |
| 88 | Expected<SymbolMap> Result) mutable { |
| 89 | if (Result) { |
| 90 | assert(Result->size() == 1 && "Unexpected result size" ); |
| 91 | assert(Result->count(SymbolName) && "Unexpected result value" ); |
| 92 | ExecutorAddr LandingAddr = (*Result)[SymbolName].getAddress(); |
| 93 | |
| 94 | if (auto Err = notifyResolved(TrampolineAddr, ResolvedAddr: LandingAddr)) |
| 95 | NotifyLandingResolved(reportCallThroughError(Err: std::move(Err))); |
| 96 | else |
| 97 | NotifyLandingResolved(LandingAddr); |
| 98 | } else { |
| 99 | NotifyLandingResolved(reportCallThroughError(Err: Result.takeError())); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | ES.lookup(K: LookupKind::Static, |
| 104 | SearchOrder: makeJITDylibSearchOrder(JDs: Entry->SourceJD, |
| 105 | Flags: JITDylibLookupFlags::MatchAllSymbols), |
| 106 | Symbols: std::move(SLS), RequiredState: SymbolState::Ready, NotifyComplete: std::move(Callback), |
| 107 | RegisterDependencies: NoDependenciesToRegister); |
| 108 | } |
| 109 | |
| 110 | Expected<std::unique_ptr<LazyCallThroughManager>> |
| 111 | createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, |
| 112 | ExecutorAddr ErrorHandlerAddr) { |
| 113 | switch (T.getArch()) { |
| 114 | default: |
| 115 | return make_error<StringError>( |
| 116 | Args: std::string("No callback manager available for " ) + T.str(), |
| 117 | Args: inconvertibleErrorCode()); |
| 118 | |
| 119 | case Triple::aarch64: |
| 120 | case Triple::aarch64_32: |
| 121 | return LocalLazyCallThroughManager::Create<OrcAArch64>(ES, |
| 122 | ErrorHandlerAddr); |
| 123 | |
| 124 | case Triple::x86: |
| 125 | return LocalLazyCallThroughManager::Create<OrcI386>(ES, ErrorHandlerAddr); |
| 126 | |
| 127 | case Triple::loongarch64: |
| 128 | return LocalLazyCallThroughManager::Create<OrcLoongArch64>( |
| 129 | ES, ErrorHandlerAddr); |
| 130 | |
| 131 | case Triple::mips: |
| 132 | return LocalLazyCallThroughManager::Create<OrcMips32Be>(ES, |
| 133 | ErrorHandlerAddr); |
| 134 | |
| 135 | case Triple::mipsel: |
| 136 | return LocalLazyCallThroughManager::Create<OrcMips32Le>(ES, |
| 137 | ErrorHandlerAddr); |
| 138 | |
| 139 | case Triple::mips64: |
| 140 | case Triple::mips64el: |
| 141 | return LocalLazyCallThroughManager::Create<OrcMips64>(ES, ErrorHandlerAddr); |
| 142 | |
| 143 | case Triple::riscv64: |
| 144 | return LocalLazyCallThroughManager::Create<OrcRiscv64>(ES, |
| 145 | ErrorHandlerAddr); |
| 146 | |
| 147 | case Triple::x86_64: |
| 148 | if (T.getOS() == Triple::OSType::Win32) |
| 149 | return LocalLazyCallThroughManager::Create<OrcX86_64_Win32>( |
| 150 | ES, ErrorHandlerAddr); |
| 151 | else |
| 152 | return LocalLazyCallThroughManager::Create<OrcX86_64_SysV>( |
| 153 | ES, ErrorHandlerAddr); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | LazyReexportsMaterializationUnit::LazyReexportsMaterializationUnit( |
| 158 | LazyCallThroughManager &LCTManager, RedirectableSymbolManager &RSManager, |
| 159 | JITDylib &SourceJD, SymbolAliasMap CallableAliases, ImplSymbolMap *SrcJDLoc) |
| 160 | : MaterializationUnit(extractFlags(Aliases: CallableAliases)), |
| 161 | LCTManager(LCTManager), RSManager(RSManager), SourceJD(SourceJD), |
| 162 | CallableAliases(std::move(CallableAliases)), AliaseeTable(SrcJDLoc) {} |
| 163 | |
| 164 | StringRef LazyReexportsMaterializationUnit::getName() const { |
| 165 | return "<Lazy Reexports>" ; |
| 166 | } |
| 167 | |
| 168 | void LazyReexportsMaterializationUnit::materialize( |
| 169 | std::unique_ptr<MaterializationResponsibility> R) { |
| 170 | auto RequestedSymbols = R->getRequestedSymbols(); |
| 171 | |
| 172 | SymbolAliasMap RequestedAliases; |
| 173 | for (auto &RequestedSymbol : RequestedSymbols) { |
| 174 | auto I = CallableAliases.find(Val: RequestedSymbol); |
| 175 | assert(I != CallableAliases.end() && "Symbol not found in alias map?" ); |
| 176 | RequestedAliases[I->first] = std::move(I->second); |
| 177 | CallableAliases.erase(I); |
| 178 | } |
| 179 | |
| 180 | if (!CallableAliases.empty()) |
| 181 | if (auto Err = R->replace(MU: lazyReexports(LCTManager, RSManager, SourceJD, |
| 182 | CallableAliases: std::move(CallableAliases), |
| 183 | SrcJDLoc: AliaseeTable))) { |
| 184 | R->getExecutionSession().reportError(Err: std::move(Err)); |
| 185 | R->failMaterialization(); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | SymbolMap Inits; |
| 190 | for (auto &Alias : RequestedAliases) { |
| 191 | auto CallThroughTrampoline = LCTManager.getCallThroughTrampoline( |
| 192 | SourceJD, SymbolName: Alias.second.Aliasee, |
| 193 | NotifyResolved: [&TargetJD = R->getTargetJITDylib(), &RSManager = this->RSManager, |
| 194 | StubSym = Alias.first](ExecutorAddr ResolvedAddr) -> Error { |
| 195 | return RSManager.redirect(JD&: TargetJD, Symbol: StubSym, |
| 196 | NewDest: ExecutorSymbolDef(ResolvedAddr, {})); |
| 197 | }); |
| 198 | |
| 199 | if (!CallThroughTrampoline) { |
| 200 | R->getExecutionSession().reportError(Err: CallThroughTrampoline.takeError()); |
| 201 | R->failMaterialization(); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | Inits[Alias.first] = {*CallThroughTrampoline, Alias.second.AliasFlags}; |
| 206 | } |
| 207 | |
| 208 | if (AliaseeTable != nullptr && !RequestedAliases.empty()) |
| 209 | AliaseeTable->trackImpls(ImplMaps: RequestedAliases, SrcJD: &SourceJD); |
| 210 | |
| 211 | if (auto Err = R->replace(MU: std::make_unique<RedirectableMaterializationUnit>( |
| 212 | args&: RSManager, args: std::move(Inits)))) { |
| 213 | R->getExecutionSession().reportError(Err: std::move(Err)); |
| 214 | return R->failMaterialization(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void LazyReexportsMaterializationUnit::discard(const JITDylib &JD, |
| 219 | const SymbolStringPtr &Name) { |
| 220 | assert(CallableAliases.count(Name) && |
| 221 | "Symbol not covered by this MaterializationUnit" ); |
| 222 | CallableAliases.erase(Val: Name); |
| 223 | } |
| 224 | |
| 225 | MaterializationUnit::Interface |
| 226 | LazyReexportsMaterializationUnit::(const SymbolAliasMap &Aliases) { |
| 227 | SymbolFlagsMap SymbolFlags; |
| 228 | for (auto &KV : Aliases) { |
| 229 | assert(KV.second.AliasFlags.isCallable() && |
| 230 | "Lazy re-exports must be callable symbols" ); |
| 231 | SymbolFlags[KV.first] = KV.second.AliasFlags; |
| 232 | } |
| 233 | return MaterializationUnit::Interface(std::move(SymbolFlags), nullptr); |
| 234 | } |
| 235 | |
| 236 | class LazyReexportsManager::MU : public MaterializationUnit { |
| 237 | public: |
| 238 | MU(LazyReexportsManager &LRMgr, SymbolAliasMap Reexports) |
| 239 | : MaterializationUnit(getInterface(Reexports)), LRMgr(LRMgr), |
| 240 | Reexports(std::move(Reexports)) {} |
| 241 | |
| 242 | private: |
| 243 | Interface getInterface(const SymbolAliasMap &Reexports) { |
| 244 | SymbolFlagsMap SF; |
| 245 | for (auto &[Alias, AI] : Reexports) |
| 246 | SF[Alias] = AI.AliasFlags; |
| 247 | return {std::move(SF), nullptr}; |
| 248 | } |
| 249 | |
| 250 | StringRef getName() const override { return "LazyReexportsManager::MU" ; } |
| 251 | |
| 252 | void materialize(std::unique_ptr<MaterializationResponsibility> R) override { |
| 253 | LRMgr.emitReentryTrampolines(MR: std::move(R), Reexports: std::move(Reexports)); |
| 254 | } |
| 255 | |
| 256 | void discard(const JITDylib &JD, const SymbolStringPtr &Name) override { |
| 257 | Reexports.erase(Val: Name); |
| 258 | } |
| 259 | |
| 260 | LazyReexportsManager &LRMgr; |
| 261 | SymbolAliasMap Reexports; |
| 262 | }; |
| 263 | |
| 264 | class LazyReexportsManager::Plugin : public ObjectLinkingLayer::Plugin { |
| 265 | public: |
| 266 | void modifyPassConfig(MaterializationResponsibility &MR, |
| 267 | jitlink::LinkGraph &G, |
| 268 | jitlink::PassConfiguration &Config) override {} |
| 269 | |
| 270 | Error notifyFailed(MaterializationResponsibility &MR) override { |
| 271 | return Error::success(); |
| 272 | } |
| 273 | |
| 274 | Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override { |
| 275 | return Error::success(); |
| 276 | } |
| 277 | |
| 278 | void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, |
| 279 | ResourceKey SrcKey) override {} |
| 280 | }; |
| 281 | |
| 282 | LazyReexportsManager::Listener::~Listener() = default; |
| 283 | |
| 284 | Expected<std::unique_ptr<LazyReexportsManager>> |
| 285 | LazyReexportsManager::Create(EmitTrampolinesFn EmitTrampolines, |
| 286 | RedirectableSymbolManager &RSMgr, |
| 287 | JITDylib &PlatformJD, Listener *L) { |
| 288 | Error Err = Error::success(); |
| 289 | std::unique_ptr<LazyReexportsManager> LRM(new LazyReexportsManager( |
| 290 | std::move(EmitTrampolines), RSMgr, PlatformJD, L, Err)); |
| 291 | if (Err) |
| 292 | return std::move(Err); |
| 293 | return std::move(LRM); |
| 294 | } |
| 295 | |
| 296 | Error LazyReexportsManager::handleRemoveResources(JITDylib &JD, ResourceKey K) { |
| 297 | return JD.getExecutionSession().runSessionLocked(F: [&]() -> Error { |
| 298 | auto I = KeyToReentryAddrs.find(Val: K); |
| 299 | if (I == KeyToReentryAddrs.end()) |
| 300 | return Error::success(); |
| 301 | |
| 302 | auto &ReentryAddrs = I->second; |
| 303 | for (auto &ReentryAddr : ReentryAddrs) { |
| 304 | assert(CallThroughs.count(ReentryAddr) && "CallTrhough missing" ); |
| 305 | CallThroughs.erase(Val: ReentryAddr); |
| 306 | } |
| 307 | KeyToReentryAddrs.erase(I); |
| 308 | return L ? L->onLazyReexportsRemoved(JD, K) : Error::success(); |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | void LazyReexportsManager::handleTransferResources(JITDylib &JD, |
| 313 | ResourceKey DstK, |
| 314 | ResourceKey SrcK) { |
| 315 | auto I = KeyToReentryAddrs.find(Val: SrcK); |
| 316 | if (I != KeyToReentryAddrs.end()) { |
| 317 | auto J = KeyToReentryAddrs.find(Val: DstK); |
| 318 | if (J == KeyToReentryAddrs.end()) { |
| 319 | auto Tmp = std::move(I->second); |
| 320 | KeyToReentryAddrs.erase(I); |
| 321 | KeyToReentryAddrs[DstK] = std::move(Tmp); |
| 322 | } else { |
| 323 | auto &SrcAddrs = I->second; |
| 324 | auto &DstAddrs = J->second; |
| 325 | llvm::append_range(C&: DstAddrs, R&: SrcAddrs); |
| 326 | KeyToReentryAddrs.erase(I); |
| 327 | } |
| 328 | if (L) |
| 329 | L->onLazyReexportsTransfered(JD, DstK, SrcK); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Spec for the call-controller handler that the ORC runtime calls to |
| 334 | // resolve lazy reexports. |
| 335 | namespace lazy_reexports_sps_handlers { |
| 336 | struct Resolve |
| 337 | : sps::HandlerSpec<shared::SPSExpected<shared::SPSExecutorSymbolDef>( |
| 338 | shared::SPSExecutorAddr)> { |
| 339 | static constexpr SymbolNameSpec Name = |
| 340 | SymbolNameSpec::c(Name: "__orc_rt_resolve_tag" ); |
| 341 | }; |
| 342 | } // namespace lazy_reexports_sps_handlers |
| 343 | |
| 344 | LazyReexportsManager::LazyReexportsManager(EmitTrampolinesFn EmitTrampolines, |
| 345 | RedirectableSymbolManager &RSMgr, |
| 346 | JITDylib &PlatformJD, Listener *L, |
| 347 | Error &Err) |
| 348 | : ES(PlatformJD.getExecutionSession()), |
| 349 | EmitTrampolines(std::move(EmitTrampolines)), RSMgr(RSMgr), L(L) { |
| 350 | |
| 351 | using namespace shared; |
| 352 | ErrorAsOutParameter _(&Err); |
| 353 | Err = ES.registerCallControllerHandlers( |
| 354 | JD&: PlatformJD, |
| 355 | Hs: bindCallControllerHandler<lazy_reexports_sps_handlers::Resolve>( |
| 356 | Instance: this, Method: &LazyReexportsManager::resolve)); |
| 357 | } |
| 358 | |
| 359 | std::unique_ptr<MaterializationUnit> |
| 360 | LazyReexportsManager::createLazyReexports(SymbolAliasMap Reexports) { |
| 361 | return std::make_unique<MU>(args&: *this, args: std::move(Reexports)); |
| 362 | } |
| 363 | |
| 364 | void LazyReexportsManager::emitReentryTrampolines( |
| 365 | std::unique_ptr<MaterializationResponsibility> MR, |
| 366 | SymbolAliasMap Reexports) { |
| 367 | size_t NumTrampolines = Reexports.size(); |
| 368 | auto RT = MR->getResourceTracker(); |
| 369 | EmitTrampolines( |
| 370 | std::move(RT), NumTrampolines, |
| 371 | [this, MR = std::move(MR), Reexports = std::move(Reexports)]( |
| 372 | Expected<std::vector<ExecutorSymbolDef>> ReentryPoints) mutable { |
| 373 | emitRedirectableSymbols(MR: std::move(MR), Reexports: std::move(Reexports), |
| 374 | ReentryPoints: std::move(ReentryPoints)); |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | void LazyReexportsManager::emitRedirectableSymbols( |
| 379 | std::unique_ptr<MaterializationResponsibility> MR, SymbolAliasMap Reexports, |
| 380 | Expected<std::vector<ExecutorSymbolDef>> ReentryPoints) { |
| 381 | |
| 382 | if (!ReentryPoints) { |
| 383 | MR->getExecutionSession().reportError(Err: ReentryPoints.takeError()); |
| 384 | MR->failMaterialization(); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | assert(Reexports.size() == ReentryPoints->size() && |
| 389 | "Number of reentry points doesn't match number of reexports" ); |
| 390 | |
| 391 | // Bind entry points to names. |
| 392 | SymbolMap Redirs; |
| 393 | size_t I = 0; |
| 394 | for (auto &[Name, AI] : Reexports) |
| 395 | Redirs[Name] = {(*ReentryPoints)[I++].getAddress(), AI.AliasFlags}; |
| 396 | |
| 397 | I = 0; |
| 398 | if (!Reexports.empty()) { |
| 399 | if (auto Err = MR->withResourceKeyDo(F: [&](ResourceKey K) { |
| 400 | auto &JD = MR->getTargetJITDylib(); |
| 401 | auto &ReentryAddrsForK = KeyToReentryAddrs[K]; |
| 402 | for (auto &[Name, AI] : Reexports) { |
| 403 | const auto &ReentryPoint = (*ReentryPoints)[I++]; |
| 404 | CallThroughs[ReentryPoint.getAddress()] = {.JD: &JD, .Name: Name, .BodyName: AI.Aliasee}; |
| 405 | ReentryAddrsForK.push_back(x: ReentryPoint.getAddress()); |
| 406 | } |
| 407 | if (L) |
| 408 | L->onLazyReexportsCreated(JD, K, Reexports); |
| 409 | })) { |
| 410 | MR->getExecutionSession().reportError(Err: std::move(Err)); |
| 411 | MR->failMaterialization(); |
| 412 | return; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | RSMgr.emitRedirectableSymbols(MR: std::move(MR), InitialDests: std::move(Redirs)); |
| 417 | } |
| 418 | |
| 419 | void LazyReexportsManager::resolve(ResolveSendResultFn SendResult, |
| 420 | ExecutorAddr ReentryStubAddr) { |
| 421 | |
| 422 | CallThroughInfo LandingInfo; |
| 423 | |
| 424 | ES.runSessionLocked(F: [&]() { |
| 425 | auto I = CallThroughs.find(Val: ReentryStubAddr); |
| 426 | if (I == CallThroughs.end()) |
| 427 | return SendResult(make_error<StringError>( |
| 428 | Args: "Reentry address " + formatv(Fmt: "{0:x}" , Vals&: ReentryStubAddr) + |
| 429 | " not registered" , |
| 430 | Args: inconvertibleErrorCode())); |
| 431 | LandingInfo = I->second; |
| 432 | }); |
| 433 | |
| 434 | if (L) |
| 435 | L->onLazyReexportCalled(CTI: LandingInfo); |
| 436 | |
| 437 | ES.lookup( |
| 438 | K: LookupKind::Static, |
| 439 | SearchOrder: {{LandingInfo.JD.get(), JITDylibLookupFlags::MatchAllSymbols}}, |
| 440 | Symbols: SymbolLookupSet(std::move(LandingInfo.BodyName)), RequiredState: SymbolState::Ready, |
| 441 | NotifyComplete: [this, JD = std::move(LandingInfo.JD), |
| 442 | ReentryName = std::move(LandingInfo.Name), |
| 443 | SendResult = std::move(SendResult)](Expected<SymbolMap> Result) mutable { |
| 444 | if (!Result) |
| 445 | return SendResult(Result.takeError()); |
| 446 | |
| 447 | assert(Result->size() == 1 && "Unexpected number of results" ); |
| 448 | auto BodySym = Result->begin()->second; |
| 449 | if (auto Err = RSMgr.redirect(JD&: *JD, Symbol: ReentryName, NewDest: BodySym)) |
| 450 | SendResult(std::move(Err)); |
| 451 | SendResult(BodySym); |
| 452 | }, |
| 453 | RegisterDependencies: NoDependenciesToRegister); |
| 454 | } |
| 455 | |
| 456 | class SimpleLazyReexportsSpeculator::SpeculateTask : public IdleTask { |
| 457 | public: |
| 458 | SpeculateTask(std::weak_ptr<SimpleLazyReexportsSpeculator> Speculator) |
| 459 | : Speculator(std::move(Speculator)) {} |
| 460 | |
| 461 | void printDescription(raw_ostream &OS) override { |
| 462 | OS << "Speculative Lookup Task" ; |
| 463 | } |
| 464 | |
| 465 | void run() override { |
| 466 | if (auto S = Speculator.lock()) |
| 467 | S->doNextSpeculativeLookup(); |
| 468 | } |
| 469 | |
| 470 | private: |
| 471 | std::weak_ptr<SimpleLazyReexportsSpeculator> Speculator; |
| 472 | }; |
| 473 | |
| 474 | SimpleLazyReexportsSpeculator::~SimpleLazyReexportsSpeculator() { |
| 475 | for (auto &[JD, _] : LazyReexports) |
| 476 | JITDylibSP(JD)->Release(); |
| 477 | } |
| 478 | |
| 479 | void SimpleLazyReexportsSpeculator::onLazyReexportsCreated( |
| 480 | JITDylib &JD, ResourceKey K, const SymbolAliasMap &Reexports) { |
| 481 | if (!LazyReexports.count(Val: &JD)) |
| 482 | JD.Retain(); |
| 483 | auto &BodiesVec = LazyReexports[&JD][K]; |
| 484 | for (auto &[Name, AI] : Reexports) |
| 485 | BodiesVec.push_back(x: AI.Aliasee); |
| 486 | if (!SpeculateTaskActive) { |
| 487 | SpeculateTaskActive = true; |
| 488 | ES.dispatchTask(T: std::make_unique<SpeculateTask>(args&: WeakThis)); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void SimpleLazyReexportsSpeculator::onLazyReexportsTransfered( |
| 493 | JITDylib &JD, ResourceKey DstK, ResourceKey SrcK) { |
| 494 | |
| 495 | auto I = LazyReexports.find(Val: &JD); |
| 496 | if (I == LazyReexports.end()) |
| 497 | return; |
| 498 | |
| 499 | auto &MapForJD = I->second; |
| 500 | auto J = MapForJD.find(Val: SrcK); |
| 501 | if (J == MapForJD.end()) |
| 502 | return; |
| 503 | |
| 504 | // We have something to transfer. |
| 505 | auto K = MapForJD.find(Val: DstK); |
| 506 | if (K == MapForJD.end()) { |
| 507 | auto Tmp = std::move(J->second); |
| 508 | MapForJD.erase(I: J); |
| 509 | MapForJD[DstK] = std::move(Tmp); |
| 510 | } else { |
| 511 | auto &SrcNames = J->second; |
| 512 | auto &DstNames = K->second; |
| 513 | llvm::append_range(C&: DstNames, R&: SrcNames); |
| 514 | MapForJD.erase(I: J); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | Error SimpleLazyReexportsSpeculator::onLazyReexportsRemoved(JITDylib &JD, |
| 519 | ResourceKey K) { |
| 520 | |
| 521 | auto I = LazyReexports.find(Val: &JD); |
| 522 | if (I == LazyReexports.end()) |
| 523 | return Error::success(); |
| 524 | |
| 525 | auto &MapForJD = I->second; |
| 526 | MapForJD.erase(Val: K); |
| 527 | |
| 528 | if (MapForJD.empty()) { |
| 529 | LazyReexports.erase(I); |
| 530 | JD.Release(); |
| 531 | } |
| 532 | |
| 533 | return Error::success(); |
| 534 | } |
| 535 | |
| 536 | void SimpleLazyReexportsSpeculator::onLazyReexportCalled( |
| 537 | const CallThroughInfo &CTI) { |
| 538 | if (RecordExec) |
| 539 | RecordExec(CTI); |
| 540 | } |
| 541 | |
| 542 | void SimpleLazyReexportsSpeculator::addSpeculationSuggestions( |
| 543 | std::vector<std::pair<std::string, SymbolStringPtr>> NewSuggestions) { |
| 544 | ES.runSessionLocked(F: [&]() { |
| 545 | for (auto &[JDName, SymbolName] : NewSuggestions) |
| 546 | SpeculateSuggestions.push_back( |
| 547 | x: {std::move(JDName), std::move(SymbolName)}); |
| 548 | }); |
| 549 | } |
| 550 | |
| 551 | bool SimpleLazyReexportsSpeculator::doNextSpeculativeLookup() { |
| 552 | // Use existing speculation queue if available, otherwise take the next |
| 553 | // element from LazyReexports. |
| 554 | JITDylibSP SpeculateJD = nullptr; |
| 555 | SymbolStringPtr SpeculateFn; |
| 556 | |
| 557 | auto SpeculateAgain = ES.runSessionLocked(F: [&]() { |
| 558 | while (!SpeculateSuggestions.empty()) { |
| 559 | auto [JDName, SymbolName] = std::move(SpeculateSuggestions.front()); |
| 560 | SpeculateSuggestions.pop_front(); |
| 561 | |
| 562 | if (auto *JD = ES.getJITDylibByName(Name: JDName)) { |
| 563 | SpeculateJD = JD; |
| 564 | SpeculateFn = std::move(SymbolName); |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (!SpeculateJD) { |
| 570 | assert(!LazyReexports.empty() && "LazyReexports map is empty" ); |
| 571 | auto LRItr = |
| 572 | std::next(x: LazyReexports.begin(), n: rand() % LazyReexports.size()); |
| 573 | auto &[JD, KeyToFnBodies] = *LRItr; |
| 574 | |
| 575 | assert(!KeyToFnBodies.empty() && "Key to function bodies map empty" ); |
| 576 | auto KeyToFnBodiesItr = |
| 577 | std::next(x: KeyToFnBodies.begin(), n: rand() % KeyToFnBodies.size()); |
| 578 | auto &[Key, FnBodies] = *KeyToFnBodiesItr; |
| 579 | |
| 580 | assert(!FnBodies.empty() && "Function bodies list empty" ); |
| 581 | auto FnBodyItr = std::next(x: FnBodies.begin(), n: rand() % FnBodies.size()); |
| 582 | |
| 583 | SpeculateJD = JITDylibSP(JD); |
| 584 | SpeculateFn = std::move(*FnBodyItr); |
| 585 | |
| 586 | FnBodies.erase(position: FnBodyItr); |
| 587 | if (FnBodies.empty()) { |
| 588 | KeyToFnBodies.erase(I: KeyToFnBodiesItr); |
| 589 | if (KeyToFnBodies.empty()) { |
| 590 | LRItr->first->Release(); |
| 591 | LazyReexports.erase(I: LRItr); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | SpeculateTaskActive = |
| 597 | !SpeculateSuggestions.empty() || !LazyReexports.empty(); |
| 598 | return SpeculateTaskActive; |
| 599 | }); |
| 600 | |
| 601 | LLVM_DEBUG({ |
| 602 | dbgs() << "Issuing speculative lookup for ( " << SpeculateJD->getName() |
| 603 | << ", " << SpeculateFn << " )...\n" ; |
| 604 | }); |
| 605 | |
| 606 | ES.lookup( |
| 607 | K: LookupKind::Static, SearchOrder: makeJITDylibSearchOrder(JDs: SpeculateJD.get()), |
| 608 | Symbols: {{std::move(SpeculateFn), SymbolLookupFlags::WeaklyReferencedSymbol}}, |
| 609 | RequiredState: SymbolState::Ready, |
| 610 | NotifyComplete: [](Expected<SymbolMap> Result) { consumeError(Err: Result.takeError()); }, |
| 611 | RegisterDependencies: NoDependenciesToRegister); |
| 612 | |
| 613 | if (SpeculateAgain) |
| 614 | ES.dispatchTask(T: std::make_unique<SpeculateTask>(args&: WeakThis)); |
| 615 | |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | } // End namespace orc. |
| 620 | } // End namespace llvm. |
| 621 | |