blob: feb24ce0575d3fabc45a0944131abf9437a5610e [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>
18
19#include <gtest/gtest.h>
20
21#include <vector>
22
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/Regs.h>
24
Christopher Ferris3958f802017-02-01 15:44:40 -080025#include "ElfInterfaceArm.h"
26#include "Machine.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080027
Christopher Ferrise69f4702017-10-19 16:08:58 -070028#include "ElfFake.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080029#include "MemoryFake.h"
30
Christopher Ferrisd226a512017-07-14 10:37:19 -070031namespace unwindstack {
32
Christopher Ferris3958f802017-02-01 15:44:40 -080033class ElfInterfaceArmTest : public ::testing::Test {
34 protected:
35 void SetUp() override {
36 memory_.Clear();
37 process_memory_.Clear();
38 }
39
40 MemoryFake memory_;
41 MemoryFake process_memory_;
42};
43
44TEST_F(ElfInterfaceArmTest, GetPrel32Addr) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070045 ElfInterfaceArmFake interface(&memory_);
Christopher Ferris3958f802017-02-01 15:44:40 -080046 memory_.SetData32(0x1000, 0x230000);
47
48 uint32_t value;
49 ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value));
50 ASSERT_EQ(0x231000U, value);
51
52 memory_.SetData32(0x1000, 0x80001000);
53 ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value));
54 ASSERT_EQ(0x2000U, value);
55
56 memory_.SetData32(0x1000, 0x70001000);
57 ASSERT_TRUE(interface.GetPrel31Addr(0x1000, &value));
58 ASSERT_EQ(0xf0002000U, value);
59}
60
61TEST_F(ElfInterfaceArmTest, FindEntry_start_zero) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070062 ElfInterfaceArmFake interface(&memory_);
63 interface.FakeSetStartOffset(0);
64 interface.FakeSetTotalEntries(10);
Christopher Ferris3958f802017-02-01 15:44:40 -080065
66 uint64_t entry_offset;
67 ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset));
68}
69
70TEST_F(ElfInterfaceArmTest, FindEntry_no_entries) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070071 ElfInterfaceArmFake interface(&memory_);
72 interface.FakeSetStartOffset(0x100);
73 interface.FakeSetTotalEntries(0);
Christopher Ferris3958f802017-02-01 15:44:40 -080074
75 uint64_t entry_offset;
76 ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset));
77}
78
79TEST_F(ElfInterfaceArmTest, FindEntry_no_valid_memory) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070080 ElfInterfaceArmFake interface(&memory_);
81 interface.FakeSetStartOffset(0x100);
82 interface.FakeSetTotalEntries(2);
Christopher Ferris3958f802017-02-01 15:44:40 -080083
84 uint64_t entry_offset;
85 ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset));
86}
87
88TEST_F(ElfInterfaceArmTest, FindEntry_ip_before_first) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070089 ElfInterfaceArmFake interface(&memory_);
90 interface.FakeSetStartOffset(0x1000);
91 interface.FakeSetTotalEntries(1);
Christopher Ferris3958f802017-02-01 15:44:40 -080092 memory_.SetData32(0x1000, 0x6000);
93
94 uint64_t entry_offset;
95 ASSERT_FALSE(interface.FindEntry(0x1000, &entry_offset));
96}
97
98TEST_F(ElfInterfaceArmTest, FindEntry_single_entry_negative_value) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070099 ElfInterfaceArmFake interface(&memory_);
100 interface.FakeSetStartOffset(0x8000);
101 interface.FakeSetTotalEntries(1);
Christopher Ferris3958f802017-02-01 15:44:40 -0800102 memory_.SetData32(0x8000, 0x7fffff00);
103
104 uint64_t entry_offset;
105 ASSERT_TRUE(interface.FindEntry(0x7ff0, &entry_offset));
106 ASSERT_EQ(0x8000U, entry_offset);
107}
108
109TEST_F(ElfInterfaceArmTest, FindEntry_two_entries) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700110 ElfInterfaceArmFake interface(&memory_);
111 interface.FakeSetStartOffset(0x1000);
112 interface.FakeSetTotalEntries(2);
Christopher Ferris3958f802017-02-01 15:44:40 -0800113 memory_.SetData32(0x1000, 0x6000);
114 memory_.SetData32(0x1008, 0x7000);
115
116 uint64_t entry_offset;
117 ASSERT_TRUE(interface.FindEntry(0x7000, &entry_offset));
118 ASSERT_EQ(0x1000U, entry_offset);
119}
120
Christopher Ferris3958f802017-02-01 15:44:40 -0800121TEST_F(ElfInterfaceArmTest, FindEntry_last_check_single_entry) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700122 ElfInterfaceArmFake interface(&memory_);
123 interface.FakeSetStartOffset(0x1000);
124 interface.FakeSetTotalEntries(1);
Christopher Ferris3958f802017-02-01 15:44:40 -0800125 memory_.SetData32(0x1000, 0x6000);
126
127 uint64_t entry_offset;
128 ASSERT_TRUE(interface.FindEntry(0x7000, &entry_offset));
129 ASSERT_EQ(0x1000U, entry_offset);
130
131 // To guarantee that we are using the cache on the second run,
132 // set the memory to a different value.
133 memory_.SetData32(0x1000, 0x8000);
134 ASSERT_TRUE(interface.FindEntry(0x7004, &entry_offset));
135 ASSERT_EQ(0x1000U, entry_offset);
136}
137
138TEST_F(ElfInterfaceArmTest, FindEntry_last_check_multiple_entries) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700139 ElfInterfaceArmFake interface(&memory_);
140 interface.FakeSetStartOffset(0x1000);
141 interface.FakeSetTotalEntries(2);
Christopher Ferris3958f802017-02-01 15:44:40 -0800142 memory_.SetData32(0x1000, 0x6000);
143 memory_.SetData32(0x1008, 0x8000);
144
145 uint64_t entry_offset;
146 ASSERT_TRUE(interface.FindEntry(0x9008, &entry_offset));
147 ASSERT_EQ(0x1008U, entry_offset);
148
149 // To guarantee that we are using the cache on the second run,
150 // set the memory to a different value.
151 memory_.SetData32(0x1000, 0x16000);
152 memory_.SetData32(0x1008, 0x18000);
153 ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset));
154 ASSERT_EQ(0x1008U, entry_offset);
155}
156
157TEST_F(ElfInterfaceArmTest, FindEntry_multiple_entries_even) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700158 ElfInterfaceArmFake interface(&memory_);
159 interface.FakeSetStartOffset(0x1000);
160 interface.FakeSetTotalEntries(4);
Christopher Ferris3958f802017-02-01 15:44:40 -0800161 memory_.SetData32(0x1000, 0x6000);
162 memory_.SetData32(0x1008, 0x7000);
163 memory_.SetData32(0x1010, 0x8000);
164 memory_.SetData32(0x1018, 0x9000);
165
166 uint64_t entry_offset;
167 ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset));
168 ASSERT_EQ(0x1010U, entry_offset);
169
170 // To guarantee that we are using the cache on the second run,
171 // set the memory to a different value.
172 memory_.SetData32(0x1000, 0x16000);
173 memory_.SetData32(0x1008, 0x17000);
174 memory_.SetData32(0x1010, 0x18000);
175 memory_.SetData32(0x1018, 0x19000);
176 ASSERT_TRUE(interface.FindEntry(0x9100, &entry_offset));
177 ASSERT_EQ(0x1010U, entry_offset);
178}
179
180TEST_F(ElfInterfaceArmTest, FindEntry_multiple_entries_odd) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700181 ElfInterfaceArmFake interface(&memory_);
182 interface.FakeSetStartOffset(0x1000);
183 interface.FakeSetTotalEntries(5);
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 memory_.SetData32(0x1000, 0x5000);
185 memory_.SetData32(0x1008, 0x6000);
186 memory_.SetData32(0x1010, 0x7000);
187 memory_.SetData32(0x1018, 0x8000);
188 memory_.SetData32(0x1020, 0x9000);
189
190 uint64_t entry_offset;
191 ASSERT_TRUE(interface.FindEntry(0x8100, &entry_offset));
192 ASSERT_EQ(0x1010U, entry_offset);
193
194 // To guarantee that we are using the cache on the second run,
195 // set the memory to a different value.
196 memory_.SetData32(0x1000, 0x15000);
197 memory_.SetData32(0x1008, 0x16000);
198 memory_.SetData32(0x1010, 0x17000);
199 memory_.SetData32(0x1018, 0x18000);
200 memory_.SetData32(0x1020, 0x19000);
201 ASSERT_TRUE(interface.FindEntry(0x8100, &entry_offset));
202 ASSERT_EQ(0x1010U, entry_offset);
203}
204
205TEST_F(ElfInterfaceArmTest, iterate) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700206 ElfInterfaceArmFake interface(&memory_);
207 interface.FakeSetStartOffset(0x1000);
208 interface.FakeSetTotalEntries(5);
Christopher Ferris3958f802017-02-01 15:44:40 -0800209 memory_.SetData32(0x1000, 0x5000);
210 memory_.SetData32(0x1008, 0x6000);
211 memory_.SetData32(0x1010, 0x7000);
212 memory_.SetData32(0x1018, 0x8000);
213 memory_.SetData32(0x1020, 0x9000);
214
215 std::vector<uint32_t> entries;
216 for (auto addr : interface) {
217 entries.push_back(addr);
218 }
219 ASSERT_EQ(5U, entries.size());
220 ASSERT_EQ(0x6000U, entries[0]);
221 ASSERT_EQ(0x7008U, entries[1]);
222 ASSERT_EQ(0x8010U, entries[2]);
223 ASSERT_EQ(0x9018U, entries[3]);
224 ASSERT_EQ(0xa020U, entries[4]);
225
226 // Make sure the iterate cached the entries.
227 memory_.SetData32(0x1000, 0x11000);
228 memory_.SetData32(0x1008, 0x12000);
229 memory_.SetData32(0x1010, 0x13000);
230 memory_.SetData32(0x1018, 0x14000);
231 memory_.SetData32(0x1020, 0x15000);
232
233 entries.clear();
234 for (auto addr : interface) {
235 entries.push_back(addr);
236 }
237 ASSERT_EQ(5U, entries.size());
238 ASSERT_EQ(0x6000U, entries[0]);
239 ASSERT_EQ(0x7008U, entries[1]);
240 ASSERT_EQ(0x8010U, entries[2]);
241 ASSERT_EQ(0x9018U, entries[3]);
242 ASSERT_EQ(0xa020U, entries[4]);
243}
244
Christopher Ferris3958f802017-02-01 15:44:40 -0800245TEST_F(ElfInterfaceArmTest, HandleType_not_arm_exidx) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700246 ElfInterfaceArmFake interface(&memory_);
Christopher Ferris3958f802017-02-01 15:44:40 -0800247
Christopher Ferrise69f4702017-10-19 16:08:58 -0700248 ASSERT_FALSE(interface.HandleType(0x1000, PT_NULL, 0));
249 ASSERT_FALSE(interface.HandleType(0x1000, PT_LOAD, 0));
250 ASSERT_FALSE(interface.HandleType(0x1000, PT_DYNAMIC, 0));
251 ASSERT_FALSE(interface.HandleType(0x1000, PT_INTERP, 0));
252 ASSERT_FALSE(interface.HandleType(0x1000, PT_NOTE, 0));
253 ASSERT_FALSE(interface.HandleType(0x1000, PT_SHLIB, 0));
254 ASSERT_FALSE(interface.HandleType(0x1000, PT_PHDR, 0));
255 ASSERT_FALSE(interface.HandleType(0x1000, PT_TLS, 0));
256 ASSERT_FALSE(interface.HandleType(0x1000, PT_LOOS, 0));
257 ASSERT_FALSE(interface.HandleType(0x1000, PT_HIOS, 0));
258 ASSERT_FALSE(interface.HandleType(0x1000, PT_LOPROC, 0));
259 ASSERT_FALSE(interface.HandleType(0x1000, PT_HIPROC, 0));
260 ASSERT_FALSE(interface.HandleType(0x1000, PT_GNU_EH_FRAME, 0));
261 ASSERT_FALSE(interface.HandleType(0x1000, PT_GNU_STACK, 0));
Christopher Ferris3958f802017-02-01 15:44:40 -0800262}
263
264TEST_F(ElfInterfaceArmTest, HandleType_arm_exidx) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700265 ElfInterfaceArmFake interface(&memory_);
Christopher Ferris3958f802017-02-01 15:44:40 -0800266
267 Elf32_Phdr phdr;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700268 interface.FakeSetStartOffset(0x1000);
269 interface.FakeSetTotalEntries(100);
Christopher Ferris3958f802017-02-01 15:44:40 -0800270 phdr.p_vaddr = 0x2000;
271 phdr.p_memsz = 0xa00;
272
273 // Verify that if reads fail, we don't set the values but still get true.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700274 ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
Christopher Ferris3958f802017-02-01 15:44:40 -0800275 ASSERT_EQ(0x1000U, interface.start_offset());
276 ASSERT_EQ(100U, interface.total_entries());
277
278 // Verify that if the second read fails, we still don't set the values.
279 memory_.SetData32(
280 0x1000 + reinterpret_cast<uint64_t>(&phdr.p_vaddr) - reinterpret_cast<uint64_t>(&phdr),
281 phdr.p_vaddr);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700282 ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
Christopher Ferris3958f802017-02-01 15:44:40 -0800283 ASSERT_EQ(0x1000U, interface.start_offset());
284 ASSERT_EQ(100U, interface.total_entries());
285
286 // Everything is correct and present.
287 memory_.SetData32(
288 0x1000 + reinterpret_cast<uint64_t>(&phdr.p_memsz) - reinterpret_cast<uint64_t>(&phdr),
289 phdr.p_memsz);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700290 ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0));
Christopher Ferris3958f802017-02-01 15:44:40 -0800291 ASSERT_EQ(0x2000U, interface.start_offset());
292 ASSERT_EQ(320U, interface.total_entries());
293
294 // Non-zero load bias.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700295 ASSERT_TRUE(interface.HandleType(0x1000, 0x70000001, 0x1000));
Christopher Ferris3958f802017-02-01 15:44:40 -0800296 ASSERT_EQ(0x1000U, interface.start_offset());
297 ASSERT_EQ(320U, interface.total_entries());
298}
299
300TEST_F(ElfInterfaceArmTest, StepExidx) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700301 ElfInterfaceArmFake interface(&memory_);
Christopher Ferris3958f802017-02-01 15:44:40 -0800302
303 // FindEntry fails.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700304 bool finished;
305 ASSERT_FALSE(interface.StepExidx(0x7000, nullptr, nullptr, &finished));
Christopher Ferris3958f802017-02-01 15:44:40 -0800306
307 // ExtractEntry should fail.
Christopher Ferrise69f4702017-10-19 16:08:58 -0700308 interface.FakeSetStartOffset(0x1000);
309 interface.FakeSetTotalEntries(2);
Christopher Ferris3958f802017-02-01 15:44:40 -0800310 memory_.SetData32(0x1000, 0x6000);
311 memory_.SetData32(0x1008, 0x8000);
312
313 RegsArm regs;
314 regs[ARM_REG_SP] = 0x1000;
315 regs[ARM_REG_LR] = 0x20000;
316 regs.set_sp(regs[ARM_REG_SP]);
317 regs.set_pc(0x1234);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700318 ASSERT_FALSE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
Christopher Ferris3958f802017-02-01 15:44:40 -0800319
320 // Eval should fail.
321 memory_.SetData32(0x1004, 0x81000000);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700322 ASSERT_FALSE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
Christopher Ferris3958f802017-02-01 15:44:40 -0800323
324 // Everything should pass.
325 memory_.SetData32(0x1004, 0x80b0b0b0);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700326 ASSERT_TRUE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
327 ASSERT_FALSE(finished);
Christopher Ferris3958f802017-02-01 15:44:40 -0800328 ASSERT_EQ(0x1000U, regs.sp());
329 ASSERT_EQ(0x1000U, regs[ARM_REG_SP]);
330 ASSERT_EQ(0x20000U, regs.pc());
331 ASSERT_EQ(0x20000U, regs[ARM_REG_PC]);
332}
333
334TEST_F(ElfInterfaceArmTest, StepExidx_pc_set) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700335 ElfInterfaceArmFake interface(&memory_);
Christopher Ferris3958f802017-02-01 15:44:40 -0800336
Christopher Ferrise69f4702017-10-19 16:08:58 -0700337 interface.FakeSetStartOffset(0x1000);
338 interface.FakeSetTotalEntries(2);
Christopher Ferris3958f802017-02-01 15:44:40 -0800339 memory_.SetData32(0x1000, 0x6000);
340 memory_.SetData32(0x1004, 0x808800b0);
341 memory_.SetData32(0x1008, 0x8000);
342 process_memory_.SetData32(0x10000, 0x10);
343
344 RegsArm regs;
345 regs[ARM_REG_SP] = 0x10000;
346 regs[ARM_REG_LR] = 0x20000;
347 regs.set_sp(regs[ARM_REG_SP]);
348 regs.set_pc(0x1234);
349
350 // Everything should pass.
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700351 bool finished;
352 ASSERT_TRUE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
353 ASSERT_FALSE(finished);
Christopher Ferris3958f802017-02-01 15:44:40 -0800354 ASSERT_EQ(0x10004U, regs.sp());
355 ASSERT_EQ(0x10004U, regs[ARM_REG_SP]);
356 ASSERT_EQ(0x10U, regs.pc());
357 ASSERT_EQ(0x10U, regs[ARM_REG_PC]);
358}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700359
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700360TEST_F(ElfInterfaceArmTest, StepExidx_cant_unwind) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700361 ElfInterfaceArmFake interface(&memory_);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700362
Christopher Ferrise69f4702017-10-19 16:08:58 -0700363 interface.FakeSetStartOffset(0x1000);
364 interface.FakeSetTotalEntries(1);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700365 memory_.SetData32(0x1000, 0x6000);
366 memory_.SetData32(0x1004, 1);
367
368 RegsArm regs;
369 regs[ARM_REG_SP] = 0x10000;
370 regs[ARM_REG_LR] = 0x20000;
371 regs.set_sp(regs[ARM_REG_SP]);
372 regs.set_pc(0x1234);
373
374 bool finished;
375 ASSERT_TRUE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
376 ASSERT_TRUE(finished);
377 ASSERT_EQ(0x10000U, regs.sp());
378 ASSERT_EQ(0x10000U, regs[ARM_REG_SP]);
379 ASSERT_EQ(0x1234U, regs.pc());
380}
381
382TEST_F(ElfInterfaceArmTest, StepExidx_refuse_unwind) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700383 ElfInterfaceArmFake interface(&memory_);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700384
Christopher Ferrise69f4702017-10-19 16:08:58 -0700385 interface.FakeSetStartOffset(0x1000);
386 interface.FakeSetTotalEntries(1);
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700387 memory_.SetData32(0x1000, 0x6000);
388 memory_.SetData32(0x1004, 0x808000b0);
389
390 RegsArm regs;
391 regs[ARM_REG_SP] = 0x10000;
392 regs[ARM_REG_LR] = 0x20000;
393 regs.set_sp(regs[ARM_REG_SP]);
394 regs.set_pc(0x1234);
395
396 bool finished;
397 ASSERT_TRUE(interface.StepExidx(0x7000, &regs, &process_memory_, &finished));
398 ASSERT_TRUE(finished);
399 ASSERT_EQ(0x10000U, regs.sp());
400 ASSERT_EQ(0x10000U, regs[ARM_REG_SP]);
401 ASSERT_EQ(0x1234U, regs.pc());
402}
403
Christopher Ferrisd226a512017-07-14 10:37:19 -0700404} // namespace unwindstack