Skip to content

Commit 62b1bde

Browse files
authored
Fix AnyMap for TypeIds with 128 bit (#1311)
* Fix AnyMap for TypeIds with 128 bit * make const * added test, removed static_assertions
1 parent fa63493 commit 62b1bde

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

libafl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
6767
erased-serde = { version = "0.3.21", default-features = false, features = ["alloc"] } # erased serde
6868
postcard = { version = "1.0", features = ["alloc"] } # no_std compatible serde serialization fromat
6969
bincode = {version = "1.3", optional = true }
70-
static_assertions = "1.1.0"
7170
c2rust-bitfields = { version = "0.17", features = ["no_std"] }
7271
num_enum = { version = "0.5.7", default-features = false }
7372
typed-builder = "0.14" # Implement the builder pattern at compiletime

libafl/src/bolts/anymap.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use alloc::boxed::Box;
44
use core::{
55
any::{Any, TypeId},
6+
mem::size_of,
67
ptr::addr_of,
78
};
89

@@ -43,21 +44,41 @@ macro_rules! impl_asany {
4344
///
4445
/// # Note
4546
/// Probably not safe for future compilers, fine for now.
47+
/// The size changed in later rust versions, see <https://github.com/rust-lang/compiler-team/issues/608>
48+
#[inline]
4649
#[must_use]
47-
pub fn pack_type_id(id: u64) -> TypeId {
48-
assert_eq_size!(TypeId, u64);
49-
unsafe { *(addr_of!(id) as *const TypeId) }
50+
pub const fn pack_type_id(id: u128) -> TypeId {
51+
match size_of::<TypeId>() {
52+
8 => {
53+
let id_64 = id as u64;
54+
unsafe { *(addr_of!(id_64) as *const TypeId) }
55+
}
56+
16 => unsafe { *(addr_of!(id) as *const TypeId) },
57+
_ => {
58+
// TypeId size of this size is not yet supported"
59+
panic!("Unsupported size for TypeId");
60+
}
61+
}
5062
}
5163

5264
/// Unpack a `type_id` to an `u64`
5365
/// Opposite of [`pack_type_id(id)`].
5466
///
5567
/// # Note
5668
/// Probably not safe for future compilers, fine for now.
69+
/// The size changed in later rust versions, see <https://github.com/rust-lang/compiler-team/issues/608>
70+
#[inline]
5771
#[must_use]
58-
pub fn unpack_type_id(id: TypeId) -> u64 {
59-
assert_eq_size!(TypeId, u64);
60-
unsafe { *(addr_of!(id) as *const u64) }
72+
pub const fn unpack_type_id(id: TypeId) -> u128 {
73+
#[allow(clippy::cast_ptr_alignment)] // we never actually cast to u128 if the type is u64.
74+
match size_of::<TypeId>() {
75+
8 => unsafe { *(addr_of!(id) as *const u64) as u128 },
76+
16 => unsafe { *(addr_of!(id) as *const u128) },
77+
_ => {
78+
// TypeId size of this size is not yet supported"
79+
panic!("Unsupported size for TypeId");
80+
}
81+
}
6182
}
6283

6384
/// Create `AnyMap` and `NamedAnyMap` for a given trait
@@ -432,3 +453,22 @@ macro_rules! create_anymap_for_trait {
432453
}
433454
}
434455
}
456+
457+
#[cfg(test)]
458+
mod test {
459+
use core::any::TypeId;
460+
461+
use super::{pack_type_id, unpack_type_id};
462+
463+
#[test]
464+
fn test_type_id() {
465+
let type_id_u64 = unpack_type_id(TypeId::of::<u64>());
466+
let type_id_u128 = unpack_type_id(TypeId::of::<u128>());
467+
468+
assert_eq!(pack_type_id(type_id_u64), TypeId::of::<u64>());
469+
assert_eq!(pack_type_id(type_id_u128), TypeId::of::<u128>());
470+
471+
assert_ne!(pack_type_id(type_id_u64), TypeId::of::<u128>());
472+
assert_ne!(pack_type_id(type_id_u128), TypeId::of::<u64>());
473+
}
474+
}

libafl/src/bolts/serdeany.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ macro_rules! create_serde_registry_for_trait {
100100
where
101101
V: serde::de::SeqAccess<'de>,
102102
{
103-
let id: u64 = visitor.next_element()?.unwrap();
103+
let id: u128 = visitor.next_element()?.unwrap();
104104
let cb = unsafe {
105105
*REGISTRY
106106
.deserializers
@@ -117,7 +117,7 @@ macro_rules! create_serde_registry_for_trait {
117117

118118
#[allow(unused_qualifications)]
119119
struct Registry {
120-
deserializers: Option<HashMap<u64, DeserializeCallback<dyn $trait_name>>>,
120+
deserializers: Option<HashMap<u128, DeserializeCallback<dyn $trait_name>>>,
121121
finalized: bool,
122122
}
123123

@@ -174,7 +174,7 @@ macro_rules! create_serde_registry_for_trait {
174174
/// in the registry
175175
#[derive(Debug, Serialize, Deserialize)]
176176
pub struct SerdeAnyMap {
177-
map: HashMap<u64, Box<dyn $trait_name>>,
177+
map: HashMap<u128, Box<dyn $trait_name>>,
178178
}
179179

180180
// 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 {
301301
#[allow(unused_qualifications)]
302302
#[derive(Debug, Serialize, Deserialize)]
303303
pub struct NamedSerdeAnyMap {
304-
map: HashMap<u64, HashMap<u64, Box<dyn $trait_name>>>,
304+
map: HashMap<u128, HashMap<u64, Box<dyn $trait_name>>>,
305305
}
306306

307307
// 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 {
467467
pub fn all_typeids(
468468
&self,
469469
) -> core::iter::Map<
470-
Keys<'_, u64, HashMap<u64, Box<dyn $trait_name>>>,
471-
fn(&u64) -> TypeId,
470+
Keys<'_, u128, HashMap<u64, Box<dyn $trait_name>>>,
471+
fn(&u128) -> TypeId,
472472
> {
473473
self.map.keys().map(|x| pack_type_id(*x))
474474
}

libafl/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ extern crate std;
7979
#[macro_use]
8080
#[doc(hidden)]
8181
pub extern crate alloc;
82-
#[macro_use]
83-
extern crate static_assertions;
8482
#[cfg(feature = "ctor")]
8583
#[doc(hidden)]
8684
pub use ctor::ctor;

0 commit comments

Comments
 (0)