Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/libafl/src/mutators/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
mutators::{MutationResult, Mutator, Tokens, rand_range},
nonzero,
stages::{
UnicodeIdentificationMetadata, extract_metadata,
UnicodeIdentificationMetadata,
mutational::{MutatedTransform, MutatedTransformPost},
},
state::{HasCorpus, HasMaxSize, HasRand},
Expand Down Expand Up @@ -268,7 +268,7 @@ fn rand_replace_range<S: HasRand + HasMaxSize, F: Fn(&mut S) -> char>(
}

input.0.splice(range, replacement);
input.1 = extract_metadata(input.0.mutator_bytes());
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());

MutationResult::Mutated
}
Expand Down Expand Up @@ -445,7 +445,7 @@ where
}

input.0.splice(range, token.iter().copied());
input.1 = extract_metadata(input.0.mutator_bytes());
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());
return Ok(MutationResult::Mutated);
}

Expand Down Expand Up @@ -508,7 +508,7 @@ where
}

input.0.splice(range, token.iter().copied());
input.1 = extract_metadata(input.0.mutator_bytes());
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());
return Ok(MutationResult::Mutated);
}

Expand All @@ -528,7 +528,7 @@ mod test {
corpus::NopCorpus,
inputs::{BytesInput, HasMutatorBytes},
mutators::{Mutator, UnicodeCategoryRandMutator, UnicodeSubcategoryRandMutator},
stages::extract_metadata,
stages::UnicodeIdentificationMetadata,
state::StdState,
};

Expand All @@ -550,7 +550,7 @@ mod test {
)?;

for _ in 0..(1 << 12) {
let metadata = extract_metadata(bytes.mutator_bytes());
let metadata = UnicodeIdentificationMetadata::new(bytes.mutator_bytes());
let mut input = (bytes, metadata);
let _ = mutator.mutate(&mut state, &mut input);
println!(
Expand Down Expand Up @@ -585,7 +585,7 @@ mod test {
)?;

for _ in 0..(1 << 12) {
let metadata = extract_metadata(bytes.mutator_bytes());
let metadata = UnicodeIdentificationMetadata::new(bytes.mutator_bytes());
let mut input = (bytes, metadata);
let _ = mutator.mutate(&mut state, &mut input);
println!(
Expand Down
16 changes: 11 additions & 5 deletions crates/libafl/src/stages/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ pub struct UnicodeIdentificationMetadata {
impl_serdeany!(UnicodeIdentificationMetadata);

impl UnicodeIdentificationMetadata {
/// Creates a new [`struct@UnicodeIdentificationMetadata`].
#[must_use]
pub fn new(bytes: &[u8]) -> Self {
Self {
ranges: Rc::new(extract_ranges(bytes))
}
}

/// The list of pre-computed string-like ranges in the input
#[must_use]
pub fn ranges(&self) -> &Vec<(usize, BitVec)> {
self.ranges.as_ref()
}
}

pub(crate) fn extract_metadata(bytes: &[u8]) -> UnicodeIdentificationMetadata {
fn extract_ranges(bytes: &[u8]) -> Vec<(usize, BitVec)> {
let mut ranges = Vec::new();

if !bytes.is_empty() {
Expand Down Expand Up @@ -63,9 +71,7 @@ pub(crate) fn extract_metadata(bytes: &[u8]) -> UnicodeIdentificationMetadata {
}
}

UnicodeIdentificationMetadata {
ranges: Rc::new(ranges),
}
ranges
}

/// Stage which identifies potential strings in the provided input
Expand Down Expand Up @@ -101,7 +107,7 @@ impl<I, S> UnicodeIdentificationStage<I, S> {
let input = tc.load_input(state.corpus())?;

let bytes = input.target_bytes();
let metadata = extract_metadata(&bytes);
let metadata = UnicodeIdentificationMetadata::new(&bytes);
tc.add_metadata(metadata);

Ok(())
Expand Down
Loading