Skip to content

Commit

Permalink
Fix unaligned memory access when reading DRM events
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
tronical authored and Drakulix committed Mar 27, 2024
1 parent 424981d commit 16bcbcd
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,14 +1139,16 @@ impl Iterator for Events {

fn next(&mut self) -> Option<Event> {
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(
Expand All @@ -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(
Expand Down

0 comments on commit 16bcbcd

Please sign in to comment.