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