GlobalMappingLayerTest.cpp 2.0 KB

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