1 //===--- GlobalMappingLayerTest.cpp - Unit test the global mapping layer --===//
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
10 #include "llvm/ExecutionEngine/Orc/GlobalMappingLayer.h"
11 #include "OrcTestCommon.h"
12 #include "gtest/gtest.h"
13
14 using namespace llvm;
15 using namespace llvm::orc;
16
17 namespace {
18
TEST(GlobalMappingLayerTest,Empty)19 TEST(GlobalMappingLayerTest, Empty) {
20 MockBaseLayer<int, std::shared_ptr<Module>> TestBaseLayer;
21
22 TestBaseLayer.addModuleImpl =
23 [](std::shared_ptr<Module> M, std::shared_ptr<JITSymbolResolver> R) {
24 return 42;
25 };
26
27 TestBaseLayer.findSymbolImpl =
28 [](const std::string &Name, bool ExportedSymbolsOnly) -> JITSymbol {
29 if (Name == "bar")
30 return llvm::JITSymbol(0x4567, JITSymbolFlags::Exported);
31 return nullptr;
32 };
33
34 GlobalMappingLayer<decltype(TestBaseLayer)> L(TestBaseLayer);
35
36 // Test addModule interface.
37 int H = cantFail(L.addModule(nullptr, nullptr));
38 EXPECT_EQ(H, 42) << "Incorrect result from addModule";
39
40 // Test fall-through for missing symbol.
41 auto FooSym = L.findSymbol("foo", true);
42 EXPECT_FALSE(FooSym) << "Found unexpected symbol.";
43
44 // Test fall-through for symbol in base layer.
45 auto BarSym = L.findSymbol("bar", true);
46 EXPECT_EQ(cantFail(BarSym.getAddress()),
47 static_cast<JITTargetAddress>(0x4567))
48 << "Symbol lookup fall-through failed.";
49
50 // Test setup of a global mapping.
51 L.setGlobalMapping("foo", 0x0123);
52 auto FooSym2 = L.findSymbol("foo", true);
53 EXPECT_EQ(cantFail(FooSym2.getAddress()),
54 static_cast<JITTargetAddress>(0x0123))
55 << "Symbol mapping setup failed.";
56
57 // Test removal of a global mapping.
58 L.eraseGlobalMapping("foo");
59 auto FooSym3 = L.findSymbol("foo", true);
60 EXPECT_FALSE(FooSym3) << "Symbol mapping removal failed.";
61 }
62
63 }
64