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