| 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/ExecutionEngine/Orc/SelfExecutorProcessControl.h" |
| 13 |
#include "llvm/Testing/Support/Error.h" |
| 14 |
|
| 15 |
using namespace llvm; |
| 16 |
using namespace llvm::orc; |
| 17 |
using namespace llvm::orc::shared; |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
template <typename WriteT, typename SPSWriteT> |
| 22 |
CWrapperFunctionResult 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 |
*W.Addr.template toPtr<decltype(W.Value) *>() = W.Value; |
| 28 |
}) |
| 29 |
.release(); |
| 30 |
} |
| 31 |
|
| 32 |
CWrapperFunctionResult testWritePointers(const char *ArgData, size_t ArgSize) { |
| 33 |
return WrapperFunction<void(SPSSequence<SPSMemoryAccessPointerWrite>)>:: |
| 34 |
handle(ArgData, ArgSize, |
| 35 |
[](std::vector<tpctypes::PointerWrite> Ws) { |
| 36 |
for (auto &W : Ws) |
| 37 |
*W.Addr.template toPtr<void **>() = W.Value.toPtr<void *>(); |
| 38 |
}) |
| 39 |
.release(); |
| 40 |
} |
| 41 |
|
| 42 |
CWrapperFunctionResult testWriteBuffers(const char *ArgData, size_t ArgSize) { |
| 43 |
return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle( |
| 44 |
ArgData, ArgSize, |
| 45 |
[](std::vector<tpctypes::BufferWrite> Ws) { |
| 46 |
for (auto &W : Ws) |
| 47 |
memcpy(W.Addr.template toPtr<char *>(), W.Buffer.data(), |
| 48 |
W.Buffer.size()); |
| 49 |
}) |
| 50 |
.release(); |
| 51 |
} |
| 52 |
|
| 53 |
template <typename ReadT> |
| 54 |
CWrapperFunctionResult testReadUInts(const char *ArgData, size_t ArgSize) { |
| 55 |
using SPSSig = SPSSequence<ReadT>(SPSSequence<SPSExecutorAddr>); |
| 56 |
return WrapperFunction<SPSSig>::handle(ArgData, ArgSize, |
| 57 |
[](std::vector<ExecutorAddr> Rs) { |
| 58 |
std::vector<ReadT> Result; |
| 59 |
Result.reserve(Rs.size()); |
| 60 |
for (auto &R : Rs) |
| 61 |
Result.push_back( |
| 62 |
*R.template toPtr<ReadT *>()); |
| 63 |
return Result; |
| 64 |
}) |
| 65 |
.release(); |
| 66 |
} |
| 67 |
|
| 68 |
CWrapperFunctionResult testReadPointers(const char *ArgData, size_t ArgSize) { |
| 69 |
using SPSSig = SPSSequence<SPSExecutorAddr>(SPSSequence<SPSExecutorAddr>); |
| 70 |
return WrapperFunction<SPSSig>::handle( |
| 71 |
ArgData, ArgSize, |
| 72 |
[](std::vector<ExecutorAddr> Rs) { |
| 73 |
std::vector<ExecutorAddr> Result; |
| 74 |
Result.reserve(Rs.size()); |
| 75 |
for (auto &R : Rs) |
| 76 |
Result.push_back( |
| 77 |
ExecutorAddr::fromPtr(*R.template toPtr<void **>())); |
| 78 |
return Result; |
| 79 |
}) |
| 80 |
.release(); |
| 81 |
} |
| 82 |
|
| 83 |
CWrapperFunctionResult testReadBuffers(const char *ArgData, size_t ArgSize) { |
| 84 |
using SPSSig = |
| 85 |
SPSSequence<SPSSequence<uint8_t>>(SPSSequence<SPSExecutorAddrRange>); |
| 86 |
return WrapperFunction<SPSSig>::handle( |
| 87 |
ArgData, ArgSize, |
| 88 |
[](std::vector<ExecutorAddrRange> Rs) { |
| 89 |
std::vector<std::vector<uint8_t>> Result; |
| 90 |
Result.reserve(Rs.size()); |
| 91 |
for (auto &R : Rs) { |
| 92 |
Result.push_back({}); |
| 93 |
Result.back().resize(R.size()); |
| 94 |
memcpy(reinterpret_cast<char *>(Result.back().data()), |
| 95 |
R.Start.toPtr<char *>(), R.size()); |
| 96 |
} |
| 97 |
return Result; |
| 98 |
}) |
| 99 |
.release(); |
| 100 |
} |
| 101 |
|
| 102 |
CWrapperFunctionResult testReadStrings(const char *ArgData, size_t ArgSize) { |
| 103 |
using SPSSig = SPSSequence<SPSString>(SPSSequence<SPSExecutorAddr>); |
| 104 |
return WrapperFunction<SPSSig>::handle( |
| 105 |
ArgData, ArgSize, |
| 106 |
[](std::vector<ExecutorAddr> Rs) { |
| 107 |
std::vector<std::string> Result; |
| 108 |
Result.reserve(Rs.size()); |
| 109 |
for (auto &R : Rs) |
| 110 |
Result.push_back(std::string(R.toPtr<char *>())); |
| 111 |
return Result; |
| 112 |
}) |
| 113 |
.release(); |
| 114 |
} |
| 115 |
|
| 116 |
class EPCGenericMemoryAccessTest : public testing::Test { |
| 117 |
public: |
| 118 |
EPCGenericMemoryAccessTest() { |
| 119 |
EPC = cantFail(SelfExecutorProcessControl::Create()); |
| 120 |
|
| 121 |
EPCGenericMemoryAccess::FuncAddrs FAs; |
| 122 |
FAs.WriteUInt8s = ExecutorAddr::fromPtr( |
| 123 |
&testWriteUInts<tpctypes::UInt8Write, SPSMemoryAccessUInt8Write>); |
| 124 |
FAs.WriteUInt16s = ExecutorAddr::fromPtr( |
| 125 |
&testWriteUInts<tpctypes::UInt16Write, SPSMemoryAccessUInt16Write>); |
| 126 |
FAs.WriteUInt32s = ExecutorAddr::fromPtr( |
| 127 |
&testWriteUInts<tpctypes::UInt32Write, SPSMemoryAccessUInt32Write>); |
| 128 |
FAs.WriteUInt64s = ExecutorAddr::fromPtr( |
| 129 |
&testWriteUInts<tpctypes::UInt64Write, SPSMemoryAccessUInt64Write>); |
| 130 |
FAs.WritePointers = ExecutorAddr::fromPtr(&testWritePointers); |
| 131 |
FAs.WriteBuffers = ExecutorAddr::fromPtr(&testWriteBuffers); |
| 132 |
FAs.ReadUInt8s = ExecutorAddr::fromPtr(&testReadUInts<uint8_t>); |
| 133 |
FAs.ReadUInt16s = ExecutorAddr::fromPtr(&testReadUInts<uint16_t>); |
| 134 |
FAs.ReadUInt32s = ExecutorAddr::fromPtr(&testReadUInts<uint32_t>); |
| 135 |
FAs.ReadUInt64s = ExecutorAddr::fromPtr(&testReadUInts<uint64_t>); |
| 136 |
FAs.ReadPointers = ExecutorAddr::fromPtr(&testReadPointers); |
| 137 |
FAs.ReadBuffers = ExecutorAddr::fromPtr(&testReadBuffers); |
| 138 |
FAs.ReadStrings = ExecutorAddr::fromPtr(&testReadStrings); |
| 139 |
|
| 140 |
MemAccess = std::make_unique<EPCGenericMemoryAccess>(*EPC, FAs); |
| 141 |
} |
| 142 |
|
| 143 |
~EPCGenericMemoryAccessTest() { cantFail(EPC->disconnect()); } |
| 144 |
|
| 145 |
protected: |
| 146 |
std::shared_ptr<SelfExecutorProcessControl> EPC; |
| 147 |
std::unique_ptr<MemoryAccess> MemAccess; |
| 148 |
|
| 149 |
static const uint8_t UInt8_1_TestValue; |
| 150 |
static const uint8_t UInt8_2_TestValue; |
| 151 |
static const uint16_t UInt16_TestValue; |
| 152 |
static const uint32_t UInt32_TestValue; |
| 153 |
static const uint64_t UInt64_TestValue; |
| 154 |
static const void *Pointer_TestValue; |
| 155 |
|
| 156 |
static const char Buffer_TestValue[21]; |
| 157 |
static const char *String_TestValue; |
| 158 |
}; |
| 159 |
|
| 160 |
const uint8_t EPCGenericMemoryAccessTest::UInt8_1_TestValue = 1; |
| 161 |
const uint8_t EPCGenericMemoryAccessTest::UInt8_2_TestValue = 2; |
| 162 |
const uint16_t EPCGenericMemoryAccessTest::UInt16_TestValue = 3; |
| 163 |
const uint32_t EPCGenericMemoryAccessTest::UInt32_TestValue = 4; |
| 164 |
const uint64_t EPCGenericMemoryAccessTest::UInt64_TestValue = 5; |
| 165 |
|
| 166 |
const void *EPCGenericMemoryAccessTest::Pointer_TestValue = |
| 167 |
reinterpret_cast<void *>(uintptr_t(0x6)); |
| 168 |
|
| 169 |
const char EPCGenericMemoryAccessTest::Buffer_TestValue[21] = { |
| 170 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, |
| 171 |
0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14}; |
| 172 |
const char *EPCGenericMemoryAccessTest::String_TestValue = "hello, world!"; |
| 173 |
|
| 174 |
TEST_F(EPCGenericMemoryAccessTest, WriteUInt8s) { |
| 175 |
uint8_t UInt8_1_Storage = 0; |
| 176 |
uint8_t UInt8_2_Storage = 0; |
| 177 |
|
| 178 |
auto Err = MemAccess->writeUInt8s( |
| 179 |
{{ExecutorAddr::fromPtr(&UInt8_1_Storage), UInt8_1_TestValue}, |
| 180 |
{ExecutorAddr::fromPtr(&UInt8_2_Storage), UInt8_2_TestValue}}); |
| 181 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 182 |
EXPECT_EQ(UInt8_1_Storage, UInt8_1_TestValue); |
| 183 |
EXPECT_EQ(UInt8_2_Storage, UInt8_2_TestValue); |
| 184 |
} |
| 185 |
|
| 186 |
TEST_F(EPCGenericMemoryAccessTest, ReadUInt8s) { |
| 187 |
uint8_t UInt8_1_Storage = UInt8_1_TestValue; |
| 188 |
uint8_t UInt8_2_Storage = UInt8_2_TestValue; |
| 189 |
|
| 190 |
auto Vals = |
| 191 |
MemAccess->readUInt8s({{ExecutorAddr::fromPtr(&UInt8_1_Storage), |
| 192 |
ExecutorAddr::fromPtr(&UInt8_2_Storage)}}); |
| 193 |
static_assert( |
| 194 |
std::is_same_v<decltype(Vals)::value_type::value_type, uint8_t>); |
| 195 |
if (!Vals) |
| 196 |
return ADD_FAILURE() << toString(Vals.takeError()); |
| 197 |
|
| 198 |
EXPECT_EQ(Vals->size(), 2U); |
| 199 |
if (Vals->size() >= 1) |
| 200 |
EXPECT_EQ((*Vals)[0], UInt8_1_TestValue); |
| 201 |
if (Vals->size() >= 2) |
| 202 |
EXPECT_EQ((*Vals)[1], UInt8_2_TestValue); |
| 203 |
} |
| 204 |
|
| 205 |
TEST_F(EPCGenericMemoryAccessTest, WriteUInt16s) { |
| 206 |
uint16_t UInt16_Storage = 0; |
| 207 |
|
| 208 |
auto Err = MemAccess->writeUInt16s( |
| 209 |
{{ExecutorAddr::fromPtr(&UInt16_Storage), UInt16_TestValue}}); |
| 210 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 211 |
EXPECT_EQ(UInt16_Storage, UInt16_TestValue); |
| 212 |
} |
| 213 |
|
| 214 |
TEST_F(EPCGenericMemoryAccessTest, ReadUInt16s) { |
| 215 |
uint16_t UInt16_Storage = UInt16_TestValue; |
| 216 |
|
| 217 |
auto Vals = |
| 218 |
MemAccess->readUInt16s({{ExecutorAddr::fromPtr(&UInt16_Storage)}}); |
| 219 |
static_assert( |
| 220 |
std::is_same_v<decltype(Vals)::value_type::value_type, uint16_t>); |
| 221 |
if (Vals) { |
| 222 |
EXPECT_EQ(Vals->size(), 1U); |
| 223 |
if (Vals->size() == 1) |
| 224 |
EXPECT_EQ((*Vals)[0], UInt16_TestValue); |
| 225 |
} else |
| 226 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_F(EPCGenericMemoryAccessTest, WriteUInt32s) { |
| 230 |
uint32_t UInt32_Storage = 0; |
| 231 |
|
| 232 |
auto Err = MemAccess->writeUInt32s( |
| 233 |
{{ExecutorAddr::fromPtr(&UInt32_Storage), UInt32_TestValue}}); |
| 234 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 235 |
EXPECT_EQ(UInt32_Storage, UInt32_TestValue); |
| 236 |
} |
| 237 |
|
| 238 |
TEST_F(EPCGenericMemoryAccessTest, ReadUInt32s) { |
| 239 |
uint32_t UInt32_Storage = UInt32_TestValue; |
| 240 |
|
| 241 |
auto Vals = |
| 242 |
MemAccess->readUInt32s({{ExecutorAddr::fromPtr(&UInt32_Storage)}}); |
| 243 |
static_assert( |
| 244 |
std::is_same_v<decltype(Vals)::value_type::value_type, uint32_t>); |
| 245 |
if (Vals) { |
| 246 |
EXPECT_EQ(Vals->size(), 1U); |
| 247 |
if (Vals->size() == 1) |
| 248 |
EXPECT_EQ((*Vals)[0], UInt32_TestValue); |
| 249 |
} else |
| 250 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 251 |
} |
| 252 |
|
| 253 |
TEST_F(EPCGenericMemoryAccessTest, WriteUInt64s) { |
| 254 |
uint64_t UInt64_Storage = 0; |
| 255 |
|
| 256 |
auto Err = MemAccess->writeUInt64s( |
| 257 |
{{ExecutorAddr::fromPtr(&UInt64_Storage), UInt64_TestValue}}); |
| 258 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 259 |
EXPECT_EQ(UInt64_Storage, UInt64_TestValue); |
| 260 |
} |
| 261 |
|
| 262 |
TEST_F(EPCGenericMemoryAccessTest, ReadUInt64s) { |
| 263 |
uint64_t UInt64_Storage = UInt64_TestValue; |
| 264 |
|
| 265 |
auto Vals = |
| 266 |
MemAccess->readUInt64s({{ExecutorAddr::fromPtr(&UInt64_Storage)}}); |
| 267 |
static_assert( |
| 268 |
std::is_same_v<decltype(Vals)::value_type::value_type, uint64_t>); |
| 269 |
if (Vals) { |
| 270 |
EXPECT_EQ(Vals->size(), 1U); |
| 271 |
if (Vals->size() == 1) |
| 272 |
EXPECT_EQ((*Vals)[0], UInt64_TestValue); |
| 273 |
} else |
| 274 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 275 |
} |
| 276 |
|
| 277 |
TEST_F(EPCGenericMemoryAccessTest, WritePointers) { |
| 278 |
void *Pointer_Storage = nullptr; |
| 279 |
|
| 280 |
auto Err = |
| 281 |
MemAccess->writePointers({{ExecutorAddr::fromPtr(&Pointer_Storage), |
| 282 |
ExecutorAddr::fromPtr(Pointer_TestValue)}}); |
| 283 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 284 |
EXPECT_EQ(Pointer_Storage, Pointer_TestValue); |
| 285 |
} |
| 286 |
|
| 287 |
TEST_F(EPCGenericMemoryAccessTest, ReadPointers) { |
| 288 |
auto Vals = |
| 289 |
MemAccess->readPointers({{ExecutorAddr::fromPtr(&Pointer_TestValue)}}); |
| 290 |
static_assert( |
| 291 |
std::is_same_v<decltype(Vals)::value_type::value_type, ExecutorAddr>); |
| 292 |
if (Vals) { |
| 293 |
EXPECT_EQ(Vals->size(), 1U); |
| 294 |
if (Vals->size() == 1) |
| 295 |
EXPECT_EQ((*Vals)[0], ExecutorAddr::fromPtr(Pointer_TestValue)); |
| 296 |
} else |
| 297 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 298 |
} |
| 299 |
|
| 300 |
TEST_F(EPCGenericMemoryAccessTest, WriteBuffers) { |
| 301 |
char Buffer_Storage[sizeof(Buffer_TestValue)]; |
| 302 |
memset(Buffer_Storage, 0, sizeof(Buffer_TestValue)); |
| 303 |
|
| 304 |
auto Err = MemAccess->writeBuffers( |
| 305 |
{{ExecutorAddr::fromPtr(&Buffer_Storage), |
| 306 |
ArrayRef(Buffer_TestValue, sizeof(Buffer_TestValue))}}); |
| 307 |
EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
| 308 |
EXPECT_EQ(ArrayRef(Buffer_Storage, sizeof(Buffer_TestValue)), |
| 309 |
ArrayRef(Buffer_TestValue, sizeof(Buffer_TestValue))); |
| 310 |
} |
| 311 |
|
| 312 |
TEST_F(EPCGenericMemoryAccessTest, ReadBuffers) { |
| 313 |
char Buffer_Storage[sizeof(Buffer_TestValue)]; |
| 314 |
memcpy(Buffer_Storage, Buffer_TestValue, sizeof(Buffer_TestValue)); |
| 315 |
|
| 316 |
auto Vals = MemAccess->readBuffers({{ExecutorAddrRange( |
| 317 |
ExecutorAddr::fromPtr(&Buffer_Storage), sizeof(Buffer_Storage))}}); |
| 318 |
static_assert(std::is_same_v<decltype(Vals)::value_type::value_type, |
| 319 |
std::vector<uint8_t>>); |
| 320 |
if (Vals) { |
| 321 |
EXPECT_EQ(Vals->size(), 1U); |
| 322 |
if (Vals->size() == 1) { |
| 323 |
EXPECT_EQ((*Vals)[0].size(), sizeof(Buffer_Storage)); |
| 324 |
EXPECT_EQ( |
| 325 |
memcmp((*Vals)[0].data(), Buffer_TestValue, sizeof(Buffer_Storage)), |
| 326 |
0); |
| 327 |
} |
| 328 |
} else |
| 329 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 330 |
} |
| 331 |
|
| 332 |
TEST_F(EPCGenericMemoryAccessTest, ReadStrings) { |
| 333 |
auto Vals = |
| 334 |
MemAccess->readStrings({{ExecutorAddr::fromPtr(String_TestValue)}}); |
| 335 |
static_assert( |
| 336 |
std::is_same_v<decltype(Vals)::value_type, std::vector<std::string>>); |
| 337 |
if (Vals) { |
| 338 |
EXPECT_EQ(Vals->size(), 1U); |
| 339 |
if (Vals->size() == 1) |
| 340 |
EXPECT_EQ((*Vals)[0], std::string(String_TestValue)); |
| 341 |
} else |
| 342 |
EXPECT_THAT_ERROR(Vals.takeError(), Succeeded()); |
| 343 |
} |
| 344 |
|
| 345 |
} // namespace |