summaryrefslogtreecommitdiffstats
path: root/libunwindstack/tests/DwarfCfaLogTest.cpp
blob: b17ca33a890d997406f8d6cd18c107db08e3d303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>

#include <memory>
#include <type_traits>
#include <unordered_map>

#include <android-base/stringprintf.h>
#include <gtest/gtest.h>

#include <unwindstack/DwarfLocation.h>
#include <unwindstack/DwarfMemory.h>
#include <unwindstack/DwarfStructs.h>
#include <unwindstack/Log.h>

#include "DwarfCfa.h"

#include "LogFake.h"
#include "MemoryFake.h"

namespace unwindstack {

template <typename TypeParam>
class DwarfCfaLogTest : public ::testing::Test {
 protected:
  void SetUp() override {
    ResetLogs();
    memory_.Clear();

    dmem_.reset(new DwarfMemory(&memory_));

    cie_.cfa_instructions_offset = 0x1000;
    cie_.cfa_instructions_end = 0x1030;
    // These two values should be different to distinguish between
    // operations that deal with code versus data.
    cie_.code_alignment_factor = 4;
    cie_.data_alignment_factor = 8;

    fde_.cfa_instructions_offset = 0x2000;
    fde_.cfa_instructions_end = 0x2030;
    fde_.pc_start = 0x2000;
    fde_.pc_end = 0x2000;
    fde_.pc_end = 0x10000;
    fde_.cie = &cie_;
    cfa_.reset(new DwarfCfa<TypeParam>(dmem_.get(), &fde_));
  }

  MemoryFake memory_;
  std::unique_ptr<DwarfMemory> dmem_;
  std::unique_ptr<DwarfCfa<TypeParam>> cfa_;
  DwarfCie cie_;
  DwarfFde fde_;
};
TYPED_TEST_CASE_P(DwarfCfaLogTest);

// NOTE: All class variable references have to be prefaced with this->.

TYPED_TEST_P(DwarfCfaLogTest, cfa_illegal) {
  for (uint8_t i = 0x17; i < 0x3f; i++) {
    if (i == 0x2e || i == 0x2f) {
      // Skip gnu extension ops.
      continue;
    }
    this->memory_.SetMemory(0x2000, std::vector<uint8_t>{i});

    ResetLogs();
    ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
    std::string expected = "4 unwind Illegal\n";
    expected += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", i);
    ASSERT_EQ(expected, GetFakeLogPrint());
    ASSERT_EQ("", GetFakeLogBuf());
  }
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_nop) {
  this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x00});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
  std::string expected =
      "4 unwind DW_CFA_nop\n"
      "4 unwind Raw Data: 0x00\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_offset) {
  this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x83, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2002));
  std::string expected =
      "4 unwind DW_CFA_offset register(3) 4\n"
      "4 unwind Raw Data: 0x83 0x04\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x83, 0x84, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2100, 0x2103));
  expected =
      "4 unwind DW_CFA_offset register(3) 132\n"
      "4 unwind Raw Data: 0x83 0x84 0x01\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended) {
  this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x05, 0x03, 0x02});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));
  std::string expected =
      "4 unwind DW_CFA_offset_extended register(3) 2\n"
      "4 unwind Raw Data: 0x05 0x03 0x02\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x05, 0x81, 0x01, 0x82, 0x12});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));
  expected =
      "4 unwind DW_CFA_offset_extended register(129) 2306\n"
      "4 unwind Raw Data: 0x05 0x81 0x01 0x82 0x12\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended_sf) {
  this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x11, 0x05, 0x10});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));
  std::string expected =
      "4 unwind DW_CFA_offset_extended_sf register(5) 16\n"
      "4 unwind Raw Data: 0x11 0x05 0x10\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  // Check a negative value for the offset.
  ResetLogs();
  this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x11, 0x86, 0x01, 0xff, 0x7f});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));
  expected =
      "4 unwind DW_CFA_offset_extended_sf register(134) -1\n"
      "4 unwind Raw Data: 0x11 0x86 0x01 0xff 0x7f\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_restore) {
  this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0xc2});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2001));
  std::string expected =
      "4 unwind DW_CFA_restore register(2)\n"
      "4 unwind Raw Data: 0xc2\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x82, 0x04, 0xc2});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x3000, 0x3003));
  expected =
      "4 unwind DW_CFA_offset register(2) 4\n"
      "4 unwind Raw Data: 0x82 0x04\n"
      "4 unwind DW_CFA_restore register(2)\n"
      "4 unwind Raw Data: 0xc2\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_restore_extended) {
  this->memory_.SetMemory(0x4000, std::vector<uint8_t>{0x06, 0x08});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4000, 0x4002));
  std::string expected =
      "4 unwind DW_CFA_restore_extended register(8)\n"
      "4 unwind Raw Data: 0x06 0x08\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x05, 0x82, 0x02, 0x04, 0x06, 0x82, 0x02});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x5000, 0x5007));
  expected =
      "4 unwind DW_CFA_offset_extended register(258) 4\n"
      "4 unwind Raw Data: 0x05 0x82 0x02 0x04\n"
      "4 unwind DW_CFA_restore_extended register(258)\n"
      "4 unwind Raw Data: 0x06 0x82 0x02\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_set_loc) {
  uint8_t buffer[1 + sizeof(TypeParam)];
  buffer[0] = 0x1;
  TypeParam address;
  std::string raw_data("Raw Data: 0x01 ");
  std::string address_str;
  if (std::is_same<TypeParam, uint32_t>::value) {
    address = 0x81234578U;
    address_str = "0x81234578";
    raw_data += "0x78 0x45 0x23 0x81";
  } else {
    address = 0x8123456712345678ULL;
    address_str = "0x8123456712345678";
    raw_data += "0x78 0x56 0x34 0x12 0x67 0x45 0x23 0x81";
  }
  memcpy(&buffer[1], &address, sizeof(address));

  this->memory_.SetMemory(0x50, buffer, sizeof(buffer));
  ResetLogs();

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x50, 0x51 + sizeof(TypeParam)));
  std::string expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
  expected += "4 unwind " + raw_data + "\n";
  expected += "4 unwind \n";
  expected += "4 unwind PC " + address_str + "\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  // Check for a set going back.
  ResetLogs();
  this->fde_.pc_start = address + 0x10;

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x50, 0x51 + sizeof(TypeParam)));
  expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
  expected += "4 unwind " + raw_data + "\n";
  expected += "4 unwind \n";
  expected += "4 unwind PC " + address_str + "\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc) {
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x44});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x201));
  std::string expected =
      "4 unwind DW_CFA_advance_loc 4\n"
      "4 unwind Raw Data: 0x44\n"
      "4 unwind \n"
      "4 unwind PC 0x2010\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x200, 0x201));
  expected =
      "4 unwind DW_CFA_advance_loc 4\n"
      "4 unwind Raw Data: 0x44\n"
      "4 unwind \n"
      "4 unwind PC 0x2110\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc1) {
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x02, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x202));
  std::string expected =
      "4 unwind DW_CFA_advance_loc1 4\n"
      "4 unwind Raw Data: 0x02 0x04\n"
      "4 unwind \n"
      "4 unwind PC 0x2004\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x10, 0x200, 0x202));
  expected =
      "4 unwind DW_CFA_advance_loc1 4\n"
      "4 unwind Raw Data: 0x02 0x04\n"
      "4 unwind \n"
      "4 unwind PC 0x2014\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc2) {
  this->memory_.SetMemory(0x600, std::vector<uint8_t>{0x03, 0x04, 0x03});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x600, 0x603));
  std::string expected =
      "4 unwind DW_CFA_advance_loc2 772\n"
      "4 unwind Raw Data: 0x03 0x04 0x03\n"
      "4 unwind \n"
      "4 unwind PC 0x2304\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1000, 0x600, 0x603));
  expected =
      "4 unwind DW_CFA_advance_loc2 772\n"
      "4 unwind Raw Data: 0x03 0x04 0x03\n"
      "4 unwind \n"
      "4 unwind PC 0x3304\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc4) {
  this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x04, 0x04, 0x03, 0x02, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x505));
  std::string expected =
      "4 unwind DW_CFA_advance_loc4 16909060\n"
      "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
      "4 unwind \n"
      "4 unwind PC 0x1022304\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x500, 0x505));
  expected =
      "4 unwind DW_CFA_advance_loc4 16909060\n"
      "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
      "4 unwind \n"
      "4 unwind PC 0x1024304\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_undefined) {
  this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x07, 0x09});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xa02));
  std::string expected =
      "4 unwind DW_CFA_undefined register(9)\n"
      "4 unwind Raw Data: 0x07 0x09\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  dwarf_loc_regs_t cie_loc_regs;
  this->memory_.SetMemory(0x1a00, std::vector<uint8_t>{0x07, 0x81, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1a00, 0x1a03));
  expected =
      "4 unwind DW_CFA_undefined register(129)\n"
      "4 unwind Raw Data: 0x07 0x81 0x01\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_same) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x08, 0x7f});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));
  std::string expected =
      "4 unwind DW_CFA_same_value register(127)\n"
      "4 unwind Raw Data: 0x08 0x7f\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x08, 0xff, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2100, 0x2103));
  expected =
      "4 unwind DW_CFA_same_value register(255)\n"
      "4 unwind Raw Data: 0x08 0xff 0x01\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_register) {
  this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x303));
  std::string expected =
      "4 unwind DW_CFA_register register(2) register(1)\n"
      "4 unwind Raw Data: 0x09 0x02 0x01\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x09, 0xff, 0x01, 0xff, 0x03});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4300, 0x4305));
  expected =
      "4 unwind DW_CFA_register register(255) register(511)\n"
      "4 unwind Raw Data: 0x09 0xff 0x01 0xff 0x03\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_state) {
  this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x0a});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x301));

  std::string expected =
      "4 unwind DW_CFA_remember_state\n"
      "4 unwind Raw Data: 0x0a\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x0b});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x4300, 0x4301));

  expected =
      "4 unwind DW_CFA_restore_state\n"
      "4 unwind Raw Data: 0x0b\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_state_cfa_offset_restore) {
  this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x0a, 0x0e, 0x40, 0x0b});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x3000, 0x3004));

  std::string expected =
      "4 unwind DW_CFA_remember_state\n"
      "4 unwind Raw Data: 0x0a\n"
      "4 unwind DW_CFA_def_cfa_offset 64\n"
      "4 unwind Raw Data: 0x0e 0x40\n"
      "4 unwind DW_CFA_restore_state\n"
      "4 unwind Raw Data: 0x0b\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0c, 0x7f, 0x74});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));

  std::string expected =
      "4 unwind DW_CFA_def_cfa register(127) 116\n"
      "4 unwind Raw Data: 0x0c 0x7f 0x74\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0c, 0xff, 0x02, 0xf4, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x205));

  expected =
      "4 unwind DW_CFA_def_cfa register(383) 628\n"
      "4 unwind Raw Data: 0x0c 0xff 0x02 0xf4 0x04\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_sf) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x12, 0x30, 0x25});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));

  std::string expected =
      "4 unwind DW_CFA_def_cfa_sf register(48) 37\n"
      "4 unwind Raw Data: 0x12 0x30 0x25\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  // Test a negative value.
  ResetLogs();
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x12, 0xa3, 0x01, 0xfa, 0x7f});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x205));

  expected =
      "4 unwind DW_CFA_def_cfa_sf register(163) -6\n"
      "4 unwind Raw Data: 0x12 0xa3 0x01 0xfa 0x7f\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_register) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0d, 0x72});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));

  std::string expected =
      "4 unwind DW_CFA_def_cfa_register register(114)\n"
      "4 unwind Raw Data: 0x0d 0x72\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0d, 0xf9, 0x20});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));

  expected =
      "4 unwind DW_CFA_def_cfa_register register(4217)\n"
      "4 unwind Raw Data: 0x0d 0xf9 0x20\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0e, 0x59});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));

  std::string expected =
      "4 unwind DW_CFA_def_cfa_offset 89\n"
      "4 unwind Raw Data: 0x0e 0x59\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));

  expected =
      "4 unwind DW_CFA_def_cfa_offset 89\n"
      "4 unwind Raw Data: 0x0e 0x59\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0e, 0xd4, 0x0a});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));

  expected =
      "4 unwind DW_CFA_def_cfa_offset 1364\n"
      "4 unwind Raw Data: 0x0e 0xd4 0x0a\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset_sf) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x13, 0x23});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));

  std::string expected =
      "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
      "4 unwind Raw Data: 0x13 0x23\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x102));

  expected =
      "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
      "4 unwind Raw Data: 0x13 0x23\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  // Negative offset.
  ResetLogs();
  this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x13, 0xf6, 0x7f});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x203));

  expected =
      "4 unwind DW_CFA_def_cfa_offset_sf -10\n"
      "4 unwind Raw Data: 0x13 0xf6 0x7f\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_expression) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0f, 0x04, 0x01, 0x02, 0x04, 0x05});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x106));

  std::string expected =
      "4 unwind DW_CFA_def_cfa_expression 4\n"
      "4 unwind Raw Data: 0x0f 0x04 0x01 0x02 0x04 0x05\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0x01\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0x02\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0x04\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0x05\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  std::vector<uint8_t> ops{0x0f, 0x81, 0x01};
  expected = "4 unwind Raw Data: 0x0f 0x81 0x01";
  std::string op_string;
  for (uint8_t i = 3; i < 132; i++) {
    ops.push_back(0x05);
    op_string +=
        "4 unwind   Illegal\n"
        "4 unwind   Raw Data: 0x05\n";
    expected += " 0x05";
    if (((i + 1) % 10) == 0) {
      expected += "\n4 unwind Raw Data:";
    }
  }
  expected += '\n';
  this->memory_.SetMemory(0x200, ops);
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x284));

  expected = "4 unwind DW_CFA_def_cfa_expression 129\n" + expected;
  ASSERT_EQ(expected + op_string, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_expression) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x10, 0x04, 0x02, 0xc0, 0xc1});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x105));

  std::string expected =
      "4 unwind DW_CFA_expression register(4) 2\n"
      "4 unwind Raw Data: 0x10 0x04 0x02 0xc0 0xc1\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0xc0\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0xc1\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  std::vector<uint8_t> ops{0x10, 0xff, 0x01, 0x82, 0x01};
  expected = "4 unwind Raw Data: 0x10 0xff 0x01 0x82 0x01";
  std::string op_string;
  for (uint8_t i = 5; i < 135; i++) {
    ops.push_back(0xa0 + (i - 5) % 96);
    op_string += "4 unwind   Illegal\n";
    op_string += android::base::StringPrintf("4 unwind   Raw Data: 0x%02x\n", ops.back());
    expected += android::base::StringPrintf(" 0x%02x", ops.back());
    if (((i + 1) % 10) == 0) {
      expected += "\n4 unwind Raw Data:";
    }
  }
  expected = "4 unwind DW_CFA_expression register(255) 130\n" + expected + "\n";

  this->memory_.SetMemory(0x200, ops);
  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x200, 0x287));

  ASSERT_EQ(expected + op_string, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x14, 0x45, 0x54});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));

  std::string expected =
      "4 unwind DW_CFA_val_offset register(69) 84\n"
      "4 unwind Raw Data: 0x14 0x45 0x54\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x400, std::vector<uint8_t>{0x14, 0xa2, 0x02, 0xb4, 0x05});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x400, 0x405));

  expected =
      "4 unwind DW_CFA_val_offset register(290) 692\n"
      "4 unwind Raw Data: 0x14 0xa2 0x02 0xb4 0x05\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset_sf) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x15, 0x56, 0x12});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x103));

  std::string expected =
      "4 unwind DW_CFA_val_offset_sf register(86) 18\n"
      "4 unwind Raw Data: 0x15 0x56 0x12\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  // Negative value.
  ResetLogs();
  this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x15, 0xff, 0x01, 0xc0, 0x7f});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xa05));

  expected =
      "4 unwind DW_CFA_val_offset_sf register(255) -64\n"
      "4 unwind Raw Data: 0x15 0xff 0x01 0xc0 0x7f\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_val_expression) {
  this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x16, 0x05, 0x02, 0xb0, 0xb1});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x100, 0x105));

  std::string expected =
      "4 unwind DW_CFA_val_expression register(5) 2\n"
      "4 unwind Raw Data: 0x16 0x05 0x02 0xb0 0xb1\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0xb0\n"
      "4 unwind   Illegal\n"
      "4 unwind   Raw Data: 0xb1\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  std::vector<uint8_t> ops{0x16, 0x83, 0x10, 0xa8, 0x01};
  expected = "4 unwind Raw Data: 0x16 0x83 0x10 0xa8 0x01";
  std::string op_string;
  for (uint8_t i = 0; i < 168; i++) {
    ops.push_back(0xa0 + (i % 96));
    op_string += "4 unwind   Illegal\n";
    op_string += android::base::StringPrintf("4 unwind   Raw Data: 0x%02x\n", ops.back());
    expected += android::base::StringPrintf(" 0x%02x", ops.back());
    if (((i + 6) % 10) == 0) {
      expected += "\n4 unwind Raw Data:";
    }
  }
  expected = "4 unwind DW_CFA_val_expression register(2051) 168\n" + expected + "\n";

  this->memory_.SetMemory(0xa00, ops);

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0xa00, 0xaad));

  ASSERT_EQ(expected + op_string, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_args_size) {
  this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x2e, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x2000, 0x2002));

  std::string expected =
      "4 unwind DW_CFA_GNU_args_size 4\n"
      "4 unwind Raw Data: 0x2e 0x04\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x2e, 0xa4, 0x80, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x5000, 0x5004));

  expected =
      "4 unwind DW_CFA_GNU_args_size 65572\n"
      "4 unwind Raw Data: 0x2e 0xa4 0x80 0x04\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_negative_offset_extended) {
  this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x2f, 0x08, 0x10});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x500, 0x503));

  std::string expected =
      "4 unwind DW_CFA_GNU_negative_offset_extended register(8) 16\n"
      "4 unwind Raw Data: 0x2f 0x08 0x10\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());

  ResetLogs();
  this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x2f, 0x81, 0x02, 0xff, 0x01});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x1500, 0x1505));

  expected =
      "4 unwind DW_CFA_GNU_negative_offset_extended register(257) 255\n"
      "4 unwind Raw Data: 0x2f 0x81 0x02 0xff 0x01\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

TYPED_TEST_P(DwarfCfaLogTest, cfa_register_override) {
  this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01, 0x09, 0x02, 0x04});

  ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0, 0x300, 0x306));

  std::string expected =
      "4 unwind DW_CFA_register register(2) register(1)\n"
      "4 unwind Raw Data: 0x09 0x02 0x01\n"
      "4 unwind DW_CFA_register register(2) register(4)\n"
      "4 unwind Raw Data: 0x09 0x02 0x04\n";
  ASSERT_EQ(expected, GetFakeLogPrint());
  ASSERT_EQ("", GetFakeLogBuf());
}

REGISTER_TYPED_TEST_CASE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
                           cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
                           cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
                           cfa_undefined, cfa_same, cfa_register, cfa_state,
                           cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
                           cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
                           cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
                           cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
                           cfa_gnu_negative_offset_extended, cfa_register_override);

typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaLogTestTypes;
INSTANTIATE_TYPED_TEST_CASE_P(, DwarfCfaLogTest, DwarfCfaLogTestTypes);

}  // namespace unwindstack