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 | |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 31 | // Adds an object with a debug section to RuntimeDyld and then returns whether |
| 32 | // the debug section was passed to the memory manager. |
| 33 | static bool testSetProcessAllSections(std::unique_ptr<MemoryBuffer> Obj, |
| 34 | bool ProcessAllSections) { |
| 35 | class MemoryManagerWrapper : public SectionMemoryManager { |
| 36 | public: |
| 37 | MemoryManagerWrapper(bool &DebugSeen) : DebugSeen(DebugSeen) {} |
| 38 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 39 | unsigned SectionID, StringRef SectionName, |
| 40 | bool IsReadOnly) override { |
| 41 | if (SectionName == ".debug_str") |
| 42 | DebugSeen = true; |
| 43 | return SectionMemoryManager::allocateDataSection( |
| 44 | Size, Alignment, SectionID, SectionName, IsReadOnly); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | bool &DebugSeen; |
| 49 | }; |
| 50 | |
| 51 | bool DebugSectionSeen = false; |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 52 | |
Lang Hames | f6107d4 | 2018-09-05 20:57:41 +0000 | [diff] [blame] | 53 | ExecutionSession ES; |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 54 | auto &JD = ES.createJITDylib("main"); |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame^] | 55 | auto Foo = ES.intern("foo"); |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 56 | |
Lang Hames | bf98525 | 2018-09-06 19:39:26 +0000 | [diff] [blame] | 57 | RTDyldObjectLinkingLayer2 ObjLayer(ES, [&DebugSectionSeen](VModuleKey) { |
| 58 | return llvm::make_unique<MemoryManagerWrapper>(DebugSectionSeen); |
| 59 | }); |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 60 | |
| 61 | auto OnResolveDoNothing = [](Expected<SymbolMap> R) { |
| 62 | cantFail(std::move(R)); |
| 63 | }; |
| 64 | |
| 65 | auto OnReadyDoNothing = [](Error Err) { cantFail(std::move(Err)); }; |
| 66 | |
| 67 | ObjLayer.setProcessAllSections(ProcessAllSections); |
| 68 | auto K = ES.allocateVModule(); |
| 69 | cantFail(ObjLayer.add(JD, K, std::move(Obj))); |
| 70 | ES.lookup({&JD}, {Foo}, OnResolveDoNothing, OnReadyDoNothing, |
| 71 | NoDependenciesToRegister); |
| 72 | return DebugSectionSeen; |
| 73 | } |
| 74 | |
| 75 | TEST(RTDyldObjectLinkingLayer2Test, TestSetProcessAllSections) { |
| 76 | LLVMContext Context; |
| 77 | auto M = llvm::make_unique<Module>("", Context); |
| 78 | M->setTargetTriple("x86_64-unknown-linux-gnu"); |
| 79 | Type *Int32Ty = IntegerType::get(Context, 32); |
| 80 | GlobalVariable *GV = |
| 81 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, |
| 82 | ConstantInt::get(Int32Ty, 42), "foo"); |
| 83 | |
| 84 | GV->setSection(".debug_str"); |
| 85 | |
| 86 | // Initialize the native target in case this is the first unit test |
| 87 | // to try to build a TM. |
| 88 | OrcNativeTarget::initialize(); |
| 89 | std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget( |
| 90 | Triple(M->getTargetTriple()), "", "", SmallVector<std::string, 1>())); |
| 91 | if (!TM) |
| 92 | return; |
| 93 | |
| 94 | auto Obj = SimpleCompiler(*TM)(*M); |
| 95 | |
| 96 | EXPECT_FALSE(testSetProcessAllSections( |
| 97 | MemoryBuffer::getMemBufferCopy(Obj->getBuffer()), false)) |
| 98 | << "Debug section seen despite ProcessAllSections being false"; |
| 99 | EXPECT_TRUE(testSetProcessAllSections(std::move(Obj), true)) |
| 100 | << "Expected to see debug section when ProcessAllSections is true"; |
| 101 | } |
| 102 | |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 103 | TEST(RTDyldObjectLinkingLayer2Test, TestOverrideObjectFlags) { |
| 104 | |
| 105 | OrcNativeTarget::initialize(); |
| 106 | |
| 107 | std::unique_ptr<TargetMachine> TM( |
| 108 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 109 | SmallVector<std::string, 1>())); |
| 110 | |
| 111 | if (!TM) |
| 112 | return; |
| 113 | |
| 114 | // Our compiler is going to modify symbol visibility settings without telling |
| 115 | // ORC. This will test our ability to override the flags later. |
| 116 | class FunkySimpleCompiler : public SimpleCompiler { |
| 117 | public: |
| 118 | FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {} |
| 119 | |
| 120 | CompileResult operator()(Module &M) { |
| 121 | auto *Foo = M.getFunction("foo"); |
| 122 | assert(Foo && "Expected function Foo not found"); |
| 123 | Foo->setVisibility(GlobalValue::HiddenVisibility); |
| 124 | return SimpleCompiler::operator()(M); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | // Create a module with two void() functions: foo and bar. |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 129 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
| 130 | ThreadSafeModule M; |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 131 | { |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 132 | ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy"); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 133 | MB.getModule()->setDataLayout(TM->createDataLayout()); |
| 134 | |
| 135 | Function *FooImpl = MB.createFunctionDecl<void()>("foo"); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 136 | BasicBlock *FooEntry = |
| 137 | BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 138 | IRBuilder<> B1(FooEntry); |
| 139 | B1.CreateRetVoid(); |
| 140 | |
| 141 | Function *BarImpl = MB.createFunctionDecl<void()>("bar"); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 142 | BasicBlock *BarEntry = |
| 143 | BasicBlock::Create(*TSCtx.getContext(), "entry", BarImpl); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 144 | IRBuilder<> B2(BarEntry); |
| 145 | B2.CreateRetVoid(); |
| 146 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 147 | M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx)); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Create a simple stack and set the override flags option. |
| 151 | ExecutionSession ES; |
| 152 | auto &JD = ES.createJITDylib("main"); |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame^] | 153 | auto Foo = ES.intern("foo"); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 154 | RTDyldObjectLinkingLayer2 ObjLayer( |
Lang Hames | bf98525 | 2018-09-06 19:39:26 +0000 | [diff] [blame] | 155 | ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); }); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 156 | IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM)); |
| 157 | |
| 158 | ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true); |
| 159 | |
| 160 | cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M))); |
| 161 | ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); }, |
| 162 | [](Error Err) { cantFail(std::move(Err)); }, |
| 163 | NoDependenciesToRegister); |
| 164 | } |
| 165 | |
| 166 | TEST(RTDyldObjectLinkingLayer2Test, TestAutoClaimResponsibilityForSymbols) { |
| 167 | |
| 168 | OrcNativeTarget::initialize(); |
| 169 | |
| 170 | std::unique_ptr<TargetMachine> TM( |
| 171 | EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "", |
| 172 | SmallVector<std::string, 1>())); |
| 173 | |
| 174 | if (!TM) |
| 175 | return; |
| 176 | |
| 177 | // Our compiler is going to add a new symbol without telling ORC. |
| 178 | // This will test our ability to auto-claim responsibility later. |
| 179 | class FunkySimpleCompiler : public SimpleCompiler { |
| 180 | public: |
| 181 | FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {} |
| 182 | |
| 183 | CompileResult operator()(Module &M) { |
| 184 | Function *BarImpl = |
| 185 | Function::Create(TypeBuilder<void(), false>::get(M.getContext()), |
| 186 | GlobalValue::ExternalLinkage, "bar", &M); |
| 187 | BasicBlock *BarEntry = |
| 188 | BasicBlock::Create(M.getContext(), "entry", BarImpl); |
| 189 | IRBuilder<> B(BarEntry); |
| 190 | B.CreateRetVoid(); |
| 191 | |
| 192 | return SimpleCompiler::operator()(M); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | // Create a module with two void() functions: foo and bar. |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 197 | ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>()); |
| 198 | ThreadSafeModule M; |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 199 | { |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 200 | ModuleBuilder MB(*TSCtx.getContext(), TM->getTargetTriple().str(), "dummy"); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 201 | MB.getModule()->setDataLayout(TM->createDataLayout()); |
| 202 | |
| 203 | Function *FooImpl = MB.createFunctionDecl<void()>("foo"); |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 204 | BasicBlock *FooEntry = |
| 205 | BasicBlock::Create(*TSCtx.getContext(), "entry", FooImpl); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 206 | IRBuilder<> B(FooEntry); |
| 207 | B.CreateRetVoid(); |
| 208 | |
Lang Hames | 8d76c71 | 2018-09-26 01:24:12 +0000 | [diff] [blame] | 209 | M = ThreadSafeModule(MB.takeModule(), std::move(TSCtx)); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Create a simple stack and set the override flags option. |
| 213 | ExecutionSession ES; |
| 214 | auto &JD = ES.createJITDylib("main"); |
Lang Hames | 71d781c | 2018-09-30 23:18:24 +0000 | [diff] [blame^] | 215 | auto Foo = ES.intern("foo"); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 216 | RTDyldObjectLinkingLayer2 ObjLayer( |
Lang Hames | bf98525 | 2018-09-06 19:39:26 +0000 | [diff] [blame] | 217 | ES, [](VModuleKey) { return llvm::make_unique<SectionMemoryManager>(); }); |
Lang Hames | 6d32002 | 2018-08-31 00:53:17 +0000 | [diff] [blame] | 218 | IRCompileLayer2 CompileLayer(ES, ObjLayer, FunkySimpleCompiler(*TM)); |
| 219 | |
| 220 | ObjLayer.setAutoClaimResponsibilityForObjectSymbols(true); |
| 221 | |
| 222 | cantFail(CompileLayer.add(JD, ES.allocateVModule(), std::move(M))); |
| 223 | ES.lookup({&JD}, {Foo}, [](Expected<SymbolMap> R) { cantFail(std::move(R)); }, |
| 224 | [](Error Err) { cantFail(std::move(Err)); }, |
| 225 | NoDependenciesToRegister); |
| 226 | } |
| 227 | |
Lang Hames | 20d7892 | 2018-08-27 22:30:57 +0000 | [diff] [blame] | 228 | } // end anonymous namespace |