From: Richard Henderson <rth@twiddle.net>
To: qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org
Subject: [Qemu-devel] [PULL 03/26] tcg: Avoid undefined behaviour patching code at unaligned addresses
Date: Mon, 12 May 2014 16:30:51 -0700 [thread overview]
Message-ID: <1399937474-6530-4-git-send-email-rth@twiddle.net> (raw)
In-Reply-To: <1399937474-6530-1-git-send-email-rth@twiddle.net>
From: Peter Maydell <peter.maydell@linaro.org>
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>
---
tcg/i386/tcg-target.c | 12 ++++++------
tcg/tcg.c | 20 ++++++++++++++++++++
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,
--
1.9.0
next prev parent reply other threads:[~2014-05-12 23:32 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-12 23:30 [Qemu-devel] [PULL 00/26] tcg updates Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 01/26] exec-all.h: Use stl_p to avoid undefined behaviour patching x86 jumps Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 02/26] tcg: Avoid stores to unaligned addresses Richard Henderson
2014-05-12 23:30 ` Richard Henderson [this message]
2014-05-12 23:30 ` [Qemu-devel] [PULL 04/26] tcg: Introduce byte pointer arithmetic helpers Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 05/26] tcg: Define tcg_insn_unit for code pointers Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 06/26] tcg-i386: Define TCG_TARGET_INSN_UNIT_SIZE Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 07/26] tcg-ppc64: " Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 08/26] tcg-ppc: " Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 09/26] tcg-sparc: " Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 10/26] tcg-arm: " Richard Henderson
2014-05-12 23:30 ` [Qemu-devel] [PULL 11/26] tcg-aarch64: " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 12/26] tcg-s390: " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 13/26] tcg-ia64: " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 14/26] tcg-mips: " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 15/26] tci: " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 16/26] tcg: Require TCG_TARGET_INSN_UNIT_SIZE Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 17/26] tcg-i386: Rename tcg_out_calli to tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 18/26] tcg-s390: Rename tgen_calli " Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 19/26] tcg-ppc: Split out tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 20/26] tcg-ppc64: Rename tcg_out_calli to tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 21/26] tcg-sparc: Create tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 22/26] tcg-mips: Split out tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 23/26] tci: Create tcg_out_call Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 24/26] tcg: Make call address a constant parameter Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 25/26] tcg: Use tcg_target_available_regs in tcg_reg_alloc_mov Richard Henderson
2014-05-12 23:31 ` [Qemu-devel] [PULL 26/26] tcg: Remove unreachable code in tcg_out_op and op_defs Richard Henderson
2014-05-13 14:14 ` [Qemu-devel] [PULL 00/26] tcg updates Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1399937474-6530-4-git-send-email-rth@twiddle.net \
--to=rth@twiddle.net \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).