1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/debug/proc_maps_linux.h"
11
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <sys/utsname.h>
15
16 #include "base/files/file_path.h"
17 #include "base/path_service.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/system/sys_info.h"
20 #include "base/threading/platform_thread.h"
21 #include "build/build_config.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace base::debug {
25
26 namespace {
27
28 // SmapsRollup was added in Linux 4.14.
IsSmapsRollupSupported()29 bool IsSmapsRollupSupported() {
30 struct utsname info;
31 if (uname(&info) < 0) {
32 NOTREACHED();
33 }
34
35 int major, minor, patch;
36 if (sscanf(info.release, "%d.%d.%d", &major, &minor, &patch) < 3) {
37 NOTREACHED();
38 }
39
40 if (major > 4) {
41 return true;
42 }
43
44 if (major < 4 || minor < 14) {
45 return false;
46 }
47
48 return true;
49 }
50
51 } // namespace
52
TEST(ProcMapsTest,Empty)53 TEST(ProcMapsTest, Empty) {
54 std::vector<MappedMemoryRegion> regions;
55 EXPECT_TRUE(ParseProcMaps("", ®ions));
56 EXPECT_EQ(0u, regions.size());
57 }
58
TEST(ProcMapsTest,NoSpaces)59 TEST(ProcMapsTest, NoSpaces) {
60 static const char kNoSpaces[] =
61 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n";
62
63 std::vector<MappedMemoryRegion> regions;
64 ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions));
65 ASSERT_EQ(1u, regions.size());
66
67 EXPECT_EQ(0x00400000u, regions[0].start);
68 EXPECT_EQ(0x0040b000u, regions[0].end);
69 EXPECT_EQ(0x00002200u, regions[0].offset);
70 EXPECT_EQ("/bin/cat", regions[0].path);
71 }
72
TEST(ProcMapsTest,Spaces)73 TEST(ProcMapsTest, Spaces) {
74 static const char kSpaces[] =
75 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n";
76
77 std::vector<MappedMemoryRegion> regions;
78 ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions));
79 ASSERT_EQ(1u, regions.size());
80
81 EXPECT_EQ(0x00400000u, regions[0].start);
82 EXPECT_EQ(0x0040b000u, regions[0].end);
83 EXPECT_EQ(0x00002200u, regions[0].offset);
84 EXPECT_EQ("/bin/space cat", regions[0].path);
85 }
86
TEST(ProcMapsTest,NoNewline)87 TEST(ProcMapsTest, NoNewline) {
88 static const char kNoSpaces[] =
89 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat";
90
91 std::vector<MappedMemoryRegion> regions;
92 ASSERT_FALSE(ParseProcMaps(kNoSpaces, ®ions));
93 }
94
TEST(ProcMapsTest,NoPath)95 TEST(ProcMapsTest, NoPath) {
96 static const char kNoPath[] =
97 "00400000-0040b000 rw-p 00000000 00:00 0 \n";
98
99 std::vector<MappedMemoryRegion> regions;
100 ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions));
101 ASSERT_EQ(1u, regions.size());
102
103 EXPECT_EQ(0x00400000u, regions[0].start);
104 EXPECT_EQ(0x0040b000u, regions[0].end);
105 EXPECT_EQ(0x00000000u, regions[0].offset);
106 EXPECT_EQ("", regions[0].path);
107 }
108
TEST(ProcMapsTest,Heap)109 TEST(ProcMapsTest, Heap) {
110 static const char kHeap[] =
111 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n";
112
113 std::vector<MappedMemoryRegion> regions;
114 ASSERT_TRUE(ParseProcMaps(kHeap, ®ions));
115 ASSERT_EQ(1u, regions.size());
116
117 EXPECT_EQ(0x022ac000u, regions[0].start);
118 EXPECT_EQ(0x022cd000u, regions[0].end);
119 EXPECT_EQ(0x00000000u, regions[0].offset);
120 EXPECT_EQ("[heap]", regions[0].path);
121 }
122
123 #if defined(ARCH_CPU_32_BITS)
TEST(ProcMapsTest,Stack32)124 TEST(ProcMapsTest, Stack32) {
125 static const char kStack[] =
126 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n";
127
128 std::vector<MappedMemoryRegion> regions;
129 ASSERT_TRUE(ParseProcMaps(kStack, ®ions));
130 ASSERT_EQ(1u, regions.size());
131
132 EXPECT_EQ(0xbeb04000u, regions[0].start);
133 EXPECT_EQ(0xbeb25000u, regions[0].end);
134 EXPECT_EQ(0x00000000u, regions[0].offset);
135 EXPECT_EQ("[stack]", regions[0].path);
136 }
137 #elif defined(ARCH_CPU_64_BITS)
TEST(ProcMapsTest,Stack64)138 TEST(ProcMapsTest, Stack64) {
139 static const char kStack[] =
140 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n";
141
142 std::vector<MappedMemoryRegion> regions;
143 ASSERT_TRUE(ParseProcMaps(kStack, ®ions));
144 ASSERT_EQ(1u, regions.size());
145
146 EXPECT_EQ(0x7fff69c5b000u, regions[0].start);
147 EXPECT_EQ(0x7fff69c7d000u, regions[0].end);
148 EXPECT_EQ(0x00000000u, regions[0].offset);
149 EXPECT_EQ("[stack]", regions[0].path);
150 }
151 #endif
152
TEST(ProcMapsTest,Multiple)153 TEST(ProcMapsTest, Multiple) {
154 static const char kMultiple[] =
155 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n"
156 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n"
157 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n";
158
159 std::vector<MappedMemoryRegion> regions;
160 ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions));
161 ASSERT_EQ(3u, regions.size());
162
163 EXPECT_EQ(0x00400000u, regions[0].start);
164 EXPECT_EQ(0x0040b000u, regions[0].end);
165 EXPECT_EQ(0x00000000u, regions[0].offset);
166 EXPECT_EQ("/bin/cat", regions[0].path);
167
168 EXPECT_EQ(0x0060a000u, regions[1].start);
169 EXPECT_EQ(0x0060b000u, regions[1].end);
170 EXPECT_EQ(0x0000a000u, regions[1].offset);
171 EXPECT_EQ("/bin/cat", regions[1].path);
172
173 EXPECT_EQ(0x0060b000u, regions[2].start);
174 EXPECT_EQ(0x0060c000u, regions[2].end);
175 EXPECT_EQ(0x0000b000u, regions[2].offset);
176 EXPECT_EQ("/bin/cat", regions[2].path);
177 }
178
TEST(ProcMapsTest,Permissions)179 TEST(ProcMapsTest, Permissions) {
180 static struct {
181 const char* input;
182 uint8_t permissions;
183 } kTestCases[] = {
184 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0},
185 {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0},
186 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n",
187 MappedMemoryRegion::READ},
188 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n",
189 MappedMemoryRegion::WRITE},
190 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n",
191 MappedMemoryRegion::EXECUTE},
192 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n",
193 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
194 MappedMemoryRegion::EXECUTE},
195 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n",
196 MappedMemoryRegion::PRIVATE},
197 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n",
198 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE},
199 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n",
200 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE},
201 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n",
202 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
203 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
204 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
205 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
206 };
207
208 for (size_t i = 0; i < std::size(kTestCases); ++i) {
209 SCOPED_TRACE(
210 base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input));
211
212 std::vector<MappedMemoryRegion> regions;
213 EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions));
214 EXPECT_EQ(1u, regions.size());
215 if (regions.empty())
216 continue;
217 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions);
218 }
219 }
220
221 // AddressSanitizer may move local variables to a dedicated "fake stack" which
222 // is outside the stack region listed in /proc/self/maps. We disable ASan
223 // instrumentation for this function to force the variable to be local.
224 //
225 // Similarly, HWAddressSanitizer may add a tag to all stack pointers which may
226 // move it outside of the stack regions in /proc/self/maps.
CheckProcMapsRegions(const std::vector<MappedMemoryRegion> & regions)227 __attribute__((no_sanitize("address", "hwaddress"))) void CheckProcMapsRegions(
228 const std::vector<MappedMemoryRegion>& regions) {
229 // We should be able to find both the current executable as well as the stack
230 // mapped into memory. Use the address of |exe_path| as a way of finding the
231 // stack.
232 FilePath exe_path;
233 EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path));
234 uintptr_t address = reinterpret_cast<uintptr_t>(&exe_path);
235 bool found_exe = false;
236 bool found_stack = false;
237 bool found_address = false;
238
239 for (const auto& i : regions) {
240 if (i.path == exe_path.value()) {
241 // It's OK to find the executable mapped multiple times as there'll be
242 // multiple sections (e.g., text, data).
243 found_exe = true;
244 }
245
246 if (i.path == "[stack]") {
247 // On Android the test is run on a background thread, since [stack] is for
248 // the main thread, we cannot test this.
249 #if !BUILDFLAG(IS_ANDROID)
250 EXPECT_GE(address, i.start);
251 EXPECT_LT(address, i.end);
252 #endif
253 EXPECT_TRUE(i.permissions & MappedMemoryRegion::READ);
254 EXPECT_TRUE(i.permissions & MappedMemoryRegion::WRITE);
255 EXPECT_FALSE(i.permissions & MappedMemoryRegion::EXECUTE);
256 EXPECT_TRUE(i.permissions & MappedMemoryRegion::PRIVATE);
257 EXPECT_FALSE(found_stack) << "Found duplicate stacks";
258 found_stack = true;
259 }
260
261 if (address >= i.start && address < i.end) {
262 EXPECT_FALSE(found_address) << "Found same address in multiple regions";
263 found_address = true;
264 }
265 }
266
267 EXPECT_TRUE(found_exe);
268 EXPECT_TRUE(found_stack);
269 EXPECT_TRUE(found_address);
270 }
271
TEST(ProcMapsTest,ReadProcMaps)272 TEST(ProcMapsTest, ReadProcMaps) {
273 std::string proc_maps;
274 ASSERT_TRUE(ReadProcMaps(&proc_maps));
275
276 std::vector<MappedMemoryRegion> regions;
277 ASSERT_TRUE(ParseProcMaps(proc_maps, ®ions));
278 ASSERT_FALSE(regions.empty());
279
280 CheckProcMapsRegions(regions);
281 }
282
TEST(ProcMapsTest,ReadProcMapsNonEmptyString)283 TEST(ProcMapsTest, ReadProcMapsNonEmptyString) {
284 std::string old_string("I forgot to clear the string");
285 std::string proc_maps(old_string);
286 ASSERT_TRUE(ReadProcMaps(&proc_maps));
287 EXPECT_EQ(std::string::npos, proc_maps.find(old_string));
288 }
289
TEST(ProcMapsTest,MissingFields)290 TEST(ProcMapsTest, MissingFields) {
291 static const char* const kTestCases[] = {
292 "00400000\n", // Missing end + beyond.
293 "00400000-0040b000\n", // Missing perms + beyond.
294 "00400000-0040b000 r-xp\n", // Missing offset + beyond.
295 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond.
296 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond.
297 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms.
298 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset.
299 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode.
300 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end.
301 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start.
302 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device.
303 };
304
305 for (size_t i = 0; i < std::size(kTestCases); ++i) {
306 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
307 std::vector<MappedMemoryRegion> regions;
308 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions));
309 }
310 }
311
TEST(ProcMapsTest,InvalidInput)312 TEST(ProcMapsTest, InvalidInput) {
313 static const char* const kTestCases[] = {
314 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
315 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n",
316 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n",
317 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n",
318 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n",
319 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n",
320 };
321
322 for (size_t i = 0; i < std::size(kTestCases); ++i) {
323 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
324 std::vector<MappedMemoryRegion> regions;
325 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions));
326 }
327 }
328
TEST(ProcMapsTest,ParseProcMapsEmptyString)329 TEST(ProcMapsTest, ParseProcMapsEmptyString) {
330 std::vector<MappedMemoryRegion> regions;
331 EXPECT_TRUE(ParseProcMaps("", ®ions));
332 EXPECT_EQ(0ULL, regions.size());
333 }
334
335 // Testing a couple of remotely possible weird things in the input:
336 // - Line ending with \r\n or \n\r.
337 // - File name contains quotes.
338 // - File name has whitespaces.
TEST(ProcMapsTest,ParseProcMapsWeirdCorrectInput)339 TEST(ProcMapsTest, ParseProcMapsWeirdCorrectInput) {
340 std::vector<MappedMemoryRegion> regions;
341 const std::string kContents =
342 "00400000-0040b000 r-xp 00000000 fc:00 2106562 "
343 " /bin/cat\r\n"
344 "7f53b7dad000-7f53b7f62000 r-xp 00000000 fc:00 263011 "
345 " /lib/x86_64-linux-gnu/libc-2.15.so\n\r"
346 "7f53b816d000-7f53b818f000 r-xp 00000000 fc:00 264284 "
347 " /lib/x86_64-linux-gnu/ld-2.15.so\n"
348 "7fff9c7ff000-7fff9c800000 r-xp 00000000 00:00 0 "
349 " \"vd so\"\n"
350 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 "
351 " [vsys call]\n";
352 EXPECT_TRUE(ParseProcMaps(kContents, ®ions));
353 EXPECT_EQ(5ULL, regions.size());
354 EXPECT_EQ("/bin/cat", regions[0].path);
355 EXPECT_EQ("/lib/x86_64-linux-gnu/libc-2.15.so", regions[1].path);
356 EXPECT_EQ("/lib/x86_64-linux-gnu/ld-2.15.so", regions[2].path);
357 EXPECT_EQ("\"vd so\"", regions[3].path);
358 EXPECT_EQ("[vsys call]", regions[4].path);
359 }
360
TEST(SmapsRollupTest,ReadAndParse)361 TEST(SmapsRollupTest, ReadAndParse) {
362 if (!IsSmapsRollupSupported()) {
363 GTEST_SKIP() << "smaps_rollup not supported";
364 }
365
366 const auto result = ReadAndParseSmapsRollup();
367
368 EXPECT_TRUE(result.has_value());
369
370 SmapsRollup smaps_rollup = result.value();
371
372 EXPECT_GT(smaps_rollup.rss, 0u);
373 EXPECT_GT(smaps_rollup.pss, 0u);
374 EXPECT_GT(smaps_rollup.private_dirty, 0u);
375 }
376
TEST(SmapsRollupTest,Valid)377 TEST(SmapsRollupTest, Valid) {
378 const auto result = ParseSmapsRollupForTesting(
379 // This input is based on a real one captured locally, but with some
380 // values changed in order to make them unique (to test that the correct
381 // values are being parsed).
382 R"(55f4d118e000-7ffff6e62000 ---p 00000000 00:00 0 [rollup]
383 Rss: 1908 kB
384 Pss: 573 kB
385 Pss_Dirty: 144 kB
386 Pss_Anon: 100 kB
387 Pss_File: 469 kB
388 Pss_Shmem: 12 kB
389 Shared_Clean: 1356 kB
390 Shared_Dirty: 0 kB
391 Private_Clean: 448 kB
392 Private_Dirty: 104 kB
393 Referenced: 1900 kB
394 Anonymous: 105 kB
395 KSM: 0 kB
396 LazyFree: 0 kB
397 AnonHugePages: 0 kB
398 ShmemPmdMapped: 0 kB
399 FilePmdMapped: 0 kB
400 Shared_Hugetlb: 0 kB
401 Private_Hugetlb: 0 kB
402 Swap: 10 kB
403 SwapPss: 20 kB
404 Locked: 0 kB
405 )");
406
407 EXPECT_TRUE(result.has_value());
408
409 SmapsRollup smaps_rollup = result.value();
410
411 EXPECT_EQ(smaps_rollup.rss, 1024 * 1908u);
412 EXPECT_EQ(smaps_rollup.pss, 1024 * 573u);
413 EXPECT_EQ(smaps_rollup.private_dirty, 1024 * 104u);
414 EXPECT_EQ(smaps_rollup.pss_anon, 1024 * 100u);
415 EXPECT_EQ(smaps_rollup.pss_file, 1024 * 469u);
416 EXPECT_EQ(smaps_rollup.pss_shmem, 1024 * 12u);
417 EXPECT_EQ(smaps_rollup.swap, 1024 * 10u);
418 EXPECT_EQ(smaps_rollup.swap_pss, 1024 * 20u);
419 }
420
421 } // namespace base::debug
422