Skip to content

Commit

Permalink
add iced-x86 to benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
iximeow committed Aug 9, 2020
1 parent 71580bd commit 3b5fd94
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
'bench/xed/bench-xed-no-fmt',
'bench/distorm/bench-distorm-fmt',
'bench/distorm/bench-distorm-no-fmt',
'bench/iced-x86/bench-iced-fmt',
'bench/iced-x86/bench-iced-no-fmt',
]
timings = []

Expand Down
4 changes: 4 additions & 0 deletions bench/iced-x86/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bench-iced-fmt
bench-iced-no-fmt
Cargo.lock
target/
17 changes: 17 additions & 0 deletions bench/iced-x86/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "iced-bench"
version = "0.0.0"
authors = ["iximeow <me@iximeow.net>"]
description = "benchmarking wrapper for iced-x86, for use in disas-bench"
edition = "2018"

[[bin]]
name = "iced-bench"
path = "src/main.rs"

[profile.release]
opt-level = 3
lto = true

[dependencies]
iced-x86 = "1.8.0"
10 changes: 10 additions & 0 deletions bench/iced-x86/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
make-bench-iced-x86:
cargo build --release
cp target/release/iced-bench bench-iced-fmt
RUSTFLAGS="--cfg DISAS_BENCH_NO_FORMAT" cargo build --release
cp target/release/iced-bench bench-iced-no-fmt

clean:
cargo clean
rm bench-iced-fmt
rm bench-iced-no-fmt
76 changes: 76 additions & 0 deletions bench/iced-x86/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fs::File;
use std::io::Read;
use std::io::Seek;
use std::io::SeekFrom;

use iced_x86::{Decoder, DecoderError, DecoderOptions, Instruction};

#[cfg(not(DISAS_BENCH_NO_FORMAT))]
use iced_x86::{Formatter, NasmFormatter};

// translated from `load_bin.inc`

const XUL_TEXT_OFFS: u64 = 0x400;
const XUL_TEXT_LEN: usize = 0x2460400;

fn read_xul_dll() -> std::io::Result<Box<[u8]>> {
let mut f = File::open("../../input/xul.dll")?;
f.seek(SeekFrom::Start(XUL_TEXT_OFFS))?;
let mut code = vec![0; XUL_TEXT_LEN];
f.read_exact(&mut code)?;
Ok(code.into_boxed_slice())
}

pub fn main() {
let xul_code = match read_xul_dll() {
Ok(code) => code,
Err(_) => {
eprintln!("Can't read xul.dll");
return;
}
};

let mut decoder = Decoder::new(
64, // 64-bit
&xul_code,
DecoderOptions::NONE
);

#[cfg(not(DISAS_BENCH_NO_FORMAT))]
let mut text = String::new();
#[cfg(not(DISAS_BENCH_NO_FORMAT))]
let mut formatter = NasmFormatter::new();

let mut instruction = Instruction::default();

let mut num_valid_insns: usize = 0;
let mut num_bad_insns: usize = 0;
for _round in 0..20 {
decoder.set_position(0);
while decoder.can_decode() {
let offset = decoder.position();
decoder.decode_out(&mut instruction);

if decoder.last_error() == DecoderError::None {
#[cfg(not(DISAS_BENCH_NO_FORMAT))]
{
text.clear();
formatter.format(&instruction, &mut text);
}

num_valid_insns += 1;
// position is automatically updated in the happy path
} else {
num_bad_insns += 1;
// manually seek forward one byte to try again
decoder.set_position(offset + 1);
}
}
}

println!("Disassembled {} instructions ({} valid, {} bad)",
num_valid_insns + num_bad_insns,
num_valid_insns,
num_bad_insns,
);
}
1 change: 1 addition & 0 deletions clean-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(cd bench/zydis; rm -rf zydis cmake Makefile CMakeCache.txt CMakeFiles cmake_install.cmake bench-zydis*)
(cd bench/xed; make clean)
(cd bench/distorm; make clean)
(cd bench/iced-x86; make clean)
rm bench/distorm/libdistorm*

rm libs/distorm/make/mac/libdistorm*
Expand Down
5 changes: 5 additions & 0 deletions make-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,10 @@ cd bench/distorm
make || exit 1
cd ../..

echo "[*] Building iced-x86 benchmark ..."
cd bench/iced-x86
make || exit 1
cd ../..

echo "[+] All done!"

0 comments on commit 3b5fd94

Please sign in to comment.