Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 10 | #include "llvm/ExecutionEngine/Interpreter.h" |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 11 | #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DerivedTypes.h" |
| 14 | #include "llvm/IR/GlobalVariable.h" |
| 15 | #include "llvm/IR/LLVMContext.h" |
| 16 | #include "llvm/IR/Module.h" |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DynamicLibrary.h" |
NAKAMURA Takumi | 4d723ba | 2014-09-23 13:49:51 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class ExecutionEngineTest : public testing::Test { |
NAKAMURA Takumi | dbf2c21 | 2014-09-23 14:41:02 +0000 | [diff] [blame] | 26 | private: |
| 27 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 28 | |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 29 | protected: |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 30 | ExecutionEngineTest() { |
| 31 | auto Owner = make_unique<Module>("<main>", getGlobalContext()); |
| 32 | M = Owner.get(); |
| 33 | Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create()); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 36 | void SetUp() override { |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 37 | ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '" |
Dylan Noblesmith | f6f9f1d | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 38 | << Error << "'"; |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 41 | GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) { |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 42 | return new GlobalVariable(*M, T, false, // Not constant. |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 43 | GlobalValue::ExternalLinkage, nullptr, Name); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Dylan Noblesmith | f6f9f1d | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 46 | std::string Error; |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 47 | Module *M; // Owned by ExecutionEngine. |
| 48 | std::unique_ptr<ExecutionEngine> Engine; |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | TEST_F(ExecutionEngineTest, ForwardGlobalMapping) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 52 | GlobalVariable *G1 = |
| 53 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 54 | int32_t Mem1 = 3; |
| 55 | Engine->addGlobalMapping(G1, &Mem1); |
| 56 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1)); |
Lang Hames | 3dac3f7 | 2015-03-31 20:31:14 +0000 | [diff] [blame] | 57 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable("Global1")); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 58 | int32_t Mem2 = 4; |
| 59 | Engine->updateGlobalMapping(G1, &Mem2); |
| 60 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 61 | Engine->updateGlobalMapping(G1, nullptr); |
| 62 | EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 63 | Engine->updateGlobalMapping(G1, &Mem2); |
| 64 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 65 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 66 | GlobalVariable *G2 = |
| 67 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 68 | EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2)) |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 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) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 79 | GlobalVariable *G1 = |
| 80 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 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); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 87 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 88 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 89 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 90 | GlobalVariable *G2 = |
| 91 | NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 92 | Engine->updateGlobalMapping(G2, &Mem1); |
| 93 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 94 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 95 | Engine->updateGlobalMapping(G1, nullptr); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 96 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)) |
| 97 | << "Removing one mapping doesn't affect a different one."; |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 98 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 99 | Engine->updateGlobalMapping(G2, &Mem2); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 100 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 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 | |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 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 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 116 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 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 | |
Jeffrey Yasskin | f98e981 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 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(); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 136 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | f98e981 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 137 | } |
| 138 | |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 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 | |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame^] | 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__ |
Lang Hames | 2f27b2f | 2014-10-01 04:11:13 +0000 | [diff] [blame] | 150 | EXPECT_EQ(reinterpret_cast<uint64_t>(&x), |
| 151 | RTDyldMemoryManager::getSymbolAddressInProcess("_x")); |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame^] | 152 | #else |
Lang Hames | 2f27b2f | 2014-10-01 04:11:13 +0000 | [diff] [blame] | 153 | EXPECT_EQ(reinterpret_cast<uint64_t>(&_x), |
| 154 | RTDyldMemoryManager::getSymbolAddressInProcess("_x")); |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame^] | 155 | #endif |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 158 | } |