1*e7c5e80fSMitch Phillips /*
2*e7c5e80fSMitch Phillips * Copyright (C) 2018 The Android Open Source Project
3*e7c5e80fSMitch Phillips *
4*e7c5e80fSMitch Phillips * Licensed under the Apache License, Version 2.0 (the "License");
5*e7c5e80fSMitch Phillips * you may not use this file except in compliance with the License.
6*e7c5e80fSMitch Phillips * You may obtain a copy of the License at
7*e7c5e80fSMitch Phillips *
8*e7c5e80fSMitch Phillips * http://www.apache.org/licenses/LICENSE-2.0
9*e7c5e80fSMitch Phillips *
10*e7c5e80fSMitch Phillips * Unless required by applicable law or agreed to in writing, software
11*e7c5e80fSMitch Phillips * distributed under the License is distributed on an "AS IS" BASIS,
12*e7c5e80fSMitch Phillips * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e7c5e80fSMitch Phillips * See the License for the specific language governing permissions and
14*e7c5e80fSMitch Phillips * limitations under the License.
15*e7c5e80fSMitch Phillips */
16*e7c5e80fSMitch Phillips
17*e7c5e80fSMitch Phillips #pragma once
18*e7c5e80fSMitch Phillips
19*e7c5e80fSMitch Phillips #include <inttypes.h>
20*e7c5e80fSMitch Phillips #include <stdint.h>
21*e7c5e80fSMitch Phillips #include <stdlib.h>
22*e7c5e80fSMitch Phillips #include <string.h>
23*e7c5e80fSMitch Phillips #include <sys/mman.h>
24*e7c5e80fSMitch Phillips #include <sys/types.h>
25*e7c5e80fSMitch Phillips
26*e7c5e80fSMitch Phillips #include <functional>
27*e7c5e80fSMitch Phillips #include <string>
28*e7c5e80fSMitch Phillips #include <vector>
29*e7c5e80fSMitch Phillips
30*e7c5e80fSMitch Phillips #include <android-base/file.h>
31*e7c5e80fSMitch Phillips #include <android-base/strings.h>
32*e7c5e80fSMitch Phillips
33*e7c5e80fSMitch Phillips namespace android {
34*e7c5e80fSMitch Phillips namespace procinfo {
35*e7c5e80fSMitch Phillips
36*e7c5e80fSMitch Phillips /*
37*e7c5e80fSMitch Phillips * The populated fields of MapInfo corresponds to the following fields of an entry
38*e7c5e80fSMitch Phillips * in /proc/<pid>/maps:
39*e7c5e80fSMitch Phillips *
40*e7c5e80fSMitch Phillips * <start> -<end> ... <pgoff> ... <inode> <name>
41*e7c5e80fSMitch Phillips * 790b07dc6000-790b07dd9000 r--p 00000000 fe:09 21068208 /system/lib64/foo.so
42*e7c5e80fSMitch Phillips * |
43*e7c5e80fSMitch Phillips * |
44*e7c5e80fSMitch Phillips * |___ p - private (!<shared>)
45*e7c5e80fSMitch Phillips * s - <shared>
46*e7c5e80fSMitch Phillips */
47*e7c5e80fSMitch Phillips struct MapInfo {
48*e7c5e80fSMitch Phillips uint64_t start;
49*e7c5e80fSMitch Phillips uint64_t end;
50*e7c5e80fSMitch Phillips // NOTE: It should not be assumed the virtual addresses in range [start,end] all
51*e7c5e80fSMitch Phillips // correspond to valid offsets on the backing file.
52*e7c5e80fSMitch Phillips // See: MappedFileSize().
53*e7c5e80fSMitch Phillips uint16_t flags;
54*e7c5e80fSMitch Phillips uint64_t pgoff;
55*e7c5e80fSMitch Phillips ino_t inode;
56*e7c5e80fSMitch Phillips std::string name;
57*e7c5e80fSMitch Phillips bool shared;
58*e7c5e80fSMitch Phillips
59*e7c5e80fSMitch Phillips // With MTE globals, segments are remapped as anonymous mappings. They're
60*e7c5e80fSMitch Phillips // named specifically to preserve offsets and as much of the basename as
61*e7c5e80fSMitch Phillips // possible. For example,
62*e7c5e80fSMitch Phillips // "[anon:mt:/data/local/tmp/debuggerd_test/arm64/debuggerd_test64+108000]" is
63*e7c5e80fSMitch Phillips // the name of anonymized mapping for debuggerd_test64 of the segment starting
64*e7c5e80fSMitch Phillips // at 0x108000. The kernel only supports 80 characters (excluding the '[anon:'
65*e7c5e80fSMitch Phillips // prefix and ']' suffix, but including the null terminator), and in those
66*e7c5e80fSMitch Phillips // instances, we maintain the offset and as much of the basename as possible
67*e7c5e80fSMitch Phillips // by left-truncation. For example:
68*e7c5e80fSMitch Phillips // "[anon:mt:/data/nativetest64/bionic-unit-tests/bionic-loader-test-libs/libdlext_test.so+e000]"
69*e7c5e80fSMitch Phillips // would become:
70*e7c5e80fSMitch Phillips // "[anon:mt:...ivetest64/bionic-unit-tests/bionic-loader-test-libs/libdlext_test.so+e000]".
71*e7c5e80fSMitch Phillips // For mappings under MTE globals, we thus post-process the name to extract the page offset, and
72*e7c5e80fSMitch Phillips // canonicalize the name.
73*e7c5e80fSMitch Phillips static constexpr const char* kMtePrefix = "[anon:mt:";
74*e7c5e80fSMitch Phillips static constexpr size_t kMtePrefixLength = sizeof(kMtePrefix) - 1;
75*e7c5e80fSMitch Phillips
MaybeExtractMemtagGlobalsInfoMapInfo76*e7c5e80fSMitch Phillips void MaybeExtractMemtagGlobalsInfo() {
77*e7c5e80fSMitch Phillips if (!this->name.starts_with(kMtePrefix)) return;
78*e7c5e80fSMitch Phillips if (this->name.back() != ']') return;
79*e7c5e80fSMitch Phillips
80*e7c5e80fSMitch Phillips size_t offset_to_plus = this->name.rfind('+');
81*e7c5e80fSMitch Phillips if (offset_to_plus == std::string::npos) return;
82*e7c5e80fSMitch Phillips if (sscanf(this->name.c_str() + offset_to_plus + 1, "%" SCNx64 "]", &this->pgoff) != 1) return;
83*e7c5e80fSMitch Phillips
84*e7c5e80fSMitch Phillips this->name =
85*e7c5e80fSMitch Phillips std::string(this->name.begin() + kMtePrefixLength + 2, this->name.begin() + offset_to_plus);
86*e7c5e80fSMitch Phillips }
87*e7c5e80fSMitch Phillips
MapInfoMapInfo88*e7c5e80fSMitch Phillips MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t inode,
89*e7c5e80fSMitch Phillips const char* name, bool shared)
90*e7c5e80fSMitch Phillips : start(start),
91*e7c5e80fSMitch Phillips end(end),
92*e7c5e80fSMitch Phillips flags(flags),
93*e7c5e80fSMitch Phillips pgoff(pgoff),
94*e7c5e80fSMitch Phillips inode(inode),
95*e7c5e80fSMitch Phillips name(name),
96*e7c5e80fSMitch Phillips shared(shared) {
97*e7c5e80fSMitch Phillips MaybeExtractMemtagGlobalsInfo();
98*e7c5e80fSMitch Phillips }
99*e7c5e80fSMitch Phillips
MapInfoMapInfo100*e7c5e80fSMitch Phillips MapInfo(const MapInfo& params)
101*e7c5e80fSMitch Phillips : start(params.start),
102*e7c5e80fSMitch Phillips end(params.end),
103*e7c5e80fSMitch Phillips flags(params.flags),
104*e7c5e80fSMitch Phillips pgoff(params.pgoff),
105*e7c5e80fSMitch Phillips inode(params.inode),
106*e7c5e80fSMitch Phillips name(params.name),
107*e7c5e80fSMitch Phillips shared(params.shared) {}
108*e7c5e80fSMitch Phillips };
109*e7c5e80fSMitch Phillips
110*e7c5e80fSMitch Phillips typedef std::function<void(const MapInfo&)> MapInfoCallback;
111*e7c5e80fSMitch Phillips typedef std::function<void(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
112*e7c5e80fSMitch Phillips ino_t inode, const char* name, bool shared)> MapInfoParamsCallback;
113*e7c5e80fSMitch Phillips
PassSpace(char ** p)114*e7c5e80fSMitch Phillips static inline bool PassSpace(char** p) {
115*e7c5e80fSMitch Phillips if (**p != ' ') {
116*e7c5e80fSMitch Phillips return false;
117*e7c5e80fSMitch Phillips }
118*e7c5e80fSMitch Phillips while (**p == ' ') {
119*e7c5e80fSMitch Phillips (*p)++;
120*e7c5e80fSMitch Phillips }
121*e7c5e80fSMitch Phillips return true;
122*e7c5e80fSMitch Phillips }
123*e7c5e80fSMitch Phillips
PassXdigit(char ** p)124*e7c5e80fSMitch Phillips static inline bool PassXdigit(char** p) {
125*e7c5e80fSMitch Phillips if (!isxdigit(**p)) {
126*e7c5e80fSMitch Phillips return false;
127*e7c5e80fSMitch Phillips }
128*e7c5e80fSMitch Phillips do {
129*e7c5e80fSMitch Phillips (*p)++;
130*e7c5e80fSMitch Phillips } while (isxdigit(**p));
131*e7c5e80fSMitch Phillips return true;
132*e7c5e80fSMitch Phillips }
133*e7c5e80fSMitch Phillips
134*e7c5e80fSMitch Phillips // Parses the given line p pointing at proc/<pid>/maps content buffer and returns true on success
135*e7c5e80fSMitch Phillips // and false on failure parsing. The first new line character of line will be replaced by the
136*e7c5e80fSMitch Phillips // null character and *next_line will point to the character after the null.
137*e7c5e80fSMitch Phillips //
138*e7c5e80fSMitch Phillips // Example of how a parsed line look line:
139*e7c5e80fSMitch Phillips // 00400000-00409000 r-xp 00000000 fc:00 426998 /usr/lib/gvfs/gvfsd-http
ParseMapsFileLine(char * p,uint64_t & start_addr,uint64_t & end_addr,uint16_t & flags,uint64_t & pgoff,ino_t & inode,char ** name,bool & shared,char ** next_line)140*e7c5e80fSMitch Phillips static inline bool ParseMapsFileLine(char* p, uint64_t& start_addr, uint64_t& end_addr, uint16_t& flags,
141*e7c5e80fSMitch Phillips uint64_t& pgoff, ino_t& inode, char** name, bool& shared, char** next_line) {
142*e7c5e80fSMitch Phillips // Make the first new line character null.
143*e7c5e80fSMitch Phillips *next_line = strchr(p, '\n');
144*e7c5e80fSMitch Phillips if (*next_line != nullptr) {
145*e7c5e80fSMitch Phillips **next_line = '\0';
146*e7c5e80fSMitch Phillips (*next_line)++;
147*e7c5e80fSMitch Phillips }
148*e7c5e80fSMitch Phillips
149*e7c5e80fSMitch Phillips char* end;
150*e7c5e80fSMitch Phillips // start_addr
151*e7c5e80fSMitch Phillips start_addr = strtoull(p, &end, 16);
152*e7c5e80fSMitch Phillips if (end == p || *end != '-') {
153*e7c5e80fSMitch Phillips return false;
154*e7c5e80fSMitch Phillips }
155*e7c5e80fSMitch Phillips p = end + 1;
156*e7c5e80fSMitch Phillips // end_addr
157*e7c5e80fSMitch Phillips end_addr = strtoull(p, &end, 16);
158*e7c5e80fSMitch Phillips if (end == p) {
159*e7c5e80fSMitch Phillips return false;
160*e7c5e80fSMitch Phillips }
161*e7c5e80fSMitch Phillips p = end;
162*e7c5e80fSMitch Phillips if (!PassSpace(&p)) {
163*e7c5e80fSMitch Phillips return false;
164*e7c5e80fSMitch Phillips }
165*e7c5e80fSMitch Phillips // flags
166*e7c5e80fSMitch Phillips flags = 0;
167*e7c5e80fSMitch Phillips if (*p == 'r') {
168*e7c5e80fSMitch Phillips flags |= PROT_READ;
169*e7c5e80fSMitch Phillips } else if (*p != '-') {
170*e7c5e80fSMitch Phillips return false;
171*e7c5e80fSMitch Phillips }
172*e7c5e80fSMitch Phillips p++;
173*e7c5e80fSMitch Phillips if (*p == 'w') {
174*e7c5e80fSMitch Phillips flags |= PROT_WRITE;
175*e7c5e80fSMitch Phillips } else if (*p != '-') {
176*e7c5e80fSMitch Phillips return false;
177*e7c5e80fSMitch Phillips }
178*e7c5e80fSMitch Phillips p++;
179*e7c5e80fSMitch Phillips if (*p == 'x') {
180*e7c5e80fSMitch Phillips flags |= PROT_EXEC;
181*e7c5e80fSMitch Phillips } else if (*p != '-') {
182*e7c5e80fSMitch Phillips return false;
183*e7c5e80fSMitch Phillips }
184*e7c5e80fSMitch Phillips p++;
185*e7c5e80fSMitch Phillips if (*p != 'p' && *p != 's') {
186*e7c5e80fSMitch Phillips return false;
187*e7c5e80fSMitch Phillips }
188*e7c5e80fSMitch Phillips shared = *p == 's';
189*e7c5e80fSMitch Phillips
190*e7c5e80fSMitch Phillips p++;
191*e7c5e80fSMitch Phillips if (!PassSpace(&p)) {
192*e7c5e80fSMitch Phillips return false;
193*e7c5e80fSMitch Phillips }
194*e7c5e80fSMitch Phillips // pgoff
195*e7c5e80fSMitch Phillips pgoff = strtoull(p, &end, 16);
196*e7c5e80fSMitch Phillips if (end == p) {
197*e7c5e80fSMitch Phillips return false;
198*e7c5e80fSMitch Phillips }
199*e7c5e80fSMitch Phillips p = end;
200*e7c5e80fSMitch Phillips if (!PassSpace(&p)) {
201*e7c5e80fSMitch Phillips return false;
202*e7c5e80fSMitch Phillips }
203*e7c5e80fSMitch Phillips // major:minor
204*e7c5e80fSMitch Phillips if (!PassXdigit(&p) || *p++ != ':' || !PassXdigit(&p) || !PassSpace(&p)) {
205*e7c5e80fSMitch Phillips return false;
206*e7c5e80fSMitch Phillips }
207*e7c5e80fSMitch Phillips // inode
208*e7c5e80fSMitch Phillips inode = strtoull(p, &end, 10);
209*e7c5e80fSMitch Phillips if (end == p) {
210*e7c5e80fSMitch Phillips return false;
211*e7c5e80fSMitch Phillips }
212*e7c5e80fSMitch Phillips p = end;
213*e7c5e80fSMitch Phillips
214*e7c5e80fSMitch Phillips if (*p != '\0' && !PassSpace(&p)) {
215*e7c5e80fSMitch Phillips return false;
216*e7c5e80fSMitch Phillips }
217*e7c5e80fSMitch Phillips
218*e7c5e80fSMitch Phillips // Assumes that the first new character was replaced with null.
219*e7c5e80fSMitch Phillips *name = p;
220*e7c5e80fSMitch Phillips
221*e7c5e80fSMitch Phillips return true;
222*e7c5e80fSMitch Phillips }
223*e7c5e80fSMitch Phillips
ReadMapFileContent(char * content,const MapInfoParamsCallback & callback)224*e7c5e80fSMitch Phillips inline bool ReadMapFileContent(char* content, const MapInfoParamsCallback& callback) {
225*e7c5e80fSMitch Phillips uint64_t start_addr;
226*e7c5e80fSMitch Phillips uint64_t end_addr;
227*e7c5e80fSMitch Phillips uint16_t flags;
228*e7c5e80fSMitch Phillips uint64_t pgoff;
229*e7c5e80fSMitch Phillips ino_t inode;
230*e7c5e80fSMitch Phillips char* line_start = content;
231*e7c5e80fSMitch Phillips char* next_line;
232*e7c5e80fSMitch Phillips char* name;
233*e7c5e80fSMitch Phillips bool shared;
234*e7c5e80fSMitch Phillips
235*e7c5e80fSMitch Phillips while (line_start != nullptr && *line_start != '\0') {
236*e7c5e80fSMitch Phillips bool parsed = ParseMapsFileLine(line_start, start_addr, end_addr, flags, pgoff,
237*e7c5e80fSMitch Phillips inode, &name, shared, &next_line);
238*e7c5e80fSMitch Phillips if (!parsed) {
239*e7c5e80fSMitch Phillips return false;
240*e7c5e80fSMitch Phillips }
241*e7c5e80fSMitch Phillips
242*e7c5e80fSMitch Phillips line_start = next_line;
243*e7c5e80fSMitch Phillips callback(start_addr, end_addr, flags, pgoff, inode, name, shared);
244*e7c5e80fSMitch Phillips }
245*e7c5e80fSMitch Phillips return true;
246*e7c5e80fSMitch Phillips }
247*e7c5e80fSMitch Phillips
ReadMapFileContent(char * content,const MapInfoCallback & callback)248*e7c5e80fSMitch Phillips inline bool ReadMapFileContent(char* content, const MapInfoCallback& callback) {
249*e7c5e80fSMitch Phillips uint64_t start_addr;
250*e7c5e80fSMitch Phillips uint64_t end_addr;
251*e7c5e80fSMitch Phillips uint16_t flags;
252*e7c5e80fSMitch Phillips uint64_t pgoff;
253*e7c5e80fSMitch Phillips ino_t inode;
254*e7c5e80fSMitch Phillips char* line_start = content;
255*e7c5e80fSMitch Phillips char* next_line;
256*e7c5e80fSMitch Phillips char* name;
257*e7c5e80fSMitch Phillips bool shared;
258*e7c5e80fSMitch Phillips
259*e7c5e80fSMitch Phillips while (line_start != nullptr && *line_start != '\0') {
260*e7c5e80fSMitch Phillips bool parsed = ParseMapsFileLine(line_start, start_addr, end_addr, flags, pgoff,
261*e7c5e80fSMitch Phillips inode, &name, shared, &next_line);
262*e7c5e80fSMitch Phillips if (!parsed) {
263*e7c5e80fSMitch Phillips return false;
264*e7c5e80fSMitch Phillips }
265*e7c5e80fSMitch Phillips
266*e7c5e80fSMitch Phillips line_start = next_line;
267*e7c5e80fSMitch Phillips callback(MapInfo(start_addr, end_addr, flags, pgoff, inode, name, shared));
268*e7c5e80fSMitch Phillips }
269*e7c5e80fSMitch Phillips return true;
270*e7c5e80fSMitch Phillips }
271*e7c5e80fSMitch Phillips
ReadMapFile(const std::string & map_file,const MapInfoCallback & callback)272*e7c5e80fSMitch Phillips inline bool ReadMapFile(const std::string& map_file,
273*e7c5e80fSMitch Phillips const MapInfoCallback& callback) {
274*e7c5e80fSMitch Phillips std::string content;
275*e7c5e80fSMitch Phillips if (!android::base::ReadFileToString(map_file, &content)) {
276*e7c5e80fSMitch Phillips return false;
277*e7c5e80fSMitch Phillips }
278*e7c5e80fSMitch Phillips return ReadMapFileContent(&content[0], callback);
279*e7c5e80fSMitch Phillips }
280*e7c5e80fSMitch Phillips
281*e7c5e80fSMitch Phillips
ReadMapFile(const std::string & map_file,const MapInfoParamsCallback & callback,std::string & mapsBuffer)282*e7c5e80fSMitch Phillips inline bool ReadMapFile(const std::string& map_file, const MapInfoParamsCallback& callback,
283*e7c5e80fSMitch Phillips std::string& mapsBuffer) {
284*e7c5e80fSMitch Phillips if (!android::base::ReadFileToString(map_file, &mapsBuffer)) {
285*e7c5e80fSMitch Phillips return false;
286*e7c5e80fSMitch Phillips }
287*e7c5e80fSMitch Phillips return ReadMapFileContent(&mapsBuffer[0], callback);
288*e7c5e80fSMitch Phillips }
289*e7c5e80fSMitch Phillips
ReadMapFile(const std::string & map_file,const MapInfoParamsCallback & callback)290*e7c5e80fSMitch Phillips inline bool ReadMapFile(const std::string& map_file,
291*e7c5e80fSMitch Phillips const MapInfoParamsCallback& callback) {
292*e7c5e80fSMitch Phillips std::string content;
293*e7c5e80fSMitch Phillips return ReadMapFile(map_file, callback, content);
294*e7c5e80fSMitch Phillips }
295*e7c5e80fSMitch Phillips
ReadProcessMaps(pid_t pid,const MapInfoCallback & callback)296*e7c5e80fSMitch Phillips inline bool ReadProcessMaps(pid_t pid, const MapInfoCallback& callback) {
297*e7c5e80fSMitch Phillips return ReadMapFile("/proc/" + std::to_string(pid) + "/maps", callback);
298*e7c5e80fSMitch Phillips }
299*e7c5e80fSMitch Phillips
ReadProcessMaps(pid_t pid,const MapInfoParamsCallback & callback,std::string & mapsBuffer)300*e7c5e80fSMitch Phillips inline bool ReadProcessMaps(pid_t pid, const MapInfoParamsCallback& callback,
301*e7c5e80fSMitch Phillips std::string& mapsBuffer) {
302*e7c5e80fSMitch Phillips return ReadMapFile("/proc/" + std::to_string(pid) + "/maps", callback, mapsBuffer);
303*e7c5e80fSMitch Phillips }
304*e7c5e80fSMitch Phillips
ReadProcessMaps(pid_t pid,const MapInfoParamsCallback & callback)305*e7c5e80fSMitch Phillips inline bool ReadProcessMaps(pid_t pid, const MapInfoParamsCallback& callback) {
306*e7c5e80fSMitch Phillips std::string content;
307*e7c5e80fSMitch Phillips return ReadProcessMaps(pid, callback, content);
308*e7c5e80fSMitch Phillips }
309*e7c5e80fSMitch Phillips
ReadProcessMaps(pid_t pid,std::vector<MapInfo> * maps)310*e7c5e80fSMitch Phillips inline bool ReadProcessMaps(pid_t pid, std::vector<MapInfo>* maps) {
311*e7c5e80fSMitch Phillips return ReadProcessMaps(pid, [&](const MapInfo& mapinfo) { maps->emplace_back(mapinfo); });
312*e7c5e80fSMitch Phillips }
313*e7c5e80fSMitch Phillips
314*e7c5e80fSMitch Phillips // Reads maps file and executes given callback for each mapping
315*e7c5e80fSMitch Phillips // Warning: buffer should not be modified asynchronously while this function executes
316*e7c5e80fSMitch Phillips template <class CallbackType>
ReadMapFileAsyncSafe(const char * map_file,void * buffer,size_t buffer_size,const CallbackType & callback)317*e7c5e80fSMitch Phillips inline bool ReadMapFileAsyncSafe(const char* map_file, void* buffer, size_t buffer_size,
318*e7c5e80fSMitch Phillips const CallbackType& callback) {
319*e7c5e80fSMitch Phillips if (buffer == nullptr || buffer_size == 0) {
320*e7c5e80fSMitch Phillips return false;
321*e7c5e80fSMitch Phillips }
322*e7c5e80fSMitch Phillips
323*e7c5e80fSMitch Phillips int fd = open(map_file, O_RDONLY | O_CLOEXEC);
324*e7c5e80fSMitch Phillips if (fd == -1) {
325*e7c5e80fSMitch Phillips return false;
326*e7c5e80fSMitch Phillips }
327*e7c5e80fSMitch Phillips
328*e7c5e80fSMitch Phillips char* char_buffer = reinterpret_cast<char*>(buffer);
329*e7c5e80fSMitch Phillips size_t start = 0;
330*e7c5e80fSMitch Phillips size_t read_bytes = 0;
331*e7c5e80fSMitch Phillips char* line = nullptr;
332*e7c5e80fSMitch Phillips bool read_complete = false;
333*e7c5e80fSMitch Phillips while (true) {
334*e7c5e80fSMitch Phillips ssize_t bytes =
335*e7c5e80fSMitch Phillips TEMP_FAILURE_RETRY(read(fd, char_buffer + read_bytes, buffer_size - read_bytes - 1));
336*e7c5e80fSMitch Phillips if (bytes <= 0) {
337*e7c5e80fSMitch Phillips if (read_bytes == 0) {
338*e7c5e80fSMitch Phillips close(fd);
339*e7c5e80fSMitch Phillips return bytes == 0;
340*e7c5e80fSMitch Phillips }
341*e7c5e80fSMitch Phillips // Treat the last piece of data as the last line.
342*e7c5e80fSMitch Phillips char_buffer[start + read_bytes] = '\n';
343*e7c5e80fSMitch Phillips bytes = 1;
344*e7c5e80fSMitch Phillips read_complete = true;
345*e7c5e80fSMitch Phillips }
346*e7c5e80fSMitch Phillips read_bytes += bytes;
347*e7c5e80fSMitch Phillips
348*e7c5e80fSMitch Phillips while (read_bytes > 0) {
349*e7c5e80fSMitch Phillips char* newline = reinterpret_cast<char*>(memchr(&char_buffer[start], '\n', read_bytes));
350*e7c5e80fSMitch Phillips if (newline == nullptr) {
351*e7c5e80fSMitch Phillips break;
352*e7c5e80fSMitch Phillips }
353*e7c5e80fSMitch Phillips *newline = '\0';
354*e7c5e80fSMitch Phillips line = &char_buffer[start];
355*e7c5e80fSMitch Phillips start = newline - char_buffer + 1;
356*e7c5e80fSMitch Phillips read_bytes -= newline - line + 1;
357*e7c5e80fSMitch Phillips
358*e7c5e80fSMitch Phillips // Ignore the return code, errors are okay.
359*e7c5e80fSMitch Phillips ReadMapFileContent(line, callback);
360*e7c5e80fSMitch Phillips }
361*e7c5e80fSMitch Phillips
362*e7c5e80fSMitch Phillips if (read_complete) {
363*e7c5e80fSMitch Phillips close(fd);
364*e7c5e80fSMitch Phillips return true;
365*e7c5e80fSMitch Phillips }
366*e7c5e80fSMitch Phillips
367*e7c5e80fSMitch Phillips if (start == 0 && read_bytes == buffer_size - 1) {
368*e7c5e80fSMitch Phillips // The buffer provided is too small to contain this line, give up
369*e7c5e80fSMitch Phillips // and indicate failure.
370*e7c5e80fSMitch Phillips close(fd);
371*e7c5e80fSMitch Phillips return false;
372*e7c5e80fSMitch Phillips }
373*e7c5e80fSMitch Phillips
374*e7c5e80fSMitch Phillips // Copy any leftover data to the front of the buffer.
375*e7c5e80fSMitch Phillips if (start > 0) {
376*e7c5e80fSMitch Phillips if (read_bytes > 0) {
377*e7c5e80fSMitch Phillips memmove(char_buffer, &char_buffer[start], read_bytes);
378*e7c5e80fSMitch Phillips }
379*e7c5e80fSMitch Phillips start = 0;
380*e7c5e80fSMitch Phillips }
381*e7c5e80fSMitch Phillips }
382*e7c5e80fSMitch Phillips }
383*e7c5e80fSMitch Phillips
384*e7c5e80fSMitch Phillips /**
385*e7c5e80fSMitch Phillips * A file memory mapping can be created such that it is only partially
386*e7c5e80fSMitch Phillips * backed by the underlying file. i.e. the mapping size is larger than
387*e7c5e80fSMitch Phillips * the file size.
388*e7c5e80fSMitch Phillips *
389*e7c5e80fSMitch Phillips * On builds that support larger than 4KB page-size, the common assumption
390*e7c5e80fSMitch Phillips * that a file mapping is entirely backed by the underlying file, is
391*e7c5e80fSMitch Phillips * more likely to be false.
392*e7c5e80fSMitch Phillips *
393*e7c5e80fSMitch Phillips * If an access to a region of the mapping beyond the end of the file
394*e7c5e80fSMitch Phillips * occurs, there are 2 situations:
395*e7c5e80fSMitch Phillips * 1) The access is between the end of the file and the next page
396*e7c5e80fSMitch Phillips * boundary. The kernel will facilitate this although there is
397*e7c5e80fSMitch Phillips * no file here.
398*e7c5e80fSMitch Phillips * Note: That writing this region does not persist any data to
399*e7c5e80fSMitch Phillips * the actual backing file.
400*e7c5e80fSMitch Phillips * 2) The access is beyond the first page boundary after the end
401*e7c5e80fSMitch Phillips * of the file. This will cause a filemap_fault which does not
402*e7c5e80fSMitch Phillips * correspond to a valid file offset and the kernel will return
403*e7c5e80fSMitch Phillips * a SIGBUS.
404*e7c5e80fSMitch Phillips * See return value SIGBUS at:
405*e7c5e80fSMitch Phillips * https://man7.org/linux/man-pages/man2/mmap.2.html#RETURN_VALUE
406*e7c5e80fSMitch Phillips *
407*e7c5e80fSMitch Phillips * Userspace programs that parse /proc/<pid>/maps or /proc/<pid>/smaps
408*e7c5e80fSMitch Phillips * to determine the extent of memory mappings which they then use as
409*e7c5e80fSMitch Phillips * arguments to other syscalls or directly access; should be aware of
410*e7c5e80fSMitch Phillips * the second case above (2) and not assume that file mappings are
411*e7c5e80fSMitch Phillips * entirely back by the underlying file.
412*e7c5e80fSMitch Phillips *
413*e7c5e80fSMitch Phillips * This is especially important for operations that would cause a
414*e7c5e80fSMitch Phillips * page-fault on the range described in (2). In this case userspace
415*e7c5e80fSMitch Phillips * should either handle the signal or use the range backed by the
416*e7c5e80fSMitch Phillips * underlying file for the desired operation.
417*e7c5e80fSMitch Phillips *
418*e7c5e80fSMitch Phillips *
419*e7c5e80fSMitch Phillips * MappedFileSize() - Returns the size of the memory map backed
420*e7c5e80fSMitch Phillips * by the underlying file; or 0 if not file-backed.
421*e7c5e80fSMitch Phillips * @start_addr - start address of the memory map.
422*e7c5e80fSMitch Phillips * @end_addr - end address of the memory map.
423*e7c5e80fSMitch Phillips * @file_offset - file offset of the backing file corresponding to the
424*e7c5e80fSMitch Phillips * start of the memory map.
425*e7c5e80fSMitch Phillips * @file_size - size of the file (<file_path>) in bytes.
426*e7c5e80fSMitch Phillips *
427*e7c5e80fSMitch Phillips * NOTE: The arguments corresponds to the following fields of an entry
428*e7c5e80fSMitch Phillips * in /proc/<pid>/maps:
429*e7c5e80fSMitch Phillips *
430*e7c5e80fSMitch Phillips * <start_addr>-< end_addr > ... <file_offset> ... ... <file_path>
431*e7c5e80fSMitch Phillips * 790b07dc6000-790b07dd9000 r--p 00000000 fe:09 21068208 /system/lib64/foo.so
432*e7c5e80fSMitch Phillips *
433*e7c5e80fSMitch Phillips * NOTE: Clients of this API should be aware that, although unlikely,
434*e7c5e80fSMitch Phillips * it is possible for @file_size to change under us and race with
435*e7c5e80fSMitch Phillips * the checks in MappedFileSize().
436*e7c5e80fSMitch Phillips * Users should avoid concurrent modifications of @file_size, or
437*e7c5e80fSMitch Phillips * use appropriate locking according to the usecase.
438*e7c5e80fSMitch Phillips */
MappedFileSize(uint64_t start_addr,uint64_t end_addr,uint64_t file_offset,uint64_t file_size)439*e7c5e80fSMitch Phillips inline uint64_t MappedFileSize(uint64_t start_addr, uint64_t end_addr,
440*e7c5e80fSMitch Phillips uint64_t file_offset, uint64_t file_size) {
441*e7c5e80fSMitch Phillips uint64_t len = end_addr - start_addr;
442*e7c5e80fSMitch Phillips
443*e7c5e80fSMitch Phillips // This VMA may have been split from a larger file mapping; or the
444*e7c5e80fSMitch Phillips // file may have been resized since the mapping was created.
445*e7c5e80fSMitch Phillips if (file_offset > file_size) {
446*e7c5e80fSMitch Phillips return 0;
447*e7c5e80fSMitch Phillips }
448*e7c5e80fSMitch Phillips
449*e7c5e80fSMitch Phillips // Mapping exceeds file_size ?
450*e7c5e80fSMitch Phillips if ((file_offset + len) > file_size) {
451*e7c5e80fSMitch Phillips return file_size - file_offset;
452*e7c5e80fSMitch Phillips }
453*e7c5e80fSMitch Phillips
454*e7c5e80fSMitch Phillips return len;
455*e7c5e80fSMitch Phillips }
456*e7c5e80fSMitch Phillips
457*e7c5e80fSMitch Phillips /*
458*e7c5e80fSMitch Phillips * MappedFileSize() - Returns the size of the memory map backed
459*e7c5e80fSMitch Phillips * by the underlying file; or 0 if not file-backed.
460*e7c5e80fSMitch Phillips */
MappedFileSize(const MapInfo & map)461*e7c5e80fSMitch Phillips inline uint64_t MappedFileSize(const MapInfo& map) {
462*e7c5e80fSMitch Phillips // Anon mapping or device?
463*e7c5e80fSMitch Phillips if (map.name.empty() || map.name[0] != '/' ||
464*e7c5e80fSMitch Phillips android::base::StartsWith(map.name, "/dev/")) {
465*e7c5e80fSMitch Phillips return 0;
466*e7c5e80fSMitch Phillips }
467*e7c5e80fSMitch Phillips
468*e7c5e80fSMitch Phillips struct stat file_stat;
469*e7c5e80fSMitch Phillips if (stat(map.name.c_str(), &file_stat) != 0) {
470*e7c5e80fSMitch Phillips return 0;
471*e7c5e80fSMitch Phillips }
472*e7c5e80fSMitch Phillips
473*e7c5e80fSMitch Phillips return MappedFileSize(map.start, map.end, map.pgoff, file_stat.st_size);
474*e7c5e80fSMitch Phillips }
475*e7c5e80fSMitch Phillips
476*e7c5e80fSMitch Phillips } /* namespace procinfo */
477*e7c5e80fSMitch Phillips } /* namespace android */
478