Files
wasmtime/crates/lightbeam/src/disassemble.rs
Jef 957677c6f5 Integrate Lightbeam with latest Wasmtime master (#1232)
* Implement trap info in Lightbeam

* Start using wasm-reader instead of wasmparser for parsing operators

* Update to use wasm-reader, some reductions in allocation, support source location tracking for traps, start to support multi-value

The only thing that still needs to be supported for multi-value is stack returns, but we need to make it compatible with Cranelift.

* Error when running out of registers (although we'd hope it should be impossible) instead of panicking

* WIP: Update Lightbeam to work with latest Wasmtime

* WIP: Update Lightbeam to use current wasmtime

* WIP: Migrate to new system for builtin functions

* WIP: Update Lightbeam to work with latest Wasmtime

* Remove multi_mut

* Format

* Fix some bugs around arguments, add debuginfo offset tracking

* Complete integration with new Wasmtime

* Remove commented code

* Fix formatting

* Fix warnings, remove unused dependencies

* Fix `iter` if there are too many elements, fix compilation for latest wasmtime

* Fix float arguments on stack

* Remove wasm-reader and trap info work

* Allocate stack space _before_ passing arguments, fail if we can't zero a xmm reg

* Fix stack argument offset calculation

* Fix stack arguments in Lightbeam

* Re-add WASI because it somehow got removed during rebase

* Workaround for apparent `type_alias_impl_trait`-related bug in rustdoc

* Fix breakages caused by rebase, remove module offset info as it is unrelated to wasmtime integration PR and was broken by rebase

* Add TODO comment explaining `lightbeam::ModuleContext` trait
2020-04-29 16:26:40 -07:00

52 lines
1.2 KiB
Rust

use capstone::prelude::*;
use dynasmrt::AssemblyOffset;
use std::error::Error;
use std::fmt::{Display, Write};
pub fn disassemble<D: Display>(
mem: &[u8],
mut ops: &[(AssemblyOffset, D)],
) -> Result<(), Box<dyn Error>> {
let cs = Capstone::new()
.x86()
.mode(arch::x86::ArchMode::Mode64)
.build()?;
println!("{} bytes:", mem.len());
let insns = cs.disasm_all(&mem, 0x0)?;
for i in insns.iter() {
let mut line = String::new();
let address = i.address();
while let Some((offset, op)) = ops.first() {
if offset.0 as u64 <= address {
ops = &ops[1..];
println!("{}", op);
} else {
break;
}
}
write!(&mut line, "{:4x}:\t", i.address())?;
let mut bytes_str = String::new();
for b in i.bytes() {
write!(&mut bytes_str, "{:02x} ", b)?;
}
write!(&mut line, "{:24}\t", bytes_str)?;
if let Some(s) = i.mnemonic() {
write!(&mut line, "{}\t", s)?;
}
if let Some(s) = i.op_str() {
write!(&mut line, "{}", s)?;
}
println!("{}", line);
}
Ok(())
}