blob: cdc56fea61124711e5048330e5cfa297f17926ca [file] [log] [blame]
Yabin Cui9581aea2018-05-10 17:19:12 -07001/*
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
Christopher Ferris8090b2e2019-08-16 10:49:34 -070019#include <inttypes.h>
20#include <sys/mman.h>
21
Yabin Cui9581aea2018-05-10 17:19:12 -070022#include <string>
Christopher Ferris8090b2e2019-08-16 10:49:34 -070023#include <vector>
Yabin Cui9581aea2018-05-10 17:19:12 -070024
25#include <android-base/file.h>
Christopher Ferris8090b2e2019-08-16 10:49:34 -070026#include <android-base/stringprintf.h>
Yabin Cui9581aea2018-05-10 17:19:12 -070027
28#include <gtest/gtest.h>
29
Yabin Cui629a8622018-10-30 17:14:35 -070030TEST(process_map, ReadMapFile) {
Yabin Cui9581aea2018-05-10 17:19:12 -070031 std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
Yabin Cui629a8622018-10-30 17:14:35 -070032 std::vector<android::procinfo::MapInfo> maps;
Yabin Cui9581aea2018-05-10 17:19:12 -070033 ASSERT_TRUE(android::procinfo::ReadMapFile(
Edgar Arriagafd214c12020-11-20 18:00:41 -080034 map_file, [&](const android::procinfo::MapInfo& mapinfo) { maps.emplace_back(mapinfo); }));
Yabin Cui9581aea2018-05-10 17:19:12 -070035 ASSERT_EQ(2043u, maps.size());
36 ASSERT_EQ(maps[0].start, 0x12c00000ULL);
37 ASSERT_EQ(maps[0].end, 0x2ac00000ULL);
38 ASSERT_EQ(maps[0].flags, PROT_READ | PROT_WRITE);
39 ASSERT_EQ(maps[0].pgoff, 0ULL);
Sandeep Patil60194582019-01-30 17:43:22 -080040 ASSERT_EQ(maps[0].inode, 10267643UL);
Joel Fernandescc49e832018-08-24 11:57:57 -070041 ASSERT_EQ(maps[0].name, "[anon:dalvik-main space (region space)]");
Yabin Cui9581aea2018-05-10 17:19:12 -070042 ASSERT_EQ(maps[876].start, 0x70e6c4f000ULL);
43 ASSERT_EQ(maps[876].end, 0x70e6c6b000ULL);
44 ASSERT_EQ(maps[876].flags, PROT_READ | PROT_EXEC);
45 ASSERT_EQ(maps[876].pgoff, 0ULL);
Sandeep Patil60194582019-01-30 17:43:22 -080046 ASSERT_EQ(maps[876].inode, 2407UL);
Yabin Cui9581aea2018-05-10 17:19:12 -070047 ASSERT_EQ(maps[876].name, "/system/lib64/libutils.so");
48 ASSERT_EQ(maps[1260].start, 0x70e96fa000ULL);
49 ASSERT_EQ(maps[1260].end, 0x70e96fb000ULL);
50 ASSERT_EQ(maps[1260].flags, PROT_READ);
51 ASSERT_EQ(maps[1260].pgoff, 0ULL);
Sandeep Patil60194582019-01-30 17:43:22 -080052 ASSERT_EQ(maps[1260].inode, 10266154UL);
Yabin Cui9581aea2018-05-10 17:19:12 -070053 ASSERT_EQ(maps[1260].name,
Joel Fernandescc49e832018-08-24 11:57:57 -070054 "[anon:dalvik-classes.dex extracted in memory from "
55 "/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk]");
Yabin Cui9581aea2018-05-10 17:19:12 -070056}
Yabin Cui629a8622018-10-30 17:14:35 -070057
58TEST(process_map, ReadProcessMaps) {
59 std::vector<android::procinfo::MapInfo> maps;
60 ASSERT_TRUE(android::procinfo::ReadProcessMaps(
Edgar Arriagafd214c12020-11-20 18:00:41 -080061 getpid(), [&](const android::procinfo::MapInfo& mapinfo) { maps.emplace_back(mapinfo); }));
Yabin Cui629a8622018-10-30 17:14:35 -070062 ASSERT_GT(maps.size(), 0u);
63 maps.clear();
64 ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
65 ASSERT_GT(maps.size(), 0u);
66}
Christopher Ferris8090b2e2019-08-16 10:49:34 -070067
68extern "C" void malloc_disable();
69extern "C" void malloc_enable();
70
71struct TestMapInfo {
72 TestMapInfo() = default;
73 TestMapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
Edgar Arriagafd214c12020-11-20 18:00:41 -080074 const char* new_name, bool isShared)
75 : start(start), end(end), flags(flags), pgoff(pgoff), inode(inode), isShared(isShared) {
Christopher Ferris8090b2e2019-08-16 10:49:34 -070076 strcpy(name, new_name);
77 }
78 uint64_t start = 0;
79 uint64_t end = 0;
80 uint16_t flags = 0;
81 uint64_t pgoff = 0;
82 ino_t inode = 0;
83 char name[100] = {};
Edgar Arriagafd214c12020-11-20 18:00:41 -080084 bool isShared = false;
Christopher Ferris8090b2e2019-08-16 10:49:34 -070085};
86
87void VerifyReadMapFileAsyncSafe(const char* maps_data,
88 const std::vector<TestMapInfo>& expected_info) {
89 TemporaryFile tf;
90 ASSERT_TRUE(android::base::WriteStringToFd(maps_data, tf.fd));
91
92 std::vector<TestMapInfo> saved_info(expected_info.size());
93 size_t num_maps = 0;
94
95 auto callback = [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
Edgar Arriagafd214c12020-11-20 18:00:41 -080096 const char* name, bool shared) {
Christopher Ferris8090b2e2019-08-16 10:49:34 -070097 if (num_maps != saved_info.size()) {
98 TestMapInfo& saved = saved_info[num_maps];
99 saved.start = start;
100 saved.end = end;
101 saved.flags = flags;
102 saved.pgoff = pgoff;
103 saved.inode = inode;
104 strcpy(saved.name, name);
Edgar Arriagafd214c12020-11-20 18:00:41 -0800105 saved.isShared = shared;
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700106 }
107 num_maps++;
108 };
109
110 std::vector<char> buffer(64 * 1024);
111
112#if defined(__BIONIC__)
113 // Any allocations will block after this call.
114 malloc_disable();
115#endif
116
117 bool parsed =
118 android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer.data(), buffer.size(), callback);
119
120#if defined(__BIONIC__)
121 malloc_enable();
122#endif
123
124 ASSERT_TRUE(parsed) << "Parsing of data failed:\n" << maps_data;
125 ASSERT_EQ(expected_info.size(), num_maps);
126 for (size_t i = 0; i < expected_info.size(); i++) {
127 const TestMapInfo& expected = expected_info[i];
128 const TestMapInfo& saved = saved_info[i];
129 EXPECT_EQ(expected.start, saved.start);
130 EXPECT_EQ(expected.end, saved.end);
131 EXPECT_EQ(expected.flags, saved.flags);
132 EXPECT_EQ(expected.pgoff, saved.pgoff);
133 EXPECT_EQ(expected.inode, saved.inode);
134 EXPECT_STREQ(expected.name, saved.name);
Edgar Arriagafd214c12020-11-20 18:00:41 -0800135 EXPECT_EQ(expected.isShared, saved.isShared);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700136 }
137}
138
139TEST(process_map, ReadMapFileAsyncSafe_invalid) {
140 std::vector<TestMapInfo> expected_info;
141
142 VerifyReadMapFileAsyncSafe("12c00000-2ac00000", expected_info);
143}
144
145TEST(process_map, ReadMapFileAsyncSafe_single) {
146 std::vector<TestMapInfo> expected_info;
147 expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
Edgar Arriagafd214c12020-11-20 18:00:41 -0800148 "/lib/fake.so", false);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700149
150 VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so",
151 expected_info);
152}
153
154TEST(process_map, ReadMapFileAsyncSafe_single_with_newline) {
155 std::vector<TestMapInfo> expected_info;
156 expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
Edgar Arriagafd214c12020-11-20 18:00:41 -0800157 "/lib/fake.so", false);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700158
159 VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so\n",
160 expected_info);
161}
162
163TEST(process_map, ReadMapFileAsyncSafe_single_no_library) {
164 std::vector<TestMapInfo> expected_info;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800165 expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 0xb00, 101, "",
166 false);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700167
168 VerifyReadMapFileAsyncSafe("a0000-c0000 rwxp 00000b00 00:05 101", expected_info);
169}
170
171TEST(process_map, ReadMapFileAsyncSafe_multiple) {
172 std::vector<TestMapInfo> expected_info;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800173 expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 1, 100, "",
174 false);
175 expected_info.emplace_back(0xd0000, 0xe0000, PROT_READ, 2, 101, "/lib/libsomething1.so", false);
176 expected_info.emplace_back(0xf0000, 0x100000, PROT_WRITE, 3, 102, "/lib/libsomething2.so", false);
177 expected_info.emplace_back(0x110000, 0x120000, PROT_EXEC, 4, 103, "[anon:something or another]",
178 false);
179 expected_info.emplace_back(0x130000, 0x140000, PROT_READ, 5, 104, "/lib/libsomething3.so", true);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700180
181 std::string map_data =
182 "0a0000-0c0000 rwxp 00000001 00:05 100\n"
183 "0d0000-0e0000 r--p 00000002 00:05 101 /lib/libsomething1.so\n"
184 "0f0000-100000 -w-p 00000003 00:05 102 /lib/libsomething2.so\n"
Edgar Arriagafd214c12020-11-20 18:00:41 -0800185 "110000-120000 --xp 00000004 00:05 103 [anon:something or another]\n"
186 "130000-140000 r--s 00000005 00:05 104 /lib/libsomething3.so\n";
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700187
188 VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
189}
190
191TEST(process_map, ReadMapFileAsyncSafe_multiple_reads) {
192 std::vector<TestMapInfo> expected_info;
193 std::string map_data;
194 uint64_t start = 0xa0000;
195 for (size_t i = 0; i < 10000; i++) {
196 map_data += android::base::StringPrintf("%" PRIx64 "-%" PRIx64 " r--p %zx 01:20 %zu fake.so\n",
197 start, start + 0x1000, i, 1000 + i);
Edgar Arriagafd214c12020-11-20 18:00:41 -0800198 expected_info.emplace_back(start, start + 0x1000, PROT_READ, i, 1000 + i, "fake.so", false);
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700199 }
200
201 VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
202}
203
204TEST(process_map, ReadMapFileAsyncSafe_buffer_nullptr) {
205 size_t num_calls = 0;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800206 auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700207
208#if defined(__BIONIC__)
209 // Any allocations will block after this call.
210 malloc_disable();
211#endif
212
213 bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", nullptr, 10, callback);
214
215#if defined(__BIONIC__)
216 malloc_enable();
217#endif
218
219 ASSERT_FALSE(parsed);
220 EXPECT_EQ(0UL, num_calls);
221}
222
223TEST(process_map, ReadMapFileAsyncSafe_buffer_size_zero) {
224 size_t num_calls = 0;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800225 auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700226
227#if defined(__BIONIC__)
228 // Any allocations will block after this call.
229 malloc_disable();
230#endif
231
232 char buffer[10];
233 bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, 0, callback);
234
235#if defined(__BIONIC__)
236 malloc_enable();
237#endif
238
239 ASSERT_FALSE(parsed);
240 EXPECT_EQ(0UL, num_calls);
241}
242
243TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_no_calls) {
244 size_t num_calls = 0;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800245 auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700246
247#if defined(__BIONIC__)
248 // Any allocations will block after this call.
249 malloc_disable();
250#endif
251
252 char buffer[10];
253 bool parsed =
254 android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, sizeof(buffer), callback);
255
256#if defined(__BIONIC__)
257 malloc_enable();
258#endif
259
260 ASSERT_FALSE(parsed);
261 EXPECT_EQ(0UL, num_calls);
262}
263
264TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_could_parse) {
265 TemporaryFile tf;
266 ASSERT_TRUE(android::base::WriteStringToFd(
267 "0a0000-0c0000 rwxp 00000001 00:05 100 /fake/lib.so\n", tf.fd));
268
269 size_t num_calls = 0;
Edgar Arriagafd214c12020-11-20 18:00:41 -0800270 auto callback = [&](const android::procinfo::MapInfo&) { num_calls++; };
Christopher Ferris8090b2e2019-08-16 10:49:34 -0700271
272#if defined(__BIONIC__)
273 // Any allocations will block after this call.
274 malloc_disable();
275#endif
276
277 char buffer[39];
278 bool parsed = android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer, sizeof(buffer), callback);
279
280#if defined(__BIONIC__)
281 malloc_enable();
282#endif
283
284 ASSERT_FALSE(parsed);
285 EXPECT_EQ(0UL, num_calls);
286}