blob: 3e7d3f6db39cd9e6fa3386e9658570e8080f9b85 [file] [log] [blame]
Lang Hames67de5d22017-02-20 05:45:14 +00001//===- RTDyldObjectLinkingLayerTest.cpp - RTDyld linking layer unit 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
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
Lang Hames859d73c2016-01-09 19:50:40 +000011#include "OrcTestCommon.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000012#include "llvm/ExecutionEngine/ExecutionEngine.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000013#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
14#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"
Chandler Carruth9a67b072017-06-06 11:06:56 +000017#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Lang Hames5f7fcef2015-10-29 03:53:42 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/LLVMContext.h"
20#include "gtest/gtest.h"
21
22using namespace llvm;
23using namespace llvm::orc;
24
25namespace {
26
Lang Hames67de5d22017-02-20 05:45:14 +000027class RTDyldObjectLinkingLayerExecutionTest : public testing::Test,
28 public OrcExecutionTest {
Mehdi Amini03b42e42016-04-14 21:59:01 +000029
Lang Hames859d73c2016-01-09 19:50:40 +000030};
31
NAKAMURA Takumi96e30312016-01-10 15:56:49 +000032class SectionMemoryManagerWrapper : public SectionMemoryManager {
33public:
34 int FinalizationCount = 0;
Lang Hames2fe7acb2016-01-19 21:06:38 +000035 int NeedsToReserveAllocationSpaceCount = 0;
36
37 bool needsToReserveAllocationSpace() override {
38 ++NeedsToReserveAllocationSpaceCount;
39 return SectionMemoryManager::needsToReserveAllocationSpace();
40 }
41
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000042 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
NAKAMURA Takumi96e30312016-01-10 15:56:49 +000043 ++FinalizationCount;
44 return SectionMemoryManager::finalizeMemory(ErrMsg);
45 }
46};
47
Lang Hames67de5d22017-02-20 05:45:14 +000048TEST(RTDyldObjectLinkingLayerTest, TestSetProcessAllSections) {
Lang Hames5b518162017-07-04 04:42:30 +000049 class MemoryManagerWrapper : public SectionMemoryManager {
Lang Hames5f7fcef2015-10-29 03:53:42 +000050 public:
Lang Hames5b518162017-07-04 04:42:30 +000051 MemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {}
Lang Hames5f7fcef2015-10-29 03:53:42 +000052 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
53 unsigned SectionID,
54 StringRef SectionName,
55 bool IsReadOnly) override {
56 if (SectionName == ".debug_str")
57 DebugSeen = true;
58 return SectionMemoryManager::allocateDataSection(Size, Alignment,
59 SectionID,
60 SectionName,
61 IsReadOnly);
62 }
63 private:
Lang Hamesd22bade2017-04-04 17:03:49 +000064 bool &DebugSeen;
Lang Hames5f7fcef2015-10-29 03:53:42 +000065 };
66
Lang Hames5b518162017-07-04 04:42:30 +000067 bool DebugSectionSeen = false;
68 auto MM = std::make_shared<MemoryManagerWrapper>(DebugSectionSeen);
69
Lang Hames4b546c92018-02-06 21:25:11 +000070 SymbolStringPool SSP;
71 ExecutionSession ES(SSP);
72
73 RTDyldObjectLinkingLayer ObjLayer(
74 ES, [&MM](VModuleKey) { return MM; },
75 [](orc::VModuleKey) { return std::make_shared<NullResolver>(); });
Lang Hames5f7fcef2015-10-29 03:53:42 +000076
Mehdi Amini03b42e42016-04-14 21:59:01 +000077 LLVMContext Context;
78 auto M = llvm::make_unique<Module>("", Context);
Lang Hames5f7fcef2015-10-29 03:53:42 +000079 M->setTargetTriple("x86_64-unknown-linux-gnu");
Mehdi Amini03b42e42016-04-14 21:59:01 +000080 Type *Int32Ty = IntegerType::get(Context, 32);
Lang Hames5f7fcef2015-10-29 03:53:42 +000081 GlobalVariable *GV =
82 new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
83 ConstantInt::get(Int32Ty, 42), "foo");
84
85 GV->setSection(".debug_str");
86
Lang Hamesd22bade2017-04-04 17:03:49 +000087
88 // Initialize the native target in case this is the first unit test
89 // to try to build a TM.
90 OrcNativeTarget::initialize();
Lang Hames5f7fcef2015-10-29 03:53:42 +000091 std::unique_ptr<TargetMachine> TM(
92 EngineBuilder().selectTarget(Triple(M->getTargetTriple()), "", "",
93 SmallVector<std::string, 1>()));
94 if (!TM)
95 return;
96
Lang Hames26620222017-06-22 21:06:54 +000097 auto Obj =
98 std::make_shared<object::OwningBinary<object::ObjectFile>>(
99 SimpleCompiler(*TM)(*M));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000100
Lang Hames5f7fcef2015-10-29 03:53:42 +0000101 {
102 // Test with ProcessAllSections = false (the default).
Lang Hames4b546c92018-02-06 21:25:11 +0000103 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
Lang Hames4ce98662017-07-07 02:59:13 +0000104 cantFail(ObjLayer.emitAndFinalize(H));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000105 EXPECT_EQ(DebugSectionSeen, false)
106 << "Unexpected debug info section";
Lang Hames4ce98662017-07-07 02:59:13 +0000107 cantFail(ObjLayer.removeObject(H));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000108 }
109
110 {
111 // Test with ProcessAllSections = true.
112 ObjLayer.setProcessAllSections(true);
Lang Hames4b546c92018-02-06 21:25:11 +0000113 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), Obj));
Lang Hames4ce98662017-07-07 02:59:13 +0000114 cantFail(ObjLayer.emitAndFinalize(H));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000115 EXPECT_EQ(DebugSectionSeen, true)
116 << "Expected debug info section not seen";
Lang Hames4ce98662017-07-07 02:59:13 +0000117 cantFail(ObjLayer.removeObject(H));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000118 }
119}
120
Lang Hames67de5d22017-02-20 05:45:14 +0000121TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
Lang Hames859d73c2016-01-09 19:50:40 +0000122 if (!TM)
123 return;
124
Lang Hames4b546c92018-02-06 21:25:11 +0000125 SymbolStringPool SSP;
126 ExecutionSession ES(SSP);
127
Lang Hames5b518162017-07-04 04:42:30 +0000128 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
129
Lang Hames4b546c92018-02-06 21:25:11 +0000130 std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
131
132 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) { return MM; },
133 [&](VModuleKey K) {
134 auto I = Resolvers.find(K);
135 assert(I != Resolvers.end() &&
136 "Missing resolver");
137 auto R = std::move(I->second);
138 Resolvers.erase(I);
139 return R;
140 });
Lang Hames859d73c2016-01-09 19:50:40 +0000141 SimpleCompiler Compile(*TM);
142
143 // Create a pair of modules that will trigger recursive finalization:
144 // Module 1:
145 // int bar() { return 42; }
146 // Module 2:
147 // int bar();
148 // int foo() { return bar(); }
Lang Hames133f1532016-01-18 01:00:19 +0000149 //
150 // Verify that the memory manager is only finalized once (for Module 2).
151 // Failure suggests that finalize is being called on the inner RTDyld
152 // instance (for Module 1) which is unsafe, as it will prevent relocation of
153 // Module 2.
Lang Hames859d73c2016-01-09 19:50:40 +0000154
Mehdi Amini03b42e42016-04-14 21:59:01 +0000155 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000156 {
157 MB1.getModule()->setDataLayout(TM->createDataLayout());
158 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000159 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000160 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000161 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames859d73c2016-01-09 19:50:40 +0000162 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
163 Builder.CreateRet(FourtyTwo);
164 }
165
Lang Hames26620222017-06-22 21:06:54 +0000166 auto Obj1 =
167 std::make_shared<object::OwningBinary<object::ObjectFile>>(
168 Compile(*MB1.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000169
Mehdi Amini03b42e42016-04-14 21:59:01 +0000170 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000171 {
172 MB2.getModule()->setDataLayout(TM->createDataLayout());
173 Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
174 Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000175 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000176 IRBuilder<> Builder(FooEntry);
177 Builder.CreateRet(Builder.CreateCall(BarDecl));
178 }
Lang Hames26620222017-06-22 21:06:54 +0000179 auto Obj2 =
180 std::make_shared<object::OwningBinary<object::ObjectFile>>(
181 Compile(*MB2.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000182
Lang Hames4b546c92018-02-06 21:25:11 +0000183 auto K1 = ES.allocateVModule();
184 Resolvers[K1] = std::make_shared<NullResolver>();
185 cantFail(ObjLayer.addObject(K1, std::move(Obj1)));
186
187 auto K2 = ES.allocateVModule();
188 auto LegacyLookup = [&](const std::string &Name) {
189 return ObjLayer.findSymbol(Name, true);
190 };
191
192 Resolvers[K2] = createSymbolResolver(
193 [&](SymbolFlagsMap &SymbolFlags, const SymbolNameSet &Symbols) {
194 return cantFail(
195 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup));
Lang Hames859d73c2016-01-09 19:50:40 +0000196 },
Lang Hames4b546c92018-02-06 21:25:11 +0000197 [&](AsynchronousSymbolQuery &Query, const SymbolNameSet &Symbols) {
198 return lookupWithLegacyFn(Query, Symbols, LegacyLookup);
Lang Hames859d73c2016-01-09 19:50:40 +0000199 });
200
Lang Hames4b546c92018-02-06 21:25:11 +0000201 auto H = cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
Lang Hames4ce98662017-07-07 02:59:13 +0000202 cantFail(ObjLayer.emitAndFinalize(H));
203 cantFail(ObjLayer.removeObject(H));
204
Lang Hames859d73c2016-01-09 19:50:40 +0000205 // Finalization of module 2 should trigger finalization of module 1.
206 // Verify that finalize on SMMW is only called once.
Lang Hames5b518162017-07-04 04:42:30 +0000207 EXPECT_EQ(MM->FinalizationCount, 1)
Lang Hames859d73c2016-01-09 19:50:40 +0000208 << "Extra call to finalize";
209}
210
Lang Hames67de5d22017-02-20 05:45:14 +0000211TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
Lang Hames2fe7acb2016-01-19 21:06:38 +0000212 if (!TM)
213 return;
214
Lang Hames4b546c92018-02-06 21:25:11 +0000215 SymbolStringPool SSP;
216 ExecutionSession ES(SSP);
217
Lang Hames5b518162017-07-04 04:42:30 +0000218 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
219
Lang Hames4b546c92018-02-06 21:25:11 +0000220 RTDyldObjectLinkingLayer ObjLayer(
221 ES, [&MM](VModuleKey) { return MM; },
222 [](VModuleKey) { return std::make_shared<NullResolver>(); });
Lang Hames2fe7acb2016-01-19 21:06:38 +0000223 SimpleCompiler Compile(*TM);
224
225 // Create a pair of unrelated modules:
226 //
227 // Module 1:
228 // int foo() { return 42; }
229 // Module 2:
230 // int bar() { return 7; }
231 //
232 // Both modules will share a memory manager. We want to verify that the
233 // second object is not loaded before the first one is finalized. To do this
234 // in a portable way, we abuse the
235 // RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is
236 // called once per object before any sections are allocated.
237
Mehdi Amini03b42e42016-04-14 21:59:01 +0000238 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000239 {
240 MB1.getModule()->setDataLayout(TM->createDataLayout());
241 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000242 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000243 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000244 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000245 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
246 Builder.CreateRet(FourtyTwo);
247 }
248
Lang Hames26620222017-06-22 21:06:54 +0000249 auto Obj1 =
250 std::make_shared<object::OwningBinary<object::ObjectFile>>(
251 Compile(*MB1.getModule()));
Lang Hames2fe7acb2016-01-19 21:06:38 +0000252
Mehdi Amini03b42e42016-04-14 21:59:01 +0000253 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000254 {
255 MB2.getModule()->setDataLayout(TM->createDataLayout());
256 Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000257 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000258 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000259 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000260 Value *Seven = ConstantInt::getSigned(Int32Ty, 7);
261 Builder.CreateRet(Seven);
262 }
Lang Hames26620222017-06-22 21:06:54 +0000263 auto Obj2 =
264 std::make_shared<object::OwningBinary<object::ObjectFile>>(
265 Compile(*MB2.getModule()));
Lang Hames2fe7acb2016-01-19 21:06:38 +0000266
Lang Hames4b546c92018-02-06 21:25:11 +0000267 auto H = cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj1)));
268 cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj2)));
Lang Hames4ce98662017-07-07 02:59:13 +0000269 cantFail(ObjLayer.emitAndFinalize(H));
270 cantFail(ObjLayer.removeObject(H));
271
Lang Hames2fe7acb2016-01-19 21:06:38 +0000272 // Only one call to needsToReserveAllocationSpace should have been made.
Lang Hames5b518162017-07-04 04:42:30 +0000273 EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1)
Lang Hames2fe7acb2016-01-19 21:06:38 +0000274 << "More than one call to needsToReserveAllocationSpace "
275 "(multiple unrelated objects loaded prior to finalization)";
276}
277
Lang Hames705db632017-09-28 17:43:07 +0000278TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
Lang Hames4b546c92018-02-06 21:25:11 +0000279 SymbolStringPool SSP;
280 ExecutionSession ES(SSP);
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000281 RTDyldObjectLinkingLayer ObjLayer(
Lang Hames4b546c92018-02-06 21:25:11 +0000282 ES, [](VModuleKey) { return nullptr; },
283 [](VModuleKey) { return std::make_shared<NullResolver>(); },
Lang Hames13cda492017-09-29 05:03:43 +0000284 [](RTDyldObjectLinkingLayer::ObjHandleT,
285 const RTDyldObjectLinkingLayer::ObjectPtr &obj,
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000286 const RuntimeDyld::LoadedObjectInfo &info) {});
Lang Hames705db632017-09-28 17:43:07 +0000287}
288
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000289} // end anonymous namespace