1 // 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"
14 #include "gtest/gtest.h"
15 
16 namespace {
17 
GetDataFilePath(const char * name,std::string * path)18 void 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 {
25     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 
33     data_dir.erase(pos);
34   }
35 
36   *path = data_dir + "/" + name;
37 }
38 
OpenRelocsTestFile(const char * name,FILE ** stream)39 void OpenRelocsTestFile(const char* name, FILE** stream) {
40   std::string path;
41   GetDataFilePath(name, &path);
42 
43   FILE* testfile = fopen(path.c_str(), "rb");
44   ASSERT_FALSE(testfile == NULL) << "Error opening '" << path << "'";
45 
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 
OpenRelocsTestFiles(const std::string & arch,FILE ** relocs_so,FILE ** packed_relocs_so)65 void OpenRelocsTestFiles(const std::string& arch, FILE** relocs_so, FILE** packed_relocs_so) {
66   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 
CloseRelocsTestFile(FILE * temporary)74 void CloseRelocsTestFile(FILE* temporary) {
75   fclose(temporary);
76 }
77 
CloseRelocsTestFiles(FILE * relocs_so,FILE * packed_relocs_so)78 void CloseRelocsTestFiles(FILE* relocs_so, FILE* packed_relocs_so) {
79   CloseRelocsTestFile(relocs_so);
80   CloseRelocsTestFile(packed_relocs_so);
81 }
82 
CheckFileContentsEqual(FILE * first,FILE * second)83 void 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 
102 template <typename ELF>
ProcessUnpack(FILE * relocs_so,FILE * packed_relocs_so)103 static void ProcessUnpack(FILE* relocs_so, FILE* packed_relocs_so) {
104   relocation_packer::ElfFile<ELF> elf_file(fileno(packed_relocs_so));
105 
106   // Ensure packing already packed elf-file does not fail the build.
107   EXPECT_TRUE(elf_file.PackRelocations());
108 
109   // 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 
RunUnpackRelocationsTestFor(const std::string & arch)116 static void RunUnpackRelocationsTestFor(const std::string& arch) {
117   ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
118 
119   FILE* relocs_so = NULL;
120   FILE* packed_relocs_so = NULL;
121   OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
122 
123   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 
138 template <typename ELF>
ProcessPack(FILE * relocs_so,FILE * packed_relocs_so)139 static void ProcessPack(FILE* relocs_so, FILE* packed_relocs_so) {
140   relocation_packer::ElfFile<ELF> elf_file(fileno(relocs_so));
141 
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 
RunPackRelocationsTestFor(const std::string & arch)152 static void RunPackRelocationsTestFor(const std::string& arch) {
153   ASSERT_NE(static_cast<uint32_t>(EV_NONE), elf_version(EV_CURRENT));
154 
155   FILE* relocs_so = NULL;
156   FILE* packed_relocs_so = NULL;
157   OpenRelocsTestFiles(arch, &relocs_so, &packed_relocs_so);
158 
159   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 }
173 
174 }  // namespace
175 
176 namespace relocation_packer {
177 
TEST(ElfFile,PackRelocationsArm32)178 TEST(ElfFile, PackRelocationsArm32) {
179   RunPackRelocationsTestFor("arm32");
180 }
181 
TEST(ElfFile,PackRelocationsArm64)182 TEST(ElfFile, PackRelocationsArm64) {
183   RunPackRelocationsTestFor("arm64");
184 }
185 
TEST(ElfFile,PackRelocationsMips32)186 TEST(ElfFile, PackRelocationsMips32) {
187   RunPackRelocationsTestFor("mips32");
188 }
189 
TEST(ElfFile,PackRelocationsIa32)190 TEST(ElfFile, PackRelocationsIa32) {
191   RunPackRelocationsTestFor("ia32");
192 }
193 
TEST(ElfFile,PackRelocationsX64)194 TEST(ElfFile, PackRelocationsX64) {
195   RunPackRelocationsTestFor("x64");
196 }
197 
TEST(ElfFile,UnpackRelocationsArm32)198 TEST(ElfFile, UnpackRelocationsArm32) {
199   RunUnpackRelocationsTestFor("arm32");
200 }
201 
TEST(ElfFile,UnpackRelocationsArm64)202 TEST(ElfFile, UnpackRelocationsArm64) {
203   RunUnpackRelocationsTestFor("arm64");
204 }
205 
TEST(ElfFile,UnpackRelocationsMips32)206 TEST(ElfFile, UnpackRelocationsMips32) {
207   RunUnpackRelocationsTestFor("mips32");
208 }
209 
TEST(ElfFile,UnpackRelocationsIa32)210 TEST(ElfFile, UnpackRelocationsIa32) {
211   RunUnpackRelocationsTestFor("ia32");
212 }
213 
TEST(ElfFile,UnpackRelocationsX64)214 TEST(ElfFile, UnpackRelocationsX64) {
215   RunUnpackRelocationsTestFor("x64");
216 }
217 
218 }  // namespace relocation_packer
219