Skip to content

Commit

Permalink
Make disassembler output more readable (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmakarov committed Sep 17, 2021
1 parent d0b0a16 commit c3dd337
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,46 @@ fn byteswap_str(name: &str, insn: &ebpf::Insn) -> String {
format!("{}{} r{}", name, insn.imm, insn.dst)
}

#[inline]
fn signed_off_str(value: i16) -> String {
if value < 0 {
format!("-{:#x}", -value)
} else {
format!("+{:#x}", value)
}
}

#[inline]
fn ld_st_imm_str(name: &str, insn: &ebpf::Insn) -> String {
format!("{} [r{}+{:#x}], {}", name, insn.dst, insn.off, insn.imm)
format!(
"{} [r{}{}], {}",
name,
insn.dst,
signed_off_str(insn.off),
insn.imm
)
}

#[inline]
fn ld_reg_str(name: &str, insn: &ebpf::Insn) -> String {
format!("{} r{}, [r{}+{:#x}]", name, insn.dst, insn.src, insn.off)
format!(
"{} r{}, [r{}{}]",
name,
insn.dst,
insn.src,
signed_off_str(insn.off)
)
}

#[inline]
fn st_reg_str(name: &str, insn: &ebpf::Insn) -> String {
format!("{} [r{}+{:#x}], r{}", name, insn.dst, insn.off, insn.src)
format!(
"{} [r{}{}], r{}",
name,
insn.dst,
signed_off_str(insn.off),
insn.src
)
}

#[inline]
Expand Down
6 changes: 6 additions & 0 deletions tests/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ fn test_neg64() {
#[test]
fn test_ldxw() {
disasm!("entrypoint:\n ldxw r1, [r2+0x5]\n");
disasm!("entrypoint:\n ldxw r1, [r2-0x5]\n");
}

// Example for InstructionType::StoreImm.
#[test]
fn test_stw() {
disasm!("entrypoint:\n stw [r2+0x5], 7\n");
disasm!("entrypoint:\n stw [r2-0x5], 7\n");
}

// Example for InstructionType::StoreReg.
#[test]
fn test_stxw() {
disasm!("entrypoint:\n stxw [r2+0x5], r8\n");
disasm!("entrypoint:\n stxw [r2-0x5], r8\n");
}

// Example for InstructionType::JumpUnconditional.
Expand Down Expand Up @@ -147,18 +150,21 @@ fn test_ldindw() {
#[test]
fn test_ldxdw() {
disasm!("entrypoint:\n ldxdw r1, [r2+0x3]\n");
disasm!("entrypoint:\n ldxdw r1, [r2-0x3]\n");
}

// Example for InstructionType::StoreImm.
#[test]
fn test_sth() {
disasm!("entrypoint:\n sth [r1+0x2], 3\n");
disasm!("entrypoint:\n sth [r1-0x2], 3\n");
}

// Example for InstructionType::StoreReg.
#[test]
fn test_stxh() {
disasm!("entrypoint:\n stxh [r1+0x2], r3\n");
disasm!("entrypoint:\n stxh [r1-0x2], r3\n");
}

// Test all supported AluBinary mnemonics.
Expand Down

0 comments on commit c3dd337

Please sign in to comment.