From patchwork Tue Jan 19 16:10:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8063701 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3E2079F1CC for ; Tue, 19 Jan 2016 16:18:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 984E120411 for ; Tue, 19 Jan 2016 16:18:23 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DCD60203F1 for ; Tue, 19 Jan 2016 16:18:22 +0000 (UTC) Received: from localhost ([::1]:37827 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYz7-0000jl-TC for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Jan 2016 11:18:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42332) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYry-0004Wy-3F for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aLYru-00021q-1D for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45238) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aLYrt-00021f-SV for qemu-devel@nongnu.org; Tue, 19 Jan 2016 11:10:53 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 97634A3274; Tue, 19 Jan 2016 16:10:53 +0000 (UTC) Received: from red.redhat.com (ovpn-113-211.phx2.redhat.com [10.3.113.211]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0JGAlYQ008625; Tue, 19 Jan 2016 11:10:53 -0500 From: Eric Blake To: qemu-devel@nongnu.org Date: Tue, 19 Jan 2016 09:10:20 -0700 Message-Id: <1453219845-30939-13-git-send-email-eblake@redhat.com> In-Reply-To: <1453219845-30939-1-git-send-email-eblake@redhat.com> References: <1453219845-30939-1-git-send-email-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: marcandre.lureau@redhat.com, armbru@redhat.com, Michael Roth Subject: [Qemu-devel] [PATCH v9 12/37] qapi: Don't cast Enum* to int* 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP C compilers are allowed to represent enums as a smaller type than int, if all enum values fit in the smaller type. There are even compiler flags that force the use of this smaller representation, and using them changes the ABI of a binary. Therefore, our generated code for visit_type_ENUM() (for all qapi enums) was wrong for casting Enum* to int* when calling visit_type_enum(). It appears that no one has been doing this for qemu, because if they had, we are potentially dereferencing beyond bounds or even risking a SIGBUS on platforms where unaligned pointer dereferencing is fatal. Better is to avoid the practice entirely, and just use the correct types. This matches the fix for alternate qapi types, done earlier in commit 0426d53 "qapi: Simplify visiting of alternate types", with generated code changing as: | void visit_type_GuestDiskBusType(Visitor *v, GuestDiskBusType *obj, const char *name, Error **errp) | { |- visit_type_enum(v, (int *)obj, GuestDiskBusType_lookup, "GuestDiskBusType", name, errp); |+ int tmp = *obj; |+ visit_type_enum(v, &tmp, GuestDiskBusType_lookup, "GuestDiskBusType", name, errp); |+ *obj = tmp; | } Signed-off-by: Eric Blake Reviewed-by: Marc-André Lureau --- v9: mention earlier commit id, enhance commit message v8: no change v7: rebase on typo fix v6: new patch --- scripts/qapi-visit.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py index 4a4f67d..6bd188b 100644 --- a/scripts/qapi-visit.py +++ b/scripts/qapi-visit.py @@ -178,12 +178,13 @@ out: def gen_visit_enum(name): - # FIXME cast from enum *obj to int * invalidly assumes enum is int return mcgen(''' void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name, Error **errp) { - visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp); + int tmp = *obj; + visit_type_enum(v, &tmp, %(c_name)s_lookup, "%(name)s", name, errp); + *obj = tmp; } ''', c_name=c_name(name), name=name)