11b091540SLang Hames //===--- JITLinkMemoryManager.cpp - JITLinkMemoryManager implementation ---===//
21b091540SLang Hames //
3c874dd53SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c874dd53SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
5c874dd53SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61b091540SLang Hames //
71b091540SLang Hames //===----------------------------------------------------------------------===//
81b091540SLang Hames 
91b091540SLang Hames #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
10962a2479SLang Hames #include "llvm/ExecutionEngine/JITLink/JITLink.h"
11962a2479SLang Hames #include "llvm/Support/FormatVariadic.h"
121b091540SLang Hames #include "llvm/Support/Process.h"
131b091540SLang Hames 
14962a2479SLang Hames #define DEBUG_TYPE "jitlink"
15962a2479SLang Hames 
16b77c6db9SLang Hames using namespace llvm;
17b77c6db9SLang Hames 
181b091540SLang Hames namespace llvm {
191b091540SLang Hames namespace jitlink {
201b091540SLang Hames 
211b091540SLang Hames JITLinkMemoryManager::~JITLinkMemoryManager() = default;
22962a2479SLang Hames JITLinkMemoryManager::InFlightAlloc::~InFlightAlloc() = default;
231b091540SLang Hames 
BasicLayout(LinkGraph & G)24962a2479SLang Hames BasicLayout::BasicLayout(LinkGraph &G) : G(G) {
25962a2479SLang Hames 
26962a2479SLang Hames   for (auto &Sec : G.sections()) {
27962a2479SLang Hames     // Skip empty sections.
28962a2479SLang Hames     if (empty(Sec.blocks()))
29962a2479SLang Hames       continue;
30962a2479SLang Hames 
31962a2479SLang Hames     auto &Seg = Segments[{Sec.getMemProt(), Sec.getMemDeallocPolicy()}];
32962a2479SLang Hames     for (auto *B : Sec.blocks())
33962a2479SLang Hames       if (LLVM_LIKELY(!B->isZeroFill()))
34962a2479SLang Hames         Seg.ContentBlocks.push_back(B);
35962a2479SLang Hames       else
36962a2479SLang Hames         Seg.ZeroFillBlocks.push_back(B);
37962a2479SLang Hames   }
38962a2479SLang Hames 
39962a2479SLang Hames   // Build Segments map.
40962a2479SLang Hames   auto CompareBlocks = [](const Block *LHS, const Block *RHS) {
41962a2479SLang Hames     // Sort by section, address and size
42962a2479SLang Hames     if (LHS->getSection().getOrdinal() != RHS->getSection().getOrdinal())
43962a2479SLang Hames       return LHS->getSection().getOrdinal() < RHS->getSection().getOrdinal();
44962a2479SLang Hames     if (LHS->getAddress() != RHS->getAddress())
45962a2479SLang Hames       return LHS->getAddress() < RHS->getAddress();
46962a2479SLang Hames     return LHS->getSize() < RHS->getSize();
47962a2479SLang Hames   };
48962a2479SLang Hames 
49962a2479SLang Hames   LLVM_DEBUG(dbgs() << "Generated BasicLayout for " << G.getName() << ":\n");
50962a2479SLang Hames   for (auto &KV : Segments) {
51962a2479SLang Hames     auto &Seg = KV.second;
52962a2479SLang Hames 
53962a2479SLang Hames     llvm::sort(Seg.ContentBlocks, CompareBlocks);
54962a2479SLang Hames     llvm::sort(Seg.ZeroFillBlocks, CompareBlocks);
55962a2479SLang Hames 
56962a2479SLang Hames     for (auto *B : Seg.ContentBlocks) {
57962a2479SLang Hames       Seg.ContentSize = alignToBlock(Seg.ContentSize, *B);
58962a2479SLang Hames       Seg.ContentSize += B->getSize();
59962a2479SLang Hames       Seg.Alignment = std::max(Seg.Alignment, Align(B->getAlignment()));
60962a2479SLang Hames     }
61962a2479SLang Hames 
62962a2479SLang Hames     uint64_t SegEndOffset = Seg.ContentSize;
63962a2479SLang Hames     for (auto *B : Seg.ZeroFillBlocks) {
64962a2479SLang Hames       SegEndOffset = alignToBlock(SegEndOffset, *B);
65962a2479SLang Hames       SegEndOffset += B->getSize();
66962a2479SLang Hames       Seg.Alignment = std::max(Seg.Alignment, Align(B->getAlignment()));
67962a2479SLang Hames     }
68962a2479SLang Hames     Seg.ZeroFillSize = SegEndOffset - Seg.ContentSize;
69962a2479SLang Hames 
70962a2479SLang Hames     LLVM_DEBUG({
71962a2479SLang Hames       dbgs() << "  Seg " << KV.first
72962a2479SLang Hames              << ": content-size=" << formatv("{0:x}", Seg.ContentSize)
73962a2479SLang Hames              << ", zero-fill-size=" << formatv("{0:x}", Seg.ZeroFillSize)
74962a2479SLang Hames              << ", align=" << formatv("{0:x}", Seg.Alignment.value()) << "\n";
75962a2479SLang Hames     });
76962a2479SLang Hames   }
77962a2479SLang Hames }
78962a2479SLang Hames 
79962a2479SLang Hames Expected<BasicLayout::ContiguousPageBasedLayoutSizes>
getContiguousPageBasedLayoutSizes(uint64_t PageSize)80962a2479SLang Hames BasicLayout::getContiguousPageBasedLayoutSizes(uint64_t PageSize) {
81962a2479SLang Hames   ContiguousPageBasedLayoutSizes SegsSizes;
82962a2479SLang Hames 
83962a2479SLang Hames   for (auto &KV : segments()) {
84962a2479SLang Hames     auto &AG = KV.first;
85962a2479SLang Hames     auto &Seg = KV.second;
86962a2479SLang Hames 
87962a2479SLang Hames     if (Seg.Alignment > PageSize)
88962a2479SLang Hames       return make_error<StringError>("Segment alignment greater than page size",
89962a2479SLang Hames                                      inconvertibleErrorCode());
90962a2479SLang Hames 
91962a2479SLang Hames     uint64_t SegSize = alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize);
92962a2479SLang Hames     if (AG.getMemDeallocPolicy() == MemDeallocPolicy::Standard)
93962a2479SLang Hames       SegsSizes.StandardSegs += SegSize;
94962a2479SLang Hames     else
95962a2479SLang Hames       SegsSizes.FinalizeSegs += SegSize;
96962a2479SLang Hames   }
97962a2479SLang Hames 
98962a2479SLang Hames   return SegsSizes;
99962a2479SLang Hames }
100962a2479SLang Hames 
apply()101962a2479SLang Hames Error BasicLayout::apply() {
102962a2479SLang Hames   for (auto &KV : Segments) {
103962a2479SLang Hames     auto &Seg = KV.second;
104962a2479SLang Hames 
105962a2479SLang Hames     assert(!(Seg.ContentBlocks.empty() && Seg.ZeroFillBlocks.empty()) &&
106962a2479SLang Hames            "Empty section recorded?");
107962a2479SLang Hames 
108962a2479SLang Hames     for (auto *B : Seg.ContentBlocks) {
109962a2479SLang Hames       // Align addr and working-mem-offset.
110962a2479SLang Hames       Seg.Addr = alignToBlock(Seg.Addr, *B);
111962a2479SLang Hames       Seg.NextWorkingMemOffset = alignToBlock(Seg.NextWorkingMemOffset, *B);
112962a2479SLang Hames 
113962a2479SLang Hames       // Update block addr.
114962a2479SLang Hames       B->setAddress(Seg.Addr);
115962a2479SLang Hames       Seg.Addr += B->getSize();
116962a2479SLang Hames 
117962a2479SLang Hames       // Copy content to working memory, then update content to point at working
118962a2479SLang Hames       // memory.
119962a2479SLang Hames       memcpy(Seg.WorkingMem + Seg.NextWorkingMemOffset, B->getContent().data(),
120962a2479SLang Hames              B->getSize());
121962a2479SLang Hames       B->setMutableContent(
122962a2479SLang Hames           {Seg.WorkingMem + Seg.NextWorkingMemOffset, B->getSize()});
123962a2479SLang Hames       Seg.NextWorkingMemOffset += B->getSize();
124962a2479SLang Hames     }
125962a2479SLang Hames 
126962a2479SLang Hames     for (auto *B : Seg.ZeroFillBlocks) {
127962a2479SLang Hames       // Align addr.
128962a2479SLang Hames       Seg.Addr = alignToBlock(Seg.Addr, *B);
129962a2479SLang Hames       // Update block addr.
130962a2479SLang Hames       B->setAddress(Seg.Addr);
131962a2479SLang Hames       Seg.Addr += B->getSize();
132962a2479SLang Hames     }
133962a2479SLang Hames 
134962a2479SLang Hames     Seg.ContentBlocks.clear();
135962a2479SLang Hames     Seg.ZeroFillBlocks.clear();
136962a2479SLang Hames   }
137962a2479SLang Hames 
138962a2479SLang Hames   return Error::success();
139962a2479SLang Hames }
140962a2479SLang Hames 
graphAllocActions()141089acf25SLang Hames orc::shared::AllocActions &BasicLayout::graphAllocActions() {
142089acf25SLang Hames   return G.allocActions();
143089acf25SLang Hames }
144962a2479SLang Hames 
Create(JITLinkMemoryManager & MemMgr,const JITLinkDylib * JD,SegmentMap Segments,OnCreatedFunction OnCreated)145962a2479SLang Hames void SimpleSegmentAlloc::Create(JITLinkMemoryManager &MemMgr,
146962a2479SLang Hames                                 const JITLinkDylib *JD, SegmentMap Segments,
147962a2479SLang Hames                                 OnCreatedFunction OnCreated) {
148962a2479SLang Hames 
149962a2479SLang Hames   static_assert(AllocGroup::NumGroups == 16,
150962a2479SLang Hames                 "AllocGroup has changed. Section names below must be updated");
151962a2479SLang Hames   StringRef AGSectionNames[] = {
152962a2479SLang Hames       "__---.standard", "__R--.standard", "__-W-.standard", "__RW-.standard",
153962a2479SLang Hames       "__--X.standard", "__R-X.standard", "__-WX.standard", "__RWX.standard",
154962a2479SLang Hames       "__---.finalize", "__R--.finalize", "__-W-.finalize", "__RW-.finalize",
155962a2479SLang Hames       "__--X.finalize", "__R-X.finalize", "__-WX.finalize", "__RWX.finalize"};
156962a2479SLang Hames 
157962a2479SLang Hames   auto G =
158962a2479SLang Hames       std::make_unique<LinkGraph>("", Triple(), 0, support::native, nullptr);
159962a2479SLang Hames   AllocGroupSmallMap<Block *> ContentBlocks;
160962a2479SLang Hames 
161118e953bSLang Hames   orc::ExecutorAddr NextAddr(0x100000);
162962a2479SLang Hames   for (auto &KV : Segments) {
163962a2479SLang Hames     auto &AG = KV.first;
164962a2479SLang Hames     auto &Seg = KV.second;
165962a2479SLang Hames 
166962a2479SLang Hames     auto AGSectionName =
167962a2479SLang Hames         AGSectionNames[static_cast<unsigned>(AG.getMemProt()) |
168962a2479SLang Hames                        static_cast<bool>(AG.getMemDeallocPolicy()) << 3];
169962a2479SLang Hames 
170962a2479SLang Hames     auto &Sec = G->createSection(AGSectionName, AG.getMemProt());
171962a2479SLang Hames     Sec.setMemDeallocPolicy(AG.getMemDeallocPolicy());
172962a2479SLang Hames 
173962a2479SLang Hames     if (Seg.ContentSize != 0) {
174118e953bSLang Hames       NextAddr =
175118e953bSLang Hames           orc::ExecutorAddr(alignTo(NextAddr.getValue(), Seg.ContentAlign));
176962a2479SLang Hames       auto &B =
177962a2479SLang Hames           G->createMutableContentBlock(Sec, G->allocateBuffer(Seg.ContentSize),
178962a2479SLang Hames                                        NextAddr, Seg.ContentAlign.value(), 0);
179962a2479SLang Hames       ContentBlocks[AG] = &B;
180962a2479SLang Hames       NextAddr += Seg.ContentSize;
181962a2479SLang Hames     }
182962a2479SLang Hames   }
183962a2479SLang Hames 
184962a2479SLang Hames   // GRef declared separately since order-of-argument-eval isn't specified.
185962a2479SLang Hames   auto &GRef = *G;
186962a2479SLang Hames   MemMgr.allocate(JD, GRef,
187962a2479SLang Hames                   [G = std::move(G), ContentBlocks = std::move(ContentBlocks),
188962a2479SLang Hames                    OnCreated = std::move(OnCreated)](
189962a2479SLang Hames                       JITLinkMemoryManager::AllocResult Alloc) mutable {
190962a2479SLang Hames                     if (!Alloc)
191962a2479SLang Hames                       OnCreated(Alloc.takeError());
192962a2479SLang Hames                     else
193962a2479SLang Hames                       OnCreated(SimpleSegmentAlloc(std::move(G),
194962a2479SLang Hames                                                    std::move(ContentBlocks),
195962a2479SLang Hames                                                    std::move(*Alloc)));
196962a2479SLang Hames                   });
197962a2479SLang Hames }
198962a2479SLang Hames 
199962a2479SLang Hames Expected<SimpleSegmentAlloc>
Create(JITLinkMemoryManager & MemMgr,const JITLinkDylib * JD,SegmentMap Segments)200962a2479SLang Hames SimpleSegmentAlloc::Create(JITLinkMemoryManager &MemMgr, const JITLinkDylib *JD,
201962a2479SLang Hames                            SegmentMap Segments) {
202962a2479SLang Hames   std::promise<MSVCPExpected<SimpleSegmentAlloc>> AllocP;
203962a2479SLang Hames   auto AllocF = AllocP.get_future();
204962a2479SLang Hames   Create(MemMgr, JD, std::move(Segments),
205962a2479SLang Hames          [&](Expected<SimpleSegmentAlloc> Result) {
206962a2479SLang Hames            AllocP.set_value(std::move(Result));
207962a2479SLang Hames          });
208962a2479SLang Hames   return AllocF.get();
209962a2479SLang Hames }
210962a2479SLang Hames 
211962a2479SLang Hames SimpleSegmentAlloc::SimpleSegmentAlloc(SimpleSegmentAlloc &&) = default;
212962a2479SLang Hames SimpleSegmentAlloc &
213962a2479SLang Hames SimpleSegmentAlloc::operator=(SimpleSegmentAlloc &&) = default;
214*3a3cb929SKazu Hirata SimpleSegmentAlloc::~SimpleSegmentAlloc() = default;
215962a2479SLang Hames 
getSegInfo(AllocGroup AG)216962a2479SLang Hames SimpleSegmentAlloc::SegmentInfo SimpleSegmentAlloc::getSegInfo(AllocGroup AG) {
217962a2479SLang Hames   auto I = ContentBlocks.find(AG);
218962a2479SLang Hames   if (I != ContentBlocks.end()) {
219962a2479SLang Hames     auto &B = *I->second;
220962a2479SLang Hames     return {B.getAddress(), B.getAlreadyMutableContent()};
221962a2479SLang Hames   }
222962a2479SLang Hames   return {};
223962a2479SLang Hames }
224962a2479SLang Hames 
SimpleSegmentAlloc(std::unique_ptr<LinkGraph> G,AllocGroupSmallMap<Block * > ContentBlocks,std::unique_ptr<JITLinkMemoryManager::InFlightAlloc> Alloc)225962a2479SLang Hames SimpleSegmentAlloc::SimpleSegmentAlloc(
226962a2479SLang Hames     std::unique_ptr<LinkGraph> G, AllocGroupSmallMap<Block *> ContentBlocks,
227962a2479SLang Hames     std::unique_ptr<JITLinkMemoryManager::InFlightAlloc> Alloc)
228962a2479SLang Hames     : G(std::move(G)), ContentBlocks(std::move(ContentBlocks)),
229962a2479SLang Hames       Alloc(std::move(Alloc)) {}
230962a2479SLang Hames 
231962a2479SLang Hames class InProcessMemoryManager::IPInFlightAlloc
232962a2479SLang Hames     : public JITLinkMemoryManager::InFlightAlloc {
2331b091540SLang Hames public:
IPInFlightAlloc(InProcessMemoryManager & MemMgr,LinkGraph & G,BasicLayout BL,sys::MemoryBlock StandardSegments,sys::MemoryBlock FinalizationSegments)234962a2479SLang Hames   IPInFlightAlloc(InProcessMemoryManager &MemMgr, LinkGraph &G, BasicLayout BL,
235962a2479SLang Hames                   sys::MemoryBlock StandardSegments,
236962a2479SLang Hames                   sys::MemoryBlock FinalizationSegments)
237962a2479SLang Hames       : MemMgr(MemMgr), G(G), BL(std::move(BL)),
238962a2479SLang Hames         StandardSegments(std::move(StandardSegments)),
239962a2479SLang Hames         FinalizationSegments(std::move(FinalizationSegments)) {}
240962a2479SLang Hames 
finalize(OnFinalizedFunction OnFinalized)241962a2479SLang Hames   void finalize(OnFinalizedFunction OnFinalized) override {
242962a2479SLang Hames 
243962a2479SLang Hames     // Apply memory protections to all segments.
244962a2479SLang Hames     if (auto Err = applyProtections()) {
245962a2479SLang Hames       OnFinalized(std::move(Err));
246962a2479SLang Hames       return;
2471b091540SLang Hames     }
248962a2479SLang Hames 
249962a2479SLang Hames     // Run finalization actions.
250c0fdc748SLang Hames     auto DeallocActions = runFinalizeActions(G.allocActions());
251c0fdc748SLang Hames     if (!DeallocActions) {
252c0fdc748SLang Hames       OnFinalized(DeallocActions.takeError());
253962a2479SLang Hames       return;
2541b091540SLang Hames     }
255962a2479SLang Hames 
256962a2479SLang Hames     // Release the finalize segments slab.
257962a2479SLang Hames     if (auto EC = sys::Memory::releaseMappedMemory(FinalizationSegments)) {
258962a2479SLang Hames       OnFinalized(errorCodeToError(EC));
259962a2479SLang Hames       return;
260c85d0aaaSLang Hames     }
261962a2479SLang Hames 
262962a2479SLang Hames     // Continue with finalized allocation.
263962a2479SLang Hames     OnFinalized(MemMgr.createFinalizedAlloc(std::move(StandardSegments),
264c0fdc748SLang Hames                                             std::move(*DeallocActions)));
265962a2479SLang Hames   }
266962a2479SLang Hames 
abandon(OnAbandonedFunction OnAbandoned)267962a2479SLang Hames   void abandon(OnAbandonedFunction OnAbandoned) override {
268962a2479SLang Hames     Error Err = Error::success();
269962a2479SLang Hames     if (auto EC = sys::Memory::releaseMappedMemory(FinalizationSegments))
270962a2479SLang Hames       Err = joinErrors(std::move(Err), errorCodeToError(EC));
271962a2479SLang Hames     if (auto EC = sys::Memory::releaseMappedMemory(StandardSegments))
272962a2479SLang Hames       Err = joinErrors(std::move(Err), errorCodeToError(EC));
273962a2479SLang Hames     OnAbandoned(std::move(Err));
2741b091540SLang Hames   }
2751b091540SLang Hames 
2761b091540SLang Hames private:
applyProtections()2771b091540SLang Hames   Error applyProtections() {
278962a2479SLang Hames     for (auto &KV : BL.segments()) {
279962a2479SLang Hames       const auto &AG = KV.first;
280962a2479SLang Hames       auto &Seg = KV.second;
281962a2479SLang Hames 
282962a2479SLang Hames       auto Prot = toSysMemoryProtectionFlags(AG.getMemProt());
283962a2479SLang Hames 
284962a2479SLang Hames       uint64_t SegSize =
285962a2479SLang Hames           alignTo(Seg.ContentSize + Seg.ZeroFillSize, MemMgr.PageSize);
286962a2479SLang Hames       sys::MemoryBlock MB(Seg.WorkingMem, SegSize);
287962a2479SLang Hames       if (auto EC = sys::Memory::protectMappedMemory(MB, Prot))
2881b091540SLang Hames         return errorCodeToError(EC);
2891b091540SLang Hames       if (Prot & sys::Memory::MF_EXEC)
290962a2479SLang Hames         sys::Memory::InvalidateInstructionCache(MB.base(), MB.allocatedSize());
2911b091540SLang Hames     }
2921b091540SLang Hames     return Error::success();
2931b091540SLang Hames   }
2941b091540SLang Hames 
295962a2479SLang Hames   InProcessMemoryManager &MemMgr;
296962a2479SLang Hames   LinkGraph &G;
297962a2479SLang Hames   BasicLayout BL;
298962a2479SLang Hames   sys::MemoryBlock StandardSegments;
299962a2479SLang Hames   sys::MemoryBlock FinalizationSegments;
3001b091540SLang Hames };
3011b091540SLang Hames 
302962a2479SLang Hames Expected<std::unique_ptr<InProcessMemoryManager>>
Create()303962a2479SLang Hames InProcessMemoryManager::Create() {
304962a2479SLang Hames   if (auto PageSize = sys::Process::getPageSize())
305962a2479SLang Hames     return std::make_unique<InProcessMemoryManager>(*PageSize);
306962a2479SLang Hames   else
307962a2479SLang Hames     return PageSize.takeError();
308962a2479SLang Hames }
3094e920e58SLang Hames 
allocate(const JITLinkDylib * JD,LinkGraph & G,OnAllocatedFunction OnAllocated)310962a2479SLang Hames void InProcessMemoryManager::allocate(const JITLinkDylib *JD, LinkGraph &G,
311962a2479SLang Hames                                       OnAllocatedFunction OnAllocated) {
312962a2479SLang Hames 
313962a2479SLang Hames   // FIXME: Just check this once on startup.
314962a2479SLang Hames   if (!isPowerOf2_64((uint64_t)PageSize)) {
315962a2479SLang Hames     OnAllocated(make_error<StringError>("Page size is not a power of 2",
316962a2479SLang Hames                                         inconvertibleErrorCode()));
317962a2479SLang Hames     return;
318962a2479SLang Hames   }
319962a2479SLang Hames 
320962a2479SLang Hames   BasicLayout BL(G);
321962a2479SLang Hames 
322962a2479SLang Hames   /// Scan the request and calculate the group and total sizes.
323962a2479SLang Hames   /// Check that segment size is no larger than a page.
324962a2479SLang Hames   auto SegsSizes = BL.getContiguousPageBasedLayoutSizes(PageSize);
325962a2479SLang Hames   if (!SegsSizes) {
326962a2479SLang Hames     OnAllocated(SegsSizes.takeError());
327962a2479SLang Hames     return;
328962a2479SLang Hames   }
329962a2479SLang Hames 
330962a2479SLang Hames   /// Check that the total size requested (including zero fill) is not larger
331962a2479SLang Hames   /// than a size_t.
332962a2479SLang Hames   if (SegsSizes->total() > std::numeric_limits<size_t>::max()) {
333962a2479SLang Hames     OnAllocated(make_error<JITLinkError>(
3349ca50641SLang Hames         "Total requested size " + formatv("{0:x}", SegsSizes->total()) +
335962a2479SLang Hames         " for graph " + G.getName() + " exceeds address space"));
336962a2479SLang Hames     return;
337962a2479SLang Hames   }
338962a2479SLang Hames 
339962a2479SLang Hames   // Allocate one slab for the whole thing (to make sure everything is
340962a2479SLang Hames   // in-range), then partition into standard and finalization blocks.
341962a2479SLang Hames   //
342962a2479SLang Hames   // FIXME: Make two separate allocations in the future to reduce
343962a2479SLang Hames   // fragmentation: finalization segments will usually be a single page, and
344962a2479SLang Hames   // standard segments are likely to be more than one page. Where multiple
345962a2479SLang Hames   // allocations are in-flight at once (likely) the current approach will leave
346962a2479SLang Hames   // a lot of single-page holes.
347962a2479SLang Hames   sys::MemoryBlock Slab;
348962a2479SLang Hames   sys::MemoryBlock StandardSegsMem;
349962a2479SLang Hames   sys::MemoryBlock FinalizeSegsMem;
350962a2479SLang Hames   {
3511b091540SLang Hames     const sys::Memory::ProtectionFlags ReadWrite =
3521b091540SLang Hames         static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
3531b091540SLang Hames                                                   sys::Memory::MF_WRITE);
3541b091540SLang Hames 
3551b091540SLang Hames     std::error_code EC;
356962a2479SLang Hames     Slab = sys::Memory::allocateMappedMemory(SegsSizes->total(), nullptr,
357962a2479SLang Hames                                              ReadWrite, EC);
3581b091540SLang Hames 
359962a2479SLang Hames     if (EC) {
360962a2479SLang Hames       OnAllocated(errorCodeToError(EC));
361962a2479SLang Hames       return;
3621b091540SLang Hames     }
363818772a5SStefan Gränitz 
364962a2479SLang Hames     // Zero-fill the whole slab up-front.
365962a2479SLang Hames     memset(Slab.base(), 0, Slab.allocatedSize());
366962a2479SLang Hames 
367962a2479SLang Hames     StandardSegsMem = {Slab.base(),
368962a2479SLang Hames                        static_cast<size_t>(SegsSizes->StandardSegs)};
369962a2479SLang Hames     FinalizeSegsMem = {(void *)((char *)Slab.base() + SegsSizes->StandardSegs),
370962a2479SLang Hames                        static_cast<size_t>(SegsSizes->FinalizeSegs)};
371962a2479SLang Hames   }
372962a2479SLang Hames 
373118e953bSLang Hames   auto NextStandardSegAddr = orc::ExecutorAddr::fromPtr(StandardSegsMem.base());
374118e953bSLang Hames   auto NextFinalizeSegAddr = orc::ExecutorAddr::fromPtr(FinalizeSegsMem.base());
375962a2479SLang Hames 
376962a2479SLang Hames   LLVM_DEBUG({
377962a2479SLang Hames     dbgs() << "InProcessMemoryManager allocated:\n";
378962a2479SLang Hames     if (SegsSizes->StandardSegs)
379962a2479SLang Hames       dbgs() << formatv("  [ {0:x16} -- {1:x16} ]", NextStandardSegAddr,
380962a2479SLang Hames                         NextStandardSegAddr + StandardSegsMem.allocatedSize())
381962a2479SLang Hames              << " to stardard segs\n";
382962a2479SLang Hames     else
383962a2479SLang Hames       dbgs() << "  no standard segs\n";
384962a2479SLang Hames     if (SegsSizes->FinalizeSegs)
385962a2479SLang Hames       dbgs() << formatv("  [ {0:x16} -- {1:x16} ]", NextFinalizeSegAddr,
386962a2479SLang Hames                         NextFinalizeSegAddr + FinalizeSegsMem.allocatedSize())
387962a2479SLang Hames              << " to finalize segs\n";
388962a2479SLang Hames     else
389962a2479SLang Hames       dbgs() << "  no finalize segs\n";
390962a2479SLang Hames   });
391962a2479SLang Hames 
392962a2479SLang Hames   // Build ProtMap, assign addresses.
393962a2479SLang Hames   for (auto &KV : BL.segments()) {
394962a2479SLang Hames     auto &AG = KV.first;
395962a2479SLang Hames     auto &Seg = KV.second;
396962a2479SLang Hames 
397962a2479SLang Hames     auto &SegAddr = (AG.getMemDeallocPolicy() == MemDeallocPolicy::Standard)
398962a2479SLang Hames                         ? NextStandardSegAddr
399962a2479SLang Hames                         : NextFinalizeSegAddr;
400962a2479SLang Hames 
401118e953bSLang Hames     Seg.WorkingMem = SegAddr.toPtr<char *>();
402962a2479SLang Hames     Seg.Addr = SegAddr;
403962a2479SLang Hames 
404962a2479SLang Hames     SegAddr += alignTo(Seg.ContentSize + Seg.ZeroFillSize, PageSize);
405962a2479SLang Hames   }
406962a2479SLang Hames 
407962a2479SLang Hames   if (auto Err = BL.apply()) {
408962a2479SLang Hames     OnAllocated(std::move(Err));
409962a2479SLang Hames     return;
410962a2479SLang Hames   }
411962a2479SLang Hames 
412962a2479SLang Hames   OnAllocated(std::make_unique<IPInFlightAlloc>(*this, G, std::move(BL),
413962a2479SLang Hames                                                 std::move(StandardSegsMem),
414962a2479SLang Hames                                                 std::move(FinalizeSegsMem)));
415962a2479SLang Hames }
416962a2479SLang Hames 
deallocate(std::vector<FinalizedAlloc> Allocs,OnDeallocatedFunction OnDeallocated)417962a2479SLang Hames void InProcessMemoryManager::deallocate(std::vector<FinalizedAlloc> Allocs,
418962a2479SLang Hames                                         OnDeallocatedFunction OnDeallocated) {
419962a2479SLang Hames   std::vector<sys::MemoryBlock> StandardSegmentsList;
420089acf25SLang Hames   std::vector<std::vector<orc::shared::WrapperFunctionCall>> DeallocActionsList;
421962a2479SLang Hames 
422962a2479SLang Hames   {
423962a2479SLang Hames     std::lock_guard<std::mutex> Lock(FinalizedAllocsMutex);
424962a2479SLang Hames     for (auto &Alloc : Allocs) {
425118e953bSLang Hames       auto *FA = Alloc.release().toPtr<FinalizedAllocInfo *>();
426962a2479SLang Hames       StandardSegmentsList.push_back(std::move(FA->StandardSegments));
427962a2479SLang Hames       if (!FA->DeallocActions.empty())
428962a2479SLang Hames         DeallocActionsList.push_back(std::move(FA->DeallocActions));
429962a2479SLang Hames       FA->~FinalizedAllocInfo();
430962a2479SLang Hames       FinalizedAllocInfos.Deallocate(FA);
431962a2479SLang Hames     }
432962a2479SLang Hames   }
433962a2479SLang Hames 
434962a2479SLang Hames   Error DeallocErr = Error::success();
435962a2479SLang Hames 
436962a2479SLang Hames   while (!DeallocActionsList.empty()) {
437962a2479SLang Hames     auto &DeallocActions = DeallocActionsList.back();
438962a2479SLang Hames     auto &StandardSegments = StandardSegmentsList.back();
439962a2479SLang Hames 
440962a2479SLang Hames     /// Run any deallocate calls.
441962a2479SLang Hames     while (!DeallocActions.empty()) {
442089acf25SLang Hames       if (auto Err = DeallocActions.back().runWithSPSRetErrorMerged())
443962a2479SLang Hames         DeallocErr = joinErrors(std::move(DeallocErr), std::move(Err));
444962a2479SLang Hames       DeallocActions.pop_back();
445962a2479SLang Hames     }
446962a2479SLang Hames 
447962a2479SLang Hames     /// Release the standard segments slab.
448962a2479SLang Hames     if (auto EC = sys::Memory::releaseMappedMemory(StandardSegments))
449962a2479SLang Hames       DeallocErr = joinErrors(std::move(DeallocErr), errorCodeToError(EC));
450962a2479SLang Hames 
451962a2479SLang Hames     DeallocActionsList.pop_back();
452962a2479SLang Hames     StandardSegmentsList.pop_back();
453962a2479SLang Hames   }
454962a2479SLang Hames 
455962a2479SLang Hames   OnDeallocated(std::move(DeallocErr));
456962a2479SLang Hames }
457962a2479SLang Hames 
458962a2479SLang Hames JITLinkMemoryManager::FinalizedAlloc
createFinalizedAlloc(sys::MemoryBlock StandardSegments,std::vector<orc::shared::WrapperFunctionCall> DeallocActions)459962a2479SLang Hames InProcessMemoryManager::createFinalizedAlloc(
460962a2479SLang Hames     sys::MemoryBlock StandardSegments,
461089acf25SLang Hames     std::vector<orc::shared::WrapperFunctionCall> DeallocActions) {
462962a2479SLang Hames   std::lock_guard<std::mutex> Lock(FinalizedAllocsMutex);
463962a2479SLang Hames   auto *FA = FinalizedAllocInfos.Allocate<FinalizedAllocInfo>();
464962a2479SLang Hames   new (FA) FinalizedAllocInfo(
465962a2479SLang Hames       {std::move(StandardSegments), std::move(DeallocActions)});
466118e953bSLang Hames   return FinalizedAlloc(orc::ExecutorAddr::fromPtr(FA));
4671b091540SLang Hames }
4681b091540SLang Hames 
4691b091540SLang Hames } // end namespace jitlink
4701b091540SLang Hames } // end namespace llvm
471