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
#[cfg(feature = "std")]
use serde::de::DeserializeOwned;
#[cfg(feature = "std")]
use crate::{
bolts::shmem::ShMemProvider,
events::{LlmpRestartingEventManager, ManagerKind, RestartingMgr},
inputs::Input,
observers::ObserversTuple,
stats::Stats,
Error,
};
#[cfg(all(windows, feature = "std"))]
use crate::bolts::os::startable_self;
#[cfg(all(unix, feature = "std"))]
use crate::bolts::os::{dup2, fork, ForkResult};
#[cfg(all(unix, feature = "std"))]
use std::{fs::File, os::unix::io::AsRawFd};
#[cfg(feature = "std")]
use std::net::SocketAddr;
#[cfg(all(windows, feature = "std"))]
use std::process::Stdio;
#[cfg(all(windows, feature = "std"))]
use core_affinity::CoreId;
#[cfg(feature = "std")]
use typed_builder::TypedBuilder;
#[cfg(feature = "std")]
pub type LauncherClientFnRef<'a, I, OT, S, SP> =
&'a mut dyn FnMut(Option<S>, LlmpRestartingEventManager<I, OT, S, SP>) -> Result<(), Error>;
const _AFL_LAUNCHER_CLIENT: &str = "AFL_LAUNCHER_CLIENT";
#[cfg(feature = "std")]
#[derive(TypedBuilder)]
#[allow(clippy::type_complexity)]
pub struct Launcher<'a, I, OT, S, SP, ST>
where
I: Input,
ST: Stats,
SP: ShMemProvider + 'static,
OT: ObserversTuple<I, S>,
S: DeserializeOwned,
{
shmem_provider: SP,
stats: ST,
configuration: String,
run_client: LauncherClientFnRef<'a, I, OT, S, SP>,
#[builder(default = 1337_u16)]
broker_port: u16,
cores: &'a [usize],
#[builder(default = None)]
stdout_file: Option<&'a str>,
#[builder(default = None)]
remote_broker_addr: Option<SocketAddr>,
#[builder(default = true)]
spawn_broker: bool,
}
#[cfg(feature = "std")]
impl<'a, I, OT, S, SP, ST> Launcher<'a, I, OT, S, SP, ST>
where
I: Input,
OT: ObserversTuple<I, S>,
ST: Stats + Clone,
SP: ShMemProvider + 'static,
S: DeserializeOwned,
{
#[cfg(all(unix, feature = "std"))]
#[allow(clippy::similar_names)]
pub fn launch(&mut self) -> Result<(), Error> {
let core_ids = core_affinity::get_core_ids().unwrap();
let num_cores = core_ids.len();
let mut handles = vec![];
println!("spawning on cores: {:?}", self.cores);
let file = self
.stdout_file
.map(|filename| File::create(filename).unwrap());
for (id, bind_to) in core_ids.iter().enumerate().take(num_cores) {
if self.cores.iter().any(|&x| x == id) {
self.shmem_provider.pre_fork()?;
match unsafe { fork() }? {
ForkResult::Parent(child) => {
self.shmem_provider.post_fork(false)?;
handles.push(child.pid);
#[cfg(feature = "std")]
println!("child spawned and bound to core {}", id);
}
ForkResult::Child => {
self.shmem_provider.post_fork(true)?;
#[cfg(feature = "std")]
std::thread::sleep(std::time::Duration::from_secs((id + 1) as u64));
#[cfg(feature = "std")]
if file.is_some() {
dup2(file.as_ref().unwrap().as_raw_fd(), libc::STDOUT_FILENO)?;
dup2(file.as_ref().unwrap().as_raw_fd(), libc::STDERR_FILENO)?;
}
let (state, mgr) = RestartingMgr::<I, OT, S, SP, ST>::builder()
.shmem_provider(self.shmem_provider.clone())
.broker_port(self.broker_port)
.kind(ManagerKind::Client {
cpu_core: Some(*bind_to),
})
.configuration(self.configuration.clone())
.build()
.launch()?;
(self.run_client)(state, mgr)?;
break;
}
};
}
}
if self.spawn_broker {
#[cfg(feature = "std")]
println!("I am broker!!.");
RestartingMgr::<I, OT, S, SP, ST>::builder()
.shmem_provider(self.shmem_provider.clone())
.stats(Some(self.stats.clone()))
.broker_port(self.broker_port)
.kind(ManagerKind::Broker)
.remote_broker_addr(self.remote_broker_addr)
.configuration(self.configuration.clone())
.build()
.launch()?;
for handle in &handles {
unsafe {
libc::kill(*handle, libc::SIGINT);
}
}
} else {
for handle in &handles {
let mut status = 0;
println!("Not spawning broker (spawn_broker is false). Waiting for fuzzer children to exit...");
unsafe {
libc::waitpid(*handle, &mut status, 0);
if status != 0 {
println!("Client with pid {} exited with status {}", handle, status);
}
}
}
}
Ok(())
}
#[cfg(all(windows, feature = "std"))]
#[allow(unused_mut)]
pub fn launch(&mut self) -> Result<(), Error> {
let is_client = std::env::var(_AFL_LAUNCHER_CLIENT);
let mut handles = match is_client {
Ok(core_conf) => {
let (state, mgr) = RestartingMgr::<I, OT, S, SP, ST>::builder()
.shmem_provider(self.shmem_provider.clone())
.broker_port(self.broker_port)
.kind(ManagerKind::Client {
cpu_core: Some(CoreId {
id: core_conf.parse()?,
}),
})
.configuration(self.configuration.clone())
.build()
.launch()?;
(self.run_client)(state, mgr)?;
unreachable!("Fuzzer client code should never get here!");
}
Err(std::env::VarError::NotPresent) => {
if self.stdout_file.is_some() {
println!("Child process file stdio is not supported on Windows yet. Dumping to stdout instead...");
}
let core_ids = core_affinity::get_core_ids().unwrap();
let num_cores = core_ids.len();
let mut handles = vec![];
println!("spawning on cores: {:?}", self.cores);
for (id, _) in core_ids.iter().enumerate().take(num_cores) {
if self.cores.iter().any(|&x| x == id) {
for id in 0..num_cores {
let stdio = if self.stdout_file.is_some() {
Stdio::inherit()
} else {
Stdio::null()
};
if self.cores.iter().any(|&x| x == id) {
std::env::set_var(_AFL_LAUNCHER_CLIENT, id.to_string());
let child = startable_self()?.stdout(stdio).spawn()?;
handles.push(child);
}
}
}
}
handles
}
Err(_) => panic!("Env variables are broken, received non-unicode!"),
};
if self.spawn_broker {
#[cfg(feature = "std")]
println!("I am broker!!.");
RestartingMgr::<I, OT, S, SP, ST>::builder()
.shmem_provider(self.shmem_provider.clone())
.stats(Some(self.stats.clone()))
.broker_port(self.broker_port)
.kind(ManagerKind::Broker)
.remote_broker_addr(self.remote_broker_addr)
.configuration(self.configuration.clone())
.build()
.launch()?;
for handle in &mut handles {
handle.kill()?;
}
} else {
println!("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() {
println!("Client with handle {:?} exited with {:?}", handle, ecode);
}
}
}
Ok(())
}
}