Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: base/debug/proc_maps_linux_unittest.cc

Issue 18178015: Implement /proc/self/maps/ parsing code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/debug/proc_maps_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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 #include "base/files/file_path.h"
7 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12 namespace debug {
13
14 TEST(ProcMapsTest, Empty) {
15 std::vector<MappedMemoryRegion> regions;
16 EXPECT_TRUE(ParseProcMaps("", &regions));
17 EXPECT_EQ(0u, regions.size());
18 }
19
20 TEST(ProcMapsTest, NoSpaces) {
21 static const char kNoSpaces[] =
22 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n";
23
24 std::vector<MappedMemoryRegion> regions;
25 ASSERT_TRUE(ParseProcMaps(kNoSpaces, &regions));
26 ASSERT_EQ(1u, regions.size());
27
28 EXPECT_EQ(0x00400000u, regions[0].start);
29 EXPECT_EQ(0x0040b000u, regions[0].end);
30 EXPECT_EQ(0x00002200u, regions[0].offset);
31 EXPECT_EQ("/bin/cat", regions[0].path);
32 }
33
34 TEST(ProcMapsTest, Spaces) {
35 static const char kSpaces[] =
36 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n";
37
38 std::vector<MappedMemoryRegion> regions;
39 ASSERT_TRUE(ParseProcMaps(kSpaces, &regions));
40 ASSERT_EQ(1u, regions.size());
41
42 EXPECT_EQ(0x00400000u, regions[0].start);
43 EXPECT_EQ(0x0040b000u, regions[0].end);
44 EXPECT_EQ(0x00002200u, regions[0].offset);
45 EXPECT_EQ("/bin/space cat", regions[0].path);
46 }
47
48 TEST(ProcMapsTest, NoNewline) {
49 static const char kNoSpaces[] =
50 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat";
51
52 std::vector<MappedMemoryRegion> regions;
53 ASSERT_FALSE(ParseProcMaps(kNoSpaces, &regions));
54 }
55
56 TEST(ProcMapsTest, NoPath) {
57 static const char kNoPath[] =
58 "00400000-0040b000 rw-p 00000000 00:00 0 \n";
59
60 std::vector<MappedMemoryRegion> regions;
61 ASSERT_TRUE(ParseProcMaps(kNoPath, &regions));
62 ASSERT_EQ(1u, regions.size());
63
64 EXPECT_EQ(0x00400000u, regions[0].start);
65 EXPECT_EQ(0x0040b000u, regions[0].end);
66 EXPECT_EQ(0x00000000u, regions[0].offset);
67 EXPECT_EQ("", regions[0].path);
68 }
69
70 TEST(ProcMapsTest, Heap) {
71 static const char kHeap[] =
72 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n";
73
74 std::vector<MappedMemoryRegion> regions;
75 ASSERT_TRUE(ParseProcMaps(kHeap, &regions));
76 ASSERT_EQ(1u, regions.size());
77
78 EXPECT_EQ(0x022ac000u, regions[0].start);
79 EXPECT_EQ(0x022cd000u, regions[0].end);
80 EXPECT_EQ(0x00000000u, regions[0].offset);
81 EXPECT_EQ("[heap]", regions[0].path);
82 }
83
84 #if defined(ARCH_CPU_32_BITS)
85 TEST(ProcMapsTest, Stack32) {
86 static const char kStack[] =
87 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n";
88
89 std::vector<MappedMemoryRegion> regions;
90 ASSERT_TRUE(ParseProcMaps(kStack, &regions));
91 ASSERT_EQ(1u, regions.size());
92
93 EXPECT_EQ(0xbeb04000u, regions[0].start);
94 EXPECT_EQ(0xbeb25000u, regions[0].end);
95 EXPECT_EQ(0x00000000u, regions[0].offset);
96 EXPECT_EQ("[stack]", regions[0].path);
97 }
98 #elif defined(ARCH_CPU_64_BITS)
99 TEST(ProcMapsTest, Stack64) {
100 static const char kStack[] =
101 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n";
102
103 std::vector<MappedMemoryRegion> regions;
104 ASSERT_TRUE(ParseProcMaps(kStack, &regions));
105 ASSERT_EQ(1u, regions.size());
106
107 EXPECT_EQ(0x7fff69c5b000u, regions[0].start);
108 EXPECT_EQ(0x7fff69c7d000u, regions[0].end);
109 EXPECT_EQ(0x00000000u, regions[0].offset);
110 EXPECT_EQ("[stack]", regions[0].path);
111 }
112 #endif
113
114 TEST(ProcMapsTest, Multiple) {
115 static const char kMultiple[] =
116 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n"
117 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n"
118 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n";
119
120 std::vector<MappedMemoryRegion> regions;
121 ASSERT_TRUE(ParseProcMaps(kMultiple, &regions));
122 ASSERT_EQ(3u, regions.size());
123
124 EXPECT_EQ(0x00400000u, regions[0].start);
125 EXPECT_EQ(0x0040b000u, regions[0].end);
126 EXPECT_EQ(0x00000000u, regions[0].offset);
127 EXPECT_EQ("/bin/cat", regions[0].path);
128
129 EXPECT_EQ(0x0060a000u, regions[1].start);
130 EXPECT_EQ(0x0060b000u, regions[1].end);
131 EXPECT_EQ(0x0000a000u, regions[1].offset);
132 EXPECT_EQ("/bin/cat", regions[1].path);
133
134 EXPECT_EQ(0x0060b000u, regions[2].start);
135 EXPECT_EQ(0x0060c000u, regions[2].end);
136 EXPECT_EQ(0x0000b000u, regions[2].offset);
137 EXPECT_EQ("/bin/cat", regions[2].path);
138 }
139
140 TEST(ProcMapsTest, Permissions) {
141 static struct {
142 const char* input;
143 uint8 permissions;
144 } kTestCases[] = {
145 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0},
146 {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0},
147 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n",
148 MappedMemoryRegion::READ},
149 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n",
150 MappedMemoryRegion::WRITE},
151 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n",
152 MappedMemoryRegion::EXECUTE},
153 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n",
154 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
155 MappedMemoryRegion::EXECUTE},
156 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n",
157 MappedMemoryRegion::PRIVATE},
158 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n",
159 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE},
160 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n",
161 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE},
162 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n",
163 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
164 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
165 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
166 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
167 };
168
169 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
170 SCOPED_TRACE(
171 base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input));
172
173 std::vector<MappedMemoryRegion> regions;
174 EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, &regions));
175 EXPECT_EQ(1u, regions.size());
176 if (regions.empty())
177 continue;
178 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions);
179 }
180 }
181
182 TEST(ProcMapsTest, ReadProcMaps) {
183 std::string proc_maps;
184 ASSERT_TRUE(ReadProcMaps(&proc_maps));
185
186 std::vector<MappedMemoryRegion> regions;
187 ASSERT_TRUE(ParseProcMaps(proc_maps, &regions));
188 ASSERT_FALSE(regions.empty());
189
190 // We should be able to find both the current executable as well as the stack
191 // mapped into memory. Use the address of |proc_maps| as a way of finding the
192 // stack.
193 FilePath exe_path;
194 EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path));
195 uintptr_t address = reinterpret_cast<uintptr_t>(&proc_maps);
196 bool found_exe = false;
197 bool found_stack = false;
198 for (size_t i = 0; i < regions.size(); ++i) {
199 if (regions[i].path == exe_path.value()) {
200 found_exe = true;
Mark Mentovai 2013/07/03 13:26:36 EXPECT_FALSE(found_exe) before this line.
scherkus (not reviewing) 2013/07/04 01:26:08 Addressed in https://codereview.chromium.org/18328
201 }
202
203 if (address >= regions[i].start && address < regions[i].end) {
204 EXPECT_EQ("[stack]", regions[i].path);
205 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::READ);
206 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::WRITE);
207 EXPECT_FALSE(regions[i].permissions & MappedMemoryRegion::EXECUTE);
208 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::PRIVATE);
209 EXPECT_FALSE(found_stack) << "Found duplicate stacks";
210 found_stack = true;
211 }
212 }
213
214 EXPECT_TRUE(found_exe);
215 EXPECT_TRUE(found_stack);
216 }
217
218 TEST(ProcMapsTest, MissingFields) {
219 static const char* kTestCases[] = {
220 "00400000\n", // Missing end + beyond.
221 "00400000-0040b000\n", // Missing perms + beyond.
222 "00400000-0040b000 r-xp\n", // Missing offset + beyond.
223 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond.
224 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond.
225 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms.
226 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset.
227 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode.
228 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end.
229 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start.
230 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device.
231 };
232
233 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
234 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
235 std::vector<MappedMemoryRegion> regions;
236 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
237 }
238 }
239
240 TEST(ProcMapsTest, InvalidInput) {
241 static const char* kTestCases[] = {
242 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
243 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n",
244 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n",
245 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n",
246 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n",
247 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n",
248 };
249
250 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
251 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
252 std::vector<MappedMemoryRegion> regions;
253 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
254 }
255 }
256
257 } // namespace debug
258 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/proc_maps_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698