1 | //===--------- JITLinkMocks.cpp - Mock APIs 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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "JITLinkMocks.h" |
10 | #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h" |
11 | |
12 | #include "llvm/Testing/Support/Error.h" |
13 | #include "gtest/gtest.h" |
14 | |
15 | using namespace llvm; |
16 | using namespace llvm::orc; |
17 | using namespace llvm::jitlink; |
18 | |
19 | void lookupResolveEverythingToNull( |
20 | const llvm::jitlink::JITLinkContext::LookupMap &Symbols, |
21 | std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC) { |
22 | llvm::orc::ExecutorAddr Null; |
23 | llvm::jitlink::AsyncLookupResult Result; |
24 | for (auto &KV : Symbols) |
25 | Result[KV.first] = {Null, llvm::JITSymbolFlags::Exported}; |
26 | LC->run(LR: std::move(Result)); |
27 | } |
28 | |
29 | void lookupErrorOut( |
30 | const llvm::jitlink::JITLinkContext::LookupMap &Symbols, |
31 | std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC) { |
32 | LC->run(LR: llvm::make_error<llvm::StringError>(Args: "Lookup failed" , |
33 | Args: llvm::inconvertibleErrorCode())); |
34 | } |
35 | |
36 | std::unique_ptr<MockJITLinkContext> makeMockContext( |
37 | llvm::unique_function<void(llvm::Error)> HandleFailed, |
38 | llvm::unique_function<void(MockJITLinkMemoryManager &)> SetupMemMgr, |
39 | llvm::unique_function<void(MockJITLinkContext &)> SetupContext) { |
40 | auto MemMgr = std::make_unique<MockJITLinkMemoryManager>(); |
41 | SetupMemMgr(*MemMgr); |
42 | auto Ctx = std::make_unique<MockJITLinkContext>(args: std::move(MemMgr), |
43 | args: std::move(HandleFailed)); |
44 | SetupContext(*Ctx); |
45 | return Ctx; |
46 | } |
47 | |
48 | void defaultMemMgrSetup(MockJITLinkMemoryManager &) {} |
49 | void defaultCtxSetup(MockJITLinkContext &) {} |
50 | |
51 | TEST(JITLinkMocks, SmokeTest) { |
52 | // Check that the testing infrastructure defaults can "link" a graph |
53 | // successfully. |
54 | auto G = std::make_unique<LinkGraph>(args: "foo" , args: Triple("x86_64-apple-darwin" ), args: 8, |
55 | args: llvm::endianness::little, |
56 | args&: getGenericEdgeKindName); |
57 | |
58 | ArrayRef<char> Content = "hello, world!" ; |
59 | auto &Sec = |
60 | G->createSection(Name: "__data" , Prot: orc::MemProt::Read | orc::MemProt::Write); |
61 | orc::ExecutorAddr B1Addr(0x1000); |
62 | auto &B = G->createContentBlock(Parent&: Sec, Content, Address: B1Addr, Alignment: 8, AlignmentOffset: 0); |
63 | G->addDefinedSymbol(Content&: B, Offset: 4, Name: "S" , Size: 4, L: Linkage::Strong, S: Scope::Default, IsCallable: false, |
64 | IsLive: false); |
65 | |
66 | Error Err = Error::success(); |
67 | auto Ctx = |
68 | makeMockContext(HandleFailed: JoinErrorsInto(Err), SetupMemMgr: defaultMemMgrSetup, SetupContext: defaultCtxSetup); |
69 | |
70 | link_MachO_x86_64(G: std::move(G), Ctx: std::move(Ctx)); |
71 | |
72 | EXPECT_THAT_ERROR(std::move(Err), Succeeded()); |
73 | } |
74 | |