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