OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/debug/proc_maps_linux.h" | 5 #include "base/debug/proc_maps_linux.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 namespace debug { | 13 namespace debug { |
13 | 14 |
14 TEST(ProcMapsTest, Empty) { | 15 TEST(ProcMapsTest, Empty) { |
15 std::vector<MappedMemoryRegion> regions; | 16 std::vector<MappedMemoryRegion> regions; |
16 EXPECT_TRUE(ParseProcMaps("", ®ions)); | 17 EXPECT_TRUE(ParseProcMaps("", ®ions)); |
17 EXPECT_EQ(0u, regions.size()); | 18 EXPECT_EQ(0u, regions.size()); |
18 } | 19 } |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 ASSERT_FALSE(regions.empty()); | 189 ASSERT_FALSE(regions.empty()); |
189 | 190 |
190 // We should be able to find both the current executable as well as the stack | 191 // 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 // mapped into memory. Use the address of |proc_maps| as a way of finding the |
192 // stack. | 193 // stack. |
193 FilePath exe_path; | 194 FilePath exe_path; |
194 EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path)); | 195 EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path)); |
195 uintptr_t address = reinterpret_cast<uintptr_t>(&proc_maps); | 196 uintptr_t address = reinterpret_cast<uintptr_t>(&proc_maps); |
196 bool found_exe = false; | 197 bool found_exe = false; |
197 bool found_stack = false; | 198 bool found_stack = false; |
| 199 bool found_address = false; |
198 for (size_t i = 0; i < regions.size(); ++i) { | 200 for (size_t i = 0; i < regions.size(); ++i) { |
199 if (regions[i].path == exe_path.value()) { | 201 if (regions[i].path == exe_path.value()) { |
| 202 // It's OK to find the executable mapped multiple times as there'll be |
| 203 // multiple sections (e.g., text, data). |
200 found_exe = true; | 204 found_exe = true; |
201 } | 205 } |
202 | 206 |
203 if (address >= regions[i].start && address < regions[i].end) { | 207 if (regions[i].path == "[stack]") { |
204 EXPECT_EQ("[stack]", regions[i].path); | 208 // Only check if |address| lies within the real stack when not running |
| 209 // Valgrind, otherwise |address| will be on a stack that Valgrind creates. |
| 210 if (!RunningOnValgrind()) { |
| 211 EXPECT_GE(address, regions[i].start); |
| 212 EXPECT_LT(address, regions[i].end); |
| 213 } |
| 214 |
205 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::READ); | 215 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::READ); |
206 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::WRITE); | 216 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::WRITE); |
207 EXPECT_FALSE(regions[i].permissions & MappedMemoryRegion::EXECUTE); | 217 EXPECT_FALSE(regions[i].permissions & MappedMemoryRegion::EXECUTE); |
208 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::PRIVATE); | 218 EXPECT_TRUE(regions[i].permissions & MappedMemoryRegion::PRIVATE); |
209 EXPECT_FALSE(found_stack) << "Found duplicate stacks"; | 219 EXPECT_FALSE(found_stack) << "Found duplicate stacks"; |
210 found_stack = true; | 220 found_stack = true; |
211 } | 221 } |
| 222 |
| 223 if (address >= regions[i].start && address < regions[i].end) { |
| 224 EXPECT_FALSE(found_address) << "Found same address in multiple regions"; |
| 225 found_address = true; |
| 226 } |
212 } | 227 } |
213 | 228 |
214 EXPECT_TRUE(found_exe); | 229 EXPECT_TRUE(found_exe); |
215 EXPECT_TRUE(found_stack); | 230 EXPECT_TRUE(found_stack); |
| 231 EXPECT_TRUE(found_address); |
216 } | 232 } |
217 | 233 |
218 TEST(ProcMapsTest, MissingFields) { | 234 TEST(ProcMapsTest, MissingFields) { |
219 static const char* kTestCases[] = { | 235 static const char* kTestCases[] = { |
220 "00400000\n", // Missing end + beyond. | 236 "00400000\n", // Missing end + beyond. |
221 "00400000-0040b000\n", // Missing perms + beyond. | 237 "00400000-0040b000\n", // Missing perms + beyond. |
222 "00400000-0040b000 r-xp\n", // Missing offset + beyond. | 238 "00400000-0040b000 r-xp\n", // Missing offset + beyond. |
223 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. | 239 "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. |
224 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. | 240 "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. |
225 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. | 241 "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. |
(...skipping 23 matching lines...) Expand all Loading... |
249 | 265 |
250 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 266 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
251 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); | 267 SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); |
252 std::vector<MappedMemoryRegion> regions; | 268 std::vector<MappedMemoryRegion> regions; |
253 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); | 269 EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
254 } | 270 } |
255 } | 271 } |
256 | 272 |
257 } // namespace debug | 273 } // namespace debug |
258 } // namespace base | 274 } // namespace base |
OLD | NEW |