Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 1 | //===--- 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 Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 14 | #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 | |
| 23 | using namespace llvm; |
| 24 | using namespace llvm::orc; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class RTDyldObjectLinkingLayer2ExecutionTest : public testing::Test, |
| 29 | public OrcExecutionTest {}; |
| 30 | |
| 31 | class SectionMemoryManagerWrapper : public SectionMemoryManager { |
| 32 | public: |
| 33 | int FinalizationCount = 0; |
| 34 | int NeedsToReserveAllocationSpaceCount = 0; |
| 35 | |
| 36 | bool needsToReserveAllocationSpace() override { |
| 37 | ++NeedsToReserveAllocationSpaceCount; |
| 38 | return SectionMemoryManager::needsToReserveAllocationSpace(); |
| 39 | } |
| 40 | |
| 41 | bool finalizeMemory(std::string *ErrMsg = nullptr) override { |
| 42 | ++FinalizationCount; |
| 43 | return SectionMemoryManager::finalizeMemory(ErrMsg); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | // Adds an object with a debug section to RuntimeDyld and then returns whether |
| 48 | // the debug section was passed to the memory manager. |
| 49 | static bool testSetProcessAllSections(std::unique_ptr<MemoryBuffer> Obj, |
| 50 | bool ProcessAllSections) { |
| 51 | class MemoryManagerWrapper : public SectionMemoryManager { |
| 52 | public: |
| 53 | MemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {} |
| 54 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 55 | unsigned SectionID, StringRef SectionName, |
| 56 | bool IsReadOnly) override { |
| 57 | if (SectionName == ".debug_str") |
| 58 | DebugSeen = true; |
| 59 | return SectionMemoryManager::allocateDataSection( |
| 60 | Size, Alignment, SectionID, SectionName, IsReadOnly); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | bool &DebugSeen; |
| 65 | }; |
| 66 | |
| 67 | bool DebugSectionSeen = false; |
| 68 | auto MM = std::make_shared<MemoryManagerWrapper>(DebugSectionSeen); |
| 69 | |
| 70 | ExecutionSession ES(std::make_shared<SymbolStringPool>()); |
| 71 | auto &JD = ES.createJITDylib("main"); |
| 72 | auto Foo = ES.getSymbolStringPool().intern("foo"); |
| 73 | |
| 74 | RTDyldObjectLinkingLayer2 ObjLayer(ES, [&MM](VModuleKey) { return MM; }); |
| 75 | |
| 76 | auto OnResolveDoNothing = [](Expected<SymbolMap> R) { |
| 77 | cantFail(std::move(R)); |
| 78 | }; |
| 79 | |
| 80 | auto OnReadyDoNothing = [](Error Err) { cantFail(std::move(Err)); }; |
| 81 | |
| 82 | ObjLayer.setProcessAllSections(ProcessAllSections); |
| 83 | auto K = ES.allocateVModule(); |
| 84 | cantFail(ObjLayer.add(JD, K, std::move(Obj))); |
| 85 | ES.lookup({&JD}, {Foo}, OnResolveDoNothing, OnReadyDoNothing, |
| 86 | NoDependenciesToRegister); |
| 87 | return DebugSectionSeen; |
| 88 | } |
| 89 | |
| 90 | TEST(RTDyldObjectLinkingLayer2Test, TestSetProcessAllSections) { |
| 91 | LLVMContext Context; |
| 92 | auto M = llvm::make_unique<Module>("", Context); |
| 93 | M->setTargetTriple("x86_64-unknown-linux-gnu"); |
| 94 | Type *Int32Ty = IntegerType::get(Context, 32); |
| 95 | GlobalVariable *GV = |
| 96 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
| 97 | ConstantInt::get(Int32Ty, 42), "foo"); |
| 98 | |
| 99 | GV->setSection(".debug_str"); |
| 100 | |
| 101 | // Initialize the native target in case this is the first unit test |
| 102 | // to try to build a TM. |
| 103 | OrcNativeTarget::initialize(); |
| 104 | std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget( |
| 105 | Triple(M->getTargetTriple()), "", "", SmallVector<std::string, 1>())); |
| 106 | if (!TM) |
| 107 | return; |
| 108 | |
| 109 | auto Obj = SimpleCompiler(*TM)(*M); |
| 110 | |
| 111 | EXPECT_FALSE(testSetProcessAllSections( |
| 112 | MemoryBuffer::getMemBufferCopy(Obj->getBuffer()), false)) |
| 113 | << "Debug section seen despite ProcessAllSections being false"; |
| 114 | EXPECT_TRUE(testSetProcessAllSections(std::move(Obj), true)) |
| 115 | << "Expected to see debug section when ProcessAllSections is true"; |
| 116 | } |
| 117 | |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 118 | TEST(RTDyldObjectLinkingLayer2Test, TestOverrideObjectFlags) { |
| 119 | |
| 120 | OrcNativeTarget::initialize(); |
| 121 | |
| 122 | std::unique_ptr<TargetMachine> TM( |
| 123 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 124 | SmallVector<std::string, 1>())); |
| 125 | |
| 126 | if (!TM) |
| 127 | return; |
| 128 | |
| 129 | // Our compiler is going to modify symbol visibility settings without telling |
| 130 | // ORC. This will test our ability to override the flags later. |
| 131 | class FunkySimpleCompiler : public SimpleCompiler { |
| 132 | public: |
| 133 | FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {} |
| 134 | |
| 135 | CompileResult operator()(Module &M) { |
| 136 | auto *Foo = M.getFunction("foo"); |
| 137 | assert(Foo && "Expected function Foo not found"); |
| 138 | Foo->setVisibility(GlobalValue::HiddenVisibility); |
| 139 | return SimpleCompiler::operator()(M); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | // Create a module with two void() functions: foo and bar. |
| 144 | LLVMContext Context; |
| 145 | std::unique_ptr<Module> M; |
| 146 | { |
| 147 | ModuleBuilder MB(Context, TM->getTargetTriple().str(), "dummy"); |
| 148 | MB.getModule()->setDataLayout(TM->createDataLayout()); |
| 149 | |
| 150 | Function *FooImpl = MB.createFunctionDecl<void()>("foo"); |
| 151 | BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl); |
| 152 | IRBuilder<> B1(FooEntry); |
| 153 | B1.CreateRetVoid(); |
| 154 | |
| 155 | Function *BarImpl = MB.createFunctionDecl<void()>("bar"); |
| 156 | BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl); |
| 157 | IRBuilder<> B2(BarEntry); |
| 158 | B2.CreateRetVoid(); |
| 159 | |
| 160 | M = MB.takeModule(); |
| 161 | } |
| 162 | |
| 163 | // Create a simple stack and set the override flags option. |
| 164 | ExecutionSession ES; |
| 165 | auto &JD = ES.createJITDylib("main"); |
| 166 | auto Foo = ES.getSymbolStringPool().intern("foo"); |
| 167 | RTDyldObjectLinkingLayer2 ObjLayer( |
| 168 | ES, [](VModuleKey) { return std::make_shared<SectionMemoryManager>(); }); |
| 169 | IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM)); |
| 170 | |
| 171 | ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); |
| 172 | |
| 173 | cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M))); |
| 174 | ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); }, |
| 175 | [](Error Err) { cantFail(std::move(Err)); }, |
| 176 | NoDependenciesToRegister); |
| 177 | } |
| 178 | |
| 179 | TEST(RTDyldObjectLinkingLayer2Test, TestAutoClaimResponsibilityForSymbols) { |
| 180 | |
| 181 | OrcNativeTarget::initialize(); |
| 182 | |
| 183 | std::unique_ptr<TargetMachine> TM( |
| 184 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 185 | SmallVector<std::string, 1>())); |
| 186 | |
| 187 | if (!TM) |
| 188 | return; |
| 189 | |
| 190 | // Our compiler is going to add a new symbol without telling ORC. |
| 191 | // This will test our ability to auto-claim responsibility later. |
| 192 | class FunkySimpleCompiler : public SimpleCompiler { |
| 193 | public: |
| 194 | FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {} |
| 195 | |
| 196 | CompileResult operator()(Module &M) { |
| 197 | Function *BarImpl = |
| 198 | Function::Create(TypeBuilder<void(), false>::get(M.getContext()), |
| 199 | GlobalValue::ExternalLinkage, "bar", &M); |
| 200 | BasicBlock *BarEntry = |
| 201 | BasicBlock::Create(M.getContext(), "entry", BarImpl); |
| 202 | IRBuilder<> B(BarEntry); |
| 203 | B.CreateRetVoid(); |
| 204 | |
| 205 | return SimpleCompiler::operator()(M); |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | // Create a module with two void() functions: foo and bar. |
| 210 | LLVMContext Context; |
| 211 | std::unique_ptr<Module> M; |
| 212 | { |
| 213 | ModuleBuilder MB(Context, TM->getTargetTriple().str(), "dummy"); |
| 214 | MB.getModule()->setDataLayout(TM->createDataLayout()); |
| 215 | |
| 216 | Function *FooImpl = MB.createFunctionDecl<void()>("foo"); |
| 217 | BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl); |
| 218 | IRBuilder<> B(FooEntry); |
| 219 | B.CreateRetVoid(); |
| 220 | |
| 221 | M = MB.takeModule(); |
| 222 | } |
| 223 | |
| 224 | // Create a simple stack and set the override flags option. |
| 225 | ExecutionSession ES; |
| 226 | auto &JD = ES.createJITDylib("main"); |
| 227 | auto Foo = ES.getSymbolStringPool().intern("foo"); |
| 228 | RTDyldObjectLinkingLayer2 ObjLayer( |
| 229 | ES, [](VModuleKey) { return std::make_shared<SectionMemoryManager>(); }); |
| 230 | IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM)); |
| 231 | |
| 232 | ObjLayer.setAutoClaimResponsibilityForObjectSymbols(true); |
| 233 | |
| 234 | cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M))); |
| 235 | ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); }, |
| 236 | [](Error Err) { cantFail(std::move(Err)); }, |
| 237 | NoDependenciesToRegister); |
| 238 | } |
| 239 | |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 240 | TEST(RTDyldObjectLinkingLayer2Test, NoDuplicateFinalization) { |
| 241 | // Create a pair of modules that will trigger recursive finalization: |
| 242 | // Module 1: |
| 243 | // int bar() { return 42; } |
| 244 | // Module 2: |
| 245 | // int bar(); |
| 246 | // int foo() { return bar(); } |
| 247 | // |
| 248 | // Verify that the memory manager is only finalized once (for Module 2). |
| 249 | // Failure suggests that finalize is being called on the inner RTDyld |
| 250 | // instance (for Module 1) which is unsafe, as it will prevent relocation of |
| 251 | // Module 2. |
| 252 | |
| 253 | // Initialize the native target in case this is the first unit test |
| 254 | // to try to build a TM. |
| 255 | OrcNativeTarget::initialize(); |
| 256 | std::unique_ptr<TargetMachine> TM( |
| 257 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 258 | SmallVector<std::string, 1>())); |
| 259 | |
| 260 | if (!TM) |
| 261 | return; |
| 262 | |
| 263 | LLVMContext Context; |
| 264 | ExecutionSession ES(std::make_shared<SymbolStringPool>()); |
| 265 | auto &JD = ES.createJITDylib("main"); |
| 266 | |
| 267 | auto Foo = ES.getSymbolStringPool().intern("foo"); |
| 268 | |
| 269 | auto MM = std::make_shared<SectionMemoryManagerWrapper>(); |
| 270 | |
| 271 | RTDyldObjectLinkingLayer2 ObjLayer(ES, [&](VModuleKey K) { return MM; }); |
| 272 | |
| 273 | SimpleCompiler Compile(*TM); |
| 274 | |
| 275 | ModuleBuilder MB1(Context, TM->getTargetTriple().str(), "dummy"); |
| 276 | { |
| 277 | MB1.getModule()->setDataLayout(TM->createDataLayout()); |
| 278 | Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar"); |
| 279 | BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl); |
| 280 | IRBuilder<> Builder(BarEntry); |
| 281 | IntegerType *Int32Ty = IntegerType::get(Context, 32); |
| 282 | Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42); |
| 283 | Builder.CreateRet(FourtyTwo); |
| 284 | } |
| 285 | |
| 286 | auto Obj1 = Compile(*MB1.getModule()); |
| 287 | |
| 288 | ModuleBuilder MB2(Context, TM->getTargetTriple().str(), "dummy"); |
| 289 | { |
| 290 | MB2.getModule()->setDataLayout(TM->createDataLayout()); |
| 291 | Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar"); |
| 292 | Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo"); |
| 293 | BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl); |
| 294 | IRBuilder<> Builder(FooEntry); |
| 295 | Builder.CreateRet(Builder.CreateCall(BarDecl)); |
| 296 | } |
| 297 | auto Obj2 = Compile(*MB2.getModule()); |
| 298 | |
| 299 | auto K1 = ES.allocateVModule(); |
| 300 | cantFail(ObjLayer.add(JD, K1, std::move(Obj1))); |
| 301 | |
| 302 | auto K2 = ES.allocateVModule(); |
| 303 | cantFail(ObjLayer.add(JD, K2, std::move(Obj2))); |
| 304 | |
| 305 | auto OnResolve = [](Expected<SymbolMap> Symbols) { |
| 306 | cantFail(std::move(Symbols)); |
| 307 | }; |
| 308 | auto OnReady = [](Error Err) { cantFail(std::move(Err)); }; |
| 309 | |
| 310 | ES.lookup({&JD}, {Foo}, OnResolve, OnReady, NoDependenciesToRegister); |
| 311 | |
| 312 | // Finalization of module 2 should trigger finalization of module 1. |
| 313 | // Verify that finalize on SMMW is only called once. |
| 314 | EXPECT_EQ(MM->FinalizationCount, 1) << "Extra call to finalize"; |
| 315 | } |
| 316 | |
| 317 | TEST(RTDyldObjectLinkingLayer2Test, NoPrematureAllocation) { |
| 318 | // Create a pair of unrelated modules: |
| 319 | // |
| 320 | // Module 1: |
| 321 | // int foo() { return 42; } |
| 322 | // Module 2: |
| 323 | // int bar() { return 7; } |
| 324 | // |
| 325 | // Both modules will share a memory manager. We want to verify that the |
| 326 | // second object is not loaded before the first one is finalized. To do this |
| 327 | // in a portable way, we abuse the |
| 328 | // RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is |
| 329 | // called once per object before any sections are allocated. |
| 330 | |
| 331 | // Initialize the native target in case this is the first unit test |
| 332 | // to try to build a TM. |
| 333 | OrcNativeTarget::initialize(); |
| 334 | std::unique_ptr<TargetMachine> TM( |
| 335 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 336 | SmallVector<std::string, 1>())); |
| 337 | |
| 338 | if (!TM) |
| 339 | return; |
| 340 | |
| 341 | ExecutionSession ES(std::make_shared<SymbolStringPool>()); |
| 342 | auto &JD = ES.createJITDylib("main"); |
| 343 | |
| 344 | auto Foo = ES.getSymbolStringPool().intern("foo"); |
| 345 | |
| 346 | auto MM = std::make_shared<SectionMemoryManagerWrapper>(); |
| 347 | |
| 348 | RTDyldObjectLinkingLayer2 ObjLayer(ES, [&MM](VModuleKey K) { return MM; }); |
| 349 | SimpleCompiler Compile(*TM); |
| 350 | |
| 351 | LLVMContext Context; |
| 352 | ModuleBuilder MB1(Context, TM->getTargetTriple().str(), "dummy"); |
| 353 | { |
| 354 | MB1.getModule()->setDataLayout(TM->createDataLayout()); |
| 355 | Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo"); |
| 356 | BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl); |
| 357 | IRBuilder<> Builder(BarEntry); |
| 358 | IntegerType *Int32Ty = IntegerType::get(Context, 32); |
| 359 | Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42); |
| 360 | Builder.CreateRet(FourtyTwo); |
| 361 | } |
| 362 | |
| 363 | auto Obj1 = Compile(*MB1.getModule()); |
| 364 | |
| 365 | ModuleBuilder MB2(Context, TM->getTargetTriple().str(), "dummy"); |
| 366 | { |
| 367 | MB2.getModule()->setDataLayout(TM->createDataLayout()); |
| 368 | Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar"); |
| 369 | BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl); |
| 370 | IRBuilder<> Builder(BarEntry); |
| 371 | IntegerType *Int32Ty = IntegerType::get(Context, 32); |
| 372 | Value *Seven = ConstantInt::getSigned(Int32Ty, 7); |
| 373 | Builder.CreateRet(Seven); |
| 374 | } |
| 375 | auto Obj2 = Compile(*MB2.getModule()); |
| 376 | |
| 377 | cantFail(ObjLayer.add(JD, ES.allocateVModule(), std::move(Obj1))); |
| 378 | cantFail(ObjLayer.add(JD, ES.allocateVModule(), std::move(Obj2))); |
| 379 | |
| 380 | auto OnResolve = [](Expected<SymbolMap> Result) { |
| 381 | cantFail(std::move(Result)); |
| 382 | }; |
| 383 | |
| 384 | auto OnReady = [](Error Err) { cantFail(std::move(Err)); }; |
| 385 | |
| 386 | ES.lookup({&JD}, {Foo}, OnResolve, OnReady, NoDependenciesToRegister); |
| 387 | |
| 388 | // Only one call to needsToReserveAllocationSpace should have been made. |
| 389 | EXPECT_EQ(MM->NeedsToReserveAllocationSpaceCount, 1) |
| 390 | << "More than one call to needsToReserveAllocationSpace " |
| 391 | "(multiple unrelated objects loaded prior to finalization)"; |
| 392 | } |
| 393 | |
| 394 | TEST(RTDyldObjectLinkingLayer2Test, TestNotifyLoadedSignature) { |
| 395 | ExecutionSession ES(std::make_shared<SymbolStringPool>()); |
| 396 | RTDyldObjectLinkingLayer2 ObjLayer( |
| 397 | ES, |
| 398 | [](VModuleKey) -> std::shared_ptr<RuntimeDyld::MemoryManager> { |
| 399 | return nullptr; |
| 400 | }, |
| 401 | [](VModuleKey, const object::ObjectFile &obj, |
| 402 | const RuntimeDyld::LoadedObjectInfo &info) {}); |
| 403 | } |
| 404 | |
| 405 | } // end anonymous namespace |