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 "gtest/gtest.h"
21 class ExecutionEngineTest : public testing::Test {
24 : M(new Module("<main>", getGlobalContext())), Error(""),
25 Engine(EngineBuilder(M).setErrorStr(&Error).create()) {
28 virtual void SetUp() {
29 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
33 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
34 return new GlobalVariable(*M, T, false, // Not constant.
35 GlobalValue::ExternalLinkage, nullptr, Name);
40 const std::unique_ptr<ExecutionEngine> Engine;
43 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
45 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
47 Engine->addGlobalMapping(G1, &Mem1);
48 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
50 Engine->updateGlobalMapping(G1, &Mem2);
51 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
52 Engine->updateGlobalMapping(G1, nullptr);
53 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
54 Engine->updateGlobalMapping(G1, &Mem2);
55 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
58 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
59 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
60 << "The NULL return shouldn't depend on having called"
61 << " updateGlobalMapping(..., NULL)";
62 // Check that update...() can be called before add...().
63 Engine->updateGlobalMapping(G2, &Mem1);
64 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
65 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
66 << "A second mapping shouldn't affect the first.";
69 TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
71 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
74 Engine->addGlobalMapping(G1, &Mem1);
75 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
77 Engine->updateGlobalMapping(G1, &Mem2);
78 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
79 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
82 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
83 Engine->updateGlobalMapping(G2, &Mem1);
84 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
85 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
86 Engine->updateGlobalMapping(G1, nullptr);
87 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
88 << "Removing one mapping doesn't affect a different one.";
89 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
90 Engine->updateGlobalMapping(G2, &Mem2);
91 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
92 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
93 << "Once a mapping is removed, we can point another GV at the"
94 << " now-free address.";
97 TEST_F(ExecutionEngineTest, ClearModuleMappings) {
99 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
102 Engine->addGlobalMapping(G1, &Mem1);
103 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
105 Engine->clearGlobalMappingsFromModule(M);
107 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
110 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
111 // After clearing the module mappings, we can assign a new GV to the
113 Engine->addGlobalMapping(G2, &Mem1);
114 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
117 TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
119 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
121 Engine->addGlobalMapping(G1, &Mem1);
122 // Make sure the reverse mapping is enabled.
123 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
124 // When the GV goes away, the ExecutionEngine should remove any
125 // mappings that refer to it.
126 G1->eraseFromParent();
127 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));