From e03e5931431f3c807370e251604bd423a9acc6a7 Mon Sep 17 00:00:00 2001 From: Roland Scheidegger Date: Thu, 29 Feb 2024 20:42:27 +0100 Subject: [PATCH] auxiliary/rtasm: fix unaligned stores Unaliged stores are unspecified behavior according to C rules, hence address sanitizers may complain. Even though this worked fine in practice (it's almost impossible here for the compiler to do something "wrong" even if it assumes the store is aligned, given such stores work just fine on x86), we should follow the rules. The widely accepted solution for this (it may be somewhat surprising you can't actually do unaligned assignments explicitly somehow in C) nowadays is to just use memcpy(). The compiler should figure out (at least with optimizations enabled) it's just a trivial store and optimize it back to a single cpu instruction, while still satisfying asan. (I've verified that even in debug builds the memcpy() is actually optimized away anyway, I suspect there's some compiler flags somewhere forcing this behavior.) Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/10208 Reviewed-by: Jose Fonseca Part-of: --- src/gallium/auxiliary/rtasm/rtasm_x86sse.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/gallium/auxiliary/rtasm/rtasm_x86sse.c b/src/gallium/auxiliary/rtasm/rtasm_x86sse.c index 8f27a03c202..6cf85b333ba 100644 --- a/src/gallium/auxiliary/rtasm/rtasm_x86sse.c +++ b/src/gallium/auxiliary/rtasm/rtasm_x86sse.c @@ -194,8 +194,8 @@ static void emit_1b( struct x86_function *p, char b0 ) static void emit_1i( struct x86_function *p, int i0 ) { - int *icsr = (int *)reserve(p, sizeof(i0)); - *icsr = i0; + unsigned char *csr = reserve(p, sizeof(i0)); + memcpy(csr, &i0, sizeof(i0)); } static void emit_1ub( struct x86_function *p, unsigned char b0 ) @@ -434,7 +434,8 @@ int x86_call_forward( struct x86_function *p) void x86_fixup_fwd_jump( struct x86_function *p, int fixup ) { - *(int *)(p->store + fixup - 4) = x86_get_label(p) - fixup; + int lblfixed = x86_get_label(p) - fixup; + memcpy(p->store + fixup - 4, &lblfixed, sizeof(lblfixed)); } void x86_jmp( struct x86_function *p, int label)