blob: b99fc494880e5ca507f88ee52668efb1629cb701 [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");
55 auto Foo = ES.getSymbolStringPool().intern("foo");
56
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.
129 LLVMContext Context;
130 std::unique_ptr<Module> M;
131 {
132 ModuleBuilder MB(Context, TM->getTargetTriple().str(), "dummy");
133 MB.getModule()->setDataLayout(TM->createDataLayout());
134
135 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
136 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
137 IRBuilder<> B1(FooEntry);
138 B1.CreateRetVoid();
139
140 Function *BarImpl = MB.createFunctionDecl<void()>("bar");
141 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
142 IRBuilder<> B2(BarEntry);
143 B2.CreateRetVoid();
144
145 M = MB.takeModule();
146 }
147
148 // Create a simple stack and set the override flags option.
149 ExecutionSession ES;
150 auto &JD = ES.createJITDylib("main");
151 auto Foo = ES.getSymbolStringPool().intern("foo");
152 RTDyldObjectLinkingLayer2 ObjLayer(
Lang Hamesbf985252018-09-06 19:39:26 +0000153 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
Lang Hames6d320022018-08-31 00:53:17 +0000154 IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM));
155
156 ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
157
158 cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M)));
159 ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); },
160 [](Error Err) { cantFail(std::move(Err)); },
161 NoDependenciesToRegister);
162}
163
164TEST(RTDyldObjectLinkingLayer2Test, TestAutoClaimResponsibilityForSymbols) {
165
166 OrcNativeTarget::initialize();
167
168 std::unique_ptr<TargetMachine> TM(
169 EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
170 SmallVector<std::string, 1>()));
171
172 if (!TM)
173 return;
174
175 // Our compiler is going to add a new symbol without telling ORC.
176 // This will test our ability to auto-claim responsibility later.
177 class FunkySimpleCompiler : public SimpleCompiler {
178 public:
179 FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {}
180
181 CompileResult operator()(Module &M) {
182 Function *BarImpl =
183 Function::Create(TypeBuilder<void(), false>::get(M.getContext()),
184 GlobalValue::ExternalLinkage, "bar", &M);
185 BasicBlock *BarEntry =
186 BasicBlock::Create(M.getContext(), "entry", BarImpl);
187 IRBuilder<> B(BarEntry);
188 B.CreateRetVoid();
189
190 return SimpleCompiler::operator()(M);
191 }
192 };
193
194 // Create a module with two void() functions: foo and bar.
195 LLVMContext Context;
196 std::unique_ptr<Module> M;
197 {
198 ModuleBuilder MB(Context, TM->getTargetTriple().str(), "dummy");
199 MB.getModule()->setDataLayout(TM->createDataLayout());
200
201 Function *FooImpl = MB.createFunctionDecl<void()>("foo");
202 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
203 IRBuilder<> B(FooEntry);
204 B.CreateRetVoid();
205
206 M = MB.takeModule();
207 }
208
209 // Create a simple stack and set the override flags option.
210 ExecutionSession ES;
211 auto &JD = ES.createJITDylib("main");
212 auto Foo = ES.getSymbolStringPool().intern("foo");
213 RTDyldObjectLinkingLayer2 ObjLayer(
Lang Hamesbf985252018-09-06 19:39:26 +0000214 ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); });
Lang Hames6d320022018-08-31 00:53:17 +0000215 IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM));
216
217 ObjLayer.setAutoClaimResponsibilityForObjectSymbols(true);
218
219 cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M)));
220 ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); },
221 [](Error Err) { cantFail(std::move(Err)); },
222 NoDependenciesToRegister);
223}
224
Lang Hames20d78922018-08-27 22:30:57 +0000225} // end anonymous namespace