Debian Codegraph @

llvm/unittests/ExecutionEngine/Orc/MemoryMapperTest.cpp

raw
1 //===------------------------ MemoryMapperTest.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 "llvm/ExecutionEngine/Orc/MemoryMapper.h"
10 #include "llvm/Support/Process.h"
11 #include "llvm/Testing/Support/Error.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15 using namespace llvm::orc;
16 using namespace llvm::orc::shared;
17
18 namespace {
19
20 Expected<ExecutorAddrRange> reserve(MemoryMapper &M, size_t NumBytes) {
21 std::promise<MSVCPExpected<ExecutorAddrRange>> P;
22 auto F = P.get_future();
23 M.reserve(NumBytes, [&](auto R) { P.set_value(std::move(R)); });
24 return F.get();
25 }
26
27 Expected<ExecutorAddr> initialize(MemoryMapper &M,
28 MemoryMapper::AllocInfo &AI) {
29 std::promise<MSVCPExpected<ExecutorAddr>> P;
30 auto F = P.get_future();
31 M.initialize(AI, [&](auto R) { P.set_value(std::move(R)); });
32 return F.get();
33 }
34
35 Error deinitialize(MemoryMapper &M,
36 const std::vector<ExecutorAddr> &Allocations) {
37 std::promise<MSVCPError> P;
38 auto F = P.get_future();
39 M.deinitialize(Allocations, [&](auto R) { P.set_value(std::move(R)); });
40 return F.get();
41 }
42
43 Error release(MemoryMapper &M, const std::vector<ExecutorAddr> &Reservations) {
44 std::promise<MSVCPError> P;
45 auto F = P.get_future();
46 M.release(Reservations, [&](auto R) { P.set_value(std::move(R)); });
47 return F.get();
48 }
49
50 // A basic function to be used as both initializer/deinitializer
51 CWrapperFunctionResult incrementWrapper(const char *ArgData, size_t ArgSize) {
52 return WrapperFunction<SPSError(SPSExecutorAddr)>::handle(
53 ArgData, ArgSize,
54 [](ExecutorAddr A) -> Error {
55 *A.toPtr<int *>() += 1;
56 return Error::success();
57 })
58 .release();
59 }
60
61 TEST(MemoryMapperTest, InitializeDeinitialize) {
62 // These counters are used to track how many times the initializer and
63 // deinitializer functions are called
64 int InitializeCounter = 0;
65 int DeinitializeCounter = 0;
66 {
67 std::unique_ptr<MemoryMapper> Mapper =
68 cantFail(InProcessMemoryMapper::Create());
69
70 // We will do two separate allocations
71 auto PageSize = Mapper->getPageSize();
72 auto TotalSize = PageSize * 2;
73
74 // Reserve address space
75 auto Mem1 = reserve(*Mapper, TotalSize);
76 EXPECT_THAT_ERROR(Mem1.takeError(), Succeeded());
77
78 // Test string for memory transfer
79 std::string HW = "Hello, world!";
80
81 {
82 // Provide working memory
83 char *WA1 = Mapper->prepare(Mem1->Start, HW.size() + 1);
84 std::strcpy(static_cast<char *>(WA1), HW.c_str());
85 }
86
87 // A structure to be passed to initialize
88 MemoryMapper::AllocInfo Alloc1;
89 {
90 MemoryMapper::AllocInfo::SegInfo Seg1;
91 Seg1.Offset = 0;
92 Seg1.ContentSize = HW.size();
93 Seg1.ZeroFillSize = PageSize - Seg1.ContentSize;
94 Seg1.AG = MemProt::Read | MemProt::Write;
95
96 Alloc1.MappingBase = Mem1->Start;
97 Alloc1.Segments.push_back(Seg1);
98 Alloc1.Actions.push_back(
99 {cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
100 ExecutorAddr::fromPtr(incrementWrapper),
101 ExecutorAddr::fromPtr(&InitializeCounter))),
102 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
103 ExecutorAddr::fromPtr(incrementWrapper),
104 ExecutorAddr::fromPtr(&DeinitializeCounter)))});
105 }
106
107 {
108 char *WA2 = Mapper->prepare(Mem1->Start + PageSize, HW.size() + 1);
109 std::strcpy(static_cast<char *>(WA2), HW.c_str());
110 }
111
112 MemoryMapper::AllocInfo Alloc2;
113 {
114 MemoryMapper::AllocInfo::SegInfo Seg2;
115 Seg2.Offset = PageSize;
116 Seg2.ContentSize = HW.size();
117 Seg2.ZeroFillSize = PageSize - Seg2.ContentSize;
118 Seg2.AG = MemProt::Read | MemProt::Write;
119
120 Alloc2.MappingBase = Mem1->Start;
121 Alloc2.Segments.push_back(Seg2);
122 Alloc2.Actions.push_back(
123 {cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
124 ExecutorAddr::fromPtr(incrementWrapper),
125 ExecutorAddr::fromPtr(&InitializeCounter))),
126 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
127 ExecutorAddr::fromPtr(incrementWrapper),
128 ExecutorAddr::fromPtr(&DeinitializeCounter)))});
129 }
130
131 EXPECT_EQ(InitializeCounter, 0);
132 EXPECT_EQ(DeinitializeCounter, 0);
133
134 // Set memory protections and run initializers
135 auto Init1 = initialize(*Mapper, Alloc1);
136 EXPECT_THAT_ERROR(Init1.takeError(), Succeeded());
137 EXPECT_EQ(HW, std::string(static_cast<char *>(Init1->toPtr<char *>())));
138
139 EXPECT_EQ(InitializeCounter, 1);
140 EXPECT_EQ(DeinitializeCounter, 0);
141
142 auto Init2 = initialize(*Mapper, Alloc2);
143 EXPECT_THAT_ERROR(Init2.takeError(), Succeeded());
144 EXPECT_EQ(HW, std::string(static_cast<char *>(Init2->toPtr<char *>())));
145
146 EXPECT_EQ(InitializeCounter, 2);
147 EXPECT_EQ(DeinitializeCounter, 0);
148
149 // Explicit deinitialization of first allocation
150 std::vector<ExecutorAddr> DeinitAddr = {*Init1};
151 EXPECT_THAT_ERROR(deinitialize(*Mapper, DeinitAddr), Succeeded());
152
153 EXPECT_EQ(InitializeCounter, 2);
154 EXPECT_EQ(DeinitializeCounter, 1);
155
156 // Test explicit release
157 {
158 auto Mem2 = reserve(*Mapper, PageSize);
159 EXPECT_THAT_ERROR(Mem2.takeError(), Succeeded());
160
161 char *WA = Mapper->prepare(Mem2->Start, HW.size() + 1);
162 std::strcpy(static_cast<char *>(WA), HW.c_str());
163
164 MemoryMapper::AllocInfo Alloc3;
165 {
166 MemoryMapper::AllocInfo::SegInfo Seg3;
167 Seg3.Offset = 0;
168 Seg3.ContentSize = HW.size();
169 Seg3.ZeroFillSize = PageSize - Seg3.ContentSize;
170 Seg3.AG = MemProt::Read | MemProt::Write;
171
172 Alloc3.MappingBase = Mem2->Start;
173 Alloc3.Segments.push_back(Seg3);
174 Alloc3.Actions.push_back(
175 {cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
176 ExecutorAddr::fromPtr(incrementWrapper),
177 ExecutorAddr::fromPtr(&InitializeCounter))),
178 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
179 ExecutorAddr::fromPtr(incrementWrapper),
180 ExecutorAddr::fromPtr(&DeinitializeCounter)))});
181 }
182 auto Init3 = initialize(*Mapper, Alloc3);
183 EXPECT_THAT_ERROR(Init3.takeError(), Succeeded());
184 EXPECT_EQ(HW, std::string(static_cast<char *>(Init3->toPtr<char *>())));
185
186 EXPECT_EQ(InitializeCounter, 3);
187 EXPECT_EQ(DeinitializeCounter, 1);
188
189 std::vector<ExecutorAddr> ReleaseAddrs = {Mem2->Start};
190 EXPECT_THAT_ERROR(release(*Mapper, ReleaseAddrs), Succeeded());
191
192 EXPECT_EQ(InitializeCounter, 3);
193 EXPECT_EQ(DeinitializeCounter, 2);
194 }
195 }
196
197 // Implicit deinitialization by the destructor
198 EXPECT_EQ(InitializeCounter, 3);
199 EXPECT_EQ(DeinitializeCounter, 3);
200 }
201
202 } // namespace