blob: 1dbd48b597258c2c9879b7bcb917ce83b470aa8e [file] [log] [blame]
Lang Hames20d78922018-08-27 22:30:57 +00001//===--- RTDyldObjectLinkingLayer2Test.cpp - RTDyld linking layer tests ---===//
2//
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
10#include "OrcTestCommon.h"
11#include "llvm/ExecutionEngine/ExecutionEngine.h"
12#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
Lang Hames6d320022018-08-31 00:53:17 +000013#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
Lang Hames20d78922018-08-27 22:30:57 +000014#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
15#include "llvm/ExecutionEngine/Orc/Legacy.h"
16#include "llvm/ExecutionEngine/Orc/NullResolver.h"
17#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
18#include "llvm/ExecutionEngine/SectionMemoryManager.h"
19#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
28class RTDyldObjectLinkingLayer2ExecutionTest : public testing::Test,
29 public OrcExecutionTest {};
30
Lang Hames20d78922018-08-27 22:30:57 +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) {
35 class MemoryManagerWrapper : public SectionMemoryManager {
36 public:
37 MemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {}
38 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
39 unsigned SectionID, StringRef SectionName,
40 bool IsReadOnly) override {
41 if (SectionName == ".debug_str")
42 DebugSeen = true;
43 return SectionMemoryManager::allocateDataSection(
44 Size, Alignment, SectionID, SectionName, IsReadOnly);
45 }
46
47 private:
48 bool &DebugSeen;
49 };
50
51 bool DebugSectionSeen = false;
Lang Hames20d78922018-08-27 22:30:57 +000052
Lang Hamesf6107d42018-09-05 20:57:41 +000053 ExecutionSession ES;
Lang Hames20d78922018-08-27 22:30:57 +000054 auto &JD = ES.createJITDylib("main");
Lang Hames71d781c2018-09-30 23:18:24 +000055 auto Foo = ES.intern("foo");
Lang Hames20d78922018-08-27 22:30:57 +000056
Lang Hamesbf985252018-09-06 19:39:26 +000057 RTDyldObjectLinkingLayer2 ObjLayer(ES, [&DebugSectionSeen](VModuleKey) {
58 return llvm::make_unique<MemoryManagerWrapper>(DebugSectionSeen);
59 });
Lang Hames20d78922018-08-27 22:30:57 +000060
61 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(RTDyldObjectLinkingLayer2Test, TestSetProcessAllSections) {
76 LLVMContext Context;
77 auto M = llvm::make_unique<Module>("", Context);
78 M->setTargetTriple("x86_64-unknown-linux-gnu");
79 Type *Int32Ty = IntegerType::get(Context, 32);
80 GlobalVariable *GV =
81 new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
82 ConstantInt::get(Int32Ty, 42), "foo");
83
84 GV->setSection(".debug_str");
85
86 // Initialize the native target in case this is the first unit test
87 // to try to build a TM.
88 OrcNativeTarget::initialize();
89 std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget(
90 Triple(M->getTargetTriple()), "", "", SmallVector<std::string, 1>()));
91 if (!TM)
92 return;
93
94 auto Obj = SimpleCompiler(*TM)(*M);
95
96 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";
101}
102
Lang Hames6d320022018-08-31 00:53:17 +0000103TEST(RTDyldObjectLinkingLayer2Test, 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)
112 return;
113
114 // 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) {}
119
120 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 }
126 };
127
128 // Create a module with two void() functions: foo and bar.
Lang Hames8d76c712018-09-26 01:24:12 +0000129 ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
130 ThreadSafeModule M;
Lang Hames6d320022018-08-31 00:53:17 +0000131 {
Lang Hames8d76c712018-09-26 01:24:12 +0000132 ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy");
Lang Hames6d320022018-08-31 00:53:17 +0000133 MB.getModule()->setDataLayout(TM->createDataLayout());
134
135 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
Lang Hames8d76c712018-09-26 01:24:12 +0000136 BasicBlock *FooEntry =
137 BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl);
Lang Hames6d320022018-08-31 00:53:17 +0000138 IRBuilder<> B1(FooEntry);
139 B1.CreateRetVoid();
140
141 Function *BarImpl = MB.createFunctionDecl<void()>("bar");
Lang Hames8d76c712018-09-26 01:24:12 +0000142 BasicBlock *BarEntry =
143 BasicBlock::Create(*TSCtx.getContext(), "entry", BarImpl);
Lang Hames6d320022018-08-31 00:53:17 +0000144 IRBuilder<> B2(BarEntry);
145 B2.CreateRetVoid();
146
Lang Hames8d76c712018-09-26 01:24:12 +0000147 M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx));
Lang Hames6d320022018-08-31 00:53:17 +0000148 }
149
150 // Create a simple stack and set the override flags option.
151 ExecutionSession ES;
152 auto &JD = ES.createJITDylib("main");
Lang Hames71d781c2018-09-30 23:18:24 +0000153 auto Foo = ES.intern("foo");
Lang Hames6d320022018-08-31 00:53:17 +0000154 RTDyldObjectLinkingLayer2 ObjLayer(
Lang Hamesbf985252018-09-06 19:39:26 +0000155 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
Lang Hames6d320022018-08-31 00:53:17 +0000156 IRCompileLayer2 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);
164}
165
166TEST(RTDyldObjectLinkingLayer2Test, 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)
175 return;
176
177 // 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) {}
182
183 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();
191
192 return SimpleCompiler::operator()(M);
193 }
194 };
195
196 // Create a module with two void() functions: foo and bar.
Lang Hames8d76c712018-09-26 01:24:12 +0000197 ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
198 ThreadSafeModule M;
Lang Hames6d320022018-08-31 00:53:17 +0000199 {
Lang Hames8d76c712018-09-26 01:24:12 +0000200 ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy");
Lang Hames6d320022018-08-31 00:53:17 +0000201 MB.getModule()->setDataLayout(TM->createDataLayout());
202
203 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
Lang Hames8d76c712018-09-26 01:24:12 +0000204 BasicBlock *FooEntry =
205 BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl);
Lang Hames6d320022018-08-31 00:53:17 +0000206 IRBuilder<> B(FooEntry);
207 B.CreateRetVoid();
208
Lang Hames8d76c712018-09-26 01:24:12 +0000209 M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx));
Lang Hames6d320022018-08-31 00:53:17 +0000210 }
211
212 // Create a simple stack and set the override flags option.
213 ExecutionSession ES;
214 auto &JD = ES.createJITDylib("main");
Lang Hames71d781c2018-09-30 23:18:24 +0000215 auto Foo = ES.intern("foo");
Lang Hames6d320022018-08-31 00:53:17 +0000216 RTDyldObjectLinkingLayer2 ObjLayer(
Lang Hamesbf985252018-09-06 19:39:26 +0000217 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
Lang Hames6d320022018-08-31 00:53:17 +0000218 IRCompileLayer2 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);
226}
227
Lang Hames20d78922018-08-27 22:30:57 +0000228} // end anonymous namespace