From patchwork Sat Mar 29 00:27:09 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 27385 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f71.google.com (mail-pa0-f71.google.com [209.85.220.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 983A220062 for ; Sat, 29 Mar 2014 00:30:27 +0000 (UTC) Received: by mail-pa0-f71.google.com with SMTP id kq14sf14000127pab.10 for ; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:date :message-id:in-reply-to:references:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=HLdy7OaSObtTuBx2+KqCmE8EmLfKJxfNWokVmh2lI2o=; b=j5Ei/XH720MWMwXgAPbP7pQvDjElJs8ADZwqkqRmqsfR0+kPBMSfULe9BZQ31sPsai Wd0EA+Lwfw3MAarW46BW188K3MpJD1Z0kSKr6jeJR4N51QcQONVavWJrtMyRXag0fDmZ 4vuLDvWkgQdV9voHDDhsIqUzFXQ/8HNJHxIMo8kBa5DVGHOVsE7B0i7aI364xZdxdUKM g89A7emZZ/yaUG+9Ijz8fCta1rMaeiIZ9vP04EUbYL466723KPoQj6oy7uu6kt3oh1FV xNiPYAu14bpsUJE94C7yp0V2cNrlXL1Slg3t+UVw+hrNXtuU20f4AQM8SF9baPIt0l3g WT9w== X-Gm-Message-State: ALoCoQlZSq6V6kHTEEd53ZYY4TlqeHEgh2me4vQzm17/boThJfZl4G1ijDmZxPN6tY8tQ9X+yVC1 X-Received: by 10.66.144.170 with SMTP id sn10mr3184467pab.43.1396053026792; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.33.197 with SMTP id j63ls361637qgj.0.gmail; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) X-Received: by 10.58.207.74 with SMTP id lu10mr9669793vec.15.1396053026666; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) Received: from mail-vc0-x22e.google.com (mail-vc0-x22e.google.com [2607:f8b0:400c:c03::22e]) by mx.google.com with ESMTPS id o6si1482987vcn.129.2014.03.28.17.30.26 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 28 Mar 2014 17:30:26 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c03::22e is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=2607:f8b0:400c:c03::22e; Received: by mail-vc0-f174.google.com with SMTP id ld13so6434832vcb.19 for ; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) X-Received: by 10.52.12.36 with SMTP id v4mr8373978vdb.20.1396053026568; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.12.8 with SMTP id v8csp48584vcv; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) X-Received: by 10.224.36.83 with SMTP id s19mr12937753qad.50.1396053026246; Fri, 28 Mar 2014 17:30:26 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id v6si3268526qas.137.2014.03.28.17.30.26 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 28 Mar 2014 17:30:26 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:36581 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WThAH-0002b4-T2 for patch@linaro.org; Fri, 28 Mar 2014 20:30:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51521) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WTh7V-0006fX-Ax for qemu-devel@nongnu.org; Fri, 28 Mar 2014 20:27:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WTh7Q-0006iC-2l for qemu-devel@nongnu.org; Fri, 28 Mar 2014 20:27:33 -0400 Received: from mail-qg0-x22c.google.com ([2607:f8b0:400d:c04::22c]:35419) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WTh7P-0006i7-V9 for qemu-devel@nongnu.org; Fri, 28 Mar 2014 20:27:28 -0400 Received: by mail-qg0-f44.google.com with SMTP id a108so5196581qge.3 for ; Fri, 28 Mar 2014 17:27:27 -0700 (PDT) X-Received: by 10.140.102.135 with SMTP id w7mr6423577qge.29.1396052847608; Fri, 28 Mar 2014 17:27:27 -0700 (PDT) Received: from pike.twiddle.home.com (50-194-63-110-static.hfc.comcastbusiness.net. [50.194.63.110]) by mx.google.com with ESMTPSA id x5sm13105906qaj.9.2014.03.28.17.27.25 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Mar 2014 17:27:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 28 Mar 2014 17:27:09 -0700 Message-Id: <1396052834-26834-4-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1396052834-26834-1-git-send-email-rth@twiddle.net> References: <1396052834-26834-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::22c Cc: peter.maydell@linaro.org, aurelien@aurel32.net Subject: [Qemu-devel] [PATCH 3/8] 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Original-Sender: rth@twiddle.net X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 2607:f8b0:400c:c03::22e is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=fail header.i=@gmail.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 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. Signed-off-by: Peter Maydell Signed-off-by: Richard Henderson Reviewed-by: Alex Bennée --- 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 f832282..a0f9dff 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 60f06c5..727112d 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -143,6 +143,26 @@ static inline void tcg_out64(TCGContext *s, uint64_t v) s->code_ptr = p + 8; } +static inline void tcg_patch8(uint8_t *p, uint8_t v) +{ + memcpy(p, &v, sizeof(v)); +} + +static inline void tcg_patch16(uint8_t *p, uint16_t v) +{ + memcpy(p, &v, sizeof(v)); +} + +static inline void tcg_patch32(uint8_t *p, uint32_t v) +{ + memcpy(p, &v, sizeof(v)); +} + +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,