1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
//===-------------- EPCGenericJITLinkMemoryManagerTest.cpp ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "OrcTestCommon.h"
#include "llvm/ExecutionEngine/Orc/EPCGenericJITLinkMemoryManager.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
#include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Memory.h"
#include "llvm/Testing/Support/Error.h"
#include <limits>
#include <vector>
using namespace llvm;
using namespace llvm::orc;
using namespace llvm::orc::shared;
namespace {
class SimpleAllocator {
public:
Expected<ExecutorAddr> reserve(uint64_t Size) {
std::error_code EC;
auto MB = sys::Memory::allocateMappedMemory(
Size, 0, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
if (EC)
return errorCodeToError(EC);
Blocks[MB.base()] = sys::OwningMemoryBlock(std::move(MB));
return ExecutorAddr::fromPtr(MB.base());
}
Error finalize(tpctypes::FinalizeRequest FR) {
for (auto &Seg : FR.Segments) {
char *Mem = Seg.Addr.toPtr<char *>();
memcpy(Mem, Seg.Content.data(), Seg.Content.size());
memset(Mem + Seg.Content.size(), 0, Seg.Size - Seg.Content.size());
assert(Seg.Size <= std::numeric_limits<size_t>::max());
if (auto EC = sys::Memory::protectMappedMemory(
{Mem, static_cast<size_t>(Seg.Size)},
tpctypes::fromWireProtectionFlags(Seg.Prot)))
return errorCodeToError(EC);
if (Seg.Prot & tpctypes::WPF_Exec)
sys::Memory::InvalidateInstructionCache(Mem, Seg.Size);
}
return Error::success();
}
Error deallocate(std::vector<ExecutorAddr> &Bases) {
Error Err = Error::success();
for (auto &Base : Bases) {
auto I = Blocks.find(Base.toPtr<void *>());
if (I == Blocks.end()) {
Err = joinErrors(
std::move(Err),
make_error<StringError>("No allocation for " +
formatv("{0:x}", Base.getValue()),
inconvertibleErrorCode()));
continue;
}
auto MB = std::move(I->second);
Blocks.erase(I);
if (auto EC = MB.release())
Err = joinErrors(std::move(Err), errorCodeToError(EC));
}
return Err;
}
private:
DenseMap<void *, sys::OwningMemoryBlock> Blocks;
};
llvm::orc::shared::CWrapperFunctionResult testReserve(const char *ArgData,
size_t ArgSize) {
return WrapperFunction<rt::SPSSimpleExecutorMemoryManagerReserveSignature>::
handle(ArgData, ArgSize,
makeMethodWrapperHandler(&SimpleAllocator::reserve))
.release();
}
llvm::orc::shared::CWrapperFunctionResult testFinalize(const char *ArgData,
size_t ArgSize) {
return WrapperFunction<rt::SPSSimpleExecutorMemoryManagerFinalizeSignature>::
handle(ArgData, ArgSize,
makeMethodWrapperHandler(&SimpleAllocator::finalize))
.release();
}
llvm::orc::shared::CWrapperFunctionResult testDeallocate(const char *ArgData,
size_t ArgSize) {
return WrapperFunction<
rt::SPSSimpleExecutorMemoryManagerDeallocateSignature>::
handle(ArgData, ArgSize,
makeMethodWrapperHandler(&SimpleAllocator::deallocate))
.release();
}
TEST(EPCGenericJITLinkMemoryManagerTest, AllocFinalizeFree) {
auto SelfEPC = cantFail(SelfExecutorProcessControl::Create());
SimpleAllocator SA;
EPCGenericJITLinkMemoryManager::SymbolAddrs SAs;
SAs.Allocator = ExecutorAddr::fromPtr(&SA);
SAs.Reserve = ExecutorAddr::fromPtr(&testReserve);
SAs.Finalize = ExecutorAddr::fromPtr(&testFinalize);
SAs.Deallocate = ExecutorAddr::fromPtr(&testDeallocate);
auto MemMgr = std::make_unique<EPCGenericJITLinkMemoryManager>(*SelfEPC, SAs);
StringRef Hello = "hello";
auto SSA = jitlink::SimpleSegmentAlloc::Create(
*MemMgr, nullptr, {{jitlink::MemProt::Read, {Hello.size(), Align(1)}}});
EXPECT_THAT_EXPECTED(SSA, Succeeded());
auto SegInfo = SSA->getSegInfo(jitlink::MemProt::Read);
memcpy(SegInfo.WorkingMem.data(), Hello.data(), Hello.size());
auto FA = SSA->finalize();
EXPECT_THAT_EXPECTED(FA, Succeeded());
ExecutorAddr TargetAddr(SegInfo.Addr);
const char *TargetMem = TargetAddr.toPtr<const char *>();
EXPECT_NE(TargetMem, SegInfo.WorkingMem.data());
StringRef TargetHello(TargetMem, Hello.size());
EXPECT_EQ(Hello, TargetHello);
auto Err2 = MemMgr->deallocate(std::move(*FA));
EXPECT_THAT_ERROR(std::move(Err2), Succeeded());
cantFail(SelfEPC->disconnect());
}
} // namespace
|