Skip to content

Commit

Permalink
Fix pointer alignment check in get_batch_aligned_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
InBetweenNames committed Oct 19, 2022
1 parent 11f76f0 commit a06c865
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
18 changes: 6 additions & 12 deletions crates/bevy_ptr/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,13 @@ impl<'a, T> ThinSimdAlignedSlicePtr<'a, T> {

let off_ptr = self.ptr.as_ptr().add(index);

//NOTE: ZSTs may cause this "slice" to point into nothingness.
//This sounds dangerous, but won't cause harm as nothing
//will actually access anything "in the slice"

//TODO: when pointer_is_aligned is standardized, we can just use ptr::is_aligned()
#[cfg(debug_assertions)]
if core::mem::size_of::<T>() > 0 {
//NOTE: ZSTs may cause this "slice" to point into nothingness.
//A "batch" of ZSTs is itself a ZST.
//This sounds dangerous, but won't cause harm as nothing
//will actually access anything "in the slice".
//ZSTs are trivially well aligned, so there is nothing to be checked.
//align_offset will probably return MAX for these types which
//can throw the test off.
//When is_aligned is standardized, we can just use that.

debug_assert_eq!(off_ptr.align_offset(ALIGN), 0);
}
debug_assert_eq!(off_ptr as usize % ALIGN, 0);

//SAFETY: off_ptr is not null
off_ptr as *const AlignedBatchT<T, N, ALIGN>
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ptr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ impl<'a, T> ThinSimdAlignedSlicePtr<'a, T> {
/// `ptr` must be aligned to at least `MAX_SIMD_ALIGNMENT`
#[inline]
pub unsafe fn new(ptr: *mut T, _len: usize) -> Self {
//NOTE: ZSTs may cause this "slice" to point into nothingness.
//This sounds dangerous, but won't cause harm as nothing
//will actually access anything "in the slice"

//TODO: when pointer_is_aligned is standardized, we can just use ptr::is_aligned()
#[cfg(debug_assertions)]
if core::mem::size_of::<T>() > 0 {
//NOTE: ZSTs may cause this "slice" to point into nothingness.
//This sounds dangerous, but won't cause harm as nothing
//will actually access anything "in the slice"
debug_assert!(ptr.align_offset(batch::MAX_SIMD_ALIGNMENT) == 0);
}
debug_assert_eq!(ptr as usize % batch::MAX_SIMD_ALIGNMENT, 0);

Self {
ptr: NonNull::new_unchecked(ptr),
Expand Down

0 comments on commit a06c865

Please sign in to comment.