blob: ec5fab6c489335a1432739e58dba4def6d755129 [file] [log] [blame]
Jeffrey Yasskin0a962312009-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 Carruth3c0d6072017-06-06 11:06:56 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth5a88dda2012-12-04 10:23:08 +000011#include "llvm/ExecutionEngine/Interpreter.h"
David Blaikie03b46672014-09-29 21:25:13 +000012#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Chandler Carruth0b8c9a82013-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 Blaikie03b46672014-09-29 21:25:13 +000017#include "llvm/Support/DynamicLibrary.h"
NAKAMURA Takumi0c5fa472014-09-23 13:49:51 +000018#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
25class ExecutionEngineTest : public testing::Test {
NAKAMURA Takumia6c580e2014-09-23 14:41:02 +000026private:
27 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
28
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000029protected:
Rafael Espindola3f4ed322014-08-19 04:04:25 +000030 ExecutionEngineTest() {
Mehdi Amini8be77072016-04-14 21:59:01 +000031 auto Owner = make_unique<Module>("<main>", Context);
Rafael Espindola3f4ed322014-08-19 04:04:25 +000032 M = Owner.get();
33 Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000034 }
35
Alexander Kornienkoc16fc542015-04-11 02:11:45 +000036 void SetUp() override {
Craig Topperb1770412014-06-08 22:29:17 +000037 ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '"
Dylan Noblesmith35af1d72011-12-02 20:53:53 +000038 << Error << "'";
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000039 }
40
Chris Lattnerdb125cf2011-07-18 04:54:35 +000041 GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) {
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000042 return new GlobalVariable(*M, T, false, // Not constant.
Craig Topperb1770412014-06-08 22:29:17 +000043 GlobalValue::ExternalLinkage, nullptr, Name);
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000044 }
45
Dylan Noblesmith35af1d72011-12-02 20:53:53 +000046 std::string Error;
Mehdi Amini8be77072016-04-14 21:59:01 +000047 LLVMContext Context;
Rafael Espindola3f4ed322014-08-19 04:04:25 +000048 Module *M; // Owned by ExecutionEngine.
49 std::unique_ptr<ExecutionEngine> Engine;
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000050};
51
52TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
Mehdi Amini8be77072016-04-14 21:59:01 +000053 GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000054 int32_t Mem1 = 3;
55 Engine->addGlobalMapping(G1, &Mem1);
56 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
Lang Hames83b5f342015-03-31 20:31:14 +000057 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable("Global1"));
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000058 int32_t Mem2 = 4;
59 Engine->updateGlobalMapping(G1, &Mem2);
60 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
Craig Topperb1770412014-06-08 22:29:17 +000061 Engine->updateGlobalMapping(G1, nullptr);
62 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1));
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000063 Engine->updateGlobalMapping(G1, &Mem2);
64 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
65
Mehdi Amini8be77072016-04-14 21:59:01 +000066 GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
Craig Topperb1770412014-06-08 22:29:17 +000067 EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
Jeffrey Yasskin0a962312009-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) {
Mehdi Amini8be77072016-04-14 21:59:01 +000078 GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000079
80 int32_t Mem1 = 3;
81 Engine->addGlobalMapping(G1, &Mem1);
82 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
83 int32_t Mem2 = 4;
84 Engine->updateGlobalMapping(G1, &Mem2);
Craig Topperb1770412014-06-08 22:29:17 +000085 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000086 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
87
Mehdi Amini8be77072016-04-14 21:59:01 +000088 GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000089 Engine->updateGlobalMapping(G2, &Mem1);
90 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
91 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
Craig Topperb1770412014-06-08 22:29:17 +000092 Engine->updateGlobalMapping(G1, nullptr);
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000093 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
94 << "Removing one mapping doesn't affect a different one.";
Craig Topperb1770412014-06-08 22:29:17 +000095 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2));
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000096 Engine->updateGlobalMapping(G2, &Mem2);
Craig Topperb1770412014-06-08 22:29:17 +000097 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000098 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
99 << "Once a mapping is removed, we can point another GV at the"
100 << " now-free address.";
101}
102
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000103TEST_F(ExecutionEngineTest, ClearModuleMappings) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000104 GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000105
106 int32_t Mem1 = 3;
107 Engine->addGlobalMapping(G1, &Mem1);
108 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
109
110 Engine->clearGlobalMappingsFromModule(M);
111
Craig Topperb1770412014-06-08 22:29:17 +0000112 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000113
Mehdi Amini8be77072016-04-14 21:59:01 +0000114 GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2");
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000115 // After clearing the module mappings, we can assign a new GV to the
116 // same address.
117 Engine->addGlobalMapping(G2, &Mem1);
118 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
119}
120
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000121TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
Mehdi Amini8be77072016-04-14 21:59:01 +0000122 GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000123 int32_t Mem1 = 3;
124 Engine->addGlobalMapping(G1, &Mem1);
125 // Make sure the reverse mapping is enabled.
126 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
127 // When the GV goes away, the ExecutionEngine should remove any
128 // mappings that refer to it.
129 G1->eraseFromParent();
Craig Topperb1770412014-06-08 22:29:17 +0000130 EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000131}
132
David Blaikie03b46672014-09-29 21:25:13 +0000133TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) {
134 int x;
135 int _x;
136 llvm::sys::DynamicLibrary::AddSymbol("x", &x);
137 llvm::sys::DynamicLibrary::AddSymbol("_x", &_x);
138
Lang Hames9ab9fd52016-03-03 21:23:15 +0000139 // RTDyldMemoryManager::getSymbolAddressInProcess expects a mangled symbol,
140 // but DynamicLibrary is a wrapper for dlsym, which expects the unmangled C
141 // symbol name. This test verifies that getSymbolAddressInProcess strips the
Lang Hamesb60f9a22016-08-18 01:33:28 +0000142 // leading '_' on Darwin, but not on other platforms.
143#ifdef __APPLE__
Lang Hamese2ef4412014-10-01 04:11:13 +0000144 EXPECT_EQ(reinterpret_cast<uint64_t>(&x),
145 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
Lang Hames9ab9fd52016-03-03 21:23:15 +0000146#else
Lang Hamese2ef4412014-10-01 04:11:13 +0000147 EXPECT_EQ(reinterpret_cast<uint64_t>(&_x),
148 RTDyldMemoryManager::getSymbolAddressInProcess("_x"));
Lang Hames9ab9fd52016-03-03 21:23:15 +0000149#endif
David Blaikie03b46672014-09-29 21:25:13 +0000150}
151
Jeffrey Yasskin0a962312009-08-04 23:53:16 +0000152}