From f47a85e002824b79e81bb088be02193e9e21da0d Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 9 Jun 2023 11:45:38 +0200 Subject: [PATCH 1/3] Fix AnyMap for TypeIds with 128 bit --- libafl/src/bolts/anymap.rs | 31 +++++++++++++++++++++++++------ libafl/src/bolts/serdeany.rs | 12 ++++++------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/libafl/src/bolts/anymap.rs b/libafl/src/bolts/anymap.rs index 01c5c23a1e2..fb4fd5c3584 100644 --- a/libafl/src/bolts/anymap.rs +++ b/libafl/src/bolts/anymap.rs @@ -3,6 +3,7 @@ use alloc::boxed::Box; use core::{ any::{Any, TypeId}, + mem::size_of, ptr::addr_of, }; @@ -44,9 +45,19 @@ macro_rules! impl_asany { /// # Note /// Probably not safe for future compilers, fine for now. #[must_use] -pub fn pack_type_id(id: u64) -> TypeId { - assert_eq_size!(TypeId, u64); - unsafe { *(addr_of!(id) as *const TypeId) } +pub fn pack_type_id(id: u128) -> TypeId { + match size_of::() { + 8 => { + let id_64 = id as u64; + unsafe { *(addr_of!(id_64) as *const TypeId) } + } + 16 => unsafe { *(addr_of!(id) as *const TypeId) }, + size => { + // this will complain at compiletime. + assert_eq_size!(TypeId, u64); + panic!("TypeId size of {size} bits is not supported"); + } + } } /// Unpack a `type_id` to an `u64` @@ -55,9 +66,17 @@ pub fn pack_type_id(id: u64) -> TypeId { /// # Note /// Probably not safe for future compilers, fine for now. #[must_use] -pub fn unpack_type_id(id: TypeId) -> u64 { - assert_eq_size!(TypeId, u64); - unsafe { *(addr_of!(id) as *const u64) } +pub fn unpack_type_id(id: TypeId) -> u128 { + #[allow(clippy::cast_ptr_alignment)] // we never actually cast to u128 if the type is u64. + match size_of::() { + 8 => unsafe { u128::from(*(addr_of!(id) as *const u64)) }, + 16 => unsafe { *(addr_of!(id) as *const u128) }, + size => { + // this will complain at compiletime. + assert_eq_size!(TypeId, u64); + panic!("TypeId size of {size} bits is not supported"); + } + } } /// Create `AnyMap` and `NamedAnyMap` for a given trait diff --git a/libafl/src/bolts/serdeany.rs b/libafl/src/bolts/serdeany.rs index 60d6bee70ae..b0980229be2 100644 --- a/libafl/src/bolts/serdeany.rs +++ b/libafl/src/bolts/serdeany.rs @@ -100,7 +100,7 @@ macro_rules! create_serde_registry_for_trait { where V: serde::de::SeqAccess<'de>, { - let id: u64 = visitor.next_element()?.unwrap(); + let id: u128 = visitor.next_element()?.unwrap(); let cb = unsafe { *REGISTRY .deserializers @@ -117,7 +117,7 @@ macro_rules! create_serde_registry_for_trait { #[allow(unused_qualifications)] struct Registry { - deserializers: Option>>, + deserializers: Option>>, finalized: bool, } @@ -174,7 +174,7 @@ macro_rules! create_serde_registry_for_trait { /// in the registry #[derive(Debug, Serialize, Deserialize)] pub struct SerdeAnyMap { - map: HashMap>, + map: HashMap>, } // Cloning by serializing and deserializing. It ain't fast, but it's honest work. @@ -301,7 +301,7 @@ macro_rules! create_serde_registry_for_trait { #[allow(unused_qualifications)] #[derive(Debug, Serialize, Deserialize)] pub struct NamedSerdeAnyMap { - map: HashMap>>, + map: HashMap>>, } // Cloning by serializing and deserializing. It ain't fast, but it's honest work. @@ -467,8 +467,8 @@ macro_rules! create_serde_registry_for_trait { pub fn all_typeids( &self, ) -> core::iter::Map< - Keys<'_, u64, HashMap>>, - fn(&u64) -> TypeId, + Keys<'_, u128, HashMap>>, + fn(&u128) -> TypeId, > { self.map.keys().map(|x| pack_type_id(*x)) } From bec52c7c4c3765bb3bf62f2024673e1478e5edd5 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 9 Jun 2023 13:09:14 +0200 Subject: [PATCH 2/3] make const --- libafl/src/bolts/anymap.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libafl/src/bolts/anymap.rs b/libafl/src/bolts/anymap.rs index fb4fd5c3584..744d7d75f6d 100644 --- a/libafl/src/bolts/anymap.rs +++ b/libafl/src/bolts/anymap.rs @@ -45,17 +45,18 @@ macro_rules! impl_asany { /// # Note /// Probably not safe for future compilers, fine for now. #[must_use] -pub fn pack_type_id(id: u128) -> TypeId { +pub const fn pack_type_id(id: u128) -> TypeId { match size_of::() { 8 => { let id_64 = id as u64; unsafe { *(addr_of!(id_64) as *const TypeId) } } 16 => unsafe { *(addr_of!(id) as *const TypeId) }, - size => { - // this will complain at compiletime. + _ => { + // TypeId size of this size is not yet supported" + // The assert will complain at compiletime. assert_eq_size!(TypeId, u64); - panic!("TypeId size of {size} bits is not supported"); + unreachable!(); } } } @@ -66,15 +67,16 @@ pub fn pack_type_id(id: u128) -> TypeId { /// # Note /// Probably not safe for future compilers, fine for now. #[must_use] -pub fn unpack_type_id(id: TypeId) -> u128 { +pub const fn unpack_type_id(id: TypeId) -> u128 { #[allow(clippy::cast_ptr_alignment)] // we never actually cast to u128 if the type is u64. match size_of::() { - 8 => unsafe { u128::from(*(addr_of!(id) as *const u64)) }, + 8 => unsafe { *(addr_of!(id) as *const u64) as u128 }, 16 => unsafe { *(addr_of!(id) as *const u128) }, - size => { - // this will complain at compiletime. + _ => { + // TypeId size of this size is not yet supported" + // The assert will complain at compiletime. assert_eq_size!(TypeId, u64); - panic!("TypeId size of {size} bits is not supported"); + unreachable!(); } } } From a843109857a7405076578ea62aab692a0d8ebad9 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 9 Jun 2023 13:34:12 +0200 Subject: [PATCH 3/3] added test, removed static_assertions --- libafl/Cargo.toml | 1 - libafl/src/bolts/anymap.rs | 31 +++++++++++++++++++++++++------ libafl/src/lib.rs | 2 -- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/libafl/Cargo.toml b/libafl/Cargo.toml index 683d659a87f..265df683ba1 100644 --- a/libafl/Cargo.toml +++ b/libafl/Cargo.toml @@ -67,7 +67,6 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv erased-serde = { version = "0.3.21", default-features = false, features = ["alloc"] } # erased serde postcard = { version = "1.0", features = ["alloc"] } # no_std compatible serde serialization fromat bincode = {version = "1.3", optional = true } -static_assertions = "1.1.0" c2rust-bitfields = { version = "0.17", features = ["no_std"] } num_enum = { version = "0.5.7", default-features = false } typed-builder = "0.14" # Implement the builder pattern at compiletime diff --git a/libafl/src/bolts/anymap.rs b/libafl/src/bolts/anymap.rs index 744d7d75f6d..46bd7c65e90 100644 --- a/libafl/src/bolts/anymap.rs +++ b/libafl/src/bolts/anymap.rs @@ -44,6 +44,8 @@ macro_rules! impl_asany { /// /// # Note /// Probably not safe for future compilers, fine for now. +/// The size changed in later rust versions, see +#[inline] #[must_use] pub const fn pack_type_id(id: u128) -> TypeId { match size_of::() { @@ -54,9 +56,7 @@ pub const fn pack_type_id(id: u128) -> TypeId { 16 => unsafe { *(addr_of!(id) as *const TypeId) }, _ => { // TypeId size of this size is not yet supported" - // The assert will complain at compiletime. - assert_eq_size!(TypeId, u64); - unreachable!(); + panic!("Unsupported size for TypeId"); } } } @@ -66,6 +66,8 @@ pub const fn pack_type_id(id: u128) -> TypeId { /// /// # Note /// Probably not safe for future compilers, fine for now. +/// The size changed in later rust versions, see +#[inline] #[must_use] pub const fn unpack_type_id(id: TypeId) -> u128 { #[allow(clippy::cast_ptr_alignment)] // we never actually cast to u128 if the type is u64. @@ -74,9 +76,7 @@ pub const fn unpack_type_id(id: TypeId) -> u128 { 16 => unsafe { *(addr_of!(id) as *const u128) }, _ => { // TypeId size of this size is not yet supported" - // The assert will complain at compiletime. - assert_eq_size!(TypeId, u64); - unreachable!(); + panic!("Unsupported size for TypeId"); } } } @@ -453,3 +453,22 @@ macro_rules! create_anymap_for_trait { } } } + +#[cfg(test)] +mod test { + use core::any::TypeId; + + use super::{pack_type_id, unpack_type_id}; + + #[test] + fn test_type_id() { + let type_id_u64 = unpack_type_id(TypeId::of::()); + let type_id_u128 = unpack_type_id(TypeId::of::()); + + assert_eq!(pack_type_id(type_id_u64), TypeId::of::()); + assert_eq!(pack_type_id(type_id_u128), TypeId::of::()); + + assert_ne!(pack_type_id(type_id_u64), TypeId::of::()); + assert_ne!(pack_type_id(type_id_u128), TypeId::of::()); + } +} diff --git a/libafl/src/lib.rs b/libafl/src/lib.rs index c3aaea58cfd..81fceecb6cc 100644 --- a/libafl/src/lib.rs +++ b/libafl/src/lib.rs @@ -79,8 +79,6 @@ extern crate std; #[macro_use] #[doc(hidden)] pub extern crate alloc; -#[macro_use] -extern crate static_assertions; #[cfg(feature = "ctor")] #[doc(hidden)] pub use ctor::ctor;