Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
target/arm: Add compile time asserts to load/store_cpu_field macros
Add some compile-time asserts to the load_cpu_field() and store_cpu_field()
macros that the struct field being accessed is the expected size. This
lets us catch cases where we incorrectly tried to do a 32-bit load
from a 64-bit struct field.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-id: 20230424153909.1419369-3-peter.maydell@linaro.org
  • Loading branch information
pm215 committed May 2, 2023
1 parent 562f2a1 commit 2a625f6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions target/arm/translate-a32.h
Expand Up @@ -59,7 +59,12 @@ static inline TCGv_i32 load_cpu_offset(int offset)
return tmp;
}

#define load_cpu_field(name) load_cpu_offset(offsetof(CPUARMState, name))
/* Load from a 32-bit field to a TCGv_i32 */
#define load_cpu_field(name) \
({ \
QEMU_BUILD_BUG_ON(sizeof_field(CPUARMState, name) != 4); \
load_cpu_offset(offsetof(CPUARMState, name)); \
})

/* Load from the low half of a 64-bit field to a TCGv_i32 */
#define load_cpu_field_low32(name) \
Expand All @@ -70,9 +75,13 @@ static inline TCGv_i32 load_cpu_offset(int offset)

void store_cpu_offset(TCGv_i32 var, int offset, int size);

#define store_cpu_field(var, name) \
store_cpu_offset(var, offsetof(CPUARMState, name), \
sizeof_field(CPUARMState, name))
#define store_cpu_field(val, name) \
({ \
QEMU_BUILD_BUG_ON(sizeof_field(CPUARMState, name) != 4 \
&& sizeof_field(CPUARMState, name) != 1); \
store_cpu_offset(val, offsetof(CPUARMState, name), \
sizeof_field(CPUARMState, name)); \
})

#define store_cpu_field_constant(val, name) \
store_cpu_field(tcg_constant_i32(val), name)
Expand Down

0 comments on commit 2a625f6

Please sign in to comment.