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

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: fixes 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 r--s 00000000 fc:00 794418 /bin/cat\n",
146 MappedMemoryRegion::READ},
147 {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n",
148 MappedMemoryRegion::WRITE},
149 {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n",
150 MappedMemoryRegion::EXECUTE},
151 {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n",
152 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
153 MappedMemoryRegion::EXECUTE},
154 {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n",
155 MappedMemoryRegion::PRIVATE},
156 {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n",
157 MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE},
158 {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n",
159 MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE},
160 {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n",
161 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
162 {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
163 MappedMemoryRegion::READ | MappedMemoryRegion::WRITE |
164 MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE},
165 { NULL, 0 },
166 };
167
168 for (size_t i = 0; kTestCases[i].input != NULL; ++i) {
169 SCOPED_TRACE(
170 base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input));
171
172 std::vector<MappedMemoryRegion> regions;
173 EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, &regions));
174 EXPECT_EQ(1u, regions.size());
175 if (regions.empty())
176 continue;
177 EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions);
178 }
179 }
180
181 TEST(ProcMapsTest, ReadProcMaps) {
182 std::string proc_maps;
183 ASSERT_TRUE(ReadProcMaps(&proc_maps));
184
185 std::vector<MappedMemoryRegion> regions;
186 ASSERT_TRUE(ParseProcMaps(proc_maps, &regions));
187 ASSERT_FALSE(regions.empty());
188
189 // We should be able to find where our stack is mapped by looking at the
190 // address of |proc_maps|.
191 uintptr_t address = reinterpret_cast<uintptr_t>(&proc_maps);
192 for (size_t i = 0; i < regions.size(); ++i) {
193 if (address >= regions[i].start && address < regions[i].end) {
194 EXPECT_EQ("[stack]", regions[i].path);
195 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::READ);
196 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::WRITE);
197 EXPECT_FALSE(regions[i].permissions & MappedMemoryRegion::EXECUTE);
198 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::PRIVATE);
199 return;
200 }
201 }
202
203 ADD_FAILURE() << "Did not find stack in /proc/self/maps.";
204 }
205
206 TEST(ProcMapsTest, MissingFields) {
207 static const char* kTestCases[] = {
208 "00400000\n", // Missing end + beyond.
209 "00400000-0040b000\n", // Missing perms + beyond.
210 "00400000-0040b000 r-xp\n", // Missing offset + beyond.
211 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond.
212 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond.
213 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms.
214 "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset.
215 "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode.
216 "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end.
217 "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start.
218 "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device.
219 };
220
221 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
222 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
223 std::vector<MappedMemoryRegion> regions;
224 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
225 }
226 }
227
228 TEST(ProcMapsTest, InvalidInput) {
229 static const char* kTestCases[] = {
230 "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n",
231 "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n",
232 "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n",
233 "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n",
234 "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n",
235 "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n",
236 };
237
238 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
239 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i]));
240 std::vector<MappedMemoryRegion> regions;
241 EXPECT_FALSE(ParseProcMaps(kTestCases[i], &regions));
242 }
243 }
244
245 } // namespace debug
246 } // 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