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 "utils/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     fake_memory_ = new MemoryFake;
45     std::shared_ptr<Memory> memory(fake_memory_);
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* fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_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->fake_memory_->SetMemory(0x5000,
201                                 std::vector<uint8_t>{0x05, 0x82, 0x02, 0x04, 0x06, 0x82, 0x02});
202 
203   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5007));
204   expected =
205       "4 unwind DW_CFA_offset_extended register(258) 4\n"
206       "4 unwind Raw Data: 0x05 0x82 0x02 0x04\n"
207       "4 unwind DW_CFA_restore_extended register(258)\n"
208       "4 unwind Raw Data: 0x06 0x82 0x02\n";
209   ASSERT_EQ(expected, GetFakeLogPrint());
210   ASSERT_EQ("", GetFakeLogBuf());
211 }
212 
TYPED_TEST_P(DwarfCfaLogTest,cfa_set_loc)213 TYPED_TEST_P(DwarfCfaLogTest, cfa_set_loc) {
214   uint8_t buffer[1 + sizeof(TypeParam)];
215   buffer[0] = 0x1;
216   TypeParam address;
217   std::string raw_data("Raw Data: 0x01 ");
218   std::string address_str;
219   if (std::is_same<TypeParam, uint32_t>::value) {
220     address = 0x81234578U;
221     address_str = "0x81234578";
222     raw_data += "0x78 0x45 0x23 0x81";
223   } else {
224     address = 0x8123456712345678ULL;
225     address_str = "0x8123456712345678";
226     raw_data += "0x78 0x56 0x34 0x12 0x67 0x45 0x23 0x81";
227   }
228   memcpy(&buffer[1], &address, sizeof(address));
229 
230   this->fake_memory_->SetMemory(0x50, buffer, sizeof(buffer));
231   ResetLogs();
232 
233   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
234   std::string expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
235   expected += "4 unwind " + raw_data + "\n";
236   expected += "4 unwind \n";
237   expected += "4 unwind PC " + address_str + "\n";
238   ASSERT_EQ(expected, GetFakeLogPrint());
239   ASSERT_EQ("", GetFakeLogBuf());
240 
241   // Check for a set going back.
242   ResetLogs();
243   this->fde_.pc_start = address + 0x10;
244 
245   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
246   expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
247   expected += "4 unwind " + raw_data + "\n";
248   expected += "4 unwind \n";
249   expected += "4 unwind PC " + address_str + "\n";
250   ASSERT_EQ(expected, GetFakeLogPrint());
251   ASSERT_EQ("", GetFakeLogBuf());
252 }
253 
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc)254 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc) {
255   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x44});
256 
257   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x201));
258   std::string expected =
259       "4 unwind DW_CFA_advance_loc 4\n"
260       "4 unwind Raw Data: 0x44\n"
261       "4 unwind \n"
262       "4 unwind PC 0x2010\n";
263   ASSERT_EQ(expected, GetFakeLogPrint());
264   ASSERT_EQ("", GetFakeLogBuf());
265 }
266 
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc1)267 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc1) {
268   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x02, 0x04});
269 
270   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x202));
271   std::string expected =
272       "4 unwind DW_CFA_advance_loc1 4\n"
273       "4 unwind Raw Data: 0x02 0x04\n"
274       "4 unwind \n"
275       "4 unwind PC 0x2004\n";
276   ASSERT_EQ(expected, GetFakeLogPrint());
277   ASSERT_EQ("", GetFakeLogBuf());
278 }
279 
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc2)280 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc2) {
281   this->fake_memory_->SetMemory(0x600, std::vector<uint8_t>{0x03, 0x04, 0x03});
282 
283   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x600, 0x603));
284   std::string expected =
285       "4 unwind DW_CFA_advance_loc2 772\n"
286       "4 unwind Raw Data: 0x03 0x04 0x03\n"
287       "4 unwind \n"
288       "4 unwind PC 0x2304\n";
289   ASSERT_EQ(expected, GetFakeLogPrint());
290   ASSERT_EQ("", GetFakeLogBuf());
291 }
292 
TYPED_TEST_P(DwarfCfaLogTest,cfa_advance_loc4)293 TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc4) {
294   this->fake_memory_->SetMemory(0x500, std::vector<uint8_t>{0x04, 0x04, 0x03, 0x02, 0x01});
295 
296   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x505));
297   std::string expected =
298       "4 unwind DW_CFA_advance_loc4 16909060\n"
299       "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
300       "4 unwind \n"
301       "4 unwind PC 0x1022304\n";
302   ASSERT_EQ(expected, GetFakeLogPrint());
303   ASSERT_EQ("", GetFakeLogBuf());
304 }
305 
TYPED_TEST_P(DwarfCfaLogTest,cfa_undefined)306 TYPED_TEST_P(DwarfCfaLogTest, cfa_undefined) {
307   this->fake_memory_->SetMemory(0xa00, std::vector<uint8_t>{0x07, 0x09});
308 
309   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa02));
310   std::string expected =
311       "4 unwind DW_CFA_undefined register(9)\n"
312       "4 unwind Raw Data: 0x07 0x09\n";
313   ASSERT_EQ(expected, GetFakeLogPrint());
314   ASSERT_EQ("", GetFakeLogBuf());
315 
316   ResetLogs();
317   DwarfLocations cie_loc_regs;
318   this->fake_memory_->SetMemory(0x1a00, std::vector<uint8_t>{0x07, 0x81, 0x01});
319 
320   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1a00, 0x1a03));
321   expected =
322       "4 unwind DW_CFA_undefined register(129)\n"
323       "4 unwind Raw Data: 0x07 0x81 0x01\n";
324   ASSERT_EQ(expected, GetFakeLogPrint());
325   ASSERT_EQ("", GetFakeLogBuf());
326 }
327 
TYPED_TEST_P(DwarfCfaLogTest,cfa_same)328 TYPED_TEST_P(DwarfCfaLogTest, cfa_same) {
329   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x08, 0x7f});
330 
331   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
332   std::string expected =
333       "4 unwind DW_CFA_same_value register(127)\n"
334       "4 unwind Raw Data: 0x08 0x7f\n";
335   ASSERT_EQ(expected, GetFakeLogPrint());
336   ASSERT_EQ("", GetFakeLogBuf());
337 
338   ResetLogs();
339   this->fake_memory_->SetMemory(0x2100, std::vector<uint8_t>{0x08, 0xff, 0x01});
340 
341   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
342   expected =
343       "4 unwind DW_CFA_same_value register(255)\n"
344       "4 unwind Raw Data: 0x08 0xff 0x01\n";
345   ASSERT_EQ(expected, GetFakeLogPrint());
346   ASSERT_EQ("", GetFakeLogBuf());
347 }
348 
TYPED_TEST_P(DwarfCfaLogTest,cfa_register)349 TYPED_TEST_P(DwarfCfaLogTest, cfa_register) {
350   this->fake_memory_->SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01});
351 
352   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x303));
353   std::string expected =
354       "4 unwind DW_CFA_register register(2) register(1)\n"
355       "4 unwind Raw Data: 0x09 0x02 0x01\n";
356   ASSERT_EQ(expected, GetFakeLogPrint());
357   ASSERT_EQ("", GetFakeLogBuf());
358 
359   ResetLogs();
360   this->fake_memory_->SetMemory(0x4300, std::vector<uint8_t>{0x09, 0xff, 0x01, 0xff, 0x03});
361 
362   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4305));
363   expected =
364       "4 unwind DW_CFA_register register(255) register(511)\n"
365       "4 unwind Raw Data: 0x09 0xff 0x01 0xff 0x03\n";
366   ASSERT_EQ(expected, GetFakeLogPrint());
367   ASSERT_EQ("", GetFakeLogBuf());
368 }
369 
TYPED_TEST_P(DwarfCfaLogTest,cfa_state)370 TYPED_TEST_P(DwarfCfaLogTest, cfa_state) {
371   this->fake_memory_->SetMemory(0x300, std::vector<uint8_t>{0x0a});
372 
373   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x301));
374 
375   std::string expected =
376       "4 unwind DW_CFA_remember_state\n"
377       "4 unwind Raw Data: 0x0a\n";
378   ASSERT_EQ(expected, GetFakeLogPrint());
379   ASSERT_EQ("", GetFakeLogBuf());
380 
381   ResetLogs();
382   this->fake_memory_->SetMemory(0x4300, std::vector<uint8_t>{0x0b});
383 
384   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4301));
385 
386   expected =
387       "4 unwind DW_CFA_restore_state\n"
388       "4 unwind Raw Data: 0x0b\n";
389   ASSERT_EQ(expected, GetFakeLogPrint());
390   ASSERT_EQ("", GetFakeLogBuf());
391 }
392 
TYPED_TEST_P(DwarfCfaLogTest,cfa_state_cfa_offset_restore)393 TYPED_TEST_P(DwarfCfaLogTest, cfa_state_cfa_offset_restore) {
394   this->fake_memory_->SetMemory(0x3000, std::vector<uint8_t>{0x0a, 0x0e, 0x40, 0x0b});
395 
396   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3004));
397 
398   std::string expected =
399       "4 unwind DW_CFA_remember_state\n"
400       "4 unwind Raw Data: 0x0a\n"
401       "4 unwind DW_CFA_def_cfa_offset 64\n"
402       "4 unwind Raw Data: 0x0e 0x40\n"
403       "4 unwind DW_CFA_restore_state\n"
404       "4 unwind Raw Data: 0x0b\n";
405   ASSERT_EQ(expected, GetFakeLogPrint());
406   ASSERT_EQ("", GetFakeLogBuf());
407 }
408 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa)409 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa) {
410   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x0c, 0x7f, 0x74});
411 
412   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
413 
414   std::string expected =
415       "4 unwind DW_CFA_def_cfa register(127) 116\n"
416       "4 unwind Raw Data: 0x0c 0x7f 0x74\n";
417   ASSERT_EQ(expected, GetFakeLogPrint());
418   ASSERT_EQ("", GetFakeLogBuf());
419 
420   ResetLogs();
421   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x0c, 0xff, 0x02, 0xf4, 0x04});
422 
423   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
424 
425   expected =
426       "4 unwind DW_CFA_def_cfa register(383) 628\n"
427       "4 unwind Raw Data: 0x0c 0xff 0x02 0xf4 0x04\n";
428   ASSERT_EQ(expected, GetFakeLogPrint());
429   ASSERT_EQ("", GetFakeLogBuf());
430 }
431 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_sf)432 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_sf) {
433   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x12, 0x30, 0x25});
434 
435   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
436 
437   std::string expected =
438       "4 unwind DW_CFA_def_cfa_sf register(48) 37\n"
439       "4 unwind Raw Data: 0x12 0x30 0x25\n";
440   ASSERT_EQ(expected, GetFakeLogPrint());
441   ASSERT_EQ("", GetFakeLogBuf());
442 
443   // Test a negative value.
444   ResetLogs();
445   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x12, 0xa3, 0x01, 0xfa, 0x7f});
446 
447   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
448 
449   expected =
450       "4 unwind DW_CFA_def_cfa_sf register(163) -6\n"
451       "4 unwind Raw Data: 0x12 0xa3 0x01 0xfa 0x7f\n";
452   ASSERT_EQ(expected, GetFakeLogPrint());
453   ASSERT_EQ("", GetFakeLogBuf());
454 }
455 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_register)456 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_register) {
457   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x0d, 0x72});
458 
459   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
460 
461   std::string expected =
462       "4 unwind DW_CFA_def_cfa_register register(114)\n"
463       "4 unwind Raw Data: 0x0d 0x72\n";
464   ASSERT_EQ(expected, GetFakeLogPrint());
465   ASSERT_EQ("", GetFakeLogBuf());
466 
467   ResetLogs();
468   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x0d, 0xf9, 0x20});
469 
470   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
471 
472   expected =
473       "4 unwind DW_CFA_def_cfa_register register(4217)\n"
474       "4 unwind Raw Data: 0x0d 0xf9 0x20\n";
475   ASSERT_EQ(expected, GetFakeLogPrint());
476   ASSERT_EQ("", GetFakeLogBuf());
477 }
478 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_offset)479 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset) {
480   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x0e, 0x59});
481 
482   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
483 
484   std::string expected =
485       "4 unwind DW_CFA_def_cfa_offset 89\n"
486       "4 unwind Raw Data: 0x0e 0x59\n";
487   ASSERT_EQ(expected, GetFakeLogPrint());
488   ASSERT_EQ("", GetFakeLogBuf());
489 
490   ResetLogs();
491   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
492 
493   expected =
494       "4 unwind DW_CFA_def_cfa_offset 89\n"
495       "4 unwind Raw Data: 0x0e 0x59\n";
496   ASSERT_EQ(expected, GetFakeLogPrint());
497   ASSERT_EQ("", GetFakeLogBuf());
498 
499   ResetLogs();
500   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x0e, 0xd4, 0x0a});
501 
502   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
503 
504   expected =
505       "4 unwind DW_CFA_def_cfa_offset 1364\n"
506       "4 unwind Raw Data: 0x0e 0xd4 0x0a\n";
507   ASSERT_EQ(expected, GetFakeLogPrint());
508   ASSERT_EQ("", GetFakeLogBuf());
509 }
510 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_offset_sf)511 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset_sf) {
512   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x13, 0x23});
513 
514   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
515 
516   std::string expected =
517       "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
518       "4 unwind Raw Data: 0x13 0x23\n";
519   ASSERT_EQ(expected, GetFakeLogPrint());
520   ASSERT_EQ("", GetFakeLogBuf());
521 
522   ResetLogs();
523   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
524 
525   expected =
526       "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
527       "4 unwind Raw Data: 0x13 0x23\n";
528   ASSERT_EQ(expected, GetFakeLogPrint());
529   ASSERT_EQ("", GetFakeLogBuf());
530 
531   // Negative offset.
532   ResetLogs();
533   this->fake_memory_->SetMemory(0x200, std::vector<uint8_t>{0x13, 0xf6, 0x7f});
534 
535   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
536 
537   expected =
538       "4 unwind DW_CFA_def_cfa_offset_sf -10\n"
539       "4 unwind Raw Data: 0x13 0xf6 0x7f\n";
540   ASSERT_EQ(expected, GetFakeLogPrint());
541   ASSERT_EQ("", GetFakeLogBuf());
542 }
543 
TYPED_TEST_P(DwarfCfaLogTest,cfa_def_cfa_expression)544 TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_expression) {
545   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x0f, 0x04, 0x01, 0x02, 0x04, 0x05});
546 
547   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x106));
548 
549   std::string expected =
550       "4 unwind DW_CFA_def_cfa_expression 4\n"
551       "4 unwind Raw Data: 0x0f 0x04 0x01 0x02 0x04 0x05\n"
552       "4 unwind   Illegal\n"
553       "4 unwind   Raw Data: 0x01\n"
554       "4 unwind   Illegal\n"
555       "4 unwind   Raw Data: 0x02\n"
556       "4 unwind   Illegal\n"
557       "4 unwind   Raw Data: 0x04\n"
558       "4 unwind   Illegal\n"
559       "4 unwind   Raw Data: 0x05\n";
560   ASSERT_EQ(expected, GetFakeLogPrint());
561   ASSERT_EQ("", GetFakeLogBuf());
562 
563   ResetLogs();
564   std::vector<uint8_t> ops{0x0f, 0x81, 0x01};
565   expected = "4 unwind Raw Data: 0x0f 0x81 0x01";
566   std::string op_string;
567   for (uint8_t i = 3; i < 132; i++) {
568     ops.push_back(0x05);
569     op_string +=
570         "4 unwind   Illegal\n"
571         "4 unwind   Raw Data: 0x05\n";
572     expected += " 0x05";
573     if (((i + 1) % 10) == 0) {
574       expected += "\n4 unwind Raw Data:";
575     }
576   }
577   expected += '\n';
578   this->fake_memory_->SetMemory(0x200, ops);
579   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x284));
580 
581   expected = "4 unwind DW_CFA_def_cfa_expression 129\n" + expected;
582   ASSERT_EQ(expected + op_string, GetFakeLogPrint());
583   ASSERT_EQ("", GetFakeLogBuf());
584 }
585 
TYPED_TEST_P(DwarfCfaLogTest,cfa_expression)586 TYPED_TEST_P(DwarfCfaLogTest, cfa_expression) {
587   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x10, 0x04, 0x02, 0xc0, 0xc1});
588 
589   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
590 
591   std::string expected =
592       "4 unwind DW_CFA_expression register(4) 2\n"
593       "4 unwind Raw Data: 0x10 0x04 0x02 0xc0 0xc1\n"
594       "4 unwind   Illegal\n"
595       "4 unwind   Raw Data: 0xc0\n"
596       "4 unwind   Illegal\n"
597       "4 unwind   Raw Data: 0xc1\n";
598   ASSERT_EQ(expected, GetFakeLogPrint());
599   ASSERT_EQ("", GetFakeLogBuf());
600 
601   ResetLogs();
602   std::vector<uint8_t> ops{0x10, 0xff, 0x01, 0x82, 0x01};
603   expected = "4 unwind Raw Data: 0x10 0xff 0x01 0x82 0x01";
604   std::string op_string;
605   for (uint8_t i = 5; i < 135; i++) {
606     ops.push_back(0xa0 + (i - 5) % 96);
607     op_string += "4 unwind   Illegal\n";
608     op_string += android::base::StringPrintf("4 unwind   Raw Data: 0x%02x\n", ops.back());
609     expected += android::base::StringPrintf(" 0x%02x", ops.back());
610     if (((i + 1) % 10) == 0) {
611       expected += "\n4 unwind Raw Data:";
612     }
613   }
614   expected = "4 unwind DW_CFA_expression register(255) 130\n" + expected + "\n";
615 
616   this->fake_memory_->SetMemory(0x200, ops);
617   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x287));
618 
619   ASSERT_EQ(expected + op_string, GetFakeLogPrint());
620   ASSERT_EQ("", GetFakeLogBuf());
621 }
622 
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_offset)623 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset) {
624   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x14, 0x45, 0x54});
625 
626   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
627 
628   std::string expected =
629       "4 unwind DW_CFA_val_offset register(69) 84\n"
630       "4 unwind Raw Data: 0x14 0x45 0x54\n";
631   ASSERT_EQ(expected, GetFakeLogPrint());
632   ASSERT_EQ("", GetFakeLogBuf());
633 
634   ResetLogs();
635   this->fake_memory_->SetMemory(0x400, std::vector<uint8_t>{0x14, 0xa2, 0x02, 0xb4, 0x05});
636 
637   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x400, 0x405));
638 
639   expected =
640       "4 unwind DW_CFA_val_offset register(290) 692\n"
641       "4 unwind Raw Data: 0x14 0xa2 0x02 0xb4 0x05\n";
642   ASSERT_EQ(expected, GetFakeLogPrint());
643   ASSERT_EQ("", GetFakeLogBuf());
644 }
645 
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_offset_sf)646 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset_sf) {
647   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x15, 0x56, 0x12});
648 
649   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
650 
651   std::string expected =
652       "4 unwind DW_CFA_val_offset_sf register(86) 18\n"
653       "4 unwind Raw Data: 0x15 0x56 0x12\n";
654   ASSERT_EQ(expected, GetFakeLogPrint());
655   ASSERT_EQ("", GetFakeLogBuf());
656 
657   // Negative value.
658   ResetLogs();
659   this->fake_memory_->SetMemory(0xa00, std::vector<uint8_t>{0x15, 0xff, 0x01, 0xc0, 0x7f});
660 
661   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa05));
662 
663   expected =
664       "4 unwind DW_CFA_val_offset_sf register(255) -64\n"
665       "4 unwind Raw Data: 0x15 0xff 0x01 0xc0 0x7f\n";
666   ASSERT_EQ(expected, GetFakeLogPrint());
667   ASSERT_EQ("", GetFakeLogBuf());
668 }
669 
TYPED_TEST_P(DwarfCfaLogTest,cfa_val_expression)670 TYPED_TEST_P(DwarfCfaLogTest, cfa_val_expression) {
671   this->fake_memory_->SetMemory(0x100, std::vector<uint8_t>{0x16, 0x05, 0x02, 0xb0, 0xb1});
672 
673   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
674 
675   std::string expected =
676       "4 unwind DW_CFA_val_expression register(5) 2\n"
677       "4 unwind Raw Data: 0x16 0x05 0x02 0xb0 0xb1\n"
678       "4 unwind   Illegal\n"
679       "4 unwind   Raw Data: 0xb0\n"
680       "4 unwind   Illegal\n"
681       "4 unwind   Raw Data: 0xb1\n";
682   ASSERT_EQ(expected, GetFakeLogPrint());
683   ASSERT_EQ("", GetFakeLogBuf());
684 
685   ResetLogs();
686   std::vector<uint8_t> ops{0x16, 0x83, 0x10, 0xa8, 0x01};
687   expected = "4 unwind Raw Data: 0x16 0x83 0x10 0xa8 0x01";
688   std::string op_string;
689   for (uint8_t i = 0; i < 168; i++) {
690     ops.push_back(0xa0 + (i % 96));
691     op_string += "4 unwind   Illegal\n";
692     op_string += android::base::StringPrintf("4 unwind   Raw Data: 0x%02x\n", ops.back());
693     expected += android::base::StringPrintf(" 0x%02x", ops.back());
694     if (((i + 6) % 10) == 0) {
695       expected += "\n4 unwind Raw Data:";
696     }
697   }
698   expected = "4 unwind DW_CFA_val_expression register(2051) 168\n" + expected + "\n";
699 
700   this->fake_memory_->SetMemory(0xa00, ops);
701 
702   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xaad));
703 
704   ASSERT_EQ(expected + op_string, GetFakeLogPrint());
705   ASSERT_EQ("", GetFakeLogBuf());
706 }
707 
TYPED_TEST_P(DwarfCfaLogTest,cfa_gnu_args_size)708 TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_args_size) {
709   this->fake_memory_->SetMemory(0x2000, std::vector<uint8_t>{0x2e, 0x04});
710 
711   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
712 
713   std::string expected =
714       "4 unwind DW_CFA_GNU_args_size 4\n"
715       "4 unwind Raw Data: 0x2e 0x04\n";
716   ASSERT_EQ(expected, GetFakeLogPrint());
717   ASSERT_EQ("", GetFakeLogBuf());
718 
719   ResetLogs();
720   this->fake_memory_->SetMemory(0x5000, std::vector<uint8_t>{0x2e, 0xa4, 0x80, 0x04});
721 
722   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5004));
723 
724   expected =
725       "4 unwind DW_CFA_GNU_args_size 65572\n"
726       "4 unwind Raw Data: 0x2e 0xa4 0x80 0x04\n";
727   ASSERT_EQ(expected, GetFakeLogPrint());
728   ASSERT_EQ("", GetFakeLogBuf());
729 }
730 
TYPED_TEST_P(DwarfCfaLogTest,cfa_gnu_negative_offset_extended)731 TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_negative_offset_extended) {
732   this->fake_memory_->SetMemory(0x500, std::vector<uint8_t>{0x2f, 0x08, 0x10});
733 
734   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
735 
736   std::string expected =
737       "4 unwind DW_CFA_GNU_negative_offset_extended register(8) 16\n"
738       "4 unwind Raw Data: 0x2f 0x08 0x10\n";
739   ASSERT_EQ(expected, GetFakeLogPrint());
740   ASSERT_EQ("", GetFakeLogBuf());
741 
742   ResetLogs();
743   this->fake_memory_->SetMemory(0x1500, std::vector<uint8_t>{0x2f, 0x81, 0x02, 0xff, 0x01});
744 
745   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
746 
747   expected =
748       "4 unwind DW_CFA_GNU_negative_offset_extended register(257) 255\n"
749       "4 unwind Raw Data: 0x2f 0x81 0x02 0xff 0x01\n";
750   ASSERT_EQ(expected, GetFakeLogPrint());
751   ASSERT_EQ("", GetFakeLogBuf());
752 }
753 
TYPED_TEST_P(DwarfCfaLogTest,cfa_register_override)754 TYPED_TEST_P(DwarfCfaLogTest, cfa_register_override) {
755   this->fake_memory_->SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01, 0x09, 0x02, 0x04});
756 
757   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x306));
758 
759   std::string expected =
760       "4 unwind DW_CFA_register register(2) register(1)\n"
761       "4 unwind Raw Data: 0x09 0x02 0x01\n"
762       "4 unwind DW_CFA_register register(2) register(4)\n"
763       "4 unwind Raw Data: 0x09 0x02 0x04\n";
764   ASSERT_EQ(expected, GetFakeLogPrint());
765   ASSERT_EQ("", GetFakeLogBuf());
766 }
767 
TYPED_TEST_P(DwarfCfaLogTest,cfa_aarch64_negate_ra_state)768 TYPED_TEST_P(DwarfCfaLogTest, cfa_aarch64_negate_ra_state) {
769   // Verify that if the cfa op is handled properly depending on aarch.
770   this->fake_memory_->SetMemory(0x2000, std::vector<uint8_t>{0x2d});
771 
772   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
773   std::string expected = "4 unwind Illegal (Only valid on aarch64)\n";
774   expected += "4 unwind Raw Data: 0x2d\n";
775   ASSERT_EQ(expected, GetFakeLogPrint());
776   ASSERT_EQ("", GetFakeLogBuf());
777 
778   ResetLogs();
779   this->cfa_.reset(new DwarfCfa<TypeParam>(this->dmem_.get(), &this->fde_, ARCH_ARM64));
780 
781   ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
782   expected = "4 unwind DW_CFA_AARCH64_negate_ra_state\n";
783   expected += "4 unwind Raw Data: 0x2d\n";
784   ASSERT_EQ(expected, GetFakeLogPrint());
785   ASSERT_EQ("", GetFakeLogBuf());
786 }
787 
788 REGISTER_TYPED_TEST_SUITE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
789                             cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
790                             cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
791                             cfa_undefined, cfa_same, cfa_register, cfa_state,
792                             cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
793                             cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
794                             cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
795                             cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
796                             cfa_gnu_negative_offset_extended, cfa_register_override,
797                             cfa_aarch64_negate_ra_state);
798 
799 typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaLogTestTypes;
800 INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfCfaLogTest, DwarfCfaLogTestTypes);
801 
802 }  // namespace unwindstack
803