blob: 8ffc1c8f2c6197dabab5aa4eec0ce5d5497d9662 [file] [log] [blame]
Jeffrey Yasskin337b1242009-08-04 23:53:16 +00001//===- 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
Chandler Carruth130cec22012-12-04 10:23:08 +000010#include "llvm/ExecutionEngine/Interpreter.h"
David Blaikiece3f5732014-09-29 21:25:13 +000011#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000012#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/DerivedTypes.h"
14#include "llvm/IR/GlobalVariable.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
David Blaikiece3f5732014-09-29 21:25:13 +000017#include "llvm/Support/DynamicLibrary.h"
NAKAMURA Takumi4d723ba2014-09-23 13:49:51 +000018#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
25class ExecutionEngineTest : public testing::Test {
NAKAMURA Takumidbf2c212014-09-23 14:41:02 +000026private:
27 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
28
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000029protected:
Rafael Espindola2a8a2792014-08-19 04:04:25 +000030 ExecutionEngineTest() {
31 auto Owner = make_unique<Module>("<main>", getGlobalContext());
32 M = Owner.get();
33 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000034 }
35
36 virtual void SetUp() {
Craig Topper66f09ad2014-06-08 22:29:17 +000037 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
Dylan Noblesmithf6f9f1d2011-12-02 20:53:53 +000038 << Error << "'";
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000039 }
40
Chris Lattner229907c2011-07-18 04:54:35 +000041 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000042 return new GlobalVariable(*M, T, false, // Not constant.
Craig Topper66f09ad2014-06-08 22:29:17 +000043 GlobalValue::ExternalLinkage, nullptr, Name);
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000044 }
45
Dylan Noblesmithf6f9f1d2011-12-02 20:53:53 +000046 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +000047 Module *M; // Owned by ExecutionEngine.
48 std::unique_ptr<ExecutionEngine> Engine;
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000049};
50
51TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
Owen Anderson55f1c092009-08-13 21:58:54 +000052 GlobalVariable *G1 =
53 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000054 int32_t Mem1 = 3;
55 Engine->addGlobalMapping(G1, &Mem1);
56 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
57 int32_t Mem2 = 4;
58 Engine->updateGlobalMapping(G1, &Mem2);
59 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
Craig Topper66f09ad2014-06-08 22:29:17 +000060 Engine->updateGlobalMapping(G1, nullptr);
61 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000062 Engine->updateGlobalMapping(G1, &Mem2);
63 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
64
Owen Anderson55f1c092009-08-13 21:58:54 +000065 GlobalVariable *G2 =
66 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Craig Topper66f09ad2014-06-08 22:29:17 +000067 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000068 << "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.";
75}
76
77TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
Owen Anderson55f1c092009-08-13 21:58:54 +000078 GlobalVariable *G1 =
79 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000080
81 int32_t Mem1 = 3;
82 Engine->addGlobalMapping(G1, &Mem1);
83 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
84 int32_t Mem2 = 4;
85 Engine->updateGlobalMapping(G1, &Mem2);
Craig Topper66f09ad2014-06-08 22:29:17 +000086 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000087 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
88
Owen Anderson55f1c092009-08-13 21:58:54 +000089 GlobalVariable *G2 =
90 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000091 Engine->updateGlobalMapping(G2, &Mem1);
92 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
93 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
Craig Topper66f09ad2014-06-08 22:29:17 +000094 Engine->updateGlobalMapping(G1, nullptr);
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000095 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
96 << "Removing one mapping doesn't affect a different one.";
Craig Topper66f09ad2014-06-08 22:29:17 +000097 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
Jeffrey Yasskin337b1242009-08-04 23:53:16 +000098 Engine->updateGlobalMapping(G2, &Mem2);
Craig Topper66f09ad2014-06-08 22:29:17 +000099 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin337b1242009-08-04 23:53:16 +0000100 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
101 << "Once a mapping is removed, we can point another GV at the"
102 << " now-free address.";
103}
104
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000105TEST_F(ExecutionEngineTest, ClearModuleMappings) {
106 GlobalVariable *G1 =
107 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
108
109 int32_t Mem1 = 3;
110 Engine->addGlobalMapping(G1, &Mem1);
111 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
112
113 Engine->clearGlobalMappingsFromModule(M);
114
Craig Topper66f09ad2014-06-08 22:29:17 +0000115 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000116
117 GlobalVariable *G2 =
118 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
119 // After clearing the module mappings, we can assign a new GV to the
120 // same address.
121 Engine->addGlobalMapping(G2, &Mem1);
122 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
123}
124
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000125TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
126 GlobalVariable *G1 =
127 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
128 int32_t Mem1 = 3;
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();
Craig Topper66f09ad2014-06-08 22:29:17 +0000135 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000136}
137
David Blaikiece3f5732014-09-29 21:25:13 +0000138TEST_F(ExecutionEngineTest, LookupWithMangledName) {
139 int x;
140 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
141
142 // Demonstrate that getSymbolAddress accepts mangled names and always strips
143 // the leading underscore.
Lang Hames2f27b2f2014-10-01 04:11:13 +0000144 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
145 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
David Blaikiece3f5732014-09-29 21:25:13 +0000146}
147
148TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
149 int x;
150 int _x;
151 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
152 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
153
154 // Lookup the demangled name first, even if there's a demangled symbol that
155 // matches the input already.
Lang Hames2f27b2f2014-10-01 04:11:13 +0000156 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
157 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
David Blaikiece3f5732014-09-29 21:25:13 +0000158}
159
160TEST_F(ExecutionEngineTest, LookupwithDemangledName) {
161 int _x;
162 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
163
164 // But do fallback to looking up a demangled name if there's no ambiguity
Lang Hames2f27b2f2014-10-01 04:11:13 +0000165 EXPECT_EQ(reinterpret_cast<uint64_t>(&_x),
166 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
David Blaikiece3f5732014-09-29 21:25:13 +0000167}
168
Jeffrey Yasskin337b1242009-08-04 23:53:16 +0000169}