|
| 1 | +#![feature(proc_macro)] |
| 2 | + |
| 3 | +extern crate assert_instr_macro; |
| 4 | +extern crate backtrace; |
| 5 | +extern crate cc; |
| 6 | +extern crate rustc_demangle; |
| 7 | +#[macro_use] |
| 8 | +extern crate lazy_static; |
| 9 | + |
| 10 | +use std::collections::HashMap; |
| 11 | +use std::env; |
| 12 | +use std::process::Command; |
| 13 | +use std::str; |
| 14 | + |
| 15 | +pub use assert_instr_macro::*; |
| 16 | + |
| 17 | +lazy_static! { |
| 18 | + static ref DISASSEMBLY: HashMap<String, Vec<Function>> = disassemble_myself(); |
| 19 | +} |
| 20 | + |
| 21 | +struct Function { |
| 22 | + instrs: Vec<Instruction>, |
| 23 | +} |
| 24 | + |
| 25 | +struct Instruction { |
| 26 | + parts: Vec<String>, |
| 27 | +} |
| 28 | + |
| 29 | +fn disassemble_myself() -> HashMap<String, Vec<Function>> { |
| 30 | + let me = env::current_exe().expect("failed to get current exe"); |
| 31 | + |
| 32 | + if cfg!(target_arch = "x86_64") && |
| 33 | + cfg!(target_os = "windows") && |
| 34 | + cfg!(target_env = "msvc") { |
| 35 | + let mut cmd = cc::windows_registry::find("x86_64-pc-windows-msvc", "dumpbin.exe") |
| 36 | + .expect("failed to find `dumpbin` tool"); |
| 37 | + let output = cmd.arg("/DISASM").arg(&me).output() |
| 38 | + .expect("failed to execute dumpbin"); |
| 39 | + println!("{}\n{}", output.status, String::from_utf8_lossy(&output.stderr)); |
| 40 | + assert!(output.status.success()); |
| 41 | + parse_dumpbin(&String::from_utf8_lossy(&output.stdout)) |
| 42 | + } else if cfg!(target_os = "windows") { |
| 43 | + panic!("disassembly unimplemented") |
| 44 | + } else if cfg!(target_os = "macos") { |
| 45 | + let output = Command::new("otool") |
| 46 | + .arg("-vt") |
| 47 | + .arg(&me) |
| 48 | + .output() |
| 49 | + .expect("failed to execute otool"); |
| 50 | + println!("{}\n{}", output.status, String::from_utf8_lossy(&output.stderr)); |
| 51 | + assert!(output.status.success()); |
| 52 | + |
| 53 | + parse_otool(&str::from_utf8(&output.stdout).expect("stdout not utf8")) |
| 54 | + } else { |
| 55 | + let output = Command::new("objdump") |
| 56 | + .arg("--disassemble") |
| 57 | + .arg(&me) |
| 58 | + .output() |
| 59 | + .expect("failed to execute objdump"); |
| 60 | + println!("{}\n{}", output.status, String::from_utf8_lossy(&output.stderr)); |
| 61 | + assert!(output.status.success()); |
| 62 | + |
| 63 | + parse_objdump(&str::from_utf8(&output.stdout).expect("stdout not utf8")) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn parse_objdump(output: &str) -> HashMap<String, Vec<Function>> { |
| 68 | + let mut lines = output.lines(); |
| 69 | + |
| 70 | + for line in output.lines().take(100) { |
| 71 | + println!("{}", line); |
| 72 | + } |
| 73 | + |
| 74 | + let mut ret = HashMap::new(); |
| 75 | + while let Some(header) = lines.next() { |
| 76 | + // symbols should start with `$hex_addr <$name>:` |
| 77 | + if !header.ends_with(">:") { |
| 78 | + continue |
| 79 | + } |
| 80 | + let start = header.find("<").unwrap(); |
| 81 | + let symbol = &header[start + 1..header.len() - 2]; |
| 82 | + |
| 83 | + let mut instructions = Vec::new(); |
| 84 | + while let Some(instruction) = lines.next() { |
| 85 | + if instruction.is_empty() { |
| 86 | + break |
| 87 | + } |
| 88 | + // Each line of instructions should look like: |
| 89 | + // |
| 90 | + // $rel_offset: ab cd ef 00 $instruction... |
| 91 | + let parts = instruction.split_whitespace() |
| 92 | + .skip(1) |
| 93 | + .skip_while(|s| { |
| 94 | + s.len() == 2 && usize::from_str_radix(s, 16).is_ok() |
| 95 | + }) |
| 96 | + .map(|s| s.to_string()) |
| 97 | + .collect::<Vec<String>>(); |
| 98 | + instructions.push(Instruction { parts }); |
| 99 | + } |
| 100 | + |
| 101 | + ret.entry(normalize(symbol)) |
| 102 | + .or_insert(Vec::new()) |
| 103 | + .push(Function { instrs: instructions }); |
| 104 | + } |
| 105 | + |
| 106 | + return ret |
| 107 | +} |
| 108 | + |
| 109 | +fn parse_otool(output: &str) -> HashMap<String, Vec<Function>> { |
| 110 | + let mut lines = output.lines(); |
| 111 | + |
| 112 | + for line in output.lines().take(100) { |
| 113 | + println!("{}", line); |
| 114 | + } |
| 115 | + |
| 116 | + let mut ret = HashMap::new(); |
| 117 | + let mut cached_header = None; |
| 118 | + loop { |
| 119 | + let header = match cached_header.take().or_else(|| lines.next()) { |
| 120 | + Some(header) => header, |
| 121 | + None => break, |
| 122 | + }; |
| 123 | + // symbols should start with `$symbol:` |
| 124 | + if !header.ends_with(":") { |
| 125 | + continue |
| 126 | + } |
| 127 | + // strip the leading underscore and the trailing colon |
| 128 | + let symbol = &header[1..header.len() - 1]; |
| 129 | + |
| 130 | + let mut instructions = Vec::new(); |
| 131 | + while let Some(instruction) = lines.next() { |
| 132 | + if instruction.ends_with(":") { |
| 133 | + cached_header = Some(instruction); |
| 134 | + break |
| 135 | + } |
| 136 | + // Each line of instructions should look like: |
| 137 | + // |
| 138 | + // $addr $instruction... |
| 139 | + let parts = instruction.split_whitespace() |
| 140 | + .skip(1) |
| 141 | + .map(|s| s.to_string()) |
| 142 | + .collect::<Vec<String>>(); |
| 143 | + instructions.push(Instruction { parts }); |
| 144 | + } |
| 145 | + |
| 146 | + ret.entry(normalize(symbol)) |
| 147 | + .or_insert(Vec::new()) |
| 148 | + .push(Function { instrs: instructions }); |
| 149 | + } |
| 150 | + |
| 151 | + return ret |
| 152 | +} |
| 153 | + |
| 154 | +fn parse_dumpbin(output: &str) -> HashMap<String, Vec<Function>> { |
| 155 | + let mut lines = output.lines(); |
| 156 | + |
| 157 | + for line in output.lines().take(100) { |
| 158 | + println!("{}", line); |
| 159 | + } |
| 160 | + |
| 161 | + let mut ret = HashMap::new(); |
| 162 | + let mut cached_header = None; |
| 163 | + loop { |
| 164 | + let header = match cached_header.take().or_else(|| lines.next()) { |
| 165 | + Some(header) => header, |
| 166 | + None => break, |
| 167 | + }; |
| 168 | + // symbols should start with `$symbol:` |
| 169 | + if !header.ends_with(":") { |
| 170 | + continue |
| 171 | + } |
| 172 | + // strip the trailing colon |
| 173 | + let symbol = &header[..header.len() - 1]; |
| 174 | + |
| 175 | + let mut instructions = Vec::new(); |
| 176 | + while let Some(instruction) = lines.next() { |
| 177 | + if !instruction.starts_with(" ") { |
| 178 | + cached_header = Some(instruction); |
| 179 | + break |
| 180 | + } |
| 181 | + // Each line looks like: |
| 182 | + // |
| 183 | + // > $addr: ab cd ef $instr.. |
| 184 | + // > 00 12 # this line os optional |
| 185 | + if instruction.starts_with(" ") { |
| 186 | + continue |
| 187 | + } |
| 188 | + let parts = instruction.split_whitespace() |
| 189 | + .skip(1) |
| 190 | + .skip_while(|s| { |
| 191 | + s.len() == 2 && usize::from_str_radix(s, 16).is_ok() |
| 192 | + }) |
| 193 | + .map(|s| s.to_string()) |
| 194 | + .collect::<Vec<String>>(); |
| 195 | + instructions.push(Instruction { parts }); |
| 196 | + } |
| 197 | + |
| 198 | + ret.entry(normalize(symbol)) |
| 199 | + .or_insert(Vec::new()) |
| 200 | + .push(Function { instrs: instructions }); |
| 201 | + } |
| 202 | + |
| 203 | + return ret |
| 204 | +} |
| 205 | + |
| 206 | +fn normalize(symbol: &str) -> String { |
| 207 | + let symbol = rustc_demangle::demangle(symbol).to_string(); |
| 208 | + match symbol.rfind("::h") { |
| 209 | + Some(i) => symbol[..i].to_string(), |
| 210 | + None => symbol.to_string(), |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +pub fn assert(fnptr: usize, expected: &str) { |
| 215 | + let mut sym = None; |
| 216 | + backtrace::resolve(fnptr as *mut _, |name| { |
| 217 | + sym = name.name().and_then(|s| s.as_str()).map(normalize); |
| 218 | + }); |
| 219 | + |
| 220 | + let sym = match sym { |
| 221 | + Some(s) => s, |
| 222 | + None => panic!("failed to get symbol of function pointer: {}", fnptr), |
| 223 | + }; |
| 224 | + |
| 225 | + let functions = &DISASSEMBLY.get(&sym) |
| 226 | + .expect(&format!("failed to find disassembly of {}", sym)); |
| 227 | + assert_eq!(functions.len(), 1); |
| 228 | + let function = &functions[0]; |
| 229 | + for instr in function.instrs.iter() { |
| 230 | + if let Some(part) = instr.parts.get(0) { |
| 231 | + if part == expected { |
| 232 | + return |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + println!("disassembly for {}: ", sym); |
| 238 | + for (i, instr) in function.instrs.iter().enumerate() { |
| 239 | + print!("\t{:2}: ", i); |
| 240 | + for part in instr.parts.iter() { |
| 241 | + print!("{} ", part); |
| 242 | + } |
| 243 | + println!(""); |
| 244 | + } |
| 245 | + panic!("failed to find instruction `{}` in the disassembly", expected); |
| 246 | +} |
| 247 | + |
0 commit comments