• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 <stdlib.h>
18 
19 #include <memory>
20 #include <string>
21 
22 #include <gtest/gtest.h>
23 #include <android-base/file.h>
24 
25 #include "BacktraceMock.h"
26 #include "log_fake.h"
27 #include "utility.h"
28 
29 const char g_expected_full_dump[] =
30 "\nmemory near r1:\n"
31 #if defined(__LP64__)
32 "    0000000012345658 0706050403020100 0f0e0d0c0b0a0908  ................\n"
33 "    0000000012345668 1716151413121110 1f1e1d1c1b1a1918  ................\n"
34 "    0000000012345678 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
35 "    0000000012345688 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
36 "    0000000012345698 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
37 "    00000000123456a8 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
38 "    00000000123456b8 6766656463626160 6f6e6d6c6b6a6968  `abcdefghijklmno\n"
39 "    00000000123456c8 7776757473727170 7f7e7d7c7b7a7978  pqrstuvwxyz{|}~.\n"
40 "    00000000123456d8 8786858483828180 8f8e8d8c8b8a8988  ................\n"
41 "    00000000123456e8 9796959493929190 9f9e9d9c9b9a9998  ................\n"
42 "    00000000123456f8 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
43 "    0000000012345708 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
44 "    0000000012345718 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
45 "    0000000012345728 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
46 "    0000000012345738 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
47 "    0000000012345748 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
48 #else
49 "    12345658 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
50 "    12345668 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
51 "    12345678 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
52 "    12345688 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
53 "    12345698 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
54 "    123456a8 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
55 "    123456b8 63626160 67666564 6b6a6968 6f6e6d6c  `abcdefghijklmno\n"
56 "    123456c8 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.\n"
57 "    123456d8 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
58 "    123456e8 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
59 "    123456f8 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
60 "    12345708 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
61 "    12345718 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
62 "    12345728 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
63 "    12345738 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
64 "    12345748 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
65 #endif
66 
67 const char g_expected_partial_dump[] = \
68 "\nmemory near pc:\n"
69 #if defined(__LP64__)
70 "    00000000123455e0 0706050403020100 0f0e0d0c0b0a0908  ................\n"
71 "    00000000123455f0 1716151413121110 1f1e1d1c1b1a1918  ................\n"
72 "    0000000012345600 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
73 "    0000000012345610 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
74 "    0000000012345620 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
75 "    0000000012345630 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
76 "    0000000012345640 6766656463626160 ----------------  `abcdefg........\n"
77 "    0000000012345650 ---------------- ----------------  ................\n"
78 "    0000000012345660 ---------------- ----------------  ................\n"
79 "    0000000012345670 ---------------- ----------------  ................\n"
80 "    0000000012345680 ---------------- ----------------  ................\n"
81 "    0000000012345690 ---------------- ----------------  ................\n"
82 "    00000000123456a0 ---------------- ----------------  ................\n"
83 "    00000000123456b0 ---------------- ----------------  ................\n"
84 "    00000000123456c0 ---------------- ----------------  ................\n"
85 "    00000000123456d0 ---------------- ----------------  ................\n";
86 #else
87 "    123455e0 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
88 "    123455f0 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
89 "    12345600 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
90 "    12345610 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
91 "    12345620 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
92 "    12345630 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
93 "    12345640 63626160 67666564 -------- --------  `abcdefg........\n"
94 "    12345650 -------- -------- -------- --------  ................\n"
95 "    12345660 -------- -------- -------- --------  ................\n"
96 "    12345670 -------- -------- -------- --------  ................\n"
97 "    12345680 -------- -------- -------- --------  ................\n"
98 "    12345690 -------- -------- -------- --------  ................\n"
99 "    123456a0 -------- -------- -------- --------  ................\n"
100 "    123456b0 -------- -------- -------- --------  ................\n"
101 "    123456c0 -------- -------- -------- --------  ................\n"
102 "    123456d0 -------- -------- -------- --------  ................\n";
103 #endif
104 
105 class DumpMemoryTest : public ::testing::Test {
106  protected:
SetUp()107   virtual void SetUp() {
108     map_mock_.reset(new BacktraceMapMock());
109     backtrace_mock_.reset(new BacktraceMock(map_mock_.get()));
110 
111     char tmp_file[256];
112     const char data_template[] = "/data/local/tmp/debuggerd_memory_testXXXXXX";
113     memcpy(tmp_file, data_template, sizeof(data_template));
114     int tombstone_fd = mkstemp(tmp_file);
115     if (tombstone_fd == -1) {
116       const char tmp_template[] = "/tmp/debuggerd_memory_testXXXXXX";
117       memcpy(tmp_file, tmp_template, sizeof(tmp_template));
118       tombstone_fd = mkstemp(tmp_file);
119       if (tombstone_fd == -1) {
120         abort();
121       }
122     }
123     if (unlink(tmp_file) == -1) {
124       abort();
125     }
126 
127     log_.tfd = tombstone_fd;
128     log_.amfd_data = nullptr;
129     log_.crashed_tid = 12;
130     log_.current_tid = 12;
131     log_.should_retrieve_logcat = false;
132 
133     resetLogs();
134   }
135 
TearDown()136   virtual void TearDown() {
137     if (log_.tfd >= 0) {
138       close(log_.tfd);
139     }
140   }
141 
142   std::unique_ptr<BacktraceMapMock> map_mock_;
143   std::unique_ptr<BacktraceMock> backtrace_mock_;
144 
145   log_t log_;
146 };
147 
TEST_F(DumpMemoryTest,aligned_addr)148 TEST_F(DumpMemoryTest, aligned_addr) {
149   uint8_t buffer[256];
150   for (size_t i = 0; i < sizeof(buffer); i++) {
151     buffer[i] = i;
152   }
153   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
154 
155   dump_memory(&log_, backtrace_mock_.get(), 0x12345678, "memory near %.2s:", "r1");
156 
157   std::string tombstone_contents;
158   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
159   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
160   ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());
161 
162   // Verify that the log buf is empty, and no error messages.
163   ASSERT_STREQ("", getFakeLogBuf().c_str());
164   ASSERT_STREQ("", getFakeLogPrint().c_str());
165 }
166 
TEST_F(DumpMemoryTest,partial_read)167 TEST_F(DumpMemoryTest, partial_read) {
168   uint8_t buffer[256];
169   for (size_t i = 0; i < sizeof(buffer); i++) {
170     buffer[i] = i;
171   }
172   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
173   backtrace_mock_->SetPartialReadAmount(96);
174 
175   dump_memory(&log_, backtrace_mock_.get(), 0x12345679, "memory near %.2s:", "r1");
176 
177   std::string tombstone_contents;
178   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
179   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
180   ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());
181 
182   // Verify that the log buf is empty, and no error messages.
183   ASSERT_STREQ("", getFakeLogBuf().c_str());
184   ASSERT_STREQ("", getFakeLogPrint().c_str());
185 }
186 
TEST_F(DumpMemoryTest,unaligned_addr)187 TEST_F(DumpMemoryTest, unaligned_addr) {
188   uint8_t buffer[256];
189   for (size_t i = 0; i < sizeof(buffer); i++) {
190     buffer[i] = i;
191   }
192   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
193 
194   dump_memory(&log_, backtrace_mock_.get(), 0x12345679, "memory near %.2s:", "r1");
195 
196   std::string tombstone_contents;
197   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
198   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
199   ASSERT_STREQ(g_expected_full_dump, tombstone_contents.c_str());
200 
201   // Verify that the log buf is empty, and no error messages.
202   ASSERT_STREQ("", getFakeLogBuf().c_str());
203   ASSERT_STREQ("", getFakeLogPrint().c_str());
204 }
205 
TEST_F(DumpMemoryTest,memory_unreadable)206 TEST_F(DumpMemoryTest, memory_unreadable) {
207   dump_memory(&log_, backtrace_mock_.get(), 0xa2345678, "memory near pc:");
208 
209   std::string tombstone_contents;
210   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
211   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
212   const char* expected_dump = \
213 "\nmemory near pc:\n"
214 #if defined(__LP64__)
215 "    00000000a2345658 ---------------- ----------------  ................\n"
216 "    00000000a2345668 ---------------- ----------------  ................\n"
217 "    00000000a2345678 ---------------- ----------------  ................\n"
218 "    00000000a2345688 ---------------- ----------------  ................\n"
219 "    00000000a2345698 ---------------- ----------------  ................\n"
220 "    00000000a23456a8 ---------------- ----------------  ................\n"
221 "    00000000a23456b8 ---------------- ----------------  ................\n"
222 "    00000000a23456c8 ---------------- ----------------  ................\n"
223 "    00000000a23456d8 ---------------- ----------------  ................\n"
224 "    00000000a23456e8 ---------------- ----------------  ................\n"
225 "    00000000a23456f8 ---------------- ----------------  ................\n"
226 "    00000000a2345708 ---------------- ----------------  ................\n"
227 "    00000000a2345718 ---------------- ----------------  ................\n"
228 "    00000000a2345728 ---------------- ----------------  ................\n"
229 "    00000000a2345738 ---------------- ----------------  ................\n"
230 "    00000000a2345748 ---------------- ----------------  ................\n";
231 #else
232 "    a2345658 -------- -------- -------- --------  ................\n"
233 "    a2345668 -------- -------- -------- --------  ................\n"
234 "    a2345678 -------- -------- -------- --------  ................\n"
235 "    a2345688 -------- -------- -------- --------  ................\n"
236 "    a2345698 -------- -------- -------- --------  ................\n"
237 "    a23456a8 -------- -------- -------- --------  ................\n"
238 "    a23456b8 -------- -------- -------- --------  ................\n"
239 "    a23456c8 -------- -------- -------- --------  ................\n"
240 "    a23456d8 -------- -------- -------- --------  ................\n"
241 "    a23456e8 -------- -------- -------- --------  ................\n"
242 "    a23456f8 -------- -------- -------- --------  ................\n"
243 "    a2345708 -------- -------- -------- --------  ................\n"
244 "    a2345718 -------- -------- -------- --------  ................\n"
245 "    a2345728 -------- -------- -------- --------  ................\n"
246 "    a2345738 -------- -------- -------- --------  ................\n"
247 "    a2345748 -------- -------- -------- --------  ................\n";
248 #endif
249   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
250 
251   // Verify that the log buf is empty, and no error messages.
252   ASSERT_STREQ("", getFakeLogBuf().c_str());
253   ASSERT_STREQ("", getFakeLogPrint().c_str());
254 }
255 
TEST_F(DumpMemoryTest,memory_partially_unreadable)256 TEST_F(DumpMemoryTest, memory_partially_unreadable) {
257   uint8_t buffer[104];
258   for (size_t i = 0; i < sizeof(buffer); i++) {
259     buffer[i] = i;
260   }
261   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
262 
263   dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:");
264 
265   std::string tombstone_contents;
266   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
267   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
268   ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());
269 
270   // Verify that the log buf is empty, and no error messages.
271   ASSERT_STREQ("", getFakeLogBuf().c_str());
272   ASSERT_STREQ("", getFakeLogPrint().c_str());
273 }
274 
TEST_F(DumpMemoryTest,memory_partially_unreadable_unaligned_return)275 TEST_F(DumpMemoryTest, memory_partially_unreadable_unaligned_return) {
276   uint8_t buffer[104];
277   for (size_t i = 0; i < sizeof(buffer); i++) {
278     buffer[i] = i;
279   }
280   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
281   backtrace_mock_->SetPartialReadAmount(102);
282 
283   dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:");
284 
285   std::string tombstone_contents;
286   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
287   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
288   ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());
289 
290 #if defined(__LP64__)
291   ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 8\n", getFakeLogPrint().c_str());
292 #else
293   ASSERT_STREQ("6 DEBUG Bytes read 102, is not a multiple of 4\n", getFakeLogPrint().c_str());
294 #endif
295 
296   // Verify that the log buf is empty, and no error messages.
297   ASSERT_STREQ("", getFakeLogBuf().c_str());
298 }
299 
TEST_F(DumpMemoryTest,memory_partially_unreadable_two_unaligned_reads)300 TEST_F(DumpMemoryTest, memory_partially_unreadable_two_unaligned_reads) {
301   uint8_t buffer[106];
302   for (size_t i = 0; i < sizeof(buffer); i++) {
303     buffer[i] = i;
304   }
305   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
306   backtrace_mock_->SetPartialReadAmount(45);
307 
308   dump_memory(&log_, backtrace_mock_.get(), 0x12345600, "memory near pc:");
309 
310   std::string tombstone_contents;
311   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
312   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
313   ASSERT_STREQ(g_expected_partial_dump, tombstone_contents.c_str());
314 
315 #if defined(__LP64__)
316   ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 8\n"
317                "6 DEBUG Bytes after second read 106, is not a multiple of 8\n",
318                getFakeLogPrint().c_str());
319 #else
320   ASSERT_STREQ("6 DEBUG Bytes read 45, is not a multiple of 4\n"
321                "6 DEBUG Bytes after second read 106, is not a multiple of 4\n",
322                getFakeLogPrint().c_str());
323 #endif
324 
325   // Verify that the log buf is empty, and no error messages.
326   ASSERT_STREQ("", getFakeLogBuf().c_str());
327 }
328 
TEST_F(DumpMemoryTest,address_low_fence)329 TEST_F(DumpMemoryTest, address_low_fence) {
330   uint8_t buffer[256];
331   memset(buffer, 0, sizeof(buffer));
332   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
333 
334   dump_memory(&log_, backtrace_mock_.get(), 0x1000, "memory near %.2s:", "r1");
335 
336   std::string tombstone_contents;
337   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
338   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
339   const char* expected_dump = \
340 "\nmemory near r1:\n"
341 #if defined(__LP64__)
342 "    0000000000001000 0000000000000000 0000000000000000  ................\n"
343 "    0000000000001010 0000000000000000 0000000000000000  ................\n"
344 "    0000000000001020 0000000000000000 0000000000000000  ................\n"
345 "    0000000000001030 0000000000000000 0000000000000000  ................\n"
346 "    0000000000001040 0000000000000000 0000000000000000  ................\n"
347 "    0000000000001050 0000000000000000 0000000000000000  ................\n"
348 "    0000000000001060 0000000000000000 0000000000000000  ................\n"
349 "    0000000000001070 0000000000000000 0000000000000000  ................\n"
350 "    0000000000001080 0000000000000000 0000000000000000  ................\n"
351 "    0000000000001090 0000000000000000 0000000000000000  ................\n"
352 "    00000000000010a0 0000000000000000 0000000000000000  ................\n"
353 "    00000000000010b0 0000000000000000 0000000000000000  ................\n"
354 "    00000000000010c0 0000000000000000 0000000000000000  ................\n"
355 "    00000000000010d0 0000000000000000 0000000000000000  ................\n"
356 "    00000000000010e0 0000000000000000 0000000000000000  ................\n"
357 "    00000000000010f0 0000000000000000 0000000000000000  ................\n";
358 #else
359 "    00001000 00000000 00000000 00000000 00000000  ................\n"
360 "    00001010 00000000 00000000 00000000 00000000  ................\n"
361 "    00001020 00000000 00000000 00000000 00000000  ................\n"
362 "    00001030 00000000 00000000 00000000 00000000  ................\n"
363 "    00001040 00000000 00000000 00000000 00000000  ................\n"
364 "    00001050 00000000 00000000 00000000 00000000  ................\n"
365 "    00001060 00000000 00000000 00000000 00000000  ................\n"
366 "    00001070 00000000 00000000 00000000 00000000  ................\n"
367 "    00001080 00000000 00000000 00000000 00000000  ................\n"
368 "    00001090 00000000 00000000 00000000 00000000  ................\n"
369 "    000010a0 00000000 00000000 00000000 00000000  ................\n"
370 "    000010b0 00000000 00000000 00000000 00000000  ................\n"
371 "    000010c0 00000000 00000000 00000000 00000000  ................\n"
372 "    000010d0 00000000 00000000 00000000 00000000  ................\n"
373 "    000010e0 00000000 00000000 00000000 00000000  ................\n"
374 "    000010f0 00000000 00000000 00000000 00000000  ................\n";
375 #endif
376   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
377 
378   // Verify that the log buf is empty, and no error messages.
379   ASSERT_STREQ("", getFakeLogBuf().c_str());
380   ASSERT_STREQ("", getFakeLogPrint().c_str());
381 }
382 
TEST_F(DumpMemoryTest,memory_address_too_low)383 TEST_F(DumpMemoryTest, memory_address_too_low) {
384   uint8_t buffer[256];
385   memset(buffer, 0, sizeof(buffer));
386   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
387 
388   dump_memory(&log_, backtrace_mock_.get(), 0, "memory near %.2s:", "r1");
389 
390   std::string tombstone_contents;
391   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
392   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
393   ASSERT_STREQ("", tombstone_contents.c_str());
394 
395   // Verify that the log buf is empty, and no error messages.
396   ASSERT_STREQ("", getFakeLogBuf().c_str());
397   ASSERT_STREQ("", getFakeLogPrint().c_str());
398 }
399 
TEST_F(DumpMemoryTest,memory_address_too_high)400 TEST_F(DumpMemoryTest, memory_address_too_high) {
401   uint8_t buffer[256];
402   memset(buffer, 0, sizeof(buffer));
403   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
404 
405 #if defined(__LP64__)
406   dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL, "memory near %.2s:", "r1");
407   dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 32, "memory near %.2s:", "r1");
408   dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 216, "memory near %.2s:", "r1");
409 #else
410   dump_memory(&log_, backtrace_mock_.get(), 0xffff0000, "memory near %.2s:", "r1");
411   dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 32, "memory near %.2s:", "r1");
412   dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 220, "memory near %.2s:", "r1");
413 #endif
414 
415   std::string tombstone_contents;
416   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
417   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
418   ASSERT_STREQ("", tombstone_contents.c_str());
419 
420   // Verify that the log buf is empty, and no error messages.
421   ASSERT_STREQ("", getFakeLogBuf().c_str());
422   ASSERT_STREQ("", getFakeLogPrint().c_str());
423 }
424 
TEST_F(DumpMemoryTest,memory_address_would_overflow)425 TEST_F(DumpMemoryTest, memory_address_would_overflow) {
426   uint8_t buffer[256];
427   memset(buffer, 0, sizeof(buffer));
428   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
429 
430 #if defined(__LP64__)
431   dump_memory(&log_, backtrace_mock_.get(), 0xfffffffffffffff0, "memory near %.2s:", "r1");
432 #else
433   dump_memory(&log_, backtrace_mock_.get(), 0xfffffff0, "memory near %.2s:", "r1");
434 #endif
435 
436   std::string tombstone_contents;
437   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
438   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
439   ASSERT_STREQ("", tombstone_contents.c_str());
440 
441   // Verify that the log buf is empty, and no error messages.
442   ASSERT_STREQ("", getFakeLogBuf().c_str());
443   ASSERT_STREQ("", getFakeLogPrint().c_str());
444 }
445 
TEST_F(DumpMemoryTest,memory_address_nearly_too_high)446 TEST_F(DumpMemoryTest, memory_address_nearly_too_high) {
447   uint8_t buffer[256];
448   for (size_t i = 0; i < sizeof(buffer); i++) {
449     buffer[i] = i;
450   }
451   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
452 
453 #if defined(__LP64__)
454   dump_memory(&log_, backtrace_mock_.get(), 0x4000000000000000UL - 224, "memory near %.2s:", "r4");
455 #else
456   dump_memory(&log_, backtrace_mock_.get(), 0xffff0000 - 224, "memory near %.2s:", "r4");
457 #endif
458 
459   std::string tombstone_contents;
460   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
461   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
462   const char* expected_dump = \
463 "\nmemory near r4:\n"
464 #if defined(__LP64__)
465 "    3fffffffffffff00 0706050403020100 0f0e0d0c0b0a0908  ................\n"
466 "    3fffffffffffff10 1716151413121110 1f1e1d1c1b1a1918  ................\n"
467 "    3fffffffffffff20 2726252423222120 2f2e2d2c2b2a2928   !\"#$%&'()*+,-./\n"
468 "    3fffffffffffff30 3736353433323130 3f3e3d3c3b3a3938  0123456789:;<=>?\n"
469 "    3fffffffffffff40 4746454443424140 4f4e4d4c4b4a4948  @ABCDEFGHIJKLMNO\n"
470 "    3fffffffffffff50 5756555453525150 5f5e5d5c5b5a5958  PQRSTUVWXYZ[\\]^_\n"
471 "    3fffffffffffff60 6766656463626160 6f6e6d6c6b6a6968  `abcdefghijklmno\n"
472 "    3fffffffffffff70 7776757473727170 7f7e7d7c7b7a7978  pqrstuvwxyz{|}~.\n"
473 "    3fffffffffffff80 8786858483828180 8f8e8d8c8b8a8988  ................\n"
474 "    3fffffffffffff90 9796959493929190 9f9e9d9c9b9a9998  ................\n"
475 "    3fffffffffffffa0 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
476 "    3fffffffffffffb0 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
477 "    3fffffffffffffc0 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
478 "    3fffffffffffffd0 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
479 "    3fffffffffffffe0 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
480 "    3ffffffffffffff0 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
481 #else
482 "    fffeff00 03020100 07060504 0b0a0908 0f0e0d0c  ................\n"
483 "    fffeff10 13121110 17161514 1b1a1918 1f1e1d1c  ................\n"
484 "    fffeff20 23222120 27262524 2b2a2928 2f2e2d2c   !\"#$%&'()*+,-./\n"
485 "    fffeff30 33323130 37363534 3b3a3938 3f3e3d3c  0123456789:;<=>?\n"
486 "    fffeff40 43424140 47464544 4b4a4948 4f4e4d4c  @ABCDEFGHIJKLMNO\n"
487 "    fffeff50 53525150 57565554 5b5a5958 5f5e5d5c  PQRSTUVWXYZ[\\]^_\n"
488 "    fffeff60 63626160 67666564 6b6a6968 6f6e6d6c  `abcdefghijklmno\n"
489 "    fffeff70 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.\n"
490 "    fffeff80 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
491 "    fffeff90 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
492 "    fffeffa0 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
493 "    fffeffb0 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
494 "    fffeffc0 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
495 "    fffeffd0 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
496 "    fffeffe0 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
497 "    fffefff0 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
498 #endif
499   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
500 
501   // Verify that the log buf is empty, and no error messages.
502   ASSERT_STREQ("", getFakeLogBuf().c_str());
503   ASSERT_STREQ("", getFakeLogPrint().c_str());
504 }
505 
TEST_F(DumpMemoryTest,first_read_empty)506 TEST_F(DumpMemoryTest, first_read_empty) {
507   uint8_t buffer[256];
508   for (size_t i = 0; i < sizeof(buffer); i++) {
509     buffer[i] = i;
510   }
511   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
512   backtrace_mock_->SetPartialReadAmount(0);
513 
514   size_t page_size = sysconf(_SC_PAGE_SIZE);
515   uintptr_t addr = 0x10000020 + page_size - 120;
516   dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4");
517 
518   std::string tombstone_contents;
519   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
520   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
521   const char* expected_dump = \
522 "\nmemory near r4:\n"
523 #if defined(__LP64__)
524 "    0000000010000f88 ---------------- ----------------  ................\n"
525 "    0000000010000f98 ---------------- ----------------  ................\n"
526 "    0000000010000fa8 ---------------- ----------------  ................\n"
527 "    0000000010000fb8 ---------------- ----------------  ................\n"
528 "    0000000010000fc8 ---------------- ----------------  ................\n"
529 "    0000000010000fd8 ---------------- ----------------  ................\n"
530 "    0000000010000fe8 ---------------- ----------------  ................\n"
531 "    0000000010000ff8 ---------------- 7f7e7d7c7b7a7978  ........xyz{|}~.\n"
532 "    0000000010001008 8786858483828180 8f8e8d8c8b8a8988  ................\n"
533 "    0000000010001018 9796959493929190 9f9e9d9c9b9a9998  ................\n"
534 "    0000000010001028 a7a6a5a4a3a2a1a0 afaeadacabaaa9a8  ................\n"
535 "    0000000010001038 b7b6b5b4b3b2b1b0 bfbebdbcbbbab9b8  ................\n"
536 "    0000000010001048 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
537 "    0000000010001058 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
538 "    0000000010001068 e7e6e5e4e3e2e1e0 efeeedecebeae9e8  ................\n"
539 "    0000000010001078 f7f6f5f4f3f2f1f0 fffefdfcfbfaf9f8  ................\n";
540 #else
541 "    10000f88 -------- -------- -------- --------  ................\n"
542 "    10000f98 -------- -------- -------- --------  ................\n"
543 "    10000fa8 -------- -------- -------- --------  ................\n"
544 "    10000fb8 -------- -------- -------- --------  ................\n"
545 "    10000fc8 -------- -------- -------- --------  ................\n"
546 "    10000fd8 -------- -------- -------- --------  ................\n"
547 "    10000fe8 -------- -------- -------- --------  ................\n"
548 "    10000ff8 -------- -------- 7b7a7978 7f7e7d7c  ........xyz{|}~.\n"
549 "    10001008 83828180 87868584 8b8a8988 8f8e8d8c  ................\n"
550 "    10001018 93929190 97969594 9b9a9998 9f9e9d9c  ................\n"
551 "    10001028 a3a2a1a0 a7a6a5a4 abaaa9a8 afaeadac  ................\n"
552 "    10001038 b3b2b1b0 b7b6b5b4 bbbab9b8 bfbebdbc  ................\n"
553 "    10001048 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
554 "    10001058 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
555 "    10001068 e3e2e1e0 e7e6e5e4 ebeae9e8 efeeedec  ................\n"
556 "    10001078 f3f2f1f0 f7f6f5f4 fbfaf9f8 fffefdfc  ................\n";
557 #endif
558   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
559 
560   // Verify that the log buf is empty, and no error messages.
561   ASSERT_STREQ("", getFakeLogBuf().c_str());
562   ASSERT_STREQ("", getFakeLogPrint().c_str());
563 }
564 
TEST_F(DumpMemoryTest,first_read_empty_second_read_stops)565 TEST_F(DumpMemoryTest, first_read_empty_second_read_stops) {
566   uint8_t buffer[224];
567   for (size_t i = 0; i < sizeof(buffer); i++) {
568     buffer[i] = i;
569   }
570   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
571   backtrace_mock_->SetPartialReadAmount(0);
572 
573   size_t page_size = sysconf(_SC_PAGE_SIZE);
574   uintptr_t addr = 0x10000020 + page_size - 192;
575   dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4");
576 
577   std::string tombstone_contents;
578   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
579   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
580   const char* expected_dump = \
581 "\nmemory near r4:\n"
582 #if defined(__LP64__)
583 "    0000000010000f40 ---------------- ----------------  ................\n"
584 "    0000000010000f50 ---------------- ----------------  ................\n"
585 "    0000000010000f60 ---------------- ----------------  ................\n"
586 "    0000000010000f70 ---------------- ----------------  ................\n"
587 "    0000000010000f80 ---------------- ----------------  ................\n"
588 "    0000000010000f90 ---------------- ----------------  ................\n"
589 "    0000000010000fa0 ---------------- ----------------  ................\n"
590 "    0000000010000fb0 ---------------- ----------------  ................\n"
591 "    0000000010000fc0 ---------------- ----------------  ................\n"
592 "    0000000010000fd0 ---------------- ----------------  ................\n"
593 "    0000000010000fe0 ---------------- ----------------  ................\n"
594 "    0000000010000ff0 ---------------- ----------------  ................\n"
595 "    0000000010001000 c7c6c5c4c3c2c1c0 cfcecdcccbcac9c8  ................\n"
596 "    0000000010001010 d7d6d5d4d3d2d1d0 dfdedddcdbdad9d8  ................\n"
597 "    0000000010001020 ---------------- ----------------  ................\n"
598 "    0000000010001030 ---------------- ----------------  ................\n";
599 #else
600 "    10000f40 -------- -------- -------- --------  ................\n"
601 "    10000f50 -------- -------- -------- --------  ................\n"
602 "    10000f60 -------- -------- -------- --------  ................\n"
603 "    10000f70 -------- -------- -------- --------  ................\n"
604 "    10000f80 -------- -------- -------- --------  ................\n"
605 "    10000f90 -------- -------- -------- --------  ................\n"
606 "    10000fa0 -------- -------- -------- --------  ................\n"
607 "    10000fb0 -------- -------- -------- --------  ................\n"
608 "    10000fc0 -------- -------- -------- --------  ................\n"
609 "    10000fd0 -------- -------- -------- --------  ................\n"
610 "    10000fe0 -------- -------- -------- --------  ................\n"
611 "    10000ff0 -------- -------- -------- --------  ................\n"
612 "    10001000 c3c2c1c0 c7c6c5c4 cbcac9c8 cfcecdcc  ................\n"
613 "    10001010 d3d2d1d0 d7d6d5d4 dbdad9d8 dfdedddc  ................\n"
614 "    10001020 -------- -------- -------- --------  ................\n"
615 "    10001030 -------- -------- -------- --------  ................\n";
616 #endif
617   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
618 
619   // Verify that the log buf is empty, and no error messages.
620   ASSERT_STREQ("", getFakeLogBuf().c_str());
621   ASSERT_STREQ("", getFakeLogPrint().c_str());
622 }
623 
TEST_F(DumpMemoryTest,first_read_empty_next_page_out_of_range)624 TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range) {
625   uint8_t buffer[256];
626   for (size_t i = 0; i < sizeof(buffer); i++) {
627     buffer[i] = i;
628   }
629   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
630   backtrace_mock_->SetPartialReadAmount(0);
631 
632   uintptr_t addr = 0x10000020;
633   dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4");
634 
635   std::string tombstone_contents;
636   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
637   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
638   const char* expected_dump = \
639 "\nmemory near r4:\n"
640 #if defined(__LP64__)
641 "    0000000010000000 ---------------- ----------------  ................\n"
642 "    0000000010000010 ---------------- ----------------  ................\n"
643 "    0000000010000020 ---------------- ----------------  ................\n"
644 "    0000000010000030 ---------------- ----------------  ................\n"
645 "    0000000010000040 ---------------- ----------------  ................\n"
646 "    0000000010000050 ---------------- ----------------  ................\n"
647 "    0000000010000060 ---------------- ----------------  ................\n"
648 "    0000000010000070 ---------------- ----------------  ................\n"
649 "    0000000010000080 ---------------- ----------------  ................\n"
650 "    0000000010000090 ---------------- ----------------  ................\n"
651 "    00000000100000a0 ---------------- ----------------  ................\n"
652 "    00000000100000b0 ---------------- ----------------  ................\n"
653 "    00000000100000c0 ---------------- ----------------  ................\n"
654 "    00000000100000d0 ---------------- ----------------  ................\n"
655 "    00000000100000e0 ---------------- ----------------  ................\n"
656 "    00000000100000f0 ---------------- ----------------  ................\n";
657 #else
658 "    10000000 -------- -------- -------- --------  ................\n"
659 "    10000010 -------- -------- -------- --------  ................\n"
660 "    10000020 -------- -------- -------- --------  ................\n"
661 "    10000030 -------- -------- -------- --------  ................\n"
662 "    10000040 -------- -------- -------- --------  ................\n"
663 "    10000050 -------- -------- -------- --------  ................\n"
664 "    10000060 -------- -------- -------- --------  ................\n"
665 "    10000070 -------- -------- -------- --------  ................\n"
666 "    10000080 -------- -------- -------- --------  ................\n"
667 "    10000090 -------- -------- -------- --------  ................\n"
668 "    100000a0 -------- -------- -------- --------  ................\n"
669 "    100000b0 -------- -------- -------- --------  ................\n"
670 "    100000c0 -------- -------- -------- --------  ................\n"
671 "    100000d0 -------- -------- -------- --------  ................\n"
672 "    100000e0 -------- -------- -------- --------  ................\n"
673 "    100000f0 -------- -------- -------- --------  ................\n";
674 #endif
675   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
676 
677   // Verify that the log buf is empty, and no error messages.
678   ASSERT_STREQ("", getFakeLogBuf().c_str());
679   ASSERT_STREQ("", getFakeLogPrint().c_str());
680 }
681 
TEST_F(DumpMemoryTest,first_read_empty_next_page_out_of_range_fence_post)682 TEST_F(DumpMemoryTest, first_read_empty_next_page_out_of_range_fence_post) {
683   uint8_t buffer[256];
684   for (size_t i = 0; i < sizeof(buffer); i++) {
685     buffer[i] = i;
686   }
687   backtrace_mock_->SetReadData(buffer, sizeof(buffer));
688   backtrace_mock_->SetPartialReadAmount(0);
689 
690   size_t page_size = sysconf(_SC_PAGE_SIZE);
691   uintptr_t addr = 0x10000020 + page_size - 256;
692 
693   dump_memory(&log_, backtrace_mock_.get(), addr, "memory near %.2s:", "r4");
694 
695   std::string tombstone_contents;
696   ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
697   ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &tombstone_contents));
698   const char* expected_dump = \
699 "\nmemory near r4:\n"
700 #if defined(__LP64__)
701 "    0000000010000f00 ---------------- ----------------  ................\n"
702 "    0000000010000f10 ---------------- ----------------  ................\n"
703 "    0000000010000f20 ---------------- ----------------  ................\n"
704 "    0000000010000f30 ---------------- ----------------  ................\n"
705 "    0000000010000f40 ---------------- ----------------  ................\n"
706 "    0000000010000f50 ---------------- ----------------  ................\n"
707 "    0000000010000f60 ---------------- ----------------  ................\n"
708 "    0000000010000f70 ---------------- ----------------  ................\n"
709 "    0000000010000f80 ---------------- ----------------  ................\n"
710 "    0000000010000f90 ---------------- ----------------  ................\n"
711 "    0000000010000fa0 ---------------- ----------------  ................\n"
712 "    0000000010000fb0 ---------------- ----------------  ................\n"
713 "    0000000010000fc0 ---------------- ----------------  ................\n"
714 "    0000000010000fd0 ---------------- ----------------  ................\n"
715 "    0000000010000fe0 ---------------- ----------------  ................\n"
716 "    0000000010000ff0 ---------------- ----------------  ................\n";
717 #else
718 "    10000f00 -------- -------- -------- --------  ................\n"
719 "    10000f10 -------- -------- -------- --------  ................\n"
720 "    10000f20 -------- -------- -------- --------  ................\n"
721 "    10000f30 -------- -------- -------- --------  ................\n"
722 "    10000f40 -------- -------- -------- --------  ................\n"
723 "    10000f50 -------- -------- -------- --------  ................\n"
724 "    10000f60 -------- -------- -------- --------  ................\n"
725 "    10000f70 -------- -------- -------- --------  ................\n"
726 "    10000f80 -------- -------- -------- --------  ................\n"
727 "    10000f90 -------- -------- -------- --------  ................\n"
728 "    10000fa0 -------- -------- -------- --------  ................\n"
729 "    10000fb0 -------- -------- -------- --------  ................\n"
730 "    10000fc0 -------- -------- -------- --------  ................\n"
731 "    10000fd0 -------- -------- -------- --------  ................\n"
732 "    10000fe0 -------- -------- -------- --------  ................\n"
733 "    10000ff0 -------- -------- -------- --------  ................\n";
734 #endif
735   ASSERT_STREQ(expected_dump, tombstone_contents.c_str());
736 
737   // Verify that the log buf is empty, and no error messages.
738   ASSERT_STREQ("", getFakeLogBuf().c_str());
739   ASSERT_STREQ("", getFakeLogPrint().c_str());
740 }
741