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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! The [`Launcher`] launches multiple fuzzer instances in parallel.
//! Thanks to it, we won't need a `for` loop in a shell script...
//!
//! It will hide child output, unless the settings indicate otherwise, or the `LIBAFL_DEBUG_OUTPUT` env variable is set.
//!
//! To use multiple [`Launcher`]`s` for individual configurations,
//! we can set `spawn_broker` to `false` on all but one.
//!
//! To connect multiple nodes together via TCP, we can use the `remote_broker_addr`.
//! (this requires the `llmp_bind_public` compile-time feature for `LibAFL`).
//!
//! On `Unix` systems, the [`Launcher`] will use `fork` if the `fork` feature is used for `LibAFL`.
//! Else, it will start subsequent nodes with the same commandline, and will set special `env` variables accordingly.
use alloc::string::String;
use core::{
fmt::{self, Debug, Formatter},
net::SocketAddr,
num::NonZeroUsize,
time::Duration,
};
use libafl_bolts::{
core_affinity::{CoreId, Cores},
shmem::ShMemProvider,
tuples::tuple_list,
};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use typed_builder::TypedBuilder;
#[cfg(all(unix, feature = "fork"))]
use {
crate::{
events::{CentralizedLlmpHook, StdLlmpEventHook, centralized::CentralizedEventManager},
inputs::Input,
},
alloc::boxed::Box,
alloc::string::ToString,
libafl_bolts::{
core_affinity::get_core_ids,
llmp::{Broker, Brokers, LlmpBroker},
os::{ForkResult, fork},
},
};
#[cfg(unix)]
use {
libafl_bolts::os::dup2,
std::{fs::File, os::unix::io::AsRawFd},
};
#[cfg(any(windows, not(feature = "fork")))]
use {libafl_bolts::os::startable_self, std::process::Stdio};
#[cfg(all(unix, feature = "fork", feature = "multi_machine"))]
use crate::events::multi_machine::{NodeDescriptor, TcpMultiMachineHooks};
use crate::{
Error,
events::{
EventConfig, EventManagerHooksTuple,
llmp::{LlmpRestartingEventManager, LlmpShouldSaveState, ManagerKind, RestartingMgr},
},
monitors::Monitor,
};
/// The (internal) `env` that indicates we're running as client.
const _AFL_LAUNCHER_CLIENT: &str = "AFL_LAUNCHER_CLIENT";
/// The env variable to set in order to enable child output
#[cfg(all(feature = "fork", unix))]
const LIBAFL_DEBUG_OUTPUT: &str = "LIBAFL_DEBUG_OUTPUT";
/// Information about this client from the launcher
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientDescription {
id: usize,
overcommit_id: usize,
core_id: CoreId,
}
impl ClientDescription {
/// Create a [`ClientDescription`]
#[must_use]
pub fn new(id: usize, overcommit_id: usize, core_id: CoreId) -> Self {
Self {
id,
overcommit_id,
core_id,
}
}
/// Id unique to all clients spawned by this launcher
#[must_use]
pub fn id(&self) -> usize {
self.id
}
/// [`CoreId`] this client is bound to
#[must_use]
pub fn core_id(&self) -> CoreId {
self.core_id
}
/// Incremental id unique for all clients on the same core
#[must_use]
pub fn overcommit_id(&self) -> usize {
self.overcommit_id
}
/// Create a string representation safe for environment variables
#[must_use]
pub fn to_safe_string(&self) -> String {
format!("{}_{}_{}", self.id, self.overcommit_id, self.core_id.0)
}
/// Parse the string created by [`Self::to_safe_string`].
#[must_use]
pub fn from_safe_string(input: &str) -> Self {
let mut iter = input.split('_');
let id = iter.next().unwrap().parse().unwrap();
let overcommit_id = iter.next().unwrap().parse().unwrap();
let core_id = iter.next().unwrap().parse::<usize>().unwrap().into();
Self {
id,
overcommit_id,
core_id,
}
}
}
/// Provides a [`Launcher`], which can be used to launch a fuzzing run on a specified list of cores
///
/// Will hide child output, unless the settings indicate otherwise, or the `LIBAFL_DEBUG_OUTPUT` env variable is set.
#[derive(TypedBuilder)]
pub struct Launcher<'a, CF, MT, SP> {
/// The `ShmemProvider` to use
shmem_provider: SP,
/// The monitor instance to use
monitor: MT,
/// The configuration
configuration: EventConfig,
/// The 'main' function to run for each client forked. This probably shouldn't return
#[builder(default, setter(strip_option))]
run_client: Option<CF>,
/// The broker port to use (or to attach to, in case [`Self::spawn_broker`] is `false`)
#[builder(default = 1337_u16)]
broker_port: u16,
/// The list of cores to run on
cores: &'a Cores,
/// The number of clients to spawn on each core
#[builder(default = 1)]
overcommit: usize,
/// A file name to write all client output to
#[cfg(unix)]
#[builder(default = None)]
stdout_file: Option<&'a str>,
/// The time in milliseconds to delay between child launches
#[builder(default = 10)]
launch_delay: u64,
/// The actual, opened, `stdout_file` - so that we keep it open until the end
#[cfg(all(unix, feature = "fork"))]
#[builder(setter(skip), default = None)]
opened_stdout_file: Option<File>,
/// A file name to write all client stderr output to. If not specified, output is sent to
/// `stdout_file`.
#[cfg(unix)]
#[builder(default = None)]
stderr_file: Option<&'a str>,
/// The actual, opened, `stdout_file` - so that we keep it open until the end
#[cfg(all(unix, feature = "fork"))]
#[builder(setter(skip), default = None)]
opened_stderr_file: Option<File>,
/// The `ip:port` address of another broker to connect our new broker to for multi-machine
/// clusters.
#[builder(default = None)]
remote_broker_addr: Option<SocketAddr>,
/// If this launcher should spawn a new `broker` on `[Self::broker_port]` (default).
/// The reason you may not want this is, if you already have a [`Launcher`]
/// with a different configuration (for the same target) running on this machine.
/// Then, clients launched by this [`Launcher`] can connect to the original `broker`.
#[builder(default = true)]
spawn_broker: bool,
/// Tell the manager to serialize or not the state on restart
#[builder(default = LlmpShouldSaveState::OnRestart)]
serialize_state: LlmpShouldSaveState,
}
impl<CF, MT, SP> Debug for Launcher<'_, CF, MT, SP> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut dbg_struct = f.debug_struct("Launcher");
dbg_struct
.field("configuration", &self.configuration)
.field("broker_port", &self.broker_port)
.field("core", &self.cores)
.field("spawn_broker", &self.spawn_broker)
.field("remote_broker_addr", &self.remote_broker_addr);
#[cfg(unix)]
{
dbg_struct
.field("stdout_file", &self.stdout_file)
.field("stderr_file", &self.stderr_file);
}
dbg_struct.finish_non_exhaustive()
}
}
impl<CF, MT, SP> Launcher<'_, CF, MT, SP>
where
MT: Monitor + Clone,
{
/// Launch the broker and the clients and fuzz
#[cfg(any(windows, not(feature = "fork"), all(unix, feature = "fork")))]
pub fn launch<I, S>(&mut self) -> Result<(), Error>
where
CF: FnOnce(
Option<S>,
LlmpRestartingEventManager<(), I, S, SP::ShMem, SP>,
ClientDescription,
) -> Result<(), Error>,
I: DeserializeOwned,
S: DeserializeOwned + Serialize,
SP: ShMemProvider,
{
Self::launch_with_hooks(self, tuple_list!())
}
}
impl<CF, MT, SP> Launcher<'_, CF, MT, SP>
where
MT: Monitor + Clone,
SP: ShMemProvider,
{
/// Launch the broker and the clients and fuzz with a user-supplied hook
#[cfg(all(unix, feature = "fork"))]
pub fn launch_with_hooks<EMH, I, S>(&mut self, hooks: EMH) -> Result<(), Error>
where
S: DeserializeOwned + Serialize,
I: DeserializeOwned,
EMH: EventManagerHooksTuple<I, S> + Clone + Copy,
CF: FnOnce(
Option<S>,
LlmpRestartingEventManager<EMH, I, S, SP::ShMem, SP>,
ClientDescription,
) -> Result<(), Error>,
{
if self.cores.ids.is_empty() {
return Err(Error::illegal_argument(
"No cores to spawn on given, cannot launch anything.",
));
}
if self.run_client.is_none() {
return Err(Error::illegal_argument(
"No client callback provided".to_string(),
));
}
let core_ids = get_core_ids()?;
let mut handles = vec![];
log::info!("spawning on cores: {:?}", self.cores);
self.opened_stdout_file = self
.stdout_file
.map(|filename| File::create(filename).unwrap());
self.opened_stderr_file = self
.stderr_file
.map(|filename| File::create(filename).unwrap());
let debug_output = std::env::var(LIBAFL_DEBUG_OUTPUT).is_ok();
// Spawn clients
let mut index = 0_usize;
for bind_to in core_ids {
if self.cores.ids.contains(&bind_to) {
for overcommit_id in 0..self.overcommit {
index += 1;
self.shmem_provider.pre_fork()?;
// # Safety
// Fork is safe in general, apart from potential side effects to the OS and other threads
match unsafe { fork() }? {
ForkResult::Parent(child) => {
self.shmem_provider.post_fork(false)?;
handles.push(child.pid);
log::info!(
"child spawned with id {index} and bound to core {bind_to:?}"
);
}
ForkResult::Child => {
// # Safety
// A call to `getpid` is safe.
log::info!("{:?} PostFork", unsafe { libc::getpid() });
self.shmem_provider.post_fork(true)?;
std::thread::sleep(Duration::from_millis(
index as u64 * self.launch_delay,
));
if !debug_output {
if let Some(file) = &self.opened_stdout_file {
// # Safety
// We assume the file descriptors are valid here
unsafe {
dup2(file.as_raw_fd(), libc::STDOUT_FILENO)?;
match &self.opened_stderr_file {
Some(stderr) => {
dup2(stderr.as_raw_fd(), libc::STDERR_FILENO)?;
}
_ => {
dup2(file.as_raw_fd(), libc::STDERR_FILENO)?;
}
}
}
}
}
let client_description =
ClientDescription::new(index, overcommit_id, bind_to);
// Fuzzer client. keeps retrying the connection to broker till the broker starts
let builder = RestartingMgr::<EMH, I, MT, S, SP>::builder()
.shmem_provider(self.shmem_provider.clone())
.broker_port(self.broker_port)
.kind(ManagerKind::Client {
client_description: client_description.clone(),
})
.configuration(self.configuration)
.serialize_state(self.serialize_state)
.hooks(hooks);
let (state, mgr) = builder.build().launch()?;
return (self.run_client.take().unwrap())(
state,
mgr,
client_description,
);
}
}
}
}
}
if self.spawn_broker {
log::info!("I am broker!!.");
// TODO we don't want always a broker here, think about using different laucher process to spawn different configurations
let builder = RestartingMgr::<EMH, I, MT, S, SP>::builder()
.shmem_provider(self.shmem_provider.clone())
.monitor(Some(self.monitor.clone()))
.broker_port(self.broker_port)
.kind(ManagerKind::Broker)
.remote_broker_addr(self.remote_broker_addr)
.exit_cleanly_after(Some(NonZeroUsize::try_from(self.cores.ids.len()).unwrap()))
.configuration(self.configuration)
.serialize_state(self.serialize_state)
.hooks(hooks);
builder.build().launch()?;
// Broker exited. kill all clients.
for handle in &handles {
// # Safety
// Normal libc call, no dereferences whatsoever
unsafe {
libc::kill(*handle, libc::SIGINT);
}
}
} else {
for handle in &handles {
let mut status = 0;
log::info!(
"Not spawning broker (spawn_broker is false). Waiting for fuzzer children to exit..."
);
unsafe {
libc::waitpid(*handle, &raw mut status, 0);
if status != 0 {
log::info!("Client with pid {handle} exited with status {status}");
}
}
}
}
Ok(())
}
/// Launch the broker and the clients and fuzz
#[cfg(any(windows, not(feature = "fork")))]
#[expect(clippy::too_many_lines, clippy::match_wild_err_arm)]
pub fn launch_with_hooks<EMH, I, S>(&mut self, hooks: EMH) -> Result<(), Error>
where
CF: FnOnce(
Option<S>,
LlmpRestartingEventManager<EMH, I, S, SP::ShMem, SP>,
ClientDescription,
) -> Result<(), Error>,
EMH: EventManagerHooksTuple<I, S> + Clone + Copy,
I: DeserializeOwned,
S: DeserializeOwned + Serialize,
{
use libafl_bolts::core_affinity::get_core_ids;
let is_client = std::env::var(_AFL_LAUNCHER_CLIENT);
let mut handles = match is_client {
Ok(core_conf) => {
let client_description = ClientDescription::from_safe_string(&core_conf);
// the actual client. do the fuzzing
let builder = RestartingMgr::<EMH, I, MT, S, SP>::builder()
.shmem_provider(self.shmem_provider.clone())
.broker_port(self.broker_port)
.kind(ManagerKind::Client {
client_description: client_description.clone(),
})
.configuration(self.configuration)
.serialize_state(self.serialize_state)
.hooks(hooks);
let (state, mgr) = builder.build().launch()?;
return (self.run_client.take().unwrap())(state, mgr, client_description);
}
Err(std::env::VarError::NotPresent) => {
// I am a broker
// before going to the broker loop, spawn n clients
let core_ids = get_core_ids().unwrap();
let mut handles = vec![];
log::info!("spawning on cores: {:?}", self.cores);
let debug_output = std::env::var("LIBAFL_DEBUG_OUTPUT").is_ok();
#[cfg(unix)]
{
// Set own stdout and stderr as set by the user
if !debug_output {
let opened_stdout_file = self
.stdout_file
.map(|filename| File::create(filename).unwrap());
let opened_stderr_file = self
.stderr_file
.map(|filename| File::create(filename).unwrap());
if let Some(file) = opened_stdout_file {
// # Safety
// We assume the file descriptors are valid here
unsafe {
dup2(file.as_raw_fd(), libc::STDOUT_FILENO)?;
if let Some(stderr) = opened_stderr_file {
dup2(stderr.as_raw_fd(), libc::STDERR_FILENO)?;
} else {
dup2(file.as_raw_fd(), libc::STDERR_FILENO)?;
}
}
}
}
}
//spawn clients
let mut index = 0;
for core_id in core_ids {
if self.cores.ids.contains(&core_id) {
for overcommit_i in 0..self.overcommit {
index += 1;
// Forward own stdio to child processes, if requested by user
#[allow(unused_mut)] // mut only on certain cfgs
let (mut stdout, mut stderr) = (Stdio::null(), Stdio::null());
#[cfg(unix)]
{
if self.stdout_file.is_some() || self.stderr_file.is_some() {
stdout = Stdio::inherit();
stderr = Stdio::inherit();
}
}
std::thread::sleep(Duration::from_millis(
core_id.0 as u64 * self.launch_delay,
));
let client_description =
ClientDescription::new(index, overcommit_i, core_id);
// # Safety
// This is set only once, in here, for the child.
unsafe {
std::env::set_var(
_AFL_LAUNCHER_CLIENT,
client_description.to_safe_string(),
);
}
let mut child = startable_self()?;
let child = (if debug_output {
&mut child
} else {
child.stdout(stdout);
child.stderr(stderr)
})
.spawn()?;
handles.push(child);
}
}
}
handles
}
Err(_) => panic!("Env variables are broken, received non-unicode!"),
};
// It's fine to check this after the client spawn loop - since we won't have spawned any clients...
// Doing it later means one less check in each spawned process.
if self.cores.ids.is_empty() {
return Err(Error::illegal_argument(
"No cores to spawn on given, cannot launch anything.",
));
}
if self.spawn_broker {
log::info!("I am broker!!.");
let builder = RestartingMgr::<EMH, I, MT, S, SP>::builder()
.shmem_provider(self.shmem_provider.clone())
.monitor(Some(self.monitor.clone()))
.broker_port(self.broker_port)
.kind(ManagerKind::Broker)
.remote_broker_addr(self.remote_broker_addr)
.exit_cleanly_after(Some(NonZeroUsize::try_from(self.cores.ids.len()).unwrap()))
.configuration(self.configuration)
.serialize_state(self.serialize_state)
.hooks(hooks);
builder.build().launch()?;
//broker exited. kill all clients.
for handle in &mut handles {
handle.kill()?;
}
} else {
log::info!(
"Not spawning broker (spawn_broker is false). Waiting for fuzzer children to exit..."
);
for handle in &mut handles {
let ecode = handle.wait()?;
if !ecode.success() {
log::info!("Client with handle {handle:?} exited with {ecode:?}");
}
}
}
Ok(())
}
}
/// A Launcher that minimizes re-execution of shared testcases.
///
/// Provides a Launcher, which can be used to launch a fuzzing run on a specified list of cores with a single main and multiple secondary nodes
/// This is for centralized, the 4th argument of the closure should mean if this is the main node.
#[cfg(all(unix, feature = "fork"))]
#[derive(TypedBuilder)]
pub struct CentralizedLauncher<'a, CF, MF, MT, SP> {
/// The `ShmemProvider` to use
shmem_provider: SP,
/// The monitor instance to use
monitor: MT,
/// The configuration
configuration: EventConfig,
/// The 'main' function to run for each secondary client forked. This probably shouldn't return
#[builder(default, setter(strip_option))]
secondary_run_client: Option<CF>,
/// The 'main' function to run for the main evaluator node.
#[builder(default, setter(strip_option))]
main_run_client: Option<MF>,
/// The broker port to use (or to attach to, in case [`Self::spawn_broker`] is `false`)
#[builder(default = 1337_u16)]
broker_port: u16,
/// The centralized broker port to use (or to attach to, in case [`Self::spawn_broker`] is `false`)
#[builder(default = 1338_u16)]
centralized_broker_port: u16,
/// The time observer by which to adaptively serialize
/// The list of cores to run on
cores: &'a Cores,
/// The number of clients to spawn on each core
#[builder(default = 1)]
overcommit: usize,
/// A file name to write all client output to
#[builder(default = None)]
stdout_file: Option<&'a str>,
/// The time in milliseconds to delay between child launches
#[builder(default = 10)]
launch_delay: u64,
/// The actual, opened, `stdout_file` - so that we keep it open until the end
#[cfg(all(unix, feature = "fork"))]
#[builder(setter(skip), default = None)]
opened_stdout_file: Option<File>,
/// A file name to write all client stderr output to. If not specified, output is sent to
/// `stdout_file`.
#[builder(default = None)]
stderr_file: Option<&'a str>,
/// The actual, opened, `stdout_file` - so that we keep it open until the end
#[cfg(all(unix, feature = "fork"))]
#[builder(setter(skip), default = None)]
opened_stderr_file: Option<File>,
/// The `ip:port` address of another broker to connect our new broker to for multi-machine
/// clusters.
#[builder(default = None)]
remote_broker_addr: Option<SocketAddr>,
#[cfg(feature = "multi_machine")]
multi_machine_node_descriptor: NodeDescriptor<SocketAddr>,
/// If this launcher should spawn a new `broker` on `[Self::broker_port]` (default).
/// The reason you may not want this is, if you already have a [`Launcher`]
/// with a different configuration (for the same target) running on this machine.
/// Then, clients launched by this [`Launcher`] can connect to the original `broker`.
#[builder(default = true)]
spawn_broker: bool,
/// Tell the manager to serialize or not the state on restart
#[builder(default = LlmpShouldSaveState::OnRestart)]
serialize_state: LlmpShouldSaveState,
}
#[cfg(all(unix, feature = "fork"))]
impl<CF, MF, MT, SP> Debug for CentralizedLauncher<'_, CF, MF, MT, SP> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Launcher")
.field("configuration", &self.configuration)
.field("broker_port", &self.broker_port)
.field("cores", &self.cores)
.field("overcommit", &self.overcommit)
.field("spawn_broker", &self.spawn_broker)
.field("remote_broker_addr", &self.remote_broker_addr)
.field("stdout_file", &self.stdout_file)
.field("stderr_file", &self.stderr_file)
.finish_non_exhaustive()
}
}
/// The standard inner manager of centralized
pub type StdCentralizedInnerMgr<I, S, SHM, SP> = LlmpRestartingEventManager<(), I, S, SHM, SP>;
#[cfg(all(unix, feature = "fork"))]
impl<CF, MF, MT, SP> CentralizedLauncher<'_, CF, MF, MT, SP>
where
MT: Monitor + Clone + 'static,
SP: ShMemProvider + 'static,
{
/// Launch a standard Centralized-based fuzzer
pub fn launch<I, S>(&mut self) -> Result<(), Error>
where
S: DeserializeOwned + Serialize,
I: DeserializeOwned + Input + Send + Sync + 'static,
CF: FnOnce(
Option<S>,
CentralizedEventManager<
StdCentralizedInnerMgr<I, S, SP::ShMem, SP>,
I,
S,
SP::ShMem,
SP,
>,
ClientDescription,
) -> Result<(), Error>,
MF: FnOnce(
Option<S>,
CentralizedEventManager<
StdCentralizedInnerMgr<I, S, SP::ShMem, SP>,
I,
S,
SP::ShMem,
SP,
>,
ClientDescription,
) -> Result<(), Error>,
{
let restarting_mgr_builder =
|centralized_launcher: &Self, client_description: ClientDescription| {
// Fuzzer client. keeps retrying the connection to broker till the broker starts
let builder = RestartingMgr::<(), I, MT, S, SP>::builder()
.shmem_provider(centralized_launcher.shmem_provider.clone())
.broker_port(centralized_launcher.broker_port)
.kind(ManagerKind::Client { client_description })
.configuration(centralized_launcher.configuration)
.serialize_state(centralized_launcher.serialize_state)
.hooks(tuple_list!());
builder.build().launch()
};
self.launch_generic(restarting_mgr_builder, restarting_mgr_builder)
}
}
#[cfg(all(unix, feature = "fork"))]
impl<CF, MF, MT, SP> CentralizedLauncher<'_, CF, MF, MT, SP>
where
MT: Monitor + Clone + 'static,
SP: ShMemProvider + 'static,
{
/// Launch a Centralized-based fuzzer.
/// - `main_inner_mgr_builder` will be called to build the inner manager of the main node.
/// - `secondary_inner_mgr_builder` will be called to build the inner manager of the secondary nodes.
pub fn launch_generic<EM, EMB, I, S>(
&mut self,
main_inner_mgr_builder: EMB,
secondary_inner_mgr_builder: EMB,
) -> Result<(), Error>
where
I: Input + Send + Sync + 'static,
CF: FnOnce(
Option<S>,
CentralizedEventManager<EM, I, S, SP::ShMem, SP>,
ClientDescription,
) -> Result<(), Error>,
EMB: FnOnce(&Self, ClientDescription) -> Result<(Option<S>, EM), Error>,
MF: FnOnce(
Option<S>,
CentralizedEventManager<EM, I, S, SP::ShMem, SP>, // No broker_hooks for centralized EM
ClientDescription,
) -> Result<(), Error>,
{
let mut main_inner_mgr_builder = Some(main_inner_mgr_builder);
let mut secondary_inner_mgr_builder = Some(secondary_inner_mgr_builder);
if self.cores.ids.is_empty() {
return Err(Error::illegal_argument(
"No cores to spawn on given, cannot launch anything.",
));
}
if self.secondary_run_client.is_none() {
return Err(Error::illegal_argument(
"No client callback provided".to_string(),
));
}
let core_ids = get_core_ids().unwrap();
let mut handles = vec![];
log::debug!("spawning on cores: {:?}", self.cores);
self.opened_stdout_file = self
.stdout_file
.map(|filename| File::create(filename).unwrap());
self.opened_stderr_file = self
.stderr_file
.map(|filename| File::create(filename).unwrap());
let debug_output = std::env::var(LIBAFL_DEBUG_OUTPUT).is_ok();
// Spawn clients
let mut index = 0_usize;
for bind_to in core_ids {
if self.cores.ids.contains(&bind_to) {
for overcommit_id in 0..self.overcommit {
index += 1;
self.shmem_provider.pre_fork()?;
match unsafe { fork() }? {
ForkResult::Parent(child) => {
self.shmem_provider.post_fork(false)?;
handles.push(child.pid);
log::info!(
"child with client id {index} spawned and bound to core {bind_to:?}"
);
}
ForkResult::Child => {
log::info!("{:?} PostFork", unsafe { libc::getpid() });
self.shmem_provider.post_fork(true)?;
std::thread::sleep(Duration::from_millis(
index as u64 * self.launch_delay,
));
if !debug_output {
if let Some(file) = &self.opened_stdout_file {
// # Safety
// We assume the file descriptors are valid here
unsafe {
dup2(file.as_raw_fd(), libc::STDOUT_FILENO)?;
match &self.opened_stderr_file {
Some(stderr) => {
dup2(stderr.as_raw_fd(), libc::STDERR_FILENO)?;
}
_ => {
dup2(file.as_raw_fd(), libc::STDERR_FILENO)?;
}
}
}
}
}
let client_description =
ClientDescription::new(index, overcommit_id, bind_to);
if index == 1 {
// Main client
log::debug!("Running main client on PID {}", std::process::id());
let (state, mgr) = main_inner_mgr_builder.take().unwrap()(
self,
client_description.clone(),
)?;
let mut centralized_event_manager_builder =
CentralizedEventManager::builder();
centralized_event_manager_builder =
centralized_event_manager_builder.is_main(true);
let c_mgr = centralized_event_manager_builder.build_on_port(
mgr,
// tuple_list!(multi_machine_event_manager_hook.take().unwrap()),
self.shmem_provider.clone(),
self.centralized_broker_port,
)?;
self.main_run_client.take().unwrap()(
state,
c_mgr,
client_description,
)?;
Err(Error::shutting_down())
} else {
// Secondary clients
log::debug!(
"Running secondary client on PID {}",
std::process::id()
);
let (state, mgr) = secondary_inner_mgr_builder.take().unwrap()(
self,
client_description.clone(),
)?;
let centralized_builder = CentralizedEventManager::builder();
let c_mgr = centralized_builder.build_on_port(
mgr,
self.shmem_provider.clone(),
self.centralized_broker_port,
)?;
self.secondary_run_client.take().unwrap()(
state,
c_mgr,
client_description,
)?;
Err(Error::shutting_down())
}
}?,
}
}
}
}
// Create this after forks, to avoid problems with tokio runtime
// # Safety
// The `multi_machine_receiver_hook` needs messages to outlive the receiver.
// The underlying memory region for incoming messages lives longer than the async thread processing them.
#[cfg(feature = "multi_machine")]
let TcpMultiMachineHooks {
sender: multi_machine_sender_hook,
receiver: multi_machine_receiver_hook,
} = unsafe {
TcpMultiMachineHooks::builder()
.node_descriptor(self.multi_machine_node_descriptor.clone())
.build::<I>()?
};
let mut brokers = Brokers::new();
let exit_cleanly_after = NonZeroUsize::try_from(self.cores.ids.len()).unwrap();
// Add centralized broker
brokers.add(Box::new({
#[cfg(feature = "multi_machine")]
let centralized_hooks = tuple_list!(
CentralizedLlmpHook::<I>::new()?,
multi_machine_receiver_hook,
);
#[cfg(not(feature = "multi_machine"))]
let centralized_hooks = tuple_list!(CentralizedLlmpHook::<I>::new()?);
// TODO switch to false after solving the bug
let mut broker = LlmpBroker::with_keep_pages_attach_to_tcp(
self.shmem_provider.clone(),
centralized_hooks,
self.centralized_broker_port,
true,
)?;
broker.set_exit_after(exit_cleanly_after);
broker
}));
#[cfg(feature = "multi_machine")]
assert!(
self.spawn_broker,
"Multi machine is not compatible with externally spawned brokers for now."
);
// If we should add another broker, add it to other brokers.
if self.spawn_broker {
log::info!("I am broker!!.");
#[cfg(not(feature = "multi_machine"))]
let llmp_hook = tuple_list!(StdLlmpEventHook::<I, MT>::new(self.monitor.clone())?);
#[cfg(feature = "multi_machine")]
let llmp_hook = tuple_list!(
StdLlmpEventHook::<I, MT>::new(self.monitor.clone())?,
multi_machine_sender_hook,
);
let mut broker = LlmpBroker::create_attach_to_tcp(
self.shmem_provider.clone(),
llmp_hook,
self.broker_port,
)?;
if let Some(remote_broker_addr) = self.remote_broker_addr {
log::info!("B2b: Connecting to {:?}", &remote_broker_addr);
broker.inner_mut().connect_b2b(remote_broker_addr)?;
}
broker.set_exit_after(exit_cleanly_after);
brokers.add(Box::new(broker));
}
log::debug!("Broker has been initialized; pid {}.", std::process::id());
// Loop over all the brokers that should be polled
brokers.loop_with_timeouts(Duration::from_secs(30), Some(Duration::from_millis(5)));
#[cfg(feature = "llmp_debug")]
log::info!("The last client quit. Exiting.");
// Brokers exited. kill all clients.
for handle in &handles {
unsafe {
libc::kill(*handle, libc::SIGINT);
}
}
Err(Error::shutting_down())
}
}