Skip to content

Commit d782585

Browse files
Fix SimpleRestartingMonitor after restart (#1669)
* Fix SimpleRestartingMonitor after restart * a * a * a * a * a * a * a * a * a * ci --------- Co-authored-by: toka <[email protected]>
1 parent 0750a6c commit d782585

File tree

7 files changed

+82
-28
lines changed

7 files changed

+82
-28
lines changed

libafl/src/events/simple.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use alloc::{
99
use core::ffi::c_void;
1010
#[cfg(all(unix, feature = "std"))]
1111
use core::ptr::write_volatile;
12-
#[cfg(feature = "std")]
13-
use core::sync::atomic::{compiler_fence, Ordering};
1412
use core::{fmt::Debug, marker::PhantomData};
13+
#[cfg(feature = "std")]
14+
use core::{
15+
sync::atomic::{compiler_fence, Ordering},
16+
time::Duration,
17+
};
1518

1619
#[cfg(all(feature = "std", any(windows, not(feature = "fork"))))]
1720
use libafl_bolts::os::startable_self;
@@ -28,12 +31,6 @@ use serde::{de::DeserializeOwned, Serialize};
2831
use super::{CustomBufEventResult, CustomBufHandlerFn, HasCustomBufHandlers, ProgressReporter};
2932
#[cfg(all(unix, feature = "std"))]
3033
use crate::events::{shutdown_handler, SHUTDOWN_SIGHANDLER_DATA};
31-
#[cfg(feature = "std")]
32-
use crate::{
33-
corpus::Corpus,
34-
monitors::SimplePrintingMonitor,
35-
state::{HasCorpus, HasSolutions},
36-
};
3734
use crate::{
3835
events::{
3936
BrokerEventResult, Event, EventFirer, EventManager, EventManagerId, EventProcessor,
@@ -44,6 +41,11 @@ use crate::{
4441
state::{HasClientPerfMonitor, HasExecutions, HasLastReportTime, HasMetadata, UsesState},
4542
Error,
4643
};
44+
#[cfg(feature = "std")]
45+
use crate::{
46+
monitors::{ClientStats, SimplePrintingMonitor},
47+
state::{HasCorpus, HasSolutions},
48+
};
4749

4850
/// The llmp connection from the actual fuzzer to the process supervising it
4951
const _ENV_FUZZER_SENDER: &str = "_AFL_ENV_FUZZER_SENDER";
@@ -343,14 +345,19 @@ where
343345
#[cfg(feature = "std")]
344346
impl<MT, S, SP> EventRestarter for SimpleRestartingEventManager<MT, S, SP>
345347
where
348+
MT: Monitor,
346349
S: UsesInput + Serialize,
347350
SP: ShMemProvider,
348351
{
349352
/// Reset the single page (we reuse it over and over from pos 0), then send the current state to the next runner.
350353
fn on_restart(&mut self, state: &mut S) -> Result<(), Error> {
351354
// First, reset the page to 0 so the next iteration can read read from the beginning of this page
352355
self.staterestorer.reset();
353-
self.staterestorer.save(state)
356+
self.staterestorer.save(&(
357+
state,
358+
self.simple_event_mgr.monitor.start_time(),
359+
self.simple_event_mgr.monitor.client_stats(),
360+
))
354361
}
355362

356363
fn send_exiting(&mut self) -> Result<(), Error> {
@@ -539,7 +546,7 @@ where
539546
};
540547

541548
// If we're restarting, deserialize the old state.
542-
let (state, mgr) = match staterestorer.restore::<S>()? {
549+
let (state, mgr) = match staterestorer.restore::<(S, Duration, Vec<ClientStats>)>()? {
543550
None => {
544551
log::info!("First run. Let's set it all up");
545552
// Mgr to send and receive msgs from/to all other fuzzer instances
@@ -549,15 +556,14 @@ where
549556
)
550557
}
551558
// Restoring from a previous run, deserialize state and corpus.
552-
Some(state) => {
559+
Some((state, start_time, clients_stats)) => {
553560
log::info!("Subsequent run. Loaded previous state.");
554561
// We reset the staterestorer, the next staterestorer and receiver (after crash) will reuse the page from the initial message.
555562
staterestorer.reset();
556563

557-
// load the corpus size into monitor to still display the correct numbers after restart.
558-
let client_stats = monitor.client_stats_mut_for(ClientId(0));
559-
client_stats.update_corpus_size(state.corpus().count().try_into()?);
560-
client_stats.update_objective_size(state.solutions().count().try_into()?);
564+
// reload the state of the monitor to display the correct stats after restarts
565+
monitor.set_start_time(start_time);
566+
*monitor.client_stats_mut() = clients_stats;
561567

562568
(
563569
Some(state),

libafl/src/monitors/disk.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ where
3939
}
4040

4141
/// Time this fuzzing run stated
42-
fn start_time(&mut self) -> Duration {
42+
fn start_time(&self) -> Duration {
4343
self.base.start_time()
4444
}
4545

46+
/// Set creation time
47+
fn set_start_time(&mut self, time: Duration) {
48+
self.base.set_start_time(time);
49+
}
50+
4651
fn display(&mut self, event_msg: String, sender_id: ClientId) {
4752
let cur_time = current_time();
4853

@@ -184,10 +189,14 @@ where
184189
self.base.client_stats()
185190
}
186191

187-
fn start_time(&mut self) -> Duration {
192+
fn start_time(&self) -> Duration {
188193
self.base.start_time()
189194
}
190195

196+
fn set_start_time(&mut self, time: Duration) {
197+
self.base.set_start_time(time);
198+
}
199+
191200
fn display(&mut self, event_msg: String, sender_id: ClientId) {
192201
if (self.log_record)(&mut self.base) {
193202
let file = OpenOptions::new()

libafl/src/monitors/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn prettify_float(value: f64) -> String {
8282
}
8383

8484
/// A simple struct to keep track of client monitor
85-
#[derive(Debug, Clone, Default, Serialize)]
85+
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8686
pub struct ClientStats {
8787
// monitor (maybe we need a separated struct?)
8888
/// The corpus size for this client
@@ -235,7 +235,10 @@ pub trait Monitor {
235235
fn client_stats(&self) -> &[ClientStats];
236236

237237
/// Creation time
238-
fn start_time(&mut self) -> Duration;
238+
fn start_time(&self) -> Duration;
239+
240+
/// Set creation time
241+
fn set_start_time(&mut self, time: Duration);
239242

240243
/// Show the monitor to the user
241244
fn display(&mut self, event_msg: String, sender_id: ClientId);
@@ -311,10 +314,15 @@ impl Monitor for NopMonitor {
311314
}
312315

313316
/// Time this fuzzing run stated
314-
fn start_time(&mut self) -> Duration {
317+
fn start_time(&self) -> Duration {
315318
self.start_time
316319
}
317320

321+
/// Time this fuzzing run stated
322+
fn set_start_time(&mut self, time: Duration) {
323+
self.start_time = time;
324+
}
325+
318326
fn display(&mut self, _event_msg: String, _sender_id: ClientId) {}
319327
}
320328

@@ -375,10 +383,15 @@ impl Monitor for SimplePrintingMonitor {
375383
}
376384

377385
/// Time this fuzzing run stated
378-
fn start_time(&mut self) -> Duration {
386+
fn start_time(&self) -> Duration {
379387
self.start_time
380388
}
381389

390+
/// Time this fuzzing run stated
391+
fn set_start_time(&mut self, time: Duration) {
392+
self.start_time = time;
393+
}
394+
382395
fn display(&mut self, event_msg: String, sender_id: ClientId) {
383396
let mut userstats = self.client_stats()[sender_id.0 as usize]
384397
.user_monitor
@@ -452,10 +465,15 @@ where
452465
}
453466

454467
/// Time this fuzzing run stated
455-
fn start_time(&mut self) -> Duration {
468+
fn start_time(&self) -> Duration {
456469
self.start_time
457470
}
458471

472+
/// Set creation time
473+
fn set_start_time(&mut self, time: Duration) {
474+
self.start_time = time;
475+
}
476+
459477
fn display(&mut self, event_msg: String, sender_id: ClientId) {
460478
let mut fmt = format!(
461479
"[{} #{}] run time: {}, clients: {}, corpus: {}, objectives: {}, executions: {}, exec/sec: {}",
@@ -1113,8 +1131,13 @@ pub mod pybind {
11131131
}
11141132

11151133
/// Time this fuzzing run stated
1116-
fn start_time(&mut self) -> Duration {
1117-
unwrap_me_mut!(self.wrapper, m, { m.start_time() })
1134+
fn start_time(&self) -> Duration {
1135+
unwrap_me!(self.wrapper, m, { m.start_time() })
1136+
}
1137+
1138+
/// set start time
1139+
fn set_start_time(&mut self, time: Duration) {
1140+
unwrap_me_mut!(self.wrapper, m, { m.set_start_time(time) });
11181141
}
11191142

11201143
fn display(&mut self, event_msg: String, sender_id: ClientId) {

libafl/src/monitors/multi.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ where
4949
&self.client_stats
5050
}
5151

52+
/// Set creation time
53+
fn set_start_time(&mut self, time: Duration) {
54+
self.start_time = time;
55+
}
56+
5257
/// Time this fuzzing run stated
53-
fn start_time(&mut self) -> Duration {
58+
fn start_time(&self) -> Duration {
5459
self.start_time
5560
}
5661

libafl/src/monitors/prometheus.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ where
8888
}
8989

9090
/// Time this fuzzing run stated
91-
fn start_time(&mut self) -> Duration {
91+
fn start_time(&self) -> Duration {
9292
self.start_time
9393
}
9494

95+
/// Set creation time
96+
fn set_start_time(&mut self, time: Duration) {
97+
self.start_time = time;
98+
}
99+
95100
#[allow(clippy::cast_sign_loss)]
96101
fn display(&mut self, event_msg: String, sender_id: ClientId) {
97102
// Update the prometheus metrics

libafl/src/monitors/tui/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,15 @@ impl Monitor for TuiMonitor {
346346
}
347347

348348
/// Time this fuzzing run stated
349-
fn start_time(&mut self) -> Duration {
349+
fn start_time(&self) -> Duration {
350350
self.start_time
351351
}
352352

353+
/// Set creation time
354+
fn set_start_time(&mut self, time: Duration) {
355+
self.start_time = time;
356+
}
357+
353358
#[allow(clippy::cast_sign_loss)]
354359
fn display(&mut self, event_msg: String, sender_id: ClientId) {
355360
let cur_time = current_time();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
/// oom observer
12
#[cfg(feature = "libfuzzer_oom")]
2-
mod oom;
3+
pub mod oom;
34
#[cfg(feature = "libfuzzer_oom")]
45
pub use oom::*;

0 commit comments

Comments
 (0)