Skip to content

Commit

Permalink
tcg: Avoid undefined behaviour patching code at unaligned addresses
Browse files Browse the repository at this point in the history
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 <alex.bennee@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <rth@twiddle.net>
  • Loading branch information
pm215 authored and rth7680 committed May 12, 2014
1 parent 4387345 commit 5c53bb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions tcg/i386/tcg-target.c
Expand Up @@ -151,14 +151,14 @@ static void patch_reloc(uint8_t *code_ptr, int type,
if (value != (int32_t)value) {
tcg_abort();
}
*(uint32_t *)code_ptr = value;
tcg_patch32(code_ptr, value);
break;
case R_386_PC8:
value -= (uintptr_t)code_ptr;
if (value != (int8_t)value) {
tcg_abort();
}
*(uint8_t *)code_ptr = value;
tcg_patch8(code_ptr, value);
break;
default:
tcg_abort();
Expand Down Expand Up @@ -1276,9 +1276,9 @@ static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
uint8_t **label_ptr = &l->label_ptr[0];

/* resolve label address */
*(uint32_t *)label_ptr[0] = (uint32_t)(s->code_ptr - label_ptr[0] - 4);
tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4);
if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
*(uint32_t *)label_ptr[1] = (uint32_t)(s->code_ptr - label_ptr[1] - 4);
tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4);
}

if (TCG_TARGET_REG_BITS == 32) {
Expand Down Expand Up @@ -1360,9 +1360,9 @@ static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
TCGReg retaddr;

/* resolve label address */
*(uint32_t *)label_ptr[0] = (uint32_t)(s->code_ptr - label_ptr[0] - 4);
tcg_patch32(label_ptr[0], s->code_ptr - label_ptr[0] - 4);
if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
*(uint32_t *)label_ptr[1] = (uint32_t)(s->code_ptr - label_ptr[1] - 4);
tcg_patch32(label_ptr[1], s->code_ptr - label_ptr[1] - 4);
}

if (TCG_TARGET_REG_BITS == 32) {
Expand Down
20 changes: 20 additions & 0 deletions tcg/tcg.c
Expand Up @@ -122,27 +122,47 @@ 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;
memcpy(p, &v, sizeof(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;
memcpy(p, &v, sizeof(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;
memcpy(p, &v, sizeof(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,
Expand Down

0 comments on commit 5c53bb8

Please sign in to comment.