xref: /aosp_15_r20/external/cronet/base/debug/proc_maps_linux.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 
7 #include <fcntl.h>
8 #include <stddef.h>
9 
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_file.h"
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "build/build_config.h"
15 
16 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
17 #include <inttypes.h>
18 #endif
19 
20 namespace base {
21 namespace debug {
22 
23 // Scans |proc_maps| starting from |pos| returning true if the gate VMA was
24 // found, otherwise returns false.
ContainsGateVMA(std::string * proc_maps,size_t pos)25 static bool ContainsGateVMA(std::string* proc_maps, size_t pos) {
26 #if defined(ARCH_CPU_ARM_FAMILY)
27   // The gate VMA on ARM kernels is the interrupt vectors page.
28   return proc_maps->find(" [vectors]\n", pos) != std::string::npos;
29 #elif defined(ARCH_CPU_X86_64)
30   // The gate VMA on x86 64-bit kernels is the virtual system call page.
31   return proc_maps->find(" [vsyscall]\n", pos) != std::string::npos;
32 #else
33   // Otherwise assume there is no gate VMA in which case we shouldn't
34   // get duplicate entires.
35   return false;
36 #endif
37 }
38 
ReadProcMaps(std::string * proc_maps)39 bool ReadProcMaps(std::string* proc_maps) {
40   // seq_file only writes out a page-sized amount on each call. Refer to header
41   // file for details.
42   const size_t read_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
43 
44   base::ScopedFD fd(HANDLE_EINTR(open("/proc/self/maps", O_RDONLY)));
45   if (!fd.is_valid()) {
46     DPLOG(ERROR) << "Couldn't open /proc/self/maps";
47     return false;
48   }
49   proc_maps->clear();
50 
51   while (true) {
52     // To avoid a copy, resize |proc_maps| so read() can write directly into it.
53     // Compute |buffer| afterwards since resize() may reallocate.
54     size_t pos = proc_maps->size();
55     proc_maps->resize(pos + read_size);
56     void* buffer = &(*proc_maps)[pos];
57 
58     ssize_t bytes_read = HANDLE_EINTR(read(fd.get(), buffer, read_size));
59     if (bytes_read < 0) {
60       DPLOG(ERROR) << "Couldn't read /proc/self/maps";
61       proc_maps->clear();
62       return false;
63     }
64 
65     // ... and don't forget to trim off excess bytes.
66     proc_maps->resize(pos + static_cast<size_t>(bytes_read));
67 
68     if (bytes_read == 0)
69       break;
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;
84 }
85 
ParseProcMaps(const std::string & input,std::vector<MappedMemoryRegion> * regions_out)86 bool ParseProcMaps(const std::string& input,
87                    std::vector<MappedMemoryRegion>* regions_out) {
88   CHECK(regions_out);
89   std::vector<MappedMemoryRegion> regions;
90 
91   // This isn't async safe nor terribly efficient, but it doesn't need to be at
92   // this point in time.
93   std::vector<std::string> lines = SplitString(
94       input, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
95 
96   for (size_t i = 0; i < lines.size(); ++i) {
97     // Due to splitting on '\n' the last line should be empty.
98     if (i == lines.size() - 1) {
99       if (!lines[i].empty()) {
100         DLOG(WARNING) << "Last line not empty";
101         return false;
102       }
103       break;
104     }
105 
106     MappedMemoryRegion region;
107     const char* line = lines[i].c_str();
108     char permissions[5] = {'\0'};  // Ensure NUL-terminated string.
109     uint8_t dev_major = 0;
110     uint8_t dev_minor = 0;
111     long inode = 0;
112     int path_index = 0;
113 
114     // Sample format from man 5 proc:
115     //
116     // address           perms offset  dev   inode   pathname
117     // 08048000-08056000 r-xp 00000000 03:0c 64593   /usr/sbin/gpm
118     //
119     // The final %n term captures the offset in the input string, which is used
120     // to determine the path name. It *does not* increment the return value.
121     // Refer to man 3 sscanf for details.
122     if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4c %llx %hhx:%hhx %ld %n",
123                &region.start, &region.end, permissions, &region.offset,
124                &dev_major, &dev_minor, &inode, &path_index) < 7) {
125       DPLOG(WARNING) << "sscanf failed for line: " << line;
126       return false;
127     }
128 
129     region.permissions = 0;
130 
131     if (permissions[0] == 'r')
132       region.permissions |= MappedMemoryRegion::READ;
133     else if (permissions[0] != '-')
134       return false;
135 
136     if (permissions[1] == 'w')
137       region.permissions |= MappedMemoryRegion::WRITE;
138     else if (permissions[1] != '-')
139       return false;
140 
141     if (permissions[2] == 'x')
142       region.permissions |= MappedMemoryRegion::EXECUTE;
143     else if (permissions[2] != '-')
144       return false;
145 
146     if (permissions[3] == 'p')
147       region.permissions |= MappedMemoryRegion::PRIVATE;
148     else if (permissions[3] != 's' && permissions[3] != 'S')  // Shared memory.
149       return false;
150 
151     // Pushing then assigning saves us a string copy.
152     regions.push_back(region);
153     regions.back().path.assign(line + path_index);
154   }
155 
156   regions_out->swap(regions);
157   return true;
158 }
159 
160 }  // namespace debug
161 }  // namespace base
162