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/IR/DerivedTypes.h"
12 #include "llvm/IR/GlobalVariable.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "gtest/gtest.h"
22 class ExecutionEngineTest : public testing::Test {
24 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
27 ExecutionEngineTest() {
28 auto Owner = make_unique<Module>("<main>", getGlobalContext());
30 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
33 virtual void SetUp() {
34 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
38 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
39 return new GlobalVariable(*M, T, false, // Not constant.
40 GlobalValue::ExternalLinkage, nullptr, Name);
44 Module *M; // Owned by ExecutionEngine.
45 std::unique_ptr<ExecutionEngine> Engine;
48 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
50 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
52 Engine->addGlobalMapping(G1, &Mem1);
53 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
55 Engine->updateGlobalMapping(G1, &Mem2);
56 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
57 Engine->updateGlobalMapping(G1, nullptr);
58 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
59 Engine->updateGlobalMapping(G1, &Mem2);
60 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
63 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
64 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
65 << "The NULL return shouldn't depend on having called"
66 << " updateGlobalMapping(..., NULL)";
67 // Check that update...() can be called before add...().
68 Engine->updateGlobalMapping(G2, &Mem1);
69 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
70 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
71 << "A second mapping shouldn't affect the first.";
74 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
76 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
79 Engine->addGlobalMapping(G1, &Mem1);
80 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
82 Engine->updateGlobalMapping(G1, &Mem2);
83 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
84 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
87 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
88 Engine->updateGlobalMapping(G2, &Mem1);
89 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
90 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
91 Engine->updateGlobalMapping(G1, nullptr);
92 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
93 << "Removing one mapping doesn't affect a different one.";
94 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
95 Engine->updateGlobalMapping(G2, &Mem2);
96 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
97 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
98 << "Once a mapping is removed, we can point another GV at the"
99 << " now-free address.";
102 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
104 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
107 Engine->addGlobalMapping(G1, &Mem1);
108 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
110 Engine->clearGlobalMappingsFromModule(M);
112 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
115 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
116 // After clearing the module mappings, we can assign a new GV to the
118 Engine->addGlobalMapping(G2, &Mem1);
119 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
122 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
124 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
126 Engine->addGlobalMapping(G1, &Mem1);
127 // Make sure the reverse mapping is enabled.
128 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
129 // When the GV goes away, the ExecutionEngine should remove any
130 // mappings that refer to it.
131 G1->eraseFromParent();
132 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));