From patchwork Mon Apr 28 19:28: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: 29272 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f197.google.com (mail-qc0-f197.google.com [209.85.216.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 797C320B8D for ; Mon, 28 Apr 2014 19:32:29 +0000 (UTC) Received: by mail-qc0-f197.google.com with SMTP id i8sf26131457qcq.0 for ; Mon, 28 Apr 2014 12:32:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:mime-version: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:content-type :content-transfer-encoding; bh=B7f1UrmDLQ1+q1hWH2Tx+WRyUpCJYHdu0UmywIxjOX8=; b=IcLY5MOL3Efbv1b3AMXjqhcxK/J+aN/bY8PEqDXUGwjXpWaDnWo/L404FlcW2Yc3SY B6+20RvkrBPIPI0aAe+Xmg9OFcy/bERStdKi5mgcfFSWL+Wu6hgjvegtmLgu1y/fw0eG Y9FwcaED+1cvusAK6vrBGmuV7+rUEIPoFd5mJR7QIND8X5NIhe43XrZ8o0bcprO6KBww H65HRIWZ7xrNIOcggDAgo5EL47sCrek+dPtaCDJx276iJNHhGScGqSZVirf52T4NC7Cf Km/9Uk573JBQ15mPTaV9DZ7PEwubnOqRtGdY1LqvQQ6h3N7VgKqb3kliv0NcKFQcq4Ld L+Hw== X-Gm-Message-State: ALoCoQk12p2A4DvlKWn2psBdCInrTtiLRp7aO9vAvp+oOZ6uh5zXgrGg6F3Eykto0MbSjW3qms52 X-Received: by 10.224.147.72 with SMTP id k8mr13254416qav.5.1398713549067; Mon, 28 Apr 2014 12:32:29 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.37.164 with SMTP id r33ls2859692qgr.2.gmail; Mon, 28 Apr 2014 12:32:28 -0700 (PDT) X-Received: by 10.52.69.146 with SMTP id e18mr21469566vdu.15.1398713548943; Mon, 28 Apr 2014 12:32:28 -0700 (PDT) Received: from mail-vc0-x22f.google.com (mail-vc0-x22f.google.com [2607:f8b0:400c:c03::22f]) by mx.google.com with ESMTPS id ie18si3892295vec.175.2014.04.28.12.32.28 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 28 Apr 2014 12:32:28 -0700 (PDT) Received-SPF: none (google.com: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) client-ip=2607:f8b0:400c:c03::22f; Received: by mail-vc0-f175.google.com with SMTP id lh4so1970345vcb.6 for ; Mon, 28 Apr 2014 12:32:28 -0700 (PDT) X-Received: by 10.220.7.131 with SMTP id d3mr32762vcd.45.1398713548860; Mon, 28 Apr 2014 12:32:28 -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.221.72 with SMTP id ib8csp141542vcb; Mon, 28 Apr 2014 12:32:28 -0700 (PDT) X-Received: by 10.140.38.18 with SMTP id s18mr25626905qgs.88.1398713547708; Mon, 28 Apr 2014 12:32:27 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id d5si8343357qad.247.2014.04.28.12.32.27 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 28 Apr 2014 12:32:27 -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]:45624 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WerHv-0001M4-8C for patch@linaro.org; Mon, 28 Apr 2014 15:32:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60167) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WerEi-0005TY-LA for qemu-devel@nongnu.org; Mon, 28 Apr 2014 15:29:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WerEd-0008O7-SO for qemu-devel@nongnu.org; Mon, 28 Apr 2014 15:29:08 -0400 Received: from mail-qc0-x230.google.com ([2607:f8b0:400d:c01::230]:62121) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WerEd-0008Nv-LY for qemu-devel@nongnu.org; Mon, 28 Apr 2014 15:29:03 -0400 Received: by mail-qc0-f176.google.com with SMTP id x13so6527217qcv.7 for ; Mon, 28 Apr 2014 12:28:59 -0700 (PDT) X-Received: by 10.224.172.2 with SMTP id j2mr36334392qaz.83.1398713339107; Mon, 28 Apr 2014 12:28:59 -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 v9sm34841261qav.28.2014.04.28.12.28.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Apr 2014 12:28:58 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 28 Apr 2014 12:28:09 -0700 Message-Id: <1398713302-29657-4-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1398713302-29657-1-git-send-email-rth@twiddle.net> References: <1398713302-29657-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:c01::230 Cc: Peter Maydell Subject: [Qemu-devel] [PATCH v3 03/16] 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: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) 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. 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,