xref: /llvm-project/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp (revision 3b514554a2ab643813c91f161d69c5a5c305efcf)
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:
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 
36   void SetUp() override {
37     ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
38       << Error << "'";
39   }
40 
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 
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 
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 
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 
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 
139 TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
140   int x;
141   int _x;
142   llvm::sys::DynamicLibrary::AddSymbol("x", &x);
143   llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
144 
145   // RTDyldMemoryManager::getSymbolAddressInProcess expects a mangled symbol,
146   // but DynamicLibrary is a wrapper for dlsym, which expects the unmangled C
147   // symbol name. This test verifies that getSymbolAddressInProcess strips the
148   // leading '_' on Darwin, but not on other platforms.
149 #ifdef __APPLE__
150   EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
151             RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
152 #else
153   EXPECT_EQ(reinterpret_cast<uint64_t>(&_x),
154             RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
155 #endif
156 }
157 
158 }
159