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

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: eliminated copy and added test for 'S' 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
« base/debug/proc_maps_linux.cc ('K') | « 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/logging.h"
7 #include "base/strings/stringprintf.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace base {
11 namespace debug {
12
13 TEST(ProcMapsTest, Empty) {
14 std::vector<MappedMemoryRegion> regions;
15 EXPECT_TRUE(ParseProcMaps("", &regions));
16 EXPECT_EQ(0u, regions.size());
17 }
18
19 TEST(ProcMapsTest, NoSpaces) {
20 static const char kNoSpaces[] =
21 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n";
22
23 std::vector<MappedMemoryRegion> regions;
24 ASSERT_TRUE(ParseProcMaps(kNoSpaces, &regions));
25 ASSERT_EQ(1u, regions.size());
26
27 EXPECT_EQ(0x00400000u, regions[0].start);
28 EXPECT_EQ(0x0040b000u, regions[0].end);
29 EXPECT_EQ(0x00002200u, regions[0].offset);
30 EXPECT_EQ("/bin/cat", regions[0].path);
31 }
32
33 TEST(ProcMapsTest, Spaces) {
34 static const char kSpaces[] =
35 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n";
36
37 std::vector<MappedMemoryRegion> regions;
38 ASSERT_TRUE(ParseProcMaps(kSpaces, &regions));
39 ASSERT_EQ(1u, regions.size());
40
41 EXPECT_EQ(0x00400000u, regions[0].start);
42 EXPECT_EQ(0x0040b000u, regions[0].end);
43 EXPECT_EQ(0x00002200u, regions[0].offset);
44 EXPECT_EQ("/bin/space cat", regions[0].path);
45 }
46
47 TEST(ProcMapsTest, NoNewline) {
48 static const char kNoSpaces[] =
49 "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat";
50
51 std::vector<MappedMemoryRegion> regions;
52 ASSERT_FALSE(ParseProcMaps(kNoSpaces, &regions));
53 }
54
55 TEST(ProcMapsTest, NoPath) {
56 static const char kNoPath[] =
57 "00400000-0040b000 rw-p 00000000 00:00 0 \n";
58
59 std::vector<MappedMemoryRegion> regions;
60 ASSERT_TRUE(ParseProcMaps(kNoPath, &regions));
61 ASSERT_EQ(1u, regions.size());
62
63 EXPECT_EQ(0x00400000u, regions[0].start);
64 EXPECT_EQ(0x0040b000u, regions[0].end);
65 EXPECT_EQ(0x00000000u, regions[0].offset);
66 EXPECT_EQ("", regions[0].path);
67 }
68
69 TEST(ProcMapsTest, Heap) {
70 static const char kHeap[] =
71 "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n";
72
73 std::vector<MappedMemoryRegion> regions;
74 ASSERT_TRUE(ParseProcMaps(kHeap, &regions));
75 ASSERT_EQ(1u, regions.size());
76
77 EXPECT_EQ(0x022ac000u, regions[0].start);
78 EXPECT_EQ(0x022cd000u, regions[0].end);
79 EXPECT_EQ(0x00000000u, regions[0].offset);
80 EXPECT_EQ("[heap]", regions[0].path);
81 }
82
83 #if defined(ARCH_CPU_32_BITS)
84 TEST(ProcMapsTest, Stack32) {
85 static const char kStack[] =
86 "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n";
87
88 std::vector<MappedMemoryRegion> regions;
89 ASSERT_TRUE(ParseProcMaps(kStack, &regions));
90 ASSERT_EQ(1u, regions.size());
91
92 EXPECT_EQ(0xbeb04000u, regions[0].start);
93 EXPECT_EQ(0xbeb25000u, regions[0].end);
94 EXPECT_EQ(0x00000000u, regions[0].offset);
95 EXPECT_EQ("[stack]", regions[0].path);
96 }
97 #elif defined(ARCH_CPU_64_BITS)
98 TEST(ProcMapsTest, Stack64) {
99 static const char kStack[] =
100 "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n";
101
102 std::vector<MappedMemoryRegion> regions;
103 ASSERT_TRUE(ParseProcMaps(kStack, &regions));
104 ASSERT_EQ(1u, regions.size());
105
106 EXPECT_EQ(0x7fff69c5b000u, regions[0].start);
107 EXPECT_EQ(0x7fff69c7d000u, regions[0].end);
108 EXPECT_EQ(0x00000000u, regions[0].offset);
109 EXPECT_EQ("[stack]", regions[0].path);
110 }
111 #endif
112
113 TEST(ProcMapsTest, Multiple) {
114 static const char kMultiple[] =
115 "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n"
116 "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n"
117 "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n";
118
119 std::vector<MappedMemoryRegion> regions;
120 ASSERT_TRUE(ParseProcMaps(kMultiple, &regions));
121 ASSERT_EQ(3u, regions.size());
122
123 EXPECT_EQ(0x00400000u, regions[0].start);
124 EXPECT_EQ(0x0040b000u, regions[0].end);
125 EXPECT_EQ(0x00000000u, regions[0].offset);
126 EXPECT_EQ("/bin/cat", regions[0].path);
127
128 EXPECT_EQ(0x0060a000u, regions[1].start);
129 EXPECT_EQ(0x0060b000u, regions[1].end);
130 EXPECT_EQ(0x0000a000u, regions[1].offset);
131 EXPECT_EQ("/bin/cat", regions[1].path);
132
133 EXPECT_EQ(0x0060b000u, regions[2].start);
134 EXPECT_EQ(0x0060c000u, regions[2].end);
135 EXPECT_EQ(0x0000b000u, regions[2].offset);
136 EXPECT_EQ("/bin/cat", regions[2].path);
137 }
138
139 TEST(ProcMapsTest, Permissions) {
140 static struct {
141 const char* input;
142 uint8 permissions;
143 } kTestCases[] = {
144 {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0},
145 {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0},
146 {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n",
147 MappedMemoryRegion::READ},
148 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n",
149 MappedMemoryRegion::WRITE},
150 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n",
151 MappedMemoryRegion::EXECUTE},
152 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n",
153 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
154 MappedMemoryRegion::EXECUTE},
155 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n",
156 MappedMemoryRegion::PRIVATE},
157 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n",
158 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE},
159 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n",
160 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE},
161 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n",
162 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
163 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
164 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
165 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
166 { NULL, 0 },
167 };
168
169 for (size_t i = 0; kTestCases[i].input != NULL; ++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 where our stack is mapped by looking at the
191 // address of |proc_maps|.
192 uintptr_t address = reinterpret_cast<uintptr_t>(&proc_maps);
193 for (size_t i = 0; i < regions.size(); ++i) {
194 if (address >= regions[i].start && address < regions[i].end) {
195 EXPECT_EQ("[stack]", regions[i].path);
196 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::READ);
197 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::WRITE);
198 EXPECT_FALSE(regions[i].permissions & MappedMemoryRegion::EXECUTE);
199 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::PRIVATE);
200 return;
Mark Mentovai 2013/07/02 22:21:36 Instead of returning here, stay in the loop to mak
scherkus (not reviewing) 2013/07/02 23:07:47 Done.
201 }
202 }
203
204 ADD_FAILURE() << "Did not find stack in /proc/self/maps.";
205 }
206
207 TEST(ProcMapsTest, MissingFields) {
208 static const char* kTestCases[] = {
209 "00400000\n", // Missing end + beyond.
210 "00400000-0040b000\n", // Missing perms + beyond.
211 "00400000-0040b000 r-xp\n", // Missing offset + beyond.
212 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond.
213 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond.
214 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms.
215 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset.
216 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode.
217 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end.
218 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start.
219 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device.
220 };
221
222 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
223 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
224 std::vector<MappedMemoryRegion> regions;
225 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
226 }
227 }
228
229 TEST(ProcMapsTest, InvalidInput) {
230 static const char* kTestCases[] = {
231 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
232 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n",
233 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n",
234 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n",
235 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n",
236 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n",
237 };
238
239 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
240 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
241 std::vector<MappedMemoryRegion> regions;
242 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
243 }
244 }
245
246 } // namespace debug
247 } // namespace base
OLDNEW
« base/debug/proc_maps_linux.cc ('K') | « base/debug/proc_maps_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698