blob: 75ccfc9ab0d025e60290d85f5eb82a6b5f72b289 [file] [log] [blame]
Lang Hames079df9a2018-10-15 22:56:10 +00001//===--- RTDyldObjectLinkingLayerTest.cpp - RTDyld linking layer tests ---===//
Lang Hames5f7fcef2015-10-29 03:53:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Lang Hames859d73c2016-01-09 19:50:40 +000010#include "OrcTestCommon.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000011#include "llvm/ExecutionEngine/ExecutionEngine.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000012#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
Lang Hames079df9a2018-10-15 22:56:10 +000013#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000014#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
Lang Hames4b546c92018-02-06 21:25:11 +000015#include "llvm/ExecutionEngine/Orc/Legacy.h"
Lang Hames2fe7acb2016-01-19 21:06:38 +000016#include "llvm/ExecutionEngine/Orc/NullResolver.h"
Lang Hames079df9a2018-10-15 22:56:10 +000017#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
Chandler Carruth9a67b072017-06-06 11:06:56 +000018#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/LLVMContext.h"
21#include "gtest/gtest.h"
22
23using namespace llvm;
24using namespace llvm::orc;
25
26namespace {
27
Lang Hames67de5d22017-02-20 05:45:14 +000028class RTDyldObjectLinkingLayerExecutionTest : public testing::Test,
Lang Hames079df9a2018-10-15 22:56:10 +000029 public OrcExecutionTest {};
Mehdi Amini03b42e42016-04-14 21:59:01 +000030
Lang Hames079df9a2018-10-15 22:56:10 +000031// Adds an object with a debug section to RuntimeDyld and then returns whether
32// the debug section was passed to the memory manager.
33static bool testSetProcessAllSections(std::unique_ptr<MemoryBuffer> Obj,
34 bool ProcessAllSections) {
Lang Hames5b518162017-07-04 04:42:30 +000035 class MemoryManagerWrapper : public SectionMemoryManager {
Lang Hames5f7fcef2015-10-29 03:53:42 +000036 public:
Lang Hames5b518162017-07-04 04:42:30 +000037 MemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {}
Lang Hames5f7fcef2015-10-29 03:53:42 +000038 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Lang Hames079df9a2018-10-15 22:56:10 +000039 unsigned SectionID, StringRef SectionName,
Lang Hames5f7fcef2015-10-29 03:53:42 +000040 bool IsReadOnly) override {
41 if (SectionName == ".debug_str")
42 DebugSeen = true;
Lang Hames079df9a2018-10-15 22:56:10 +000043 return SectionMemoryManager::allocateDataSection(
44 Size, Alignment, SectionID, SectionName, IsReadOnly);
Lang Hames5f7fcef2015-10-29 03:53:42 +000045 }
Lang Hames079df9a2018-10-15 22:56:10 +000046
Lang Hames5f7fcef2015-10-29 03:53:42 +000047 private:
Lang Hamesd22bade2017-04-04 17:03:49 +000048 bool &DebugSeen;
Lang Hames5f7fcef2015-10-29 03:53:42 +000049 };
50
Lang Hames5b518162017-07-04 04:42:30 +000051 bool DebugSectionSeen = false;
Lang Hames5b518162017-07-04 04:42:30 +000052
Lang Hamesf6107d42018-09-05 20:57:41 +000053 ExecutionSession ES;
Lang Hames079df9a2018-10-15 22:56:10 +000054 auto &JD = ES.createJITDylib("main");
55 auto Foo = ES.intern("foo");
Lang Hames4b546c92018-02-06 21:25:11 +000056
Lang Hames079df9a2018-10-15 22:56:10 +000057 RTDyldObjectLinkingLayer ObjLayer(ES, [&DebugSectionSeen](VModuleKey) {
58 return llvm::make_unique<MemoryManagerWrapper>(DebugSectionSeen);
Lang Hames1cd3dd02018-02-14 22:13:02 +000059 });
Lang Hames5f7fcef2015-10-29 03:53:42 +000060
Lang Hames079df9a2018-10-15 22:56:10 +000061 auto OnResolveDoNothing = [](Expected<SymbolMap> R) {
62 cantFail(std::move(R));
63 };
64
65 auto OnReadyDoNothing = [](Error Err) { cantFail(std::move(Err)); };
66
67 ObjLayer.setProcessAllSections(ProcessAllSections);
68 auto K = ES.allocateVModule();
69 cantFail(ObjLayer.add(JD, K, std::move(Obj)));
70 ES.lookup({&JD}, {Foo}, OnResolveDoNothing, OnReadyDoNothing,
71 NoDependenciesToRegister);
72 return DebugSectionSeen;
73}
74
75TEST(RTDyldObjectLinkingLayerTest, TestSetProcessAllSections) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000076 LLVMContext Context;
77 auto M = llvm::make_unique<Module>("", Context);
Lang Hames5f7fcef2015-10-29 03:53:42 +000078 M->setTargetTriple("x86_64-unknown-linux-gnu");
Mehdi Amini03b42e42016-04-14 21:59:01 +000079 Type *Int32Ty = IntegerType::get(Context, 32);
Lang Hames5f7fcef2015-10-29 03:53:42 +000080 GlobalVariable *GV =
Lang Hames079df9a2018-10-15 22:56:10 +000081 new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
Lang Hames5f7fcef2015-10-29 03:53:42 +000082 ConstantInt::get(Int32Ty, 42), "foo");
83
84 GV->setSection(".debug_str");
85
Lang Hamesd22bade2017-04-04 17:03:49 +000086 // Initialize the native target in case this is the first unit test
87 // to try to build a TM.
88 OrcNativeTarget::initialize();
Lang Hames079df9a2018-10-15 22:56:10 +000089 std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget(
90 Triple(M->getTargetTriple()), "", "", SmallVector<std::string, 1>()));
Lang Hames5f7fcef2015-10-29 03:53:42 +000091 if (!TM)
92 return;
93
Lang Hames589eece2018-02-21 21:55:49 +000094 auto Obj = SimpleCompiler(*TM)(*M);
Lang Hames5f7fcef2015-10-29 03:53:42 +000095
Lang Hames079df9a2018-10-15 22:56:10 +000096 EXPECT_FALSE(testSetProcessAllSections(
97 MemoryBuffer::getMemBufferCopy(Obj->getBuffer()), false))
98 << "Debug section seen despite ProcessAllSections being false";
99 EXPECT_TRUE(testSetProcessAllSections(std::move(Obj), true))
100 << "Expected to see debug section when ProcessAllSections is true";
Lang Hames5f7fcef2015-10-29 03:53:42 +0000101}
102
Lang Hames079df9a2018-10-15 22:56:10 +0000103TEST(RTDyldObjectLinkingLayerTest, TestOverrideObjectFlags) {
104
105 OrcNativeTarget::initialize();
106
107 std::unique_ptr<TargetMachine> TM(
108 EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
109 SmallVector<std::string, 1>()));
110
111 if (!TM)
Lang Hames859d73c2016-01-09 19:50:40 +0000112 return;
113
Lang Hames079df9a2018-10-15 22:56:10 +0000114 // Our compiler is going to modify symbol visibility settings without telling
115 // ORC. This will test our ability to override the flags later.
116 class FunkySimpleCompiler : public SimpleCompiler {
117 public:
118 FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {}
Lang Hames4b546c92018-02-06 21:25:11 +0000119
Lang Hames079df9a2018-10-15 22:56:10 +0000120 CompileResult operator()(Module &M) {
121 auto *Foo = M.getFunction("foo");
122 assert(Foo && "Expected function Foo not found");
123 Foo->setVisibility(GlobalValue::HiddenVisibility);
124 return SimpleCompiler::operator()(M);
125 }
Lang Hames4b546c92018-02-06 21:25:11 +0000126 };
127
Lang Hames079df9a2018-10-15 22:56:10 +0000128 // Create a module with two void() functions: foo and bar.
129 ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
130 ThreadSafeModule M;
131 {
132 ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy");
133 MB.getModule()->setDataLayout(TM->createDataLayout());
Lang Hames859d73c2016-01-09 19:50:40 +0000134
Lang Hames079df9a2018-10-15 22:56:10 +0000135 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
136 BasicBlock *FooEntry =
137 BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl);
138 IRBuilder<> B1(FooEntry);
139 B1.CreateRetVoid();
Lang Hames4ce98662017-07-07 02:59:13 +0000140
Lang Hames079df9a2018-10-15 22:56:10 +0000141 Function *BarImpl = MB.createFunctionDecl<void()>("bar");
142 BasicBlock *BarEntry =
143 BasicBlock::Create(*TSCtx.getContext(), "entry", BarImpl);
144 IRBuilder<> B2(BarEntry);
145 B2.CreateRetVoid();
146
147 M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx));
148 }
149
150 // Create a simple stack and set the override flags option.
151 ExecutionSession ES;
152 auto &JD = ES.createJITDylib("main");
153 auto Foo = ES.intern("foo");
154 RTDyldObjectLinkingLayer ObjLayer(
155 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
156 IRCompileLayer CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM));
157
158 ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
159
160 cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M)));
161 ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); },
162 [](Error Err) { cantFail(std::move(Err)); },
163 NoDependenciesToRegister);
Lang Hames859d73c2016-01-09 19:50:40 +0000164}
165
Lang Hames079df9a2018-10-15 22:56:10 +0000166TEST(RTDyldObjectLinkingLayerTest, TestAutoClaimResponsibilityForSymbols) {
167
168 OrcNativeTarget::initialize();
169
170 std::unique_ptr<TargetMachine> TM(
171 EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
172 SmallVector<std::string, 1>()));
173
174 if (!TM)
Lang Hames2fe7acb2016-01-19 21:06:38 +0000175 return;
176
Lang Hames079df9a2018-10-15 22:56:10 +0000177 // Our compiler is going to add a new symbol without telling ORC.
178 // This will test our ability to auto-claim responsibility later.
179 class FunkySimpleCompiler : public SimpleCompiler {
180 public:
181 FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {}
Lang Hames4b546c92018-02-06 21:25:11 +0000182
Lang Hames079df9a2018-10-15 22:56:10 +0000183 CompileResult operator()(Module &M) {
184 Function *BarImpl =
185 Function::Create(TypeBuilder<void(), false>::get(M.getContext()),
186 GlobalValue::ExternalLinkage, "bar", &M);
187 BasicBlock *BarEntry =
188 BasicBlock::Create(M.getContext(), "entry", BarImpl);
189 IRBuilder<> B(BarEntry);
190 B.CreateRetVoid();
Lang Hames5b518162017-07-04 04:42:30 +0000191
Lang Hames079df9a2018-10-15 22:56:10 +0000192 return SimpleCompiler::operator()(M);
193 }
194 };
Lang Hames2fe7acb2016-01-19 21:06:38 +0000195
Lang Hames079df9a2018-10-15 22:56:10 +0000196 // Create a module with two void() functions: foo and bar.
197 ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
198 ThreadSafeModule M;
Lang Hames2fe7acb2016-01-19 21:06:38 +0000199 {
Lang Hames079df9a2018-10-15 22:56:10 +0000200 ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy");
201 MB.getModule()->setDataLayout(TM->createDataLayout());
202
203 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
204 BasicBlock *FooEntry =
205 BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl);
206 IRBuilder<> B(FooEntry);
207 B.CreateRetVoid();
208
209 M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx));
Lang Hames2fe7acb2016-01-19 21:06:38 +0000210 }
211
Lang Hames079df9a2018-10-15 22:56:10 +0000212 // Create a simple stack and set the override flags option.
Lang Hamesf6107d42018-09-05 20:57:41 +0000213 ExecutionSession ES;
Lang Hames079df9a2018-10-15 22:56:10 +0000214 auto &JD = ES.createJITDylib("main");
215 auto Foo = ES.intern("foo");
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000216 RTDyldObjectLinkingLayer ObjLayer(
Lang Hames079df9a2018-10-15 22:56:10 +0000217 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
218 IRCompileLayer CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM));
219
220 ObjLayer.setAutoClaimResponsibilityForObjectSymbols(true);
221
222 cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M)));
223 ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); },
224 [](Error Err) { cantFail(std::move(Err)); },
225 NoDependenciesToRegister);
Lang Hames705db632017-09-28 17:43:07 +0000226}
227
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000228} // end anonymous namespace