1 //===--------- JITLinkMocks.cpp - Mock APIs for JITLink unit tests --------===//
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 "JITLinkMocks.h"
10 #include "llvm/ExecutionEngine/JITLink/MachO_x86_64.h"
12 #include "llvm/Testing/Support/Error.h"
13 #include "gtest/gtest.h"
16 using namespace llvm::orc
;
17 using namespace llvm::jitlink
;
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(std::move(Result
));
30 const llvm::jitlink::JITLinkContext::LookupMap
&Symbols
,
31 std::unique_ptr
<llvm::jitlink::JITLinkAsyncLookupContinuation
> LC
) {
32 LC
->run(llvm::make_error
<llvm::StringError
>("Lookup failed",
33 llvm::inconvertibleErrorCode()));
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
>();
42 auto Ctx
= std::make_unique
<MockJITLinkContext
>(std::move(MemMgr
),
43 std::move(HandleFailed
));
48 void defaultMemMgrSetup(MockJITLinkMemoryManager
&) {}
49 void defaultCtxSetup(MockJITLinkContext
&) {}
51 TEST(JITLinkMocks
, SmokeTest
) {
52 // Check that the testing infrastructure defaults can "link" a graph
54 auto G
= std::make_unique
<LinkGraph
>("foo", Triple("x86_64-apple-darwin"), 8,
55 llvm::endianness::little
,
56 getGenericEdgeKindName
);
58 ArrayRef
<char> Content
= "hello, world!";
60 G
->createSection("__data", orc::MemProt::Read
| orc::MemProt::Write
);
61 orc::ExecutorAddr
B1Addr(0x1000);
62 auto &B
= G
->createContentBlock(Sec
, Content
, B1Addr
, 8, 0);
63 G
->addDefinedSymbol(B
, 4, "S", 4, Linkage::Strong
, Scope::Default
, false,
66 Error Err
= Error::success();
68 makeMockContext(JoinErrorsInto(Err
), defaultMemMgrSetup
, defaultCtxSetup
);
70 link_MachO_x86_64(std::move(G
), std::move(Ctx
));
72 EXPECT_THAT_ERROR(std::move(Err
), Succeeded());