xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/EPCGenericMemoryAccessTest.cpp (revision 209c242845e56eb8dbbde5e380b165d261769266)
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>
testWriteUInts(const char * ArgData,size_t ArgSize)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 
testWriteBuffers(const char * ArgData,size_t ArgSize)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 
testWritePointers(const char * ArgData,size_t ArgSize)44 llvm::orc::shared::CWrapperFunctionResult testWritePointers(const char *ArgData,
45                                                             size_t ArgSize) {
46   return WrapperFunction<void(SPSSequence<SPSMemoryAccessPointerWrite>)>::
47       handle(ArgData, ArgSize,
48              [](std::vector<tpctypes::PointerWrite> Ws) {
49                for (auto &W : Ws)
50                  *W.Addr.template toPtr<uint64_t *>() = W.Value.getValue();
51              })
52           .release();
53 }
54 
TEST(EPCGenericMemoryAccessTest,MemWrites)55 TEST(EPCGenericMemoryAccessTest, MemWrites) {
56   auto SelfEPC = cantFail(SelfExecutorProcessControl::Create());
57 
58   EPCGenericMemoryAccess::FuncAddrs FAs;
59   FAs.WriteUInt8s = ExecutorAddr::fromPtr(
60       &testWriteUInts<tpctypes::UInt8Write, SPSMemoryAccessUInt8Write>);
61   FAs.WriteUInt16s = ExecutorAddr::fromPtr(
62       &testWriteUInts<tpctypes::UInt16Write, SPSMemoryAccessUInt16Write>);
63   FAs.WriteUInt32s = ExecutorAddr::fromPtr(
64       &testWriteUInts<tpctypes::UInt32Write, SPSMemoryAccessUInt32Write>);
65   FAs.WriteUInt64s = ExecutorAddr::fromPtr(
66       &testWriteUInts<tpctypes::UInt64Write, SPSMemoryAccessUInt64Write>);
67   FAs.WriteBuffers = ExecutorAddr::fromPtr(&testWriteBuffers);
68   FAs.WritePointers = ExecutorAddr::fromPtr(&testWritePointers);
69 
70   auto MemAccess = std::make_unique<EPCGenericMemoryAccess>(*SelfEPC, FAs);
71 
72   uint8_t Test_UInt8_1 = 0;
73   uint8_t Test_UInt8_2 = 0;
74   uint16_t Test_UInt16 = 0;
75   uint32_t Test_UInt32 = 0;
76   uint64_t Test_UInt64 = 0;
77   uint64_t Test_Pointer = 0;
78   char Test_Buffer[21];
79 
80   auto Err1 =
81       MemAccess->writeUInt8s({{ExecutorAddr::fromPtr(&Test_UInt8_1), 1},
82                               {ExecutorAddr::fromPtr(&Test_UInt8_2), 0xFE}});
83 
84   EXPECT_THAT_ERROR(std::move(Err1), Succeeded());
85   EXPECT_EQ(Test_UInt8_1, 1U);
86   EXPECT_EQ(Test_UInt8_2, 0xFE);
87 
88   auto Err2 =
89       MemAccess->writeUInt16s({{ExecutorAddr::fromPtr(&Test_UInt16), 1}});
90   EXPECT_THAT_ERROR(std::move(Err2), Succeeded());
91   EXPECT_EQ(Test_UInt16, 1U);
92 
93   auto Err3 =
94       MemAccess->writeUInt32s({{ExecutorAddr::fromPtr(&Test_UInt32), 1}});
95   EXPECT_THAT_ERROR(std::move(Err3), Succeeded());
96   EXPECT_EQ(Test_UInt32, 1U);
97 
98   auto Err4 =
99       MemAccess->writeUInt64s({{ExecutorAddr::fromPtr(&Test_UInt64), 1}});
100   EXPECT_THAT_ERROR(std::move(Err4), Succeeded());
101   EXPECT_EQ(Test_UInt64, 1U);
102 
103   StringRef TestMsg("test-message");
104   auto Err5 =
105       MemAccess->writeBuffers({{ExecutorAddr::fromPtr(&Test_Buffer), TestMsg}});
106   EXPECT_THAT_ERROR(std::move(Err5), Succeeded());
107   EXPECT_EQ(StringRef(Test_Buffer, TestMsg.size()), TestMsg);
108 
109   auto Err6 = MemAccess->writePointers(
110       {{ExecutorAddr::fromPtr(&Test_Pointer), ExecutorAddr(1U)}});
111   EXPECT_THAT_ERROR(std::move(Err6), Succeeded());
112   EXPECT_EQ(Test_Pointer, 1U);
113 
114   cantFail(SelfEPC->disconnect());
115 }
116 
117 } // namespace
118