1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <inttypes.h>
18 #include <sys/mman.h>
19
20 #include <android-base/file.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/test_utils.h>
23 #include <gtest/gtest.h>
24
25 #include <unwindstack/Maps.h>
26
27 namespace unwindstack {
28
VerifyLine(std::string line,MapInfo * info)29 static void VerifyLine(std::string line, MapInfo* info) {
30 BufferMaps maps(line.c_str());
31
32 if (info == nullptr) {
33 ASSERT_FALSE(maps.Parse()) << "Failed on: " + line;
34 } else {
35 ASSERT_TRUE(maps.Parse()) << "Failed on: " + line;
36 MapInfo* element = maps.Get(0);
37 ASSERT_TRUE(element != nullptr) << "Failed on: " + line;
38 info->start = element->start;
39 info->end = element->end;
40 info->offset = element->offset;
41 info->flags = element->flags;
42 info->name = element->name;
43 info->elf_offset = element->elf_offset;
44 }
45 }
46
TEST(MapsTest,map_add)47 TEST(MapsTest, map_add) {
48 Maps maps;
49
50 maps.Add(0x1000, 0x2000, 0, PROT_READ, "fake_map", 0);
51 maps.Add(0x3000, 0x4000, 0x10, 0, "", 0x1234);
52 maps.Add(0x5000, 0x6000, 1, 2, "fake_map2", static_cast<uint64_t>(-1));
53
54 ASSERT_EQ(3U, maps.Total());
55 MapInfo* info = maps.Get(0);
56 ASSERT_EQ(0x1000U, info->start);
57 ASSERT_EQ(0x2000U, info->end);
58 ASSERT_EQ(0U, info->offset);
59 ASSERT_EQ(PROT_READ, info->flags);
60 ASSERT_EQ("fake_map", info->name);
61 ASSERT_EQ(0U, info->elf_offset);
62 ASSERT_EQ(0U, info->load_bias.load());
63 }
64
TEST(MapsTest,verify_parse_line)65 TEST(MapsTest, verify_parse_line) {
66 MapInfo info;
67
68 VerifyLine("01-02 rwxp 03 04:05 06\n", &info);
69 EXPECT_EQ(1U, info.start);
70 EXPECT_EQ(2U, info.end);
71 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info.flags);
72 EXPECT_EQ(3U, info.offset);
73 EXPECT_EQ("", info.name);
74
75 VerifyLine("0a-0b ---s 0c 0d:0e 06 /fake/name\n", &info);
76 EXPECT_EQ(0xaU, info.start);
77 EXPECT_EQ(0xbU, info.end);
78 EXPECT_EQ(0U, info.flags);
79 EXPECT_EQ(0xcU, info.offset);
80 EXPECT_EQ("/fake/name", info.name);
81
82 VerifyLine("01-02 rwxp 03 04:05 06 /fake/name/again\n", &info);
83 EXPECT_EQ(1U, info.start);
84 EXPECT_EQ(2U, info.end);
85 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info.flags);
86 EXPECT_EQ(3U, info.offset);
87 EXPECT_EQ("/fake/name/again", info.name);
88
89 VerifyLine("-00 rwxp 00 00:00 0\n", nullptr);
90 VerifyLine("00- rwxp 00 00:00 0\n", nullptr);
91 VerifyLine("00-00 rwxp 00 :00 0\n", nullptr);
92 VerifyLine("00-00 rwxp 00 00:00 \n", nullptr);
93 VerifyLine("x-00 rwxp 00 00:00 0\n", nullptr);
94 VerifyLine("00 -00 rwxp 00 00:00 0\n", nullptr);
95 VerifyLine("00-x rwxp 00 00:00 0\n", nullptr);
96 VerifyLine("00-x rwxp 00 00:00 0\n", nullptr);
97 VerifyLine("00-00x rwxp 00 00:00 0\n", nullptr);
98 VerifyLine("00-00 rwxp0 00 00:00 0\n", nullptr);
99 VerifyLine("00-00 rwxp0 00 00:00 0\n", nullptr);
100 VerifyLine("00-00 rwp 00 00:00 0\n", nullptr);
101 VerifyLine("00-00 rwxp 0000:00 0\n", nullptr);
102 VerifyLine("00-00 rwxp 00 00 :00 0\n", nullptr);
103 VerifyLine("00-00 rwxp 00 00: 00 0\n", nullptr);
104 VerifyLine("00-00 rwxp 00 00:000\n", nullptr);
105 VerifyLine("00-00 rwxp 00 00:00 0/fake\n", nullptr);
106 VerifyLine("00-00 xxxx 00 00:00 0 /fake\n", nullptr);
107 VerifyLine("00-00 ywxp 00 00:00 0 /fake\n", nullptr);
108 VerifyLine("00-00 ryxp 00 00:00 0 /fake\n", nullptr);
109 VerifyLine("00-00 rwyp 00 00:00 0 /fake\n", nullptr);
110 VerifyLine("00-00 rwx- 00 00:00 0 /fake\n", nullptr);
111 VerifyLine("0\n", nullptr);
112 VerifyLine("00\n", nullptr);
113 VerifyLine("00-\n", nullptr);
114 VerifyLine("00-0\n", nullptr);
115 VerifyLine("00-00\n", nullptr);
116 VerifyLine("00-00 \n", nullptr);
117 VerifyLine("00-00 -\n", nullptr);
118 VerifyLine("00-00 r\n", nullptr);
119 VerifyLine("00-00 --\n", nullptr);
120 VerifyLine("00-00 rw\n", nullptr);
121 VerifyLine("00-00 ---\n", nullptr);
122 VerifyLine("00-00 rwx\n", nullptr);
123 VerifyLine("00-00 ---s\n", nullptr);
124 VerifyLine("00-00 ---p\n", nullptr);
125 VerifyLine("00-00 ---s 0\n", nullptr);
126 VerifyLine("00-00 ---p 0 \n", nullptr);
127 VerifyLine("00-00 ---p 0 0\n", nullptr);
128 VerifyLine("00-00 ---p 0 0:\n", nullptr);
129 VerifyLine("00-00 ---p 0 0:0\n", nullptr);
130 VerifyLine("00-00 ---p 0 0:0 \n", nullptr);
131
132 // Line to verify that the parser will detect a completely malformed line
133 // properly.
134 VerifyLine("7ffff7dda000-7ffff7dfd7ffff7ff3000-7ffff7ff4000 ---p 0000f000 fc:02 44171565\n",
135 nullptr);
136 }
137
TEST(MapsTest,verify_large_values)138 TEST(MapsTest, verify_large_values) {
139 MapInfo info;
140 #if defined(__LP64__)
141 VerifyLine("fabcdef012345678-f12345678abcdef8 rwxp f0b0d0f010305070 00:00 0\n", &info);
142 EXPECT_EQ(0xfabcdef012345678UL, info.start);
143 EXPECT_EQ(0xf12345678abcdef8UL, info.end);
144 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info.flags);
145 EXPECT_EQ(0xf0b0d0f010305070UL, info.offset);
146 #else
147 VerifyLine("f2345678-fabcdef8 rwxp f0305070 00:00 0\n", &info);
148 EXPECT_EQ(0xf2345678UL, info.start);
149 EXPECT_EQ(0xfabcdef8UL, info.end);
150 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info.flags);
151 EXPECT_EQ(0xf0305070UL, info.offset);
152 #endif
153 }
154
TEST(MapsTest,parse_permissions)155 TEST(MapsTest, parse_permissions) {
156 BufferMaps maps(
157 "1000-2000 ---s 00000000 00:00 0\n"
158 "2000-3000 r--s 00000000 00:00 0\n"
159 "3000-4000 -w-s 00000000 00:00 0\n"
160 "4000-5000 --xp 00000000 00:00 0\n"
161 "5000-6000 rwxp 00000000 00:00 0\n");
162
163 ASSERT_TRUE(maps.Parse());
164 ASSERT_EQ(5U, maps.Total());
165
166 MapInfo* info = maps.Get(0);
167 ASSERT_TRUE(info != nullptr);
168 EXPECT_EQ(PROT_NONE, info->flags);
169 EXPECT_EQ(0x1000U, info->start);
170 EXPECT_EQ(0x2000U, info->end);
171 EXPECT_EQ(0U, info->offset);
172 EXPECT_EQ("", info->name);
173
174 info = maps.Get(1);
175 ASSERT_TRUE(info != nullptr);
176 EXPECT_EQ(PROT_READ, info->flags);
177 EXPECT_EQ(0x2000U, info->start);
178 EXPECT_EQ(0x3000U, info->end);
179 EXPECT_EQ(0U, info->offset);
180 EXPECT_EQ("", info->name);
181
182 info = maps.Get(2);
183 ASSERT_TRUE(info != nullptr);
184 EXPECT_EQ(PROT_WRITE, info->flags);
185 EXPECT_EQ(0x3000U, info->start);
186 EXPECT_EQ(0x4000U, info->end);
187 EXPECT_EQ(0U, info->offset);
188 EXPECT_EQ("", info->name);
189
190 info = maps.Get(3);
191 ASSERT_TRUE(info != nullptr);
192 EXPECT_EQ(PROT_EXEC, info->flags);
193 EXPECT_EQ(0x4000U, info->start);
194 EXPECT_EQ(0x5000U, info->end);
195 EXPECT_EQ(0U, info->offset);
196 EXPECT_EQ("", info->name);
197
198 info = maps.Get(4);
199 ASSERT_TRUE(info != nullptr);
200 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info->flags);
201 EXPECT_EQ(0x5000U, info->start);
202 EXPECT_EQ(0x6000U, info->end);
203 EXPECT_EQ(0U, info->offset);
204 EXPECT_EQ("", info->name);
205
206 ASSERT_TRUE(maps.Get(5) == nullptr);
207 }
208
TEST(MapsTest,parse_name)209 TEST(MapsTest, parse_name) {
210 BufferMaps maps(
211 "7b29b000-7b29e000 rw-p 00000000 00:00 0\n"
212 "7b29e000-7b29f000 rw-p 00000000 00:00 0 /system/lib/fake.so\n"
213 "7b29f000-7b2a0000 rw-p 00000000 00:00 0");
214
215 ASSERT_TRUE(maps.Parse());
216 ASSERT_EQ(3U, maps.Total());
217
218 MapInfo* info = maps.Get(0);
219 ASSERT_TRUE(info != nullptr);
220 EXPECT_EQ("", info->name);
221 EXPECT_EQ(0x7b29b000U, info->start);
222 EXPECT_EQ(0x7b29e000U, info->end);
223 EXPECT_EQ(0U, info->offset);
224 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
225
226 info = maps.Get(1);
227 ASSERT_TRUE(info != nullptr);
228 EXPECT_EQ("/system/lib/fake.so", info->name);
229 EXPECT_EQ(0x7b29e000U, info->start);
230 EXPECT_EQ(0x7b29f000U, info->end);
231 EXPECT_EQ(0U, info->offset);
232 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
233
234 info = maps.Get(2);
235 ASSERT_TRUE(info != nullptr);
236 EXPECT_EQ("", info->name);
237 EXPECT_EQ(0x7b29f000U, info->start);
238 EXPECT_EQ(0x7b2a0000U, info->end);
239 EXPECT_EQ(0U, info->offset);
240 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
241
242 ASSERT_TRUE(maps.Get(3) == nullptr);
243 }
244
TEST(MapsTest,parse_offset)245 TEST(MapsTest, parse_offset) {
246 BufferMaps maps(
247 "a000-e000 rw-p 00000000 00:00 0 /system/lib/fake.so\n"
248 "e000-f000 rw-p 00a12345 00:00 0 /system/lib/fake.so\n");
249
250 ASSERT_TRUE(maps.Parse());
251 ASSERT_EQ(2U, maps.Total());
252
253 MapInfo* info = maps.Get(0);
254 ASSERT_TRUE(info != nullptr);
255 EXPECT_EQ(0U, info->offset);
256 EXPECT_EQ(0xa000U, info->start);
257 EXPECT_EQ(0xe000U, info->end);
258 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
259 EXPECT_EQ("/system/lib/fake.so", info->name);
260
261 info = maps.Get(1);
262 ASSERT_TRUE(info != nullptr);
263 EXPECT_EQ(0xa12345U, info->offset);
264 EXPECT_EQ(0xe000U, info->start);
265 EXPECT_EQ(0xf000U, info->end);
266 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
267 EXPECT_EQ("/system/lib/fake.so", info->name);
268
269 ASSERT_TRUE(maps.Get(2) == nullptr);
270 }
271
TEST(MapsTest,iterate)272 TEST(MapsTest, iterate) {
273 BufferMaps maps(
274 "a000-e000 rw-p 00000000 00:00 0 /system/lib/fake.so\n"
275 "e000-f000 rw-p 00a12345 00:00 0 /system/lib/fake.so\n");
276
277 ASSERT_TRUE(maps.Parse());
278 ASSERT_EQ(2U, maps.Total());
279
280 Maps::iterator it = maps.begin();
281 EXPECT_EQ(0xa000U, (*it)->start);
282 EXPECT_EQ(0xe000U, (*it)->end);
283 ++it;
284 EXPECT_EQ(0xe000U, (*it)->start);
285 EXPECT_EQ(0xf000U, (*it)->end);
286 ++it;
287 EXPECT_EQ(maps.end(), it);
288 }
289
TEST(MapsTest,const_iterate)290 TEST(MapsTest, const_iterate) {
291 BufferMaps maps(
292 "a000-e000 rw-p 00000000 00:00 0 /system/lib/fake.so\n"
293 "e000-f000 rw-p 00a12345 00:00 0 /system/lib/fake.so\n");
294
295 ASSERT_TRUE(maps.Parse());
296 ASSERT_EQ(2U, maps.Total());
297
298 Maps::const_iterator it = maps.begin();
299 EXPECT_EQ(0xa000U, (*it)->start);
300 EXPECT_EQ(0xe000U, (*it)->end);
301 ++it;
302 EXPECT_EQ(0xe000U, (*it)->start);
303 EXPECT_EQ(0xf000U, (*it)->end);
304 ++it;
305 EXPECT_EQ(maps.end(), it);
306 }
307
TEST(MapsTest,device)308 TEST(MapsTest, device) {
309 BufferMaps maps(
310 "a000-e000 rw-p 00000000 00:00 0 /dev/\n"
311 "f000-f100 rw-p 00000000 00:00 0 /dev/does_not_exist\n"
312 "f100-f200 rw-p 00000000 00:00 0 /dev/ashmem/does_not_exist\n"
313 "f200-f300 rw-p 00000000 00:00 0 /devsomething/does_not_exist\n");
314
315 ASSERT_TRUE(maps.Parse());
316 ASSERT_EQ(4U, maps.Total());
317
318 MapInfo* info = maps.Get(0);
319 ASSERT_TRUE(info != nullptr);
320 EXPECT_TRUE(info->flags & 0x8000);
321 EXPECT_EQ("/dev/", info->name);
322
323 info = maps.Get(1);
324 EXPECT_TRUE(info->flags & 0x8000);
325 EXPECT_EQ("/dev/does_not_exist", info->name);
326
327 info = maps.Get(2);
328 EXPECT_FALSE(info->flags & 0x8000);
329 EXPECT_EQ("/dev/ashmem/does_not_exist", info->name);
330
331 info = maps.Get(3);
332 EXPECT_FALSE(info->flags & 0x8000);
333 EXPECT_EQ("/devsomething/does_not_exist", info->name);
334 }
335
TEST(MapsTest,file_smoke)336 TEST(MapsTest, file_smoke) {
337 TemporaryFile tf;
338 ASSERT_TRUE(tf.fd != -1);
339
340 ASSERT_TRUE(
341 android::base::WriteStringToFile("7b29b000-7b29e000 r-xp a0000000 00:00 0 /fake.so\n"
342 "7b2b0000-7b2e0000 r-xp b0000000 00:00 0 /fake2.so\n"
343 "7b2e0000-7b2f0000 r-xp c0000000 00:00 0 /fake3.so\n",
344 tf.path, 0660, getuid(), getgid()));
345
346 FileMaps maps(tf.path);
347
348 ASSERT_TRUE(maps.Parse());
349 ASSERT_EQ(3U, maps.Total());
350
351 MapInfo* info = maps.Get(0);
352 ASSERT_TRUE(info != nullptr);
353 EXPECT_EQ(0x7b29b000U, info->start);
354 EXPECT_EQ(0x7b29e000U, info->end);
355 EXPECT_EQ(0xa0000000U, info->offset);
356 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
357 EXPECT_EQ("/fake.so", info->name);
358
359 info = maps.Get(1);
360 ASSERT_TRUE(info != nullptr);
361 EXPECT_EQ(0x7b2b0000U, info->start);
362 EXPECT_EQ(0x7b2e0000U, info->end);
363 EXPECT_EQ(0xb0000000U, info->offset);
364 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
365 EXPECT_EQ("/fake2.so", info->name);
366
367 info = maps.Get(2);
368 ASSERT_TRUE(info != nullptr);
369 EXPECT_EQ(0x7b2e0000U, info->start);
370 EXPECT_EQ(0x7b2f0000U, info->end);
371 EXPECT_EQ(0xc0000000U, info->offset);
372 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
373 EXPECT_EQ("/fake3.so", info->name);
374
375 ASSERT_TRUE(maps.Get(3) == nullptr);
376 }
377
TEST(MapsTest,file_no_map_name)378 TEST(MapsTest, file_no_map_name) {
379 TemporaryFile tf;
380 ASSERT_TRUE(tf.fd != -1);
381
382 ASSERT_TRUE(
383 android::base::WriteStringToFile("7b29b000-7b29e000 r-xp a0000000 00:00 0\n"
384 "7b2b0000-7b2e0000 r-xp b0000000 00:00 0 /fake2.so\n"
385 "7b2e0000-7b2f0000 r-xp c0000000 00:00 0 \n",
386 tf.path, 0660, getuid(), getgid()));
387
388 FileMaps maps(tf.path);
389
390 ASSERT_TRUE(maps.Parse());
391 ASSERT_EQ(3U, maps.Total());
392
393 MapInfo* info = maps.Get(0);
394 ASSERT_TRUE(info != nullptr);
395 EXPECT_EQ(0x7b29b000U, info->start);
396 EXPECT_EQ(0x7b29e000U, info->end);
397 EXPECT_EQ(0xa0000000U, info->offset);
398 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
399 EXPECT_EQ("", info->name);
400
401 info = maps.Get(1);
402 ASSERT_TRUE(info != nullptr);
403 EXPECT_EQ(0x7b2b0000U, info->start);
404 EXPECT_EQ(0x7b2e0000U, info->end);
405 EXPECT_EQ(0xb0000000U, info->offset);
406 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
407 EXPECT_EQ("/fake2.so", info->name);
408
409 info = maps.Get(2);
410 ASSERT_TRUE(info != nullptr);
411 EXPECT_EQ(0x7b2e0000U, info->start);
412 EXPECT_EQ(0x7b2f0000U, info->end);
413 EXPECT_EQ(0xc0000000U, info->offset);
414 EXPECT_EQ(PROT_READ | PROT_EXEC, info->flags);
415 EXPECT_EQ("", info->name);
416
417 ASSERT_TRUE(maps.Get(3) == nullptr);
418 }
419
420 // Verify that a file that crosses a buffer is parsed correctly.
CreateEntry(size_t index)421 static std::string CreateEntry(size_t index) {
422 return android::base::StringPrintf("%08zx-%08zx rwxp 0000 00:00 0\n", index * 4096,
423 (index + 1) * 4096);
424 }
425
TEST(MapsTest,file_buffer_cross)426 TEST(MapsTest, file_buffer_cross) {
427 constexpr size_t kBufferSize = 2048;
428 TemporaryFile tf;
429 ASSERT_TRUE(tf.fd != -1);
430
431 // Compute how many to add in the first buffer.
432 size_t entry_len = CreateEntry(0).size();
433 size_t index;
434 std::string file_data;
435 for (index = 0; index < kBufferSize / entry_len; index++) {
436 file_data += CreateEntry(index);
437 }
438 // Add a long name to make sure that the first buffer does not contain a
439 // complete line.
440 // Remove the last newline.
441 size_t extra = 0;
442 size_t leftover = kBufferSize % entry_len;
443 size_t overlap1_index = 0;
444 std::string overlap1_name;
445 if (leftover == 0) {
446 // Exact match, add a long name to cross over the value.
447 overlap1_name = "/fake/name/is/long/on/purpose";
448 file_data.erase(file_data.size() - 1);
449 file_data += ' ' + overlap1_name + '\n';
450 extra = entry_len + overlap1_name.size() + 1;
451 overlap1_index = index;
452 }
453
454 // Compute how many need to go in to hit the buffer boundary exactly.
455 size_t bytes_left_in_buffer = kBufferSize - extra;
456 size_t entries_to_add = bytes_left_in_buffer / entry_len + index;
457 for (; index < entries_to_add; index++) {
458 file_data += CreateEntry(index);
459 }
460
461 // Now figure out how many bytes to add to get exactly to the buffer boundary.
462 leftover = bytes_left_in_buffer % entry_len;
463 std::string overlap2_name;
464 size_t overlap2_index = 0;
465 if (leftover != 0) {
466 file_data.erase(file_data.size() - 1);
467 file_data += ' ';
468 overlap2_name = std::string(leftover - 1, 'x');
469 file_data += overlap2_name + '\n';
470 overlap2_index = index - 1;
471 }
472
473 // Now add a few entries on the next page.
474 for (size_t start = index; index < start + 10; index++) {
475 file_data += CreateEntry(index);
476 }
477
478 ASSERT_TRUE(android::base::WriteStringToFile(file_data, tf.path, 0660, getuid(), getgid()));
479
480 FileMaps maps(tf.path);
481 ASSERT_TRUE(maps.Parse());
482 EXPECT_EQ(index, maps.Total());
483 // Verify all of the maps.
484 for (size_t i = 0; i < index; i++) {
485 MapInfo* info = maps.Get(i);
486 ASSERT_TRUE(info != nullptr) << "Failed verifying index " + std::to_string(i);
487 EXPECT_EQ(i * 4096, info->start) << "Failed verifying index " + std::to_string(i);
488 EXPECT_EQ((i + 1) * 4096, info->end) << "Failed verifying index " + std::to_string(i);
489 EXPECT_EQ(0U, info->offset) << "Failed verifying index " + std::to_string(i);
490 if (overlap1_index != 0 && i == overlap1_index) {
491 EXPECT_EQ(overlap1_name, info->name) << "Failed verifying overlap1 name " + std::to_string(i);
492 } else if (overlap2_index != 0 && i == overlap2_index) {
493 EXPECT_EQ(overlap2_name, info->name) << "Failed verifying overlap2 name " + std::to_string(i);
494 } else {
495 EXPECT_EQ("", info->name) << "Failed verifying index " + std::to_string(i);
496 }
497 }
498 }
499
TEST(MapsTest,file_should_fail)500 TEST(MapsTest, file_should_fail) {
501 TemporaryFile tf;
502 ASSERT_TRUE(tf.fd != -1);
503
504 ASSERT_TRUE(android::base::WriteStringToFile(
505 "7ffff7dda000-7ffff7dfd7ffff7ff3000-7ffff7ff4000 ---p 0000f000 fc:02 44171565\n", tf.path,
506 0660, getuid(), getgid()));
507
508 FileMaps maps(tf.path);
509
510 ASSERT_FALSE(maps.Parse());
511 }
512
513 // Create a maps file that is extremely large.
TEST(MapsTest,large_file)514 TEST(MapsTest, large_file) {
515 TemporaryFile tf;
516 ASSERT_TRUE(tf.fd != -1);
517
518 std::string file_data;
519 uint64_t start = 0x700000;
520 for (size_t i = 0; i < 5000; i++) {
521 file_data +=
522 android::base::StringPrintf("%" PRIx64 "-%" PRIx64 " r-xp 1000 00:0 0 /fake%zu.so\n",
523 start + i * 4096, start + (i + 1) * 4096, i);
524 }
525
526 ASSERT_TRUE(android::base::WriteStringToFile(file_data, tf.path, 0660, getuid(), getgid()));
527
528 FileMaps maps(tf.path);
529
530 ASSERT_TRUE(maps.Parse());
531 ASSERT_EQ(5000U, maps.Total());
532 for (size_t i = 0; i < 5000; i++) {
533 MapInfo* info = maps.Get(i);
534 EXPECT_EQ(start + i * 4096, info->start) << "Failed at map " + std::to_string(i);
535 EXPECT_EQ(start + (i + 1) * 4096, info->end) << "Failed at map " + std::to_string(i);
536 std::string name = "/fake" + std::to_string(i) + ".so";
537 EXPECT_EQ(name, info->name) << "Failed at map " + std::to_string(i);
538 }
539 }
540
TEST(MapsTest,find)541 TEST(MapsTest, find) {
542 BufferMaps maps(
543 "1000-2000 r--p 00000010 00:00 0 /system/lib/fake1.so\n"
544 "3000-4000 -w-p 00000020 00:00 0 /system/lib/fake2.so\n"
545 "6000-8000 --xp 00000030 00:00 0 /system/lib/fake3.so\n"
546 "a000-b000 rw-p 00000040 00:00 0 /system/lib/fake4.so\n"
547 "e000-f000 rwxp 00000050 00:00 0 /system/lib/fake5.so\n");
548 ASSERT_TRUE(maps.Parse());
549 ASSERT_EQ(5U, maps.Total());
550
551 EXPECT_TRUE(maps.Find(0x500) == nullptr);
552 EXPECT_TRUE(maps.Find(0x2000) == nullptr);
553 EXPECT_TRUE(maps.Find(0x5010) == nullptr);
554 EXPECT_TRUE(maps.Find(0x9a00) == nullptr);
555 EXPECT_TRUE(maps.Find(0xf000) == nullptr);
556 EXPECT_TRUE(maps.Find(0xf010) == nullptr);
557
558 MapInfo* info = maps.Find(0x1000);
559 ASSERT_TRUE(info != nullptr);
560 EXPECT_EQ(0x1000U, info->start);
561 EXPECT_EQ(0x2000U, info->end);
562 EXPECT_EQ(0x10U, info->offset);
563 EXPECT_EQ(PROT_READ, info->flags);
564 EXPECT_EQ("/system/lib/fake1.so", info->name);
565
566 info = maps.Find(0x3020);
567 ASSERT_TRUE(info != nullptr);
568 EXPECT_EQ(0x3000U, info->start);
569 EXPECT_EQ(0x4000U, info->end);
570 EXPECT_EQ(0x20U, info->offset);
571 EXPECT_EQ(PROT_WRITE, info->flags);
572 EXPECT_EQ("/system/lib/fake2.so", info->name);
573
574 info = maps.Find(0x6020);
575 ASSERT_TRUE(info != nullptr);
576 EXPECT_EQ(0x6000U, info->start);
577 EXPECT_EQ(0x8000U, info->end);
578 EXPECT_EQ(0x30U, info->offset);
579 EXPECT_EQ(PROT_EXEC, info->flags);
580 EXPECT_EQ("/system/lib/fake3.so", info->name);
581
582 info = maps.Find(0xafff);
583 ASSERT_TRUE(info != nullptr);
584 EXPECT_EQ(0xa000U, info->start);
585 EXPECT_EQ(0xb000U, info->end);
586 EXPECT_EQ(0x40U, info->offset);
587 EXPECT_EQ(PROT_READ | PROT_WRITE, info->flags);
588 EXPECT_EQ("/system/lib/fake4.so", info->name);
589
590 info = maps.Find(0xe500);
591 ASSERT_TRUE(info != nullptr);
592 EXPECT_EQ(0xe000U, info->start);
593 EXPECT_EQ(0xf000U, info->end);
594 EXPECT_EQ(0x50U, info->offset);
595 EXPECT_EQ(PROT_READ | PROT_WRITE | PROT_EXEC, info->flags);
596 EXPECT_EQ("/system/lib/fake5.so", info->name);
597 }
598
599 } // namespace unwindstack
600