Skip to content

Commit 7543a54

Browse files
Add dynamic frida runtime list called FridaRuntimeVec (AFLplusplus#2799)
Co-authored-by: Dominik Maier <domenukk@gmail.com>
1 parent 187e06c commit 7543a54

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

libafl_frida/src/helper.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::fmt::{self, Debug, Formatter};
22
use std::{
3+
any::TypeId,
34
cell::{Ref, RefCell, RefMut},
45
ffi::CStr,
56
fs::{self, read_to_string},
@@ -34,7 +35,7 @@ use crate::cmplog_rt::CmpLogRuntime;
3435
use crate::{asan::asan_rt::AsanRuntime, coverage_rt::CoverageRuntime, drcov_rt::DrCovRuntime};
3536

3637
/// The Runtime trait
37-
pub trait FridaRuntime: 'static + Debug {
38+
pub trait FridaRuntime: 'static + Debug + std::any::Any {
3839
/// Initialization
3940
fn init(
4041
&mut self,
@@ -193,6 +194,67 @@ where
193194
}
194195
}
195196

197+
/// Vector of `FridaRuntime`
198+
#[derive(Debug)]
199+
pub struct FridaRuntimeVec(pub Vec<Box<dyn FridaRuntime>>);
200+
201+
impl MatchFirstType for FridaRuntimeVec {
202+
fn match_first_type<T: 'static>(&self) -> Option<&T> {
203+
for member in &self.0 {
204+
if TypeId::of::<T>() == member.type_id() {
205+
let raw = std::ptr::from_ref::<dyn FridaRuntime>(&**member) as *const T;
206+
return unsafe { raw.as_ref() };
207+
}
208+
}
209+
210+
None
211+
}
212+
213+
fn match_first_type_mut<T: 'static>(&mut self) -> Option<&mut T> {
214+
for member in &mut self.0 {
215+
if TypeId::of::<T>() == member.type_id() {
216+
let raw = std::ptr::from_mut::<dyn FridaRuntime>(&mut **member) as *mut T;
217+
return unsafe { raw.as_mut() };
218+
}
219+
}
220+
221+
None
222+
}
223+
}
224+
225+
impl FridaRuntimeTuple for FridaRuntimeVec {
226+
fn init_all(
227+
&mut self,
228+
gum: &Gum,
229+
ranges: &RangeMap<u64, (u16, String)>,
230+
module_map: &Rc<ModuleMap>,
231+
) {
232+
for runtime in &mut self.0 {
233+
runtime.init(gum, ranges, module_map);
234+
}
235+
}
236+
237+
fn deinit_all(&mut self, gum: &Gum) {
238+
for runtime in &mut self.0 {
239+
runtime.deinit(gum);
240+
}
241+
}
242+
243+
fn pre_exec_all(&mut self, input_bytes: &[u8]) -> Result<(), Error> {
244+
for runtime in &mut self.0 {
245+
runtime.pre_exec(input_bytes)?;
246+
}
247+
Ok(())
248+
}
249+
250+
fn post_exec_all(&mut self, input_bytes: &[u8]) -> Result<(), Error> {
251+
for runtime in &mut self.0 {
252+
runtime.post_exec(input_bytes)?;
253+
}
254+
Ok(())
255+
}
256+
}
257+
196258
/// Represents a range to be skipped for instrumentation
197259
#[derive(Debug, Clone, PartialEq, Eq)]
198260
pub enum SkipRange {

0 commit comments

Comments
 (0)