Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pulley: Add a simple disassembler example #9656

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pulley/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ sptr = { workspace = true }

[dev-dependencies]
env_logger = { workspace = true }
object = { workspace = true, features = ['std'] }
anyhow = { workspace = true, features = ['std'] }

[features]
std = []
Expand All @@ -31,3 +33,7 @@ interp = ["decode", "encode"]

[package.metadata.docs.rs]
all-features = true

[[example]]
name = "objdump"
required-features = ["disas"]
48 changes: 48 additions & 0 deletions pulley/examples/objdump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Small helper utility to disassemble `*.cwasm` files produced by Wasmtime.
//!
//! Run with:
//!
//! cargo run --example objdump -F disas -p pulley-interpreter foo.cwasm

use anyhow::{bail, Result};
use object::{File, Object as _, ObjectSection, ObjectSymbol, SectionKind, SymbolKind};
use pulley_interpreter::decode::Decoder;
use pulley_interpreter::disas::Disassembler;

fn main() -> Result<()> {
let cwasm = std::fs::read(std::env::args().nth(1).unwrap())?;

let image = File::parse(&cwasm[..])?;

let text = match image.sections().find(|s| s.kind() == SectionKind::Text) {
Some(section) => section.data()?,
None => bail!("no text section"),
};

for sym in image.symbols() {
if !sym.is_definition() {
continue;
}
if sym.kind() != SymbolKind::Text {
continue;
}
let address = sym.address();
let size = sym.size();
if size == 0 {
continue;
}

let name = sym.name()?;
let code = &text[address as usize..][..size as usize];

println!("{address:#08x}: <{name}>:");
let mut disas = Disassembler::new(code);
disas.start_offset(address as usize);
let result = Decoder::decode_all(&mut disas);
println!("{}", disas.disas());
if let Err(e) = result {
println!(" : error disassembling: {e:?}");
}
}
Ok(())
}
16 changes: 14 additions & 2 deletions pulley/src/disas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Disassembler<'a> {
raw_bytecode: &'a [u8],
bytecode: SafeBytecodeStream<'a>,
disas: String,
start_offset: usize,
start: usize,
temp: String,
offsets: bool,
Expand All @@ -39,6 +40,7 @@ impl<'a> Disassembler<'a> {
bytecode: SafeBytecodeStream::new(bytecode),
disas: String::new(),
start: 0,
start_offset: 0,
temp: String::new(),
offsets: true,
hexdump: true,
Expand All @@ -61,6 +63,16 @@ impl<'a> Disassembler<'a> {
self
}

/// Configures the offset that this function starts from, if it doesn't
/// start from 0.
///
/// This can possibly be useful when a single function at a time is being
/// disassembled.
pub fn start_offset(&mut self, offset: usize) -> &mut Self {
self.start_offset = offset;
self
}

/// Get the disassembly thus far.
pub fn disas(&self) -> &str {
&self.disas
Expand All @@ -73,7 +85,7 @@ impl<'a> Disassembler<'a> {
write!(&mut self.temp, ",").unwrap();
}
write!(&mut self.temp, " ").unwrap();
val.disas(self.start, &mut self.temp);
val.disas(self.start + self.start_offset, &mut self.temp);
}
}
}
Expand Down Expand Up @@ -216,7 +228,7 @@ impl<'a> OpVisitor for Disassembler<'a> {

fn after_visit(&mut self) {
if self.offsets {
write!(&mut self.disas, "{:8x}: ", self.start).unwrap();
write!(&mut self.disas, "{:8x}: ", self.start + self.start_offset).unwrap();
}
if self.hexdump {
let size = self.bytecode.position() - self.start;
Expand Down