From e32b1eee6aab52e2b7b75ee15e506b3e7dd30e68 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 27 Oct 2021 13:40:52 -0700 Subject: [PATCH] [ORC] Change SPSExecutorAddr serialization, SupportFunctionCall struct. SPSExecutorAddr will now be serializable to/from ExecutorAddr, rather than uint64_t. This improves type safety when working with serialized addresses. Also updates the SupportFunctionCall to use an ExecutorAddrRange (rather than a separate ExecutorAddr addr and uint64_t size field), and updates the tpctypes::*Write data structures to use ExecutorAddr rather than JITTargetAddress. --- .../Orc/Shared/ExecutorAddress.h | 2 + .../Orc/Shared/SimplePackedSerialization.h | 7 --- .../Orc/Shared/TargetProcessControlTypes.h | 47 ++++++++++--------- .../Orc/EPCEHFrameRegistrar.cpp | 4 +- .../Orc/EPCGenericJITLinkMemoryManager.cpp | 11 +++-- .../Orc/EPCGenericRTDyldMemoryManager.cpp | 7 ++- .../Orc/EPCIndirectionUtils.cpp | 14 +++--- .../Orc/ExecutorProcessControl.cpp | 11 ++--- .../Orc/TargetProcess/OrcRTBootstrap.cpp | 7 ++- .../Orc/TargetProcess/RegisterEHFrames.cpp | 10 ++-- 10 files changed, 61 insertions(+), 59 deletions(-) diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h index 6b795799a961..3c0b2b9edd52 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h @@ -160,6 +160,8 @@ struct ExecutorAddrRange { namespace shared { +class SPSExecutorAddr {}; + /// SPS serializatior for ExecutorAddr. template <> class SPSSerializationTraits { public: diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h index 570c11e25897..9ac13a493e9d 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h @@ -195,13 +195,6 @@ template class SPSSequence; /// SPS tag type for strings, which are equivalent to sequences of chars. using SPSString = SPSSequence; -/// SPS tag type for executor addresseses. -class SPSExecutorAddr {}; - -template <> -class SPSSerializationTraits - : public SPSSerializationTraits {}; - /// SPS tag type for maps. /// /// SPS maps are just sequences of (Key, Value) tuples. diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h index a7de07cbeb99..fc70c46df231 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h @@ -73,12 +73,19 @@ struct SupportFunctionCall { using FnTy = shared::detail::CWrapperFunctionResult(const char *ArgData, size_t ArgSize); ExecutorAddr Func; - ExecutorAddr ArgData; - uint64_t ArgSize; + ExecutorAddrRange ArgDataRange; + + SupportFunctionCall() = default; + SupportFunctionCall(ExecutorAddr Func, ExecutorAddr ArgData, + ExecutorAddrDiff ArgSize) + : Func(Func), ArgDataRange(ArgData, ArgSize) {} + SupportFunctionCall(ExecutorAddr Func, ExecutorAddrRange ArgDataRange) + : Func(Func), ArgDataRange(ArgDataRange) {} Error run() { - shared::WrapperFunctionResult WFR( - Func.toPtr()(ArgData.toPtr(), ArgSize)); + shared::WrapperFunctionResult WFR(Func.toPtr()( + ArgDataRange.Start.toPtr(), + static_cast(ArgDataRange.size().getValue()))); if (const char *ErrMsg = WFR.getOutOfBandError()) return make_error(ErrMsg, inconvertibleErrorCode()); if (!WFR.empty()) @@ -108,10 +115,9 @@ struct FinalizeRequest { template struct UIntWrite { UIntWrite() = default; - UIntWrite(JITTargetAddress Address, T Value) - : Address(Address), Value(Value) {} + UIntWrite(ExecutorAddr Addr, T Value) : Addr(Addr), Value(Value) {} - JITTargetAddress Address = 0; + ExecutorAddr Addr; T Value = 0; }; @@ -131,10 +137,10 @@ using UInt64Write = UIntWrite; /// For use with TargetProcessControl::MemoryAccess objects. struct BufferWrite { BufferWrite() = default; - BufferWrite(JITTargetAddress Address, StringRef Buffer) - : Address(Address), Buffer(Buffer) {} + BufferWrite(ExecutorAddr Addr, StringRef Buffer) + : Addr(Addr), Buffer(Buffer) {} - JITTargetAddress Address = 0; + ExecutorAddr Addr; StringRef Buffer; }; @@ -149,8 +155,7 @@ namespace shared { class SPSMemoryProtectionFlags {}; -using SPSSupportFunctionCall = - SPSTuple; +using SPSSupportFunctionCall = SPSTuple; using SPSSegFinalizeRequest = SPSTuple, tpctypes::UIntWrite> { public: static size_t size(const tpctypes::UIntWrite &W) { - return SPSTuple::AsArgList::size(W.Address, W.Value); + return SPSTuple::AsArgList::size(W.Addr, W.Value); } static bool serialize(SPSOutputBuffer &OB, const tpctypes::UIntWrite &W) { - return SPSTuple::AsArgList::serialize(OB, W.Address, + return SPSTuple::AsArgList::serialize(OB, W.Addr, W.Value); } static bool deserialize(SPSInputBuffer &IB, tpctypes::UIntWrite &W) { - return SPSTuple::AsArgList::deserialize(IB, W.Address, + return SPSTuple::AsArgList::deserialize(IB, W.Addr, W.Value); } }; @@ -302,17 +307,17 @@ class SPSSerializationTraits>::AsArgList::size( - W.Address, W.Buffer); + W.Addr, W.Buffer); } static bool serialize(SPSOutputBuffer &OB, const tpctypes::BufferWrite &W) { return SPSTuple>::AsArgList ::serialize( - OB, W.Address, W.Buffer); + OB, W.Addr, W.Buffer); } static bool deserialize(SPSInputBuffer &IB, tpctypes::BufferWrite &W) { return SPSTuple>::AsArgList ::deserialize(IB, W.Address, + SPSSequence>::AsArgList ::deserialize(IB, W.Addr, W.Buffer); } }; diff --git a/llvm/lib/ExecutionEngine/Orc/EPCEHFrameRegistrar.cpp b/llvm/lib/ExecutionEngine/Orc/EPCEHFrameRegistrar.cpp index 4e0323b9ca0d..4c0fab8aa9fa 100644 --- a/llvm/lib/ExecutionEngine/Orc/EPCEHFrameRegistrar.cpp +++ b/llvm/lib/ExecutionEngine/Orc/EPCEHFrameRegistrar.cpp @@ -59,14 +59,14 @@ EPCEHFrameRegistrar::Create(ExecutionSession &ES) { Error EPCEHFrameRegistrar::registerEHFrames(JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) { return ES.callSPSWrapper( - RegisterEHFrameWrapperFnAddr, EHFrameSectionAddr, + RegisterEHFrameWrapperFnAddr, ExecutorAddr(EHFrameSectionAddr), static_cast(EHFrameSectionSize)); } Error EPCEHFrameRegistrar::deregisterEHFrames( JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) { return ES.callSPSWrapper( - DeregisterEHFrameWrapperFnAddr, EHFrameSectionAddr, + DeregisterEHFrameWrapperFnAddr, ExecutorAddr(EHFrameSectionAddr), static_cast(EHFrameSectionSize)); } diff --git a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp index 005ba14794d1..9b712cb8f7ca 100644 --- a/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.cpp @@ -60,11 +60,12 @@ public: // turn this into a std::swap. FR.Actions.reserve(G.allocActions().size()); for (auto &ActPair : G.allocActions()) - FR.Actions.push_back( - {{ExecutorAddr(ActPair.Finalize.FnAddr), - ExecutorAddr(ActPair.Finalize.CtxAddr), ActPair.Finalize.CtxSize}, - {ExecutorAddr(ActPair.Dealloc.FnAddr), - ExecutorAddr(ActPair.Dealloc.CtxAddr), ActPair.Dealloc.CtxSize}}); + FR.Actions.push_back({{ExecutorAddr(ActPair.Finalize.FnAddr), + {ExecutorAddr(ActPair.Finalize.CtxAddr), + ExecutorAddrDiff(ActPair.Finalize.CtxSize)}}, + {ExecutorAddr(ActPair.Dealloc.FnAddr), + {ExecutorAddr(ActPair.Dealloc.CtxAddr), + ExecutorAddrDiff(ActPair.Dealloc.CtxSize)}}}); G.allocActions().clear(); Parent.EPC.callSPSWrapperAsync< diff --git a/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp b/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp index 944b66422a24..1d98e104a4d7 100644 --- a/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp +++ b/llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp @@ -262,8 +262,11 @@ bool EPCGenericRTDyldMemoryManager::finalizeMemory(std::string *ErrMsg) { } for (auto &Frame : ObjAllocs.UnfinalizedEHFrames) - FR.Actions.push_back({{SAs.RegisterEHFrame, Frame.Addr, Frame.Size}, - {SAs.DeregisterEHFrame, Frame.Addr, Frame.Size}}); + FR.Actions.push_back( + {{SAs.RegisterEHFrame, + {ExecutorAddr(Frame.Addr), ExecutorAddrDiff(Frame.Size)}}, + {SAs.DeregisterEHFrame, + {ExecutorAddr(Frame.Addr), ExecutorAddrDiff(Frame.Size)}}}); // We'll also need to make an extra allocation for the eh-frame wrapper call // arguments. diff --git a/llvm/lib/ExecutionEngine/Orc/EPCIndirectionUtils.cpp b/llvm/lib/ExecutionEngine/Orc/EPCIndirectionUtils.cpp index a08f29c29f2f..818b6b52ff83 100644 --- a/llvm/lib/ExecutionEngine/Orc/EPCIndirectionUtils.cpp +++ b/llvm/lib/ExecutionEngine/Orc/EPCIndirectionUtils.cpp @@ -161,16 +161,18 @@ Error EPCIndirectStubsManager::createStubs(const StubInitsMap &StubInits) { unsigned ASIdx = 0; std::vector PtrUpdates; for (auto &SI : StubInits) - PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress, - static_cast(SI.second.first)}); + PtrUpdates.push_back( + {ExecutorAddr((*AvailableStubInfos)[ASIdx++].PointerAddress), + static_cast(SI.second.first)}); return MemAccess.writeUInt32s(PtrUpdates); } case 8: { unsigned ASIdx = 0; std::vector PtrUpdates; for (auto &SI : StubInits) - PtrUpdates.push_back({(*AvailableStubInfos)[ASIdx++].PointerAddress, - static_cast(SI.second.first)}); + PtrUpdates.push_back( + {ExecutorAddr((*AvailableStubInfos)[ASIdx++].PointerAddress), + static_cast(SI.second.first)}); return MemAccess.writeUInt64s(PtrUpdates); } default: @@ -212,11 +214,11 @@ Error EPCIndirectStubsManager::updatePointer(StringRef Name, auto &MemAccess = EPCIU.getExecutorProcessControl().getMemoryAccess(); switch (EPCIU.getABISupport().getPointerSize()) { case 4: { - tpctypes::UInt32Write PUpdate(PtrAddr, NewAddr); + tpctypes::UInt32Write PUpdate(ExecutorAddr(PtrAddr), NewAddr); return MemAccess.writeUInt32s(PUpdate); } case 8: { - tpctypes::UInt64Write PUpdate(PtrAddr, NewAddr); + tpctypes::UInt64Write PUpdate(ExecutorAddr(PtrAddr), NewAddr); return MemAccess.writeUInt64s(PUpdate); } default: diff --git a/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp b/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp index 1c3d728409fe..b5435293da13 100644 --- a/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp +++ b/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp @@ -138,36 +138,35 @@ Error SelfExecutorProcessControl::disconnect() { void SelfExecutorProcessControl::writeUInt8sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; + *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt16sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; + *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt32sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; + *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt64sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = W.Value; + *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeBuffersAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) - memcpy(jitTargetAddressToPointer(W.Address), W.Buffer.data(), - W.Buffer.size()); + memcpy(W.Addr.toPtr(), W.Buffer.data(), W.Buffer.size()); OnWriteComplete(Error::success()); } diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp index 356bb0be33f0..c74186b0fc2c 100644 --- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp +++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/OrcRTBootstrap.cpp @@ -28,8 +28,7 @@ writeUIntsWrapper(const char *ArgData, size_t ArgSize) { ArgData, ArgSize, [](std::vector Ws) { for (auto &W : Ws) - *jitTargetAddressToPointer(W.Address) = - W.Value; + *W.Addr.template toPtr() = W.Value; }) .release(); } @@ -40,8 +39,8 @@ writeBuffersWrapper(const char *ArgData, size_t ArgSize) { ArgData, ArgSize, [](std::vector Ws) { for (auto &W : Ws) - memcpy(jitTargetAddressToPointer(W.Address), - W.Buffer.data(), W.Buffer.size()); + memcpy(W.Addr.template toPtr(), W.Buffer.data(), + W.Buffer.size()); }) .release(); } diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.cpp index ee67e61358b2..89d2807e0a46 100644 --- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.cpp +++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.cpp @@ -176,14 +176,12 @@ llvm_orc_deregisterEHFrameSectionCustomDirectWrapper( return llvm::orc::shared::detail::CWrapperFunctionResult(); } -static Error registerEHFrameWrapper(JITTargetAddress Addr, uint64_t Size) { - return llvm::orc::registerEHFrameSection( - jitTargetAddressToPointer(Addr), Size); +static Error registerEHFrameWrapper(ExecutorAddr Addr, uint64_t Size) { + return llvm::orc::registerEHFrameSection(Addr.toPtr(), Size); } -static Error deregisterEHFrameWrapper(JITTargetAddress Addr, uint64_t Size) { - return llvm::orc::deregisterEHFrameSection( - jitTargetAddressToPointer(Addr), Size); +static Error deregisterEHFrameWrapper(ExecutorAddr Addr, uint64_t Size) { + return llvm::orc::deregisterEHFrameSection(Addr.toPtr(), Size); } extern "C" orc::shared::detail::CWrapperFunctionResult