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

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

Issue 18661009: Update ReadProcMaps() to reflect lack of atomicity when reading /proc/self/maps. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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.h ('k') | base/debug/proc_maps_linux_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 6
7 #include <fcntl.h>
8
7 #if defined(OS_LINUX) 9 #if defined(OS_LINUX)
8 #include <inttypes.h> 10 #include <inttypes.h>
9 #endif 11 #endif
10 12
11 #include "base/file_util.h" 13 #include "base/file_util.h"
12 #include "base/strings/string_split.h" 14 #include "base/strings/string_split.h"
13 15
14 #if defined(OS_ANDROID) 16 #if defined(OS_ANDROID)
15 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which 17 // Bionic's inttypes.h defines PRI/SCNxPTR as an unsigned long int, which
16 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int: 18 // is incompatible with Bionic's stdint.h defining uintptr_t as a unsigned int:
17 // https://code.google.com/p/android/issues/detail?id=57218 19 // https://code.google.com/p/android/issues/detail?id=57218
18 #undef SCNxPTR 20 #undef SCNxPTR
19 #define SCNxPTR "x" 21 #define SCNxPTR "x"
20 #endif 22 #endif
21 23
22 namespace base { 24 namespace base {
23 namespace debug { 25 namespace debug {
24 26
27 // Scans |proc_maps| starting from |pos| returning true if the gate VMA was
28 // found, otherwise returns false.
29 static bool ContainsGateVMA(std::string* proc_maps, size_t pos) {
30 #if defined(ARCH_CPU_ARM_FAMILY)
31 // The gate VMA on ARM kernels is the interrupt vectors page.
32 return proc_maps->find("[vectors]", pos) != std::string::npos;
Mark Mentovai 2013/09/05 16:31:17 Wanna look for the trailing \n too? And one leadin
scherkus (not reviewing) 2013/09/05 17:24:34 Done.
33 #elif defined(ARCH_CPU_X86_64)
34 // The gate VMA on x86 64-bit kernels is the virtual system call page.
35 return proc_maps->find("[vsyscall]", pos) != std::string::npos;
36 #else
37 // Otherwise assume there is no gate VMA in which case we shouldn't
38 // get duplicate entires.
39 return false;
40 #endif
41 }
42
25 bool ReadProcMaps(std::string* proc_maps) { 43 bool ReadProcMaps(std::string* proc_maps) {
26 FilePath proc_maps_path("/proc/self/maps"); 44 // seq_file only writes out a page-sized amount on each call. Refer to header
27 return ReadFileToString(proc_maps_path, proc_maps); 45 // file for details.
46 const long kBufferSize = sysconf(_SC_PAGESIZE);
47 scoped_ptr<char[]> buffer(new char[kBufferSize]);
48
49 int fd = HANDLE_EINTR(open("/proc/self/maps", O_RDONLY));
50 if (fd == -1) {
51 DPLOG(ERROR) << "Couldn't open /proc/self/maps";
52 return false;
53 }
54 file_util::ScopedFD fd_closer(&fd);
55
56 while (true) {
57 ssize_t bytes_read = HANDLE_EINTR(read(fd, buffer.get(), kBufferSize));
58 if (bytes_read < 0) {
59 DPLOG(ERROR) << "Couldn't read /proc/self/maps";
60 return false;
61 }
62
63 if (bytes_read == 0)
64 break;
65
66 // Use 2-arg version of append() as |buffer| *is not* NUL-terminated. This
67 // also avoids an unnecessary length computation.
68 size_t pos = proc_maps->size();
Mark Mentovai 2013/09/05 16:31:17 You haven’t documented that proc_maps should start
scherkus (not reviewing) 2013/09/05 17:24:34 Done and test added.
69 proc_maps->append(buffer.get(), bytes_read);
70
71 // The gate VMA is handled as a special case after seq_file has finished
72 // iterating through all entries in the virtual memory table.
73 //
74 // Unfortunately, if additional entries are added at this point in time
75 // seq_file gets confused and the next call to read() will return duplicate
76 // entries including the gate VMA again.
77 //
78 // Avoid this by searching for the gate VMA and breaking early.
79 if (ContainsGateVMA(proc_maps, pos))
80 break;
81 }
82
83 return true;
28 } 84 }
29 85
30 bool ParseProcMaps(const std::string& input, 86 bool ParseProcMaps(const std::string& input,
31 std::vector<MappedMemoryRegion>* regions_out) { 87 std::vector<MappedMemoryRegion>* regions_out) {
32 std::vector<MappedMemoryRegion> regions; 88 std::vector<MappedMemoryRegion> regions;
33 89
34 // This isn't async safe nor terribly efficient, but it doesn't need to be at 90 // This isn't async safe nor terribly efficient, but it doesn't need to be at
35 // this point in time. 91 // this point in time.
36 std::vector<std::string> lines; 92 std::vector<std::string> lines;
37 SplitString(input, '\n', &lines); 93 SplitString(input, '\n', &lines);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 regions.push_back(region); 148 regions.push_back(region);
93 regions.back().path.assign(line + path_index); 149 regions.back().path.assign(line + path_index);
94 } 150 }
95 151
96 regions_out->swap(regions); 152 regions_out->swap(regions);
97 return true; 153 return true;
98 } 154 }
99 155
100 } // namespace debug 156 } // namespace debug
101 } // namespace base 157 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/proc_maps_linux.h ('k') | base/debug/proc_maps_linux_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698