Fix UB in anymap.rs and other minor warnings. (#1926)

* fix

* clippy

* fix
This commit is contained in:
Dongjia "toka" Zhang 2024-03-11 18:15:56 +01:00 committed by GitHub
parent e745401a39
commit 9b780cc0a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 29 deletions

View File

@ -4,7 +4,7 @@ use alloc::boxed::Box;
use core::{ use core::{
any::{Any, TypeId}, any::{Any, TypeId},
mem::size_of, mem::size_of,
ptr::addr_of, ptr::{addr_of, read_unaligned},
}; };
/// Convert to an Any trait object /// Convert to an Any trait object
@ -39,7 +39,7 @@ macro_rules! impl_asany {
}; };
} }
/// Get a `type_id` from its previously unpacked `u64`. /// Get a `type_id` from its previously unpacked `u128`.
/// Opposite of [`unpack_type_id(id)`]. /// Opposite of [`unpack_type_id(id)`].
/// ///
/// # Note /// # Note
@ -47,26 +47,13 @@ macro_rules! impl_asany {
/// The size changed in later rust versions, see <https://github.com/rust-lang/compiler-team/issues/608> /// The size changed in later rust versions, see <https://github.com/rust-lang/compiler-team/issues/608>
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(clippy::cast_ptr_alignment)]
pub const fn pack_type_id(id: u128) -> TypeId { pub const fn pack_type_id(id: u128) -> TypeId {
match size_of::<TypeId>() { // TypeId size of other sizes is not yet supported"
8 => { assert!(size_of::<TypeId>() == 16, "Unsupported size for TypeId");
let id_64 = id as u64; unsafe { *(addr_of!(id) as *const TypeId) }
// false positive: this branch only executes on 64 bit `TypeId`s
#[allow(clippy::cast_ptr_alignment)]
unsafe {
*(addr_of!(id_64) as *const TypeId)
}
}
16 => unsafe { *(addr_of!(id) as *const TypeId) },
_ => {
// TypeId size of this size is not yet supported"
panic!("Unsupported size for TypeId");
}
}
} }
/// Unpack a `type_id` to an `u64` /// Unpack a `type_id` to an `u128`
/// Opposite of [`pack_type_id(id)`]. /// Opposite of [`pack_type_id(id)`].
/// ///
/// # Note /// # Note
@ -75,15 +62,11 @@ pub const fn pack_type_id(id: u128) -> TypeId {
#[inline] #[inline]
#[must_use] #[must_use]
pub const 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. // see any.rs, it's alway u128 hence 16 bytes.
match size_of::<TypeId>() { // TypeId size of other sizes is not yet supported"
8 => unsafe { *(addr_of!(id) as *const u64) as u128 }, assert!(size_of::<TypeId>() == 16, "Unsupported size for TypeId");
16 => unsafe { *(addr_of!(id) as *const u128) }, let ret: u128 = unsafe { read_unaligned::<u128>(addr_of!(id) as *const u128) };
_ => { ret
// TypeId size of this size is not yet supported"
panic!("Unsupported size for TypeId");
}
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -43,6 +43,7 @@ sancov_pcguard_hitcounts = ["coverage"]
sancov_value_profile = ["common"] sancov_value_profile = ["common"]
sancov_8bit = [] sancov_8bit = []
sancov_ngram4 = ["coverage"] sancov_ngram4 = ["coverage"]
sancov_ngram8 = ["coverage"]
sancov_ctx = ["coverage"] sancov_ctx = ["coverage"]
sancov_cmplog = ["common"] # Defines cmp and __sanitizer_weak_hook functions. Use libfuzzer_interceptors to define interceptors (only compatible with Linux) sancov_cmplog = ["common"] # Defines cmp and __sanitizer_weak_hook functions. Use libfuzzer_interceptors to define interceptors (only compatible with Linux)
sancov_pcguard = ["sancov_pcguard_hitcounts"] sancov_pcguard = ["sancov_pcguard_hitcounts"]

View File

@ -36,14 +36,17 @@ type Ngram8 = core::simd::u32x8;
#[rustversion::nightly] #[rustversion::nightly]
pub static mut PREV_ARRAY_4: Ngram4 = Ngram4::from_array([0, 0, 0, 0]); pub static mut PREV_ARRAY_4: Ngram4 = Ngram4::from_array([0, 0, 0, 0]);
/// The array holding the previous locs. This is required for NGRAM-4 instrumentation
#[cfg(feature = "sancov_ngram8")] #[cfg(feature = "sancov_ngram8")]
#[rustversion::nightly] #[rustversion::nightly]
pub static mut PREV_ARRAY_8: Ngram8 = Ngram8::from_array([0, 0, 0, 0, 0, 0, 0, 0]); pub static mut PREV_ARRAY_8: Ngram8 = Ngram8::from_array([0, 0, 0, 0, 0, 0, 0, 0]);
/// We shift each of the values in ngram4 everytime we see new edges
#[cfg(feature = "sancov_ngram4")] #[cfg(feature = "sancov_ngram4")]
#[rustversion::nightly] #[rustversion::nightly]
pub static SHR_4: Ngram4 = Ngram4::from_array([1, 1, 1, 1]); pub static SHR_4: Ngram4 = Ngram4::from_array([1, 1, 1, 1]);
/// We shift each of the values in ngram8 everytime we see new edges
#[cfg(feature = "sancov_ngram8")] #[cfg(feature = "sancov_ngram8")]
#[rustversion::nightly] #[rustversion::nightly]
pub static SHR_8: Ngram8 = Ngram8::from_array([1, 1, 1, 1, 1, 1, 1, 1]); pub static SHR_8: Ngram8 = Ngram8::from_array([1, 1, 1, 1, 1, 1, 1, 1]);
@ -77,7 +80,7 @@ impl ExecutorHook for NgramHook {
#[cfg(feature = "sancov_ngram8")] #[cfg(feature = "sancov_ngram8")]
unsafe { unsafe {
PREV_ARRAY_8 = Ngram8::from_array([0, 0, 0, 0, 0, 0, 0, 0]) PREV_ARRAY_8 = Ngram8::from_array([0, 0, 0, 0, 0, 0, 0, 0]);
} }
} }
fn post_exec<EM, I, S, Z>( fn post_exec<EM, I, S, Z>(