116 lines · cpp
1//===------- JITLinkTestUtils.cpp - Utilities for JITLink unit tests ------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "JITLinkTestUtils.h"10#include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"11 12#include "llvm/Testing/Support/Error.h"13#include "gtest/gtest.h"14 15using namespace llvm;16using namespace llvm::orc;17using namespace llvm::jitlink;18 19static const char BlockContentBytes[] = {20 0x54, 0x68, 0x65, 0x72, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6d, 0x6f,21 0x76, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68,22 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x66,23 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x6f, 0x72, 0x64, 0x20,24 0x68, 0x61, 0x64, 0x20, 0x70, 0x61, 0x73, 0x73, 0x65, 0x64, 0x20, 0x61,25 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x0a, 0x54, 0x68, 0x61, 0x74, 0x20, 0x74,26 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6c, 0x74, 0x20, 0x66, 0x72, 0x6f, 0x6d,27 0x20, 0x4f, 0x6c, 0x64, 0x20, 0x52, 0x65, 0x67, 0x72, 0x65, 0x74, 0x20,28 0x68, 0x61, 0x64, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x61, 0x77, 0x61, 0x79,29 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x68, 0x61, 0x64, 0x20, 0x6a, 0x6f,30 0x69, 0x6e, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, 0x6c,31 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, 0x65,32 0x73, 0x20, 0x2d, 0x2d, 0x20, 0x68, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20,33 0x77, 0x6f, 0x72, 0x74, 0x68, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75,34 0x73, 0x61, 0x6e, 0x64, 0x20, 0x70, 0x6f, 0x75, 0x6e, 0x64, 0x2c, 0x0a,35 0x53, 0x6f, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63,36 0x72, 0x61, 0x63, 0x6b, 0x73, 0x20, 0x68, 0x61, 0x64, 0x20, 0x67, 0x61,37 0x74, 0x68, 0x65, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68,38 0x65, 0x20, 0x66, 0x72, 0x61, 0x79, 0x2e, 0x0a, 0x41, 0x6c, 0x6c, 0x20,39 0x74, 0x68, 0x65, 0x20, 0x74, 0x72, 0x69, 0x65, 0x64, 0x20, 0x61, 0x6e,40 0x64, 0x20, 0x6e, 0x6f, 0x74, 0x65, 0x64, 0x20, 0x72, 0x69, 0x64, 0x65,41 0x72, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20,42 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6e, 0x65, 0x61,43 0x72, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x61, 0x72, 0x0a, 0x48, 0x61,44 0x64, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x65, 0x72, 0x65, 0x64, 0x20, 0x61,45 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x73, 0x74,46 0x65, 0x61, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x67, 0x68,47 0x74, 0x2c, 0x0a, 0x46, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62,48 0x75, 0x73, 0x68, 0x6d, 0x65, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20,49 0x68, 0x61, 0x72, 0x64, 0x20, 0x72, 0x69, 0x64, 0x69, 0x6e, 0x67, 0x20,50 0x77, 0x68, 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69,51 0x6c, 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73,52 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20,53 0x74, 0x68, 0x65, 0x20, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2d, 0x68, 0x6f,54 0x72, 0x73, 0x65, 0x20, 0x73, 0x6e, 0x75, 0x66, 0x66, 0x73, 0x20, 0x74,55 0x68, 0x65, 0x20, 0x62, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x20, 0x77, 0x69,56 0x74, 0x68, 0x20, 0x64, 0x65, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x2e, 0x00};57 58ArrayRef<char> BlockContent(BlockContentBytes);59 60void lookupResolveEverythingToNull(61 const llvm::jitlink::JITLinkContext::LookupMap &Symbols,62 std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC) {63 llvm::orc::ExecutorAddr Null;64 llvm::jitlink::AsyncLookupResult Result;65 for (auto &KV : Symbols)66 Result[KV.first] = {Null, llvm::JITSymbolFlags::Exported};67 LC->run(std::move(Result));68}69 70void lookupErrorOut(71 const llvm::jitlink::JITLinkContext::LookupMap &Symbols,72 std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC) {73 LC->run(llvm::make_error<llvm::StringError>("Lookup failed",74 llvm::inconvertibleErrorCode()));75}76 77std::unique_ptr<MockJITLinkContext> makeMockContext(78 llvm::unique_function<void(llvm::Error)> HandleFailed,79 llvm::unique_function<void(MockJITLinkMemoryManager &)> SetupMemMgr,80 llvm::unique_function<void(MockJITLinkContext &)> SetupContext) {81 auto MemMgr = std::make_unique<MockJITLinkMemoryManager>();82 SetupMemMgr(*MemMgr);83 auto Ctx = std::make_unique<MockJITLinkContext>(std::move(MemMgr),84 std::move(HandleFailed));85 SetupContext(*Ctx);86 return Ctx;87}88 89void defaultMemMgrSetup(MockJITLinkMemoryManager &) {}90void defaultCtxSetup(MockJITLinkContext &) {}91 92TEST(JITLinkMocks, SmokeTest) {93 // Check that the testing infrastructure defaults can "link" a graph94 // successfully.95 auto G = std::make_unique<LinkGraph>(96 "foo", std::make_shared<orc::SymbolStringPool>(),97 Triple("x86_64-apple-darwin"), SubtargetFeatures(),98 getGenericEdgeKindName);99 100 ArrayRef<char> Content = "hello, world!";101 auto &Sec =102 G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);103 orc::ExecutorAddr B1Addr(0x1000);104 auto &B = G->createContentBlock(Sec, Content, B1Addr, 8, 0);105 G->addDefinedSymbol(B, 4, "S", 4, Linkage::Strong, Scope::Default, false,106 false);107 108 Error Err = Error::success();109 auto Ctx =110 makeMockContext(JoinErrorsInto(Err), defaultMemMgrSetup, defaultCtxSetup);111 112 link_MachO_x86_64(std::move(G), std::move(Ctx));113 114 EXPECT_THAT_ERROR(std::move(Err), Succeeded());115}116