Dongjia "toka" Zhang 379e2ae89b
Scalability introspector + State refactor (#1674)
* check

* clippy fmt fixing all the stuff

* restore Cargo.toml

* a

* ci

* ci

* a

* a

* workging?

* work

* ?

* why it worksgit add -u

* ci

* ci

* TMATE

* ci

* ci

* ci

* remove tmate

* less

* fuck; let's try with introspection first

* fucking macro

* another windows shit

* stop it

* i'm harassed by how shit windows is

* fixing

* ci

* ziopera

* fix from main

* ci

* ci
2023-11-21 14:38:48 +01:00

83 lines
1.9 KiB
Rust

use std::{
collections::{BTreeSet, HashMap},
marker::PhantomData,
};
use libafl::{
corpus::{Corpus, CorpusId, Testcase},
feedbacks::MapNoveltiesMetadata,
inputs::UsesInput,
schedulers::{RemovableScheduler, Scheduler},
state::{HasCorpus, HasMetadata, UsesState, State},
Error,
};
#[derive(Clone, Debug)]
pub struct MergeScheduler<S> {
mapping: HashMap<usize, CorpusId>,
all: BTreeSet<CorpusId>,
phantom: PhantomData<S>,
}
impl<S> UsesState for MergeScheduler<S>
where
S: State,
{
type State = S;
}
impl<S> RemovableScheduler for MergeScheduler<S>
where
S: State + HasCorpus,
{
fn on_remove(
&mut self,
_state: &mut Self::State,
idx: CorpusId,
_testcase: &Option<Testcase<<Self::State as UsesInput>::Input>>,
) -> Result<(), Error> {
self.all.remove(&idx);
Ok(())
}
}
impl<S> Scheduler for MergeScheduler<S>
where
S: State + HasCorpus,
{
fn on_add(&mut self, state: &mut Self::State, idx: CorpusId) -> Result<(), Error> {
self.all.insert(idx);
let testcase = state.corpus().get(idx)?.borrow();
let meta = testcase.metadata::<MapNoveltiesMetadata>()?;
for cov_idx in &meta.list {
self.mapping.insert(*cov_idx, idx);
}
Ok(())
}
fn next(&mut self, _state: &mut Self::State) -> Result<CorpusId, Error> {
unimplemented!("Not suitable for actual scheduling.");
}
}
impl<S> MergeScheduler<S> {
pub fn new() -> Self {
Self {
mapping: HashMap::default(),
all: BTreeSet::default(),
phantom: PhantomData,
}
}
pub fn removable(&self) -> BTreeSet<CorpusId> {
self.all
.difference(&self.mapping.values().copied().collect())
.copied()
.collect()
}
pub fn current(&self) -> &BTreeSet<CorpusId> {
&self.all
}
}