2018-09-12 11:46:27 +02:00
|
|
|
//! Disassembly calling function for `wasm32` targets.
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
|
|
use ::*;
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(module = "child_process")]
|
|
|
|
|
extern "C" {
|
|
|
|
|
#[wasm_bindgen(js_name = execSync)]
|
|
|
|
|
fn exec_sync(cmd: &str) -> Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(module = "buffer")]
|
|
|
|
|
extern "C" {
|
|
|
|
|
type Buffer;
|
|
|
|
|
#[wasm_bindgen(method, js_name = toString)]
|
|
|
|
|
fn to_string(this: &Buffer) -> String;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
#[wasm_bindgen(js_namespace = require)]
|
|
|
|
|
fn resolve(module: &str) -> String;
|
|
|
|
|
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
2018-12-13 20:17:30 -06:00
|
|
|
pub fn js_console_log(s: &str);
|
2018-09-12 11:46:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn disassemble_myself() -> HashMap<String, Vec<Function>> {
|
|
|
|
|
use std::path::Path;
|
2018-10-23 09:10:54 -07:00
|
|
|
::console_error_panic_hook::set_once();
|
2018-09-12 11:46:27 +02:00
|
|
|
// Our wasm module in the wasm-bindgen test harness is called
|
|
|
|
|
// "wasm-bindgen-test_bg". When running in node this is actually a shim JS
|
|
|
|
|
// file. Ask node where that JS file is, and then we use that with a wasm
|
|
|
|
|
// extension to find the wasm file itself.
|
|
|
|
|
let js_shim = resolve("wasm-bindgen-test_bg");
|
|
|
|
|
let js_shim = Path::new(&js_shim).with_extension("wasm");
|
|
|
|
|
|
|
|
|
|
// Execute `wasm2wat` synchronously, waiting for and capturing all of its
|
|
|
|
|
// output.
|
|
|
|
|
let output =
|
|
|
|
|
exec_sync(&format!("wasm2wat {}", js_shim.display())).to_string();
|
|
|
|
|
|
|
|
|
|
let mut ret: HashMap<String, Vec<Function>> = HashMap::new();
|
|
|
|
|
let mut lines = output.lines().map(|s| s.trim());
|
|
|
|
|
while let Some(line) = lines.next() {
|
|
|
|
|
// If we found the table of function pointers, fill in the known
|
|
|
|
|
// address for all our `Function` instances
|
|
|
|
|
if line.starts_with("(elem") {
|
2018-12-13 20:17:30 -06:00
|
|
|
let mut parts = line.split_whitespace().skip(3);
|
|
|
|
|
let offset = parts.next()
|
|
|
|
|
.unwrap()
|
2019-01-30 15:11:35 -08:00
|
|
|
.trim_end_matches(")")
|
2018-12-13 20:17:30 -06:00
|
|
|
.parse::<usize>()
|
|
|
|
|
.unwrap();
|
|
|
|
|
for (i, name) in parts.enumerate() {
|
2019-01-30 15:11:35 -08:00
|
|
|
let name = name.trim_end_matches(")");
|
2018-09-12 11:46:27 +02:00
|
|
|
for f in ret.get_mut(name).expect("ret.get_mut(name) failed") {
|
2018-12-13 20:17:30 -06:00
|
|
|
f.addr = Some(i + offset);
|
2018-09-12 11:46:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this isn't a function, we don't care about it.
|
|
|
|
|
if !line.starts_with("(func ") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut function = Function {
|
|
|
|
|
instrs: Vec::new(),
|
|
|
|
|
addr: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Empty functions will end in `))` so there's nothing to do, otherwise
|
|
|
|
|
// we'll have a bunch of following lines which are instructions.
|
|
|
|
|
//
|
|
|
|
|
// Lines that have an imbalanced `)` mark the end of a function.
|
|
|
|
|
if !line.ends_with("))") {
|
|
|
|
|
while let Some(line) = lines.next() {
|
|
|
|
|
function.instrs.push(Instruction {
|
|
|
|
|
parts: line
|
|
|
|
|
.split_whitespace()
|
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
|
.collect(),
|
|
|
|
|
});
|
|
|
|
|
if !line.starts_with("(") && line.ends_with(")") {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The second element here split on whitespace should be the name of
|
|
|
|
|
// the function, skipping the type/params/results
|
|
|
|
|
ret.entry(line.split_whitespace().nth(1).unwrap().to_string())
|
|
|
|
|
.or_insert(Vec::new())
|
|
|
|
|
.push(function);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|