Skip to content

Better type_eq #2946

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

Merged
merged 4 commits into from
Feb 8, 2025
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ strum = "0.26.3"
strum_macros = "0.26.4"
toml = "0.8.19" # For parsing the injections toml file
typed-builder = "0.20.0" # Implement the builder pattern at compiletime
typeid = "1.0.0" # Safe type_eq that doesn't rely on std specialization
uuid = { version = "1.10.0", features = ["serde", "v4"] }
which = "6.0.3"
windows = "0.59.0"
Expand Down
1 change: 1 addition & 0 deletions libafl_bolts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ rustversion = { workspace = true }
[dependencies]
libafl_derive = { workspace = true, default-features = true, optional = true }
static_assertions = { workspace = true }
typeid = { workspace = true }

tuple_list = { version = "0.1.3" }
hashbrown = { workspace = true, features = [
Expand Down
43 changes: 3 additions & 40 deletions libafl_bolts/src/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use alloc::{borrow::Cow, vec::Vec};
use core::{
any::type_name,
fmt::{Debug, Formatter},
marker::PhantomData,
ops::{Deref, DerefMut, Index, IndexMut},
};
use core::{any::TypeId, cell::Cell, marker::PhantomData, mem::transmute};
use core::{any::TypeId, mem::transmute};

#[cfg(feature = "alloc")]
use serde::{Deserialize, Serialize};
Expand All @@ -21,36 +22,9 @@ use crate::HasLen;
use crate::Named;

/// Returns if the type `T` is equal to `U`, ignoring lifetimes.
#[inline] // this entire call gets optimized away :)
#[must_use]
pub fn type_eq<T: ?Sized, U: ?Sized>() -> bool {
// decider struct: hold a cell (which we will update if the types are unequal) and some
// phantom data using a function pointer to allow for Copy to be implemented
struct W<'a, T: ?Sized, U: ?Sized>(&'a Cell<bool>, PhantomData<fn() -> (&'a T, &'a U)>);

// default implementation: if the types are unequal, we will use the clone implementation
impl<T: ?Sized, U: ?Sized> Clone for W<'_, T, U> {
#[inline]
fn clone(&self) -> Self {
// indicate that the types are unequal
// unfortunately, use of interior mutability (Cell) makes this not const-compatible
// not really possible to get around at this time
self.0.set(false);
W(self.0, self.1)
}
}

// specialized implementation: Copy is only implemented if the types are the same
#[expect(clippy::mismatching_type_param_order)]
impl<T: ?Sized> Copy for W<'_, T, T> {}

let detected = Cell::new(true);
// [].clone() is *specialized* in core.
// Types which implement copy will have their copy implementations used, falling back to clone.
// If the types are the same, then our clone implementation (which sets our Cell to false)
// will never be called, meaning that our Cell's content remains true.
let res = [W::<T, U>(&detected, PhantomData)].clone();
res[0].0.get()
typeid::of::<T>() == typeid::of::<U>()
Copy link
Member

Choose a reason for hiding this comment

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

Can we use this typeid for our SerdeAnyMap well?

}

/// Borrow each member of the tuple
Expand Down Expand Up @@ -955,8 +929,6 @@ mod test {
}

#[test]
#[cfg(feature = "alloc")]
#[expect(unused_qualifications)] // for type name tests
fn test_type_eq() {
// An alias for equality testing
type OwnedMutSliceAlias<'a> = OwnedMutSlice<'a, u8>;
Expand All @@ -976,15 +948,6 @@ mod test {
// test weirder lifetime things
assert!(type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u8>>());
assert!(!type_eq::<OwnedMutSlice<u8>, OwnedMutSlice<u32>>());

assert!(type_eq::<
OwnedMutSlice<u8>,
crate::ownedref::OwnedMutSlice<u8>,
>());
assert!(!type_eq::<
OwnedMutSlice<u8>,
crate::ownedref::OwnedMutSlice<u32>,
>());
}

#[test]
Expand Down
Loading