From 428b498cca32cc2f3fd2224bb90fdfc537029dc8 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Wed, 20 Jan 2021 23:18:08 +1100 Subject: [PATCH] [common] fix invalid read from unaligned addresses (fixes #410) --- common/src/framebuffer.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/common/src/framebuffer.c b/common/src/framebuffer.c index 37977940..0b8d283c 100644 --- a/common/src/framebuffer.c +++ b/common/src/framebuffer.c @@ -58,8 +58,6 @@ bool framebuffer_read(const FrameBuffer * frame, void * restrict dst, uint_least32_t rp = 0; size_t y = 0; const size_t linewidth = width * bpp; - const size_t blocks = linewidth / 64; - const size_t left = linewidth % 64; while(y < height) { @@ -77,8 +75,21 @@ bool framebuffer_read(const FrameBuffer * frame, void * restrict dst, wp = atomic_load_explicit(&frame->wp, memory_order_acquire); } + /* copy any unaligned bytes */ + const uint8_t * src = frame->data + rp; + const size_t unaligned = (uintptr_t)src & 0xF; + if (unaligned) + { + memcpy(d, src, unaligned); + src += unaligned; + d += unaligned; + } + + const size_t blocks = (linewidth - unaligned) / 64; + const size_t left = (linewidth - unaligned) % 64; + _mm_mfence(); - __m128i * restrict s = (__m128i *)(frame->data + rp); + __m128i * restrict s = (__m128i *)src; for(int i = 0; i < blocks; ++i) { __m128i *_d = (__m128i *)d;