1 //===- EPCGenericMemoryAccessTest.cpp -- Tests for EPCGenericMemoryAccess -===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OrcTestCommon.h"
11 #include "llvm/ExecutionEngine/Orc/EPCGenericMemoryAccess.h"
12 #include "llvm/Testing/Support/Error.h"
15 using namespace llvm::orc
;
16 using namespace llvm::orc::shared
;
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(
25 [](std::vector
<WriteT
> Ws
) {
27 *jitTargetAddressToPointer
<decltype(W
.Value
) *>(W
.Address
) =
33 llvm::orc::shared::detail::CWrapperFunctionResult
34 testWriteBuffers(const char *ArgData
, size_t ArgSize
) {
35 return WrapperFunction
<void(SPSSequence
<SPSMemoryAccessBufferWrite
>)>::handle(
37 [](std::vector
<tpctypes::BufferWrite
> Ws
) {
39 memcpy(jitTargetAddressToPointer
<char *>(W
.Address
),
40 W
.Buffer
.data(), W
.Buffer
.size());
45 TEST(EPCGenericMemoryAccessTest
, MemWrites
) {
46 auto SelfEPC
= cantFail(SelfExecutorProcessControl::Create());
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
);
59 auto MemAccess
= std::make_unique
<EPCGenericMemoryAccess
>(*SelfEPC
, FAs
);
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;
68 auto Err1
= MemAccess
->writeUInt8s(
69 {{pointerToJITTargetAddress(&Test_UInt8_1
), 1},
70 {pointerToJITTargetAddress(&Test_UInt8_2
), 0xFE}});
72 EXPECT_THAT_ERROR(std::move(Err1
), Succeeded());
73 EXPECT_EQ(Test_UInt8_1
, 1U);
74 EXPECT_EQ(Test_UInt8_2
, 0xFE);
77 MemAccess
->writeUInt16s({{pointerToJITTargetAddress(&Test_UInt16
), 1}});
78 EXPECT_THAT_ERROR(std::move(Err2
), Succeeded());
79 EXPECT_EQ(Test_UInt16
, 1U);
82 MemAccess
->writeUInt32s({{pointerToJITTargetAddress(&Test_UInt32
), 1}});
83 EXPECT_THAT_ERROR(std::move(Err3
), Succeeded());
84 EXPECT_EQ(Test_UInt32
, 1U);
87 MemAccess
->writeUInt64s({{pointerToJITTargetAddress(&Test_UInt64
), 1}});
88 EXPECT_THAT_ERROR(std::move(Err4
), Succeeded());
89 EXPECT_EQ(Test_UInt64
, 1U);
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
);