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