From c3d2919877eb4f0e55f95b51c698dfdf0cfb2946 Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 28 Aug 2024 14:02:16 -0700 Subject: [PATCH 1/2] `trait Pixels`: Add `fn wrapping_*as_{mut_,}ptr` methods for non-bounds checked versions. --- src/pixels.rs | 22 ++++++++++++++++++---- src/with_offset.rs | 8 ++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pixels.rs b/src/pixels.rs index 5c17f6b65..a52029489 100644 --- a/src/pixels.rs +++ b/src/pixels.rs @@ -28,7 +28,7 @@ pub trait Pixels { self.as_mut_ptr::().cast_const() } - /// Absolute ptr to [`BitDepth::Pixel`]s starting at `offset`. + /// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`. /// /// Bounds checked, but not [`DisjointMut`]-checked. #[cfg_attr(debug_assertions, track_caller)] @@ -49,12 +49,26 @@ pub trait Pixels { unsafe { self.as_mut_ptr::().add(pixel_offset) } } - /// Absolute ptr to [`BitDepth::Pixel`]s starting at `offset`. + /// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`. /// /// Bounds checked, but not [`DisjointMut`]-checked. #[cfg_attr(debug_assertions, track_caller)] - fn as_ptr_at(&self, offset: usize) -> *const BD::Pixel { - self.as_mut_ptr_at::(offset).cast_const() + fn as_ptr_at(&self, pixel_offset: usize) -> *const BD::Pixel { + self.as_mut_ptr_at::(pixel_offset).cast_const() + } + + /// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`. + /// + /// There is no bounds checking and this ptr may wrap and go out of bounds. + fn wrapping_as_mut_ptr_at(&self, pixel_offset: usize) -> *mut BD::Pixel { + self.as_mut_ptr::().wrapping_add(pixel_offset) + } + + /// Absolute ptr to [`BitDepth::Pixel`]s starting at `pixel_offset`. + /// + /// There is no bounds checking and this ptr may wrap and go out of bounds. + fn wrapping_as_ptr_at(&self, pixel_offset: usize) -> *const BD::Pixel { + self.as_ptr::().wrapping_add(pixel_offset) } /// Determine if they reference the same data. diff --git a/src/with_offset.rs b/src/with_offset.rs index 1aa76f388..b057d9ad5 100644 --- a/src/with_offset.rs +++ b/src/with_offset.rs @@ -92,6 +92,14 @@ impl WithOffset

{ pub fn as_mut_ptr(&self) -> *mut BD::Pixel { self.data.as_mut_ptr_at::(self.offset) } + + pub fn wrapping_as_ptr(&self) -> *const BD::Pixel { + self.data.wrapping_as_ptr_at::(self.offset) + } + + pub fn wrapping_as_mut_ptr(&self) -> *const BD::Pixel { + self.data.wrapping_as_mut_ptr_at::(self.offset) + } } impl Strided for WithOffset { From e541a11e528f30ab0586349554b908202302a24e Mon Sep 17 00:00:00 2001 From: Khyber Sen Date: Wed, 28 Aug 2024 14:03:35 -0700 Subject: [PATCH 2/2] `fn cdef::Fn::call`: Use `.wrapping_as_ptr` for `bottom` to avoid out-of-bounds, as the ptr is temporarily out of bounds when passed to asm. --- src/cdef.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cdef.rs b/src/cdef.rs index a768431af..464e94386 100644 --- a/src/cdef.rs +++ b/src/cdef.rs @@ -86,7 +86,7 @@ impl cdef::Fn { let stride = dst.stride(); let left = ptr::from_ref(left).cast(); let top_ptr = top.as_ptr::().cast(); - let bottom_ptr = bottom.as_ptr::().cast(); + let bottom_ptr = bottom.wrapping_as_ptr::().cast(); let top = FFISafe::new(&top); let bottom = FFISafe::new(&bottom); let sec_strength = sec_strength as c_int;