Skip to content
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

Merged
merged 35 commits into from
Oct 18, 2024
Merged

Use nonzero_lit #2589

merged 35 commits into from
Oct 18, 2024

Conversation

tokatoka
Copy link
Member

@tokatoka tokatoka commented Oct 7, 2024

No description provided.

@@ -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());
Copy link
Member

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

Copy link
Member

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.

Copy link
Member Author

@tokatoka tokatoka Oct 8, 2024

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.

Copy link
Member

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..

@@ -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;
Copy link
Member

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

Self::transforming_with_max_iterations(mutator, DEFAULT_MUTATIONAL_MAX_ITERATIONS).unwrap()
Self::transforming_with_max_iterations(
mutator,
nonzero_lit::usize!(DEFAULT_MUTATIONAL_MAX_ITERATIONS),
Copy link
Member

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?

@domenukk
Copy link
Member

domenukk commented Oct 8, 2024

I have concerns with the changes:

  • Adding NonZero to constructors makes them way more ugly to use. We can just check the parameter on creation dynamically. I had this originally and changed it back.
  • I don't think the extra dependency for nonzero_lit is worth it. It makes the code no more legible and is an extra dependency and extra macro.

@tokatoka
Copy link
Member Author

tokatoka commented Oct 8, 2024

I don't think the extra dependency for nonzero_lit is worth it. It makes the code no more legible and is an extra dependency and extra macro.

unwrap() is more ugly.

@tokatoka
Copy link
Member Author

Adapted the stuff

@@ -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,
Copy link
Member

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?

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member Author

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing changed here, right?

libafl_bolts/Cargo.toml Outdated Show resolved Hide resolved
@domenukk
Copy link
Member

domenukk commented Oct 15, 2024

  • I'm not sure how much sense it makes to let users shoot themselves in the foot with 0 sized generators and 0 stacking.
  • I still don't see why we don't just keep checking this inside the constructor and return an IllegalValue, that is literally what the error is for...
  • With the nonzero! maybe using NonZero in the API isn't so ugly anymore?

I'll leave the choice up to you, just wanted to voice my opinion one last time, do what you think is best ;)

@tokatoka
Copy link
Member Author

0 size generator is odd but 0 stacking makes sense. because that stacking is about power

if size == 0 {
size = 1;
}
let size = 1 + state.rand_mut().below(self.max_size);
Copy link
Member

@domenukk domenukk Oct 16, 2024

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

libafl_bolts/src/lib.rs Outdated Show resolved Hide resolved
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() };
Copy link
Member

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.

Copy link
Member

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
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Member

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()
}

?

Copy link
Member Author

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

Copy link
Member Author

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

Copy link
Contributor

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

@tokatoka tokatoka merged commit fda1596 into main Oct 18, 2024
98 checks passed
@tokatoka tokatoka deleted the nonzero_lit branch October 18, 2024 21:33
Comment on lines 85 to +86
let mut size = state.rand_mut().below(self.max_size);
if size == 0 {
size = 1;
}
size = min(size, 1);
Copy link
Contributor

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?

Copy link
Member Author

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

riesentoaster pushed a commit to riesentoaster/LibAFL that referenced this pull request Dec 11, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants