blob: 7e10935ebf3d1c4ee0fca3e5c9ef595165d5fe5d [file] [log] [blame]
Christopher Ferris53a3c9b2017-05-10 18:34:15 -07001/*
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 <stdint.h>
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080022#include <unwindstack/DwarfError.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/DwarfSection.h>
24
25#include "DwarfEncoding.h"
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070026
27#include "LogFake.h"
28#include "MemoryFake.h"
29#include "RegsFake.h"
30
Christopher Ferrisd226a512017-07-14 10:37:19 -070031namespace unwindstack {
32
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070033template <typename TypeParam>
34class MockDwarfSectionImpl : public DwarfSectionImpl<TypeParam> {
35 public:
36 MockDwarfSectionImpl(Memory* memory) : DwarfSectionImpl<TypeParam>(memory) {}
37 virtual ~MockDwarfSectionImpl() = default;
38
39 MOCK_METHOD2(Init, bool(uint64_t, uint64_t));
40
41 MOCK_METHOD2(GetFdeOffsetFromPc, bool(uint64_t, uint64_t*));
42
43 MOCK_METHOD1(GetFdeFromIndex, const DwarfFde*(size_t));
44
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070045 MOCK_METHOD1(GetCieOffsetFromFde32, uint64_t(uint32_t));
46
47 MOCK_METHOD1(GetCieOffsetFromFde64, uint64_t(uint64_t));
48
49 MOCK_METHOD1(AdjustPcFromFde, uint64_t(uint64_t));
50
Christopher Ferrisc9dee842017-11-03 14:50:27 -070051 void TestSetCie32Value(uint32_t value32) { this->cie32_value_ = value32; }
52
53 void TestSetCie64Value(uint64_t value64) { this->cie64_value_ = value64; }
54
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070055 void TestSetCachedCieEntry(uint64_t offset, const DwarfCie& cie) {
56 this->cie_entries_[offset] = cie;
57 }
58 void TestClearCachedCieEntry() { this->cie_entries_.clear(); }
59
60 void TestSetCachedFdeEntry(uint64_t offset, const DwarfFde& fde) {
61 this->fde_entries_[offset] = fde;
62 }
63 void TestClearCachedFdeEntry() { this->fde_entries_.clear(); }
64
65 void TestSetCachedCieLocRegs(uint64_t offset, const dwarf_loc_regs_t& loc_regs) {
66 this->cie_loc_regs_[offset] = loc_regs;
67 }
68 void TestClearCachedCieLocRegs() { this->cie_loc_regs_.clear(); }
69
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080070 void TestClearError() { this->last_error_.code = DWARF_ERROR_NONE; }
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070071};
72
73template <typename TypeParam>
74class DwarfSectionImplTest : public ::testing::Test {
75 protected:
76 void SetUp() override {
77 memory_.Clear();
78 section_ = new MockDwarfSectionImpl<TypeParam>(&memory_);
79 ResetLogs();
Christopher Ferrisc9dee842017-11-03 14:50:27 -070080 section_->TestSetCie32Value(static_cast<uint32_t>(-1));
81 section_->TestSetCie64Value(static_cast<uint64_t>(-1));
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070082 }
83
84 void TearDown() override { delete section_; }
85
86 MemoryFake memory_;
87 MockDwarfSectionImpl<TypeParam>* section_ = nullptr;
88};
89TYPED_TEST_CASE_P(DwarfSectionImplTest);
90
91// NOTE: All test class variables need to be referenced as this->.
92
93TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_eval_fail) {
94 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -070095 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070096 dwarf_loc_regs_t loc_regs;
97
98 regs.set_pc(0x100);
99 regs.set_sp(0x2000);
100 regs[5] = 0x20;
101 regs[9] = 0x3000;
102 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700103 bool finished;
104 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800105 EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
106 EXPECT_EQ(0x5000U, this->section_->LastErrorAddress());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700107}
108
109TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_no_stack) {
110 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700111 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700112 dwarf_loc_regs_t loc_regs;
113
114 regs.set_pc(0x100);
115 regs.set_sp(0x2000);
116 regs[5] = 0x20;
117 regs[9] = 0x3000;
118 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x96, 0x96, 0x96});
119 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700120 bool finished;
121 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800122 EXPECT_EQ(DWARF_ERROR_ILLEGAL_STATE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700123}
124
125TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr) {
126 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700127 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700128 dwarf_loc_regs_t loc_regs;
129
130 regs.set_pc(0x100);
131 regs.set_sp(0x2000);
132 regs[5] = 0x20;
133 regs[9] = 0x3000;
134 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80});
135 TypeParam cfa_value = 0x12345;
136 this->memory_.SetMemory(0x80000000, &cfa_value, sizeof(cfa_value));
137 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x4, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700138 bool finished;
139 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
140 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700141 EXPECT_EQ(0x12345U, regs.sp());
142 EXPECT_EQ(0x20U, regs.pc());
143}
144
145TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_val_expr) {
146 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700147 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700148 dwarf_loc_regs_t loc_regs;
149
150 regs.set_pc(0x100);
151 regs.set_sp(0x2000);
152 regs[5] = 0x20;
153 regs[9] = 0x3000;
154 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80});
155 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x4, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700156 bool finished;
157 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
158 ASSERT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700159 EXPECT_EQ(0x80000000U, regs.sp());
160 EXPECT_EQ(0x20U, regs.pc());
161}
162
163TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_expr_is_register) {
164 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700165 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700166 dwarf_loc_regs_t loc_regs;
167
168 regs.set_pc(0x100);
169 regs.set_sp(0x2000);
170 regs[5] = 0x20;
171 regs[9] = 0x3000;
172 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x50, 0x96, 0x96});
173 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x2, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700174 bool finished;
175 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800176 EXPECT_EQ(DWARF_ERROR_NOT_IMPLEMENTED, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700177}
178
179TYPED_TEST_P(DwarfSectionImplTest, Eval_bad_regs) {
180 DwarfCie cie{.return_address_register = 60};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700181 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700182 dwarf_loc_regs_t loc_regs;
183
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700184 bool finished;
185 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800186 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700187}
188
189TYPED_TEST_P(DwarfSectionImplTest, Eval_no_cfa) {
190 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700191 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700192 dwarf_loc_regs_t loc_regs;
193
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700194 bool finished;
195 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800196 EXPECT_EQ(DWARF_ERROR_CFA_NOT_DEFINED, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700197}
198
199TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_bad) {
200 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700201 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700202 dwarf_loc_regs_t loc_regs;
203
204 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {20, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700205 bool finished;
206 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800207 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700208
209 this->section_->TestClearError();
210 loc_regs.erase(CFA_REG);
211 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_INVALID, {0, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700212 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800213 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700214
215 this->section_->TestClearError();
216 loc_regs.erase(CFA_REG);
217 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_OFFSET, {0, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700218 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800219 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700220
221 this->section_->TestClearError();
222 loc_regs.erase(CFA_REG);
223 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_VAL_OFFSET, {0, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700224 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800225 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700226}
227
228TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_prev) {
229 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700230 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700231 dwarf_loc_regs_t loc_regs;
232
233 regs.set_pc(0x100);
234 regs.set_sp(0x2000);
235 regs[5] = 0x20;
236 regs[9] = 0x3000;
237 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {9, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700238 bool finished;
239 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
240 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700241 EXPECT_EQ(0x20U, regs.pc());
242 EXPECT_EQ(0x2000U, regs.sp());
243}
244
245TYPED_TEST_P(DwarfSectionImplTest, Eval_cfa_register_from_value) {
246 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700247 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700248 dwarf_loc_regs_t loc_regs;
249
250 regs.set_pc(0x100);
251 regs.set_sp(0x2000);
252 regs[5] = 0x20;
253 regs[6] = 0x4000;
254 regs[9] = 0x3000;
255 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {6, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700256 bool finished;
257 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
258 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700259 EXPECT_EQ(0x20U, regs.pc());
260 EXPECT_EQ(0x4000U, regs.sp());
261}
262
263TYPED_TEST_P(DwarfSectionImplTest, Eval_double_indirection) {
264 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700265 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700266 dwarf_loc_regs_t loc_regs;
267
268 regs.set_pc(0x100);
269 regs.set_sp(0x2000);
Christopher Ferris98984b42018-01-17 12:59:45 -0800270 regs[1] = 0x100;
271 regs[3] = 0x300;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700272 regs[8] = 0x10;
273 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
Christopher Ferris98984b42018-01-17 12:59:45 -0800274 loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {3, 1}};
275 loc_regs[9] = DwarfLocation{DWARF_LOCATION_REGISTER, {1, 2}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700276 bool finished;
Christopher Ferris98984b42018-01-17 12:59:45 -0800277 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
278 EXPECT_EQ(0x301U, regs[1]);
279 EXPECT_EQ(0x300U, regs[3]);
280 EXPECT_EQ(0x10U, regs[8]);
281 EXPECT_EQ(0x102U, regs[9]);
282}
283
284TYPED_TEST_P(DwarfSectionImplTest, Eval_register_reference_chain) {
285 DwarfCie cie{.return_address_register = 5};
286 RegsImplFake<TypeParam> regs(10, 9);
287 dwarf_loc_regs_t loc_regs;
288
289 regs.set_pc(0x100);
290 regs.set_sp(0x2000);
291 regs[0] = 0x10;
292 regs[1] = 0x20;
293 regs[2] = 0x30;
294 regs[3] = 0x40;
295 regs[4] = 0x50;
296 regs[5] = 0x60;
297 regs[8] = 0x20;
298 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
299 loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {0, 1}};
300 loc_regs[2] = DwarfLocation{DWARF_LOCATION_REGISTER, {1, 2}};
301 loc_regs[3] = DwarfLocation{DWARF_LOCATION_REGISTER, {2, 3}};
302 loc_regs[4] = DwarfLocation{DWARF_LOCATION_REGISTER, {3, 4}};
303 loc_regs[5] = DwarfLocation{DWARF_LOCATION_REGISTER, {4, 5}};
304 bool finished;
305 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
306 EXPECT_EQ(0x10U, regs[0]);
307 EXPECT_EQ(0x11U, regs[1]);
308 EXPECT_EQ(0x22U, regs[2]);
309 EXPECT_EQ(0x33U, regs[3]);
310 EXPECT_EQ(0x44U, regs[4]);
311 EXPECT_EQ(0x55U, regs[5]);
312 EXPECT_EQ(0x20U, regs[8]);
313}
314
315TYPED_TEST_P(DwarfSectionImplTest, Eval_dex_pc) {
316 DwarfCie cie{.return_address_register = 5};
317 RegsImplFake<TypeParam> regs(10, 9);
318 dwarf_loc_regs_t loc_regs;
319
320 regs.set_pc(0x100);
321 regs.set_sp(0x2000);
322 regs[0] = 0x10;
323 regs[8] = 0x20;
324 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
325 loc_regs[0x20444558] = DwarfLocation{DWARF_LOCATION_REGISTER, {0, 1}};
326 bool finished;
327 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
328 EXPECT_EQ(0x10U, regs[0]);
329 EXPECT_EQ(0x20U, regs[8]);
330 EXPECT_EQ(0x11U, regs.dex_pc());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700331}
332
333TYPED_TEST_P(DwarfSectionImplTest, Eval_invalid_register) {
334 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700335 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700336 dwarf_loc_regs_t loc_regs;
337
338 regs.set_pc(0x100);
339 regs.set_sp(0x2000);
340 regs[8] = 0x10;
341 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
342 loc_regs[1] = DwarfLocation{DWARF_LOCATION_REGISTER, {10, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700343 bool finished;
344 ASSERT_FALSE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800345 EXPECT_EQ(DWARF_ERROR_ILLEGAL_VALUE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700346}
347
348TYPED_TEST_P(DwarfSectionImplTest, Eval_different_reg_locations) {
349 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700350 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700351 dwarf_loc_regs_t loc_regs;
352
353 if (sizeof(TypeParam) == sizeof(uint64_t)) {
354 this->memory_.SetData64(0x2150, 0x12345678abcdef00ULL);
355 } else {
356 this->memory_.SetData32(0x2150, 0x12345678);
357 }
358
359 regs.set_pc(0x100);
360 regs.set_sp(0x2000);
361 regs[3] = 0x234;
362 regs[5] = 0x10;
363 regs[8] = 0x2100;
364 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
365 loc_regs[1] = DwarfLocation{DWARF_LOCATION_VAL_OFFSET, {0x100, 0}};
366 loc_regs[2] = DwarfLocation{DWARF_LOCATION_OFFSET, {0x50, 0}};
367 loc_regs[3] = DwarfLocation{DWARF_LOCATION_UNDEFINED, {0, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700368 bool finished;
369 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
370 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700371 EXPECT_EQ(0x10U, regs.pc());
372 EXPECT_EQ(0x2100U, regs.sp());
373 EXPECT_EQ(0x2200U, regs[1]);
374 EXPECT_EQ(0x234U, regs[3]);
375 if (sizeof(TypeParam) == sizeof(uint64_t)) {
376 EXPECT_EQ(0x12345678abcdef00ULL, regs[2]);
377 } else {
378 EXPECT_EQ(0x12345678U, regs[2]);
379 }
380}
381
382TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address_undefined) {
383 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700384 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700385 dwarf_loc_regs_t loc_regs;
386
387 regs.set_pc(0x100);
388 regs.set_sp(0x2000);
389 regs[5] = 0x20;
390 regs[8] = 0x10;
391 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
392 loc_regs[5] = DwarfLocation{DWARF_LOCATION_UNDEFINED, {0, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700393 bool finished;
394 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
395 EXPECT_TRUE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700396 EXPECT_EQ(0U, regs.pc());
397 EXPECT_EQ(0x10U, regs.sp());
398}
399
Christopher Ferris2502a602017-10-23 13:51:54 -0700400TYPED_TEST_P(DwarfSectionImplTest, Eval_pc_zero) {
401 DwarfCie cie{.return_address_register = 5};
402 RegsImplFake<TypeParam> regs(10, 9);
403 dwarf_loc_regs_t loc_regs;
404
405 regs.set_pc(0x100);
406 regs.set_sp(0x2000);
407 regs[5] = 0;
408 regs[8] = 0x10;
409 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
410 bool finished;
411 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
412 EXPECT_TRUE(finished);
413 EXPECT_EQ(0U, regs.pc());
414 EXPECT_EQ(0x10U, regs.sp());
415}
416
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700417TYPED_TEST_P(DwarfSectionImplTest, Eval_return_address) {
418 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700419 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700420 dwarf_loc_regs_t loc_regs;
421
422 regs.set_pc(0x100);
423 regs.set_sp(0x2000);
424 regs[5] = 0x20;
425 regs[8] = 0x10;
426 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700427 bool finished;
428 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
429 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700430 EXPECT_EQ(0x20U, regs.pc());
431 EXPECT_EQ(0x10U, regs.sp());
432}
433
434TYPED_TEST_P(DwarfSectionImplTest, Eval_ignore_large_reg_loc) {
435 DwarfCie cie{.return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700436 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700437 dwarf_loc_regs_t loc_regs;
438
439 regs.set_pc(0x100);
440 regs.set_sp(0x2000);
441 regs[5] = 0x20;
442 regs[8] = 0x10;
443 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
444 // This should not result in any errors.
445 loc_regs[20] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700446 bool finished;
447 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
448 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700449 EXPECT_EQ(0x20U, regs.pc());
450 EXPECT_EQ(0x10U, regs.sp());
451}
452
453TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_expr) {
454 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700455 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700456 dwarf_loc_regs_t loc_regs;
457
458 regs.set_pc(0x100);
459 regs.set_sp(0x2000);
460 regs[8] = 0x3000;
461 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80});
462 TypeParam cfa_value = 0x12345;
463 this->memory_.SetMemory(0x80000000, &cfa_value, sizeof(cfa_value));
464 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
465 loc_regs[5] = DwarfLocation{DWARF_LOCATION_EXPRESSION, {0x4, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700466 bool finished;
467 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
468 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700469 EXPECT_EQ(0x3000U, regs.sp());
470 EXPECT_EQ(0x12345U, regs.pc());
471}
472
473TYPED_TEST_P(DwarfSectionImplTest, Eval_reg_val_expr) {
474 DwarfCie cie{.version = 3, .return_address_register = 5};
Christopher Ferrisf6f691b2017-09-25 19:23:07 -0700475 RegsImplFake<TypeParam> regs(10, 9);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700476 dwarf_loc_regs_t loc_regs;
477
478 regs.set_pc(0x100);
479 regs.set_sp(0x2000);
480 regs[8] = 0x3000;
481 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x0c, 0x00, 0x00, 0x00, 0x80});
482 loc_regs[CFA_REG] = DwarfLocation{DWARF_LOCATION_REGISTER, {8, 0}};
483 loc_regs[5] = DwarfLocation{DWARF_LOCATION_VAL_EXPRESSION, {0x4, 0x5000}};
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700484 bool finished;
485 ASSERT_TRUE(this->section_->Eval(&cie, &this->memory_, loc_regs, &regs, &finished));
486 EXPECT_FALSE(finished);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700487 EXPECT_EQ(0x3000U, regs.sp());
488 EXPECT_EQ(0x80000000U, regs.pc());
489}
490
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700491TYPED_TEST_P(DwarfSectionImplTest, GetCie_fail_should_not_cache) {
492 ASSERT_TRUE(this->section_->GetCie(0x4000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800493 EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
494 EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700495 this->section_->TestClearError();
496 ASSERT_TRUE(this->section_->GetCie(0x4000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800497 EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
498 EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700499}
500
501TYPED_TEST_P(DwarfSectionImplTest, GetCie_32_version_check) {
502 this->memory_.SetData32(0x5000, 0x100);
503 this->memory_.SetData32(0x5004, 0xffffffff);
504 this->memory_.SetData8(0x5008, 0x1);
505 this->memory_.SetData8(0x5009, '\0');
506 this->memory_.SetData8(0x500a, 4);
507 this->memory_.SetData8(0x500b, 8);
508 this->memory_.SetData8(0x500c, 0x20);
509
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700510 const DwarfCie* cie = this->section_->GetCie(0x5000);
511 ASSERT_TRUE(cie != nullptr);
512 EXPECT_EQ(1U, cie->version);
513 EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
514 EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
515 EXPECT_EQ(0U, cie->segment_size);
516 EXPECT_EQ(1U, cie->augmentation_string.size());
517 EXPECT_EQ('\0', cie->augmentation_string[0]);
518 EXPECT_EQ(0U, cie->personality_handler);
519 EXPECT_EQ(0x500dU, cie->cfa_instructions_offset);
520 EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
521 EXPECT_EQ(4U, cie->code_alignment_factor);
522 EXPECT_EQ(8, cie->data_alignment_factor);
523 EXPECT_EQ(0x20U, cie->return_address_register);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800524 EXPECT_EQ(DWARF_ERROR_NONE, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700525
526 this->section_->TestClearCachedCieEntry();
527 // Set version to 0, 2, 5 and verify we fail.
528 this->memory_.SetData8(0x5008, 0x0);
529 this->section_->TestClearError();
530 ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800531 EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700532
533 this->memory_.SetData8(0x5008, 0x2);
534 this->section_->TestClearError();
535 ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800536 EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700537
538 this->memory_.SetData8(0x5008, 0x5);
539 this->section_->TestClearError();
540 ASSERT_TRUE(this->section_->GetCie(0x5000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800541 EXPECT_EQ(DWARF_ERROR_UNSUPPORTED_VERSION, this->section_->LastErrorCode());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700542}
543
544TYPED_TEST_P(DwarfSectionImplTest, GetCie_negative_data_alignment_factor) {
545 this->memory_.SetData32(0x5000, 0x100);
546 this->memory_.SetData32(0x5004, 0xffffffff);
547 this->memory_.SetData8(0x5008, 0x1);
548 this->memory_.SetData8(0x5009, '\0');
549 this->memory_.SetData8(0x500a, 4);
550 this->memory_.SetMemory(0x500b, std::vector<uint8_t>{0xfc, 0xff, 0xff, 0xff, 0x7f});
551 this->memory_.SetData8(0x5010, 0x20);
552
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700553 const DwarfCie* cie = this->section_->GetCie(0x5000);
554 ASSERT_TRUE(cie != nullptr);
555 EXPECT_EQ(1U, cie->version);
556 EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
557 EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
558 EXPECT_EQ(0U, cie->segment_size);
559 EXPECT_EQ(1U, cie->augmentation_string.size());
560 EXPECT_EQ('\0', cie->augmentation_string[0]);
561 EXPECT_EQ(0U, cie->personality_handler);
562 EXPECT_EQ(0x5011U, cie->cfa_instructions_offset);
563 EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
564 EXPECT_EQ(4U, cie->code_alignment_factor);
565 EXPECT_EQ(-4, cie->data_alignment_factor);
566 EXPECT_EQ(0x20U, cie->return_address_register);
567}
568
569TYPED_TEST_P(DwarfSectionImplTest, GetCie_64_no_augment) {
570 this->memory_.SetData32(0x8000, 0xffffffff);
571 this->memory_.SetData64(0x8004, 0x200);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700572 this->memory_.SetData64(0x800c, 0xffffffffffffffffULL);
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700573 this->memory_.SetData8(0x8014, 0x1);
574 this->memory_.SetData8(0x8015, '\0');
575 this->memory_.SetData8(0x8016, 4);
576 this->memory_.SetData8(0x8017, 8);
577 this->memory_.SetData8(0x8018, 0x20);
578
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700579 const DwarfCie* cie = this->section_->GetCie(0x8000);
580 ASSERT_TRUE(cie != nullptr);
581 EXPECT_EQ(1U, cie->version);
582 EXPECT_EQ(DW_EH_PE_sdata8, cie->fde_address_encoding);
583 EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
584 EXPECT_EQ(0U, cie->segment_size);
585 EXPECT_EQ(1U, cie->augmentation_string.size());
586 EXPECT_EQ('\0', cie->augmentation_string[0]);
587 EXPECT_EQ(0U, cie->personality_handler);
588 EXPECT_EQ(0x8019U, cie->cfa_instructions_offset);
589 EXPECT_EQ(0x820cU, cie->cfa_instructions_end);
590 EXPECT_EQ(4U, cie->code_alignment_factor);
591 EXPECT_EQ(8, cie->data_alignment_factor);
592 EXPECT_EQ(0x20U, cie->return_address_register);
593}
594
595TYPED_TEST_P(DwarfSectionImplTest, GetCie_augment) {
596 this->memory_.SetData32(0x5000, 0x100);
597 this->memory_.SetData32(0x5004, 0xffffffff);
598 this->memory_.SetData8(0x5008, 0x1);
599 this->memory_.SetMemory(0x5009, std::vector<uint8_t>{'z', 'L', 'P', 'R', '\0'});
600 this->memory_.SetData8(0x500e, 4);
601 this->memory_.SetData8(0x500f, 8);
602 this->memory_.SetData8(0x5010, 0x10);
603 // Augment length.
604 this->memory_.SetData8(0x5011, 0xf);
605 // L data.
606 this->memory_.SetData8(0x5012, DW_EH_PE_textrel | DW_EH_PE_udata2);
607 // P data.
608 this->memory_.SetData8(0x5013, DW_EH_PE_udata4);
609 this->memory_.SetData32(0x5014, 0x12345678);
610 // R data.
611 this->memory_.SetData8(0x5018, DW_EH_PE_udata2);
612
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700613 const DwarfCie* cie = this->section_->GetCie(0x5000);
614 ASSERT_TRUE(cie != nullptr);
615 EXPECT_EQ(1U, cie->version);
616 EXPECT_EQ(DW_EH_PE_udata2, cie->fde_address_encoding);
617 EXPECT_EQ(DW_EH_PE_textrel | DW_EH_PE_udata2, cie->lsda_encoding);
618 EXPECT_EQ(0U, cie->segment_size);
619 EXPECT_EQ(5U, cie->augmentation_string.size());
620 EXPECT_EQ('z', cie->augmentation_string[0]);
621 EXPECT_EQ('L', cie->augmentation_string[1]);
622 EXPECT_EQ('P', cie->augmentation_string[2]);
623 EXPECT_EQ('R', cie->augmentation_string[3]);
624 EXPECT_EQ('\0', cie->augmentation_string[4]);
625 EXPECT_EQ(0x12345678U, cie->personality_handler);
626 EXPECT_EQ(0x5021U, cie->cfa_instructions_offset);
627 EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
628 EXPECT_EQ(4U, cie->code_alignment_factor);
629 EXPECT_EQ(8, cie->data_alignment_factor);
630 EXPECT_EQ(0x10U, cie->return_address_register);
631}
632
633TYPED_TEST_P(DwarfSectionImplTest, GetCie_version_3) {
634 this->memory_.SetData32(0x5000, 0x100);
635 this->memory_.SetData32(0x5004, 0xffffffff);
636 this->memory_.SetData8(0x5008, 0x3);
637 this->memory_.SetData8(0x5009, '\0');
638 this->memory_.SetData8(0x500a, 4);
639 this->memory_.SetData8(0x500b, 8);
640 this->memory_.SetMemory(0x500c, std::vector<uint8_t>{0x81, 0x03});
641
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700642 const DwarfCie* cie = this->section_->GetCie(0x5000);
643 ASSERT_TRUE(cie != nullptr);
644 EXPECT_EQ(3U, cie->version);
645 EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
646 EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
647 EXPECT_EQ(0U, cie->segment_size);
648 EXPECT_EQ(1U, cie->augmentation_string.size());
649 EXPECT_EQ('\0', cie->augmentation_string[0]);
650 EXPECT_EQ(0U, cie->personality_handler);
651 EXPECT_EQ(0x500eU, cie->cfa_instructions_offset);
652 EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
653 EXPECT_EQ(4U, cie->code_alignment_factor);
654 EXPECT_EQ(8, cie->data_alignment_factor);
655 EXPECT_EQ(0x181U, cie->return_address_register);
656}
657
658TYPED_TEST_P(DwarfSectionImplTest, GetCie_version_4) {
659 this->memory_.SetData32(0x5000, 0x100);
660 this->memory_.SetData32(0x5004, 0xffffffff);
661 this->memory_.SetData8(0x5008, 0x4);
662 this->memory_.SetData8(0x5009, '\0');
663 this->memory_.SetData8(0x500a, 4);
664 this->memory_.SetData8(0x500b, 0x13);
665 this->memory_.SetData8(0x500c, 4);
666 this->memory_.SetData8(0x500d, 8);
667 this->memory_.SetMemory(0x500e, std::vector<uint8_t>{0x81, 0x03});
668
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700669 const DwarfCie* cie = this->section_->GetCie(0x5000);
670 ASSERT_TRUE(cie != nullptr);
671 EXPECT_EQ(4U, cie->version);
672 EXPECT_EQ(DW_EH_PE_sdata4, cie->fde_address_encoding);
673 EXPECT_EQ(DW_EH_PE_omit, cie->lsda_encoding);
674 EXPECT_EQ(0x13U, cie->segment_size);
675 EXPECT_EQ(1U, cie->augmentation_string.size());
676 EXPECT_EQ('\0', cie->augmentation_string[0]);
677 EXPECT_EQ(0U, cie->personality_handler);
678 EXPECT_EQ(0x5010U, cie->cfa_instructions_offset);
679 EXPECT_EQ(0x5104U, cie->cfa_instructions_end);
680 EXPECT_EQ(4U, cie->code_alignment_factor);
681 EXPECT_EQ(8, cie->data_alignment_factor);
682 EXPECT_EQ(0x181U, cie->return_address_register);
683}
684
685TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_fail_should_not_cache) {
686 ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800687 EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
688 EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700689 this->section_->TestClearError();
690 ASSERT_TRUE(this->section_->GetFdeFromOffset(0x4000) == nullptr);
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800691 EXPECT_EQ(DWARF_ERROR_MEMORY_INVALID, this->section_->LastErrorCode());
692 EXPECT_EQ(0x4000U, this->section_->LastErrorAddress());
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700693}
694
695TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_no_augment) {
696 this->memory_.SetData32(0x4000, 0x20);
697 this->memory_.SetData32(0x4004, 0x8000);
698 this->memory_.SetData32(0x4008, 0x5000);
699 this->memory_.SetData32(0x400c, 0x100);
700
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700701 EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
702 DwarfCie cie{};
703 cie.fde_address_encoding = DW_EH_PE_udata4;
704 this->section_->TestSetCachedCieEntry(0x8000, cie);
705 EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
706
707 const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
708 ASSERT_TRUE(fde != nullptr);
709 ASSERT_TRUE(fde->cie != nullptr);
710 EXPECT_EQ(0x4010U, fde->cfa_instructions_offset);
711 EXPECT_EQ(0x4024U, fde->cfa_instructions_end);
712 EXPECT_EQ(0x5000U, fde->pc_start);
713 EXPECT_EQ(0x5100U, fde->pc_end);
714 EXPECT_EQ(0x8000U, fde->cie_offset);
715 EXPECT_EQ(0U, fde->lsda_address);
716}
717
718TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_no_augment_non_zero_segment_size) {
719 this->memory_.SetData32(0x4000, 0x30);
720 this->memory_.SetData32(0x4004, 0x8000);
721 this->memory_.SetData32(0x4018, 0x5000);
722 this->memory_.SetData32(0x401c, 0x100);
723
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700724 EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
725 DwarfCie cie{};
726 cie.fde_address_encoding = DW_EH_PE_udata4;
727 cie.segment_size = 0x10;
728 this->section_->TestSetCachedCieEntry(0x8000, cie);
729 EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
730
731 const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
732 ASSERT_TRUE(fde != nullptr);
733 ASSERT_TRUE(fde->cie != nullptr);
734 EXPECT_EQ(0x4020U, fde->cfa_instructions_offset);
735 EXPECT_EQ(0x4034U, fde->cfa_instructions_end);
736 EXPECT_EQ(0x5000U, fde->pc_start);
737 EXPECT_EQ(0x5100U, fde->pc_end);
738 EXPECT_EQ(0x8000U, fde->cie_offset);
739 EXPECT_EQ(0U, fde->lsda_address);
740}
741
742TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_32_augment) {
743 this->memory_.SetData32(0x4000, 0x100);
744 this->memory_.SetData32(0x4004, 0x8000);
745 this->memory_.SetData32(0x4008, 0x5000);
746 this->memory_.SetData32(0x400c, 0x100);
747 this->memory_.SetMemory(0x4010, std::vector<uint8_t>{0x82, 0x01});
748 this->memory_.SetData16(0x4012, 0x1234);
749
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700750 EXPECT_CALL(*this->section_, GetCieOffsetFromFde32(0x8000)).WillOnce(::testing::Return(0x8000));
751 DwarfCie cie{};
752 cie.fde_address_encoding = DW_EH_PE_udata4;
753 cie.augmentation_string.push_back('z');
754 cie.lsda_encoding = DW_EH_PE_udata2;
755 this->section_->TestSetCachedCieEntry(0x8000, cie);
756 EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
757
758 const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
759 ASSERT_TRUE(fde != nullptr);
760 ASSERT_TRUE(fde->cie != nullptr);
761 EXPECT_EQ(0x4094U, fde->cfa_instructions_offset);
762 EXPECT_EQ(0x4104U, fde->cfa_instructions_end);
763 EXPECT_EQ(0x5000U, fde->pc_start);
764 EXPECT_EQ(0x5100U, fde->pc_end);
765 EXPECT_EQ(0x8000U, fde->cie_offset);
766 EXPECT_EQ(0x1234U, fde->lsda_address);
767}
768
769TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_64_no_augment) {
770 this->memory_.SetData32(0x4000, 0xffffffff);
771 this->memory_.SetData64(0x4004, 0x100);
772 this->memory_.SetData64(0x400c, 0x12345678);
773 this->memory_.SetData32(0x4014, 0x5000);
774 this->memory_.SetData32(0x4018, 0x100);
775
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700776 EXPECT_CALL(*this->section_, GetCieOffsetFromFde64(0x12345678))
777 .WillOnce(::testing::Return(0x12345678));
778 DwarfCie cie{};
779 cie.fde_address_encoding = DW_EH_PE_udata4;
780 this->section_->TestSetCachedCieEntry(0x12345678, cie);
781 EXPECT_CALL(*this->section_, AdjustPcFromFde(0x5000)).WillOnce(::testing::Return(0x5000));
782
783 const DwarfFde* fde = this->section_->GetFdeFromOffset(0x4000);
784 ASSERT_TRUE(fde != nullptr);
785 ASSERT_TRUE(fde->cie != nullptr);
786 EXPECT_EQ(0x401cU, fde->cfa_instructions_offset);
787 EXPECT_EQ(0x410cU, fde->cfa_instructions_end);
788 EXPECT_EQ(0x5000U, fde->pc_start);
789 EXPECT_EQ(0x5100U, fde->pc_end);
790 EXPECT_EQ(0x12345678U, fde->cie_offset);
791 EXPECT_EQ(0U, fde->lsda_address);
792}
793
794TYPED_TEST_P(DwarfSectionImplTest, GetFdeFromOffset_cached) {
795 DwarfCie cie{};
796 cie.fde_address_encoding = DW_EH_PE_udata4;
797 cie.augmentation_string.push_back('z');
798 cie.lsda_encoding = DW_EH_PE_udata2;
799
800 DwarfFde fde_cached{};
801 fde_cached.cfa_instructions_offset = 0x1000;
802 fde_cached.cfa_instructions_end = 0x1100;
803 fde_cached.pc_start = 0x9000;
804 fde_cached.pc_end = 0x9400;
805 fde_cached.cie_offset = 0x30000;
806 fde_cached.cie = &cie;
807 this->section_->TestSetCachedFdeEntry(0x6000, fde_cached);
808
809 const DwarfFde* fde = this->section_->GetFdeFromOffset(0x6000);
810 ASSERT_TRUE(fde != nullptr);
811 ASSERT_EQ(&cie, fde->cie);
812 EXPECT_EQ(0x1000U, fde->cfa_instructions_offset);
813 EXPECT_EQ(0x1100U, fde->cfa_instructions_end);
814 EXPECT_EQ(0x9000U, fde->pc_start);
815 EXPECT_EQ(0x9400U, fde->pc_end);
816 EXPECT_EQ(0x30000U, fde->cie_offset);
817}
818
819TYPED_TEST_P(DwarfSectionImplTest, GetCfaLocationInfo_cie_not_cached) {
820 DwarfCie cie{};
821 cie.cfa_instructions_offset = 0x3000;
822 cie.cfa_instructions_end = 0x3002;
823 DwarfFde fde{};
824 fde.cie = &cie;
825 fde.cie_offset = 0x8000;
826 fde.cfa_instructions_offset = 0x6000;
827 fde.cfa_instructions_end = 0x6002;
828
829 this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x09, 0x02, 0x01});
830 this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0x09, 0x04, 0x03});
831
832 dwarf_loc_regs_t loc_regs;
833 ASSERT_TRUE(this->section_->GetCfaLocationInfo(0x100, &fde, &loc_regs));
834 ASSERT_EQ(2U, loc_regs.size());
835
836 auto entry = loc_regs.find(2);
837 ASSERT_NE(entry, loc_regs.end());
838 ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type);
839 ASSERT_EQ(1U, entry->second.values[0]);
840
841 entry = loc_regs.find(4);
842 ASSERT_NE(entry, loc_regs.end());
843 ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type);
844 ASSERT_EQ(3U, entry->second.values[0]);
845}
846
847TYPED_TEST_P(DwarfSectionImplTest, GetCfaLocationInfo_cie_cached) {
848 DwarfCie cie{};
849 cie.cfa_instructions_offset = 0x3000;
850 cie.cfa_instructions_end = 0x3002;
851 DwarfFde fde{};
852 fde.cie = &cie;
853 fde.cie_offset = 0x8000;
854 fde.cfa_instructions_offset = 0x6000;
855 fde.cfa_instructions_end = 0x6002;
856
857 dwarf_loc_regs_t cie_loc_regs{{6, {DWARF_LOCATION_REGISTER, {4, 0}}}};
858 this->section_->TestSetCachedCieLocRegs(0x8000, cie_loc_regs);
859 this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0x09, 0x04, 0x03});
860
861 dwarf_loc_regs_t loc_regs;
862 ASSERT_TRUE(this->section_->GetCfaLocationInfo(0x100, &fde, &loc_regs));
863 ASSERT_EQ(2U, loc_regs.size());
864
865 auto entry = loc_regs.find(6);
866 ASSERT_NE(entry, loc_regs.end());
867 ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type);
868 ASSERT_EQ(4U, entry->second.values[0]);
869
870 entry = loc_regs.find(4);
871 ASSERT_NE(entry, loc_regs.end());
872 ASSERT_EQ(DWARF_LOCATION_REGISTER, entry->second.type);
873 ASSERT_EQ(3U, entry->second.values[0]);
874}
875
876TYPED_TEST_P(DwarfSectionImplTest, Log) {
877 DwarfCie cie{};
878 cie.cfa_instructions_offset = 0x5000;
879 cie.cfa_instructions_end = 0x5001;
880 DwarfFde fde{};
881 fde.cie = &cie;
882 fde.cfa_instructions_offset = 0x6000;
883 fde.cfa_instructions_end = 0x6001;
884
885 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x00});
886 this->memory_.SetMemory(0x6000, std::vector<uint8_t>{0xc2});
887 ASSERT_TRUE(this->section_->Log(2, 0x1000, 0x1000, &fde));
888
889 ASSERT_EQ(
890 "4 unwind DW_CFA_nop\n"
891 "4 unwind Raw Data: 0x00\n"
892 "4 unwind DW_CFA_restore register(2)\n"
893 "4 unwind Raw Data: 0xc2\n",
894 GetFakeLogPrint());
895 ASSERT_EQ("", GetFakeLogBuf());
896}
897
898REGISTER_TYPED_TEST_CASE_P(
899 DwarfSectionImplTest, Eval_cfa_expr_eval_fail, Eval_cfa_expr_no_stack,
900 Eval_cfa_expr_is_register, Eval_cfa_expr, Eval_cfa_val_expr, Eval_bad_regs, Eval_no_cfa,
901 Eval_cfa_bad, Eval_cfa_register_prev, Eval_cfa_register_from_value, Eval_double_indirection,
Christopher Ferris98984b42018-01-17 12:59:45 -0800902 Eval_register_reference_chain, Eval_dex_pc, Eval_invalid_register, Eval_different_reg_locations,
903 Eval_return_address_undefined, Eval_pc_zero, Eval_return_address, Eval_ignore_large_reg_loc,
904 Eval_reg_expr, Eval_reg_val_expr, GetCie_fail_should_not_cache, GetCie_32_version_check,
905 GetCie_negative_data_alignment_factor, GetCie_64_no_augment, GetCie_augment, GetCie_version_3,
906 GetCie_version_4, GetFdeFromOffset_fail_should_not_cache, GetFdeFromOffset_32_no_augment,
Christopher Ferris53a3c9b2017-05-10 18:34:15 -0700907 GetFdeFromOffset_32_no_augment_non_zero_segment_size, GetFdeFromOffset_32_augment,
908 GetFdeFromOffset_64_no_augment, GetFdeFromOffset_cached, GetCfaLocationInfo_cie_not_cached,
909 GetCfaLocationInfo_cie_cached, Log);
910
911typedef ::testing::Types<uint32_t, uint64_t> DwarfSectionImplTestTypes;
912INSTANTIATE_TYPED_TEST_CASE_P(, DwarfSectionImplTest, DwarfSectionImplTestTypes);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700913
914} // namespace unwindstack