Debian Codegraph @

llvm/unittests/ExecutionEngine/Orc/SimpleExecutorMemoryManagerTest.cpp

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