aboutsummaryrefslogtreecommitdiff
path: root/tcg
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2014-03-28 15:29:48 +0000
committerRichard Henderson <rth@twiddle.net>2014-05-12 10:03:04 -0700
commit5c53bb812152c3d7919cadfd47c210b181bf89ac (patch)
tree1e6de84b05aa30c933b709bfb6097dc45b6044d0 /tcg
parent4387345a962284c77435a9644d528d2356adecc5 (diff)
downloadqemu-5c53bb812152c3d7919cadfd47c210b181bf89ac.zip
qemu-5c53bb812152c3d7919cadfd47c210b181bf89ac.tar.gz
qemu-5c53bb812152c3d7919cadfd47c210b181bf89ac.tar.bz2
tcg: Avoid undefined behaviour patching code at unaligned addresses
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>
Diffstat (limited to 'tcg')
-rw-r--r--tcg/i386/tcg-target.c12
-rw-r--r--tcg/tcg.c20
2 files changed, 26 insertions, 6 deletions
diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index 34ece1f..9a585ab 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -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();
@@ -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) {
@@ -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) {
diff --git a/tcg/tcg.c b/tcg/tcg.c
index 293f00b..31a5d48 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,