Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: courgette/disassembler_elf_32_x86_unittest.cc

Issue 1676683002: [Courgette] Clean up Disassembler; fix ELF Memory leaks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Wrap #include <iostream> under #if COURGETTE_HISTOGRAM_TARGETS. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « courgette/disassembler_elf_32_x86.cc ('k') | courgette/disassembler_win32_x64.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm>
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 const std::vector<RVA>& abs32_list = disassembler->Abs32Locations();
50 EXPECT_EQ(disassembler->Rel32Locations().size(), expected_rel_count);
51 59
52 // Prove that none of the rel32 RVAs overlap with abs32 RVAs 60 // Flatten the list typed rel32 to a list of rel32 RVAs.
53 std::set<courgette::RVA> abs(disassembler->Abs32Locations().begin(), 61 std::vector<RVA> rel32_list;
54 disassembler->Abs32Locations().end()); 62 rel32_list.reserve(disassembler->Rel32Locations().size());
55 std::set<courgette::DisassemblerElf32::TypedRVA*> 63 for (TypedRVA* typed_rel32 : disassembler->Rel32Locations())
56 rel(disassembler->Rel32Locations().begin(), 64 rel32_list.push_back(typed_rel32->rva());
57 disassembler->Rel32Locations().end()); 65
58 for (std::vector<courgette::DisassemblerElf32::TypedRVA*>::iterator 66 EXPECT_EQ(expected_abs_count, abs32_list.size());
59 rel32 = disassembler->Rel32Locations().begin(); 67 EXPECT_EQ(expected_rel_count, rel32_list.size());
60 rel32 != disassembler->Rel32Locations().end(); 68
61 rel32++) { 69 EXPECT_TRUE(std::is_sorted(abs32_list.begin(), abs32_list.end()));
62 EXPECT_TRUE(abs.find((*rel32)->rva()) == abs.end()); 70 EXPECT_TRUE(std::is_sorted(rel32_list.begin(), rel32_list.end()));
71
72 // Verify that rel32 RVAs do not overlap with abs32 RVAs.
73 // TODO(huangs): Fix this to account for RVA's 4-byte width.
74 bool found_match = false;
75 std::vector<RVA>::const_iterator abs32_it = abs32_list.begin();
76 std::vector<RVA>::const_iterator rel32_it = rel32_list.begin();
77 while (abs32_it != abs32_list.end() && rel32_it != rel32_list.end()) {
78 if (*abs32_it < *rel32_it) {
79 ++abs32_it;
80 } else if (*abs32_it > *rel32_it) {
81 ++rel32_it;
82 } else {
83 found_match = true;
84 }
63 } 85 }
86 EXPECT_FALSE(found_match);
87 }
64 88
65 for (std::vector<courgette::RVA>::iterator abs32 = 89 } // namespace
66 disassembler->Abs32Locations().begin();
67 abs32 != disassembler->Abs32Locations().end();
68 abs32++) {
69 bool found = false;
70 for (std::vector<courgette::DisassemblerElf32::TypedRVA*>::iterator
71 rel32 = disassembler->Rel32Locations().begin();
72 rel32 != disassembler->Rel32Locations().end();
73 rel32++) {
74 if (*abs32 == (*rel32)->rva()) {
75 found = true;
76 break;
77 }
78 }
79 EXPECT_TRUE(!found);
80 }
81 delete program;
82 }
83 90
84 TEST_F(DisassemblerElf32X86Test, All) { 91 TEST_F(DisassemblerElf32X86Test, All) {
85 TestExe("elf-32-1", 200, 3442); 92 TestExe("elf-32-1", 200, 3442);
86 } 93 }
94
95 } // namespace courgette
OLDNEW
« no previous file with comments | « courgette/disassembler_elf_32_x86.cc ('k') | courgette/disassembler_win32_x64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698