From patchwork Wed Mar 16 14:11:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 8600811 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D995AC0553 for ; Wed, 16 Mar 2016 14:11:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 16CDF202EC for ; Wed, 16 Mar 2016 14:11:22 +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 798E7202F2 for ; Wed, 16 Mar 2016 14:11:16 +0000 (UTC) Received: from localhost ([::1]:56554 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agCAN-0005Pk-RQ for patchwork-qemu-devel@patchwork.kernel.org; Wed, 16 Mar 2016 10:11:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40514) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agCAE-0005MW-Eq for qemu-devel@nongnu.org; Wed, 16 Mar 2016 10:11:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1agCAD-0003zg-IB for qemu-devel@nongnu.org; Wed, 16 Mar 2016 10:11:06 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:56200) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1agCAD-0003zS-C0 for qemu-devel@nongnu.org; Wed, 16 Mar 2016 10:11:05 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1agCAB-0000px-EO; Wed, 16 Mar 2016 14:11:03 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Date: Wed, 16 Mar 2016 14:11:02 +0000 Message-Id: <1458137462-30587-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.9.1 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Cc: Thomas Hanson , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [Qemu-devel] [PATCH] qdev_try_create(): Assert that devices we put onto the system bus are SysBusDevices 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 If qdev_try_create() is passed NULL for the bus, it will automatically put the newly created device onto the default system bus. However if the device is not actually a SysBusDevice then this will result in later crashes (for instance when running the monitor "info qtree" command) because code reasonably assumes that all devices on the system bus are system bus devices. Generally the mistake is that the calling code should create the object with object_new(TYPE_FOO) rather than qdev_create(NULL, TYPE_FOO); see commit 6749695eaaf346c1 for an example of fixing this bug. Assert in qdev_try_create() if the device isn't suitable to put on the system bus, so that this mistake results in failure earlier and more reliably. Signed-off-by: Peter Maydell Reviewed-by: Andreas Färber --- This needs to go in after http://patchwork.ozlabs.org/patch/597716/ as otherwise the bug fixed by that patch will become a 'make check' failure. hw/core/qdev.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index db41aa1..fb7db86 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -35,6 +35,7 @@ #include "qemu/error-report.h" #include "hw/hotplug.h" #include "hw/boards.h" +#include "hw/sysbus.h" #include "qapi-event.h" int qdev_hotplug = 0; @@ -161,6 +162,12 @@ DeviceState *qdev_try_create(BusState *bus, const char *type) } if (!bus) { + /* Assert that the device really is a SysBusDevice before + * we put it onto the sysbus. (Non-sysbus devices which aren't + * being put onto a bus should be created with object_new(TYPE_FOO), + * not qdev_create(NULL, TYPE_FOO).) + */ + SYS_BUS_DEVICE(dev); bus = sysbus_get_default(); }