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 void SetUp() override {
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));
57 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable("Global1"));
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));
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.";
78 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
80 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
83 Engine->addGlobalMapping(G1, &Mem1);
84 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
86 Engine->updateGlobalMapping(G1, &Mem2);
87 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
88 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
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.";
106 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
108 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
111 Engine->addGlobalMapping(G1, &Mem1);
112 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
114 Engine->clearGlobalMappingsFromModule(M);
116 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
119 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
120 // After clearing the module mappings, we can assign a new GV to the
122 Engine->addGlobalMapping(G2, &Mem1);
123 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
126 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
128 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
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));
139 TEST_F(ExecutionEngineTest, LookupWithMangledName) {
141 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
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"));
149 TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
152 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
153 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
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"));
161 TEST_F(ExecutionEngineTest, LookupwithDemangledName) {
163 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
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"));