|
| 1 | +// Note: This file is only currently used on targets that call out to the code |
| 2 | +// in `mod libs_dl_iterate_phdr` (e.g. linux, freebsd, ...); it may be more |
| 3 | +// general purpose, but it hasn't been tested elsewhere. |
| 4 | + |
| 5 | +use super::mystd::fs::File; |
| 6 | +use super::mystd::io::Read; |
| 7 | +use super::mystd::str::FromStr; |
| 8 | +use super::{OsString, String, Vec}; |
| 9 | + |
| 10 | +#[derive(PartialEq, Eq, Debug)] |
| 11 | +pub(super) struct MapsEntry { |
| 12 | + /// start (inclusive) and limit (exclusive) of address range. |
| 13 | + address: (usize, usize), |
| 14 | + /// The perms field are the permissions for the entry |
| 15 | + /// |
| 16 | + /// r = read |
| 17 | + /// w = write |
| 18 | + /// x = execute |
| 19 | + /// s = shared |
| 20 | + /// p = private (copy on write) |
| 21 | + perms: [char; 4], |
| 22 | + /// Offset into the file (or "whatever"). |
| 23 | + offset: usize, |
| 24 | + /// device (major, minor) |
| 25 | + dev: (usize, usize), |
| 26 | + /// inode on the device. 0 indicates that no inode is associated with the memory region (e.g. uninitalized data aka BSS). |
| 27 | + inode: usize, |
| 28 | + /// Usually the file backing the mapping. |
| 29 | + /// |
| 30 | + /// Note: The man page for proc includes a note about "coordination" by |
| 31 | + /// using readelf to see the Offset field in ELF program headers. pnkfelix |
| 32 | + /// is not yet sure if that is intended to be a comment on pathname, or what |
| 33 | + /// form/purpose such coordination is meant to have. |
| 34 | + /// |
| 35 | + /// There are also some pseudo-paths: |
| 36 | + /// "[stack]": The initial process's (aka main thread's) stack. |
| 37 | + /// "[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.) |
| 38 | + /// "[vdso]": Virtual dynamically linked shared object |
| 39 | + /// "[heap]": The process's heap |
| 40 | + /// |
| 41 | + /// The pathname can be blank, which means it is an anonymous mapping |
| 42 | + /// obtained via mmap. |
| 43 | + /// |
| 44 | + /// Newlines in pathname are replaced with an octal escape sequence. |
| 45 | + /// |
| 46 | + /// The pathname may have "(deleted)" appended onto it if the file-backed |
| 47 | + /// path has been deleted. |
| 48 | + /// |
| 49 | + /// Note that modifications like the latter two indicated above imply that |
| 50 | + /// in general the pathname may be ambiguous. (I.e. you cannot tell if the |
| 51 | + /// denoted filename actually ended with the text "(deleted)", or if that |
| 52 | + /// was added by the maps rendering. |
| 53 | + pathname: OsString, |
| 54 | +} |
| 55 | + |
| 56 | +pub(super) fn parse_maps() -> Result<Vec<MapsEntry>, &'static str> { |
| 57 | + let mut v = Vec::new(); |
| 58 | + let mut proc_self_maps = |
| 59 | + File::open("/proc/self/maps").map_err(|_| "Couldn't open /proc/self/maps")?; |
| 60 | + let mut buf = String::new(); |
| 61 | + let _bytes_read = proc_self_maps |
| 62 | + .read_to_string(&mut buf) |
| 63 | + .map_err(|_| "Couldn't read /proc/self/maps")?; |
| 64 | + for line in buf.lines() { |
| 65 | + v.push(line.parse()?); |
| 66 | + } |
| 67 | + |
| 68 | + Ok(v) |
| 69 | +} |
| 70 | + |
| 71 | +impl MapsEntry { |
| 72 | + pub(super) fn pathname(&self) -> &OsString { |
| 73 | + &self.pathname |
| 74 | + } |
| 75 | + |
| 76 | + pub(super) fn ip_matches(&self, ip: usize) -> bool { |
| 77 | + self.address.0 <= ip && ip < self.address.1 |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl FromStr for MapsEntry { |
| 82 | + type Err = &'static str; |
| 83 | + |
| 84 | + // Format: address perms offset dev inode pathname |
| 85 | + // e.g.: "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]" |
| 86 | + // e.g.: "7f5985f46000-7f5985f48000 rw-p 00039000 103:06 76021795 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2" |
| 87 | + // e.g.: "35b1a21000-35b1a22000 rw-p 00000000 00:00 0" |
| 88 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 89 | + let mut parts = s |
| 90 | + .split(' ') // space-separated fields |
| 91 | + .filter(|s| s.len() > 0); // multiple spaces implies empty strings that need to be skipped. |
| 92 | + let range_str = parts.next().ok_or("Couldn't find address")?; |
| 93 | + let perms_str = parts.next().ok_or("Couldn't find permissions")?; |
| 94 | + let offset_str = parts.next().ok_or("Couldn't find offset")?; |
| 95 | + let dev_str = parts.next().ok_or("Couldn't find dev")?; |
| 96 | + let inode_str = parts.next().ok_or("Couldn't find inode")?; |
| 97 | + let pathname_str = parts.next().unwrap_or(""); // pathname may be omitted. |
| 98 | + |
| 99 | + let hex = |s| usize::from_str_radix(s, 16).map_err(|_| "Couldn't parse hex number"); |
| 100 | + let address = { |
| 101 | + // This could use `range_str.split_once('-')` once the MSRV passes 1.52. |
| 102 | + if let Some(idx) = range_str.find('-') { |
| 103 | + let (start, rest) = range_str.split_at(idx); |
| 104 | + let (_div, limit) = rest.split_at(1); |
| 105 | + (hex(start)?, hex(limit)?) |
| 106 | + } else { |
| 107 | + return Err("Couldn't parse address range"); |
| 108 | + } |
| 109 | + }; |
| 110 | + let perms: [char; 4] = { |
| 111 | + let mut chars = perms_str.chars(); |
| 112 | + let mut c = || chars.next().ok_or("insufficient perms"); |
| 113 | + let perms = [c()?, c()?, c()?, c()?]; |
| 114 | + if chars.next().is_some() { |
| 115 | + return Err("too many perms"); |
| 116 | + } |
| 117 | + perms |
| 118 | + }; |
| 119 | + let offset = hex(offset_str)?; |
| 120 | + let dev = { |
| 121 | + // This could use `dev_str.split_once(':')` once the MSRV passes 1.52. |
| 122 | + if let Some(idx) = dev_str.find(':') { |
| 123 | + let (major, rest) = dev_str.split_at(idx); |
| 124 | + let (_div, minor) = rest.split_at(1); |
| 125 | + (hex(major)?, hex(minor)?) |
| 126 | + } else { |
| 127 | + return Err("Couldn't parse dev")?; |
| 128 | + } |
| 129 | + }; |
| 130 | + let inode = hex(inode_str)?; |
| 131 | + let pathname = pathname_str.into(); |
| 132 | + |
| 133 | + Ok(MapsEntry { |
| 134 | + address, |
| 135 | + perms, |
| 136 | + offset, |
| 137 | + dev, |
| 138 | + inode, |
| 139 | + pathname, |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Make sure we can parse 64-bit sample output if we're on a 64-bit target. |
| 145 | +#[cfg(target_pointer_width = "64")] |
| 146 | +#[test] |
| 147 | +fn check_maps_entry_parsing_64bit() { |
| 148 | + assert_eq!( |
| 149 | + "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 \ |
| 150 | + [vsyscall]" |
| 151 | + .parse::<MapsEntry>() |
| 152 | + .unwrap(), |
| 153 | + MapsEntry { |
| 154 | + address: (0xffffffffff600000, 0xffffffffff601000), |
| 155 | + perms: ['-', '-', 'x', 'p'], |
| 156 | + offset: 0x00000000, |
| 157 | + dev: (0x00, 0x00), |
| 158 | + inode: 0x0, |
| 159 | + pathname: "[vsyscall]".into(), |
| 160 | + } |
| 161 | + ); |
| 162 | + |
| 163 | + assert_eq!( |
| 164 | + "7f5985f46000-7f5985f48000 rw-p 00039000 103:06 76021795 \ |
| 165 | + /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2" |
| 166 | + .parse::<MapsEntry>() |
| 167 | + .unwrap(), |
| 168 | + MapsEntry { |
| 169 | + address: (0x7f5985f46000, 0x7f5985f48000), |
| 170 | + perms: ['r', 'w', '-', 'p'], |
| 171 | + offset: 0x00039000, |
| 172 | + dev: (0x103, 0x06), |
| 173 | + inode: 0x76021795, |
| 174 | + pathname: "/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".into(), |
| 175 | + } |
| 176 | + ); |
| 177 | + assert_eq!( |
| 178 | + "35b1a21000-35b1a22000 rw-p 00000000 00:00 0" |
| 179 | + .parse::<MapsEntry>() |
| 180 | + .unwrap(), |
| 181 | + MapsEntry { |
| 182 | + address: (0x35b1a21000, 0x35b1a22000), |
| 183 | + perms: ['r', 'w', '-', 'p'], |
| 184 | + offset: 0x00000000, |
| 185 | + dev: (0x00, 0x00), |
| 186 | + inode: 0x0, |
| 187 | + pathname: Default::default(), |
| 188 | + } |
| 189 | + ); |
| 190 | +} |
| 191 | + |
| 192 | +// (This output was taken from a 32-bit machine, but will work on any target) |
| 193 | +#[test] |
| 194 | +fn check_maps_entry_parsing_32bit() { |
| 195 | + /* Example snippet of output: |
| 196 | + 08056000-08077000 rw-p 00000000 00:00 0 [heap] |
| 197 | + b7c79000-b7e02000 r--p 00000000 08:01 60662705 /usr/lib/locale/locale-archive |
| 198 | + b7e02000-b7e03000 rw-p 00000000 00:00 0 |
| 199 | + */ |
| 200 | + assert_eq!( |
| 201 | + "08056000-08077000 rw-p 00000000 00:00 0 \ |
| 202 | + [heap]" |
| 203 | + .parse::<MapsEntry>() |
| 204 | + .unwrap(), |
| 205 | + MapsEntry { |
| 206 | + address: (0x08056000, 0x08077000), |
| 207 | + perms: ['r', 'w', '-', 'p'], |
| 208 | + offset: 0x00000000, |
| 209 | + dev: (0x00, 0x00), |
| 210 | + inode: 0x0, |
| 211 | + pathname: "[heap]".into(), |
| 212 | + } |
| 213 | + ); |
| 214 | + |
| 215 | + assert_eq!( |
| 216 | + "b7c79000-b7e02000 r--p 00000000 08:01 60662705 \ |
| 217 | + /usr/lib/locale/locale-archive" |
| 218 | + .parse::<MapsEntry>() |
| 219 | + .unwrap(), |
| 220 | + MapsEntry { |
| 221 | + address: (0xb7c79000, 0xb7e02000), |
| 222 | + perms: ['r', '-', '-', 'p'], |
| 223 | + offset: 0x00000000, |
| 224 | + dev: (0x08, 0x01), |
| 225 | + inode: 0x60662705, |
| 226 | + pathname: "/usr/lib/locale/locale-archive".into(), |
| 227 | + } |
| 228 | + ); |
| 229 | + assert_eq!( |
| 230 | + "b7e02000-b7e03000 rw-p 00000000 00:00 0" |
| 231 | + .parse::<MapsEntry>() |
| 232 | + .unwrap(), |
| 233 | + MapsEntry { |
| 234 | + address: (0xb7e02000, 0xb7e03000), |
| 235 | + perms: ['r', 'w', '-', 'p'], |
| 236 | + offset: 0x00000000, |
| 237 | + dev: (0x00, 0x00), |
| 238 | + inode: 0x0, |
| 239 | + pathname: Default::default(), |
| 240 | + } |
| 241 | + ); |
| 242 | +} |
0 commit comments