blob: 740abedeed952375a1702d707f8e215ad034d345 [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
Lang Hames1cd3dd02018-02-14 22:13:02 +000073 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) {
74 return RTDyldObjectLinkingLayer::Resources{
75 MM, std::make_shared<NullResolver>()};
76 });
Lang Hames5f7fcef2015-10-29 03:53:42 +000077
Mehdi Amini03b42e42016-04-14 21:59:01 +000078 LLVMContext Context;
79 auto M = llvm::make_unique<Module>("", Context);
Lang Hames5f7fcef2015-10-29 03:53:42 +000080 M->setTargetTriple("x86_64-unknown-linux-gnu");
Mehdi Amini03b42e42016-04-14 21:59:01 +000081 Type *Int32Ty = IntegerType::get(Context, 32);
Lang Hames5f7fcef2015-10-29 03:53:42 +000082 GlobalVariable *GV =
83 new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
84 ConstantInt::get(Int32Ty, 42), "foo");
85
86 GV->setSection(".debug_str");
87
Lang Hamesd22bade2017-04-04 17:03:49 +000088
89 // Initialize the native target in case this is the first unit test
90 // to try to build a TM.
91 OrcNativeTarget::initialize();
Lang Hames5f7fcef2015-10-29 03:53:42 +000092 std::unique_ptr<TargetMachine> TM(
93 EngineBuilder().selectTarget(Triple(M->getTargetTriple()), "", "",
94 SmallVector<std::string, 1>()));
95 if (!TM)
96 return;
97
Lang Hames26620222017-06-22 21:06:54 +000098 auto Obj =
99 std::make_shared<object::OwningBinary<object::ObjectFile>>(
100 SimpleCompiler(*TM)(*M));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000101
Lang Hames5f7fcef2015-10-29 03:53:42 +0000102 {
103 // Test with ProcessAllSections = false (the default).
Lang Hames0976cee2018-02-09 02:30:40 +0000104 auto K = ES.allocateVModule();
105 cantFail(ObjLayer.addObject(K, Obj));
106 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000107 EXPECT_EQ(DebugSectionSeen, false)
108 << "Unexpected debug info section";
Lang Hames0976cee2018-02-09 02:30:40 +0000109 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000110 }
111
112 {
113 // Test with ProcessAllSections = true.
114 ObjLayer.setProcessAllSections(true);
Lang Hames0976cee2018-02-09 02:30:40 +0000115 auto K = ES.allocateVModule();
116 cantFail(ObjLayer.addObject(K, Obj));
117 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000118 EXPECT_EQ(DebugSectionSeen, true)
119 << "Expected debug info section not seen";
Lang Hames0976cee2018-02-09 02:30:40 +0000120 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000121 }
122}
123
Lang Hames67de5d22017-02-20 05:45:14 +0000124TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
Lang Hames859d73c2016-01-09 19:50:40 +0000125 if (!TM)
126 return;
127
Lang Hames4b546c92018-02-06 21:25:11 +0000128 SymbolStringPool SSP;
129 ExecutionSession ES(SSP);
130
Lang Hames5b518162017-07-04 04:42:30 +0000131 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
132
Lang Hames4b546c92018-02-06 21:25:11 +0000133 std::map<orc::VModuleKey, std::shared_ptr<orc::SymbolResolver>> Resolvers;
134
Lang Hames1cd3dd02018-02-14 22:13:02 +0000135 RTDyldObjectLinkingLayer ObjLayer(ES, [&](VModuleKey K) {
136 auto I = Resolvers.find(K);
137 assert(I != Resolvers.end() && "Missing resolver");
138 auto R = std::move(I->second);
139 Resolvers.erase(I);
140 return RTDyldObjectLinkingLayer::Resources{MM, std::move(R)};
141 });
Lang Hames859d73c2016-01-09 19:50:40 +0000142 SimpleCompiler Compile(*TM);
143
144 // Create a pair of modules that will trigger recursive finalization:
145 // Module 1:
146 // int bar() { return 42; }
147 // Module 2:
148 // int bar();
149 // int foo() { return bar(); }
Lang Hames133f1532016-01-18 01:00:19 +0000150 //
151 // Verify that the memory manager is only finalized once (for Module 2).
152 // Failure suggests that finalize is being called on the inner RTDyld
153 // instance (for Module 1) which is unsafe, as it will prevent relocation of
154 // Module 2.
Lang Hames859d73c2016-01-09 19:50:40 +0000155
Mehdi Amini03b42e42016-04-14 21:59:01 +0000156 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000157 {
158 MB1.getModule()->setDataLayout(TM->createDataLayout());
159 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000160 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000161 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000162 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames859d73c2016-01-09 19:50:40 +0000163 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
164 Builder.CreateRet(FourtyTwo);
165 }
166
Lang Hames26620222017-06-22 21:06:54 +0000167 auto Obj1 =
168 std::make_shared<object::OwningBinary<object::ObjectFile>>(
169 Compile(*MB1.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000170
Mehdi Amini03b42e42016-04-14 21:59:01 +0000171 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000172 {
173 MB2.getModule()->setDataLayout(TM->createDataLayout());
174 Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
175 Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000176 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000177 IRBuilder<> Builder(FooEntry);
178 Builder.CreateRet(Builder.CreateCall(BarDecl));
179 }
Lang Hames26620222017-06-22 21:06:54 +0000180 auto Obj2 =
181 std::make_shared<object::OwningBinary<object::ObjectFile>>(
182 Compile(*MB2.getModule()));
Lang Hames859d73c2016-01-09 19:50:40 +0000183
Lang Hames4b546c92018-02-06 21:25:11 +0000184 auto K1 = ES.allocateVModule();
185 Resolvers[K1] = std::make_shared<NullResolver>();
186 cantFail(ObjLayer.addObject(K1, std::move(Obj1)));
187
188 auto K2 = ES.allocateVModule();
189 auto LegacyLookup = [&](const std::string &Name) {
190 return ObjLayer.findSymbol(Name, true);
191 };
192
193 Resolvers[K2] = createSymbolResolver(
194 [&](SymbolFlagsMap &SymbolFlags, const SymbolNameSet &Symbols) {
195 return cantFail(
196 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup));
Lang Hames859d73c2016-01-09 19:50:40 +0000197 },
Lang Hamese833fe82018-02-14 22:12:56 +0000198 [&](std::shared_ptr<AsynchronousSymbolQuery> Query,
199 const SymbolNameSet &Symbols) {
200 return lookupWithLegacyFn(*Query, Symbols, LegacyLookup);
Lang Hames859d73c2016-01-09 19:50:40 +0000201 });
202
Lang Hames0976cee2018-02-09 02:30:40 +0000203 cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
204 cantFail(ObjLayer.emitAndFinalize(K2));
205 cantFail(ObjLayer.removeObject(K2));
Lang Hames4ce98662017-07-07 02:59:13 +0000206
Lang Hames859d73c2016-01-09 19:50:40 +0000207 // Finalization of module 2 should trigger finalization of module 1.
208 // Verify that finalize on SMMW is only called once.
Lang Hames5b518162017-07-04 04:42:30 +0000209 EXPECT_EQ(MM->FinalizationCount, 1)
Lang Hames859d73c2016-01-09 19:50:40 +0000210 << "Extra call to finalize";
211}
212
Lang Hames67de5d22017-02-20 05:45:14 +0000213TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
Lang Hames2fe7acb2016-01-19 21:06:38 +0000214 if (!TM)
215 return;
216
Lang Hames4b546c92018-02-06 21:25:11 +0000217 SymbolStringPool SSP;
218 ExecutionSession ES(SSP);
219
Lang Hames5b518162017-07-04 04:42:30 +0000220 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
221
Lang Hames1cd3dd02018-02-14 22:13:02 +0000222 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey K) {
223 return RTDyldObjectLinkingLayer::Resources{
224 MM, std::make_shared<NullResolver>()};
225 });
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 Hames1cd3dd02018-02-14 22:13:02 +0000286 ES,
287 [](VModuleKey) {
288 return RTDyldObjectLinkingLayer::Resources{
289 nullptr, std::make_shared<NullResolver>()};
290 },
Lang Hames0976cee2018-02-09 02:30:40 +0000291 [](VModuleKey, const RTDyldObjectLinkingLayer::ObjectPtr &obj,
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000292 const RuntimeDyld::LoadedObjectInfo &info) {});
Lang Hames705db632017-09-28 17:43:07 +0000293}
294
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000295} // end anonymous namespace