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