forked from hgarrereyn/LibAFL-FrameShift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_input.rs
More file actions
214 lines (175 loc) · 6.42 KB
/
Copy pathstructured_input.rs
File metadata and controls
214 lines (175 loc) · 6.42 KB
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
use ahash::RandomState;
use libafl::{corpus::CorpusId, inputs::{HasMutatorBytes, HasTargetBytes, Input}, Error};
use libafl_bolts::{fs::write_file_atomic, prelude::OwnedSlice, HasLen};
use rand::{rngs::StdRng, Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::{hash::{BuildHasher, Hasher}, io::Read, path::Path};
use std::fmt::Debug;
use crate::core::structured::Structured;
#[derive(Serialize, Deserialize, Clone)]
pub struct StructuredInput {
pub input: Structured,
pub status: InputStatus,
pub seed: u64,
}
impl Debug for StructuredInput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StructuredInput")
.field("status", &self.status)
.finish()
}
}
impl StructuredInput {
pub fn new_raw(bytes: &[u8]) -> Self {
Self {
input: Structured::raw(bytes.to_vec()),
status: InputStatus::New,
seed: 0,
}
}
pub fn new_structured(input: Structured) -> Self {
Self {
input,
status: InputStatus::New,
seed: 0,
}
}
pub fn set_seed(&mut self, seed: u64) {
self.seed = seed;
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum InputStatus {
/// New grammar, has not been searched.
New,
/// Mutated from a grammar which has been searched.
Mutated,
/// Sometimes we will crash during the search, so we mark inputs as in progress to avoid falling into a loop.
InProgress,
/// A searched grammar (corpus entry should match the entry here).
Searched(CorpusId),
}
impl Input for StructuredInput {
fn generate_name(&self, _idx: usize) -> String {
let mut hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher();
hasher.write(&self.input.get_raw());
format!("{:016x}", hasher.finish())
}
fn to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
// Write raw data to file
write_file_atomic(&path, &self.input.get_raw())?;
let parent = path.as_ref().parent().unwrap();
let file_name = path.as_ref().file_name().unwrap();
let full_path = parent.join(format!(".{}.annotated", file_name.to_string_lossy()));
// Write annotated data to file
let json = serde_json::to_string(&self.input).unwrap();
write_file_atomic(full_path, json.as_bytes())?;
Ok(())
}
/// Load the content of this input from a file
fn from_file<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let parent = path.as_ref().parent().unwrap();
let file_name = path.as_ref().file_name().unwrap();
let full_path = parent.join(format!(".{}.annotated", file_name.to_string_lossy()));
// Check if annotated file exists
if full_path.exists() {
// Load annotated data
let json = std::fs::read_to_string(full_path)?;
let structure: Structured = serde_json::from_str(&json)?;
Ok(StructuredInput::new_structured(structure))
} else {
// Load raw data
let mut file = std::fs::File::open(path)?;
let mut bytes = vec![];
file.read_to_end(&mut bytes)?;
Ok(StructuredInput::new_raw(&bytes))
}
}
}
impl HasLen for StructuredInput {
fn len(&self) -> usize {
self.input.get_raw().len()
}
}
impl HasTargetBytes for StructuredInput {
fn target_bytes(&self) -> OwnedSlice<u8> {
let x = self.input.get_raw();
OwnedSlice::from(x)
}
}
impl HasMutatorBytes for StructuredInput {
fn bytes(&self) -> &[u8] {
self.input.get_raw()
}
fn bytes_mut(&mut self) -> &mut [u8] {
self.input.get_raw_mut()
}
fn resize(&mut self, new_len: usize, value: u8) {
let mut rng = StdRng::seed_from_u64(self.seed);
let prev_len = self.input.get_raw().len();
if new_len > prev_len {
let diff = new_len - prev_len;
let data = vec![value; diff];
// Find insertion point.
let insertions = self.input.insertion_points();
let insert_pos = insertions[rng.gen_range(0..insertions.len())];
self.input.insert_disabling(insert_pos, &data);
} else if new_len < prev_len {
self.input.remove_disabling(new_len, prev_len - new_len);
}
}
fn extend<'a, I: IntoIterator<Item = &'a u8>>(&mut self, iter: I) {
let data = iter.into_iter().cloned().collect::<Vec<_>>();
self.input.insert_disabling(self.input.get_raw().len(), &data);
}
fn splice<R, I>(&mut self, range: R, replace_with: I) -> Option<std::vec::Splice<'_, I::IntoIter>>
where
R: std::ops::RangeBounds<usize>,
I: IntoIterator<Item = u8> {
let start = match range.start_bound() {
std::ops::Bound::Included(&x) => x,
std::ops::Bound::Excluded(&x) => x + 1,
std::ops::Bound::Unbounded => 0,
};
let end = match range.end_bound() {
std::ops::Bound::Included(&x) => x + 1,
std::ops::Bound::Excluded(&x) => x,
std::ops::Bound::Unbounded => self.input.get_raw().len(),
};
let replace_with = replace_with.into_iter().collect::<Vec<_>>();
let prev_size = end - start;
let new_size = replace_with.len();
if prev_size == new_size {
self.input.write(start, &replace_with);
} else if prev_size > new_size {
self.input.write(start, &replace_with);
self.input.remove_disabling(start + new_size, prev_size - new_size);
} else {
self.input.write(start, &replace_with[..prev_size]);
self.input.insert_disabling(end, &replace_with[prev_size..]);
}
None
}
fn drain<R>(&mut self, range: R) -> Option<std::vec::Drain<'_, u8>>
where
R: std::ops::RangeBounds<usize> {
let start = match range.start_bound() {
std::ops::Bound::Included(&x) => x,
std::ops::Bound::Excluded(&x) => x + 1,
std::ops::Bound::Unbounded => 0,
};
let end = match range.end_bound() {
std::ops::Bound::Included(&x) => x + 1,
std::ops::Bound::Excluded(&x) => x,
std::ops::Bound::Unbounded => self.input.get_raw().len(),
};
self.input.remove_disabling(start, end - start);
None
}
}