| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "courgette/disassembler_elf_32_x86.h" | |
| 6 | |
| 7 #include <stddef.h> | 5 #include <stddef.h> |
| 8 #include <stdint.h> | 6 #include <stdint.h> |
| 9 | 7 |
| 10 #include <algorithm> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "courgette/assembly_program.h" | 8 #include "courgette/assembly_program.h" |
| 15 #include "courgette/base_test_unittest.h" | 9 #include "courgette/base_test_unittest.h" |
| 16 #include "courgette/image_utils.h" | 10 #include "courgette/disassembler_elf_32_x86.h" |
| 17 | |
| 18 namespace courgette { | |
| 19 | |
| 20 namespace { | |
| 21 | 11 |
| 22 class DisassemblerElf32X86Test : public BaseTest { | 12 class DisassemblerElf32X86Test : public BaseTest { |
| 23 public: | 13 public: |
| 14 |
| 24 void TestExe(const char* file_name, | 15 void TestExe(const char* file_name, |
| 25 size_t expected_abs_count, | 16 size_t expected_abs_count, |
| 26 size_t expected_rel_count) const; | 17 size_t expected_rel_count) const; |
| 27 }; | 18 }; |
| 28 | 19 |
| 29 void DisassemblerElf32X86Test::TestExe(const char* file_name, | 20 void DisassemblerElf32X86Test::TestExe(const char* file_name, |
| 30 size_t expected_abs_count, | 21 size_t expected_abs_count, |
| 31 size_t expected_rel_count) const { | 22 size_t expected_rel_count) const { |
| 32 using TypedRVA = DisassemblerElf32::TypedRVA; | |
| 33 std::string file1 = FileContents(file_name); | 23 std::string file1 = FileContents(file_name); |
| 34 | 24 |
| 35 scoped_ptr<DisassemblerElf32X86> disassembler( | 25 scoped_ptr<courgette::DisassemblerElf32X86> disassembler( |
| 36 new DisassemblerElf32X86(file1.c_str(), file1.length())); | 26 new courgette::DisassemblerElf32X86(file1.c_str(), file1.length())); |
| 37 | 27 |
| 38 bool can_parse_header = disassembler->ParseHeader(); | 28 bool can_parse_header = disassembler->ParseHeader(); |
| 39 EXPECT_TRUE(can_parse_header); | 29 EXPECT_TRUE(can_parse_header); |
| 40 EXPECT_TRUE(disassembler->ok()); | 30 EXPECT_TRUE(disassembler->ok()); |
| 41 | 31 |
| 42 // The length of the disassembled value will be slightly smaller than the | 32 // The length of the disassembled value will be slightly smaller than the |
| 43 // real file, since trailing debug info is not included | 33 // real file, since trailing debug info is not included |
| 44 EXPECT_EQ(file1.length(), disassembler->length()); | 34 EXPECT_EQ(file1.length(), disassembler->length()); |
| 45 | 35 |
| 46 const uint8_t* offset_p = disassembler->FileOffsetToPointer(0); | 36 const uint8_t* offset_p = disassembler->OffsetToPointer(0); |
| 47 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), | 37 EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()), |
| 48 reinterpret_cast<const void*>(offset_p)); | 38 reinterpret_cast<const void*>(offset_p)); |
| 49 EXPECT_EQ(0x7F, offset_p[0]); | 39 EXPECT_EQ(0x7F, offset_p[0]); |
| 50 EXPECT_EQ('E', offset_p[1]); | 40 EXPECT_EQ('E', offset_p[1]); |
| 51 EXPECT_EQ('L', offset_p[2]); | 41 EXPECT_EQ('L', offset_p[2]); |
| 52 EXPECT_EQ('F', offset_p[3]); | 42 EXPECT_EQ('F', offset_p[3]); |
| 53 | 43 |
| 54 scoped_ptr<AssemblyProgram> program(new AssemblyProgram(EXE_ELF_32_X86)); | 44 courgette::AssemblyProgram* program = |
| 45 new courgette::AssemblyProgram(courgette::EXE_ELF_32_X86); |
| 55 | 46 |
| 56 EXPECT_TRUE(disassembler->Disassemble(program.get())); | 47 EXPECT_TRUE(disassembler->Disassemble(program)); |
| 57 | 48 |
| 58 const std::vector<RVA>& abs32_list = disassembler->Abs32Locations(); | 49 EXPECT_EQ(disassembler->Abs32Locations().size(), expected_abs_count); |
| 50 EXPECT_EQ(disassembler->Rel32Locations().size(), expected_rel_count); |
| 59 | 51 |
| 60 // Flatten the list typed rel32 to a list of rel32 RVAs. | 52 // Prove that none of the rel32 RVAs overlap with abs32 RVAs |
| 61 std::vector<RVA> rel32_list; | 53 std::set<courgette::RVA> abs(disassembler->Abs32Locations().begin(), |
| 62 rel32_list.reserve(disassembler->Rel32Locations().size()); | 54 disassembler->Abs32Locations().end()); |
| 63 for (TypedRVA* typed_rel32 : disassembler->Rel32Locations()) | 55 std::set<courgette::DisassemblerElf32::TypedRVA*> |
| 64 rel32_list.push_back(typed_rel32->rva()); | 56 rel(disassembler->Rel32Locations().begin(), |
| 57 disassembler->Rel32Locations().end()); |
| 58 for (std::vector<courgette::DisassemblerElf32::TypedRVA*>::iterator |
| 59 rel32 = disassembler->Rel32Locations().begin(); |
| 60 rel32 != disassembler->Rel32Locations().end(); |
| 61 rel32++) { |
| 62 EXPECT_TRUE(abs.find((*rel32)->rva()) == abs.end()); |
| 63 } |
| 65 | 64 |
| 66 EXPECT_EQ(expected_abs_count, abs32_list.size()); | 65 for (std::vector<courgette::RVA>::iterator abs32 = |
| 67 EXPECT_EQ(expected_rel_count, rel32_list.size()); | 66 disassembler->Abs32Locations().begin(); |
| 68 | 67 abs32 != disassembler->Abs32Locations().end(); |
| 69 EXPECT_TRUE(std::is_sorted(abs32_list.begin(), abs32_list.end())); | 68 abs32++) { |
| 70 EXPECT_TRUE(std::is_sorted(rel32_list.begin(), rel32_list.end())); | 69 bool found = false; |
| 71 | 70 for (std::vector<courgette::DisassemblerElf32::TypedRVA*>::iterator |
| 72 // Verify that rel32 RVAs do not overlap with abs32 RVAs. | 71 rel32 = disassembler->Rel32Locations().begin(); |
| 73 // TODO(huangs): Fix this to account for RVA's 4-byte width. | 72 rel32 != disassembler->Rel32Locations().end(); |
| 74 bool found_match = false; | 73 rel32++) { |
| 75 std::vector<RVA>::const_iterator abs32_it = abs32_list.begin(); | 74 if (*abs32 == (*rel32)->rva()) { |
| 76 std::vector<RVA>::const_iterator rel32_it = rel32_list.begin(); | 75 found = true; |
| 77 while (abs32_it != abs32_list.end() && rel32_it != rel32_list.end()) { | 76 break; |
| 78 if (*abs32_it < *rel32_it) { | 77 } |
| 79 ++abs32_it; | |
| 80 } else if (*abs32_it > *rel32_it) { | |
| 81 ++rel32_it; | |
| 82 } else { | |
| 83 found_match = true; | |
| 84 } | 78 } |
| 79 EXPECT_TRUE(!found); |
| 85 } | 80 } |
| 86 EXPECT_FALSE(found_match); | 81 delete program; |
| 87 } | 82 } |
| 88 | 83 |
| 89 } // namespace | |
| 90 | |
| 91 TEST_F(DisassemblerElf32X86Test, All) { | 84 TEST_F(DisassemblerElf32X86Test, All) { |
| 92 TestExe("elf-32-1", 200, 3442); | 85 TestExe("elf-32-1", 200, 3442); |
| 93 } | 86 } |
| 94 | |
| 95 } // namespace courgette | |
| OLD | NEW |