ElfTests.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/FileUtil.h>
  17. #include <folly/experimental/TestUtil.h>
  18. #include <folly/experimental/symbolizer/Elf.h>
  19. #include <folly/portability/GTest.h>
  20. using folly::symbolizer::ElfFile;
  21. // Add some symbols for testing. Note that we have to be careful with type
  22. // signatures here to prevent name mangling
  23. uint64_t kIntegerValue = 1234567890UL;
  24. const char* kStringValue = "coconuts";
  25. class ElfTest : public ::testing::Test {
  26. protected:
  27. ElfFile elfFile_{"/proc/self/exe"};
  28. };
  29. TEST_F(ElfTest, IntegerValue) {
  30. auto sym = elfFile_.getSymbolByName("kIntegerValue");
  31. EXPECT_NE(nullptr, sym.first) << "Failed to look up symbol kIntegerValue";
  32. EXPECT_EQ(kIntegerValue, elfFile_.getSymbolValue<uint64_t>(sym.second));
  33. }
  34. TEST_F(ElfTest, PointerValue) {
  35. auto sym = elfFile_.getSymbolByName("kStringValue");
  36. EXPECT_NE(nullptr, sym.first) << "Failed to look up symbol kStringValue";
  37. ElfW(Addr) addr = elfFile_.getSymbolValue<ElfW(Addr)>(sym.second);
  38. const char* str = &elfFile_.getAddressValue<const char>(addr);
  39. EXPECT_STREQ(kStringValue, str);
  40. }
  41. TEST_F(ElfTest, iterateProgramHeaders) {
  42. auto phdr = elfFile_.iterateProgramHeaders(
  43. [](auto& h) { return h.p_type == PT_LOAD; });
  44. EXPECT_NE(nullptr, phdr);
  45. EXPECT_GE(phdr->p_filesz, 0);
  46. }
  47. TEST_F(ElfTest, TinyNonElfFile) {
  48. folly::test::TemporaryFile tmpFile;
  49. const static folly::StringPiece contents = "!";
  50. folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
  51. ElfFile elfFile;
  52. const char* msg = nullptr;
  53. auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
  54. EXPECT_EQ(ElfFile::kInvalidElfFile, res);
  55. EXPECT_STREQ("not an ELF file (too short)", msg);
  56. }
  57. TEST_F(ElfTest, NonElfScript) {
  58. folly::test::TemporaryFile tmpFile;
  59. const static folly::StringPiece contents =
  60. "#!/bin/sh\necho I'm small non-ELF executable\n";
  61. folly::writeFull(tmpFile.fd(), contents.data(), contents.size());
  62. ElfFile elfFile;
  63. const char* msg = nullptr;
  64. auto res = elfFile.openNoThrow(tmpFile.path().c_str(), true, &msg);
  65. EXPECT_EQ(ElfFile::kInvalidElfFile, res);
  66. EXPECT_STREQ("invalid ELF magic", msg);
  67. }