blob: d3853df80cfd2547c18864237b98a7aa4cfd3d43 [file] [log] [blame]
Lang Hamesa4b3d4e2015-08-27 22:20:05 +00001//===--- GlobalMappingLayerTest.cpp - Unit test the global mapping layer --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Lang Hamesa4b3d4e2015-08-27 22:20:05 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ExecutionEngine/Orc/GlobalMappingLayer.h"
Lang Hamescf771ad2017-09-28 02:17:35 +000010#include "OrcTestCommon.h"
Lang Hamesa4b3d4e2015-08-27 22:20:05 +000011#include "gtest/gtest.h"
12
13using namespace llvm;
14using namespace llvm::orc;
15
16namespace {
17
Lang Hamesa4b3d4e2015-08-27 22:20:05 +000018TEST(GlobalMappingLayerTest, Empty) {
Lang Hamescf771ad2017-09-28 02:17:35 +000019 MockBaseLayer<int, std::shared_ptr<Module>> TestBaseLayer;
20
21 TestBaseLayer.addModuleImpl =
22 [](std::shared_ptr<Module> M, std::shared_ptr<JITSymbolResolver> R) {
23 return 42;
24 };
25
26 TestBaseLayer.findSymbolImpl =
27 [](const std::string &Name, bool ExportedSymbolsOnly) -> JITSymbol {
28 if (Name == "bar")
29 return llvm::JITSymbol(0x4567, JITSymbolFlags::Exported);
30 return nullptr;
31 };
32
33 GlobalMappingLayer<decltype(TestBaseLayer)> L(TestBaseLayer);
34
35 // Test addModule interface.
36 int H = cantFail(L.addModule(nullptr, nullptr));
37 EXPECT_EQ(H, 42) << "Incorrect result from addModule";
Lang Hamesa4b3d4e2015-08-27 22:20:05 +000038
39 // Test fall-through for missing symbol.
40 auto FooSym = L.findSymbol("foo", true);
41 EXPECT_FALSE(FooSym) << "Found unexpected symbol.";
42
43 // Test fall-through for symbol in base layer.
44 auto BarSym = L.findSymbol("bar", true);
Lang Hames4ce98662017-07-07 02:59:13 +000045 EXPECT_EQ(cantFail(BarSym.getAddress()),
46 static_cast<JITTargetAddress>(0x4567))
Lang Hamesa4b3d4e2015-08-27 22:20:05 +000047 << "Symbol lookup fall-through failed.";
48
49 // Test setup of a global mapping.
50 L.setGlobalMapping("foo", 0x0123);
51 auto FooSym2 = L.findSymbol("foo", true);
Lang Hames4ce98662017-07-07 02:59:13 +000052 EXPECT_EQ(cantFail(FooSym2.getAddress()),
53 static_cast<JITTargetAddress>(0x0123))
Lang Hamesa4b3d4e2015-08-27 22:20:05 +000054 << "Symbol mapping setup failed.";
55
56 // Test removal of a global mapping.
57 L.eraseGlobalMapping("foo");
58 auto FooSym3 = L.findSymbol("foo", true);
59 EXPECT_FALSE(FooSym3) << "Symbol mapping removal failed.";
60}
61
62}