-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathparse_running_mmaps_unix.rs
372 lines (345 loc) · 13.1 KB
/
parse_running_mmaps_unix.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Note: This file is only currently used on targets that call out to the code
// in `mod libs_dl_iterate_phdr` (e.g. linux, freebsd, ...); it may be more
// general purpose, but it hasn't been tested elsewhere.
use super::mystd::fs::File;
use super::mystd::io::Read;
use super::mystd::str::FromStr;
use super::{OsString, String, Vec};
#[derive(PartialEq, Eq, Debug)]
pub(super) struct MapsEntry {
/// start (inclusive) and limit (exclusive) of address range.
address: (usize, usize),
/// The perms field are the permissions for the entry
///
/// r = read
/// w = write
/// x = execute
/// s = shared
/// p = private (copy on write)
perms: [char; 4],
/// Offset into the file (or "whatever").
offset: usize,
/// device (major, minor)
dev: (usize, usize),
/// inode on the device. 0 indicates that no inode is associated with the memory region (e.g. uninitalized data aka BSS).
inode: usize,
/// Usually the file backing the mapping.
///
/// Note: The man page for proc includes a note about "coordination" by
/// using readelf to see the Offset field in ELF program headers. pnkfelix
/// is not yet sure if that is intended to be a comment on pathname, or what
/// form/purpose such coordination is meant to have.
///
/// There are also some pseudo-paths:
/// "[stack]": The initial process's (aka main thread's) stack.
/// "[stack:<tid>]": a specific thread's stack. (This was only present for a limited range of Linux verisons; it was determined to be too expensive to provide.)
/// "[vdso]": Virtual dynamically linked shared object
/// "[heap]": The process's heap
///
/// The pathname can be blank, which means it is an anonymous mapping
/// obtained via mmap.
///
/// Newlines in pathname are replaced with an octal escape sequence.
///
/// The pathname may have "(deleted)" appended onto it if the file-backed
/// path has been deleted.
///
/// Note that modifications like the latter two indicated above imply that
/// in general the pathname may be ambiguous. (I.e. you cannot tell if the
/// denoted filename actually ended with the text "(deleted)", or if that
/// was added by the maps rendering.
pathname: OsString,
}
fn concat_str(a: &str, b: &str) -> String {
let mut e = String::from(a);
e += b;
e
}
fn read_file(file: &str) -> Result<String, String> {
let mut proc_self_maps =
File::open(file).map_err(|_| concat_str("Couldn't open file: ", file))?;
let mut buf = String::new();
let _bytes_read = proc_self_maps
.read_to_string(&mut buf)
.map_err(|_| concat_str("Couldn't read file: ", file))?;
Ok(buf)
}
pub(super) fn parse_maps() -> Result<Vec<MapsEntry>, String> {
let (file, skip) = config();
let content = read_file(file)?;
parse_maps_lines(&content, skip)
}
fn parse_maps_lines(content: &str, skip: usize) -> Result<Vec<MapsEntry>, String> {
let mut data = Vec::new();
for line in content.lines().skip(skip) {
data.push(line.parse()?);
}
Ok(data)
}
impl MapsEntry {
pub(super) fn pathname(&self) -> &OsString {
&self.pathname
}
pub(super) fn ip_matches(&self, ip: usize) -> bool {
self.address.0 <= ip && ip < self.address.1
}
}
#[cfg(not(target_os = "nto"))]
pub fn config() -> (&'static str, usize) {
("/proc/self/maps", 0)
}
#[cfg(not(target_os = "nto"))]
impl FromStr for MapsEntry {
type Err = &'static str;
// Format: address perms offset dev inode pathname
// e.g.: "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]"
// e.g.: "7f5985f46000-7f5985f48000 rw-p 00039000 103:06 76021795 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2"
// e.g.: "35b1a21000-35b1a22000 rw-p 00000000 00:00 0"
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s
.split(' ') // space-separated fields
.filter(|s| s.len() > 0); // multiple spaces implies empty strings that need to be skipped.
let range_str = parts.next().ok_or("Couldn't find address")?;
let perms_str = parts.next().ok_or("Couldn't find permissions")?;
let offset_str = parts.next().ok_or("Couldn't find offset")?;
let dev_str = parts.next().ok_or("Couldn't find dev")?;
let inode_str = parts.next().ok_or("Couldn't find inode")?;
let pathname_str = parts.next().unwrap_or(""); // pathname may be omitted.
let hex = |s| usize::from_str_radix(s, 16).map_err(|_| "Couldn't parse hex number");
let address = {
// This could use `range_str.split_once('-')` once the MSRV passes 1.52.
if let Some(idx) = range_str.find('-') {
let (start, rest) = range_str.split_at(idx);
let (_div, limit) = rest.split_at(1);
(hex(start)?, hex(limit)?)
} else {
return Err("Couldn't parse address range");
}
};
let perms: [char; 4] = {
let mut chars = perms_str.chars();
let mut c = || chars.next().ok_or("insufficient perms");
let perms = [c()?, c()?, c()?, c()?];
if chars.next().is_some() {
return Err("too many perms");
}
perms
};
let offset = hex(offset_str)?;
let dev = {
// This could use `dev_str.split_once(':')` once the MSRV passes 1.52.
if let Some(idx) = dev_str.find(':') {
let (major, rest) = dev_str.split_at(idx);
let (_div, minor) = rest.split_at(1);
(hex(major)?, hex(minor)?)
} else {
return Err("Couldn't parse dev")?;
}
};
let inode = hex(inode_str)?;
let pathname = pathname_str.into();
Ok(MapsEntry {
address,
perms,
offset,
dev,
inode,
pathname,
})
}
}
#[cfg(target_os = "nto")]
pub fn config() -> (&'static str, usize) {
("/proc/self/pmap", 1)
}
#[cfg(target_os = "nto")]
impl FromStr for MapsEntry {
type Err = &'static str;
// Format: vaddr,size,flags,prot,maxprot,dev,ino,offset,rsv,guardsize,refcnt,mapcnt,path
// e.g.: "0x00000022fa36b000,0x0000000000002000,0x00000071,0x05,0x0f,0x0000040b,0x00000000000000dd,
// 0x0000000000000000,0x0000000000000000,0x00000000,0x00000005,0x00000003,/proc/boot/cat"
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.split(',');
let vaddr_str = parts.next().ok_or("Couldn't find virtual address")?;
let size_str = parts.next().ok_or("Couldn't find size")?;
let _flags_str = parts.next().ok_or("Couldn't find flags")?;
let prot_str = parts.next().ok_or("Couldn't find protection")?;
let _maxprot_str = parts.next().ok_or("Couldn't find maximum protection")?;
let dev_str = parts.next().ok_or("Couldn't find device")?;
let ino_str = parts.next().ok_or("Couldn't find inode")?;
let offset_str = parts.next().ok_or("Couldn't find offset")?;
let _rsv_str = parts.next().ok_or("Couldn't find reserved pages")?;
let _guardsize_str = parts.next().ok_or("Couldn't find guard size")?;
let _refcnt_str = parts.next().ok_or("Couldn't find reference count")?;
let _mapcnt_str = parts.next().ok_or("Couldn't find mapped count")?;
let pathname_str = parts.next().unwrap_or(""); // pathname may be omitted.
let hex =
|s: &str| usize::from_str_radix(&s[2..], 16).map_err(|_| "Couldn't parse hex number");
let address = { (hex(vaddr_str)?, hex(vaddr_str)? + hex(size_str)?) };
// TODO: Probably a rust'ier way of doing this
let mut perms: [char; 4] = ['-', '-', '-', '-'];
let perm_str: [char; 3] = ['r', 'w', 'x'];
let perm_bits: [usize; 3] = [0x1, 0x2, 0x4];
for (pos, val) in perm_bits.iter().enumerate() {
let prot = hex(prot_str)?;
if val & prot != 0 {
perms[pos] = perm_str[pos]
}
}
let offset = hex(offset_str)?;
let dev = { (hex(dev_str)?, 0x00000000) };
let inode = hex(ino_str)?;
let pathname = pathname_str.into();
Ok(MapsEntry {
address,
perms,
offset,
dev,
inode,
pathname,
})
}
}
// Make sure we can parse 64-bit sample output if we're on a 64-bit target.
#[cfg(all(target_pointer_width = "64", not(target_os = "nto")))]
#[test]
fn check_maps_entry_parsing_64bit() {
assert_eq!(
"ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 \
[vsyscall]"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0xffffffffff600000, 0xffffffffff601000),
perms: ['-', '-', 'x', 'p'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: "[vsyscall]".into(),
}
);
assert_eq!(
"7f5985f46000-7f5985f48000 rw-p 00039000 103:06 76021795 \
/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0x7f5985f46000, 0x7f5985f48000),
perms: ['r', 'w', '-', 'p'],
offset: 0x00039000,
dev: (0x103, 0x06),
inode: 0x76021795,
pathname: "/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into(),
}
);
assert_eq!(
"35b1a21000-35b1a22000 rw-p 00000000 00:00 0"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0x35b1a21000, 0x35b1a22000),
perms: ['r', 'w', '-', 'p'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: Default::default(),
}
);
}
// (This output was taken from a 32-bit machine, but will work on any target)
#[cfg(not(target_os = "nto"))]
#[test]
fn check_maps_entry_parsing_32bit() {
/* Example snippet of output:
08056000-08077000 rw-p 00000000 00:00 0 [heap]
b7c79000-b7e02000 r--p 00000000 08:01 60662705 /usr/lib/locale/locale-archive
b7e02000-b7e03000 rw-p 00000000 00:00 0
*/
assert_eq!(
"08056000-08077000 rw-p 00000000 00:00 0 \
[heap]"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0x08056000, 0x08077000),
perms: ['r', 'w', '-', 'p'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: "[heap]".into(),
}
);
assert_eq!(
"b7c79000-b7e02000 r--p 00000000 08:01 60662705 \
/usr/lib/locale/locale-archive"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0xb7c79000, 0xb7e02000),
perms: ['r', '-', '-', 'p'],
offset: 0x00000000,
dev: (0x08, 0x01),
inode: 0x60662705,
pathname: "/usr/lib/locale/locale-archive".into(),
}
);
assert_eq!(
"b7e02000-b7e03000 rw-p 00000000 00:00 0"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0xb7e02000, 0xb7e03000),
perms: ['r', 'w', '-', 'p'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: Default::default(),
}
);
}
#[cfg(target_os = "nto")]
#[test]
fn check_maps_entry_parsing_64bit() {
assert_eq!(
"0xffffffffff600000,0x0000000000001000,0x00000071,0x04,0x0f,0x00000000,0x0000000000000000,\
0x0000000000000000,0x0000000000000000,0x00000000,0x00000005,0x00000003,/proc/boot/foo"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0xffffffffff600000, 0xffffffffff601000),
perms: ['-', '-', 'x', '-'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: "/proc/boot/foo".into(),
}
);
assert_eq!(
"0x00007f5985f46000,0x0000000000002000,0x00000071,0x03,0x0f,0x00000103,0x0000000076021795,\
0x0000000000039000,0x0000000000000000,0x00000000,0x00000005,0x00000003,/usr/lib/ldqnx-64.so.2"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0x7f5985f46000, 0x7f5985f48000),
perms: ['r', 'w', '-', '-'],
offset: 0x00039000,
dev: (0x103, 0x0),
inode: 0x76021795,
pathname: "/usr/lib/ldqnx-64.so.2".into(),
}
);
assert_eq!(
"0x00000035b1a21000,0x0000000000001000,0x00000071,0x03,0x0f,0x00000000,0x0000000000000000,\
0x0000000000000000,0x0000000000000000,0x00000000,0x00000005,0x00000003,"
.parse::<MapsEntry>()
.unwrap(),
MapsEntry {
address: (0x35b1a21000, 0x35b1a22000),
perms: ['r', 'w', '-', '-'],
offset: 0x00000000,
dev: (0x00, 0x00),
inode: 0x0,
pathname: Default::default(),
}
);
}