1 //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 class ExecutionEngineTest : public testing::Test {
27 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
30 ExecutionEngineTest() {
31 auto Owner = make_unique<Module>("<main>", getGlobalContext());
33 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
36 virtual void SetUp() {
37 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
41 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
42 return new GlobalVariable(*M, T, false, // Not constant.
43 GlobalValue::ExternalLinkage, nullptr, Name);
47 Module *M; // Owned by ExecutionEngine.
48 std::unique_ptr<ExecutionEngine> Engine;
51 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
53 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
55 Engine->addGlobalMapping(G1, &Mem1);
56 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
58 Engine->updateGlobalMapping(G1, &Mem2);
59 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
60 Engine->updateGlobalMapping(G1, nullptr);
61 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
62 Engine->updateGlobalMapping(G1, &Mem2);
63 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
66 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
67 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
68 << "The NULL return shouldn't depend on having called"
69 << " updateGlobalMapping(..., NULL)";
70 // Check that update...() can be called before add...().
71 Engine->updateGlobalMapping(G2, &Mem1);
72 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
73 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
74 << "A second mapping shouldn't affect the first.";
77 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
79 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
82 Engine->addGlobalMapping(G1, &Mem1);
83 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
85 Engine->updateGlobalMapping(G1, &Mem2);
86 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
87 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
90 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
91 Engine->updateGlobalMapping(G2, &Mem1);
92 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
93 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
94 Engine->updateGlobalMapping(G1, nullptr);
95 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
96 << "Removing one mapping doesn't affect a different one.";
97 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
98 Engine->updateGlobalMapping(G2, &Mem2);
99 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
100 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
101 << "Once a mapping is removed, we can point another GV at the"
102 << " now-free address.";
105 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
107 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
110 Engine->addGlobalMapping(G1, &Mem1);
111 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
113 Engine->clearGlobalMappingsFromModule(M);
115 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
118 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
119 // After clearing the module mappings, we can assign a new GV to the
121 Engine->addGlobalMapping(G2, &Mem1);
122 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
125 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
127 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
129 Engine->addGlobalMapping(G1, &Mem1);
130 // Make sure the reverse mapping is enabled.
131 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
132 // When the GV goes away, the ExecutionEngine should remove any
133 // mappings that refer to it.
134 G1->eraseFromParent();
135 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
138 TEST_F(ExecutionEngineTest, LookupWithMangledName) {
140 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
142 // Demonstrate that getSymbolAddress accepts mangled names and always strips
143 // the leading underscore.
144 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
145 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
148 TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
151 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
152 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
154 // Lookup the demangled name first, even if there's a demangled symbol that
155 // matches the input already.
156 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
157 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
160 TEST_F(ExecutionEngineTest, LookupwithDemangledName) {
162 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
164 // But do fallback to looking up a demangled name if there's no ambiguity
165 EXPECT_EQ(reinterpret_cast<uint64_t>(&_x),
166 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));