1 //===---------------- SimpleExecutorMemoryManagerTest.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/TargetProcess/SimpleExecutorMemoryManager.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
12
13 #include <limits>
14 #include <vector>
15
16 using namespace llvm;
17 using namespace llvm::orc;
18 using namespace llvm::orc::shared;
19 using namespace llvm::orc::rt_bootstrap;
20
21 namespace {
22
incrementWrapper(const char * ArgData,size_t ArgSize)23 orc::shared::CWrapperFunctionResult incrementWrapper(const char *ArgData,
24 size_t ArgSize) {
25 return WrapperFunction<SPSError(SPSExecutorAddr)>::handle(
26 ArgData, ArgSize,
27 [](ExecutorAddr A) -> Error {
28 *A.toPtr<int *>() += 1;
29 return Error::success();
30 })
31 .release();
32 }
33
TEST(SimpleExecutorMemoryManagerTest,AllocFinalizeFree)34 TEST(SimpleExecutorMemoryManagerTest, AllocFinalizeFree) {
35 SimpleExecutorMemoryManager MemMgr;
36
37 constexpr unsigned AllocSize = 16384;
38 auto Mem = MemMgr.allocate(AllocSize);
39 EXPECT_THAT_ERROR(Mem.takeError(), Succeeded());
40
41 std::string HW = "Hello, world!";
42
43 int FinalizeCounter = 0;
44 int DeallocateCounter = 0;
45
46 tpctypes::FinalizeRequest FR;
47 FR.Segments.push_back(
48 tpctypes::SegFinalizeRequest{MemProt::Read | MemProt::Write,
49 *Mem,
50 AllocSize,
51 {HW.data(), HW.size() + 1}});
52 FR.Actions.push_back(
53 {/* Finalize: */
54 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
55 ExecutorAddr::fromPtr(incrementWrapper),
56 ExecutorAddr::fromPtr(&FinalizeCounter))),
57 /* Deallocate: */
58 cantFail(WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>(
59 ExecutorAddr::fromPtr(incrementWrapper),
60 ExecutorAddr::fromPtr(&DeallocateCounter)))});
61
62 EXPECT_EQ(FinalizeCounter, 0);
63 EXPECT_EQ(DeallocateCounter, 0);
64
65 auto FinalizeErr = MemMgr.finalize(FR);
66 EXPECT_THAT_ERROR(std::move(FinalizeErr), Succeeded());
67
68 EXPECT_EQ(FinalizeCounter, 1);
69 EXPECT_EQ(DeallocateCounter, 0);
70
71 EXPECT_EQ(HW, std::string(Mem->toPtr<const char *>()));
72
73 auto DeallocateErr = MemMgr.deallocate({*Mem});
74 EXPECT_THAT_ERROR(std::move(DeallocateErr), Succeeded());
75
76 EXPECT_EQ(FinalizeCounter, 1);
77 EXPECT_EQ(DeallocateCounter, 1);
78 }
79
80 } // namespace
81