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 #include "base/debug/proc_maps_linux.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/threading/platform_thread.h"
14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace base {
18 namespace debug {
19
TEST(ProcMapsTest,Empty)20 TEST(ProcMapsTest, Empty) {
21 std::vector<MappedMemoryRegion> regions;
22 EXPECT_TRUE(ParseProcMaps("", ®ions));
23 EXPECT_EQ(0u, regions.size());
24 }
25
TEST(ProcMapsTest,NoSpaces)26 TEST(ProcMapsTest, NoSpaces) {
27 static const char kNoSpaces[] =
28 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n";
29
30 std::vector<MappedMemoryRegion> regions;
31 ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions));
32 ASSERT_EQ(1u, regions.size());
33
34 EXPECT_EQ(0x00400000u, regions[0].start);
35 EXPECT_EQ(0x0040b000u, regions[0].end);
36 EXPECT_EQ(0x00002200u, regions[0].offset);
37 EXPECT_EQ("/bin/cat", regions[0].path);
38 }
39
TEST(ProcMapsTest,Spaces)40 TEST(ProcMapsTest, Spaces) {
41 static const char kSpaces[] =
42 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n";
43
44 std::vector<MappedMemoryRegion> regions;
45 ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions));
46 ASSERT_EQ(1u, regions.size());
47
48 EXPECT_EQ(0x00400000u, regions[0].start);
49 EXPECT_EQ(0x0040b000u, regions[0].end);
50 EXPECT_EQ(0x00002200u, regions[0].offset);
51 EXPECT_EQ("/bin/space cat", regions[0].path);
52 }
53
TEST(ProcMapsTest,NoNewline)54 TEST(ProcMapsTest, NoNewline) {
55 static const char kNoSpaces[] =
56 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat";
57
58 std::vector<MappedMemoryRegion> regions;
59 ASSERT_FALSE(ParseProcMaps(kNoSpaces, ®ions));
60 }
61
TEST(ProcMapsTest,NoPath)62 TEST(ProcMapsTest, NoPath) {
63 static const char kNoPath[] =
64 "00400000-0040b000 rw-p 00000000 00:00 0 \n";
65
66 std::vector<MappedMemoryRegion> regions;
67 ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions));
68 ASSERT_EQ(1u, regions.size());
69
70 EXPECT_EQ(0x00400000u, regions[0].start);
71 EXPECT_EQ(0x0040b000u, regions[0].end);
72 EXPECT_EQ(0x00000000u, regions[0].offset);
73 EXPECT_EQ("", regions[0].path);
74 }
75
TEST(ProcMapsTest,Heap)76 TEST(ProcMapsTest, Heap) {
77 static const char kHeap[] =
78 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n";
79
80 std::vector<MappedMemoryRegion> regions;
81 ASSERT_TRUE(ParseProcMaps(kHeap, ®ions));
82 ASSERT_EQ(1u, regions.size());
83
84 EXPECT_EQ(0x022ac000u, regions[0].start);
85 EXPECT_EQ(0x022cd000u, regions[0].end);
86 EXPECT_EQ(0x00000000u, regions[0].offset);
87 EXPECT_EQ("[heap]", regions[0].path);
88 }
89
90 #if defined(ARCH_CPU_32_BITS)
TEST(ProcMapsTest,Stack32)91 TEST(ProcMapsTest, Stack32) {
92 static const char kStack[] =
93 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n";
94
95 std::vector<MappedMemoryRegion> regions;
96 ASSERT_TRUE(ParseProcMaps(kStack, ®ions));
97 ASSERT_EQ(1u, regions.size());
98
99 EXPECT_EQ(0xbeb04000u, regions[0].start);
100 EXPECT_EQ(0xbeb25000u, regions[0].end);
101 EXPECT_EQ(0x00000000u, regions[0].offset);
102 EXPECT_EQ("[stack]", regions[0].path);
103 }
104 #elif defined(ARCH_CPU_64_BITS)
TEST(ProcMapsTest,Stack64)105 TEST(ProcMapsTest, Stack64) {
106 static const char kStack[] =
107 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n";
108
109 std::vector<MappedMemoryRegion> regions;
110 ASSERT_TRUE(ParseProcMaps(kStack, ®ions));
111 ASSERT_EQ(1u, regions.size());
112
113 EXPECT_EQ(0x7fff69c5b000u, regions[0].start);
114 EXPECT_EQ(0x7fff69c7d000u, regions[0].end);
115 EXPECT_EQ(0x00000000u, regions[0].offset);
116 EXPECT_EQ("[stack]", regions[0].path);
117 }
118 #endif
119
TEST(ProcMapsTest,Multiple)120 TEST(ProcMapsTest, Multiple) {
121 static const char kMultiple[] =
122 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n"
123 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n"
124 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n";
125
126 std::vector<MappedMemoryRegion> regions;
127 ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions));
128 ASSERT_EQ(3u, regions.size());
129
130 EXPECT_EQ(0x00400000u, regions[0].start);
131 EXPECT_EQ(0x0040b000u, regions[0].end);
132 EXPECT_EQ(0x00000000u, regions[0].offset);
133 EXPECT_EQ("/bin/cat", regions[0].path);
134
135 EXPECT_EQ(0x0060a000u, regions[1].start);
136 EXPECT_EQ(0x0060b000u, regions[1].end);
137 EXPECT_EQ(0x0000a000u, regions[1].offset);
138 EXPECT_EQ("/bin/cat", regions[1].path);
139
140 EXPECT_EQ(0x0060b000u, regions[2].start);
141 EXPECT_EQ(0x0060c000u, regions[2].end);
142 EXPECT_EQ(0x0000b000u, regions[2].offset);
143 EXPECT_EQ("/bin/cat", regions[2].path);
144 }
145
TEST(ProcMapsTest,Permissions)146 TEST(ProcMapsTest, Permissions) {
147 static struct {
148 const char* input;
149 uint8_t permissions;
150 } kTestCases[] = {
151 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0},
152 {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0},
153 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n",
154 MappedMemoryRegion::READ},
155 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n",
156 MappedMemoryRegion::WRITE},
157 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n",
158 MappedMemoryRegion::EXECUTE},
159 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n",
160 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
161 MappedMemoryRegion::EXECUTE},
162 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n",
163 MappedMemoryRegion::PRIVATE},
164 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n",
165 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE},
166 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n",
167 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE},
168 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n",
169 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
170 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
171 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
172 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
173 };
174
175 for (size_t i = 0; i < std::size(kTestCases); ++i) {
176 SCOPED_TRACE(
177 base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input));
178
179 std::vector<MappedMemoryRegion> regions;
180 EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions));
181 EXPECT_EQ(1u, regions.size());
182 if (regions.empty())
183 continue;
184 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions);
185 }
186 }
187
188 // AddressSanitizer may move local variables to a dedicated "fake stack" which
189 // is outside the stack region listed in /proc/self/maps. We disable ASan
190 // instrumentation for this function to force the variable to be local.
191 //
192 // Similarly, HWAddressSanitizer may add a tag to all stack pointers which may
193 // move it outside of the stack regions in /proc/self/maps.
CheckProcMapsRegions(const std::vector<MappedMemoryRegion> & regions)194 __attribute__((no_sanitize("address", "hwaddress"))) void CheckProcMapsRegions(
195 const std::vector<MappedMemoryRegion>& regions) {
196 // We should be able to find both the current executable as well as the stack
197 // mapped into memory. Use the address of |exe_path| as a way of finding the
198 // stack.
199 FilePath exe_path;
200 EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path));
201 uintptr_t address = reinterpret_cast<uintptr_t>(&exe_path);
202 bool found_exe = false;
203 bool found_stack = false;
204 bool found_address = false;
205
206 for (const auto& i : regions) {
207 if (i.path == exe_path.value()) {
208 // It's OK to find the executable mapped multiple times as there'll be
209 // multiple sections (e.g., text, data).
210 found_exe = true;
211 }
212
213 if (i.path == "[stack]") {
214 // On Android the test is run on a background thread, since [stack] is for
215 // the main thread, we cannot test this.
216 #if !BUILDFLAG(IS_ANDROID)
217 EXPECT_GE(address, i.start);
218 EXPECT_LT(address, i.end);
219 #endif
220 EXPECT_TRUE(i.permissions & MappedMemoryRegion::READ);
221 EXPECT_TRUE(i.permissions & MappedMemoryRegion::WRITE);
222 EXPECT_FALSE(i.permissions & MappedMemoryRegion::EXECUTE);
223 EXPECT_TRUE(i.permissions & MappedMemoryRegion::PRIVATE);
224 EXPECT_FALSE(found_stack) << "Found duplicate stacks";
225 found_stack = true;
226 }
227
228 if (address >= i.start && address < i.end) {
229 EXPECT_FALSE(found_address) << "Found same address in multiple regions";
230 found_address = true;
231 }
232 }
233
234 EXPECT_TRUE(found_exe);
235 EXPECT_TRUE(found_stack);
236 EXPECT_TRUE(found_address);
237 }
238
TEST(ProcMapsTest,ReadProcMaps)239 TEST(ProcMapsTest, ReadProcMaps) {
240 std::string proc_maps;
241 ASSERT_TRUE(ReadProcMaps(&proc_maps));
242
243 std::vector<MappedMemoryRegion> regions;
244 ASSERT_TRUE(ParseProcMaps(proc_maps, ®ions));
245 ASSERT_FALSE(regions.empty());
246
247 CheckProcMapsRegions(regions);
248 }
249
TEST(ProcMapsTest,ReadProcMapsNonEmptyString)250 TEST(ProcMapsTest, ReadProcMapsNonEmptyString) {
251 std::string old_string("I forgot to clear the string");
252 std::string proc_maps(old_string);
253 ASSERT_TRUE(ReadProcMaps(&proc_maps));
254 EXPECT_EQ(std::string::npos, proc_maps.find(old_string));
255 }
256
TEST(ProcMapsTest,MissingFields)257 TEST(ProcMapsTest, MissingFields) {
258 static const char* const kTestCases[] = {
259 "00400000\n", // Missing end + beyond.
260 "00400000-0040b000\n", // Missing perms + beyond.
261 "00400000-0040b000 r-xp\n", // Missing offset + beyond.
262 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond.
263 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond.
264 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms.
265 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset.
266 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode.
267 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end.
268 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start.
269 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device.
270 };
271
272 for (size_t i = 0; i < std::size(kTestCases); ++i) {
273 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
274 std::vector<MappedMemoryRegion> regions;
275 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions));
276 }
277 }
278
TEST(ProcMapsTest,InvalidInput)279 TEST(ProcMapsTest, InvalidInput) {
280 static const char* const kTestCases[] = {
281 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
282 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n",
283 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n",
284 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n",
285 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n",
286 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n",
287 };
288
289 for (size_t i = 0; i < std::size(kTestCases); ++i) {
290 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
291 std::vector<MappedMemoryRegion> regions;
292 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions));
293 }
294 }
295
TEST(ProcMapsTest,ParseProcMapsEmptyString)296 TEST(ProcMapsTest, ParseProcMapsEmptyString) {
297 std::vector<MappedMemoryRegion> regions;
298 EXPECT_TRUE(ParseProcMaps("", ®ions));
299 EXPECT_EQ(0ULL, regions.size());
300 }
301
302 // Testing a couple of remotely possible weird things in the input:
303 // - Line ending with \r\n or \n\r.
304 // - File name contains quotes.
305 // - File name has whitespaces.
TEST(ProcMapsTest,ParseProcMapsWeirdCorrectInput)306 TEST(ProcMapsTest, ParseProcMapsWeirdCorrectInput) {
307 std::vector<MappedMemoryRegion> regions;
308 const std::string kContents =
309 "00400000-0040b000 r-xp 00000000 fc:00 2106562 "
310 " /bin/cat\r\n"
311 "7f53b7dad000-7f53b7f62000 r-xp 00000000 fc:00 263011 "
312 " /lib/x86_64-linux-gnu/libc-2.15.so\n\r"
313 "7f53b816d000-7f53b818f000 r-xp 00000000 fc:00 264284 "
314 " /lib/x86_64-linux-gnu/ld-2.15.so\n"
315 "7fff9c7ff000-7fff9c800000 r-xp 00000000 00:00 0 "
316 " \"vd so\"\n"
317 "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 "
318 " [vsys call]\n";
319 EXPECT_TRUE(ParseProcMaps(kContents, ®ions));
320 EXPECT_EQ(5ULL, regions.size());
321 EXPECT_EQ("/bin/cat", regions[0].path);
322 EXPECT_EQ("/lib/x86_64-linux-gnu/libc-2.15.so", regions[1].path);
323 EXPECT_EQ("/lib/x86_64-linux-gnu/ld-2.15.so", regions[2].path);
324 EXPECT_EQ("\"vd so\"", regions[3].path);
325 EXPECT_EQ("[vsys call]", regions[4].path);
326 }
327
328 } // namespace debug
329 } // namespace base
330