-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use nonzero_lit #2589
Use nonzero_lit #2589
Conversation
@@ -77,7 +77,7 @@ fn main() { | |||
|
|||
/* ANCHOR: generator */ | |||
// Generator of printable bytearrays of max size 32 | |||
let mut generator = RandPrintablesGenerator::new(32).unwrap(); | |||
let mut generator = RandPrintablesGenerator::new(NonZeroUsize::new(32).unwrap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this change, the mutator is not a performance-critical path, let's just keep it be a normal usize and check the value on creation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, use the generic NonZero::new()
instead - it's shorter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand.
I thought the point of your previous PR is that you want compile-time check instead of debug_ssert so that users won't pass 0 in inappropriate place & get runtime errors.
but now you say the opposite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, new
is only called once. If the new "unwraps" internally or an external NonZero
call does it makes no difference for the user nor for performance - but now the API is more ugly..
libafl/src/mutators/mutations.rs
Outdated
@@ -314,7 +311,7 @@ where | |||
Ok(MutationResult::Skipped) | |||
} else { | |||
let byte = state.rand_mut().choose(input.bytes_mut()).unwrap(); | |||
*byte ^= 1 + state.rand_mut().below(NonZero::new(254).unwrap()) as u8; | |||
*byte ^= 1 + state.rand_mut().below(nonzero_lit::usize!(254)) as u8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's basically the same length and we only have 30 usages in the whole code base.
I don't think it's worth it adding a new dependency for that (and adding compile time for the extra macro)
Rust optimizes the new away
libafl/src/stages/mutational.rs
Outdated
Self::transforming_with_max_iterations(mutator, DEFAULT_MUTATIONAL_MAX_ITERATIONS).unwrap() | ||
Self::transforming_with_max_iterations( | ||
mutator, | ||
nonzero_lit::usize!(DEFAULT_MUTATIONAL_MAX_ITERATIONS), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, why change the API to be ugly?
I have concerns with the changes:
|
unwrap() is more ugly. |
Adapted the stuff |
libafl/src/generators/mod.rs
Outdated
@@ -119,7 +106,7 @@ impl<S> RandBytesGenerator<S> { | |||
#[derive(Clone, Debug)] | |||
/// Generates random printable characters | |||
pub struct RandPrintablesGenerator<S> { | |||
max_size: NonZeroUsize, | |||
max_size: usize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we allow a Generator for 0 bytes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll be a bug in 99% of cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm okay maybe this guy should have NonZero really
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or i think i should just make this usize const
@@ -376,7 +379,9 @@ where | |||
{ | |||
fn mutate(&mut self, state: &mut S, input: &mut I) -> Result<MutationResult, Error> { | |||
let size = input.bytes().len(); | |||
let Some(nonzero_size) = NonZero::new(size) else { | |||
let off = if let Some(nz) = NonZero::new(size) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing changed here, right?
I'll leave the choice up to you, just wanted to voice my opinion one last time, do what you think is best ;) |
0 size generator is odd but 0 stacking makes sense. because that stacking is about power |
libafl/src/generators/mod.rs
Outdated
if size == 0 { | ||
size = 1; | ||
} | ||
let size = 1 + state.rand_mut().below(self.max_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct. You add 1 in new
and here you add 1 again.
So a max size of 10 will lead to self.max_size = 11
-> below leads to a value up to 10.
In this line, you add 1 to size
so worst case the result has 11 entries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
libafl/src/generators/mod.rs
Outdated
pub fn new(max_size: usize) -> Self { | ||
// # Safety | ||
// Saturating added 1 so it's always above 0 | ||
let max_size = unsafe { NonZeroUsize::new(max_size.saturating_add(1)).unwrap_unchecked() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for 0 here and return Error::IllegalArgument (or really have max_size be NonZero) - right now specifying a max_size of 0 will result in generated inputs of size 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pick one of the two options (or otherwise return 0 sized inputs lol)
@@ -1050,6 +1050,46 @@ pub unsafe fn set_error_print_panic_hook(new_stderr: RawFd) { | |||
})); | |||
} | |||
|
|||
// Credit goes to https://github.com/thomcc/nonzero_lit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that all this complexity is needed, you can just do
use std::num::NonZeroUsize;
const nz: NonZeroUsize = match NonZeroUsize::new(4) {
Some(x) => x,
None => panic!("was zero")
};
This has been available since 2021: rust-lang/rust#89508
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const Option::unwrap
just landed, too: rust-lang/rust#67441
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what we use currently but it's more ugly.
Maybe we could just do a
const fn nonzero(val: usize) -> NonZeroUsize {
NonZero::new(val).unwrap()
}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could just do a
this is not compile time check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to use macro because i don't want to see bunches of unwrap() inside our lib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the motivation for a macro, I just think this macro could be implemented in a simpler way: #2624
let mut size = state.rand_mut().below(self.max_size); | ||
if size == 0 { | ||
size = 1; | ||
} | ||
size = min(size, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @tokatoka, not sure I'm understanding this change. Now the function will generate only ByteInput of 1 byte?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah sorry i am dumb this should be max
* nonzero_lit * nonzero * std instead of core * a * l * test * import * api * api * aaaaa * apiapi * api * api * api * mm * api * non zero * FMT * pls * nnnaaasdfadsfafdsa * pls * MM * fix * a * sat add * aa * mistake * unreachable * no generic * api change * a
No description provided.