Skip to content

Commit

Permalink
Fixes pedantic clippy ptr cast lints in tiered storage (solana-labs#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored and buffalojoec committed Apr 16, 2024
1 parent 6add311 commit 65eabd8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
8 changes: 4 additions & 4 deletions accounts-db/src/tiered_storage/byte_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
crate::tiered_storage::{footer::AccountBlockFormat, meta::AccountMetaOptionalFields},
std::{
io::{Cursor, Read, Result as IoResult, Write},
mem,
mem, ptr,
},
};

Expand Down Expand Up @@ -74,7 +74,7 @@ impl ByteBlockWriter {
/// and bytemuck's Pod and NoUninit for more information.
pub unsafe fn write_type<T>(&mut self, value: &T) -> IoResult<usize> {
let size = mem::size_of::<T>();
let ptr = value as *const _ as *const u8;
let ptr = ptr::from_ref(value).cast();
// SAFETY: The caller ensures that `value` contains no uninitialized bytes,
// we ensure the size is safe by querying T directly,
// and Rust ensures all values are at least byte-aligned.
Expand Down Expand Up @@ -158,7 +158,7 @@ pub unsafe fn read_type<T>(byte_block: &[u8], offset: usize) -> Option<&T> {
if overflow || next > byte_block.len() {
return None;
}
let ptr = byte_block[offset..].as_ptr() as *const T;
let ptr = byte_block[offset..].as_ptr().cast();
debug_assert!(ptr as usize % std::mem::align_of::<T>() == 0);
// SAFETY: The caller ensures it is safe to cast bytes to T,
// we ensure the size is safe by querying T directly,
Expand Down Expand Up @@ -195,7 +195,7 @@ mod tests {
let (next, overflow) = offset.overflowing_add(size);
assert!(!overflow && next <= buffer.len());
let data = &buffer[offset..next];
let ptr = data.as_ptr() as *const T;
let ptr = data.as_ptr().cast();

(unsafe { std::ptr::read_unaligned(ptr) }, next)
}
Expand Down
5 changes: 3 additions & 2 deletions accounts-db/src/tiered_storage/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
io::{BufWriter, Read, Result as IoResult, Seek, SeekFrom, Write},
mem,
path::Path,
ptr,
},
};

Expand Down Expand Up @@ -83,7 +84,7 @@ impl TieredReadableFile {
/// Refer to the Safety sections in std::slice::from_raw_parts()
/// and bytemuck's Pod and AnyBitPattern for more information.
pub unsafe fn read_type<T>(&self, value: &mut T) -> IoResult<()> {
let ptr = value as *mut _ as *mut u8;
let ptr = ptr::from_mut(value).cast();
// SAFETY: The caller ensures it is safe to cast bytes to T,
// we ensure the size is safe by querying T directly,
// and Rust ensures ptr is aligned.
Expand Down Expand Up @@ -136,7 +137,7 @@ impl TieredWritableFile {
/// Refer to the Safety sections in std::slice::from_raw_parts()
/// and bytemuck's Pod and NoUninit for more information.
pub unsafe fn write_type<T>(&mut self, value: &T) -> IoResult<usize> {
let ptr = value as *const _ as *const u8;
let ptr = ptr::from_ref(value).cast();
let bytes = unsafe { std::slice::from_raw_parts(ptr, mem::size_of::<T>()) };
self.write_bytes(bytes)
}
Expand Down
21 changes: 12 additions & 9 deletions accounts-db/src/tiered_storage/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
memmap2::Mmap,
num_enum::TryFromPrimitiveError,
solana_sdk::{hash::Hash, pubkey::Pubkey},
std::{mem, path::Path},
std::{mem, path::Path, ptr},
thiserror::Error,
};

Expand Down Expand Up @@ -258,13 +258,13 @@ impl TieredStorageFooter {
/// prior to use. This ensures the formats are valid to interpret as (rust) enums.
fn sanitize(footer: &Self) -> Result<(), SanitizeFooterError> {
let account_meta_format_u16 =
unsafe { &*(&footer.account_meta_format as *const _ as *const u16) };
unsafe { &*(ptr::from_ref(&footer.account_meta_format).cast::<u16>()) };
let owners_block_format_u16 =
unsafe { &*(&footer.owners_block_format as *const _ as *const u16) };
unsafe { &*(ptr::from_ref(&footer.owners_block_format).cast::<u16>()) };
let index_block_format_u16 =
unsafe { &*(&footer.index_block_format as *const _ as *const u16) };
unsafe { &*(ptr::from_ref(&footer.index_block_format).cast::<u16>()) };
let account_block_format_u16 =
unsafe { &*(&footer.account_block_format as *const _ as *const u16) };
unsafe { &*(ptr::from_ref(&footer.account_block_format).cast::<u16>()) };

_ = AccountMetaFormat::try_from(*account_meta_format_u16)
.map_err(SanitizeFooterError::InvalidAccountMetaFormat)?;
Expand Down Expand Up @@ -389,7 +389,7 @@ mod tests {
let mut footer = TieredStorageFooter::default();
unsafe {
std::ptr::write(
&mut footer.account_meta_format as *mut _ as *mut u16,
ptr::from_mut(&mut footer.account_meta_format).cast::<u16>(),
0xBAD0,
);
}
Expand All @@ -405,7 +405,7 @@ mod tests {
let mut footer = TieredStorageFooter::default();
unsafe {
std::ptr::write(
&mut footer.owners_block_format as *mut _ as *mut u16,
ptr::from_mut(&mut footer.owners_block_format).cast::<u16>(),
0xBAD0,
);
}
Expand All @@ -420,7 +420,10 @@ mod tests {
{
let mut footer = TieredStorageFooter::default();
unsafe {
std::ptr::write(&mut footer.index_block_format as *mut _ as *mut u16, 0xBAD0);
std::ptr::write(
ptr::from_mut(&mut footer.index_block_format).cast::<u16>(),
0xBAD0,
);
}
let result = TieredStorageFooter::sanitize(&footer);
assert!(matches!(
Expand All @@ -434,7 +437,7 @@ mod tests {
let mut footer = TieredStorageFooter::default();
unsafe {
std::ptr::write(
&mut footer.account_block_format as *mut _ as *mut u16,
ptr::from_mut(&mut footer.account_block_format).cast::<u16>(),
0xBAD0,
);
}
Expand Down
2 changes: 1 addition & 1 deletion accounts-db/src/tiered_storage/mmap_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn get_pod<T: bytemuck::AnyBitPattern>(mmap: &Mmap, offset: usize) -> IoResu
/// and bytemuck's Pod and AnyBitPattern for more information.
pub unsafe fn get_type<T>(mmap: &Mmap, offset: usize) -> IoResult<(&T, usize)> {
let (data, next) = get_slice(mmap, offset, std::mem::size_of::<T>())?;
let ptr = data.as_ptr() as *const T;
let ptr = data.as_ptr().cast();
debug_assert!(ptr as usize % std::mem::align_of::<T>() == 0);
// SAFETY: The caller ensures it is safe to cast bytes to T,
// we ensure the size is safe by querying T directly,
Expand Down

0 comments on commit 65eabd8

Please sign in to comment.