From 5c53bb812152c3d7919cadfd47c210b181bf89ac Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Fri, 28 Mar 2014 15:29:48 +0000 Subject: tcg: Avoid undefined behaviour patching code at unaligned addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid C undefined behaviour when patching generated code, provide wrappers tcg_patch8/16/32/64 which use the usual memcpy trick, and use them in the i386 backend. Reviewed-by: Alex Bennée Signed-off-by: Peter Maydell Signed-off-by: Richard Henderson --- tcg/tcg.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tcg/tcg.c') diff --git a/tcg/tcg.c b/tcg/tcg.c index 293f00bc5a..31a5d48d03 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -122,6 +122,11 @@ static inline void tcg_out8(TCGContext *s, uint8_t v) *s->code_ptr++ = v; } +static inline void tcg_patch8(uint8_t *p, uint8_t v) +{ + memcpy(p, &v, sizeof(v)); +} + static inline void tcg_out16(TCGContext *s, uint16_t v) { uint8_t *p = s->code_ptr; @@ -129,6 +134,11 @@ static inline void tcg_out16(TCGContext *s, uint16_t v) s->code_ptr = p + 2; } +static inline void tcg_patch16(uint8_t *p, uint16_t v) +{ + memcpy(p, &v, sizeof(v)); +} + static inline void tcg_out32(TCGContext *s, uint32_t v) { uint8_t *p = s->code_ptr; @@ -136,6 +146,11 @@ static inline void tcg_out32(TCGContext *s, uint32_t v) s->code_ptr = p + 4; } +static inline void tcg_patch32(uint8_t *p, uint32_t v) +{ + memcpy(p, &v, sizeof(v)); +} + static inline void tcg_out64(TCGContext *s, uint64_t v) { uint8_t *p = s->code_ptr; @@ -143,6 +158,11 @@ static inline void tcg_out64(TCGContext *s, uint64_t v) s->code_ptr = p + 8; } +static inline void tcg_patch64(uint8_t *p, uint64_t v) +{ + memcpy(p, &v, sizeof(v)); +} + /* label relocation processing */ static void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, -- cgit v1.2.3