1 //===------- JITLinkTestUtils.cpp - Utilities 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 "JITLinkTestUtils.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 static 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};
58 ArrayRef
<char> BlockContent(BlockContentBytes
);
60 void 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
));
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()));
77 std::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
>();
83 auto Ctx
= std::make_unique
<MockJITLinkContext
>(std::move(MemMgr
),
84 std::move(HandleFailed
));
89 void defaultMemMgrSetup(MockJITLinkMemoryManager
&) {}
90 void defaultCtxSetup(MockJITLinkContext
&) {}
92 TEST(JITLinkMocks
, SmokeTest
) {
93 // Check that the testing infrastructure defaults can "link" a graph
95 auto G
= std::make_unique
<LinkGraph
>(
96 "foo", std::make_shared
<orc::SymbolStringPool
>(),
97 Triple("x86_64-apple-darwin"), 8, llvm::endianness::little
,
98 getGenericEdgeKindName
);
100 ArrayRef
<char> Content
= "hello, world!";
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,
108 Error Err
= Error::success();
110 makeMockContext(JoinErrorsInto(Err
), defaultMemMgrSetup
, defaultCtxSetup
);
112 link_MachO_x86_64(std::move(G
), std::move(Ctx
));
114 EXPECT_THAT_ERROR(std::move(Err
), Succeeded());