From patchwork Tue Apr 1 20:53:28 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 27575 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f197.google.com (mail-pd0-f197.google.com [209.85.192.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id AA00E20AEB for ; Tue, 1 Apr 2014 20:54:55 +0000 (UTC) Received: by mail-pd0-f197.google.com with SMTP id fp1sf26138828pdb.0 for ; Tue, 01 Apr 2014 13:54:54 -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=jlqJjkLRJJnltl9nEvOM+JnAgUb1ReI84unyHrW8ROM=; b=OKHqnebWcpuG5CfjnuuSskMDo4i6N/H8lhHVKZOpASmjMfe9y2YkD6MNmYy9tP3G3B ZylkGLzPhzpNz03zuWYyU5La4pLqSRFrtfQB1PF1wg57SojRTjOr4nVXHRy+fIzI4A1N 6iFMtixJYkIWctlBO0TASdpeByNn0f7dJmuUaxtt4DiD/S6CelXWJSa+ogT2D0S7dzer 52fNYyl6vHM+J0eZdY4Twl99LgX9cfCrBFCXvoip4B74LXQdK660fei/PLDNEQOIxEht JhZj7j8OXEcBDfMJmsjqLvcmrcbg3ENUGyzjywEPxNQvvAQh7mAj/HIwgnltdi2agiQz TBKA== X-Gm-Message-State: ALoCoQmlv5tOVCPGvV88nR7XIxWFK+JeUNOgX3YgqyAXSLmhXP0J7aGLOMrbJcZTlgGeiEPj5klC X-Received: by 10.66.141.135 with SMTP id ro7mr14220207pab.28.1396385694197; Tue, 01 Apr 2014 13:54:54 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.33.197 with SMTP id j63ls124322qgj.4.gmail; Tue, 01 Apr 2014 13:54:54 -0700 (PDT) X-Received: by 10.52.120.6 with SMTP id ky6mr2593468vdb.38.1396385694081; Tue, 01 Apr 2014 13:54:54 -0700 (PDT) Received: from mail-vc0-x236.google.com (mail-vc0-x236.google.com [2607:f8b0:400c:c03::236]) by mx.google.com with ESMTPS id sj4si3938924vdc.120.2014.04.01.13.54.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 01 Apr 2014 13:54:54 -0700 (PDT) Received-SPF: neutral (google.com: 2607:f8b0:400c:c03::236 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::236; Received: by mail-vc0-f182.google.com with SMTP id ks9so10816298vcb.27 for ; Tue, 01 Apr 2014 13:54:54 -0700 (PDT) X-Received: by 10.52.33.176 with SMTP id s16mr1890654vdi.49.1396385693915; Tue, 01 Apr 2014 13:54:53 -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 v8csp269392vcv; Tue, 1 Apr 2014 13:54:53 -0700 (PDT) X-Received: by 10.224.98.197 with SMTP id r5mr38000172qan.47.1396385693359; Tue, 01 Apr 2014 13:54:53 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id g2si8111836qab.268.2014.04.01.13.54.53 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 01 Apr 2014 13:54:53 -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]:34339 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV5hs-0001qY-Jh for patch@linaro.org; Tue, 01 Apr 2014 16:54:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV5gu-0000pY-6X for qemu-devel@nongnu.org; Tue, 01 Apr 2014 16:53:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WV5gp-0007Vx-E9 for qemu-devel@nongnu.org; Tue, 01 Apr 2014 16:53:52 -0400 Received: from mail-qc0-x229.google.com ([2607:f8b0:400d:c01::229]:33028) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WV5gp-0007Vo-9E for qemu-devel@nongnu.org; Tue, 01 Apr 2014 16:53:47 -0400 Received: by mail-qc0-f169.google.com with SMTP id i17so11402823qcy.14 for ; Tue, 01 Apr 2014 13:53:46 -0700 (PDT) X-Received: by 10.140.23.8 with SMTP id 8mr6233531qgo.0.1396385626851; Tue, 01 Apr 2014 13:53:46 -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 g20sm36923963qaq.4.2014.04.01.13.53.45 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 01 Apr 2014 13:53:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 1 Apr 2014 13:53:28 -0700 Message-Id: <1396385614-19267-4-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1396385614-19267-1-git-send-email-rth@twiddle.net> References: <1396385614-19267-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::229 Cc: peter.maydell@linaro.org Subject: [Qemu-devel] [PATCH v2 3/9] 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::236 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. 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 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,