blob: 650fd5248125e02ea49b5711ee425a1ddf4ee78f [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 Hames3fdfc042018-04-02 20:57:56 +000070 ExecutionSession ES(std::make_shared<SymbolStringPool>());
Lang Hames4b546c92018-02-06 21:25:11 +000071
Lang Hames1cd3dd02018-02-14 22:13:02 +000072 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey) {
73 return RTDyldObjectLinkingLayer::Resources{
74 MM, std::make_shared<NullResolver>()};
75 });
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 Hames589eece2018-02-21 21:55:49 +000097 auto Obj = SimpleCompiler(*TM)(*M);
Lang Hames5f7fcef2015-10-29 03:53:42 +000098
Lang Hames5f7fcef2015-10-29 03:53:42 +000099 {
100 // Test with ProcessAllSections = false (the default).
Lang Hames0976cee2018-02-09 02:30:40 +0000101 auto K = ES.allocateVModule();
Lang Hames589eece2018-02-21 21:55:49 +0000102 cantFail(ObjLayer.addObject(
103 K, MemoryBuffer::getMemBufferCopy(Obj->getBuffer())));
Lang Hames0976cee2018-02-09 02:30:40 +0000104 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000105 EXPECT_EQ(DebugSectionSeen, false)
106 << "Unexpected debug info section";
Lang Hames0976cee2018-02-09 02:30:40 +0000107 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000108 }
109
110 {
111 // Test with ProcessAllSections = true.
112 ObjLayer.setProcessAllSections(true);
Lang Hames0976cee2018-02-09 02:30:40 +0000113 auto K = ES.allocateVModule();
Lang Hames589eece2018-02-21 21:55:49 +0000114 cantFail(ObjLayer.addObject(K, std::move(Obj)));
Lang Hames0976cee2018-02-09 02:30:40 +0000115 cantFail(ObjLayer.emitAndFinalize(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000116 EXPECT_EQ(DebugSectionSeen, true)
117 << "Expected debug info section not seen";
Lang Hames0976cee2018-02-09 02:30:40 +0000118 cantFail(ObjLayer.removeObject(K));
Lang Hames5f7fcef2015-10-29 03:53:42 +0000119 }
120}
121
Lang Hames67de5d22017-02-20 05:45:14 +0000122TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoDuplicateFinalization) {
Lang Hamesa95b0df2018-03-28 03:41:45 +0000123 if (!SupportsJIT)
Lang Hames859d73c2016-01-09 19:50:40 +0000124 return;
125
Lang Hames3fdfc042018-04-02 20:57:56 +0000126 ExecutionSession ES(std::make_shared<SymbolStringPool>());
Lang Hames4b546c92018-02-06 21:25:11 +0000127
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
Lang Hames1cd3dd02018-02-14 22:13:02 +0000132 RTDyldObjectLinkingLayer ObjLayer(ES, [&](VModuleKey K) {
133 auto I = Resolvers.find(K);
134 assert(I != Resolvers.end() && "Missing resolver");
135 auto R = std::move(I->second);
136 Resolvers.erase(I);
137 return RTDyldObjectLinkingLayer::Resources{MM, std::move(R)};
138 });
Lang Hames859d73c2016-01-09 19:50:40 +0000139 SimpleCompiler Compile(*TM);
140
141 // Create a pair of modules that will trigger recursive finalization:
142 // Module 1:
143 // int bar() { return 42; }
144 // Module 2:
145 // int bar();
146 // int foo() { return bar(); }
Lang Hames133f1532016-01-18 01:00:19 +0000147 //
148 // Verify that the memory manager is only finalized once (for Module 2).
149 // Failure suggests that finalize is being called on the inner RTDyld
150 // instance (for Module 1) which is unsafe, as it will prevent relocation of
151 // Module 2.
Lang Hames859d73c2016-01-09 19:50:40 +0000152
Mehdi Amini03b42e42016-04-14 21:59:01 +0000153 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000154 {
155 MB1.getModule()->setDataLayout(TM->createDataLayout());
156 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000157 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000158 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000159 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames859d73c2016-01-09 19:50:40 +0000160 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
161 Builder.CreateRet(FourtyTwo);
162 }
163
Lang Hames589eece2018-02-21 21:55:49 +0000164 auto Obj1 = Compile(*MB1.getModule());
Lang Hames859d73c2016-01-09 19:50:40 +0000165
Mehdi Amini03b42e42016-04-14 21:59:01 +0000166 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames859d73c2016-01-09 19:50:40 +0000167 {
168 MB2.getModule()->setDataLayout(TM->createDataLayout());
169 Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
170 Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000171 BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
Lang Hames859d73c2016-01-09 19:50:40 +0000172 IRBuilder<> Builder(FooEntry);
173 Builder.CreateRet(Builder.CreateCall(BarDecl));
174 }
Lang Hames589eece2018-02-21 21:55:49 +0000175 auto Obj2 = Compile(*MB2.getModule());
Lang Hames859d73c2016-01-09 19:50:40 +0000176
Lang Hames4b546c92018-02-06 21:25:11 +0000177 auto K1 = ES.allocateVModule();
178 Resolvers[K1] = std::make_shared<NullResolver>();
179 cantFail(ObjLayer.addObject(K1, std::move(Obj1)));
180
181 auto K2 = ES.allocateVModule();
182 auto LegacyLookup = [&](const std::string &Name) {
183 return ObjLayer.findSymbol(Name, true);
184 };
185
186 Resolvers[K2] = createSymbolResolver(
187 [&](SymbolFlagsMap &SymbolFlags, const SymbolNameSet &Symbols) {
188 return cantFail(
189 lookupFlagsWithLegacyFn(SymbolFlags, Symbols, LegacyLookup));
Lang Hames859d73c2016-01-09 19:50:40 +0000190 },
Lang Hamese833fe82018-02-14 22:12:56 +0000191 [&](std::shared_ptr<AsynchronousSymbolQuery> Query,
192 const SymbolNameSet &Symbols) {
193 return lookupWithLegacyFn(*Query, Symbols, LegacyLookup);
Lang Hames859d73c2016-01-09 19:50:40 +0000194 });
195
Lang Hames0976cee2018-02-09 02:30:40 +0000196 cantFail(ObjLayer.addObject(K2, std::move(Obj2)));
197 cantFail(ObjLayer.emitAndFinalize(K2));
198 cantFail(ObjLayer.removeObject(K2));
Lang Hames4ce98662017-07-07 02:59:13 +0000199
Lang Hames859d73c2016-01-09 19:50:40 +0000200 // Finalization of module 2 should trigger finalization of module 1.
201 // Verify that finalize on SMMW is only called once.
Lang Hames5b518162017-07-04 04:42:30 +0000202 EXPECT_EQ(MM->FinalizationCount, 1)
Lang Hames859d73c2016-01-09 19:50:40 +0000203 << "Extra call to finalize";
204}
205
Lang Hames67de5d22017-02-20 05:45:14 +0000206TEST_F(RTDyldObjectLinkingLayerExecutionTest, NoPrematureAllocation) {
Lang Hamesa95b0df2018-03-28 03:41:45 +0000207 if (!SupportsJIT)
Lang Hames2fe7acb2016-01-19 21:06:38 +0000208 return;
209
Lang Hames3fdfc042018-04-02 20:57:56 +0000210 ExecutionSession ES(std::make_shared<SymbolStringPool>());
Lang Hames4b546c92018-02-06 21:25:11 +0000211
Lang Hames5b518162017-07-04 04:42:30 +0000212 auto MM = std::make_shared<SectionMemoryManagerWrapper>();
213
Lang Hames1cd3dd02018-02-14 22:13:02 +0000214 RTDyldObjectLinkingLayer ObjLayer(ES, [&MM](VModuleKey K) {
215 return RTDyldObjectLinkingLayer::Resources{
216 MM, std::make_shared<NullResolver>()};
217 });
Lang Hames2fe7acb2016-01-19 21:06:38 +0000218 SimpleCompiler Compile(*TM);
219
220 // Create a pair of unrelated modules:
221 //
222 // Module 1:
223 // int foo() { return 42; }
224 // Module 2:
225 // int bar() { return 7; }
226 //
227 // Both modules will share a memory manager. We want to verify that the
228 // second object is not loaded before the first one is finalized. To do this
229 // in a portable way, we abuse the
230 // RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is
231 // called once per object before any sections are allocated.
232
Mehdi Amini03b42e42016-04-14 21:59:01 +0000233 ModuleBuilder MB1(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000234 {
235 MB1.getModule()->setDataLayout(TM->createDataLayout());
236 Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000237 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000238 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000239 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000240 Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
241 Builder.CreateRet(FourtyTwo);
242 }
243
Lang Hames589eece2018-02-21 21:55:49 +0000244 auto Obj1 = Compile(*MB1.getModule());
Lang Hames2fe7acb2016-01-19 21:06:38 +0000245
Mehdi Amini03b42e42016-04-14 21:59:01 +0000246 ModuleBuilder MB2(Context, "", "dummy");
Lang Hames2fe7acb2016-01-19 21:06:38 +0000247 {
248 MB2.getModule()->setDataLayout(TM->createDataLayout());
249 Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar");
Mehdi Amini03b42e42016-04-14 21:59:01 +0000250 BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000251 IRBuilder<> Builder(BarEntry);
Mehdi Amini03b42e42016-04-14 21:59:01 +0000252 IntegerType *Int32Ty = IntegerType::get(Context, 32);
Lang Hames2fe7acb2016-01-19 21:06:38 +0000253 Value *Seven = ConstantInt::getSigned(Int32Ty, 7);
254 Builder.CreateRet(Seven);
255 }
Lang Hames589eece2018-02-21 21:55:49 +0000256 auto Obj2 = Compile(*MB2.getModule());
Lang Hames2fe7acb2016-01-19 21:06:38 +0000257
Lang Hames0976cee2018-02-09 02:30:40 +0000258 auto K = ES.allocateVModule();
259 cantFail(ObjLayer.addObject(K, std::move(Obj1)));
Lang Hames4b546c92018-02-06 21:25:11 +0000260 cantFail(ObjLayer.addObject(ES.allocateVModule(), std::move(Obj2)));
Lang Hames0976cee2018-02-09 02:30:40 +0000261 cantFail(ObjLayer.emitAndFinalize(K));
262 cantFail(ObjLayer.removeObject(K));
Lang Hames4ce98662017-07-07 02:59:13 +0000263
Lang Hames2fe7acb2016-01-19 21:06:38 +0000264 // Only one call to needsToReserveAllocationSpace should have been made.
Lang Hames5b518162017-07-04 04:42:30 +0000265 EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1)
Lang Hames2fe7acb2016-01-19 21:06:38 +0000266 << "More than one call to needsToReserveAllocationSpace "
267 "(multiple unrelated objects loaded prior to finalization)";
268}
269
Lang Hames705db632017-09-28 17:43:07 +0000270TEST_F(RTDyldObjectLinkingLayerExecutionTest, TestNotifyLoadedSignature) {
Lang Hames3fdfc042018-04-02 20:57:56 +0000271 ExecutionSession ES(std::make_shared<SymbolStringPool>());
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000272 RTDyldObjectLinkingLayer ObjLayer(
Lang Hames1cd3dd02018-02-14 22:13:02 +0000273 ES,
274 [](VModuleKey) {
275 return RTDyldObjectLinkingLayer::Resources{
276 nullptr, std::make_shared<NullResolver>()};
277 },
Lang Hames589eece2018-02-21 21:55:49 +0000278 [](VModuleKey, const object::ObjectFile &obj,
Evgeniy Stepanovfa769be2017-09-28 19:43:53 +0000279 const RuntimeDyld::LoadedObjectInfo &info) {});
Lang Hames705db632017-09-28 17:43:07 +0000280}
281
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000282} // end anonymous namespace