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::CWrapperFunctionResult incrementWrapper(const char *ArgData, |
24 | size_t ArgSize) { |
25 | return WrapperFunction<SPSError(SPSExecutorAddr)>::handle( |
26 | ArgData, ArgSize, |
27 | Handler: [](ExecutorAddr A) -> Error { |
28 | *A.toPtr<int *>() += 1; |
29 | return Error::success(); |
30 | }) |
31 | .release(); |
32 | } |
33 | |
34 | TEST(SimpleExecutorMemoryManagerTest, AllocFinalizeFree) { |
35 | SimpleExecutorMemoryManager MemMgr; |
36 | |
37 | constexpr unsigned AllocSize = 16384; |
38 | auto Mem = MemMgr.allocate(Size: 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 | x: tpctypes::SegFinalizeRequest{.RAG: MemProt::Read | MemProt::Write, |
49 | .Addr: *Mem, |
50 | .Size: AllocSize, |
51 | .Content: {HW.data(), HW.size() + 1}}); |
52 | FR.Actions.push_back( |
53 | x: {/* Finalize: */ |
54 | .Finalize: cantFail(ValOrErr: WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>( |
55 | FnAddr: ExecutorAddr::fromPtr(Ptr: incrementWrapper), |
56 | Args: ExecutorAddr::fromPtr(Ptr: &FinalizeCounter))), |
57 | /* Deallocate: */ |
58 | .Dealloc: cantFail(ValOrErr: WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddr>>( |
59 | FnAddr: ExecutorAddr::fromPtr(Ptr: incrementWrapper), |
60 | Args: ExecutorAddr::fromPtr(Ptr: &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(Bases: {*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 | |