From patchwork Mon May 12 23:30:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 348138 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 26B0A140082 for ; Tue, 13 May 2014 09:33:23 +1000 (EST) Received: from localhost ([::1]:40576 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wjzij-0005yK-5e for incoming@patchwork.ozlabs.org; Mon, 12 May 2014 19:33:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59173) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WjzhM-0004eP-Rq for qemu-devel@nongnu.org; Mon, 12 May 2014 19:32:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WjzhI-0005m7-3N for qemu-devel@nongnu.org; Mon, 12 May 2014 19:31:56 -0400 Received: from mail-qg0-x236.google.com ([2607:f8b0:400d:c04::236]:64919) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WjzhH-0005m0-TK for qemu-devel@nongnu.org; Mon, 12 May 2014 19:31:51 -0400 Received: by mail-qg0-f54.google.com with SMTP id q108so8599775qgd.27 for ; Mon, 12 May 2014 16:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=B7f1UrmDLQ1+q1hWH2Tx+WRyUpCJYHdu0UmywIxjOX8=; b=o0OyyejNO8X46xuVyF4MZhZnmCWphYxR61WKqo3Zn5bIMr6u4Dk8ivTMSOzl+m0x2o /1vEm9bwKZLD7AP/1cc+75R092bQ3+WWd+mGEZslS1FsqpJNNQMC97HEuF4HDNmY4O2i aVfWUNWDvcTiG9ItnNG0wQ0wTYOeOLqpR+LxEW/s9GiPqtIMhcU6kgEbe6X4royLPJ2X g2mYQOWXLjnsMQGqiQfz6e4ueLPL3uMWAeF6bKH4YjdZSgnQ2W52vNrYAGqYdRppoihn DtXE1A6Ca0zSyhA6vu5IY8tTMMm+lyspZ8bP2sdSI+S2r2elCWRjgFMhTKazCA5XPEjQ 0/rg== X-Received: by 10.229.27.198 with SMTP id j6mr42929833qcc.12.1399937511517; Mon, 12 May 2014 16:31:51 -0700 (PDT) Received: from anchor.com (50-194-63-110-static.hfc.comcastbusiness.net. [50.194.63.110]) by mx.google.com with ESMTPSA id m7sm23815931qae.35.2014.05.12.16.31.50 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 May 2014 16:31:50 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 12 May 2014 16:30:51 -0700 Message-Id: <1399937474-6530-4-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1399937474-6530-1-git-send-email-rth@twiddle.net> References: <1399937474-6530-1-git-send-email-rth@twiddle.net> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::236 Cc: peter.maydell@linaro.org Subject: [Qemu-devel] [PULL 03/26] tcg: Avoid undefined behaviour patching code at unaligned addresses X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Peter Maydell 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/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,