disassembler_elf_32_x86_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "courgette/disassembler_elf_32_x86.h"
  5. #include <ctype.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <algorithm>
  9. #include <memory>
  10. #include <set>
  11. #include <string>
  12. #include <vector>
  13. #include "base/memory/ptr_util.h"
  14. #include "courgette/assembly_program.h"
  15. #include "courgette/base_test_unittest.h"
  16. #include "courgette/image_utils.h"
  17. namespace courgette {
  18. namespace {
  19. class TestDisassemblerElf32X86 : public DisassemblerElf32X86 {
  20. public:
  21. TestDisassemblerElf32X86(const uint8_t* start, size_t length)
  22. : DisassemblerElf32X86(start, length) {}
  23. ~TestDisassemblerElf32X86() override = default;
  24. void TestSectionHeaderFileOffsetOrder() {
  25. std::vector<FileOffset> file_offsets;
  26. for (Elf32_Half section_id : section_header_file_offset_order_) {
  27. const Elf32_Shdr* section_header = SectionHeader(section_id);
  28. file_offsets.push_back(section_header->sh_offset);
  29. }
  30. EXPECT_EQ(static_cast<size_t>(SectionHeaderCount()), file_offsets.size());
  31. EXPECT_TRUE(std::is_sorted(file_offsets.begin(), file_offsets.end()));
  32. }
  33. void TestSectionName() {
  34. std::set<std::string> name_set;
  35. for (const Elf32_Shdr& section_header : section_header_table_) {
  36. std::string name;
  37. EXPECT_TRUE(SectionName(section_header, &name));
  38. // Ensure |name| is unique and is printable (may be empty though).
  39. EXPECT_EQ(0U, name_set.count(name));
  40. EXPECT_TRUE(std::all_of(name.begin(), name.end(), ::isprint));
  41. name_set.insert(name);
  42. }
  43. // Check for existence of a few common sections.
  44. EXPECT_EQ(1U, name_set.count(".text"));
  45. EXPECT_EQ(1U, name_set.count(".data"));
  46. EXPECT_EQ(1U, name_set.count(".rodata"));
  47. EXPECT_EQ(1U, name_set.count(".bss"));
  48. EXPECT_EQ(1U, name_set.count(".shstrtab"));
  49. }
  50. };
  51. class DisassemblerElf32X86Test : public BaseTest {
  52. public:
  53. void TestExe(const char* file_name,
  54. size_t expected_abs_count,
  55. size_t expected_rel_count) const;
  56. };
  57. void DisassemblerElf32X86Test::TestExe(const char* file_name,
  58. size_t expected_abs_count,
  59. size_t expected_rel_count) const {
  60. std::string file1 = FileContents(file_name);
  61. auto disassembler = std::make_unique<TestDisassemblerElf32X86>(
  62. reinterpret_cast<const uint8_t*>(file1.c_str()), file1.length());
  63. bool can_parse_header = disassembler->ParseHeader();
  64. EXPECT_TRUE(can_parse_header);
  65. EXPECT_TRUE(disassembler->ok());
  66. EXPECT_EQ(EXE_ELF_32_X86, disassembler->kind());
  67. EXPECT_EQ(0U, disassembler->image_base());
  68. // The length of the disassembled value will be slightly smaller than the
  69. // real file, since trailing debug info is not included
  70. EXPECT_EQ(file1.length(), disassembler->length());
  71. const uint8_t* offset_p = disassembler->FileOffsetToPointer(0);
  72. EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()),
  73. reinterpret_cast<const void*>(offset_p));
  74. EXPECT_EQ(0x7F, offset_p[0]);
  75. EXPECT_EQ('E', offset_p[1]);
  76. EXPECT_EQ('L', offset_p[2]);
  77. EXPECT_EQ('F', offset_p[3]);
  78. std::unique_ptr<AssemblyProgram> program = disassembler->CreateProgram(false);
  79. EXPECT_TRUE(nullptr != program.get());
  80. const std::vector<RVA>& abs32_list = disassembler->Abs32Locations();
  81. // Flatten the list typed rel32 to a list of rel32 RVAs.
  82. std::vector<RVA> rel32_list;
  83. rel32_list.reserve(disassembler->Rel32Locations().size());
  84. for (auto& typed_rel32 : disassembler->Rel32Locations())
  85. rel32_list.push_back(typed_rel32->rva());
  86. EXPECT_EQ(expected_abs_count, abs32_list.size());
  87. EXPECT_EQ(expected_rel_count, rel32_list.size());
  88. EXPECT_TRUE(std::is_sorted(abs32_list.begin(), abs32_list.end()));
  89. EXPECT_TRUE(std::is_sorted(rel32_list.begin(), rel32_list.end()));
  90. // Verify that rel32 RVAs do not overlap with abs32 RVAs.
  91. // TODO(huangs): Fix this to account for RVA's 4-byte width.
  92. bool found_match = false;
  93. std::vector<RVA>::const_iterator abs32_it = abs32_list.begin();
  94. std::vector<RVA>::const_iterator rel32_it = rel32_list.begin();
  95. while (abs32_it != abs32_list.end() && rel32_it != rel32_list.end()) {
  96. if (*abs32_it < *rel32_it) {
  97. ++abs32_it;
  98. } else if (*abs32_it > *rel32_it) {
  99. ++rel32_it;
  100. } else {
  101. found_match = true;
  102. }
  103. }
  104. EXPECT_FALSE(found_match);
  105. disassembler->TestSectionHeaderFileOffsetOrder();
  106. disassembler->TestSectionName();
  107. }
  108. } // namespace
  109. TEST_F(DisassemblerElf32X86Test, All) {
  110. TestExe("elf-32-1", 200, 3337);
  111. TestExe("elf-32-high-bss", 0, 4);
  112. }
  113. } // namespace courgette