1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use core::{marker::PhantomData, mem::drop};

use crate::{
    corpus::Corpus,
    executors::{Executor, HasExecHooks, HasExecHooksTuple, HasObservers, HasObserversHooks},
    inputs::Input,
    mark_feature_time,
    observers::ObserversTuple,
    stages::Stage,
    start_timer,
    state::{HasClientPerfStats, HasCorpus, HasExecutions},
    Error,
};

#[cfg(feature = "introspection")]
use crate::stats::PerfFeature;

/// The default mutational stage
#[derive(Clone, Debug)]
pub struct TracingStage<C, EM, I, OT, S, TE, Z>
where
    I: Input,
    C: Corpus<I>,
    TE: Executor<I>
        + HasObservers<OT>
        + HasExecHooks<EM, I, S, Z>
        + HasObserversHooks<EM, I, OT, S, Z>,
    OT: ObserversTuple + HasExecHooksTuple<EM, I, S, Z>,
    S: HasClientPerfStats + HasExecutions + HasCorpus<C, I>,
{
    tracer_executor: TE,
    #[allow(clippy::type_complexity)]
    phantom: PhantomData<(C, EM, I, OT, S, TE, Z)>,
}

impl<E, C, EM, I, OT, S, TE, Z> Stage<E, EM, S, Z> for TracingStage<C, EM, I, OT, S, TE, Z>
where
    I: Input,
    C: Corpus<I>,
    TE: Executor<I>
        + HasObservers<OT>
        + HasExecHooks<EM, I, S, Z>
        + HasObserversHooks<EM, I, OT, S, Z>,
    OT: ObserversTuple + HasExecHooksTuple<EM, I, S, Z>,
    S: HasClientPerfStats + HasExecutions + HasCorpus<C, I>,
{
    #[inline]
    fn perform(
        &mut self,
        fuzzer: &mut Z,
        _executor: &mut E,
        state: &mut S,
        manager: &mut EM,
        corpus_idx: usize,
    ) -> Result<(), Error> {
        start_timer!(state);
        let input = state
            .corpus()
            .get(corpus_idx)?
            .borrow_mut()
            .load_input()?
            .clone();
        mark_feature_time!(state, PerfFeature::GetInputFromCorpus);

        start_timer!(state);
        self.tracer_executor
            .pre_exec_observers(fuzzer, state, manager, &input)?;
        mark_feature_time!(state, PerfFeature::PreExecObservers);

        start_timer!(state);
        self.tracer_executor
            .pre_exec(fuzzer, state, manager, &input)?;
        mark_feature_time!(state, PerfFeature::PreExec);

        start_timer!(state);
        drop(self.tracer_executor.run_target(&input)?);
        mark_feature_time!(state, PerfFeature::TargetExecution);

        start_timer!(state);
        self.tracer_executor
            .post_exec(fuzzer, state, manager, &input)?;
        mark_feature_time!(state, PerfFeature::PostExec);

        *state.executions_mut() += 1;

        start_timer!(state);
        self.tracer_executor
            .post_exec_observers(fuzzer, state, manager, &input)?;
        mark_feature_time!(state, PerfFeature::PostExecObservers);

        Ok(())
    }
}

impl<C, EM, I, OT, S, TE, Z> TracingStage<C, EM, I, OT, S, TE, Z>
where
    I: Input,
    C: Corpus<I>,
    TE: Executor<I>
        + HasObservers<OT>
        + HasExecHooks<EM, I, S, Z>
        + HasObserversHooks<EM, I, OT, S, Z>,
    OT: ObserversTuple + HasExecHooksTuple<EM, I, S, Z>,
    S: HasClientPerfStats + HasExecutions + HasCorpus<C, I>,
{
    /// Creates a new default mutational stage
    pub fn new(tracer_executor: TE) -> Self {
        Self {
            tracer_executor,
            phantom: PhantomData,
        }
    }
}