Skip to content

Commit

Permalink
Add dynamic frida runtime list called FridaRuntimeVec (AFLplusplus#…
Browse files Browse the repository at this point in the history
…2799)

Co-authored-by: Dominik Maier <[email protected]>
  • Loading branch information
jejuisland87654 and domenukk authored Jan 2, 2025
1 parent 187e06c commit 7543a54
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion libafl_frida/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt::{self, Debug, Formatter};
use std::{
any::TypeId,
cell::{Ref, RefCell, RefMut},
ffi::CStr,
fs::{self, read_to_string},
Expand Down Expand Up @@ -34,7 +35,7 @@ use crate::cmplog_rt::CmpLogRuntime;
use crate::{asan::asan_rt::AsanRuntime, coverage_rt::CoverageRuntime, drcov_rt::DrCovRuntime};

/// The Runtime trait
pub trait FridaRuntime: 'static + Debug {
pub trait FridaRuntime: 'static + Debug + std::any::Any {
/// Initialization
fn init(
&mut self,
Expand Down Expand Up @@ -193,6 +194,67 @@ where
}
}

/// Vector of `FridaRuntime`
#[derive(Debug)]
pub struct FridaRuntimeVec(pub Vec<Box<dyn FridaRuntime>>);

impl MatchFirstType for FridaRuntimeVec {
fn match_first_type<T: 'static>(&self) -> Option<&T> {
for member in &self.0 {
if TypeId::of::<T>() == member.type_id() {
let raw = std::ptr::from_ref::<dyn FridaRuntime>(&**member) as *const T;
return unsafe { raw.as_ref() };
}
}

None
}

fn match_first_type_mut<T: 'static>(&mut self) -> Option<&mut T> {
for member in &mut self.0 {
if TypeId::of::<T>() == member.type_id() {
let raw = std::ptr::from_mut::<dyn FridaRuntime>(&mut **member) as *mut T;
return unsafe { raw.as_mut() };
}
}

None
}
}

impl FridaRuntimeTuple for FridaRuntimeVec {
fn init_all(
&mut self,
gum: &Gum,
ranges: &RangeMap<u64, (u16, String)>,
module_map: &Rc<ModuleMap>,
) {
for runtime in &mut self.0 {
runtime.init(gum, ranges, module_map);
}
}

fn deinit_all(&mut self, gum: &Gum) {
for runtime in &mut self.0 {
runtime.deinit(gum);
}
}

fn pre_exec_all(&mut self, input_bytes: &[u8]) -> Result<(), Error> {
for runtime in &mut self.0 {
runtime.pre_exec(input_bytes)?;
}
Ok(())
}

fn post_exec_all(&mut self, input_bytes: &[u8]) -> Result<(), Error> {
for runtime in &mut self.0 {
runtime.post_exec(input_bytes)?;
}
Ok(())
}
}

/// Represents a range to be skipped for instrumentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SkipRange {
Expand Down

0 comments on commit 7543a54

Please sign in to comment.