summaryrefslogtreecommitdiffstats
blob: 7c8a0ea147846ba8f570a4ac4ed86399144ef9d5 (plain) (blame)
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
/*
 * Copyright (C) 2015 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 <stdlib.h>

#include <memory>
#include <string>

#include <android-base/file.h>
#include <gtest/gtest.h>
#include <unwindstack/Memory.h>

#include "libdebuggerd/utility.h"

#include "log_fake.h"

const char g_expected_full_dump[] =
"\nmemory near r1:\n"
#if defined(__LP64__)
"    0000000012345658 0706050403020100 0f0e0d0c0b0a0908  ................\n"
"    0000000012345668 1716151413121110 1f1e1d1c1b1a1918  ................\n"
"    0000000012345678 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
"    0000000012345688 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
"    0000000012345698 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
"    00000000123456a8 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
"    00000000123456b8 6766656463626160 6f6e6d6c6b6a6968  `abcdefghijklmno\n"
"    00000000123456c8 7776757473727170 7f7e7d7c7b7a7978  pqrstuvwxyz{|}~.\n"
"    00000000123456d8 8786858483828180 8f8e8d8c8b8a8988  ................\n"
"    00000000123456e8 9796959493929190 9f9e9d9c9b9a9998  ................\n"
"    00000000123456f8 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
"    0000000012345708 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
"    0000000012345718 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
"    0000000012345728 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
"    0000000012345738 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
"    0000000012345748 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
#else
"    12345658 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
"    12345668 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
"    12345678 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
"    12345688 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
"    12345698 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
"    123456a8 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
"    123456b8 63626160 67666564 6b6a6968 6f6e6d6c  `abcdefghijklmno\n"
"    123456c8 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.\n"
"    123456d8 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
"    123456e8 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
"    123456f8 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
"    12345708 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
"    12345718 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
"    12345728 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
"    12345738 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
"    12345748 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
#endif

const char g_expected_partial_dump[] = \
"\nmemory near pc:\n"
#if defined(__LP64__)
"    00000000123455e0 0706050403020100 0f0e0d0c0b0a0908  ................\n"
"    00000000123455f0 1716151413121110 1f1e1d1c1b1a1918  ................\n"
"    0000000012345600 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
"    0000000012345610 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
"    0000000012345620 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
"    0000000012345630 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
"    0000000012345640 6766656463626160 ----------------  `abcdefg........\n"
"    0000000012345650 ---------------- ----------------  ................\n"
"    0000000012345660 ---------------- ----------------  ................\n"
"    0000000012345670 ---------------- ----------------  ................\n"
"    0000000012345680 ---------------- ----------------  ................\n"
"    0000000012345690 ---------------- ----------------  ................\n"
"    00000000123456a0 ---------------- ----------------  ................\n"
"    00000000123456b0 ---------------- ----------------  ................\n"
"    00000000123456c0 ---------------- ----------------  ................\n"
"    00000000123456d0 ---------------- ----------------  ................\n";
#else
"    123455e0 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
"    123455f0 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
"    12345600 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
"    12345610 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
"    12345620 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
"    12345630 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
"    12345640 63626160 67666564 -------- --------  `abcdefg........\n"
"    12345650 -------- -------- -------- --------  ................\n"
"    12345660 -------- -------- -------- --------  ................\n"
"    12345670 -------- -------- -------- --------  ................\n"
"    12345680 -------- -------- -------- --------  ................\n"
"    12345690 -------- -------- -------- --------  ................\n"
"    123456a0 -------- -------- -------- --------  ................\n"
"    123456b0 -------- -------- -------- --------  ................\n"
"    123456c0 -------- -------- -------- --------  ................\n"
"    123456d0 -------- -------- -------- --------  ................\n";
#endif

class MemoryMock : public unwindstack::Memory {
 public:
  virtual ~MemoryMock() = default;

  virtual size_t Read(uint64_t addr, void* buffer, size_t bytes) override {
    size_t offset = 0;
    if (last_read_addr_ > 0) {
      offset = addr - last_read_addr_;
    }
    size_t bytes_available = buffer_.size() - offset;

    if (partial_read_) {
      bytes = std::min(bytes, bytes_partial_read_);
      bytes_partial_read_ -= bytes;
      partial_read_ = bytes_partial_read_;
    } else if (bytes > bytes_available) {
      bytes = bytes_available;
    }

    if (bytes > 0) {
      memcpy(buffer, buffer_.data() + offset, bytes);
    }

    last_read_addr_ = addr;
    return bytes;
  }

  void SetReadData(uint8_t* buffer, size_t bytes) {
    buffer_.resize(bytes);
    memcpy(buffer_.data(), buffer, bytes);
    bytes_partial_read_ = 0;
    last_read_addr_ = 0;
  }

  void SetPartialReadAmount(size_t bytes) {
    if (bytes > buffer_.size()) {
      abort();
    }
    partial_read_ = true;
    bytes_partial_read_ = bytes;
  }

 private:
  std::vector<uint8_t> buffer_;
  bool partial_read_ = false;
  size_t bytes_partial_read_ = 0;
  uintptr_t last_read_addr_ = 0;
};

class DumpMemoryTest : public ::testing::Test {
 protected:
  virtual void SetUp() {
    memory_mock_ = std::make_unique<MemoryMock>();

    char tmp_file[256];
    const char data_template[] = "/data/local/tmp/debuggerd_memory_testXXXXXX";
    memcpy(tmp_file, data_template, sizeof(data_template));
    int tombstone_fd = mkstemp(tmp_file);
    if (tombstone_fd == -1) {
      const char tmp_template[] = "/tmp/debuggerd_memory_testXXXXXX";
      memcpy(tmp_file, tmp_template, sizeof(tmp_template));
      tombstone_fd = mkstemp(tmp_file);
      if (tombstone_fd == -1) {
        abort();
      }
    }
    if (unlink(tmp_file) == -1) {
      abort();
    }

    log_.tfd = tombstone_fd;
    log_.amfd_data = nullptr;
    log_.crashed_tid = 12;
    log_.current_tid = 12;
    log_.should_retrieve_logcat = false;

    resetLogs();
  }

  virtual void TearDown() {
    if (log_.tfd >= 0) {
      close(log_.tfd);
    }
    memory_mock_.reset();
  }

  std::unique_ptr<MemoryMock> memory_mock_;

  log_t log_;
};

TEST_F(DumpMemoryTest, aligned_addr) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));

  dump_memory(&log_, memory_mock_.get(), 0x12345678, "memory near %.2s:", "r1");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, partial_read) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(96);

  dump_memory(&log_, memory_mock_.get(), 0x12345679, "memory near %.2s:", "r1");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, unaligned_addr) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));

  dump_memory(&log_, memory_mock_.get(), 0x12345679, "memory near %.2s:", "r1");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_unreadable) {
  dump_memory(&log_, memory_mock_.get(), 0xa2345678, "memory near pc:");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near pc:\n"
#if defined(__LP64__)
"    00000000a2345658 ---------------- ----------------  ................\n"
"    00000000a2345668 ---------------- ----------------  ................\n"
"    00000000a2345678 ---------------- ----------------  ................\n"
"    00000000a2345688 ---------------- ----------------  ................\n"
"    00000000a2345698 ---------------- ----------------  ................\n"
"    00000000a23456a8 ---------------- ----------------  ................\n"
"    00000000a23456b8 ---------------- ----------------  ................\n"
"    00000000a23456c8 ---------------- ----------------  ................\n"
"    00000000a23456d8 ---------------- ----------------  ................\n"
"    00000000a23456e8 ---------------- ----------------  ................\n"
"    00000000a23456f8 ---------------- ----------------  ................\n"
"    00000000a2345708 ---------------- ----------------  ................\n"
"    00000000a2345718 ---------------- ----------------  ................\n"
"    00000000a2345728 ---------------- ----------------  ................\n"
"    00000000a2345738 ---------------- ----------------  ................\n"
"    00000000a2345748 ---------------- ----------------  ................\n";
#else
"    a2345658 -------- -------- -------- --------  ................\n"
"    a2345668 -------- -------- -------- --------  ................\n"
"    a2345678 -------- -------- -------- --------  ................\n"
"    a2345688 -------- -------- -------- --------  ................\n"
"    a2345698 -------- -------- -------- --------  ................\n"
"    a23456a8 -------- -------- -------- --------  ................\n"
"    a23456b8 -------- -------- -------- --------  ................\n"
"    a23456c8 -------- -------- -------- --------  ................\n"
"    a23456d8 -------- -------- -------- --------  ................\n"
"    a23456e8 -------- -------- -------- --------  ................\n"
"    a23456f8 -------- -------- -------- --------  ................\n"
"    a2345708 -------- -------- -------- --------  ................\n"
"    a2345718 -------- -------- -------- --------  ................\n"
"    a2345728 -------- -------- -------- --------  ................\n"
"    a2345738 -------- -------- -------- --------  ................\n"
"    a2345748 -------- -------- -------- --------  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_partially_unreadable) {
  uint8_t buffer[104];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));

  dump_memory(&log_, memory_mock_.get(), 0x12345600, "memory near pc:");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_partially_unreadable_unaligned_return) {
  uint8_t buffer[104];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(102);

  dump_memory(&log_, memory_mock_.get(), 0x12345600, "memory near pc:");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());

#if defined(__LP64__)
  ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 8\n", getFakeLogPrint().c_str());
#else
  ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 4\n", getFakeLogPrint().c_str());
#endif

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
}

TEST_F(DumpMemoryTest, memory_partially_unreadable_two_unaligned_reads) {
  uint8_t buffer[106];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(45);

  dump_memory(&log_, memory_mock_.get(), 0x12345600, "memory near pc:");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());

#if defined(__LP64__)
  ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 8\n"
               "6 DEBUG Bytes after second read 106, is not a multiple of 8\n",
               getFakeLogPrint().c_str());
#else
  ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 4\n"
               "6 DEBUG Bytes after second read 106, is not a multiple of 4\n",
               getFakeLogPrint().c_str());
#endif

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
}

TEST_F(DumpMemoryTest, address_low_fence) {
  uint8_t buffer[256];
  memset(buffer, 0, sizeof(buffer));
  memory_mock_->SetReadData(buffer, sizeof(buffer));

  dump_memory(&log_, memory_mock_.get(), 0x1000, "memory near %.2s:", "r1");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r1:\n"
#if defined(__LP64__)
"    0000000000001000 0000000000000000 0000000000000000  ................\n"
"    0000000000001010 0000000000000000 0000000000000000  ................\n"
"    0000000000001020 0000000000000000 0000000000000000  ................\n"
"    0000000000001030 0000000000000000 0000000000000000  ................\n"
"    0000000000001040 0000000000000000 0000000000000000  ................\n"
"    0000000000001050 0000000000000000 0000000000000000  ................\n"
"    0000000000001060 0000000000000000 0000000000000000  ................\n"
"    0000000000001070 0000000000000000 0000000000000000  ................\n"
"    0000000000001080 0000000000000000 0000000000000000  ................\n"
"    0000000000001090 0000000000000000 0000000000000000  ................\n"
"    00000000000010a0 0000000000000000 0000000000000000  ................\n"
"    00000000000010b0 0000000000000000 0000000000000000  ................\n"
"    00000000000010c0 0000000000000000 0000000000000000  ................\n"
"    00000000000010d0 0000000000000000 0000000000000000  ................\n"
"    00000000000010e0 0000000000000000 0000000000000000  ................\n"
"    00000000000010f0 0000000000000000 0000000000000000  ................\n";
#else
"    00001000 00000000 00000000 00000000 00000000  ................\n"
"    00001010 00000000 00000000 00000000 00000000  ................\n"
"    00001020 00000000 00000000 00000000 00000000  ................\n"
"    00001030 00000000 00000000 00000000 00000000  ................\n"
"    00001040 00000000 00000000 00000000 00000000  ................\n"
"    00001050 00000000 00000000 00000000 00000000  ................\n"
"    00001060 00000000 00000000 00000000 00000000  ................\n"
"    00001070 00000000 00000000 00000000 00000000  ................\n"
"    00001080 00000000 00000000 00000000 00000000  ................\n"
"    00001090 00000000 00000000 00000000 00000000  ................\n"
"    000010a0 00000000 00000000 00000000 00000000  ................\n"
"    000010b0 00000000 00000000 00000000 00000000  ................\n"
"    000010c0 00000000 00000000 00000000 00000000  ................\n"
"    000010d0 00000000 00000000 00000000 00000000  ................\n"
"    000010e0 00000000 00000000 00000000 00000000  ................\n"
"    000010f0 00000000 00000000 00000000 00000000  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_address_too_low) {
  uint8_t buffer[256];
  memset(buffer, 0, sizeof(buffer));
  memory_mock_->SetReadData(buffer, sizeof(buffer));

  dump_memory(&log_, memory_mock_.get(), 0, "memory near %.2s:", "r1");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ("", tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_address_too_high) {
  uint8_t buffer[256];
  memset(buffer, 0, sizeof(buffer));
  memory_mock_->SetReadData(buffer, sizeof(buffer));

#if defined(__LP64__)
  dump_memory(&log_, memory_mock_.get(), 0x4000000000000000UL, "memory near %.2s:", "r1");
  dump_memory(&log_, memory_mock_.get(), 0x4000000000000000UL - 32, "memory near %.2s:", "r1");
  dump_memory(&log_, memory_mock_.get(), 0x4000000000000000UL - 216, "memory near %.2s:", "r1");
#else
  dump_memory(&log_, memory_mock_.get(), 0xffff0000, "memory near %.2s:", "r1");
  dump_memory(&log_, memory_mock_.get(), 0xffff0000 - 32, "memory near %.2s:", "r1");
  dump_memory(&log_, memory_mock_.get(), 0xffff0000 - 220, "memory near %.2s:", "r1");
#endif

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ("", tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_address_would_overflow) {
  uint8_t buffer[256];
  memset(buffer, 0, sizeof(buffer));
  memory_mock_->SetReadData(buffer, sizeof(buffer));

#if defined(__LP64__)
  dump_memory(&log_, memory_mock_.get(), 0xfffffffffffffff0, "memory near %.2s:", "r1");
#else
  dump_memory(&log_, memory_mock_.get(), 0xfffffff0, "memory near %.2s:", "r1");
#endif

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  ASSERT_STREQ("", tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, memory_address_nearly_too_high) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));

#if defined(__LP64__)
  dump_memory(&log_, memory_mock_.get(), 0x4000000000000000UL - 224, "memory near %.2s:", "r4");
#else
  dump_memory(&log_, memory_mock_.get(), 0xffff0000 - 224, "memory near %.2s:", "r4");
#endif

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r4:\n"
#if defined(__LP64__)
"    3fffffffffffff00 0706050403020100 0f0e0d0c0b0a0908  ................\n"
"    3fffffffffffff10 1716151413121110 1f1e1d1c1b1a1918  ................\n"
"    3fffffffffffff20 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
"    3fffffffffffff30 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
"    3fffffffffffff40 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
"    3fffffffffffff50 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
"    3fffffffffffff60 6766656463626160 6f6e6d6c6b6a6968  `abcdefghijklmno\n"
"    3fffffffffffff70 7776757473727170 7f7e7d7c7b7a7978  pqrstuvwxyz{|}~.\n"
"    3fffffffffffff80 8786858483828180 8f8e8d8c8b8a8988  ................\n"
"    3fffffffffffff90 9796959493929190 9f9e9d9c9b9a9998  ................\n"
"    3fffffffffffffa0 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
"    3fffffffffffffb0 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
"    3fffffffffffffc0 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
"    3fffffffffffffd0 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
"    3fffffffffffffe0 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
"    3ffffffffffffff0 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
#else
"    fffeff00 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
"    fffeff10 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
"    fffeff20 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
"    fffeff30 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
"    fffeff40 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
"    fffeff50 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
"    fffeff60 63626160 67666564 6b6a6968 6f6e6d6c  `abcdefghijklmno\n"
"    fffeff70 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.\n"
"    fffeff80 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
"    fffeff90 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
"    fffeffa0 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
"    fffeffb0 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
"    fffeffc0 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
"    fffeffd0 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
"    fffeffe0 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
"    fffefff0 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, first_read_empty) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(0);

  size_t page_size = sysconf(_SC_PAGE_SIZE);
  uintptr_t addr = 0x10000020 + page_size - 120;
  dump_memory(&log_, memory_mock_.get(), addr, "memory near %.2s:", "r4");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r4:\n"
#if defined(__LP64__)
"    0000000010000f88 ---------------- ----------------  ................\n"
"    0000000010000f98 ---------------- ----------------  ................\n"
"    0000000010000fa8 ---------------- ----------------  ................\n"
"    0000000010000fb8 ---------------- ----------------  ................\n"
"    0000000010000fc8 ---------------- ----------------  ................\n"
"    0000000010000fd8 ---------------- ----------------  ................\n"
"    0000000010000fe8 ---------------- ----------------  ................\n"
"    0000000010000ff8 ---------------- 7f7e7d7c7b7a7978  ........xyz{|}~.\n"
"    0000000010001008 8786858483828180 8f8e8d8c8b8a8988  ................\n"
"    0000000010001018 9796959493929190 9f9e9d9c9b9a9998  ................\n"
"    0000000010001028 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
"    0000000010001038 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
"    0000000010001048 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
"    0000000010001058 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
"    0000000010001068 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
"    0000000010001078 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
#else
"    10000f88 -------- -------- -------- --------  ................\n"
"    10000f98 -------- -------- -------- --------  ................\n"
"    10000fa8 -------- -------- -------- --------  ................\n"
"    10000fb8 -------- -------- -------- --------  ................\n"
"    10000fc8 -------- -------- -------- --------  ................\n"
"    10000fd8 -------- -------- -------- --------  ................\n"
"    10000fe8 -------- -------- -------- --------  ................\n"
"    10000ff8 -------- -------- 7b7a7978 7f7e7d7c  ........xyz{|}~.\n"
"    10001008 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
"    10001018 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
"    10001028 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
"    10001038 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
"    10001048 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
"    10001058 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
"    10001068 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
"    10001078 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, first_read_empty_second_read_stops) {
  uint8_t buffer[224];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(0);

  size_t page_size = sysconf(_SC_PAGE_SIZE);
  uintptr_t addr = 0x10000020 + page_size - 192;
  dump_memory(&log_, memory_mock_.get(), addr, "memory near %.2s:", "r4");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r4:\n"
#if defined(__LP64__)
"    0000000010000f40 ---------------- ----------------  ................\n"
"    0000000010000f50 ---------------- ----------------  ................\n"
"    0000000010000f60 ---------------- ----------------  ................\n"
"    0000000010000f70 ---------------- ----------------  ................\n"
"    0000000010000f80 ---------------- ----------------  ................\n"
"    0000000010000f90 ---------------- ----------------  ................\n"
"    0000000010000fa0 ---------------- ----------------  ................\n"
"    0000000010000fb0 ---------------- ----------------  ................\n"
"    0000000010000fc0 ---------------- ----------------  ................\n"
"    0000000010000fd0 ---------------- ----------------  ................\n"
"    0000000010000fe0 ---------------- ----------------  ................\n"
"    0000000010000ff0 ---------------- ----------------  ................\n"
"    0000000010001000 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
"    0000000010001010 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
"    0000000010001020 ---------------- ----------------  ................\n"
"    0000000010001030 ---------------- ----------------  ................\n";
#else
"    10000f40 -------- -------- -------- --------  ................\n"
"    10000f50 -------- -------- -------- --------  ................\n"
"    10000f60 -------- -------- -------- --------  ................\n"
"    10000f70 -------- -------- -------- --------  ................\n"
"    10000f80 -------- -------- -------- --------  ................\n"
"    10000f90 -------- -------- -------- --------  ................\n"
"    10000fa0 -------- -------- -------- --------  ................\n"
"    10000fb0 -------- -------- -------- --------  ................\n"
"    10000fc0 -------- -------- -------- --------  ................\n"
"    10000fd0 -------- -------- -------- --------  ................\n"
"    10000fe0 -------- -------- -------- --------  ................\n"
"    10000ff0 -------- -------- -------- --------  ................\n"
"    10001000 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
"    10001010 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
"    10001020 -------- -------- -------- --------  ................\n"
"    10001030 -------- -------- -------- --------  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(0);

  uintptr_t addr = 0x10000020;
  dump_memory(&log_, memory_mock_.get(), addr, "memory near %.2s:", "r4");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r4:\n"
#if defined(__LP64__)
"    0000000010000000 ---------------- ----------------  ................\n"
"    0000000010000010 ---------------- ----------------  ................\n"
"    0000000010000020 ---------------- ----------------  ................\n"
"    0000000010000030 ---------------- ----------------  ................\n"
"    0000000010000040 ---------------- ----------------  ................\n"
"    0000000010000050 ---------------- ----------------  ................\n"
"    0000000010000060 ---------------- ----------------  ................\n"
"    0000000010000070 ---------------- ----------------  ................\n"
"    0000000010000080 ---------------- ----------------  ................\n"
"    0000000010000090 ---------------- ----------------  ................\n"
"    00000000100000a0 ---------------- ----------------  ................\n"
"    00000000100000b0 ---------------- ----------------  ................\n"
"    00000000100000c0 ---------------- ----------------  ................\n"
"    00000000100000d0 ---------------- ----------------  ................\n"
"    00000000100000e0 ---------------- ----------------  ................\n"
"    00000000100000f0 ---------------- ----------------  ................\n";
#else
"    10000000 -------- -------- -------- --------  ................\n"
"    10000010 -------- -------- -------- --------  ................\n"
"    10000020 -------- -------- -------- --------  ................\n"
"    10000030 -------- -------- -------- --------  ................\n"
"    10000040 -------- -------- -------- --------  ................\n"
"    10000050 -------- -------- -------- --------  ................\n"
"    10000060 -------- -------- -------- --------  ................\n"
"    10000070 -------- -------- -------- --------  ................\n"
"    10000080 -------- -------- -------- --------  ................\n"
"    10000090 -------- -------- -------- --------  ................\n"
"    100000a0 -------- -------- -------- --------  ................\n"
"    100000b0 -------- -------- -------- --------  ................\n"
"    100000c0 -------- -------- -------- --------  ................\n"
"    100000d0 -------- -------- -------- --------  ................\n"
"    100000e0 -------- -------- -------- --------  ................\n"
"    100000f0 -------- -------- -------- --------  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}

TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range_fence_post) {
  uint8_t buffer[256];
  for (size_t i = 0; i < sizeof(buffer); i++) {
    buffer[i] = i;
  }
  memory_mock_->SetReadData(buffer, sizeof(buffer));
  memory_mock_->SetPartialReadAmount(0);

  size_t page_size = sysconf(_SC_PAGE_SIZE);
  uintptr_t addr = 0x10000020 + page_size - 256;

  dump_memory(&log_, memory_mock_.get(), addr, "memory near %.2s:", "r4");

  std::string tombstone_contents;
  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
  const char* expected_dump = \
"\nmemory near r4:\n"
#if defined(__LP64__)
"    0000000010000f00 ---------------- ----------------  ................\n"
"    0000000010000f10 ---------------- ----------------  ................\n"
"    0000000010000f20 ---------------- ----------------  ................\n"
"    0000000010000f30 ---------------- ----------------  ................\n"
"    0000000010000f40 ---------------- ----------------  ................\n"
"    0000000010000f50 ---------------- ----------------  ................\n"
"    0000000010000f60 ---------------- ----------------  ................\n"
"    0000000010000f70 ---------------- ----------------  ................\n"
"    0000000010000f80 ---------------- ----------------  ................\n"
"    0000000010000f90 ---------------- ----------------  ................\n"
"    0000000010000fa0 ---------------- ----------------  ................\n"
"    0000000010000fb0 ---------------- ----------------  ................\n"
"    0000000010000fc0 ---------------- ----------------  ................\n"
"    0000000010000fd0 ---------------- ----------------  ................\n"
"    0000000010000fe0 ---------------- ----------------  ................\n"
"    0000000010000ff0 ---------------- ----------------  ................\n";
#else
"    10000f00 -------- -------- -------- --------  ................\n"
"    10000f10 -------- -------- -------- --------  ................\n"
"    10000f20 -------- -------- -------- --------  ................\n"
"    10000f30 -------- -------- -------- --------  ................\n"
"    10000f40 -------- -------- -------- --------  ................\n"
"    10000f50 -------- -------- -------- --------  ................\n"
"    10000f60 -------- -------- -------- --------  ................\n"
"    10000f70 -------- -------- -------- --------  ................\n"
"    10000f80 -------- -------- -------- --------  ................\n"
"    10000f90 -------- -------- -------- --------  ................\n"
"    10000fa0 -------- -------- -------- --------  ................\n"
"    10000fb0 -------- -------- -------- --------  ................\n"
"    10000fc0 -------- -------- -------- --------  ................\n"
"    10000fd0 -------- -------- -------- --------  ................\n"
"    10000fe0 -------- -------- -------- --------  ................\n"
"    10000ff0 -------- -------- -------- --------  ................\n";
#endif
  ASSERT_STREQ(expected_dump, tombstone_contents.c_str());

  // Verify that the log buf is empty, and no error messages.
  ASSERT_STREQ("", getFakeLogBuf().c_str());
  ASSERT_STREQ("", getFakeLogPrint().c_str());
}