Debian Codegraph @

llvm/unittests/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManagerTest.cpp

raw
1 //===-------------- EPCGenericJITLinkMemoryManagerTest.cpp ----------------===//
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 "OrcTestCommon.h"
10
11 #include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
12
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"
15 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
16 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/Memory.h"
19 #include "llvm/Testing/Support/Error.h"
20
21 #include <limits>
22 #include <vector>
23
24 using namespace llvm;
25 using namespace llvm::orc;
26 using namespace llvm::orc::shared;
27
28 namespace {
29
30 class SimpleAllocator {
31 public:
32 Expected<ExecutorAddr> reserve(uint64_t Size) {
33 std::error_code EC;
34 auto MB = sys::Memory::allocateMappedMemory(
35 Size, 0, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
36 if (EC)
37 return errorCodeToError(EC);
38 Blocks[MB.base()] = sys::OwningMemoryBlock(std::move(MB));
39 return ExecutorAddr::fromPtr(MB.base());
40 }
41
42 Error finalize(tpctypes::FinalizeRequest FR) {
43 for (auto &Seg : FR.Segments) {
44 char *Mem = Seg.Addr.toPtr<char *>();
45 memcpy(Mem, Seg.Content.data(), Seg.Content.size());
46 memset(Mem + Seg.Content.size(), 0, Seg.Size - Seg.Content.size());
47 assert(Seg.Size <= std::numeric_limits<size_t>::max());
48 if (auto EC = sys::Memory::protectMappedMemory(
49 {Mem, static_cast<size_t>(Seg.Size)},
50 toSysMemoryProtectionFlags(Seg.RAG.Prot)))
51 return errorCodeToError(EC);
52 if ((Seg.RAG.Prot & MemProt::Exec) != MemProt::Exec)
53 sys::Memory::InvalidateInstructionCache(Mem, Seg.Size);
54 }
55 return Error::success();
56 }
57
58 Error deallocate(std::vector<ExecutorAddr> &Bases) {
59 Error Err = Error::success();
60 for (auto &Base : Bases) {
61 auto I = Blocks.find(Base.toPtr<void *>());
62 if (I == Blocks.end()) {
63 Err = joinErrors(
64 std::move(Err),
65 make_error<StringError>("No allocation for " +
66 formatv("{0:x}", Base.getValue()),
67 inconvertibleErrorCode()));
68 continue;
69 }
70 auto MB = std::move(I->second);
71 Blocks.erase(I);
72 if (auto EC = MB.release())
73 Err = joinErrors(std::move(Err), errorCodeToError(EC));
74 }
75 return Err;
76 }
77
78 private:
79 DenseMap<void *, sys::OwningMemoryBlock> Blocks;
80 };
81
82 CWrapperFunctionResult testReserve(const char *ArgData, size_t ArgSize) {
83 return WrapperFunction<rt::SPSSimpleExecutorMemoryManagerReserveSignature>::
84 handle(ArgData, ArgSize,
85 makeMethodWrapperHandler(&SimpleAllocator::reserve))
86 .release();
87 }
88
89 CWrapperFunctionResult testFinalize(const char *ArgData, size_t ArgSize) {
90 return WrapperFunction<rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>::
91 handle(ArgData, ArgSize,
92 makeMethodWrapperHandler(&SimpleAllocator::finalize))
93 .release();
94 }
95
96 CWrapperFunctionResult testDeallocate(const char *ArgData, size_t ArgSize) {
97 return WrapperFunction<
98 rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>::
99 handle(ArgData, ArgSize,
100 makeMethodWrapperHandler(&SimpleAllocator::deallocate))
101 .release();
102 }
103
104 TEST(EPCGenericJITLinkMemoryManagerTest, AllocFinalizeFree) {
105 auto SelfEPC = cantFail(SelfExecutorProcessControl::Create());
106 SimpleAllocator SA;
107
108 EPCGenericJITLinkMemoryManager::SymbolAddrs SAs;
109 SAs.Allocator = ExecutorAddr::fromPtr(&SA);
110 SAs.Reserve = ExecutorAddr::fromPtr(&testReserve);
111 SAs.Finalize = ExecutorAddr::fromPtr(&testFinalize);
112 SAs.Deallocate = ExecutorAddr::fromPtr(&testDeallocate);
113
114 auto MemMgr = std::make_unique<EPCGenericJITLinkMemoryManager>(*SelfEPC, SAs);
115 StringRef Hello = "hello";
116 auto SSA = jitlink::SimpleSegmentAlloc::Create(
117 *MemMgr, std::make_shared<SymbolStringPool>(),
118 Triple("x86_64-apple-darwin"), nullptr,
119 {{MemProt::Read, {Hello.size(), Align(1)}}});
120 EXPECT_THAT_EXPECTED(SSA, Succeeded());
121 auto SegInfo = SSA->getSegInfo(MemProt::Read);
122 memcpy(SegInfo.WorkingMem.data(), Hello.data(), Hello.size());
123
124 auto FA = SSA->finalize();
125 EXPECT_THAT_EXPECTED(FA, Succeeded());
126
127 ExecutorAddr TargetAddr(SegInfo.Addr);
128
129 const char *TargetMem = TargetAddr.toPtr<const char *>();
130 EXPECT_NE(TargetMem, SegInfo.WorkingMem.data());
131 StringRef TargetHello(TargetMem, Hello.size());
132 EXPECT_EQ(Hello, TargetHello);
133
134 auto Err2 = MemMgr->deallocate(std::move(*FA));
135 EXPECT_THAT_ERROR(std::move(Err2), Succeeded());
136
137 cantFail(SelfEPC->disconnect());
138 }
139
140 } // namespace