From ea12e4adab5c8d9fd2c4272bd33276a456f6992c Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 6 Aug 2023 10:13:51 +0100 Subject: [PATCH] Avoid unaligned loads in jit_lvn_copy --- src/jit/jit-optim.c | 6 +++--- src/util.h | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/jit/jit-optim.c b/src/jit/jit-optim.c index 52e4b0214..148dbde24 100644 --- a/src/jit/jit-optim.c +++ b/src/jit/jit-optim.c @@ -777,15 +777,15 @@ static void jit_lvn_copy(jit_ir_t *ir, lvn_state_t *state) switch (count) { case 8: ir->size = JIT_SZ_64; - ir->arg1 = LVN_CONST(*(uint64_t *)src); + ir->arg1 = LVN_CONST(unaligned_load(src, uint64_t)); break; case 4: ir->size = JIT_SZ_32; - ir->arg1 = LVN_CONST(*(uint32_t *)src); + ir->arg1 = LVN_CONST(unaligned_load(src, uint32_t)); break; case 2: ir->size = JIT_SZ_16; - ir->arg1 = LVN_CONST(*(uint16_t *)src); + ir->arg1 = LVN_CONST(unaligned_load(src, uint16_t)); break; case 1: ir->size = JIT_SZ_8; diff --git a/src/util.h b/src/util.h index 317beab9e..a2059dfcb 100644 --- a/src/util.h +++ b/src/util.h @@ -451,4 +451,10 @@ void list_clear(ptr_list_t *l); #define PACK_BE64(u) \ PACK_BE32(u >> 32), PACK_BE32(u) +#define unaligned_load(ptr, type) ({ \ + const struct { type value; } \ + __attribute__((packed)) *s = ptr; \ + s->value; \ + }) + #endif // _UTIL_H