From patchwork Tue May 10 10:30:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 9056841 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 4A319BF29F for ; Tue, 10 May 2016 10:31:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AA8C020154 for ; Tue, 10 May 2016 10:31:07 +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 87187200DC for ; Tue, 10 May 2016 10:31:05 +0000 (UTC) Received: from localhost ([::1]:45659 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b04wS-0005Yp-NU for patchwork-qemu-devel@patchwork.kernel.org; Tue, 10 May 2016 06:31:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53530) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b04wB-0005Ua-UH for qemu-devel@nongnu.org; Tue, 10 May 2016 06:30:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b04wA-0007WA-Gs for qemu-devel@nongnu.org; Tue, 10 May 2016 06:30:47 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:56858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b04wA-0007W1-8O for qemu-devel@nongnu.org; Tue, 10 May 2016 06:30:46 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1b04w6-0002yp-QW; Tue, 10 May 2016 11:30:42 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 10 May 2016 11:30:42 +0100 Message-Id: <1462876242-16009-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 Subject: [Qemu-devel] [PATCH v2] 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.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= , patches@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" 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: Markus Armbruster --- v1->v2: use an explicit g_assert() rather than relying on the one hidden inside a SYS_BUS_DEVICE(dev) cast macro, as suggested by Paolo. Andreas: the SD card patch which had to go in before this patch is already in master, so you can just take this patch via your QOM tree without it causing any awkward ordering issues. hw/core/qdev.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index db41aa1..15b6713 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).) + */ + g_assert(object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) != NULL); bus = sysbus_get_default(); }