Skip to content

Commit

Permalink
cargo clippy --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tnballo committed Jun 12, 2021
1 parent ec880b7 commit ce8d918
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 145 deletions.
4 changes: 2 additions & 2 deletions benches/bench_1_misc.rs
Expand Up @@ -21,7 +21,7 @@ pub fn filter_stack_pivot_seq_fast<'a>(
let mut info_factory = iced_x86::InstructionInfoFactory::new();

let info = info_factory
.info_options(&instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);
.info_options(instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);

if info.used_registers().contains(&rsp_write)
|| info.used_registers().contains(&esp_write)
Expand All @@ -46,7 +46,7 @@ pub fn filter_reg_pop_only_regex<'a>(
let re = Regex::new(r"^(?:pop)(?:.*(?:pop))*.*(?:ret|call|jmp)").unwrap();
let mut matches = Vec::new();

for (instrs, addrs) in xgadget::fmt_gadget_str_list(&gadgets, false, false) {
for (instrs, addrs) in xgadget::fmt_gadget_str_list(gadgets, false, false) {
if re.is_match(&instrs) {
matches.push((instrs, addrs));
}
Expand Down
8 changes: 4 additions & 4 deletions benches/bench_2_fmt.rs
Expand Up @@ -46,7 +46,7 @@ pub const X_RET_AFTER_JNE_AND_ADJACENT_CALL_MIX_MATCH_X64: &[u8] = &[
];

fn get_raw_bin(name: &str, bytes: &[u8]) -> xgadget::Binary {
let mut bin = xgadget::Binary::from_bytes(&name, &bytes).unwrap();
let mut bin = xgadget::Binary::from_bytes(name, bytes).unwrap();
assert_eq!(bin.format(), xgadget::Format::Raw);
assert_eq!(bin.arch(), xgadget::Arch::Unknown);
bin.set_arch(xgadget::Arch::X64);
Expand Down Expand Up @@ -90,7 +90,7 @@ fn collect_strs_seq(gadgets: &[xgadget::Gadget], extended: bool) -> Vec<String>
}

fn fmt_bench(c: &mut Criterion) {
let bin_ret_post_jmp = get_raw_bin("bin_ret_post_jmp", &X_RET_AFTER_JNE_AND_ADJACENT_JMP_X64);
let bin_ret_post_jmp = get_raw_bin("bin_ret_post_jmp", X_RET_AFTER_JNE_AND_ADJACENT_JMP_X64);
let bins = vec![bin_ret_post_jmp];
let gadgets = xgadget::find_gadgets(&bins, MAX_LEN, xgadget::SearchConfig::DEFAULT).unwrap();

Expand All @@ -104,8 +104,8 @@ fn fmt_bench(c: &mut Criterion) {
}

fn fmt_partial_bench(c: &mut Criterion) {
let bin_ret_jmp = get_raw_bin("bin_ret_jmp", &X_RET_AFTER_JNE_AND_ADJACENT_JMP_X64);
let bin_mix = get_raw_bin("bin_mix", &X_RET_AFTER_JNE_AND_ADJACENT_CALL_MIX_MATCH_X64);
let bin_ret_jmp = get_raw_bin("bin_ret_jmp", X_RET_AFTER_JNE_AND_ADJACENT_JMP_X64);
let bin_mix = get_raw_bin("bin_mix", X_RET_AFTER_JNE_AND_ADJACENT_CALL_MIX_MATCH_X64);

let full_part_match_config = xgadget::SearchConfig::DEFAULT | xgadget::SearchConfig::PART;
assert!(full_part_match_config.intersects(xgadget::SearchConfig::PART));
Expand Down
6 changes: 3 additions & 3 deletions src/binary/binary.rs
Expand Up @@ -104,11 +104,11 @@ impl Binary {

// Bytes -> Binary
fn priv_from_buf(name: &str, bytes: &[u8]) -> Result<Binary, Box<dyn Error>> {
match goblin::Object::parse(&bytes) {
match goblin::Object::parse(bytes) {
Ok(obj) => match obj {
goblin::Object::Unknown(_) => Ok(Binary::from_raw(name, bytes)),
goblin::Object::Elf(elf) => Binary::from_elf(name, &bytes, &elf),
goblin::Object::PE(pe) => Binary::from_pe(name, &bytes, &pe),
goblin::Object::Elf(elf) => Binary::from_elf(name, bytes, &elf),
goblin::Object::PE(pe) => Binary::from_pe(name, bytes, &pe),
_ => Err("Unsupported file format!".into()),
},
_ => Ok(Binary::from_raw(name, bytes)),
Expand Down
4 changes: 2 additions & 2 deletions src/cli/main.rs
Expand Up @@ -35,7 +35,7 @@ fn main() {
let bins: Vec<xgadget::Binary> = cli
.bin_paths
.par_iter()
.map(|path| xgadget::Binary::from_path_str(&path).unwrap())
.map(|path| xgadget::Binary::from_path_str(path).unwrap())
.map(|mut binary| {
if binary.arch() == xgadget::Arch::Unknown {
binary.set_arch(cli.arch);
Expand Down Expand Up @@ -130,7 +130,7 @@ fn main() {
let mut filtered_gadgets: Vec<(xgadget::Gadget, String)> = gadgets_and_strs
.into_iter()
.filter(|(_, s)| match &filter_regex {
Some(r) => match r.is_match(&s) {
Some(r) => match r.is_match(s) {
true => {
filter_matches += 1;
true
Expand Down
5 changes: 1 addition & 4 deletions src/cli/reg_str.rs
Expand Up @@ -32,10 +32,7 @@ lazy_static! {

/// Case-insensitive string to register enum conversion
pub fn str_to_reg(rs: &str) -> Option<iced_x86::Register> {
match STR_REG_MAP.get(&rs.to_uppercase()) {
Some(reg) => Some(*reg),
None => None,
}
STR_REG_MAP.get(&rs.to_uppercase()).copied()
}

// Test ----------------------------------------------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/filters.rs
Expand Up @@ -10,7 +10,7 @@ pub fn filter_stack_pivot<'a>(gadgets: &[gadget::Gadget<'a>]) -> Vec<gadget::Gad
gadgets
.par_iter()
.filter(|g| {
let regs_overwritten = gadget::GadgetAnalysis::new(&g).regs_overwritten();
let regs_overwritten = gadget::GadgetAnalysis::new(g).regs_overwritten();
if regs_overwritten.contains(&iced_x86::Register::RSP)
|| regs_overwritten.contains(&iced_x86::Register::ESP)
|| regs_overwritten.contains(&iced_x86::Register::SP)
Expand All @@ -33,7 +33,7 @@ pub fn filter_dispatcher<'a>(gadgets: &[gadget::Gadget<'a>]) -> Vec<gadget::Gadg
// Predictable update of dispatch register
let dispatch_reg = tail_instr.op0_register();
for i in preceding_instrs {
if semantics::is_reg_rw(&i, &dispatch_reg) {
if semantics::is_reg_rw(i, &dispatch_reg) {
return true;
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn filter_set_params<'a>(

// Sets param reg
for reg in param_regs {
if semantics::is_reg_set(&instr, &reg) {
if semantics::is_reg_set(instr, reg) {
return true;
}
}
Expand All @@ -126,7 +126,7 @@ pub fn filter_no_deref<'a>(
gadgets
.par_iter()
.filter(|g| {
let mut regs_derefed = gadget::GadgetAnalysis::new(&g).regs_dereferenced();
let mut regs_derefed = gadget::GadgetAnalysis::new(g).regs_dereferenced();
match opt_regs {
Some(regs) => regs.iter().all(|r| !regs_derefed.contains(r)),
None => {
Expand All @@ -152,7 +152,7 @@ pub fn filter_regs_overwritten<'a>(
gadgets
.par_iter()
.filter(|g| {
let regs_overwritten = gadget::GadgetAnalysis::new(&g).regs_overwritten();
let regs_overwritten = gadget::GadgetAnalysis::new(g).regs_overwritten();
match opt_regs {
Some(regs) => regs.iter().all(|r| regs_overwritten.contains(r)),
None => !regs_overwritten.is_empty(),
Expand Down
4 changes: 2 additions & 2 deletions src/gadget/analysis.rs
Expand Up @@ -29,7 +29,7 @@ impl GadgetAnalysis {
let mut unique_used_mem = HashSet::default();

for instr in &gadget.instrs {
let info = info_factory.info(&instr);
let info = info_factory.info(instr);

for ur in info.used_registers() {
unique_used_regs.insert(*ur);
Expand Down Expand Up @@ -72,7 +72,7 @@ impl GadgetAnalysis {
.filter(|ur| ur.access() == iced_x86::OpAccess::ReadWrite)
.map(|ur| ur.register())
// TODO: overwrite taking precedence doesn't take into account conditional behavior
.filter(|r| !self.regs_overwritten().contains(&r))
.filter(|r| !self.regs_overwritten().contains(r))
.collect()
}

Expand Down
13 changes: 5 additions & 8 deletions src/gadget/gadget.rs
Expand Up @@ -85,10 +85,7 @@ impl<'a> Gadget<'a> {

/// Get first full match
pub fn first_full_match(&self) -> Option<u64> {
match self.full_matches.iter().next() {
Some(addr) => Some(*addr),
None => None,
}
self.full_matches.iter().next().copied()
}

/// Get count of binaries for which this gadget tracks partial matches
Expand All @@ -114,7 +111,7 @@ impl<'a> Gadget<'a> {
let mut formatter = fmt::get_formatter(att_syntax);
let mut output = fmt::GadgetFormatterOutput::new();
for instr in &self.instrs {
formatter.format(&instr, &mut output);
formatter.format(instr, &mut output);
output.write("; ", iced_x86::FormatterTextKind::Punctuation);
}
Box::new(output)
Expand Down Expand Up @@ -227,7 +224,7 @@ impl<'a> Gadget<'a> {
let mut formatter = fmt::get_formatter(att_syntax);
let mut output = String::new();
for instr in &self.instrs {
formatter.format(&instr, &mut output);
formatter.format(instr, &mut output);
output.write("; ", iced_x86::FormatterTextKind::Punctuation);
}
output
Expand Down Expand Up @@ -262,11 +259,11 @@ impl<'a> Gadget<'a> {
}

for pb in prior_bpm_bins {
Self::write_bin_name(&pb.name(), match_str);
Self::write_bin_name(pb.name(), match_str);
fmted_bin_cnt += 1;
}

Self::write_bin_name(&last_bin.name(), match_str);
Self::write_bin_name(last_bin.name(), match_str);
fmted_bin_cnt += 1;
match_str.write(
&format!("{:#016x}", bpm_addr),
Expand Down
12 changes: 6 additions & 6 deletions src/search.rs
Expand Up @@ -56,7 +56,7 @@ pub fn find_gadgets(

for (next_bin, next_set) in remaining_results {
// Filter common gadgets (set intersection)
common_gadgets.retain(|g| next_set.contains(&g));
common_gadgets.retain(|g| next_set.contains(g));

// TODO: there has to be a cleaner way to implement this! Once drain_filter() on stable?
// Update full and partial matches
Expand Down Expand Up @@ -91,7 +91,7 @@ pub fn find_gadgets(
}

for addr in &next_set_g.full_matches {
match updated_g.partial_matches.get_mut(&addr) {
match updated_g.partial_matches.get_mut(addr) {
Some(bin_ref_vec) => bin_ref_vec.push(*next_bin),
// TODO: Replace with unwrap_none() once on stable
_ => {
Expand Down Expand Up @@ -239,14 +239,14 @@ fn iterative_decode(d_config: &DecodeConfig) -> Vec<(Vec<iced_x86::Instruction>,
if let Some(i) = instrs.last() {
// ROP
// Note: 1 instr gadget (e.g. "ret;") for 16 byte re-alignment of stack pointer (avoid movaps segfault)
if (semantics::is_ret(&i))
if (semantics::is_ret(i))

// JOP
|| (semantics::is_jop_gadget_tail(&i))
|| (semantics::is_jop_gadget_tail(i))

// SYS
|| (semantics::is_syscall(&i)
|| (semantics::is_legacy_linux_syscall(&i) && (d_config.bin.format() == binary::Format::ELF)))
|| (semantics::is_syscall(i)
|| (semantics::is_legacy_linux_syscall(i) && (d_config.bin.format() == binary::Format::ELF)))
{
debug_assert!(instrs[0].ip() == buf_start_addr);
instr_sequences.push((instrs, buf_start_addr));
Expand Down
4 changes: 2 additions & 2 deletions src/semantics.rs
Expand Up @@ -83,7 +83,7 @@ pub fn is_legacy_linux_syscall(instr: &iced_x86::Instruction) -> bool {
#[inline(always)]
pub fn is_reg_rw(instr: &iced_x86::Instruction, reg: &iced_x86::Register) -> bool {
let mut info_factory = iced_x86::InstructionInfoFactory::new();
let info = info_factory.info_options(&instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);
let info = info_factory.info_options(instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);
let reg_rw = iced_x86::UsedRegister::new(*reg, iced_x86::OpAccess::ReadWrite);

info.used_registers().contains(&reg_rw)
Expand All @@ -93,7 +93,7 @@ pub fn is_reg_rw(instr: &iced_x86::Instruction, reg: &iced_x86::Register) -> boo
#[inline(always)]
pub fn is_reg_set(instr: &iced_x86::Instruction, reg: &iced_x86::Register) -> bool {
let mut info_factory = iced_x86::InstructionInfoFactory::new();
let info = info_factory.info_options(&instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);
let info = info_factory.info_options(instr, iced_x86::InstructionInfoOptions::NO_MEMORY_USAGE);
let reg_w = iced_x86::UsedRegister::new(*reg, iced_x86::OpAccess::Write);

let reg_read = |ur: iced_x86::UsedRegister| {
Expand Down
8 changes: 4 additions & 4 deletions tests/common.rs
Expand Up @@ -173,15 +173,15 @@ pub const MISC_2: &[u8] = &[

#[allow(dead_code)]
pub fn decode_single_x64_instr(ip: u64, bytes: &[u8]) -> iced_x86::Instruction {
let mut decoder = iced_x86::Decoder::new(64, &bytes, iced_x86::DecoderOptions::NONE);
let mut decoder = iced_x86::Decoder::new(64, bytes, iced_x86::DecoderOptions::NONE);
decoder.set_ip(ip);

decoder.decode()
}

#[allow(dead_code)]
pub fn get_raw_bin(name: &str, bytes: &[u8]) -> xgadget::Binary {
let mut bin = xgadget::Binary::from_bytes(&name, &bytes).unwrap();
let mut bin = xgadget::Binary::from_bytes(name, bytes).unwrap();
assert_eq!(bin.format(), xgadget::Format::Raw);
assert_eq!(bin.arch(), xgadget::Arch::Unknown);
bin.set_arch(xgadget::Arch::X64);
Expand All @@ -192,7 +192,7 @@ pub fn get_raw_bin(name: &str, bytes: &[u8]) -> xgadget::Binary {
#[allow(dead_code)]
pub fn get_gadget_strs(gadgets: &Vec<xgadget::Gadget>, att_syntax: bool) -> Vec<String> {
let mut strs = Vec::new();
for (mut instr, addrs) in xgadget::fmt_gadget_str_list(&gadgets, att_syntax, false) {
for (mut instr, addrs) in xgadget::fmt_gadget_str_list(gadgets, att_syntax, false) {
instr.push(' ');
strs.push(format!("{:-<150} {}", instr, addrs));
}
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn hash<T: Hash>(t: &T) -> u64 {
pub fn dump_instr(instr: &iced_x86::Instruction) {
let mut info_factory = iced_x86::InstructionInfoFactory::new();
let op_code = instr.op_code();
let info = info_factory.info(&instr);
let info = info_factory.info(instr);
let fpu_info = instr.fpu_stack_increment_info();
println!("\n\tOpCode: {}", op_code.op_code_string());
println!("\tInstruction: {}", op_code.instruction_string());
Expand Down
48 changes: 12 additions & 36 deletions tests/test_gadget.rs
Expand Up @@ -19,47 +19,23 @@ fn test_gadget_hasher() {
addr_2.insert(1);

// Different instructions, different address - custom hash mismatch
let g1 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_1.clone(),
);
let g2 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_deref_instr.clone()],
addr_2.clone(),
);
let g1 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_deref_instr], addr_2.clone());
assert!(common::hash(&g1) != common::hash(&g2));

// Different instructions, same address - custom hash mismatch
let g1 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_1.clone(),
);
let g2 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_deref_instr.clone()],
addr_1.clone(),
);
let g1 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_deref_instr], addr_1.clone());
assert!(common::hash(&g1) != common::hash(&g2));

// Same instructions, same address - custom hash match
let g1 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_1.clone(),
);
let g2 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_1.clone(),
);
let g1 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
assert!(common::hash(&g1) == common::hash(&g2));

// Same instructions, different address - custom hash match
let g1 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_1.clone(),
);
let g2 = xgadget::Gadget::new(
vec![pop_r15_instr.clone(), jmp_rax_instr.clone()],
addr_2.clone(),
);
let g1 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_2.clone());
assert!(common::hash(&g1) == common::hash(&g2));

// Same instructions, different decode addresses - custom hash match
Expand All @@ -69,14 +45,14 @@ fn test_gadget_hasher() {
let jmp_rax_instr_5 = common::decode_single_x64_instr(decode_addr_5, &jmp_rax);
let jmp_rax_instr_10 = common::decode_single_x64_instr(decode_addr_10, &jmp_rax);

let g1 = xgadget::Gadget::new(vec![jmp_rax_instr_5.clone()], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![jmp_rax_instr_10.clone()], addr_1.clone());
let g3 = xgadget::Gadget::new(vec![jmp_rax_instr_10.clone()], addr_2);
let g1 = xgadget::Gadget::new(vec![jmp_rax_instr_5], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![jmp_rax_instr_10], addr_1.clone());
let g3 = xgadget::Gadget::new(vec![jmp_rax_instr_10], addr_2);
assert!(common::hash(&g1) == common::hash(&g2));
assert!(common::hash(&g2) == common::hash(&g3));

// Hash set intersection
let g1 = xgadget::Gadget::new(vec![pop_r15_instr.clone(), jmp_rax_instr], addr_1.clone());
let g1 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_instr], addr_1.clone());
let g2 = xgadget::Gadget::new(vec![pop_r15_instr, jmp_rax_deref_instr], addr_1);

let mut g_set_1: HashSet<_> = HashSet::default();
Expand Down

0 comments on commit ce8d918

Please sign in to comment.