From ec6fdee274bd3f228226b3bd99fda23bc6983fe0 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 19 Mar 2024 17:06:38 +0100 Subject: [PATCH 1/2] Fix unaligned memory access when reading DRM events Running with debug assertions enabled on armv7 (an stm32mp157) panics due to unaligned memory reads: thread 'main' panicked at 'misaligned pointer dereference: address must be a multiple of 0x8 but is 0xbec90b44', .../drm-0.9.0/src/control/mod.rs:906:34 Fix this by using the corresponding functions from core to safely copy the data (in C we'd do a memcpy). --- src/control/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/control/mod.rs b/src/control/mod.rs index b534248..02d2549 100644 --- a/src/control/mod.rs +++ b/src/control/mod.rs @@ -1139,14 +1139,16 @@ impl Iterator for Events { fn next(&mut self) -> Option { if self.amount > 0 && self.i < self.amount { - let event = unsafe { &*(self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event) }; + let event_ptr = unsafe { self.event_buf.as_ptr().add(self.i) as *const ffi::drm_event }; + let event = unsafe { std::ptr::read_unaligned(event_ptr) }; self.i += event.length as usize; match event.type_ { ffi::DRM_EVENT_VBLANK => { #[allow(unknown_lints)] #[allow(invalid_reference_casting)] - let vblank_event = - unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) }; + let vblank_event = unsafe { + std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank) + }; Some(Event::Vblank(VblankEvent { frame: vblank_event.sequence, time: Duration::new( @@ -1161,8 +1163,9 @@ impl Iterator for Events { ffi::DRM_EVENT_FLIP_COMPLETE => { #[allow(unknown_lints)] #[allow(invalid_reference_casting)] - let vblank_event = - unsafe { &*(event as *const _ as *const ffi::drm_event_vblank) }; + let vblank_event = unsafe { + std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank) + }; Some(Event::PageFlip(PageFlipEvent { frame: vblank_event.sequence, duration: Duration::new( From 5c73f2a3d4443ed90c345cc72a5c003b57268f5d Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 27 Mar 2024 21:16:31 +0100 Subject: [PATCH 2/2] Remove hopefully unnecessary lint guards --- src/control/mod.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/control/mod.rs b/src/control/mod.rs index 02d2549..d9ae1c4 100644 --- a/src/control/mod.rs +++ b/src/control/mod.rs @@ -1144,8 +1144,6 @@ impl Iterator for Events { self.i += event.length as usize; match event.type_ { ffi::DRM_EVENT_VBLANK => { - #[allow(unknown_lints)] - #[allow(invalid_reference_casting)] let vblank_event = unsafe { std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank) }; @@ -1161,8 +1159,6 @@ impl Iterator for Events { })) } ffi::DRM_EVENT_FLIP_COMPLETE => { - #[allow(unknown_lints)] - #[allow(invalid_reference_casting)] let vblank_event = unsafe { std::ptr::read_unaligned(event_ptr as *const ffi::drm_event_vblank) };