1 //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
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 "llvm/ExecutionEngine/Interpreter.h"
11 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/IR/DerivedTypes.h"
14 #include "llvm/IR/GlobalVariable.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/DynamicLibrary.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "gtest/gtest.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class ExecutionEngineTest : public testing::Test {
26 private:
27 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
28
29 protected:
ExecutionEngineTest()30 ExecutionEngineTest() {
31 auto Owner = make_unique<Module>("<main>", getGlobalContext());
32 M = Owner.get();
33 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
34 }
35
SetUp()36 void SetUp() override {
37 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
38 << Error << "'";
39 }
40
NewExtGlobal(Type * T,const Twine & Name)41 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
42 return new GlobalVariable(*M, T, false, // Not constant.
43 GlobalValue::ExternalLinkage, nullptr, Name);
44 }
45
46 std::string Error;
47 Module *M; // Owned by ExecutionEngine.
48 std::unique_ptr<ExecutionEngine> Engine;
49 };
50
TEST_F(ExecutionEngineTest,ForwardGlobalMapping)51 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
52 GlobalVariable *G1 =
53 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
54 int32_t Mem1 = 3;
55 Engine->addGlobalMapping(G1, &Mem1);
56 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
57 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable("Global1"));
58 int32_t Mem2 = 4;
59 Engine->updateGlobalMapping(G1, &Mem2);
60 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
61 Engine->updateGlobalMapping(G1, nullptr);
62 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
63 Engine->updateGlobalMapping(G1, &Mem2);
64 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
65
66 GlobalVariable *G2 =
67 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
68 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
69 << "The NULL return shouldn't depend on having called"
70 << " updateGlobalMapping(..., NULL)";
71 // Check that update...() can be called before add...().
72 Engine->updateGlobalMapping(G2, &Mem1);
73 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
74 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
75 << "A second mapping shouldn't affect the first.";
76 }
77
TEST_F(ExecutionEngineTest,ReverseGlobalMapping)78 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
79 GlobalVariable *G1 =
80 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
81
82 int32_t Mem1 = 3;
83 Engine->addGlobalMapping(G1, &Mem1);
84 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
85 int32_t Mem2 = 4;
86 Engine->updateGlobalMapping(G1, &Mem2);
87 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
88 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
89
90 GlobalVariable *G2 =
91 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
92 Engine->updateGlobalMapping(G2, &Mem1);
93 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
94 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
95 Engine->updateGlobalMapping(G1, nullptr);
96 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
97 << "Removing one mapping doesn't affect a different one.";
98 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
99 Engine->updateGlobalMapping(G2, &Mem2);
100 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
101 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
102 << "Once a mapping is removed, we can point another GV at the"
103 << " now-free address.";
104 }
105
TEST_F(ExecutionEngineTest,ClearModuleMappings)106 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
107 GlobalVariable *G1 =
108 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
109
110 int32_t Mem1 = 3;
111 Engine->addGlobalMapping(G1, &Mem1);
112 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
113
114 Engine->clearGlobalMappingsFromModule(M);
115
116 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
117
118 GlobalVariable *G2 =
119 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
120 // After clearing the module mappings, we can assign a new GV to the
121 // same address.
122 Engine->addGlobalMapping(G2, &Mem1);
123 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
124 }
125
TEST_F(ExecutionEngineTest,DestructionRemovesGlobalMapping)126 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
127 GlobalVariable *G1 =
128 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
129 int32_t Mem1 = 3;
130 Engine->addGlobalMapping(G1, &Mem1);
131 // Make sure the reverse mapping is enabled.
132 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
133 // When the GV goes away, the ExecutionEngine should remove any
134 // mappings that refer to it.
135 G1->eraseFromParent();
136 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
137 }
138
TEST_F(ExecutionEngineTest,LookupWithMangledName)139 TEST_F(ExecutionEngineTest, LookupWithMangledName) {
140 int x;
141 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
142
143 // Demonstrate that getSymbolAddress accepts mangled names and always strips
144 // the leading underscore.
145 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
146 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
147 }
148
TEST_F(ExecutionEngineTest,LookupWithMangledAndDemangledSymbol)149 TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
150 int x;
151 int _x;
152 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
153 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
154
155 // Lookup the demangled name first, even if there's a demangled symbol that
156 // matches the input already.
157 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
158 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
159 }
160
TEST_F(ExecutionEngineTest,LookupwithDemangledName)161 TEST_F(ExecutionEngineTest, LookupwithDemangledName) {
162 int _x;
163 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
164
165 // But do fallback to looking up a demangled name if there's no ambiguity
166 EXPECT_EQ(reinterpret_cast<uint64_t>(&_x),
167 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
168 }
169
170 }
171