1 /*
2  * Copyright (C) 2018 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 <procinfo/process_map.h>
18 
19 #include <inttypes.h>
20 #include <sys/mman.h>
21 #include <unistd.h>
22 
23 #include <string>
24 #include <vector>
25 
26 #include <android-base/file.h>
27 #include <android-base/stringprintf.h>
28 
29 #include <gtest/gtest.h>
30 
TEST(process_map,ReadMapFile)31 TEST(process_map, ReadMapFile) {
32   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
33   std::vector<android::procinfo::MapInfo> maps;
34   ASSERT_TRUE(android::procinfo::ReadMapFile(
35       map_file, [&](const android::procinfo::MapInfo& mapinfo) { maps.emplace_back(mapinfo); }));
36   ASSERT_EQ(2043u, maps.size());
37   ASSERT_EQ(maps[0].start, 0x12c00000ULL);
38   ASSERT_EQ(maps[0].end, 0x2ac00000ULL);
39   ASSERT_EQ(maps[0].flags, PROT_READ | PROT_WRITE);
40   ASSERT_EQ(maps[0].pgoff, 0ULL);
41   ASSERT_EQ(maps[0].inode, 10267643UL);
42   ASSERT_EQ(maps[0].name, "[anon:dalvik-main space (region space)]");
43   ASSERT_EQ(maps[876].start, 0x70e6c4f000ULL);
44   ASSERT_EQ(maps[876].end, 0x70e6c6b000ULL);
45   ASSERT_EQ(maps[876].flags, PROT_READ | PROT_EXEC);
46   ASSERT_EQ(maps[876].pgoff, 0ULL);
47   ASSERT_EQ(maps[876].inode, 2407UL);
48   ASSERT_EQ(maps[876].name, "/system/lib64/libutils.so");
49   ASSERT_EQ(maps[1260].start, 0x70e96fa000ULL);
50   ASSERT_EQ(maps[1260].end, 0x70e96fb000ULL);
51   ASSERT_EQ(maps[1260].flags, PROT_READ);
52   ASSERT_EQ(maps[1260].pgoff, 0ULL);
53   ASSERT_EQ(maps[1260].inode, 10266154UL);
54   ASSERT_EQ(maps[1260].name,
55             "[anon:dalvik-classes.dex extracted in memory from "
56             "/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk]");
57 }
58 
TEST(process_map,ReadProcessMaps)59 TEST(process_map, ReadProcessMaps) {
60   std::vector<android::procinfo::MapInfo> maps;
61   ASSERT_TRUE(android::procinfo::ReadProcessMaps(
62       getpid(), [&](const android::procinfo::MapInfo& mapinfo) { maps.emplace_back(mapinfo); }));
63   ASSERT_GT(maps.size(), 0u);
64   maps.clear();
65   ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
66   ASSERT_GT(maps.size(), 0u);
67 }
68 
69 extern "C" void malloc_disable();
70 extern "C" void malloc_enable();
71 
72 struct TestMapInfo {
73   TestMapInfo() = default;
TestMapInfoTestMapInfo74   TestMapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
75               const char* new_name, bool isShared)
76       : start(start), end(end), flags(flags), pgoff(pgoff), inode(inode), isShared(isShared) {
77     strcpy(name, new_name);
78   }
79   uint64_t start = 0;
80   uint64_t end = 0;
81   uint16_t flags = 0;
82   uint64_t pgoff = 0;
83   ino_t inode = 0;
84   char name[100] = {};
85   bool isShared = false;
86 };
87 
VerifyReadMapFileAsyncSafe(const char * maps_data,const std::vector<TestMapInfo> & expected_info)88 void VerifyReadMapFileAsyncSafe(const char* maps_data,
89                                 const std::vector<TestMapInfo>& expected_info) {
90   TemporaryFile tf;
91   ASSERT_TRUE(android::base::WriteStringToFd(maps_data, tf.fd));
92 
93   std::vector<TestMapInfo> saved_info(expected_info.size());
94   size_t num_maps = 0;
95 
96   auto callback = [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
97                       const char* name, bool shared) {
98     if (num_maps != saved_info.size()) {
99       TestMapInfo& saved = saved_info[num_maps];
100       saved.start = start;
101       saved.end = end;
102       saved.flags = flags;
103       saved.pgoff = pgoff;
104       saved.inode = inode;
105       strcpy(saved.name, name);
106       saved.isShared = shared;
107     }
108     num_maps++;
109   };
110 
111   std::vector<char> buffer(64 * 1024);
112 
113 #if defined(__BIONIC__)
114   // Any allocations will block after this call.
115   malloc_disable();
116 #endif
117 
118   bool parsed =
119       android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer.data(), buffer.size(), callback);
120 
121 #if defined(__BIONIC__)
122   malloc_enable();
123 #endif
124 
125   ASSERT_TRUE(parsed) << "Parsing of data failed:\n" << maps_data;
126   ASSERT_EQ(expected_info.size(), num_maps);
127   for (size_t i = 0; i < expected_info.size(); i++) {
128     const TestMapInfo& expected = expected_info[i];
129     const TestMapInfo& saved = saved_info[i];
130     EXPECT_EQ(expected.start, saved.start);
131     EXPECT_EQ(expected.end, saved.end);
132     EXPECT_EQ(expected.flags, saved.flags);
133     EXPECT_EQ(expected.pgoff, saved.pgoff);
134     EXPECT_EQ(expected.inode, saved.inode);
135     EXPECT_STREQ(expected.name, saved.name);
136     EXPECT_EQ(expected.isShared, saved.isShared);
137   }
138 }
139 
TEST(process_map,ReadMapFileAsyncSafe_invalid)140 TEST(process_map, ReadMapFileAsyncSafe_invalid) {
141   std::vector<TestMapInfo> expected_info;
142 
143   VerifyReadMapFileAsyncSafe("12c00000-2ac00000", expected_info);
144 }
145 
TEST(process_map,ReadMapFileAsyncSafe_single)146 TEST(process_map, ReadMapFileAsyncSafe_single) {
147   std::vector<TestMapInfo> expected_info;
148   expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
149                              "/lib/fake.so", false);
150 
151   VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so",
152                              expected_info);
153 }
154 
TEST(process_map,ReadMapFileAsyncSafe_single_with_newline)155 TEST(process_map, ReadMapFileAsyncSafe_single_with_newline) {
156   std::vector<TestMapInfo> expected_info;
157   expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
158                              "/lib/fake.so", false);
159 
160   VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so\n",
161                              expected_info);
162 }
163 
TEST(process_map,ReadMapFileAsyncSafe_single_no_library)164 TEST(process_map, ReadMapFileAsyncSafe_single_no_library) {
165   std::vector<TestMapInfo> expected_info;
166   expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 0xb00, 101, "",
167                              false);
168 
169   VerifyReadMapFileAsyncSafe("a0000-c0000 rwxp 00000b00 00:05 101", expected_info);
170 }
171 
TEST(process_map,ReadMapFileAsyncSafe_multiple)172 TEST(process_map, ReadMapFileAsyncSafe_multiple) {
173   std::vector<TestMapInfo> expected_info;
174   expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 1, 100, "",
175                              false);
176   expected_info.emplace_back(0xd0000, 0xe0000, PROT_READ, 2, 101, "/lib/libsomething1.so", false);
177   expected_info.emplace_back(0xf0000, 0x100000, PROT_WRITE, 3, 102, "/lib/libsomething2.so", false);
178   expected_info.emplace_back(0x110000, 0x120000, PROT_EXEC, 4, 103, "[anon:something or another]",
179                              false);
180   expected_info.emplace_back(0x130000, 0x140000, PROT_READ, 5, 104, "/lib/libsomething3.so", true);
181 
182   std::string map_data =
183       "0a0000-0c0000 rwxp 00000001 00:05 100\n"
184       "0d0000-0e0000 r--p 00000002 00:05 101  /lib/libsomething1.so\n"
185       "0f0000-100000 -w-p 00000003 00:05 102  /lib/libsomething2.so\n"
186       "110000-120000 --xp 00000004 00:05 103  [anon:something or another]\n"
187       "130000-140000 r--s 00000005 00:05 104  /lib/libsomething3.so\n";
188 
189   VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
190 }
191 
TEST(process_map,ReadMapFileAsyncSafe_multiple_reads)192 TEST(process_map, ReadMapFileAsyncSafe_multiple_reads) {
193   std::vector<TestMapInfo> expected_info;
194   std::string map_data;
195   uint64_t start = 0xa0000;
196   for (size_t i = 0; i < 10000; i++) {
197     map_data += android::base::StringPrintf("%" PRIx64 "-%" PRIx64 " r--p %zx 01:20 %zu fake.so\n",
198                                             start, start + 0x1000, i, 1000 + i);
199     expected_info.emplace_back(start, start + 0x1000, PROT_READ, i, 1000 + i, "fake.so", false);
200   }
201 
202   VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
203 }
204 
TEST(process_map,ReadMapFileAsyncSafe_buffer_nullptr)205 TEST(process_map, ReadMapFileAsyncSafe_buffer_nullptr) {
206   size_t num_calls = 0;
207   auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
208 
209 #if defined(__BIONIC__)
210   // Any allocations will block after this call.
211   malloc_disable();
212 #endif
213 
214   bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", nullptr, 10, callback);
215 
216 #if defined(__BIONIC__)
217   malloc_enable();
218 #endif
219 
220   ASSERT_FALSE(parsed);
221   EXPECT_EQ(0UL, num_calls);
222 }
223 
TEST(process_map,ReadMapFileAsyncSafe_buffer_size_zero)224 TEST(process_map, ReadMapFileAsyncSafe_buffer_size_zero) {
225   size_t num_calls = 0;
226   auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
227 
228 #if defined(__BIONIC__)
229   // Any allocations will block after this call.
230   malloc_disable();
231 #endif
232 
233   char buffer[10];
234   bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, 0, callback);
235 
236 #if defined(__BIONIC__)
237   malloc_enable();
238 #endif
239 
240   ASSERT_FALSE(parsed);
241   EXPECT_EQ(0UL, num_calls);
242 }
243 
TEST(process_map,ReadMapFileAsyncSafe_buffer_too_small_no_calls)244 TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_no_calls) {
245   size_t num_calls = 0;
246   auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
247 
248 #if defined(__BIONIC__)
249   // Any allocations will block after this call.
250   malloc_disable();
251 #endif
252 
253   char buffer[10];
254   bool parsed =
255       android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, sizeof(buffer), callback);
256 
257 #if defined(__BIONIC__)
258   malloc_enable();
259 #endif
260 
261   ASSERT_FALSE(parsed);
262   EXPECT_EQ(0UL, num_calls);
263 }
264 
TEST(process_map,ReadMapFileAsyncSafe_buffer_too_small_could_parse)265 TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_could_parse) {
266   TemporaryFile tf;
267   ASSERT_TRUE(android::base::WriteStringToFd(
268       "0a0000-0c0000 rwxp 00000001 00:05 100    /fake/lib.so\n", tf.fd));
269 
270   size_t num_calls = 0;
271   auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
272 
273 #if defined(__BIONIC__)
274   // Any allocations will block after this call.
275   malloc_disable();
276 #endif
277 
278   char buffer[39];
279   bool parsed = android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer, sizeof(buffer), callback);
280 
281 #if defined(__BIONIC__)
282   malloc_enable();
283 #endif
284 
285   ASSERT_FALSE(parsed);
286   EXPECT_EQ(0UL, num_calls);
287 }
288 
289 class ProcessMapMappedFileSize : public ::testing::Test {
290   protected:
SetUp()291     void SetUp() override {
292       ASSERT_NE(tf.fd, -1) << "open failed: " << strerror(errno);
293       ASSERT_EQ(ftruncate(tf.fd, kFileSize), 0) << "ftruncate failed: " << strerror(errno);
294     }
295 
TearDown()296     void TearDown() override {
297       ASSERT_EQ(munmap(reinterpret_cast<void*>(map.start), map.end-map.start), 0)
298                 << "munmap failed: " << strerror(errno);
299     }
300 
PageAlign(uint64_t x)301     uint64_t PageAlign(uint64_t x) {
302       const uint64_t kPageSize = getpagesize();
303       return (x + kPageSize - 1) & ~(kPageSize - 1);
304     }
305 
CreateFileMapping(uint64_t size,uint64_t offset)306     bool CreateFileMapping(uint64_t size, uint64_t offset) {
307       void *addr = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, tf.fd, offset);
308       if (addr == MAP_FAILED) {
309         return false;
310       }
311 
312       map.start = reinterpret_cast<uint64_t>(addr);
313       map.end = PageAlign(map.start + size);
314       map.pgoff = offset;
315 
316       return true;
317     }
318 
319     TemporaryFile tf;
320     const size_t kFileSize = 65536;
321     android::procinfo::MapInfo map = android::procinfo::MapInfo(0 /* start */, 0 /* end */,
322                                                                 PROT_READ, 0 /* pgoff */,
323                                                                 0 /* inode */, tf.path, false);
324 };
325 
TEST_F(ProcessMapMappedFileSize,map_size_greater_than_file_size)326 TEST_F(ProcessMapMappedFileSize, map_size_greater_than_file_size) {
327   uint64_t size = 2 * kFileSize;
328   uint64_t offset = 0;
329 
330   ASSERT_TRUE(CreateFileMapping(size, offset));
331   uint64_t mapped_file_size = android::procinfo::MappedFileSize(map);
332 
333   ASSERT_EQ(mapped_file_size, kFileSize);
334 }
335 
TEST_F(ProcessMapMappedFileSize,map_size_less_than_file_size)336 TEST_F(ProcessMapMappedFileSize, map_size_less_than_file_size) {
337   uint64_t size = kFileSize / 2;
338   uint64_t offset = 0;
339 
340   ASSERT_TRUE(CreateFileMapping(size, offset));
341   uint64_t mapped_file_size = android::procinfo::MappedFileSize(map);
342 
343   ASSERT_EQ(mapped_file_size, size);
344 }
345 
TEST_F(ProcessMapMappedFileSize,map_size_equal_file_size)346 TEST_F(ProcessMapMappedFileSize, map_size_equal_file_size) {
347   uint64_t size = kFileSize;
348   uint64_t offset = 0;
349 
350   ASSERT_TRUE(CreateFileMapping(size, offset));
351   uint64_t mapped_file_size = android::procinfo::MappedFileSize(map);
352 
353   ASSERT_EQ(mapped_file_size, kFileSize);
354 
355 }
356 
TEST_F(ProcessMapMappedFileSize,offset_greater_than_file_size)357 TEST_F(ProcessMapMappedFileSize, offset_greater_than_file_size) {
358   uint64_t size = kFileSize;
359   uint64_t offset = kFileSize * 2;
360 
361   ASSERT_TRUE(CreateFileMapping(size, offset));
362   uint64_t mapped_file_size = android::procinfo::MappedFileSize(map);
363 
364   ASSERT_EQ(mapped_file_size, 0UL);
365 }
366 
TEST_F(ProcessMapMappedFileSize,invalid_map_name)367 TEST_F(ProcessMapMappedFileSize, invalid_map_name) {
368   uint64_t size = kFileSize;
369   uint64_t offset = 0;
370 
371   ASSERT_TRUE(CreateFileMapping(size, offset));
372 
373   // Name is empty
374   map.name = "";
375   uint64_t mapped_file_size = android::procinfo::MappedFileSize(map);
376   ASSERT_EQ(mapped_file_size, 0UL);
377 
378   // Is device path
379   map.name = "/dev/";
380   mapped_file_size = android::procinfo::MappedFileSize(map);
381   ASSERT_EQ(mapped_file_size, 0UL);
382 
383   // Does not start with '/'
384   map.name = "[anon:bss]";
385   mapped_file_size = android::procinfo::MappedFileSize(map);
386   ASSERT_EQ(mapped_file_size, 0UL);
387 
388   // File non-existent
389   map.name = "/tmp/non_existent_file";
390   mapped_file_size = android::procinfo::MappedFileSize(map);
391   ASSERT_EQ(mapped_file_size, 0UL);
392 }
393