Skip to content

Commit

Permalink
make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
expy committed Dec 31, 2023
1 parent 09f65a2 commit 009f114
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
48 changes: 21 additions & 27 deletions libafl_frida/src/cmplog_rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use libafl::{
};
use libafl_targets::{self, CMPLOG_MAP_W};
use rangemap::RangeMap;
use std::collections::HashMap;

use crate::helper::FridaRuntime;
extern "C" {
Expand All @@ -36,7 +37,10 @@ use frida_gum_sys::Insn;
use crate::utils::{disas_count, writer_register};

#[cfg(all(feature = "cmplog", target_arch = "x86_64"))]
use iced_x86;
use iced_x86::{
BlockEncoder, Code, DecoderOptions, Instruction, InstructionBlock, MemoryOperand, MemorySize,
OpKind, Register,
};

#[cfg(all(feature = "cmplog", target_arch = "aarch64"))]
/// Speciial `CmpLog` Cases for `aarch64`
Expand Down Expand Up @@ -91,17 +95,11 @@ pub enum CmplogOperandType {
#[cfg(all(feature = "cmplog", target_arch = "x86_64"))]
pub enum CmplogOperandType {
/// A Register
Reg(iced_x86::Register),
Reg(Register),
/// An immediate value
Imm(u64),
/// A memory operand
Mem(
iced_x86::Register,
iced_x86::Register,
u64,
u32,
iced_x86::MemorySize,
), // base, index, disp, scale, mem_size
Mem(Register, Register, i64, u32, MemorySize), // base, index, disp, scale, mem_size
}

/// `Frida`-based binary-only innstrumentation that logs compares to the fuzzer
Expand Down Expand Up @@ -419,17 +417,16 @@ impl CmpLogRuntime {
output: &StalkerOutput,
op1: &CmplogOperandType, //first operand of the comparsion
op2: &CmplogOperandType, //second operand of the comparsion
_shift: Option<SpecialCmpLogCase>,
_special_case: Option<SpecialCmpLogCase>,
_shift: &Option<SpecialCmpLogCase>,
_special_case: &Option<SpecialCmpLogCase>,
) {
let writer = output.writer();

writer.put_bytes(&*self.save_registers.clone().unwrap());
writer.put_bytes(&self.save_registers.clone().unwrap());

// let int3 = [0xcc];
// writer.put_bytes(&int3);

use iced_x86::*;
let mut insts = vec![];
// self ptr is not used so far
let mut size_op = 0;
Expand All @@ -438,7 +435,6 @@ impl CmpLogRuntime {
let arg_reg_2;
let arg_reg_3;
let arg_reg_4;
use std::collections::HashMap;
let mut tmp_reg = HashMap::new();
tmp_reg.insert(8, Register::RAX);
tmp_reg.insert(4, Register::EAX);
Expand Down Expand Up @@ -468,8 +464,8 @@ impl CmpLogRuntime {
// we put the operand value into rax and than push it on stack, so the
// only clobbered register is rax, and if actual operand value uses it,
// we simply restore it from stack
let mut op_num = 0;
for op in &[op1, op2] {
for (op_num, op) in [op1, op2].iter().enumerate() {
let op_num: i64 = op_num.try_into().unwrap();
match op {
CmplogOperandType::Reg(reg) => {
let info = reg.info();
Expand Down Expand Up @@ -510,14 +506,14 @@ impl CmpLogRuntime {
}
}
set_size(size);
let mut disp_adjusted = *disp as i64;
let mut disp_adjusted = *disp;
if *reg_base == Register::RSP {
disp_adjusted += 0x38 + 8 * op_num;
disp_adjusted = disp_adjusted + 0x38 + 8_i64 * op_num;
}
insts.push(
Instruction::with2(
inst,
tmp_reg.get(&size).unwrap().clone(),
*tmp_reg.get(&size).unwrap(),
MemoryOperand::with_base_index_scale_displ_size(
*reg_base,
*reg_index,
Expand Down Expand Up @@ -556,7 +552,6 @@ impl CmpLogRuntime {
);
}
}
op_num += 1;
}

insts.push(Instruction::with2(Code::Mov_r8_imm8, arg_reg_1, size_op as u64).unwrap());
Expand All @@ -566,9 +561,9 @@ impl CmpLogRuntime {
let block = InstructionBlock::new(&insts, 0);
let block = BlockEncoder::encode(64, block, DecoderOptions::NONE).unwrap();
writer.put_bytes(block.code_buffer.as_slice());
writer.put_call_address(CmpLogRuntime::populate_lists as u64);
writer.put_call_address((CmpLogRuntime::populate_lists as usize).try_into().unwrap());

writer.put_bytes(&*self.restore_registers.clone().unwrap());
writer.put_bytes(&self.restore_registers.clone().unwrap());
}

#[cfg(all(feature = "cmplog", target_arch = "aarch64"))]
Expand All @@ -580,8 +575,8 @@ impl CmpLogRuntime {
output: &StalkerOutput,
op1: &CmplogOperandType, //first operand of the comparsion
op2: &CmplogOperandType, //second operand of the comparsion
_shift: Option<(ShiftStyle, u8)>,
special_case: Option<SpecialCmpLogCase>,
_shift: &Option<(ShiftStyle, u8)>,
special_case: &Option<SpecialCmpLogCase>,
) {
let writer = output.writer();

Expand Down Expand Up @@ -687,7 +682,6 @@ impl CmpLogRuntime {
return None;
}

use iced_x86::OpKind;
let op1 = match instruction.op0_kind() {
OpKind::Register => CmplogOperandType::Reg(instruction.op0_register()),
OpKind::Immediate16
Expand All @@ -697,7 +691,7 @@ impl CmpLogRuntime {
OpKind::Memory => CmplogOperandType::Mem(
instruction.memory_base(),
instruction.memory_index(),
instruction.memory_displacement64(),
instruction.memory_displacement64().try_into().unwrap(),
instruction.memory_index_scale(),
instruction.memory_size(),
),
Expand All @@ -715,7 +709,7 @@ impl CmpLogRuntime {
OpKind::Memory => CmplogOperandType::Mem(
instruction.memory_base(),
instruction.memory_index(),
instruction.memory_displacement64(),
instruction.memory_displacement64().try_into().unwrap(),
instruction.memory_index_scale(),
instruction.memory_size(),
),
Expand Down
9 changes: 6 additions & 3 deletions libafl_frida/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ use yaxpeax_arm::armv8::a64::{ARMv8, InstDecoder};
#[cfg(target_arch = "x86_64")]
use yaxpeax_x86::amd64::InstDecoder;

#[cfg(feature = "cmplog")]
use crate::cmplog_rt::CmpLogRuntime;
use crate::coverage_rt::CoverageRuntime;
#[cfg(unix)]
use crate::asan::asan_rt::AsanRuntime;
#[cfg(all(feature = "cmplog"))]
Expand Down Expand Up @@ -534,11 +537,11 @@ where
//emit code that saves the relevant data in runtime(passes it to x0, x1)
rt.emit_comparison_handling(
address,
&output,
output,
&op1,
&op2,
shift,
special_case,
&shift,
&special_case,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions libafl_frida/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn writer_register(reg: u16, sizecode: SizeCode, zr: bool) -> Aarch64Registe
}

/// Translate from `RegSpec` to `X86Register`
#[cfg(all(target_arch = "x86_64"))]
#[cfg(target_arch = "x86_64")]
const X86_64_REGS: [(RegSpec, X86Register); 34] = [
(RegSpec::eax(), X86Register::Eax),
(RegSpec::ecx(), X86Register::Ecx),
Expand Down Expand Up @@ -162,7 +162,7 @@ const X86_64_REGS: [(RegSpec, X86Register); 34] = [
/// The writer registers
/// frida registers: <https://docs.rs/frida-gum/0.4.0/frida_gum/instruction_writer/enum.X86Register.html>
/// capstone registers: <https://docs.rs/capstone-sys/0.14.0/capstone_sys/x86_reg/index.html>
#[cfg(all(target_arch = "x86_64"))]
#[cfg(target_arch = "x86_64")]
#[must_use]
#[inline]
#[allow(clippy::unused_self)]
Expand Down

0 comments on commit 009f114

Please sign in to comment.