Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 1 | //===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/STLExtras.h" |
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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DerivedTypes.h" |
| 13 | #include "llvm/IR/GlobalVariable.h" |
| 14 | #include "llvm/IR/LLVMContext.h" |
| 15 | #include "llvm/IR/Module.h" |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DynamicLibrary.h" |
NAKAMURA Takumi | 4d723ba | 2014-09-23 13:49:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class ExecutionEngineTest : public testing::Test { |
NAKAMURA Takumi | dbf2c21 | 2014-09-23 14:41:02 +0000 | [diff] [blame] | 25 | private: |
| 26 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 27 | |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 28 | protected: |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 29 | ExecutionEngineTest() { |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 30 | auto Owner = std::make_unique<Module>("<main>", Context); |
Rafael Espindola | 2a8a279 | 2014-08-19 04:04:25 +0000 | [diff] [blame] | 31 | M = Owner.get(); |
| 32 | Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create()); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 35 | void SetUp() override { |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 36 | ASSERT_TRUE(Engine.get() != nullptr) << "EngineBuilder returned error: '" |
Dylan Noblesmith | f6f9f1d | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 37 | << Error << "'"; |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 40 | GlobalVariable *NewExtGlobal(Type *T, const Twine &Name) { |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 41 | return new GlobalVariable(*M, T, false, // Not constant. |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 42 | GlobalValue::ExternalLinkage, nullptr, Name); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Dylan Noblesmith | f6f9f1d | 2011-12-02 20:53:53 +0000 | [diff] [blame] | 45 | std::string Error; |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 46 | LLVMContext Context; |
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) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 52 | GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 53 | int32_t Mem1 = 3; |
| 54 | Engine->addGlobalMapping(G1, &Mem1); |
| 55 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1)); |
Lang Hames | 3dac3f7 | 2015-03-31 20:31:14 +0000 | [diff] [blame] | 56 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable("Global1")); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 57 | int32_t Mem2 = 4; |
| 58 | Engine->updateGlobalMapping(G1, &Mem2); |
| 59 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 60 | Engine->updateGlobalMapping(G1, nullptr); |
| 61 | EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 62 | Engine->updateGlobalMapping(G1, &Mem2); |
| 63 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)); |
| 64 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 65 | GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global1"); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 66 | EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2)) |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 67 | << "The NULL return shouldn't depend on having called" |
| 68 | << " updateGlobalMapping(..., NULL)"; |
| 69 | // Check that update...() can be called before add...(). |
| 70 | Engine->updateGlobalMapping(G2, &Mem1); |
| 71 | EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2)); |
| 72 | EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1)) |
| 73 | << "A second mapping shouldn't affect the first."; |
| 74 | } |
| 75 | |
| 76 | TEST_F(ExecutionEngineTest, ReverseGlobalMapping) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 77 | GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 78 | |
| 79 | int32_t Mem1 = 3; |
| 80 | Engine->addGlobalMapping(G1, &Mem1); |
| 81 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 82 | int32_t Mem2 = 4; |
| 83 | Engine->updateGlobalMapping(G1, &Mem2); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 84 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 85 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
| 86 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 87 | GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2"); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 88 | Engine->updateGlobalMapping(G2, &Mem1); |
| 89 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 90 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2)); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 91 | Engine->updateGlobalMapping(G1, nullptr); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 92 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)) |
| 93 | << "Removing one mapping doesn't affect a different one."; |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 94 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem2)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 95 | Engine->updateGlobalMapping(G2, &Mem2); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 96 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 97 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2)) |
| 98 | << "Once a mapping is removed, we can point another GV at the" |
| 99 | << " now-free address."; |
| 100 | } |
| 101 | |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 102 | TEST_F(ExecutionEngineTest, ClearModuleMappings) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 103 | GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1"); |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 104 | |
| 105 | int32_t Mem1 = 3; |
| 106 | Engine->addGlobalMapping(G1, &Mem1); |
| 107 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 108 | |
| 109 | Engine->clearGlobalMappingsFromModule(M); |
| 110 | |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 111 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 112 | |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 113 | GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2"); |
Jeffrey Yasskin | 307c053 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 114 | // After clearing the module mappings, we can assign a new GV to the |
| 115 | // same address. |
| 116 | Engine->addGlobalMapping(G2, &Mem1); |
| 117 | EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1)); |
| 118 | } |
| 119 | |
Jeffrey Yasskin | f98e981 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 120 | TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) { |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 121 | GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1"); |
Jeffrey Yasskin | f98e981 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 122 | int32_t Mem1 = 3; |
| 123 | Engine->addGlobalMapping(G1, &Mem1); |
| 124 | // Make sure the reverse mapping is enabled. |
| 125 | EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1)); |
| 126 | // When the GV goes away, the ExecutionEngine should remove any |
| 127 | // mappings that refer to it. |
| 128 | G1->eraseFromParent(); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 129 | EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1)); |
Jeffrey Yasskin | f98e981 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 130 | } |
| 131 | |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 132 | TEST_F(ExecutionEngineTest, LookupWithMangledAndDemangledSymbol) { |
| 133 | int x; |
| 134 | int _x; |
| 135 | llvm::sys::DynamicLibrary::AddSymbol("x", &x); |
| 136 | llvm::sys::DynamicLibrary::AddSymbol("_x", &_x); |
| 137 | |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame] | 138 | // RTDyldMemoryManager::getSymbolAddressInProcess expects a mangled symbol, |
| 139 | // but DynamicLibrary is a wrapper for dlsym, which expects the unmangled C |
| 140 | // symbol name. This test verifies that getSymbolAddressInProcess strips the |
Lang Hames | 75601bf | 2016-08-18 01:33:28 +0000 | [diff] [blame] | 141 | // leading '_' on Darwin, but not on other platforms. |
| 142 | #ifdef __APPLE__ |
Lang Hames | 2f27b2f | 2014-10-01 04:11:13 +0000 | [diff] [blame] | 143 | EXPECT_EQ(reinterpret_cast<uint64_t>(&x), |
| 144 | RTDyldMemoryManager::getSymbolAddressInProcess("_x")); |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame] | 145 | #else |
Lang Hames | 2f27b2f | 2014-10-01 04:11:13 +0000 | [diff] [blame] | 146 | EXPECT_EQ(reinterpret_cast<uint64_t>(&_x), |
| 147 | RTDyldMemoryManager::getSymbolAddressInProcess("_x")); |
Lang Hames | 3b51455 | 2016-03-03 21:23:15 +0000 | [diff] [blame] | 148 | #endif |
David Blaikie | ce3f573 | 2014-09-29 21:25:13 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jeffrey Yasskin | 337b124 | 2009-08-04 23:53:16 +0000 | [diff] [blame] | 151 | } |