1use std::hash::{DefaultHasher, Hash, Hasher};
2
3use libafl::executors::ExitKind;
4use rand::Rng;
5
6use crate::algebra::TermType;
7use crate::error::Error;
8use crate::execution::{DifferentialRunner, Runner, TraceRunner};
9use crate::fuzzer::feedback::{FAIL_AT_STEP, OBJECTIVE_HASH, OBJECTIVE_TRIGGERED};
10use crate::fuzzer::stats_stage::{
11 HARNESS_EXEC, HARNESS_EXEC_AGENT_SUCCESS, HARNESS_EXEC_SUCCESS, NB_PAYLOAD, PAYLOAD_LENGTH,
12 TERM_SIZE, TRACE_LENGTH,
13};
14use crate::protocol::{ProtocolBehavior, ProtocolTypes};
15use crate::put::PutDescriptor;
16use crate::put_registry::PutRegistry;
17use crate::trace::{Action, ConfigTrace, Spawner, Trace};
18
19pub fn harness<PB: ProtocolBehavior + 'static>(
20 put_registry: &PutRegistry<PB>,
21 put_descriptor: &PutDescriptor,
22 input: &Trace<PB::ProtocolTypes>,
23) -> ExitKind {
24 OBJECTIVE_TRIGGERED.set(false);
25
26 HARNESS_EXEC.increment();
28 TRACE_LENGTH.update(input.steps.len());
29
30 if cfg!(feature = "introspection") {
31 NB_PAYLOAD.update(input.all_payloads().len());
32 for payload in input.all_payloads() {
33 PAYLOAD_LENGTH.update(payload.len());
34 }
35 for step in &input.steps {
36 match &step.action {
37 Action::Input(input) => {
38 TERM_SIZE.update(input.recipe.size());
39 }
40 Action::Output(_) => {}
41 }
42 }
43 }
44
45 let runner = Runner::new(
47 put_registry.clone(),
48 Spawner::new(put_registry.clone()).with_mapping(
49 &input
50 .descriptors
51 .iter()
52 .map(|d| (d.name, put_descriptor.clone()))
53 .collect::<Vec<_>>(),
54 ),
55 );
56 let mut fail_at_step = 0;
57 match runner.execute(input, &mut fail_at_step) {
58 Ok(ctx) => {
59 HARNESS_EXEC_SUCCESS.increment();
60 if cfg!(feature = "introspection") && ctx.agents_successful() {
61 HARNESS_EXEC_AGENT_SUCCESS.increment();
62 }
63 }
64 Err(Error::SecurityClaim(msg)) => {
65 log::warn!("{}", msg);
66 OBJECTIVE_TRIGGERED.set(true);
67 }
68 Err(_) => {}
69 }
70
71 log::trace!(
73 "[a:trace len={}/size={}/{fail_at_step}] [[harness] Executed until {fail_at_step}.",
74 input.steps.len(),
75 input.size(),
76 );
77 FAIL_AT_STEP.set(Some(fail_at_step));
78
79 ExitKind::Ok
80}
81
82pub fn differential_harness<PB: ProtocolBehavior + 'static>(
83 put_registry: &PutRegistry<PB>,
84 first_put: &PutDescriptor,
85 second_put: &PutDescriptor,
86 input: &Trace<PB::ProtocolTypes>,
87) -> ExitKind {
88 OBJECTIVE_TRIGGERED.set(false);
89 OBJECTIVE_HASH.set(None);
90
91 let input = <PB::ProtocolTypes as ProtocolTypes>::differential_fuzzing_uniformise_put_config(
93 input.clone(),
94 );
95
96 let first_mappings: Vec<_> = input
99 .all_descriptors()
100 .iter()
101 .map(|d| (d.name, first_put.clone()))
102 .collect();
103 let second_mappings: Vec<_> = input
104 .all_descriptors()
105 .iter()
106 .map(|d| (d.name, second_put.clone()))
107 .collect();
108
109 let runner = DifferentialRunner::new(
110 put_registry.clone(),
111 Spawner::new(put_registry.clone()).with_mapping(&first_mappings),
112 Spawner::new(put_registry.clone()).with_mapping(&second_mappings),
113 );
114
115 HARNESS_EXEC.increment();
116 TRACE_LENGTH.update(input.steps.len());
117
118 if cfg!(feature = "introspection") {
119 NB_PAYLOAD.update(input.all_payloads().len());
120 for payload in input.all_payloads() {
121 PAYLOAD_LENGTH.update(payload.len());
122 }
123 for step in &input.steps {
124 match &step.action {
125 Action::Input(input) => {
126 TERM_SIZE.update(input.recipe.size());
127 }
128 Action::Output(_) => {}
129 }
130 }
131 }
132
133 let input_len = input.steps.len();
134 let input_size = input.size();
135
136 let mut fail_at_step = 0;
138 let exec_res = runner.execute_config(
139 input,
140 ConfigTrace {
141 check_security_violation: false,
142 ..Default::default()
143 },
144 &mut fail_at_step,
145 );
146
147 log::trace!(
148 "[a:trace len={}/size={}/{fail_at_step}] [[harness] Executed until {fail_at_step}.",
149 input_len,
150 input_size,
151 );
152 FAIL_AT_STEP.set(Some(fail_at_step));
153
154 match exec_res {
155 Ok(ctx) => {
156 HARNESS_EXEC_SUCCESS.increment();
157 if cfg!(feature = "introspection") {
158 if ctx.0.agents_successful() && ctx.1.agents_successful() {
159 HARNESS_EXEC_AGENT_SUCCESS.increment();
160 }
161 }
162 }
163 Err(err) => match &err {
164 Error::SecurityClaim(msg) => {
165 log::warn!("{}", msg);
166 OBJECTIVE_TRIGGERED.set(true);
167 }
168 Error::Difference {
169 differences: diffs,
170 put1_status: s1,
171 put2_status: s2,
172 } => {
173 log::warn!(
174 "{}",
175 diffs
176 .iter()
177 .map(|x| x.to_string())
178 .collect::<Vec<String>>()
179 .join("\n")
180 );
181 let mut h = DefaultHasher::new();
182 diffs.hash(&mut h);
183 s1.hash(&mut h);
184 s2.hash(&mut h);
185
186 OBJECTIVE_HASH.set(Some(h.finish()));
187
188 OBJECTIVE_TRIGGERED.set(true);
189 }
190 _ => (),
191 },
192 }
193
194 ExitKind::Ok
195}
196
197#[allow(unused)]
198#[must_use]
199pub fn dummy_harness<PB: ProtocolBehavior + 'static>(
200 _input: &Trace<PB::ProtocolTypes>,
201) -> ExitKind {
202 let mut rng = rand::thread_rng();
203
204 let n1 = rng.gen_range(0..10);
205 log::info!("Run {}", n1);
206 if n1 <= 5 {
207 return ExitKind::Timeout;
208 }
209 ExitKind::Ok }