blob: 434f10102d269827b1c90083de791c2b5d780859 [file] [log] [blame]
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08001// Copyright 2014 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 "elf_file.h"
6
7#include <limits.h>
8#include <stdio.h>
9#include <unistd.h>
10#include <string>
11#include <vector>
12#include "debug.h"
13#include "elf_traits.h"
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080014#include "gtest/gtest.h"
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080015
16namespace {
17
18void GetDataFilePath(const char* name, std::string* path) {
19 std::string data_dir;
20
21 const char* bindir = getenv("bindir");
22 if (bindir) {
23 data_dir = std::string(bindir);
24 } else {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080025 char path[PATH_MAX];
26 memset(path, 0, sizeof(path));
27 ASSERT_NE(-1, readlink("/proc/self/exe", path, sizeof(path) - 1));
28
29 data_dir = std::string(path);
30 size_t pos = data_dir.rfind('/');
31 ASSERT_NE(std::string::npos, pos);
32
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080033 data_dir.erase(pos);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080034 }
35
36 *path = data_dir + "/" + name;
37}
38
39void OpenRelocsTestFile(const char* name, FILE** stream) {
40 std::string path;
41 GetDataFilePath(name, &path);
42
43 FILE* testfile = fopen(path.c_str(), "rb");
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080044 ASSERT_FALSE(testfile == NULL) << "Error opening '" << path << "'";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080045
46 FILE* temporary = tmpfile();
47 ASSERT_FALSE(temporary == NULL);
48
49 static const size_t buffer_size = 4096;
50 unsigned char buffer[buffer_size];
51
52 size_t bytes;
53 do {
54 bytes = fread(buffer, 1, sizeof(buffer), testfile);
55 ASSERT_EQ(bytes, fwrite(buffer, 1, bytes, temporary));
56 } while (bytes > 0);
57
58 ASSERT_EQ(0, fclose(testfile));
59 ASSERT_EQ(0, fseek(temporary, 0, SEEK_SET));
60 ASSERT_EQ(0, lseek(fileno(temporary), 0, SEEK_SET));
61
62 *stream = temporary;
63}
64
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080065void OpenRelocsTestFiles(const std::string& arch, FILE** relocs_so, FILE** packed_relocs_so) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080066 const std::string base = std::string("elf_file_unittest_relocs_") + arch;
67 const std::string relocs = base + ".so";
68 const std::string packed_relocs = base + "_packed.so";
69
70 OpenRelocsTestFile(relocs.c_str(), relocs_so);
71 OpenRelocsTestFile(packed_relocs.c_str(), packed_relocs_so);
72}
73
74void CloseRelocsTestFile(FILE* temporary) {
75 fclose(temporary);
76}
77
78void CloseRelocsTestFiles(FILE* relocs_so, FILE* packed_relocs_so) {
79 CloseRelocsTestFile(relocs_so);
80 CloseRelocsTestFile(packed_relocs_so);
81}
82
83void CheckFileContentsEqual(FILE* first, FILE* second) {
84 ASSERT_EQ(0, fseek(first, 0, SEEK_SET));
85 ASSERT_EQ(0, fseek(second, 0, SEEK_SET));
86
87 static const size_t buffer_size = 4096;
88 unsigned char first_buffer[buffer_size];
89 unsigned char second_buffer[buffer_size];
90
91 do {
92 size_t first_read = fread(first_buffer, 1, sizeof(first_buffer), first);
93 size_t second_read = fread(second_buffer, 1, sizeof(second_buffer), second);
94
95 EXPECT_EQ(first_read, second_read);
96 EXPECT_EQ(0, memcmp(first_buffer, second_buffer, first_read));
97 } while (!feof(first) && !feof(second));
98
99 EXPECT_TRUE(feof(first) && feof(second));
100}
101
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800102template <typename ELF>
103static void ProcessUnpack(FILE* relocs_so, FILE* packed_relocs_so) {
104 relocation_packer::ElfFile<ELF> elf_file(fileno(packed_relocs_so));
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800105
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800106 // Ensure packing fails (already packed).
107 EXPECT_FALSE(elf_file.PackRelocations());
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800108
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800109 // Unpack golden relocations, and check files are now identical.
110 EXPECT_TRUE(elf_file.UnpackRelocations());
111 CheckFileContentsEqual(packed_relocs_so, relocs_so);
112
113 CloseRelocsTestFiles(relocs_so, packed_relocs_so);
114}
115
116static void RunUnpackRelocationsTestFor(const std::string& arch) {
117 ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800118
119 FILE* relocs_so = NULL;
120 FILE* packed_relocs_so = NULL;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800121 OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800122
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800123 if (relocs_so != NULL && packed_relocs_so != NULL) {
124 // lets detect elf class
125 ASSERT_EQ(0, fseek(relocs_so, EI_CLASS, SEEK_SET))
126 << "Invalid file length: " << strerror(errno);
127 uint8_t elf_class = 0;
128 ASSERT_EQ(1U, fread(&elf_class, 1, 1, relocs_so));
129 ASSERT_EQ(0, fseek(relocs_so, 0, SEEK_SET));
130 if (elf_class == ELFCLASS32) {
131 ProcessUnpack<ELF32_traits>(relocs_so, packed_relocs_so);
132 } else {
133 ProcessUnpack<ELF64_traits>(relocs_so, packed_relocs_so);
134 }
135 }
136}
137
138template <typename ELF>
139static void ProcessPack(FILE* relocs_so, FILE* packed_relocs_so) {
140 relocation_packer::ElfFile<ELF> elf_file(fileno(relocs_so));
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800141
142 // Ensure unpacking fails (not packed).
143 EXPECT_FALSE(elf_file.UnpackRelocations());
144
145 // Pack relocations, and check files are now identical.
146 EXPECT_TRUE(elf_file.PackRelocations());
147 CheckFileContentsEqual(relocs_so, packed_relocs_so);
148
149 CloseRelocsTestFiles(relocs_so, packed_relocs_so);
150}
151
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800152static void RunPackRelocationsTestFor(const std::string& arch) {
153 ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800154
155 FILE* relocs_so = NULL;
156 FILE* packed_relocs_so = NULL;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800157 OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800158
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800159 if (relocs_so != NULL && packed_relocs_so != NULL) {
160 // lets detect elf class
161 ASSERT_EQ(0, fseek(packed_relocs_so, EI_CLASS, SEEK_SET))
162 << "Invalid file length: " << strerror(errno);
163 uint8_t elf_class = 0;
164 ASSERT_EQ(1U, fread(&elf_class, 1, 1, packed_relocs_so));
165 fseek(packed_relocs_so, 0, SEEK_SET);
166 if (elf_class == ELFCLASS32) {
167 ProcessPack<ELF32_traits>(relocs_so, packed_relocs_so);
168 } else {
169 ProcessPack<ELF64_traits>(relocs_so, packed_relocs_so);
170 }
171 }
172}
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800173
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800174} // namespace
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800175
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800176namespace relocation_packer {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800177
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800178TEST(ElfFile, PackRelocations) {
179 RunPackRelocationsTestFor("arm32");
180 RunPackRelocationsTestFor("arm64");
181}
182
183TEST(ElfFile, UnpackRelocations) {
184 RunUnpackRelocationsTestFor("arm32");
185 RunUnpackRelocationsTestFor("arm64");
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800186}
187
188} // namespace relocation_packer