xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/EPCGenericMemoryAccessTest.cpp (revision 213666f8044990bbf2999c2dc839b6c984e5616b)
1 //===- EPCGenericMemoryAccessTest.cpp -- Tests for EPCGenericMemoryAccess -===//
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 "OrcTestCommon.h"
10 
11 #include "llvm/ExecutionEngine/Orc/EPCGenericMemoryAccess.h"
12 #include "llvm/Testing/Support/Error.h"
13 
14 using namespace llvm;
15 using namespace llvm::orc;
16 using namespace llvm::orc::shared;
17 
18 namespace {
19 
20 template <typename WriteT, typename SPSWriteT>
21 llvm::orc::shared::CWrapperFunctionResult testWriteUInts(const char *ArgData,
22                                                          size_t ArgSize) {
23   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
24              ArgData, ArgSize,
25              [](std::vector<WriteT> Ws) {
26                for (auto &W : Ws)
27                  *W.Addr.template toPtr<decltype(W.Value) *>() = W.Value;
28              })
29       .release();
30 }
31 
32 llvm::orc::shared::CWrapperFunctionResult testWriteBuffers(const char *ArgData,
33                                                            size_t ArgSize) {
34   return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
35              ArgData, ArgSize,
36              [](std::vector<tpctypes::BufferWrite> Ws) {
37                for (auto &W : Ws)
38                  memcpy(W.Addr.template toPtr<char *>(), W.Buffer.data(),
39                         W.Buffer.size());
40              })
41       .release();
42 }
43 
44 TEST(EPCGenericMemoryAccessTest, MemWrites) {
45   auto SelfEPC = cantFail(SelfExecutorProcessControl::Create());
46 
47   EPCGenericMemoryAccess::FuncAddrs FAs;
48   FAs.WriteUInt8s = ExecutorAddr::fromPtr(
49       &testWriteUInts<tpctypes::UInt8Write, SPSMemoryAccessUInt8Write>);
50   FAs.WriteUInt16s = ExecutorAddr::fromPtr(
51       &testWriteUInts<tpctypes::UInt16Write, SPSMemoryAccessUInt16Write>);
52   FAs.WriteUInt32s = ExecutorAddr::fromPtr(
53       &testWriteUInts<tpctypes::UInt32Write, SPSMemoryAccessUInt32Write>);
54   FAs.WriteUInt64s = ExecutorAddr::fromPtr(
55       &testWriteUInts<tpctypes::UInt64Write, SPSMemoryAccessUInt64Write>);
56   FAs.WriteBuffers = ExecutorAddr::fromPtr(&testWriteBuffers);
57 
58   auto MemAccess = std::make_unique<EPCGenericMemoryAccess>(*SelfEPC, FAs);
59 
60   uint8_t Test_UInt8_1 = 0;
61   uint8_t Test_UInt8_2 = 0;
62   uint16_t Test_UInt16 = 0;
63   uint32_t Test_UInt32 = 0;
64   uint64_t Test_UInt64 = 0;
65   char Test_Buffer[21];
66 
67   auto Err1 =
68       MemAccess->writeUInt8s({{ExecutorAddr::fromPtr(&Test_UInt8_1), 1},
69                               {ExecutorAddr::fromPtr(&Test_UInt8_2), 0xFE}});
70 
71   EXPECT_THAT_ERROR(std::move(Err1), Succeeded());
72   EXPECT_EQ(Test_UInt8_1, 1U);
73   EXPECT_EQ(Test_UInt8_2, 0xFE);
74 
75   auto Err2 =
76       MemAccess->writeUInt16s({{ExecutorAddr::fromPtr(&Test_UInt16), 1}});
77   EXPECT_THAT_ERROR(std::move(Err2), Succeeded());
78   EXPECT_EQ(Test_UInt16, 1U);
79 
80   auto Err3 =
81       MemAccess->writeUInt32s({{ExecutorAddr::fromPtr(&Test_UInt32), 1}});
82   EXPECT_THAT_ERROR(std::move(Err3), Succeeded());
83   EXPECT_EQ(Test_UInt32, 1U);
84 
85   auto Err4 =
86       MemAccess->writeUInt64s({{ExecutorAddr::fromPtr(&Test_UInt64), 1}});
87   EXPECT_THAT_ERROR(std::move(Err4), Succeeded());
88   EXPECT_EQ(Test_UInt64, 1U);
89 
90   StringRef TestMsg("test-message");
91   auto Err5 =
92       MemAccess->writeBuffers({{ExecutorAddr::fromPtr(&Test_Buffer), TestMsg}});
93   EXPECT_THAT_ERROR(std::move(Err5), Succeeded());
94   EXPECT_EQ(StringRef(Test_Buffer, TestMsg.size()), TestMsg);
95 
96   cantFail(SelfEPC->disconnect());
97 }
98 
99 } // namespace
100