1 //===---- EPCGenericJITLinkMemoryManager.cpp -- Mem management via EPC ----===// 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/EPCGenericJITLinkMemoryManager.h" 10 #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h" 11 12 #include <limits> 13 14 namespace llvm { 15 namespace orc { 16 17 class EPCGenericJITLinkMemoryManager::Alloc 18 : public jitlink::JITLinkMemoryManager::Allocation { 19 public: 20 struct SegInfo { 21 char *WorkingMem = nullptr; 22 ExecutorAddress TargetAddr; 23 uint64_t ContentSize = 0; 24 uint64_t ZeroFillSize = 0; 25 }; 26 using SegInfoMap = DenseMap<unsigned, SegInfo>; 27 28 Alloc(EPCGenericJITLinkMemoryManager &Parent, ExecutorAddress TargetAddr, 29 uint64_t TargetSize, std::unique_ptr<char[]> WorkingBuffer, 30 SegInfoMap Segs) 31 : Parent(Parent), TargetAddr(TargetAddr), TargetSize(TargetSize), 32 WorkingBuffer(std::move(WorkingBuffer)), Segs(std::move(Segs)) {} 33 34 MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override { 35 auto I = Segs.find(Seg); 36 assert(I != Segs.end() && "No allocation for seg"); 37 assert(I->second.ContentSize <= std::numeric_limits<size_t>::max()); 38 return {I->second.WorkingMem, static_cast<size_t>(I->second.ContentSize)}; 39 } 40 41 JITTargetAddress getTargetMemory(ProtectionFlags Seg) override { 42 auto I = Segs.find(Seg); 43 assert(I != Segs.end() && "No allocation for seg"); 44 return I->second.TargetAddr.getValue(); 45 } 46 47 void finalizeAsync(FinalizeContinuation OnFinalize) override { 48 char *WorkingMem = WorkingBuffer.get(); 49 tpctypes::FinalizeRequest FR; 50 for (auto &KV : Segs) { 51 assert(KV.second.ContentSize <= std::numeric_limits<size_t>::max()); 52 FR.push_back(tpctypes::SegFinalizeRequest{ 53 tpctypes::toWireProtectionFlags( 54 static_cast<sys::Memory::ProtectionFlags>(KV.first)), 55 KV.second.TargetAddr, 56 alignTo(KV.second.ContentSize + KV.second.ZeroFillSize, 57 Parent.EPC.getPageSize()), 58 {WorkingMem, static_cast<size_t>(KV.second.ContentSize)}}); 59 WorkingMem += KV.second.ContentSize; 60 } 61 Parent.EPC.callSPSWrapperAsync<shared::SPSOrcTargetProcessFinalize>( 62 [OnFinalize = std::move(OnFinalize)](Error SerializationErr, 63 Error FinalizeErr) { 64 if (SerializationErr) 65 OnFinalize(std::move(SerializationErr)); 66 else 67 OnFinalize(std::move(FinalizeErr)); 68 }, 69 Parent.FAs.Finalize.getValue(), std::move(FR)); 70 } 71 72 Error deallocate() override { 73 Error Err = Error::success(); 74 if (auto E2 = 75 Parent.EPC.callSPSWrapper<shared::SPSOrcTargetProcessDeallocate>( 76 Parent.FAs.Deallocate.getValue(), Err, TargetAddr, TargetSize)) 77 return E2; 78 return Err; 79 } 80 81 private: 82 EPCGenericJITLinkMemoryManager &Parent; 83 ExecutorAddress TargetAddr; 84 uint64_t TargetSize; 85 std::unique_ptr<char[]> WorkingBuffer; 86 SegInfoMap Segs; 87 }; 88 89 /// Create from a ExecutorProcessControl instance. 90 Expected<std::unique_ptr<EPCGenericJITLinkMemoryManager>> 91 EPCGenericJITLinkMemoryManager::CreateUsingOrcRTFuncs( 92 ExecutorProcessControl &EPC) { 93 94 auto H = EPC.loadDylib(""); 95 if (!H) 96 return H.takeError(); 97 98 StringRef GlobalPrefix = ""; 99 if (EPC.getTargetTriple().isOSBinFormatMachO()) 100 GlobalPrefix = "_"; 101 102 FuncAddrs FAs; 103 if (auto Err = lookupAndRecordAddrs( 104 EPC, *H, 105 {{EPC.intern((GlobalPrefix + "__orc_rt_reserve").str()), &FAs.Reserve}, 106 {EPC.intern((GlobalPrefix + "__orc_rt_finalize").str()), 107 &FAs.Finalize}, 108 {EPC.intern((GlobalPrefix + "__orc_rt_deallocate").str()), 109 &FAs.Deallocate}})) 110 return std::move(Err); 111 112 return std::make_unique<EPCGenericJITLinkMemoryManager>(EPC, std::move(FAs)); 113 } 114 115 Expected<std::unique_ptr<jitlink::JITLinkMemoryManager::Allocation>> 116 EPCGenericJITLinkMemoryManager::allocate(const jitlink::JITLinkDylib *JD, 117 const SegmentsRequestMap &Request) { 118 Alloc::SegInfoMap Segs; 119 uint64_t AllocSize = 0; 120 size_t WorkingSize = 0; 121 for (auto &KV : Request) { 122 if (!isPowerOf2_64(KV.second.getAlignment())) 123 return make_error<StringError>("Alignment is not a power of two", 124 inconvertibleErrorCode()); 125 if (KV.second.getAlignment() > EPC.getPageSize()) 126 return make_error<StringError>("Alignment exceeds page size", 127 inconvertibleErrorCode()); 128 129 auto &Seg = Segs[KV.first]; 130 Seg.ContentSize = KV.second.getContentSize(); 131 Seg.ZeroFillSize = KV.second.getZeroFillSize(); 132 AllocSize += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()); 133 WorkingSize += Seg.ContentSize; 134 } 135 136 std::unique_ptr<char[]> WorkingBuffer; 137 if (WorkingSize > 0) 138 WorkingBuffer = std::make_unique<char[]>(WorkingSize); 139 Expected<ExecutorAddress> TargetAllocAddr((ExecutorAddress())); 140 if (auto Err = EPC.callSPSWrapper<shared::SPSOrcTargetProcessAllocate>( 141 FAs.Reserve.getValue(), TargetAllocAddr, AllocSize)) 142 return std::move(Err); 143 if (!TargetAllocAddr) 144 return TargetAllocAddr.takeError(); 145 146 char *WorkingMem = WorkingBuffer.get(); 147 JITTargetAddress SegAddr = TargetAllocAddr->getValue(); 148 for (auto &KV : Segs) { 149 auto &Seg = KV.second; 150 Seg.TargetAddr.setValue(SegAddr); 151 SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, EPC.getPageSize()); 152 Seg.WorkingMem = WorkingMem; 153 WorkingMem += Seg.ContentSize; 154 } 155 156 return std::make_unique<Alloc>(*this, *TargetAllocAddr, AllocSize, 157 std::move(WorkingBuffer), std::move(Segs)); 158 } 159 160 } // end namespace orc 161 } // end namespace llvm 162