summaryrefslogtreecommitdiff
path: root/libprocinfo/process_map_test.cpp
blob: b1bdc08a581b93b7b9649cb3de69eb53310ad7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <procinfo/process_map.h>

#include <inttypes.h>
#include <sys/mman.h>

#include <string>
#include <vector>

#include <android-base/file.h>
#include <android-base/stringprintf.h>

#include <gtest/gtest.h>

TEST(process_map, ReadMapFile) {
  std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
  std::vector<android::procinfo::MapInfo> maps;
  ASSERT_TRUE(android::procinfo::ReadMapFile(
      map_file,
      [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
          const char* name) { maps.emplace_back(start, end, flags, pgoff, inode, name); }));
  ASSERT_EQ(2043u, maps.size());
  ASSERT_EQ(maps[0].start, 0x12c00000ULL);
  ASSERT_EQ(maps[0].end, 0x2ac00000ULL);
  ASSERT_EQ(maps[0].flags, PROT_READ | PROT_WRITE);
  ASSERT_EQ(maps[0].pgoff, 0ULL);
  ASSERT_EQ(maps[0].inode, 10267643UL);
  ASSERT_EQ(maps[0].name, "[anon:dalvik-main space (region space)]");
  ASSERT_EQ(maps[876].start, 0x70e6c4f000ULL);
  ASSERT_EQ(maps[876].end, 0x70e6c6b000ULL);
  ASSERT_EQ(maps[876].flags, PROT_READ | PROT_EXEC);
  ASSERT_EQ(maps[876].pgoff, 0ULL);
  ASSERT_EQ(maps[876].inode, 2407UL);
  ASSERT_EQ(maps[876].name, "/system/lib64/libutils.so");
  ASSERT_EQ(maps[1260].start, 0x70e96fa000ULL);
  ASSERT_EQ(maps[1260].end, 0x70e96fb000ULL);
  ASSERT_EQ(maps[1260].flags, PROT_READ);
  ASSERT_EQ(maps[1260].pgoff, 0ULL);
  ASSERT_EQ(maps[1260].inode, 10266154UL);
  ASSERT_EQ(maps[1260].name,
            "[anon:dalvik-classes.dex extracted in memory from "
            "/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk]");
}

TEST(process_map, ReadProcessMaps) {
  std::vector<android::procinfo::MapInfo> maps;
  ASSERT_TRUE(android::procinfo::ReadProcessMaps(
      getpid(),
      [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
          const char* name) { maps.emplace_back(start, end, flags, pgoff, inode, name); }));
  ASSERT_GT(maps.size(), 0u);
  maps.clear();
  ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
  ASSERT_GT(maps.size(), 0u);
}

extern "C" void malloc_disable();
extern "C" void malloc_enable();

struct TestMapInfo {
  TestMapInfo() = default;
  TestMapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
              const char* new_name)
      : start(start), end(end), flags(flags), pgoff(pgoff), inode(inode) {
    strcpy(name, new_name);
  }
  uint64_t start = 0;
  uint64_t end = 0;
  uint16_t flags = 0;
  uint64_t pgoff = 0;
  ino_t inode = 0;
  char name[100] = {};
};

void VerifyReadMapFileAsyncSafe(const char* maps_data,
                                const std::vector<TestMapInfo>& expected_info) {
  TemporaryFile tf;
  ASSERT_TRUE(android::base::WriteStringToFd(maps_data, tf.fd));

  std::vector<TestMapInfo> saved_info(expected_info.size());
  size_t num_maps = 0;

  auto callback = [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
                      const char* name) {
    if (num_maps != saved_info.size()) {
      TestMapInfo& saved = saved_info[num_maps];
      saved.start = start;
      saved.end = end;
      saved.flags = flags;
      saved.pgoff = pgoff;
      saved.inode = inode;
      strcpy(saved.name, name);
    }
    num_maps++;
  };

  std::vector<char> buffer(64 * 1024);

#if defined(__BIONIC__)
  // Any allocations will block after this call.
  malloc_disable();
#endif

  bool parsed =
      android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer.data(), buffer.size(), callback);

#if defined(__BIONIC__)
  malloc_enable();
#endif

  ASSERT_TRUE(parsed) << "Parsing of data failed:\n" << maps_data;
  ASSERT_EQ(expected_info.size(), num_maps);
  for (size_t i = 0; i < expected_info.size(); i++) {
    const TestMapInfo& expected = expected_info[i];
    const TestMapInfo& saved = saved_info[i];
    EXPECT_EQ(expected.start, saved.start);
    EXPECT_EQ(expected.end, saved.end);
    EXPECT_EQ(expected.flags, saved.flags);
    EXPECT_EQ(expected.pgoff, saved.pgoff);
    EXPECT_EQ(expected.inode, saved.inode);
    EXPECT_STREQ(expected.name, saved.name);
  }
}

TEST(process_map, ReadMapFileAsyncSafe_invalid) {
  std::vector<TestMapInfo> expected_info;

  VerifyReadMapFileAsyncSafe("12c00000-2ac00000", expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_single) {
  std::vector<TestMapInfo> expected_info;
  expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
                             "/lib/fake.so");

  VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so",
                             expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_single_with_newline) {
  std::vector<TestMapInfo> expected_info;
  expected_info.emplace_back(0x12c00000, 0x2ac00000, PROT_READ | PROT_WRITE, 0x100, 10267643,
                             "/lib/fake.so");

  VerifyReadMapFileAsyncSafe("12c00000-2ac00000 rw-p 00000100 00:05 10267643 /lib/fake.so\n",
                             expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_single_no_library) {
  std::vector<TestMapInfo> expected_info;
  expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 0xb00, 101, "");

  VerifyReadMapFileAsyncSafe("a0000-c0000 rwxp 00000b00 00:05 101", expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_multiple) {
  std::vector<TestMapInfo> expected_info;
  expected_info.emplace_back(0xa0000, 0xc0000, PROT_READ | PROT_WRITE | PROT_EXEC, 1, 100, "");
  expected_info.emplace_back(0xd0000, 0xe0000, PROT_READ, 2, 101, "/lib/libsomething1.so");
  expected_info.emplace_back(0xf0000, 0x100000, PROT_WRITE, 3, 102, "/lib/libsomething2.so");
  expected_info.emplace_back(0x110000, 0x120000, PROT_EXEC, 4, 103, "[anon:something or another]");

  std::string map_data =
      "0a0000-0c0000 rwxp 00000001 00:05 100\n"
      "0d0000-0e0000 r--p 00000002 00:05 101  /lib/libsomething1.so\n"
      "0f0000-100000 -w-p 00000003 00:05 102  /lib/libsomething2.so\n"
      "110000-120000 --xp 00000004 00:05 103  [anon:something or another]\n";

  VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_multiple_reads) {
  std::vector<TestMapInfo> expected_info;
  std::string map_data;
  uint64_t start = 0xa0000;
  for (size_t i = 0; i < 10000; i++) {
    map_data += android::base::StringPrintf("%" PRIx64 "-%" PRIx64 " r--p %zx 01:20 %zu fake.so\n",
                                            start, start + 0x1000, i, 1000 + i);
    expected_info.emplace_back(start, start + 0x1000, PROT_READ, i, 1000 + i, "fake.so");
  }

  VerifyReadMapFileAsyncSafe(map_data.c_str(), expected_info);
}

TEST(process_map, ReadMapFileAsyncSafe_buffer_nullptr) {
  size_t num_calls = 0;
  auto callback = [&](uint64_t, uint64_t, uint16_t, uint64_t, ino_t, const char*) { num_calls++; };

#if defined(__BIONIC__)
  // Any allocations will block after this call.
  malloc_disable();
#endif

  bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", nullptr, 10, callback);

#if defined(__BIONIC__)
  malloc_enable();
#endif

  ASSERT_FALSE(parsed);
  EXPECT_EQ(0UL, num_calls);
}

TEST(process_map, ReadMapFileAsyncSafe_buffer_size_zero) {
  size_t num_calls = 0;
  auto callback = [&](uint64_t, uint64_t, uint16_t, uint64_t, ino_t, const char*) { num_calls++; };

#if defined(__BIONIC__)
  // Any allocations will block after this call.
  malloc_disable();
#endif

  char buffer[10];
  bool parsed = android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, 0, callback);

#if defined(__BIONIC__)
  malloc_enable();
#endif

  ASSERT_FALSE(parsed);
  EXPECT_EQ(0UL, num_calls);
}

TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_no_calls) {
  size_t num_calls = 0;
  auto callback = [&](uint64_t, uint64_t, uint16_t, uint64_t, ino_t, const char*) { num_calls++; };

#if defined(__BIONIC__)
  // Any allocations will block after this call.
  malloc_disable();
#endif

  char buffer[10];
  bool parsed =
      android::procinfo::ReadMapFileAsyncSafe("/proc/self/maps", buffer, sizeof(buffer), callback);

#if defined(__BIONIC__)
  malloc_enable();
#endif

  ASSERT_FALSE(parsed);
  EXPECT_EQ(0UL, num_calls);
}

TEST(process_map, ReadMapFileAsyncSafe_buffer_too_small_could_parse) {
  TemporaryFile tf;
  ASSERT_TRUE(android::base::WriteStringToFd(
      "0a0000-0c0000 rwxp 00000001 00:05 100    /fake/lib.so\n", tf.fd));

  size_t num_calls = 0;
  auto callback = [&](uint64_t, uint64_t, uint16_t, uint64_t, ino_t, const char*) { num_calls++; };

#if defined(__BIONIC__)
  // Any allocations will block after this call.
  malloc_disable();
#endif

  char buffer[39];
  bool parsed = android::procinfo::ReadMapFileAsyncSafe(tf.path, buffer, sizeof(buffer), callback);

#if defined(__BIONIC__)
  malloc_enable();
#endif

  ASSERT_FALSE(parsed);
  EXPECT_EQ(0UL, num_calls);
}