blob: 22cade25c7d55e854b6e609c66b4c9bb1d712acf [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <elf.h>
Christopher Ferrisbae69f12017-06-28 14:51:54 -070018#include <fcntl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
Christopher Ferris3958f802017-02-01 15:44:40 -080022
23#include <gtest/gtest.h>
24
25#include "Elf.h"
26
27#include "MemoryFake.h"
28
29#if !defined(PT_ARM_EXIDX)
30#define PT_ARM_EXIDX 0x70000001
31#endif
32
Christopher Ferris3958f802017-02-01 15:44:40 -080033class ElfTest : public ::testing::Test {
34 protected:
35 void SetUp() override {
36 memory_ = new MemoryFake;
37 }
38
39 template <typename Ehdr>
40 void InitEhdr(Ehdr* ehdr) {
41 memset(ehdr, 0, sizeof(Ehdr));
42 memcpy(&ehdr->e_ident[0], ELFMAG, SELFMAG);
43 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
44 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
45 ehdr->e_ident[EI_OSABI] = ELFOSABI_SYSV;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070046
47 ehdr->e_type = ET_DYN;
48 ehdr->e_version = EV_CURRENT;
Christopher Ferris3958f802017-02-01 15:44:40 -080049 }
50
Christopher Ferrisbae69f12017-06-28 14:51:54 -070051 void InitElf32(uint32_t machine) {
Christopher Ferris3958f802017-02-01 15:44:40 -080052 Elf32_Ehdr ehdr;
53
54 InitEhdr<Elf32_Ehdr>(&ehdr);
55 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
56
Christopher Ferrisbae69f12017-06-28 14:51:54 -070057 ehdr.e_machine = machine;
Christopher Ferris3958f802017-02-01 15:44:40 -080058 ehdr.e_entry = 0;
59 ehdr.e_phoff = 0x100;
60 ehdr.e_shoff = 0;
61 ehdr.e_flags = 0;
62 ehdr.e_ehsize = sizeof(ehdr);
63 ehdr.e_phentsize = sizeof(Elf32_Phdr);
64 ehdr.e_phnum = 1;
65 ehdr.e_shentsize = sizeof(Elf32_Shdr);
66 ehdr.e_shnum = 0;
67 ehdr.e_shstrndx = 0;
Christopher Ferrisbae69f12017-06-28 14:51:54 -070068 if (machine == EM_ARM) {
Christopher Ferris3958f802017-02-01 15:44:40 -080069 ehdr.e_flags = 0x5000200;
70 ehdr.e_phnum = 2;
71 }
72 memory_->SetMemory(0, &ehdr, sizeof(ehdr));
73
74 Elf32_Phdr phdr;
75 memset(&phdr, 0, sizeof(phdr));
76 phdr.p_type = PT_LOAD;
77 phdr.p_offset = 0;
78 phdr.p_vaddr = 0;
79 phdr.p_paddr = 0;
80 phdr.p_filesz = 0x10000;
81 phdr.p_memsz = 0x10000;
82 phdr.p_flags = PF_R | PF_X;
83 phdr.p_align = 0x1000;
84 memory_->SetMemory(0x100, &phdr, sizeof(phdr));
85
Christopher Ferrisbae69f12017-06-28 14:51:54 -070086 if (machine == EM_ARM) {
Christopher Ferris3958f802017-02-01 15:44:40 -080087 memset(&phdr, 0, sizeof(phdr));
88 phdr.p_type = PT_ARM_EXIDX;
89 phdr.p_offset = 0x30000;
90 phdr.p_vaddr = 0x30000;
91 phdr.p_paddr = 0x30000;
92 phdr.p_filesz = 16;
93 phdr.p_memsz = 16;
94 phdr.p_flags = PF_R;
95 phdr.p_align = 0x4;
96 memory_->SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
97 }
98 }
99
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700100 void InitElf64(uint32_t machine) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800101 Elf64_Ehdr ehdr;
102
103 InitEhdr<Elf64_Ehdr>(&ehdr);
104 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
105
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700106 ehdr.e_machine = machine;
Christopher Ferris3958f802017-02-01 15:44:40 -0800107 ehdr.e_entry = 0;
108 ehdr.e_phoff = 0x100;
109 ehdr.e_shoff = 0;
110 ehdr.e_flags = 0x5000200;
111 ehdr.e_ehsize = sizeof(ehdr);
112 ehdr.e_phentsize = sizeof(Elf64_Phdr);
113 ehdr.e_phnum = 1;
114 ehdr.e_shentsize = sizeof(Elf64_Shdr);
115 ehdr.e_shnum = 0;
116 ehdr.e_shstrndx = 0;
117 memory_->SetMemory(0, &ehdr, sizeof(ehdr));
118
119 Elf64_Phdr phdr;
120 memset(&phdr, 0, sizeof(phdr));
121 phdr.p_type = PT_LOAD;
122 phdr.p_offset = 0;
123 phdr.p_vaddr = 0;
124 phdr.p_paddr = 0;
125 phdr.p_filesz = 0x10000;
126 phdr.p_memsz = 0x10000;
127 phdr.p_flags = PF_R | PF_X;
128 phdr.p_align = 0x1000;
129 memory_->SetMemory(0x100, &phdr, sizeof(phdr));
130 }
131
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700132 template <typename Ehdr, typename Shdr>
133 void GnuDebugdataInitFail(Ehdr* ehdr);
134
135 template <typename Ehdr, typename Shdr>
136 void GnuDebugdataInit(Ehdr* ehdr);
137
Christopher Ferris3958f802017-02-01 15:44:40 -0800138 MemoryFake* memory_;
139};
140
141TEST_F(ElfTest, invalid_memory) {
142 Elf elf(memory_);
143
144 ASSERT_FALSE(elf.Init());
145 ASSERT_FALSE(elf.valid());
146}
147
148TEST_F(ElfTest, elf_invalid) {
149 Elf elf(memory_);
150
151 InitElf32(EM_386);
152
153 // Corrupt the ELF signature.
154 memory_->SetData32(0, 0x7f000000);
155
156 ASSERT_FALSE(elf.Init());
157 ASSERT_FALSE(elf.valid());
158 ASSERT_TRUE(elf.interface() == nullptr);
159
160 std::string name;
161 ASSERT_FALSE(elf.GetSoname(&name));
162
163 uint64_t func_offset;
164 ASSERT_FALSE(elf.GetFunctionName(0, &name, &func_offset));
165
166 ASSERT_FALSE(elf.Step(0, nullptr, nullptr));
167}
168
169TEST_F(ElfTest, elf_arm) {
170 Elf elf(memory_);
171
172 InitElf32(EM_ARM);
173
174 ASSERT_TRUE(elf.Init());
175 ASSERT_TRUE(elf.valid());
176 ASSERT_EQ(static_cast<uint32_t>(EM_ARM), elf.machine_type());
177 ASSERT_EQ(ELFCLASS32, elf.class_type());
178 ASSERT_TRUE(elf.interface() != nullptr);
179}
180
181TEST_F(ElfTest, elf_x86) {
182 Elf elf(memory_);
183
184 InitElf32(EM_386);
185
186 ASSERT_TRUE(elf.Init());
187 ASSERT_TRUE(elf.valid());
188 ASSERT_EQ(static_cast<uint32_t>(EM_386), elf.machine_type());
189 ASSERT_EQ(ELFCLASS32, elf.class_type());
190 ASSERT_TRUE(elf.interface() != nullptr);
191}
192
193TEST_F(ElfTest, elf_arm64) {
194 Elf elf(memory_);
195
196 InitElf64(EM_AARCH64);
197
198 ASSERT_TRUE(elf.Init());
199 ASSERT_TRUE(elf.valid());
200 ASSERT_EQ(static_cast<uint32_t>(EM_AARCH64), elf.machine_type());
201 ASSERT_EQ(ELFCLASS64, elf.class_type());
202 ASSERT_TRUE(elf.interface() != nullptr);
203}
204
205TEST_F(ElfTest, elf_x86_64) {
206 Elf elf(memory_);
207
208 InitElf64(EM_X86_64);
209
210 ASSERT_TRUE(elf.Init());
211 ASSERT_TRUE(elf.valid());
212 ASSERT_EQ(static_cast<uint32_t>(EM_X86_64), elf.machine_type());
213 ASSERT_EQ(ELFCLASS64, elf.class_type());
214 ASSERT_TRUE(elf.interface() != nullptr);
215}
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700216
217template <typename Ehdr, typename Shdr>
218void ElfTest::GnuDebugdataInitFail(Ehdr* ehdr) {
219 Elf elf(memory_);
220
221 uint64_t offset = 0x2000;
222
223 ehdr->e_shoff = offset;
224 ehdr->e_shnum = 3;
225 ehdr->e_shentsize = sizeof(Shdr);
226 ehdr->e_shstrndx = 2;
227 memory_->SetMemory(0, ehdr, sizeof(*ehdr));
228
229 Shdr shdr;
230 memset(&shdr, 0, sizeof(shdr));
231 shdr.sh_type = SHT_NULL;
232 memory_->SetMemory(offset, &shdr, sizeof(shdr));
233 offset += ehdr->e_shentsize;
234
235 memset(&shdr, 0, sizeof(shdr));
236 shdr.sh_type = SHT_PROGBITS;
237 shdr.sh_name = 0x100;
238 shdr.sh_addr = 0x5000;
239 shdr.sh_offset = 0x5000;
240 shdr.sh_entsize = 0x100;
241 shdr.sh_size = 0x800;
242 memory_->SetMemory(offset, &shdr, sizeof(shdr));
243 offset += ehdr->e_shentsize;
244
245 memset(&shdr, 0, sizeof(shdr));
246 shdr.sh_type = SHT_STRTAB;
247 shdr.sh_name = 0x200000;
248 shdr.sh_offset = 0xf000;
249 shdr.sh_size = 0x1000;
250 memory_->SetMemory(offset, &shdr, sizeof(shdr));
251 offset += ehdr->e_shentsize;
252
253 memory_->SetMemory(0xf100, ".gnu_debugdata", sizeof(".gnu_debugdata"));
254
255 ASSERT_TRUE(elf.Init());
256 ASSERT_TRUE(elf.interface() != nullptr);
257 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
258 EXPECT_EQ(0x5000U, elf.interface()->gnu_debugdata_offset());
259 EXPECT_EQ(0x800U, elf.interface()->gnu_debugdata_size());
260
261 elf.InitGnuDebugdata();
262}
263
264TEST_F(ElfTest, gnu_debugdata_init_fail32) {
265 Elf32_Ehdr ehdr;
266 InitEhdr<Elf32_Ehdr>(&ehdr);
267 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
268 ehdr.e_machine = EM_ARM;
269
270 GnuDebugdataInitFail<Elf32_Ehdr, Elf32_Shdr>(&ehdr);
271}
272
273TEST_F(ElfTest, gnu_debugdata_init_fail64) {
274 Elf64_Ehdr ehdr;
275 InitEhdr<Elf64_Ehdr>(&ehdr);
276 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
277 ehdr.e_machine = EM_AARCH64;
278
279 GnuDebugdataInitFail<Elf64_Ehdr, Elf64_Shdr>(&ehdr);
280}
281
282template <typename Ehdr, typename Shdr>
283void ElfTest::GnuDebugdataInit(Ehdr* ehdr) {
284 Elf elf(memory_);
285
286 uint64_t offset = 0x2000;
287
288 ehdr->e_shoff = offset;
289 ehdr->e_shnum = 3;
290 ehdr->e_shentsize = sizeof(Shdr);
291 ehdr->e_shstrndx = 2;
292 memory_->SetMemory(0, ehdr, sizeof(*ehdr));
293
294 Shdr shdr;
295 memset(&shdr, 0, sizeof(shdr));
296 shdr.sh_type = SHT_NULL;
297 memory_->SetMemory(offset, &shdr, sizeof(shdr));
298 offset += ehdr->e_shentsize;
299
300 uint64_t gnu_offset = offset;
301 offset += ehdr->e_shentsize;
302
303 memset(&shdr, 0, sizeof(shdr));
304 shdr.sh_type = SHT_STRTAB;
305 shdr.sh_name = 0x200000;
306 shdr.sh_offset = 0xf000;
307 shdr.sh_size = 0x1000;
308 memory_->SetMemory(offset, &shdr, sizeof(shdr));
309 offset += ehdr->e_shentsize;
310
311 memory_->SetMemory(0xf100, ".gnu_debugdata", sizeof(".gnu_debugdata"));
312
313 // Read in the compressed elf data and put it in our fake memory.
314 std::string name("tests/");
315 if (sizeof(Ehdr) == sizeof(Elf32_Ehdr)) {
316 name += "elf32.xz";
317 } else {
318 name += "elf64.xz";
319 }
320 int fd = TEMP_FAILURE_RETRY(open(name.c_str(), O_RDONLY));
321 ASSERT_NE(-1, fd) << "Cannot open " + name;
322 // Assumes the file is less than 1024 bytes.
323 std::vector<uint8_t> buf(1024);
324 ssize_t bytes = TEMP_FAILURE_RETRY(read(fd, buf.data(), buf.size()));
325 ASSERT_GT(bytes, 0);
326 // Make sure the file isn't too big.
327 ASSERT_NE(static_cast<size_t>(bytes), buf.size())
328 << "File " + name + " is too big, increase buffer size.";
329 close(fd);
330 buf.resize(bytes);
331 memory_->SetMemory(0x5000, buf);
332
333 memset(&shdr, 0, sizeof(shdr));
334 shdr.sh_type = SHT_PROGBITS;
335 shdr.sh_name = 0x100;
336 shdr.sh_addr = 0x5000;
337 shdr.sh_offset = 0x5000;
338 shdr.sh_size = bytes;
339 memory_->SetMemory(gnu_offset, &shdr, sizeof(shdr));
340
341 ASSERT_TRUE(elf.Init());
342 ASSERT_TRUE(elf.interface() != nullptr);
343 ASSERT_TRUE(elf.gnu_debugdata_interface() == nullptr);
344 EXPECT_EQ(0x5000U, elf.interface()->gnu_debugdata_offset());
345
346 elf.InitGnuDebugdata();
347 ASSERT_TRUE(elf.gnu_debugdata_interface() != nullptr);
348}
349
350TEST_F(ElfTest, gnu_debugdata_init32) {
351 Elf32_Ehdr ehdr;
352 InitEhdr<Elf32_Ehdr>(&ehdr);
353 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
354 ehdr.e_machine = EM_ARM;
355
356 GnuDebugdataInit<Elf32_Ehdr, Elf32_Shdr>(&ehdr);
357}
358
359TEST_F(ElfTest, gnu_debugdata_init64) {
360 Elf64_Ehdr ehdr;
361 InitEhdr<Elf64_Ehdr>(&ehdr);
362 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
363 ehdr.e_machine = EM_AARCH64;
364
365 GnuDebugdataInit<Elf64_Ehdr, Elf64_Shdr>(&ehdr);
366}