1 /*
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 <memory>
20 #include <type_traits>
21 #include <unordered_map>
22
23 #include <android-base/stringprintf.h>
24 #include <gtest/gtest.h>
25
26 #include <unwindstack/DwarfLocation.h>
27 #include <unwindstack/DwarfMemory.h>
28 #include <unwindstack/DwarfStructs.h>
29 #include <unwindstack/Elf.h>
30 #include <unwindstack/Log.h>
31
32 #include "DwarfCfa.h"
33
34 #include "LogFake.h"
35 #include "MemoryFake.h"
36
37 namespace unwindstack {
38
39 template <typename TypeParam>
40 class DwarfCfaLogTest : public ::testing::Test {
41 protected:
SetUp()42 void SetUp() override {
43 ResetLogs();
44 memory_.Clear();
45
46 dmem_.reset(new DwarfMemory(&memory_));
47
48 cie_.cfa_instructions_offset = 0x1000;
49 cie_.cfa_instructions_end = 0x1030;
50 // These two values should be different to distinguish between
51 // operations that deal with code versus data.
52 cie_.code_alignment_factor = 4;
53 cie_.data_alignment_factor = 8;
54
55 fde_.cfa_instructions_offset = 0x2000;
56 fde_.cfa_instructions_end = 0x2030;
57 fde_.pc_start = 0x2000;
58 fde_.pc_end = 0x2000;
59 fde_.pc_end = 0x10000;
60 fde_.cie = &cie_;
61 cfa_.reset(new DwarfCfa<TypeParam>(dmem_.get(), &fde_, ARCH_UNKNOWN));
62 }
63
64 MemoryFake memory_;
65 std::unique_ptr<DwarfMemory> dmem_;
66 std::unique_ptr<DwarfCfa<TypeParam>> cfa_;
67 DwarfCie cie_;
68 DwarfFde fde_;
69 };
70 TYPED_TEST_SUITE_P(DwarfCfaLogTest);
71
72 // NOTE: All class variable references have to be prefaced with this->.
73
TYPED_TEST_P(DwarfCfaLogTest,cfa_illegal)74 TYPED_TEST_P(DwarfCfaLogTest, cfa_illegal) {
75 for (uint8_t i = 0x17; i < 0x3f; i++) {
76 if (i == 0x2d || i == 0x2e || i == 0x2f) {
77 // Skip gnu extension ops and aarch64 specialized op.
78 continue;
79 }
80 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{i});
81
82 ResetLogs();
83 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
84 std::string expected = "4 unwind Illegal\n";
85 expected += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", i);
86 ASSERT_EQ(expected, GetFakeLogPrint());
87 ASSERT_EQ("", GetFakeLogBuf());
88 }
89 }
90
TYPED_TEST_P(DwarfCfaLogTest,cfa_nop)91 TYPED_TEST_P(DwarfCfaLogTest, cfa_nop) {
92 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x00});
93
94 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
95 std::string expected =
96 "4 unwind DW_CFA_nop\n"
97 "4 unwind Raw Data: 0x00\n";
98 ASSERT_EQ(expected, GetFakeLogPrint());
99 ASSERT_EQ("", GetFakeLogBuf());
100 }
101
TYPED_TEST_P(DwarfCfaLogTest,cfa_offset)102 TYPED_TEST_P(DwarfCfaLogTest, cfa_offset) {
103 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x83, 0x04});
104
105 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
106 std::string expected =
107 "4 unwind DW_CFA_offset register(3) 4\n"
108 "4 unwind Raw Data: 0x83 0x04\n";
109 ASSERT_EQ(expected, GetFakeLogPrint());
110 ASSERT_EQ("", GetFakeLogBuf());
111
112 ResetLogs();
113 this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x83, 0x84, 0x01});
114
115 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
116 expected =
117 "4 unwind DW_CFA_offset register(3) 132\n"
118 "4 unwind Raw Data: 0x83 0x84 0x01\n";
119 ASSERT_EQ(expected, GetFakeLogPrint());
120 ASSERT_EQ("", GetFakeLogBuf());
121 }
122
TYPED_TEST_P(DwarfCfaLogTest,cfa_offset_extended)123 TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended) {
124 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x05, 0x03, 0x02});
125
126 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
127 std::string expected =
128 "4 unwind DW_CFA_offset_extended register(3) 2\n"
129 "4 unwind Raw Data: 0x05 0x03 0x02\n";
130 ASSERT_EQ(expected, GetFakeLogPrint());
131 ASSERT_EQ("", GetFakeLogBuf());
132
133 ResetLogs();
134 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x05, 0x81, 0x01, 0x82, 0x12});
135
136 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
137 expected =
138 "4 unwind DW_CFA_offset_extended register(129) 2306\n"
139 "4 unwind Raw Data: 0x05 0x81 0x01 0x82 0x12\n";
140 ASSERT_EQ(expected, GetFakeLogPrint());
141 ASSERT_EQ("", GetFakeLogBuf());
142 }
143
TYPED_TEST_P(DwarfCfaLogTest,cfa_offset_extended_sf)144 TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended_sf) {
145 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x11, 0x05, 0x10});
146
147 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
148 std::string expected =
149 "4 unwind DW_CFA_offset_extended_sf register(5) 16\n"
150 "4 unwind Raw Data: 0x11 0x05 0x10\n";
151 ASSERT_EQ(expected, GetFakeLogPrint());
152 ASSERT_EQ("", GetFakeLogBuf());
153
154 // Check a negative value for the offset.
155 ResetLogs();
156 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x11, 0x86, 0x01, 0xff, 0x7f});
157
158 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
159 expected =
160 "4 unwind DW_CFA_offset_extended_sf register(134) -1\n"
161 "4 unwind Raw Data: 0x11 0x86 0x01 0xff 0x7f\n";
162 ASSERT_EQ(expected, GetFakeLogPrint());
163 ASSERT_EQ("", GetFakeLogBuf());
164 }
165
TYPED_TEST_P(DwarfCfaLogTest,cfa_restore)166 TYPED_TEST_P(DwarfCfaLogTest, cfa_restore) {
167 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0xc2});
168
169 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
170 std::string expected =
171 "4 unwind DW_CFA_restore register(2)\n"
172 "4 unwind Raw Data: 0xc2\n";
173 ASSERT_EQ(expected, GetFakeLogPrint());
174 ASSERT_EQ("", GetFakeLogBuf());
175
176 ResetLogs();
177 this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x82, 0x04, 0xc2});
178
179 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3003));
180 expected =
181 "4 unwind DW_CFA_offset register(2) 4\n"
182 "4 unwind Raw Data: 0x82 0x04\n"
183 "4 unwind DW_CFA_restore register(2)\n"
184 "4 unwind Raw Data: 0xc2\n";
185 ASSERT_EQ(expected, GetFakeLogPrint());
186 ASSERT_EQ("", GetFakeLogBuf());
187 }
188
TYPED_TEST_P(DwarfCfaLogTest,cfa_restore_extended)189 TYPED_TEST_P(DwarfCfaLogTest, cfa_restore_extended) {
190 this->memory_.SetMemory(0x4000, std::vector<uint8_t>{0x06, 0x08});
191
192 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4000, 0x4002));
193 std::string expected =
194 "4 unwind DW_CFA_restore_extended register(8)\n"
195 "4 unwind Raw Data: 0x06 0x08\n";
196 ASSERT_EQ(expected, GetFakeLogPrint());
197 ASSERT_EQ("", GetFakeLogBuf());
198
199 ResetLogs();
200 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x05, 0x82, 0x02, 0x04, 0x06, 0x82, 0x02});
201
202 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5007));
203 expected =
204 "4 unwind DW_CFA_offset_extended register(258) 4\n"
205 "4 unwind Raw Data: 0x05 0x82 0x02 0x04\n"
206 "4 unwind DW_CFA_restore_extended register(258)\n"
207 "4 unwind Raw Data: 0x06 0x82 0x02\n";
208 ASSERT_EQ(expected, GetFakeLogPrint());
209 ASSERT_EQ("", GetFakeLogBuf());
210 }
211
TYPED_TEST_P(DwarfCfaLogTest,cfa_set_loc)212 TYPED_TEST_P(DwarfCfaLogTest, cfa_set_loc) {
213 uint8_t buffer[1 + sizeof(TypeParam)];
214 buffer[0] = 0x1;
215 TypeParam address;
216 std::string raw_data("Raw Data: 0x01 ");
217 std::string address_str;
218 if (std::is_same<TypeParam, uint32_t>::value) {
219 address = 0x81234578U;
220 address_str = "0x81234578";
221 raw_data += "0x78 0x45 0x23 0x81";
222 } else {
223 address = 0x8123456712345678ULL;
224 address_str = "0x8123456712345678";
225 raw_data += "0x78 0x56 0x34 0x12 0x67 0x45 0x23 0x81";
226 }
227 memcpy(&buffer[1], &address, sizeof(address));
228
229 this->memory_.SetMemory(0x50, buffer, sizeof(buffer));
230 ResetLogs();
231
232 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
233 std::string expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
234 expected += "4 unwind " + raw_data + "\n";
235 expected += "4 unwind \n";
236 expected += "4 unwind PC " + address_str + "\n";
237 ASSERT_EQ(expected, GetFakeLogPrint());
238 ASSERT_EQ("", GetFakeLogBuf());
239
240 // Check for a set going back.
241 ResetLogs();
242 this->fde_.pc_start = address + 0x10;
243
244 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
245 expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
246 expected += "4 unwind " + raw_data + "\n";
247 expected += "4 unwind \n";
248 expected += "4 unwind PC " + address_str + "\n";
249 ASSERT_EQ(expected, GetFakeLogPrint());
250 ASSERT_EQ("", GetFakeLogBuf());
251 }
252
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc)253 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc) {
254 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x44});
255
256 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x201));
257 std::string expected =
258 "4 unwind DW_CFA_advance_loc 4\n"
259 "4 unwind Raw Data: 0x44\n"
260 "4 unwind \n"
261 "4 unwind PC 0x2010\n";
262 ASSERT_EQ(expected, GetFakeLogPrint());
263 ASSERT_EQ("", GetFakeLogBuf());
264 }
265
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc1)266 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc1) {
267 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x02, 0x04});
268
269 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x202));
270 std::string expected =
271 "4 unwind DW_CFA_advance_loc1 4\n"
272 "4 unwind Raw Data: 0x02 0x04\n"
273 "4 unwind \n"
274 "4 unwind PC 0x2004\n";
275 ASSERT_EQ(expected, GetFakeLogPrint());
276 ASSERT_EQ("", GetFakeLogBuf());
277 }
278
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc2)279 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc2) {
280 this->memory_.SetMemory(0x600, std::vector<uint8_t>{0x03, 0x04, 0x03});
281
282 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x600, 0x603));
283 std::string expected =
284 "4 unwind DW_CFA_advance_loc2 772\n"
285 "4 unwind Raw Data: 0x03 0x04 0x03\n"
286 "4 unwind \n"
287 "4 unwind PC 0x2304\n";
288 ASSERT_EQ(expected, GetFakeLogPrint());
289 ASSERT_EQ("", GetFakeLogBuf());
290 }
291
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc4)292 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc4) {
293 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x04, 0x04, 0x03, 0x02, 0x01});
294
295 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x505));
296 std::string expected =
297 "4 unwind DW_CFA_advance_loc4 16909060\n"
298 "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
299 "4 unwind \n"
300 "4 unwind PC 0x1022304\n";
301 ASSERT_EQ(expected, GetFakeLogPrint());
302 ASSERT_EQ("", GetFakeLogBuf());
303 }
304
TYPED_TEST_P(DwarfCfaLogTest,cfa_undefined)305 TYPED_TEST_P(DwarfCfaLogTest, cfa_undefined) {
306 this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x07, 0x09});
307
308 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa02));
309 std::string expected =
310 "4 unwind DW_CFA_undefined register(9)\n"
311 "4 unwind Raw Data: 0x07 0x09\n";
312 ASSERT_EQ(expected, GetFakeLogPrint());
313 ASSERT_EQ("", GetFakeLogBuf());
314
315 ResetLogs();
316 DwarfLocations cie_loc_regs;
317 this->memory_.SetMemory(0x1a00, std::vector<uint8_t>{0x07, 0x81, 0x01});
318
319 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1a00, 0x1a03));
320 expected =
321 "4 unwind DW_CFA_undefined register(129)\n"
322 "4 unwind Raw Data: 0x07 0x81 0x01\n";
323 ASSERT_EQ(expected, GetFakeLogPrint());
324 ASSERT_EQ("", GetFakeLogBuf());
325 }
326
TYPED_TEST_P(DwarfCfaLogTest,cfa_same)327 TYPED_TEST_P(DwarfCfaLogTest, cfa_same) {
328 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x08, 0x7f});
329
330 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
331 std::string expected =
332 "4 unwind DW_CFA_same_value register(127)\n"
333 "4 unwind Raw Data: 0x08 0x7f\n";
334 ASSERT_EQ(expected, GetFakeLogPrint());
335 ASSERT_EQ("", GetFakeLogBuf());
336
337 ResetLogs();
338 this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x08, 0xff, 0x01});
339
340 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
341 expected =
342 "4 unwind DW_CFA_same_value register(255)\n"
343 "4 unwind Raw Data: 0x08 0xff 0x01\n";
344 ASSERT_EQ(expected, GetFakeLogPrint());
345 ASSERT_EQ("", GetFakeLogBuf());
346 }
347
TYPED_TEST_P(DwarfCfaLogTest,cfa_register)348 TYPED_TEST_P(DwarfCfaLogTest, cfa_register) {
349 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01});
350
351 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x303));
352 std::string expected =
353 "4 unwind DW_CFA_register register(2) register(1)\n"
354 "4 unwind Raw Data: 0x09 0x02 0x01\n";
355 ASSERT_EQ(expected, GetFakeLogPrint());
356 ASSERT_EQ("", GetFakeLogBuf());
357
358 ResetLogs();
359 this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x09, 0xff, 0x01, 0xff, 0x03});
360
361 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4305));
362 expected =
363 "4 unwind DW_CFA_register register(255) register(511)\n"
364 "4 unwind Raw Data: 0x09 0xff 0x01 0xff 0x03\n";
365 ASSERT_EQ(expected, GetFakeLogPrint());
366 ASSERT_EQ("", GetFakeLogBuf());
367 }
368
TYPED_TEST_P(DwarfCfaLogTest,cfa_state)369 TYPED_TEST_P(DwarfCfaLogTest, cfa_state) {
370 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x0a});
371
372 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x301));
373
374 std::string expected =
375 "4 unwind DW_CFA_remember_state\n"
376 "4 unwind Raw Data: 0x0a\n";
377 ASSERT_EQ(expected, GetFakeLogPrint());
378 ASSERT_EQ("", GetFakeLogBuf());
379
380 ResetLogs();
381 this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x0b});
382
383 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4301));
384
385 expected =
386 "4 unwind DW_CFA_restore_state\n"
387 "4 unwind Raw Data: 0x0b\n";
388 ASSERT_EQ(expected, GetFakeLogPrint());
389 ASSERT_EQ("", GetFakeLogBuf());
390 }
391
TYPED_TEST_P(DwarfCfaLogTest,cfa_state_cfa_offset_restore)392 TYPED_TEST_P(DwarfCfaLogTest, cfa_state_cfa_offset_restore) {
393 this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x0a, 0x0e, 0x40, 0x0b});
394
395 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3004));
396
397 std::string expected =
398 "4 unwind DW_CFA_remember_state\n"
399 "4 unwind Raw Data: 0x0a\n"
400 "4 unwind DW_CFA_def_cfa_offset 64\n"
401 "4 unwind Raw Data: 0x0e 0x40\n"
402 "4 unwind DW_CFA_restore_state\n"
403 "4 unwind Raw Data: 0x0b\n";
404 ASSERT_EQ(expected, GetFakeLogPrint());
405 ASSERT_EQ("", GetFakeLogBuf());
406 }
407
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa)408 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa) {
409 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0c, 0x7f, 0x74});
410
411 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
412
413 std::string expected =
414 "4 unwind DW_CFA_def_cfa register(127) 116\n"
415 "4 unwind Raw Data: 0x0c 0x7f 0x74\n";
416 ASSERT_EQ(expected, GetFakeLogPrint());
417 ASSERT_EQ("", GetFakeLogBuf());
418
419 ResetLogs();
420 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0c, 0xff, 0x02, 0xf4, 0x04});
421
422 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
423
424 expected =
425 "4 unwind DW_CFA_def_cfa register(383) 628\n"
426 "4 unwind Raw Data: 0x0c 0xff 0x02 0xf4 0x04\n";
427 ASSERT_EQ(expected, GetFakeLogPrint());
428 ASSERT_EQ("", GetFakeLogBuf());
429 }
430
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_sf)431 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_sf) {
432 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x12, 0x30, 0x25});
433
434 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
435
436 std::string expected =
437 "4 unwind DW_CFA_def_cfa_sf register(48) 37\n"
438 "4 unwind Raw Data: 0x12 0x30 0x25\n";
439 ASSERT_EQ(expected, GetFakeLogPrint());
440 ASSERT_EQ("", GetFakeLogBuf());
441
442 // Test a negative value.
443 ResetLogs();
444 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x12, 0xa3, 0x01, 0xfa, 0x7f});
445
446 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
447
448 expected =
449 "4 unwind DW_CFA_def_cfa_sf register(163) -6\n"
450 "4 unwind Raw Data: 0x12 0xa3 0x01 0xfa 0x7f\n";
451 ASSERT_EQ(expected, GetFakeLogPrint());
452 ASSERT_EQ("", GetFakeLogBuf());
453 }
454
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_register)455 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_register) {
456 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0d, 0x72});
457
458 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
459
460 std::string expected =
461 "4 unwind DW_CFA_def_cfa_register register(114)\n"
462 "4 unwind Raw Data: 0x0d 0x72\n";
463 ASSERT_EQ(expected, GetFakeLogPrint());
464 ASSERT_EQ("", GetFakeLogBuf());
465
466 ResetLogs();
467 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0d, 0xf9, 0x20});
468
469 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
470
471 expected =
472 "4 unwind DW_CFA_def_cfa_register register(4217)\n"
473 "4 unwind Raw Data: 0x0d 0xf9 0x20\n";
474 ASSERT_EQ(expected, GetFakeLogPrint());
475 ASSERT_EQ("", GetFakeLogBuf());
476 }
477
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_offset)478 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset) {
479 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0e, 0x59});
480
481 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
482
483 std::string expected =
484 "4 unwind DW_CFA_def_cfa_offset 89\n"
485 "4 unwind Raw Data: 0x0e 0x59\n";
486 ASSERT_EQ(expected, GetFakeLogPrint());
487 ASSERT_EQ("", GetFakeLogBuf());
488
489 ResetLogs();
490 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
491
492 expected =
493 "4 unwind DW_CFA_def_cfa_offset 89\n"
494 "4 unwind Raw Data: 0x0e 0x59\n";
495 ASSERT_EQ(expected, GetFakeLogPrint());
496 ASSERT_EQ("", GetFakeLogBuf());
497
498 ResetLogs();
499 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0e, 0xd4, 0x0a});
500
501 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
502
503 expected =
504 "4 unwind DW_CFA_def_cfa_offset 1364\n"
505 "4 unwind Raw Data: 0x0e 0xd4 0x0a\n";
506 ASSERT_EQ(expected, GetFakeLogPrint());
507 ASSERT_EQ("", GetFakeLogBuf());
508 }
509
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_offset_sf)510 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset_sf) {
511 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x13, 0x23});
512
513 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
514
515 std::string expected =
516 "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
517 "4 unwind Raw Data: 0x13 0x23\n";
518 ASSERT_EQ(expected, GetFakeLogPrint());
519 ASSERT_EQ("", GetFakeLogBuf());
520
521 ResetLogs();
522 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
523
524 expected =
525 "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
526 "4 unwind Raw Data: 0x13 0x23\n";
527 ASSERT_EQ(expected, GetFakeLogPrint());
528 ASSERT_EQ("", GetFakeLogBuf());
529
530 // Negative offset.
531 ResetLogs();
532 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x13, 0xf6, 0x7f});
533
534 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
535
536 expected =
537 "4 unwind DW_CFA_def_cfa_offset_sf -10\n"
538 "4 unwind Raw Data: 0x13 0xf6 0x7f\n";
539 ASSERT_EQ(expected, GetFakeLogPrint());
540 ASSERT_EQ("", GetFakeLogBuf());
541 }
542
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_expression)543 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_expression) {
544 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0f, 0x04, 0x01, 0x02, 0x04, 0x05});
545
546 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x106));
547
548 std::string expected =
549 "4 unwind DW_CFA_def_cfa_expression 4\n"
550 "4 unwind Raw Data: 0x0f 0x04 0x01 0x02 0x04 0x05\n"
551 "4 unwind Illegal\n"
552 "4 unwind Raw Data: 0x01\n"
553 "4 unwind Illegal\n"
554 "4 unwind Raw Data: 0x02\n"
555 "4 unwind Illegal\n"
556 "4 unwind Raw Data: 0x04\n"
557 "4 unwind Illegal\n"
558 "4 unwind Raw Data: 0x05\n";
559 ASSERT_EQ(expected, GetFakeLogPrint());
560 ASSERT_EQ("", GetFakeLogBuf());
561
562 ResetLogs();
563 std::vector<uint8_t> ops{0x0f, 0x81, 0x01};
564 expected = "4 unwind Raw Data: 0x0f 0x81 0x01";
565 std::string op_string;
566 for (uint8_t i = 3; i < 132; i++) {
567 ops.push_back(0x05);
568 op_string +=
569 "4 unwind Illegal\n"
570 "4 unwind Raw Data: 0x05\n";
571 expected += " 0x05";
572 if (((i + 1) % 10) == 0) {
573 expected += "\n4 unwind Raw Data:";
574 }
575 }
576 expected += '\n';
577 this->memory_.SetMemory(0x200, ops);
578 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x284));
579
580 expected = "4 unwind DW_CFA_def_cfa_expression 129\n" + expected;
581 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
582 ASSERT_EQ("", GetFakeLogBuf());
583 }
584
TYPED_TEST_P(DwarfCfaLogTest,cfa_expression)585 TYPED_TEST_P(DwarfCfaLogTest, cfa_expression) {
586 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x10, 0x04, 0x02, 0xc0, 0xc1});
587
588 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
589
590 std::string expected =
591 "4 unwind DW_CFA_expression register(4) 2\n"
592 "4 unwind Raw Data: 0x10 0x04 0x02 0xc0 0xc1\n"
593 "4 unwind Illegal\n"
594 "4 unwind Raw Data: 0xc0\n"
595 "4 unwind Illegal\n"
596 "4 unwind Raw Data: 0xc1\n";
597 ASSERT_EQ(expected, GetFakeLogPrint());
598 ASSERT_EQ("", GetFakeLogBuf());
599
600 ResetLogs();
601 std::vector<uint8_t> ops{0x10, 0xff, 0x01, 0x82, 0x01};
602 expected = "4 unwind Raw Data: 0x10 0xff 0x01 0x82 0x01";
603 std::string op_string;
604 for (uint8_t i = 5; i < 135; i++) {
605 ops.push_back(0xa0 + (i - 5) % 96);
606 op_string += "4 unwind Illegal\n";
607 op_string += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", ops.back());
608 expected += android::base::StringPrintf(" 0x%02x", ops.back());
609 if (((i + 1) % 10) == 0) {
610 expected += "\n4 unwind Raw Data:";
611 }
612 }
613 expected = "4 unwind DW_CFA_expression register(255) 130\n" + expected + "\n";
614
615 this->memory_.SetMemory(0x200, ops);
616 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x287));
617
618 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
619 ASSERT_EQ("", GetFakeLogBuf());
620 }
621
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_offset)622 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset) {
623 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x14, 0x45, 0x54});
624
625 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
626
627 std::string expected =
628 "4 unwind DW_CFA_val_offset register(69) 84\n"
629 "4 unwind Raw Data: 0x14 0x45 0x54\n";
630 ASSERT_EQ(expected, GetFakeLogPrint());
631 ASSERT_EQ("", GetFakeLogBuf());
632
633 ResetLogs();
634 this->memory_.SetMemory(0x400, std::vector<uint8_t>{0x14, 0xa2, 0x02, 0xb4, 0x05});
635
636 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x400, 0x405));
637
638 expected =
639 "4 unwind DW_CFA_val_offset register(290) 692\n"
640 "4 unwind Raw Data: 0x14 0xa2 0x02 0xb4 0x05\n";
641 ASSERT_EQ(expected, GetFakeLogPrint());
642 ASSERT_EQ("", GetFakeLogBuf());
643 }
644
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_offset_sf)645 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset_sf) {
646 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x15, 0x56, 0x12});
647
648 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
649
650 std::string expected =
651 "4 unwind DW_CFA_val_offset_sf register(86) 18\n"
652 "4 unwind Raw Data: 0x15 0x56 0x12\n";
653 ASSERT_EQ(expected, GetFakeLogPrint());
654 ASSERT_EQ("", GetFakeLogBuf());
655
656 // Negative value.
657 ResetLogs();
658 this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x15, 0xff, 0x01, 0xc0, 0x7f});
659
660 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa05));
661
662 expected =
663 "4 unwind DW_CFA_val_offset_sf register(255) -64\n"
664 "4 unwind Raw Data: 0x15 0xff 0x01 0xc0 0x7f\n";
665 ASSERT_EQ(expected, GetFakeLogPrint());
666 ASSERT_EQ("", GetFakeLogBuf());
667 }
668
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_expression)669 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_expression) {
670 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x16, 0x05, 0x02, 0xb0, 0xb1});
671
672 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
673
674 std::string expected =
675 "4 unwind DW_CFA_val_expression register(5) 2\n"
676 "4 unwind Raw Data: 0x16 0x05 0x02 0xb0 0xb1\n"
677 "4 unwind Illegal\n"
678 "4 unwind Raw Data: 0xb0\n"
679 "4 unwind Illegal\n"
680 "4 unwind Raw Data: 0xb1\n";
681 ASSERT_EQ(expected, GetFakeLogPrint());
682 ASSERT_EQ("", GetFakeLogBuf());
683
684 ResetLogs();
685 std::vector<uint8_t> ops{0x16, 0x83, 0x10, 0xa8, 0x01};
686 expected = "4 unwind Raw Data: 0x16 0x83 0x10 0xa8 0x01";
687 std::string op_string;
688 for (uint8_t i = 0; i < 168; i++) {
689 ops.push_back(0xa0 + (i % 96));
690 op_string += "4 unwind Illegal\n";
691 op_string += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", ops.back());
692 expected += android::base::StringPrintf(" 0x%02x", ops.back());
693 if (((i + 6) % 10) == 0) {
694 expected += "\n4 unwind Raw Data:";
695 }
696 }
697 expected = "4 unwind DW_CFA_val_expression register(2051) 168\n" + expected + "\n";
698
699 this->memory_.SetMemory(0xa00, ops);
700
701 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xaad));
702
703 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
704 ASSERT_EQ("", GetFakeLogBuf());
705 }
706
TYPED_TEST_P(DwarfCfaLogTest,cfa_gnu_args_size)707 TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_args_size) {
708 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x2e, 0x04});
709
710 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
711
712 std::string expected =
713 "4 unwind DW_CFA_GNU_args_size 4\n"
714 "4 unwind Raw Data: 0x2e 0x04\n";
715 ASSERT_EQ(expected, GetFakeLogPrint());
716 ASSERT_EQ("", GetFakeLogBuf());
717
718 ResetLogs();
719 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x2e, 0xa4, 0x80, 0x04});
720
721 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5004));
722
723 expected =
724 "4 unwind DW_CFA_GNU_args_size 65572\n"
725 "4 unwind Raw Data: 0x2e 0xa4 0x80 0x04\n";
726 ASSERT_EQ(expected, GetFakeLogPrint());
727 ASSERT_EQ("", GetFakeLogBuf());
728 }
729
TYPED_TEST_P(DwarfCfaLogTest,cfa_gnu_negative_offset_extended)730 TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_negative_offset_extended) {
731 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x2f, 0x08, 0x10});
732
733 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
734
735 std::string expected =
736 "4 unwind DW_CFA_GNU_negative_offset_extended register(8) 16\n"
737 "4 unwind Raw Data: 0x2f 0x08 0x10\n";
738 ASSERT_EQ(expected, GetFakeLogPrint());
739 ASSERT_EQ("", GetFakeLogBuf());
740
741 ResetLogs();
742 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x2f, 0x81, 0x02, 0xff, 0x01});
743
744 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
745
746 expected =
747 "4 unwind DW_CFA_GNU_negative_offset_extended register(257) 255\n"
748 "4 unwind Raw Data: 0x2f 0x81 0x02 0xff 0x01\n";
749 ASSERT_EQ(expected, GetFakeLogPrint());
750 ASSERT_EQ("", GetFakeLogBuf());
751 }
752
TYPED_TEST_P(DwarfCfaLogTest,cfa_register_override)753 TYPED_TEST_P(DwarfCfaLogTest, cfa_register_override) {
754 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01, 0x09, 0x02, 0x04});
755
756 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x306));
757
758 std::string expected =
759 "4 unwind DW_CFA_register register(2) register(1)\n"
760 "4 unwind Raw Data: 0x09 0x02 0x01\n"
761 "4 unwind DW_CFA_register register(2) register(4)\n"
762 "4 unwind Raw Data: 0x09 0x02 0x04\n";
763 ASSERT_EQ(expected, GetFakeLogPrint());
764 ASSERT_EQ("", GetFakeLogBuf());
765 }
766
TYPED_TEST_P(DwarfCfaLogTest,cfa_aarch64_negate_ra_state)767 TYPED_TEST_P(DwarfCfaLogTest, cfa_aarch64_negate_ra_state) {
768 // Verify that if the cfa op is handled properly depending on aarch.
769 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x2d});
770
771 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
772 std::string expected = "4 unwind Illegal (Only valid on aarch64)\n";
773 expected += "4 unwind Raw Data: 0x2d\n";
774 ASSERT_EQ(expected, GetFakeLogPrint());
775 ASSERT_EQ("", GetFakeLogBuf());
776
777 ResetLogs();
778 this->cfa_.reset(new DwarfCfa<TypeParam>(this->dmem_.get(), &this->fde_, ARCH_ARM64));
779
780 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
781 expected = "4 unwind DW_CFA_AARCH64_negate_ra_state\n";
782 expected += "4 unwind Raw Data: 0x2d\n";
783 ASSERT_EQ(expected, GetFakeLogPrint());
784 ASSERT_EQ("", GetFakeLogBuf());
785 }
786
787 REGISTER_TYPED_TEST_SUITE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
788 cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
789 cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
790 cfa_undefined, cfa_same, cfa_register, cfa_state,
791 cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
792 cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
793 cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
794 cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
795 cfa_gnu_negative_offset_extended, cfa_register_override,
796 cfa_aarch64_negate_ra_state);
797
798 typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaLogTestTypes;
799 INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfCfaLogTest, DwarfCfaLogTestTypes);
800
801 } // namespace unwindstack
802