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

Implement Hash for MapObserver #1989

Merged
merged 10 commits into from
Apr 19, 2024
Merged

Implement Hash for MapObserver #1989

merged 10 commits into from
Apr 19, 2024

Conversation

edwin1729
Copy link
Contributor

  • Rename the hash utility function (in MapObserver) to hash_easy

  • Use hash_slice as a helper function to impl Hash trait

* Rename the hash utility function (in MapObserver) to hash_easy

* Use hash_slice as a helper function to impl Hash trait
@edwin1729
Copy link
Contributor Author

I've also tried implement Hash for libafl_bolts::ownedref::OwnedMutSlice to delete hash_slice. However OwnedMapObserver::map is Vec<T> not OwnedMutSlice<T>, which prevents removal of hash_slice helper in a clean way.

@domenukk
Copy link
Member

domenukk commented Apr 2, 2024

@addisoncrump want to take a look?

@domenukk
Copy link
Member

domenukk commented Apr 2, 2024

  • I'm not a big fan of hash_easy, maybe rename to _simple to fit the rest of the names in the codebase. Also, do we really still need it?

  • there's a bunch of things that don't implement Hash yet, such as this guy:

error[E0277]: the trait bound `observers::map::pybind::PythonMapObserverI8: core::hash::Hash` is not satisfied
    --> libafl/src/observers/map.rs:2720:34
     |
2501 | /     macro_rules! define_python_map_observer {
2502 | |         ($struct_name1:ident, $py_name1:tt, $struct_name2:ident, $py_name2:tt, $struct_name_trait:ident, $py_name_trait:tt, $datatype:ty,...
2503 | |
2504 | |             #[pyclass(unsendable, name = $py_name1)]
...    |
2720 | |             impl MapObserver for $struct_name_trait {
     | |                                  ^^^^^^^^^^^^^^^^^^ the trait `core::hash::Hash` is not implemented for `observers::map::pybind::PythonMapObserverI8`
...    |
2794 | |         };
2795 | |     }
     | |_____- in this expansion of `define_python_map_observer!`
2796 |
2797 | /     define_python_map_observer!(
2798 | |         PythonStdMapObserverI8,
2799 | |         "StdMapObserverI8",
2800 | |         PythonOwnedMapObserverI8,
...    |
2805 | |         PythonMapObserverWrapperI8
2806 | |     );
     | |_____- in this macro invocation
     |
note: required by a bound in `observers::map::MapObserver`
    --> libafl/src/observers/map.rs:85:83
     |
85   | pub trait MapObserver: HasLen + Named + Serialize + serde::de::DeserializeOwned + Hash
     |                                                                                   ^^^^ required by this bound in `MapObserver`

@domenukk domenukk requested a review from addisoncrump April 2, 2024 08:27
@@ -70,8 +70,7 @@ fn init_count_class_16() {
}

/// Compute the hash of a slice
fn hash_slice<T>(slice: &[T]) -> u64 {
let mut hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher();
fn hash_slice<T, H: Hasher>(slice: &[T], hasher: &mut H) -> u64 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This entire function may replaced with .as_slice().hash(&mut hasher), and should be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hash_slice should be renamed to hash_helper. Its used to define the hash function. I can't see how to remove it.

Copy link
Member

Choose a reason for hiding this comment

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

@addisoncrump any last words on this? Otherwise we'll merge

Copy link
Collaborator

Choose a reason for hiding this comment

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

It can be removed. One sec.

@edwin1729 edwin1729 marked this pull request as draft April 6, 2024 13:51
@edwin1729 edwin1729 marked this pull request as ready for review April 7, 2024 13:02
@edwin1729 edwin1729 marked this pull request as draft April 7, 2024 13:02
* Also rename hash_easy to hash_simple
@edwin1729 edwin1729 marked this pull request as ready for review April 7, 2024 14:13
@edwin1729 edwin1729 marked this pull request as draft April 7, 2024 14:14
@edwin1729
Copy link
Contributor Author

edwin1729 commented Apr 7, 2024

Isn't there a way to trigger the CI pipeline? I can't get clippy to work, because of python dependency errors (specifically sphinx)

* hash_helper is used to define the implementation of hash function/trait
@tokatoka
Copy link
Member

tokatoka commented Apr 7, 2024

Again! python ..🤦
can you merge from main? i removed the python stuff. now you don't have to worry about it.

@tokatoka tokatoka marked this pull request as ready for review April 7, 2024 15:46
@tokatoka
Copy link
Member

tokatoka commented Apr 7, 2024

if you make this PR ready for review. then CI will run. else it won't

@domenukk
Copy link
Member

There are still some Errors in CI such as

error[E0407]: method `hash` is not a member of trait `MapObserver`
   --> libafl_targets/src/sancov_8bit.rs:199:9
    |
199 | /         fn hash(&self) -> u64 {
200 | |             let mut hasher = RandomState::with_seeds(0, 0, 0, 0).build_hasher();
201 | |             for map in unsafe { &*addr_of!(COUNTERS_MAPS) } {
202 | |                 let slice = map.as_slice();
...   |
209 | |             hasher.finish()
210 | |         }
    | |_________^ not a member of trait `MapObserver`

@domenukk domenukk requested a review from addisoncrump April 17, 2024 01:47
@edwin1729
Copy link
Contributor Author

edwin1729 commented Apr 17, 2024 via email

 * Use hash_one function to make hash_simple a one-liner
hasher.finish()
#[inline]
fn hash_simple(&self) -> u64 {
RandomState::with_seeds(0, 0, 0, 0).hash_one(self)
Copy link
Member

Choose a reason for hiding this comment

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

We could use the hash_std method here that can use xxh3 (but probably we don't need to)

pub fn hash_std(input: &[u8]) -> u64 {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why does this method even exist? We should just have a type definition for StdHash or something, probably.

Copy link
Member

Choose a reason for hiding this comment

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

Not even sure if it implements any rust traits tbh

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hash_simple does not implement any rust traits. If hash_std is okay then can use that instead. I don't really like it but Hash trait in rust doesn't seem to provide any default hashing function, so I made this

Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally, we don't actually want a default hashing function. This should be specified by the user, which is why we wanted the Hash impl (as this allows us to use a generic Hasher).

@domenukk domenukk changed the title MapObserver implements Hash Change MapObserver to implement Hash Apr 17, 2024
@domenukk domenukk changed the title Change MapObserver to implement Hash Implement Hash for MapObserver Apr 17, 2024
@addisoncrump
Copy link
Collaborator

@domenukk I think this is good to go now. Please review latest changes and then merge it up when you're satisfied with it.

@domenukk
Copy link
Member

Happy if you are. Thanks y'all :)

@addisoncrump
Copy link
Collaborator

I'll merge this when CI finishes, then 🙂

@addisoncrump addisoncrump merged commit c238b69 into AFLplusplus:main Apr 19, 2024
78 checks passed
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