blob: 9b9bedbecc7971a3579ab341e9f8b2c13e2b4aed [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 Hames0976cee2018-02-09 02:30:40 +0000103 auto K = ES.allocateVModule();
104 cantFail(ObjLayer.addObject(K, Obj));
105 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000106 EXPECT_EQ(DebugSectionSeen, false)
107 << "Unexpected debug info section";
Lang Hames0976cee2018-02-09 02:30:40 +0000108 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000109 }
110
111 {
112 // Test with ProcessAllSections = true.
113 ObjLayer.setProcessAllSections(true);
Lang Hames0976cee2018-02-09 02:30:40 +0000114 auto K = ES.allocateVModule();
115 cantFail(ObjLayer.addObject(K, Obj));
116 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000117 EXPECT_EQ(DebugSectionSeen, true)
118 << "Expected debug info section not seen";
Lang Hames0976cee2018-02-09 02:30:40 +0000119 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000120 }
121}
122
Lang Hames67de5d22017-02-20 05:45:14 +0000123TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
Lang Hames859d73c2016-01-09 19:50:40 +0000124 if (!TM)
125 return;
126
Lang Hames4b546c92018-02-06 21:25:11 +0000127 SymbolStringPool SSP;
128 ExecutionSession ES(SSP);
129
Lang Hames5b518162017-07-04 04:42:30 +0000130 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
131
Lang Hames4b546c92018-02-06 21:25:11 +0000132 std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
133
134 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) { return MM; },
135 [&](VModuleKey K) {
136 auto I = Resolvers.find(K);
137 assert(I != Resolvers.end() &&
138 "Missing resolver");
139 auto R = std::move(I->second);
140 Resolvers.erase(I);
141 return R;
142 });
Lang Hames859d73c2016-01-09 19:50:40 +0000143 SimpleCompiler Compile(*TM);
144
145 // Create a pair of modules that will trigger recursive finalization:
146 // Module 1:
147 // int bar() { return 42; }
148 // Module 2:
149 // int bar();
150 // int foo() { return bar(); }
Lang Hames133f1532016-01-18 01:00:19 +0000151 //
152 // Verify that the memory manager is only finalized once (for Module 2).
153 // Failure suggests that finalize is being called on the inner RTDyld
154 // instance (for Module 1) which is unsafe, as it will prevent relocation of
155 // Module 2.
Lang Hames859d73c2016-01-09 19:50:40 +0000156
Mehdi Amini03b42e42016-04-14 21:59:01 +0000157 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000158 {
159 MB1.getModule()->setDataLayout(TM->createDataLayout());
160 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000161 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000162 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000163 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames859d73c2016-01-09 19:50:40 +0000164 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
165 Builder.CreateRet(FourtyTwo);
166 }
167
Lang Hames26620222017-06-22 21:06:54 +0000168 auto Obj1 =
169 std::make_shared<object::OwningBinary<object::ObjectFile>>(
170 Compile(*MB1.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000171
Mehdi Amini03b42e42016-04-14 21:59:01 +0000172 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000173 {
174 MB2.getModule()->setDataLayout(TM->createDataLayout());
175 Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
176 Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000177 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000178 IRBuilder<> Builder(FooEntry);
179 Builder.CreateRet(Builder.CreateCall(BarDecl));
180 }
Lang Hames26620222017-06-22 21:06:54 +0000181 auto Obj2 =
182 std::make_shared<object::OwningBinary<object::ObjectFile>>(
183 Compile(*MB2.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000184
Lang Hames4b546c92018-02-06 21:25:11 +0000185 auto K1 = ES.allocateVModule();
186 Resolvers[K1] = std::make_shared<NullResolver>();
187 cantFail(ObjLayer.addObject(K1, std::move(Obj1)));
188
189 auto K2 = ES.allocateVModule();
190 auto LegacyLookup = [&](const std::string &Name) {
191 return ObjLayer.findSymbol(Name, true);
192 };
193
194 Resolvers[K2] = createSymbolResolver(
195 [&](SymbolFlagsMap &SymbolFlags, const SymbolNameSet &Symbols) {
196 return cantFail(
197 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup));
Lang Hames859d73c2016-01-09 19:50:40 +0000198 },
Lang Hamese833fe82018-02-14 22:12:56 +0000199 [&](std::shared_ptr<AsynchronousSymbolQuery> Query,
200 const SymbolNameSet &Symbols) {
201 return lookupWithLegacyFn(*Query, Symbols, LegacyLookup);
Lang Hames859d73c2016-01-09 19:50:40 +0000202 });
203
Lang Hames0976cee2018-02-09 02:30:40 +0000204 cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
205 cantFail(ObjLayer.emitAndFinalize(K2));
206 cantFail(ObjLayer.removeObject(K2));
Lang Hames4ce98662017-07-07 02:59:13 +0000207
Lang Hames859d73c2016-01-09 19:50:40 +0000208 // Finalization of module 2 should trigger finalization of module 1.
209 // Verify that finalize on SMMW is only called once.
Lang Hames5b518162017-07-04 04:42:30 +0000210 EXPECT_EQ(MM->FinalizationCount, 1)
Lang Hames859d73c2016-01-09 19:50:40 +0000211 << "Extra call to finalize";
212}
213
Lang Hames67de5d22017-02-20 05:45:14 +0000214TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
Lang Hames2fe7acb2016-01-19 21:06:38 +0000215 if (!TM)
216 return;
217
Lang Hames4b546c92018-02-06 21:25:11 +0000218 SymbolStringPool SSP;
219 ExecutionSession ES(SSP);
220
Lang Hames5b518162017-07-04 04:42:30 +0000221 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
222
Lang Hames4b546c92018-02-06 21:25:11 +0000223 RTDyldObjectLinkingLayer ObjLayer(
224 ES, [&MM](VModuleKey) { return MM; },
225 [](VModuleKey) { return std::make_shared<NullResolver>(); });
Lang Hames2fe7acb2016-01-19 21:06:38 +0000226 SimpleCompiler Compile(*TM);
227
228 // Create a pair of unrelated modules:
229 //
230 // Module 1:
231 // int foo() { return 42; }
232 // Module 2:
233 // int bar() { return 7; }
234 //
235 // Both modules will share a memory manager. We want to verify that the
236 // second object is not loaded before the first one is finalized. To do this
237 // in a portable way, we abuse the
238 // RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is
239 // called once per object before any sections are allocated.
240
Mehdi Amini03b42e42016-04-14 21:59:01 +0000241 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000242 {
243 MB1.getModule()->setDataLayout(TM->createDataLayout());
244 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000245 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000246 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000247 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000248 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
249 Builder.CreateRet(FourtyTwo);
250 }
251
Lang Hames26620222017-06-22 21:06:54 +0000252 auto Obj1 =
253 std::make_shared<object::OwningBinary<object::ObjectFile>>(
254 Compile(*MB1.getModule()));
Lang Hames2fe7acb2016-01-19 21:06:38 +0000255
Mehdi Amini03b42e42016-04-14 21:59:01 +0000256 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000257 {
258 MB2.getModule()->setDataLayout(TM->createDataLayout());
259 Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000260 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000261 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000262 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000263 Value *Seven = ConstantInt::getSigned(Int32Ty, 7);
264 Builder.CreateRet(Seven);
265 }
Lang Hames26620222017-06-22 21:06:54 +0000266 auto Obj2 =
267 std::make_shared<object::OwningBinary<object::ObjectFile>>(
268 Compile(*MB2.getModule()));
Lang Hames2fe7acb2016-01-19 21:06:38 +0000269
Lang Hames0976cee2018-02-09 02:30:40 +0000270 auto K = ES.allocateVModule();
271 cantFail(ObjLayer.addObject(K, std::move(Obj1)));
Lang Hames4b546c92018-02-06 21:25:11 +0000272 cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj2)));
Lang Hames0976cee2018-02-09 02:30:40 +0000273 cantFail(ObjLayer.emitAndFinalize(K));
274 cantFail(ObjLayer.removeObject(K));
Lang Hames4ce98662017-07-07 02:59:13 +0000275
Lang Hames2fe7acb2016-01-19 21:06:38 +0000276 // Only one call to needsToReserveAllocationSpace should have been made.
Lang Hames5b518162017-07-04 04:42:30 +0000277 EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1)
Lang Hames2fe7acb2016-01-19 21:06:38 +0000278 << "More than one call to needsToReserveAllocationSpace "
279 "(multiple unrelated objects loaded prior to finalization)";
280}
281
Lang Hames705db632017-09-28 17:43:07 +0000282TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
Lang Hames4b546c92018-02-06 21:25:11 +0000283 SymbolStringPool SSP;
284 ExecutionSession ES(SSP);
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000285 RTDyldObjectLinkingLayer ObjLayer(
Lang Hames4b546c92018-02-06 21:25:11 +0000286 ES, [](VModuleKey) { return nullptr; },
287 [](VModuleKey) { return std::make_shared<NullResolver>(); },
Lang Hames0976cee2018-02-09 02:30:40 +0000288 [](VModuleKey, const RTDyldObjectLinkingLayer::ObjectPtr &obj,
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000289 const RuntimeDyld::LoadedObjectInfo &info) {});
Lang Hames705db632017-09-28 17:43:07 +0000290}
291
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000292} // end anonymous namespace