1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
//===--- RTDyldObjectLinkingLayerTest.cpp - RTDyld linking layer tests ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "OrcTestCommon.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "gtest/gtest.h"
#include <string>
using namespace llvm;
using namespace llvm::orc;
namespace {
// Returns whether a non-alloc section was passed to the memory manager.
static bool testSetProcessAllSections(std::unique_ptr<MemoryBuffer> Obj,
bool ProcessAllSections) {
class MemoryManagerWrapper : public SectionMemoryManager {
public:
MemoryManagerWrapper(bool &NonAllocSeen) : NonAllocSeen(NonAllocSeen) {}
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
unsigned SectionID, StringRef SectionName,
bool IsReadOnly) override {
// We check for ".note.GNU-stack" here because it is currently the only
// non-alloc section seen in the module. If this changes in future any
// other non-alloc section would do here.
if (SectionName == ".note.GNU-stack")
NonAllocSeen = true;
return SectionMemoryManager::allocateDataSection(
Size, Alignment, SectionID, SectionName, IsReadOnly);
}
private:
bool &NonAllocSeen;
};
bool NonAllocSectionSeen = false;
ExecutionSession ES(std::make_unique<UnsupportedExecutorProcessControl>());
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
RTDyldObjectLinkingLayer ObjLayer(
ES, [&NonAllocSectionSeen](const MemoryBuffer &) {
return std::make_unique<MemoryManagerWrapper>(NonAllocSectionSeen);
});
auto OnResolveDoNothing = [](Expected<SymbolMap> R) {
cantFail(std::move(R));
};
ObjLayer.setProcessAllSections(ProcessAllSections);
cantFail(ObjLayer.add(JD, std::move(Obj)));
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
SymbolLookupSet(Foo), SymbolState::Resolved, OnResolveDoNothing,
NoDependenciesToRegister);
if (auto Err = ES.endSession())
ES.reportError(std::move(Err));
return NonAllocSectionSeen;
}
TEST(RTDyldObjectLinkingLayerTest, TestSetProcessAllSections) {
LLVMContext Context;
auto M = std::make_unique<Module>("", Context);
M->setTargetTriple(Triple("x86_64-unknown-linux-gnu"));
// These values are only here to ensure that the module is non-empty.
// They are no longer relevant to the test.
Constant *StrConstant = ConstantDataArray::getString(Context, "forty-two");
auto *GV =
new GlobalVariable(*M, StrConstant->getType(), true,
GlobalValue::ExternalLinkage, StrConstant, "foo");
GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
GV->setAlignment(Align(1));
// Initialize the native target in case this is the first unit test
// to try to build a TM.
OrcNativeTarget::initialize();
std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget(
M->getTargetTriple(), "", "", SmallVector<std::string, 1>()));
if (!TM)
GTEST_SKIP();
auto Obj = cantFail(SimpleCompiler(*TM)(*M));
EXPECT_FALSE(testSetProcessAllSections(
MemoryBuffer::getMemBufferCopy(Obj->getBuffer()), false))
<< "Non-alloc section seen despite ProcessAllSections being false";
EXPECT_TRUE(testSetProcessAllSections(std::move(Obj), true))
<< "Expected to see non-alloc section when ProcessAllSections is true";
}
TEST(RTDyldObjectLinkingLayerTest, TestOverrideObjectFlags) {
OrcNativeTarget::initialize();
std::unique_ptr<TargetMachine> TM(
EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
SmallVector<std::string, 1>()));
if (!TM)
GTEST_SKIP();
// Our compiler is going to modify symbol visibility settings without telling
// ORC. This will test our ability to override the flags later.
class FunkySimpleCompiler : public SimpleCompiler {
public:
FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {}
Expected<CompileResult> operator()(Module &M) override {
auto *Foo = M.getFunction("foo");
assert(Foo && "Expected function Foo not found");
Foo->setVisibility(GlobalValue::HiddenVisibility);
return SimpleCompiler::operator()(M);
}
};
// Create a module with two void() functions: foo and bar.
ThreadSafeModule M;
{
auto Ctx = std::make_unique<LLVMContext>();
ModuleBuilder MB(*Ctx, TM->getTargetTriple().str(), "dummy");
MB.getModule()->setDataLayout(TM->createDataLayout());
Function *FooImpl = MB.createFunctionDecl(
FunctionType::get(Type::getVoidTy(*Ctx), {}, false), "foo");
BasicBlock *FooEntry = BasicBlock::Create(*Ctx, "entry", FooImpl);
IRBuilder<> B1(FooEntry);
B1.CreateRetVoid();
Function *BarImpl = MB.createFunctionDecl(
FunctionType::get(Type::getVoidTy(*Ctx), {}, false), "bar");
BasicBlock *BarEntry = BasicBlock::Create(*Ctx, "entry", BarImpl);
IRBuilder<> B2(BarEntry);
B2.CreateRetVoid();
M = ThreadSafeModule(MB.takeModule(), std::move(Ctx));
}
// Create a simple stack and set the override flags option.
ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
RTDyldObjectLinkingLayer ObjLayer(ES, [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<FunkySimpleCompiler>(*TM));
ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
cantFail(CompileLayer.add(JD, std::move(M)));
ES.lookup(
LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),
SymbolState::Resolved,
[](Expected<SymbolMap> R) { cantFail(std::move(R)); },
NoDependenciesToRegister);
if (auto Err = ES.endSession())
ES.reportError(std::move(Err));
}
TEST(RTDyldObjectLinkingLayerTest, TestAutoClaimResponsibilityForSymbols) {
OrcNativeTarget::initialize();
std::unique_ptr<TargetMachine> TM(
EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
SmallVector<std::string, 1>()));
if (!TM)
GTEST_SKIP();
// Our compiler is going to add a new symbol without telling ORC.
// This will test our ability to auto-claim responsibility later.
class FunkySimpleCompiler : public SimpleCompiler {
public:
FunkySimpleCompiler(TargetMachine &TM) : SimpleCompiler(TM) {}
Expected<CompileResult> operator()(Module &M) override {
Function *BarImpl = Function::Create(
FunctionType::get(Type::getVoidTy(M.getContext()), {}, false),
GlobalValue::ExternalLinkage, "bar", &M);
BasicBlock *BarEntry =
BasicBlock::Create(M.getContext(), "entry", BarImpl);
IRBuilder<> B(BarEntry);
B.CreateRetVoid();
return SimpleCompiler::operator()(M);
}
};
// Create a module with two void() functions: foo and bar.
ThreadSafeModule M;
{
auto Ctx = std::make_unique<LLVMContext>();
ModuleBuilder MB(*Ctx, TM->getTargetTriple().str(), "dummy");
MB.getModule()->setDataLayout(TM->createDataLayout());
Function *FooImpl = MB.createFunctionDecl(
FunctionType::get(Type::getVoidTy(*Ctx), {}, false), "foo");
BasicBlock *FooEntry = BasicBlock::Create(*Ctx, "entry", FooImpl);
IRBuilder<> B(FooEntry);
B.CreateRetVoid();
M = ThreadSafeModule(MB.takeModule(), std::move(Ctx));
}
// Create a simple stack and set the override flags option.
ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
RTDyldObjectLinkingLayer ObjLayer(ES, [](const MemoryBuffer &) {
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<FunkySimpleCompiler>(*TM));
ObjLayer.setAutoClaimResponsibilityForObjectSymbols(true);
cantFail(CompileLayer.add(JD, std::move(M)));
ES.lookup(
LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),
SymbolState::Resolved,
[](Expected<SymbolMap> R) { cantFail(std::move(R)); },
NoDependenciesToRegister);
if (auto Err = ES.endSession())
ES.reportError(std::move(Err));
}
TEST(RTDyldObjectLinkingLayerTest, TestMemoryBufferNamePropagation) {
OrcNativeTarget::initialize();
std::unique_ptr<TargetMachine> TM(
EngineBuilder().selectTarget(Triple("x86_64-unknown-linux-gnu"), "", "",
SmallVector<std::string, 1>()));
if (!TM)
GTEST_SKIP();
// Create a module with two void() functions: foo and bar.
ThreadSafeModule M;
{
auto Ctx = std::make_unique<LLVMContext>();
ModuleBuilder MB(*Ctx, TM->getTargetTriple().str(), "dummy");
MB.getModule()->setDataLayout(TM->createDataLayout());
Function *FooImpl = MB.createFunctionDecl(
FunctionType::get(Type::getVoidTy(*Ctx), {}, false), "foo");
BasicBlock *FooEntry = BasicBlock::Create(*Ctx, "entry", FooImpl);
IRBuilder<> B1(FooEntry);
B1.CreateRetVoid();
M = ThreadSafeModule(MB.takeModule(), std::move(Ctx));
}
ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};
auto &JD = ES.createBareJITDylib("main");
auto Foo = ES.intern("foo");
std::string ObjectIdentifer;
RTDyldObjectLinkingLayer ObjLayer(
ES, [&ObjectIdentifer](const MemoryBuffer &Obj) {
// Capture the name of the object so that we can confirm that it
// contains the module name.
ObjectIdentifer = Obj.getBufferIdentifier().str();
return std::make_unique<SectionMemoryManager>();
});
IRCompileLayer CompileLayer(ES, ObjLayer,
std::make_unique<SimpleCompiler>(*TM));
// Capture the module name before we move the module.
std::string ModuleName = M.getModuleUnlocked()->getName().str();
cantFail(CompileLayer.add(JD, std::move(M)));
ES.lookup(
LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),
SymbolState::Resolved,
[](Expected<SymbolMap> R) { cantFail(std::move(R)); },
NoDependenciesToRegister);
if (auto Err = ES.endSession())
ES.reportError(std::move(Err));
EXPECT_TRUE(ObjectIdentifer.find(ModuleName) != std::string::npos);
}
} // end anonymous namespace
|