From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691626; cv=none; d=zohomail.com; s=zohoarc; b=l/7UaRbE0BH78hU7jICAY5C1otapNpCUxgTl+DRLe2tl8rof75OAwzqD09sXYq2bvEtMw8/Hdu7+qRiIUW3fksLJHayvIIU71Cl6/W/vUgguB5g4mXbAYZBDd6f214tlevkEps5AffRpeNRLN958mlK6vSHhTlwXwtf/4ud1dVc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691626; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=iM/IEvnr121ZAVLNB7YYGElME2ngtP8fjYEvWEWPm9k=; b=cit/AyW12DSDT1HautK1CJbJUui2qWW+XYxIIgZnAvbVG3wnGwtqYeTzFKVniWSdfb//YR6j9rzN6m/XAnjWdM9d6Fv/9zjREnfO6X4iuhZJyf0zNintwpN4PJtam5Eu4TdmVc4kDSAKRlu6MxLWPrnvP5VBWRNLd1yzoPZV44w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691626179246.38747626199086; Fri, 15 Nov 2024 09:27:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC04c-000423-7X; Fri, 15 Nov 2024 12:25:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04a-00040z-7h for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:32 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04Y-0001Vi-HY for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:32 -0500 Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-G5myefujMZiqFFa5KeMbWA-1; Fri, 15 Nov 2024 12:25:28 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D07CB1977319 for ; Fri, 15 Nov 2024 17:25:27 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E9EC83003B74; Fri, 15 Nov 2024 17:25:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691529; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iM/IEvnr121ZAVLNB7YYGElME2ngtP8fjYEvWEWPm9k=; b=UNshaZCzNM3qz9s4NEOVVn6RMlTKDSAz6qVRYasEW6cIaB2fWCiTgHW2ZGxLLafP611Ree iaorQbrGukyvQLZa3Mr6hxCEYlS45Ojn6w95f+tgLhZy0hvY93zMF2j1kerEqFUzvM7zxm y4frgPdex917RM0HQXrF3sCxAU8h9DY= X-MC-Unique: G5myefujMZiqFFa5KeMbWA-1 X-Mimecast-MFC-AGG-ID: G5myefujMZiqFFa5KeMbWA From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 1/9] hw: eliminate qdev_try_new, isa_try_new & usb_try_new Date: Fri, 15 Nov 2024 17:25:13 +0000 Message-ID: <20241115172521.504102-2-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.658, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691626697116600 These functions all return NULL rather than asserting, if the requested type is not registered and also cannot be dynamically loaded. In several cases their usage is pointless, since the caller then just reports an error & exits anyway. Easier to just let qdev_new fail with &error_fatal. In other cases, the desired semantics are clearer to understand if the caller directly checks module_object_class_by_name(), before calling the regular qdev_new (or specialized equiv) function. Signed-off-by: Daniel P. Berrang=C3=A9 --- hw/core/qdev.c | 9 --------- hw/i386/pc.c | 22 ++++++++++------------ hw/isa/isa-bus.c | 5 ----- hw/s390x/s390-pci-bus.c | 6 +----- hw/usb/bus.c | 6 ++---- include/hw/isa/isa.h | 1 - include/hw/net/ne2000-isa.h | 16 ++++++---------- include/hw/qdev-core.h | 12 ------------ include/hw/usb.h | 5 ----- 9 files changed, 19 insertions(+), 63 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 5f13111b77..960a704a96 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -149,15 +149,6 @@ DeviceState *qdev_new(const char *name) return DEVICE(object_new(name)); } =20 -DeviceState *qdev_try_new(const char *name) -{ - ObjectClass *oc =3D module_object_class_by_name(name); - if (!oc) { - return NULL; - } - return DEVICE(object_new_with_class(oc)); -} - static QTAILQ_HEAD(, DeviceListener) device_listeners =3D QTAILQ_HEAD_INITIALIZER(device_listeners); =20 diff --git a/hw/i386/pc.c b/hw/i386/pc.c index f9147fecbd..d668970bee 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -596,9 +596,11 @@ static gboolean pc_init_ne2k_isa(ISABus *bus, NICInfo = *nd, Error **errp) "maximum number of ISA NE2000 devices exceeded"); return false; } - isa_ne2000_init(bus, ne2000_io[nb_ne2k], - ne2000_irq[nb_ne2k], nd); - nb_ne2k++; + if (module_object_class_by_name(TYPE_ISA_NE2000)) { + isa_ne2000_init(bus, ne2000_io[nb_ne2k], + ne2000_irq[nb_ne2k], nd); + nb_ne2k++; + } return true; } =20 @@ -1087,7 +1089,7 @@ static void pc_superio_init(ISABus *isa_bus, bool cre= ate_fdctrl, int i; DriveInfo *fd[MAX_FD]; qemu_irq *a20_line; - ISADevice *i8042, *port92, *vmmouse; + ISADevice *i8042, *port92, *vmmouse =3D NULL; =20 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS); parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS); @@ -1117,9 +1119,9 @@ static void pc_superio_init(ISABus *isa_bus, bool cre= ate_fdctrl, i8042 =3D isa_create_simple(isa_bus, TYPE_I8042); if (!no_vmport) { isa_create_simple(isa_bus, TYPE_VMPORT); - vmmouse =3D isa_try_new("vmmouse"); - } else { - vmmouse =3D NULL; + if (module_object_class_by_name("vmmouse")) { + vmmouse =3D isa_new("vmmouse"); + } } if (vmmouse) { object_property_set_link(OBJECT(vmmouse), TYPE_I8042, OBJECT(i8042= ), @@ -1163,11 +1165,7 @@ void pc_basic_device_init(struct PCMachineState *pcm= s, if (pcms->hpet_enabled) { qemu_irq rtc_irq; =20 - hpet =3D qdev_try_new(TYPE_HPET); - if (!hpet) { - error_report("couldn't create HPET device"); - exit(1); - } + hpet =3D qdev_new(TYPE_HPET); /* * For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-*, * use IRQ16~23, IRQ8 and IRQ2. If the user has already set diff --git a/hw/isa/isa-bus.c b/hw/isa/isa-bus.c index f1e0f14007..8aaf44a3ef 100644 --- a/hw/isa/isa-bus.c +++ b/hw/isa/isa-bus.c @@ -158,11 +158,6 @@ ISADevice *isa_new(const char *name) return ISA_DEVICE(qdev_new(name)); } =20 -ISADevice *isa_try_new(const char *name) -{ - return ISA_DEVICE(qdev_try_new(name)); -} - ISADevice *isa_create_simple(ISABus *bus, const char *name) { ISADevice *dev; diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c index 40b2567aa7..558f17d3ba 100644 --- a/hw/s390x/s390-pci-bus.c +++ b/hw/s390x/s390-pci-bus.c @@ -922,11 +922,7 @@ static S390PCIBusDevice *s390_pci_device_new(S390pciSt= ate *s, Error *local_err =3D NULL; DeviceState *dev; =20 - dev =3D qdev_try_new(TYPE_S390_PCI_DEVICE); - if (!dev) { - error_setg(errp, "zPCI device could not be created"); - return NULL; - } + dev =3D qdev_new(TYPE_S390_PCI_DEVICE); =20 if (!object_property_set_str(OBJECT(dev), "target", target, &local_err= )) { object_unparent(OBJECT(dev)); diff --git a/hw/usb/bus.c b/hw/usb/bus.c index bfab2807d7..002f7737e4 100644 --- a/hw/usb/bus.c +++ b/hw/usb/bus.c @@ -394,7 +394,6 @@ void usb_claim_port(USBDevice *dev, Error **errp) { USBBus *bus =3D usb_bus_from_device(dev); USBPort *port; - USBDevice *hub; =20 assert(dev->port =3D=3D NULL); =20 @@ -412,9 +411,8 @@ void usb_claim_port(USBDevice *dev, Error **errp) } else { if (bus->nfree =3D=3D 1 && strcmp(object_get_typename(OBJECT(dev))= , "usb-hub") !=3D 0) { /* Create a new hub and chain it on */ - hub =3D usb_try_new("usb-hub"); - if (hub) { - usb_realize_and_unref(hub, bus, NULL); + if (module_object_class_by_name("usb-hub")) { + usb_realize_and_unref(usb_new("usb-hub"), bus, NULL); } } if (bus->nfree =3D=3D 0) { diff --git a/include/hw/isa/isa.h b/include/hw/isa/isa.h index 40d6224a4e..8475120849 100644 --- a/include/hw/isa/isa.h +++ b/include/hw/isa/isa.h @@ -81,7 +81,6 @@ IsaDma *isa_bus_get_dma(ISABus *bus, int nchan); */ qemu_irq isa_bus_get_irq(ISABus *bus, unsigned irqnum); ISADevice *isa_new(const char *name); -ISADevice *isa_try_new(const char *name); bool isa_realize_and_unref(ISADevice *dev, ISABus *bus, Error **errp); ISADevice *isa_create_simple(ISABus *bus, const char *name); =20 diff --git a/include/hw/net/ne2000-isa.h b/include/hw/net/ne2000-isa.h index 73bae10ad1..aeb3e2bfd5 100644 --- a/include/hw/net/ne2000-isa.h +++ b/include/hw/net/ne2000-isa.h @@ -20,17 +20,13 @@ static inline ISADevice *isa_ne2000_init(ISABus *bus, int base, int irq, NICInfo *nd) { - ISADevice *d; + ISADevice *d =3D isa_new(TYPE_ISA_NE2000); + DeviceState *dev =3D DEVICE(d); =20 - d =3D isa_try_new(TYPE_ISA_NE2000); - if (d) { - DeviceState *dev =3D DEVICE(d); - - qdev_prop_set_uint32(dev, "iobase", base); - qdev_prop_set_uint32(dev, "irq", irq); - qdev_set_nic_properties(dev, nd); - isa_realize_and_unref(d, bus, &error_fatal); - } + qdev_prop_set_uint32(dev, "iobase", base); + qdev_prop_set_uint32(dev, "irq", irq); + qdev_set_nic_properties(dev, nd); + isa_realize_and_unref(d, bus, &error_fatal); return d; } =20 diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 5be9844412..020417d027 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -443,18 +443,6 @@ compat_props_add(GPtrArray *arr, */ DeviceState *qdev_new(const char *name); =20 -/** - * qdev_try_new: Try to create a device on the heap - * @name: device type to create - * - * This is like qdev_new(), except it returns %NULL when type @name - * does not exist, rather than asserting. - * - * Return: a derived DeviceState object with a reference count of 1 or - * NULL if type @name does not exist. - */ -DeviceState *qdev_try_new(const char *name); - /** * qdev_is_realized() - check if device is realized * @dev: The device to check. diff --git a/include/hw/usb.h b/include/hw/usb.h index d46d96779a..bb778cb844 100644 --- a/include/hw/usb.h +++ b/include/hw/usb.h @@ -584,11 +584,6 @@ static inline USBDevice *usb_new(const char *name) return USB_DEVICE(qdev_new(name)); } =20 -static inline USBDevice *usb_try_new(const char *name) -{ - return USB_DEVICE(qdev_try_new(name)); -} - static inline bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Erro= r **errp) { return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp); --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691662; cv=none; d=zohomail.com; s=zohoarc; b=MNM9ctcnENqMOz59yaW3ocBYyEp7JG08EQy+ebEt2bxIR4V2MDJJ8L+63EyNxssI1SKPd0KNTdqeZze7H8yzTzbYvlCo55jyJpNRR4d5hd5PfLok1j1Gm3gNRl8W7cLyHr0EtRMx301U4qTxFvIXmlV1YZ4WZSiOwCaUX4nPju8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691662; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=LGGf/6SXD4klp11oD3a3PZ0lZYftk0LuT8kkItR+FyY=; b=IFFu8T/0DbHy/+X0leyOaS8btFX42xqWszBdFWuBaimDJd9JcLiM0Zj/JDs27f1w2tDYiiTEo8/dBAe82FrcZ6Q0J2U8k5oZu9gSgJw1m9WorcXorwpkzw7Ff5gz7kFmmDidxUHxw9SSX5z2I3hdgWbXFy7x2SmCBFyhjbnRG5o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691662535464.72286295151287; Fri, 15 Nov 2024 09:27:42 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC04d-00042F-QK; Fri, 15 Nov 2024 12:25:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04c-000425-U8 for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04b-0001W0-DS for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:34 -0500 Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-na-wT9zFN4-vDBaEF4b5YQ-1; Fri, 15 Nov 2024 12:25:31 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4B081197731C for ; Fri, 15 Nov 2024 17:25:30 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8B9573003B71; Fri, 15 Nov 2024 17:25:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691532; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LGGf/6SXD4klp11oD3a3PZ0lZYftk0LuT8kkItR+FyY=; b=IqPtp3bGy5goLt+qKNdg80E5JwyTIbdFNeKrEcFNdfLX8UQyrduudtE64ZiCg23aHPXQx3 LiN9hscvSpE5WY7E5aMtz3ib1xyFVAWxm1NMGRW36WWPwlK9cKtJN445KJe9nf0uR6KlRt 1IDRA0sXhG/ksay1LFAQ4lZZKyRs9XM= X-MC-Unique: na-wT9zFN4-vDBaEF4b5YQ-1 X-Mimecast-MFC-AGG-ID: na-wT9zFN4-vDBaEF4b5YQ From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 2/9] qom: refactor checking abstract property when creating instances Date: Fri, 15 Nov 2024 17:25:14 +0000 Message-ID: <20241115172521.504102-3-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.658, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691664659116600 Push an Error object into object_initialize_with_type, so that reporting of attempts to create an abstract type is handled at the lowest level. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Peter Xu --- qom/object.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/qom/object.c b/qom/object.c index 9edc06d391..9632a894ee 100644 --- a/qom/object.c +++ b/qom/object.c @@ -559,14 +559,20 @@ static void object_class_property_init_all(Object *ob= j) } } =20 -static void object_initialize_with_type(Object *obj, size_t size, TypeImpl= *type) +static bool object_initialize_with_type(Object *obj, size_t size, + TypeImpl *type, Error **errp) { type_initialize(type); =20 g_assert(type->instance_size >=3D sizeof(Object)); - g_assert(type->abstract =3D=3D false); g_assert(size >=3D type->instance_size); =20 + if (type->abstract) { + error_setg(errp, "Abstract type '%s' cannot be instantiated", + type->name); + return false; + } + memset(obj, 0, type->instance_size); obj->class =3D type->class; object_ref(obj); @@ -575,13 +581,15 @@ static void object_initialize_with_type(Object *obj, = size_t size, TypeImpl *type NULL, object_property_free); object_init_with_type(obj, type); object_post_init_with_type(obj, type); + + return true; } =20 void object_initialize(void *data, size_t size, const char *typename) { TypeImpl *type =3D type_get_or_load_by_name(typename, &error_fatal); =20 - object_initialize_with_type(data, size, type); + object_initialize_with_type(data, size, type, &error_abort); } =20 bool object_initialize_child_with_props(Object *parentobj, @@ -753,7 +761,7 @@ typedef union { } qemu_max_align_t; #endif =20 -static Object *object_new_with_type(Type type) +static Object *object_new_with_type(Type type, Error **errp) { Object *obj; size_t size, align; @@ -777,7 +785,10 @@ static Object *object_new_with_type(Type type) obj_free =3D qemu_vfree; } =20 - object_initialize_with_type(obj, size, type); + if (!object_initialize_with_type(obj, size, type, errp)) { + obj_free(obj); + return NULL; + } obj->free =3D obj_free; =20 return obj; @@ -785,14 +796,14 @@ static Object *object_new_with_type(Type type) =20 Object *object_new_with_class(ObjectClass *klass) { - return object_new_with_type(klass->type); + return object_new_with_type(klass->type, &error_abort); } =20 Object *object_new(const char *typename) { TypeImpl *ti =3D type_get_or_load_by_name(typename, &error_fatal); =20 - return object_new_with_type(ti); + return object_new_with_type(ti, &error_abort); } =20 =20 @@ -829,11 +840,10 @@ Object *object_new_with_propv(const char *typename, return NULL; } =20 - if (object_class_is_abstract(klass)) { - error_setg(errp, "object type '%s' is abstract", typename); + obj =3D object_new_with_type(klass->type, errp); + if (!obj) { return NULL; } - obj =3D object_new_with_type(klass->type); =20 if (!object_set_propv(obj, errp, vargs)) { goto error; --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691557; cv=none; d=zohomail.com; s=zohoarc; b=Q7kiwC1kpyOnJjW+onTK6hCjdxTV3VEKmDzswIC46zpMAwV4Rwn+3GmLOcMbIvPfowuhZonJn4aTUOs9X4FioQqzmw9xviYMLyTzCIeDuJCYs3CnmVla6zKj1UJrdD21pSwgIX2dA04qbPZurSi7qoHzoopTNnUr5cfBdMbaVgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691557; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cgD9ehYu1Tjdp6JJALrItoKNs/50PojM5Ut2/ekClU8=; b=F1TJveZa6ZwCPTH1Esw1ASolTI+KkXNSXfLMszMhFfcVO8L4ZnIR4DQPMf117C6G/YUt/IpAvtb25Q+gl3kGl6XnP8i1IdWGO0gpp621pELTy8OusKIfUAV1h5awNEGhMDJdxpvd/7K/HXHxMvYaHzLd5fETxay+5Ph8x5bwz1E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691557070140.46663678271193; Fri, 15 Nov 2024 09:25:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC04k-00043B-1V; Fri, 15 Nov 2024 12:25:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04f-00042f-I2 for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:37 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04d-0001WJ-NF for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:37 -0500 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-307-dJgSnjdRNA-3jkhsTJWWgA-1; Fri, 15 Nov 2024 12:25:33 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A30211955F42 for ; Fri, 15 Nov 2024 17:25:32 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D4AD13003B74; Fri, 15 Nov 2024 17:25:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691534; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cgD9ehYu1Tjdp6JJALrItoKNs/50PojM5Ut2/ekClU8=; b=W1nCyjgS9Antc71kDz0lU183T+/DKFw15E10OM5ciltLAW8EMjlCrxvXKITL1luD/0E8eQ CnQHKUHPrf9X97MyQHAePxwEO7PKVWd/m9fuAvQIyWI7LNgdUJ8O9wpzb7jwpwW2acexrH muwvVnTpckcWXKWRrIqmvWJLXTHOWmM= X-MC-Unique: dJgSnjdRNA-3jkhsTJWWgA-1 X-Mimecast-MFC-AGG-ID: dJgSnjdRNA-3jkhsTJWWgA From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 3/9] qom: allow failure of object_new_with_class Date: Fri, 15 Nov 2024 17:25:15 +0000 Message-ID: <20241115172521.504102-4-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.658, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691558206116600 Since object_new_with_class() accepts a non-const parameter for the class, callers should be prepared for failures from unexpected input. Add an Error parameter for this and make callers check. If the caller does not already have an Error parameter, it is satisfactory to use &error_abort if the class parameter choice is not driven by untrusted user input. This conversion allows removal of any object_class_is_abstract() checks immediately before object_new_with_class(). Reviewed-by: Peter Xu Signed-off-by: Daniel P. Berrang=C3=A9 --- accel/accel-user.c | 4 +++- include/qom/object.h | 9 +++++++-- net/net.c | 3 ++- qom/object.c | 4 ++-- qom/object_interfaces.c | 7 +++---- qom/qom-qmp-cmds.c | 11 ++++++----- system/vl.c | 6 ++++-- target/i386/cpu-apic.c | 8 +++++++- target/i386/cpu-sysemu.c | 11 ++++++++--- target/i386/cpu.c | 4 ++-- target/s390x/cpu_models_sysemu.c | 7 +++++-- 11 files changed, 49 insertions(+), 25 deletions(-) diff --git a/accel/accel-user.c b/accel/accel-user.c index 22b6a1a1a8..df673ec0e4 100644 --- a/accel/accel-user.c +++ b/accel/accel-user.c @@ -9,6 +9,7 @@ =20 #include "qemu/osdep.h" #include "qemu/accel.h" +#include "qapi/error.h" =20 AccelState *current_accel(void) { @@ -18,7 +19,8 @@ AccelState *current_accel(void) AccelClass *ac =3D accel_find("tcg"); =20 g_assert(ac !=3D NULL); - accel =3D ACCEL(object_new_with_class(OBJECT_CLASS(ac))); + accel =3D ACCEL(object_new_with_class(OBJECT_CLASS(ac), + &error_abort)); } return accel; } diff --git a/include/qom/object.h b/include/qom/object.h index 43c135984a..11ee472719 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -606,14 +606,19 @@ struct InterfaceClass /** * object_new_with_class: * @klass: The class to instantiate. + * @errp: pointer to be filled with error details on failure * * This function will initialize a new object using heap allocated memory. * The returned object has a reference count of 1, and will be freed when * the last reference is dropped. * - * Returns: The newly allocated and instantiated object. + * If an instance of @klass is not permitted to be instantiated, an + * error will be raised. This can happen if @klass is abstract. + * + * Returns: The newly allocated and instantiated object, or NULL + * on error. */ -Object *object_new_with_class(ObjectClass *klass); +Object *object_new_with_class(ObjectClass *klass, Error **errp); =20 /** * object_new: diff --git a/net/net.c b/net/net.c index 7ef6885876..fbbfe602a4 100644 --- a/net/net.c +++ b/net/net.c @@ -948,7 +948,8 @@ GPtrArray *qemu_get_nic_models(const char *device_type) * create this property during instance_init, so we have to cr= eate * a temporary instance here to be able to check it. */ - Object *obj =3D object_new_with_class(OBJECT_CLASS(dc)); + Object *obj =3D object_new_with_class(OBJECT_CLASS(dc), + &error_abort); if (object_property_find(obj, "netdev")) { g_ptr_array_add(nic_models, (gpointer)name); } diff --git a/qom/object.c b/qom/object.c index 9632a894ee..ad5b3b9582 100644 --- a/qom/object.c +++ b/qom/object.c @@ -794,9 +794,9 @@ static Object *object_new_with_type(Type type, Error **= errp) return obj; } =20 -Object *object_new_with_class(ObjectClass *klass) +Object *object_new_with_class(ObjectClass *klass, Error **errp) { - return object_new_with_type(klass->type, &error_abort); + return object_new_with_type(klass->type, errp); } =20 Object *object_new(const char *typename) diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c index 1a6f29c053..967b906755 100644 --- a/qom/object_interfaces.c +++ b/qom/object_interfaces.c @@ -102,13 +102,12 @@ Object *user_creatable_add_type(const char *type, con= st char *id, return NULL; } =20 - if (object_class_is_abstract(klass)) { - error_setg(errp, "object type '%s' is abstract", type); + assert(qdict); + obj =3D object_new_with_class(klass, errp); + if (!obj) { return NULL; } =20 - assert(qdict); - obj =3D object_new_with_class(klass); object_set_properties_from_qdict(obj, qdict, v, &local_err); if (local_err) { goto out; diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c index 46e4562300..4a8e269fef 100644 --- a/qom/qom-qmp-cmds.c +++ b/qom/qom-qmp-cmds.c @@ -134,14 +134,15 @@ ObjectPropertyInfoList *qmp_device_list_properties(co= nst char *typename, return NULL; } =20 - if (!object_class_dynamic_cast(klass, TYPE_DEVICE) - || object_class_is_abstract(klass)) { - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", - "a non-abstract device type"); + if (!object_class_dynamic_cast(klass, TYPE_DEVICE)) { + error_setg(errp, "Object '%s' is not a device type", typename); return NULL; } =20 - obj =3D object_new_with_class(klass); + obj =3D object_new_with_class(klass, errp); + if (!obj) { + return NULL; + } =20 object_property_iter_init(&iter, obj); while ((prop =3D object_property_iter_next(&iter))) { diff --git a/system/vl.c b/system/vl.c index d217b3d64d..f4eec7f35c 100644 --- a/system/vl.c +++ b/system/vl.c @@ -2117,7 +2117,8 @@ static void qemu_create_machine(QDict *qdict) MachineClass *machine_class =3D select_machine(qdict, &error_fatal); object_set_machine_compat_props(machine_class->compat_props); =20 - current_machine =3D MACHINE(object_new_with_class(OBJECT_CLASS(machine= _class))); + current_machine =3D MACHINE(object_new_with_class(OBJECT_CLASS(machine= _class), + &error_fatal)); object_property_add_child(object_get_root(), "machine", OBJECT(current_machine)); object_property_add_child(container_get(OBJECT(current_machine), @@ -2327,7 +2328,8 @@ static int do_configure_accelerator(void *opaque, Qem= uOpts *opts, Error **errp) } goto bad; } - accel =3D ACCEL(object_new_with_class(OBJECT_CLASS(ac))); + accel =3D ACCEL(object_new_with_class(OBJECT_CLASS(ac), + &error_fatal)); object_apply_compat_props(OBJECT(accel)); qemu_opt_foreach(opts, accelerator_set_property, accel, diff --git a/target/i386/cpu-apic.c b/target/i386/cpu-apic.c index d397ec94dc..8a518c50c7 100644 --- a/target/i386/cpu-apic.c +++ b/target/i386/cpu-apic.c @@ -43,12 +43,18 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp) { APICCommonState *apic; APICCommonClass *apic_class =3D apic_get_class(errp); + Object *apicobj; =20 if (!apic_class) { return; } =20 - cpu->apic_state =3D DEVICE(object_new_with_class(OBJECT_CLASS(apic_cla= ss))); + apicobj =3D object_new_with_class(OBJECT_CLASS(apic_class), + errp); + if (!apicobj) { + return; + } + cpu->apic_state =3D DEVICE(apicobj); object_property_add_child(OBJECT(cpu), "lapic", OBJECT(cpu->apic_state)); object_unref(OBJECT(cpu->apic_state)); diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c index 227ac021f6..612ff09e57 100644 --- a/target/i386/cpu-sysemu.c +++ b/target/i386/cpu-sysemu.c @@ -156,15 +156,20 @@ static X86CPU *x86_cpu_from_model(const char *model, = QObject *props, { X86CPU *xc =3D NULL; X86CPUClass *xcc; + Object *xcobj; Error *err =3D NULL; =20 xcc =3D X86_CPU_CLASS(cpu_class_by_name(TYPE_X86_CPU, model)); if (xcc =3D=3D NULL) { - error_setg(&err, "CPU model '%s' not found", model); - goto out; + error_setg(errp, "CPU model '%s' not found", model); + return NULL; } =20 - xc =3D X86_CPU(object_new_with_class(OBJECT_CLASS(xcc))); + xcobj =3D object_new_with_class(OBJECT_CLASS(xcc), errp); + if (!xcobj) { + return NULL; + } + xc =3D X86_CPU(xcobj); if (props) { object_apply_props(OBJECT(xc), props, props_arg_name, &err); if (err) { diff --git a/target/i386/cpu.c b/target/i386/cpu.c index 3725dbbc4b..d4315df29f 100644 --- a/target/i386/cpu.c +++ b/target/i386/cpu.c @@ -5973,7 +5973,7 @@ static GSList *get_sorted_cpu_model_list(void) =20 static char *x86_cpu_class_get_model_id(X86CPUClass *xc) { - Object *obj =3D object_new_with_class(OBJECT_CLASS(xc)); + Object *obj =3D object_new_with_class(OBJECT_CLASS(xc), &error_abort); char *r =3D object_property_get_str(obj, "model-id", &error_abort); object_unref(obj); return r; @@ -6071,7 +6071,7 @@ static void x86_cpu_class_check_missing_features(X86C= PUClass *xcc, return; } =20 - xc =3D X86_CPU(object_new_with_class(OBJECT_CLASS(xcc))); + xc =3D X86_CPU(object_new_with_class(OBJECT_CLASS(xcc), &error_abort)); =20 x86_cpu_expand_features(xc, &err); if (err) { diff --git a/target/s390x/cpu_models_sysemu.c b/target/s390x/cpu_models_sys= emu.c index f6df691b66..7fe3093056 100644 --- a/target/s390x/cpu_models_sysemu.c +++ b/target/s390x/cpu_models_sysemu.c @@ -69,7 +69,7 @@ static void create_cpu_model_list(ObjectClass *klass, voi= d *opaque) if (cpu_list_data->model) { Object *obj; S390CPU *sc; - obj =3D object_new_with_class(klass); + obj =3D object_new_with_class(klass, &error_abort); sc =3D S390_CPU(obj); if (sc->model) { info->has_unavailable_features =3D true; @@ -116,7 +116,10 @@ static void cpu_model_from_info(S390CPUModel *model, c= onst CpuModelInfo *info, error_setg(errp, "The CPU definition '%s' requires KVM", info->nam= e); return; } - obj =3D object_new_with_class(oc); + obj =3D object_new_with_class(oc, errp); + if (!obj) { + return; + } cpu =3D S390_CPU(obj); =20 if (!cpu->model) { --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691583; cv=none; d=zohomail.com; s=zohoarc; b=j+tRpFXqxET/xMaXh+Hfuv6Axzeawb49W+mTnHpAyw8k4dxuunALCtqR/18/+eUNYsd5vdwM2wHsCeoqM1yBEPSq5wIAChbP30ZljoX8FKWogjuIW4m060fAbTX3CUFcHHu2sWb3cTNtTp2yz+lnUQLpnZzB73qY8Ews1PvVifM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691583; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=I4tw7doBeG+8gbO5Vlw4cl4cj4yji98/v8xm8WxYBAA=; b=Jx7/6J25pAwVkj79G86mKQHX69aGfXbBpxT4iftINyWgYS3dyWc0oNs2iITdMdZt2LheJ8CKozgVAQqV2J0vAoXuEEst899DIwhPjF+opjCPzFlilH149WcsuqqBnqrOKzdzX9wMLwu+TTM3qtCfvEZweCQZY9qpX06npWTB1MI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17316915828069.7430347490357; Fri, 15 Nov 2024 09:26:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC04q-0004CY-0J; Fri, 15 Nov 2024 12:25:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04i-00042s-7z for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:40 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04g-0001WY-En for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:39 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-80-enFzytpGN7SA71PYQDos8Q-1; Fri, 15 Nov 2024 12:25:36 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4F5DC195FE24 for ; Fri, 15 Nov 2024 17:25:35 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3D3263003B71; Fri, 15 Nov 2024 17:25:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691537; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I4tw7doBeG+8gbO5Vlw4cl4cj4yji98/v8xm8WxYBAA=; b=UHv4mO8kIjbRSucse5D6Lty0mI/EYiVK5naj1L6j+x+MTruVwZRSzQKNOGEVVzwnQ8LPoT LCJD4NpNhN5ULsvEmi5cbmJcGZ6Eo7FDqKgkDtRSo16zUaDgWUyh95AjH7EETs52aBCx3R TFbAZD39fDcOkXaAxky3IYa32vuNWKo= X-MC-Unique: enFzytpGN7SA71PYQDos8Q-1 X-Mimecast-MFC-AGG-ID: enFzytpGN7SA71PYQDos8Q From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 4/9] qom: introduce object_new_dynamic() Date: Fri, 15 Nov 2024 17:25:16 +0000 Message-ID: <20241115172521.504102-5-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691584640116600 object_new() has a failure scenario where it will assert() if given an abstract type. Callers which are creating objects based on user input, or unknown/untrusted type names, must manually check the result of object_class_is_abstract() before calling object_new() to propagate an Error, instead of asserting. Introduce a object_new_dynamic() method which is a counterpart to object_new() that directly returns an Error, instead of asserting. This new method is to be used where the typename is specified dynamically by code separate from the immediate caller. Reviewed-by: Peter Xu Signed-off-by: Daniel P. Berrang=C3=A9 --- include/qom/object.h | 27 +++++++++++++++++++++++++++ qom/object.c | 9 +++++++++ 2 files changed, 36 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index 11ee472719..4fc01336c4 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -624,14 +624,41 @@ Object *object_new_with_class(ObjectClass *klass, Err= or **errp); * object_new: * @typename: The name of the type of the object to instantiate. * + * This method should be used where @typename is statically specified + * from a const string at build time, where the caller does not expect + * failure to be possible. + * * This function will initialize a new object using heap allocated memory. * The returned object has a reference count of 1, and will be freed when * the last reference is dropped. * + * If an instance of @typename is not permitted to be instantiated, an + * assert will be raised. This can happen if @typename is abstract. + * * Returns: The newly allocated and instantiated object. */ Object *object_new(const char *typename); =20 +/** + * object_new_dynamic: + * @typename: The name of the type of the object to instantiate. + * @errp: pointer to be filled with error details on failure + * + * This method should be used where @typename is dynamically chosen + * at runtime, which has the possibility of unexpected choices leading + * to failures. + * + * This function will initialize a new object using heap allocated memory. + * The returned object has a reference count of 1, and will be freed when + * the last reference is dropped. + * + * If an instance of @typename is not permitted to be instantiated, an + * error will be raised. This can happen if @typename is abstract. + * + * Returns: The newly allocated and instantiated object. + */ +Object *object_new_dynamic(const char *typename, Error **errp); + /** * object_new_with_props: * @typename: The name of the type of the object to instantiate. diff --git a/qom/object.c b/qom/object.c index ad5b3b9582..42ef40a1fd 100644 --- a/qom/object.c +++ b/qom/object.c @@ -806,6 +806,15 @@ Object *object_new(const char *typename) return object_new_with_type(ti, &error_abort); } =20 +Object *object_new_dynamic(const char *typename, Error **errp) +{ + TypeImpl *ti =3D type_get_or_load_by_name(typename, errp); + if (!ti) { + return NULL; + } + + return object_new_with_type(ti, errp); +} =20 Object *object_new_with_props(const char *typename, Object *parent, --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691662; cv=none; d=zohomail.com; s=zohoarc; b=Uoxc5oJq6ylDDm0LFDW/OPii74Ce0uC5tF+xtMvGLuKnVJTENujdru876dCj9ZgbuLgUiApedLLjYU4nK6l2vcywzGkacfjYpNJmd5ilfbqyDnqWch8CUb+qbBP49WGF38DSr/pW2EJ2Feb3Kd6IrvJCtznNQRfjApG15JnrTUc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691662; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cCMxcEMzK+ORsvpYHHKKb5uHv0ldXCf9Z/VSowzif5A=; b=YshA5EnADw0D2wLzZ5XHwT+euAA75Uimz2JjgBScOicXCm0MEMsiJ+4kyWOT/aqIB8yxnz2Fq2ArIu389XMUCD8u0VY7JvWRfAOf0+gBgjL9PhHjaxxglETn9QTf91TGUlwCi657i/RMMWTtEOm9RHP8xq/cOeifHyKXvI7T/uA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 173169166238250.471604075320556; Fri, 15 Nov 2024 09:27:42 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC04y-0004Kg-LR; Fri, 15 Nov 2024 12:25:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04m-0004CK-SG for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:46 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04j-0001Wz-QS for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:44 -0500 Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-6SDcBpvVM-qenZQMLsakHQ-1; Fri, 15 Nov 2024 12:25:38 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A5E581955D62 for ; Fri, 15 Nov 2024 17:25:37 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C59593003B71; Fri, 15 Nov 2024 17:25:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691541; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cCMxcEMzK+ORsvpYHHKKb5uHv0ldXCf9Z/VSowzif5A=; b=P+CVVDKu4MKaMIabl7IVrC6DnbVNL2HFDdAVTXKdovrjwg/sDgHXwtcMOw9I5GDtaEe6lo GkiXnr9tgDfS4F+iF7pctOgUNvYIWMnLQN9PO7vc0S+TRBhqAX1Zq8DD3Eep6zTwFkcP8K 3XyrZ0TPb9t6X1fXcvyGkk6mTAYwj0k= X-MC-Unique: 6SDcBpvVM-qenZQMLsakHQ-1 X-Mimecast-MFC-AGG-ID: 6SDcBpvVM-qenZQMLsakHQ From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 5/9] convert code to object_new_dynamic() where appropriate Date: Fri, 15 Nov 2024 17:25:17 +0000 Message-ID: <20241115172521.504102-6-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691662872116600 In cases where object_new() is not being passed a static, const string, the caller cannot be sure what type they are instantiating. There is a risk that instantiation could fail, if it is an abstract type. Convert such cases over to use object_new_dynamic() such that they are forced to expect failure. In some cases failure can be easily propagated, but in others using &error_abort or &error_fatal will maintain existnig behaviour. Reviewed-by: Peter Xu Signed-off-by: Daniel P. Berrang=C3=A9 --- chardev/char.c | 6 +++++- hw/arm/aspeed.c | 4 ++-- hw/arm/exynos4210.c | 3 ++- hw/arm/highbank.c | 2 +- hw/arm/integratorcp.c | 2 +- hw/arm/mps3r.c | 3 ++- hw/arm/realview.c | 3 ++- hw/arm/sbsa-ref.c | 3 ++- hw/arm/versatilepb.c | 2 +- hw/arm/vexpress.c | 2 +- hw/arm/virt.c | 6 ++++-- hw/arm/xilinx_zynq.c | 3 ++- hw/core/bus.c | 2 +- hw/core/cpu-common.c | 2 +- hw/core/qdev.c | 2 +- hw/i386/x86-common.c | 5 ++++- hw/i386/xen/xen-pvh.c | 2 +- hw/intc/xics.c | 5 ++++- hw/mips/cps.c | 3 ++- hw/pci-host/pnv_phb.c | 5 ++++- hw/ppc/e500.c | 2 +- hw/ppc/pnv.c | 4 ++-- hw/ppc/pnv_core.c | 5 ++++- hw/ppc/spapr.c | 2 +- hw/ppc/spapr_cpu_core.c | 5 ++++- hw/ppc/spapr_drc.c | 2 +- hw/s390x/s390-virtio-ccw.c | 8 +++++++- hw/sparc/leon3.c | 2 +- hw/sparc/sun4m.c | 2 +- hw/sparc64/sparc64.c | 2 +- hw/vfio/common.c | 6 +++++- hw/vfio/container.c | 7 ++++++- qom/qom-qmp-cmds.c | 5 ++++- target/arm/arm-qmp-cmds.c | 5 ++++- target/loongarch/loongarch-qmp-cmds.c | 5 ++++- target/mips/cpu.c | 2 +- target/riscv/riscv-qmp-cmds.c | 5 ++++- target/xtensa/cpu.c | 2 +- tests/unit/check-qom-interface.c | 3 ++- tests/unit/test-smp-parse.c | 20 ++++++++++---------- 40 files changed, 107 insertions(+), 52 deletions(-) diff --git a/chardev/char.c b/chardev/char.c index a1722aa076..5951b8bef5 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -996,7 +996,11 @@ static Chardev *chardev_new(const char *id, const char= *typename, assert(g_str_has_prefix(typename, "chardev-")); assert(id); =20 - obj =3D object_new(typename); + obj =3D object_new_dynamic(typename, errp); + if (!obj) { + return NULL; + } + chr =3D CHARDEV(obj); chr->handover_yank_instance =3D handover_yank_instance; chr->label =3D g_strdup(id); diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c index 6ca145362c..71196b0a4b 100644 --- a/hw/arm/aspeed.c +++ b/hw/arm/aspeed.c @@ -385,7 +385,7 @@ static void aspeed_machine_init(MachineState *machine) DriveInfo *emmc0 =3D NULL; bool boot_emmc; =20 - bmc->soc =3D ASPEED_SOC(object_new(amc->soc_name)); + bmc->soc =3D ASPEED_SOC(object_new_dynamic(amc->soc_name, &error_fatal= )); object_property_add_child(OBJECT(machine), "soc", OBJECT(bmc->soc)); object_unref(OBJECT(bmc->soc)); sc =3D ASPEED_SOC_GET_CLASS(bmc->soc); @@ -1594,7 +1594,7 @@ static void aspeed_minibmc_machine_init(MachineState = *machine) sysclk =3D clock_new(OBJECT(machine), "SYSCLK"); clock_set_hz(sysclk, SYSCLK_FRQ); =20 - bmc->soc =3D ASPEED_SOC(object_new(amc->soc_name)); + bmc->soc =3D ASPEED_SOC(object_new_dynamic(amc->soc_name, &error_fatal= )); object_property_add_child(OBJECT(machine), "soc", OBJECT(bmc->soc)); object_unref(OBJECT(bmc->soc)); qdev_connect_clock_in(DEVICE(bmc->soc), "sysclk", sysclk); diff --git a/hw/arm/exynos4210.c b/hw/arm/exynos4210.c index e3f1de2631..b966b0cd06 100644 --- a/hw/arm/exynos4210.c +++ b/hw/arm/exynos4210.c @@ -554,7 +554,8 @@ static void exynos4210_realize(DeviceState *socdev, Err= or **errp) int i, n; =20 for (n =3D 0; n < EXYNOS4210_NCPUS; n++) { - Object *cpuobj =3D object_new(ARM_CPU_TYPE_NAME("cortex-a9")); + Object *cpuobj =3D object_new_dynamic(ARM_CPU_TYPE_NAME("cortex-a9= "), + &error_fatal); =20 object_property_add_child(OBJECT(s), "cpu[*]", cpuobj); /* By default A9 CPUs have EL3 enabled. This board does not curre= ntly diff --git a/hw/arm/highbank.c b/hw/arm/highbank.c index f103921d49..07740eecdb 100644 --- a/hw/arm/highbank.c +++ b/hw/arm/highbank.c @@ -206,7 +206,7 @@ static void calxeda_init(MachineState *machine, enum cx= machines machine_id) Object *cpuobj; ARMCPU *cpu; =20 - cpuobj =3D object_new(machine->cpu_type); + cpuobj =3D object_new_dynamic(machine->cpu_type, &error_abort); cpu =3D ARM_CPU(cpuobj); =20 object_property_add_child(OBJECT(machine), "cpu[*]", cpuobj); diff --git a/hw/arm/integratorcp.c b/hw/arm/integratorcp.c index feb0dd63df..2d1adfd7f8 100644 --- a/hw/arm/integratorcp.c +++ b/hw/arm/integratorcp.c @@ -595,7 +595,7 @@ static void integratorcp_init(MachineState *machine) DriveInfo *dinfo; int i; =20 - cpuobj =3D object_new(machine->cpu_type); + cpuobj =3D object_new_dynamic(machine->cpu_type, &error_fatal); =20 /* By default ARM1176 CPUs have EL3 enabled. This board does not * currently support EL3 so the CPU EL3 property is disabled before diff --git a/hw/arm/mps3r.c b/hw/arm/mps3r.c index 4d55a6564c..60c2138b4a 100644 --- a/hw/arm/mps3r.c +++ b/hw/arm/mps3r.c @@ -387,7 +387,8 @@ static void mps3r_common_init(MachineState *machine) memory_region_add_subregion_overlap(&mms->cpu_sysmem[i], 0, &mms->sysmem_alias[i], -1); =20 - mms->cpu[i] =3D object_new(machine->cpu_type); + mms->cpu[i] =3D object_new_dynamic(machine->cpu_type, + &error_fatal); object_property_set_link(mms->cpu[i], "memory", OBJECT(&mms->cpu_sysmem[i]), &error_abort= ); object_property_set_int(mms->cpu[i], "reset-cbar", diff --git a/hw/arm/realview.c b/hw/arm/realview.c index b186f965c6..214c03fb20 100644 --- a/hw/arm/realview.c +++ b/hw/arm/realview.c @@ -116,7 +116,8 @@ static void realview_init(MachineState *machine, } =20 for (n =3D 0; n < smp_cpus; n++) { - Object *cpuobj =3D object_new(machine->cpu_type); + Object *cpuobj =3D object_new_dynamic(machine->cpu_type, + &error_fatal); =20 /* By default A9,A15 and ARM1176 CPUs have EL3 enabled. This board * does not currently support EL3 so the CPU EL3 property is disab= led diff --git a/hw/arm/sbsa-ref.c b/hw/arm/sbsa-ref.c index e3195d5449..a0006c9af0 100644 --- a/hw/arm/sbsa-ref.c +++ b/hw/arm/sbsa-ref.c @@ -767,7 +767,8 @@ static void sbsa_ref_init(MachineState *machine) break; } =20 - cpuobj =3D object_new(possible_cpus->cpus[n].type); + cpuobj =3D object_new_dynamic(possible_cpus->cpus[n].type, + &error_fatal); object_property_set_int(cpuobj, "mp-affinity", possible_cpus->cpus[n].arch_id, NULL); =20 diff --git a/hw/arm/versatilepb.c b/hw/arm/versatilepb.c index d48235453e..eb77dfc593 100644 --- a/hw/arm/versatilepb.c +++ b/hw/arm/versatilepb.c @@ -205,7 +205,7 @@ static void versatile_init(MachineState *machine, int b= oard_id) exit(1); } =20 - cpuobj =3D object_new(machine->cpu_type); + cpuobj =3D object_new_dynamic(machine->cpu_type, &error_fatal); =20 /* By default ARM1176 CPUs have EL3 enabled. This board does not * currently support EL3 so the CPU EL3 property is disabled before diff --git a/hw/arm/vexpress.c b/hw/arm/vexpress.c index de815d84cc..98ad6299a8 100644 --- a/hw/arm/vexpress.c +++ b/hw/arm/vexpress.c @@ -217,7 +217,7 @@ static void init_cpus(MachineState *ms, const char *cpu= _type, =20 /* Create the actual CPUs */ for (n =3D 0; n < smp_cpus; n++) { - Object *cpuobj =3D object_new(cpu_type); + Object *cpuobj =3D object_new_dynamic(cpu_type, &error_abort); =20 if (!secure) { object_property_set_bool(cpuobj, "has_el3", false, NULL); diff --git a/hw/arm/virt.c b/hw/arm/virt.c index 1a381e9a2b..f80ab50d41 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -2125,7 +2125,8 @@ static void machvirt_init(MachineState *machine) * we are about to deal with. Once this is done, get rid of * the object. */ - cpuobj =3D object_new(possible_cpus->cpus[0].type); + cpuobj =3D object_new_dynamic(possible_cpus->cpus[0].type, + &error_fatal); armcpu =3D ARM_CPU(cpuobj); =20 pa_bits =3D arm_pamax(armcpu); @@ -2231,7 +2232,8 @@ static void machvirt_init(MachineState *machine) break; } =20 - cpuobj =3D object_new(possible_cpus->cpus[n].type); + cpuobj =3D object_new_dynamic(possible_cpus->cpus[n].type, + &error_fatal); object_property_set_int(cpuobj, "mp-affinity", possible_cpus->cpus[n].arch_id, NULL); =20 diff --git a/hw/arm/xilinx_zynq.c b/hw/arm/xilinx_zynq.c index fde4d946b7..40a725782d 100644 --- a/hw/arm/xilinx_zynq.c +++ b/hw/arm/xilinx_zynq.c @@ -218,7 +218,8 @@ static void zynq_init(MachineState *machine) } =20 for (n =3D 0; n < smp_cpus; n++) { - Object *cpuobj =3D object_new(machine->cpu_type); + Object *cpuobj =3D object_new_dynamic(machine->cpu_type, + &error_fatal); =20 object_property_set_int(cpuobj, "midr", ZYNQ_BOARD_MIDR, &error_fatal); diff --git a/hw/core/bus.c b/hw/core/bus.c index b9d89495cd..2de714b274 100644 --- a/hw/core/bus.c +++ b/hw/core/bus.c @@ -163,7 +163,7 @@ BusState *qbus_new(const char *typename, DeviceState *p= arent, const char *name) { BusState *bus; =20 - bus =3D BUS(object_new(typename)); + bus =3D BUS(object_new_dynamic(typename, &error_abort)); qbus_init_internal(bus, parent, name); =20 return bus; diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c index 09c7903594..8768ae39ed 100644 --- a/hw/core/cpu-common.c +++ b/hw/core/cpu-common.c @@ -57,7 +57,7 @@ bool cpu_exists(int64_t id) CPUState *cpu_create(const char *typename) { Error *err =3D NULL; - CPUState *cpu =3D CPU(object_new(typename)); + CPUState *cpu =3D CPU(object_new_dynamic(typename, &error_abort)); if (!qdev_realize(DEVICE(cpu), NULL, &err)) { error_report_err(err); object_unref(OBJECT(cpu)); diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 960a704a96..7fcbbe431b 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -146,7 +146,7 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bu= s, Error **errp) =20 DeviceState *qdev_new(const char *name) { - return DEVICE(object_new(name)); + return DEVICE(object_new_dynamic(name, &error_abort)); } =20 static QTAILQ_HEAD(, DeviceListener) device_listeners diff --git a/hw/i386/x86-common.c b/hw/i386/x86-common.c index bc360a9ea4..df38235eeb 100644 --- a/hw/i386/x86-common.c +++ b/hw/i386/x86-common.c @@ -55,7 +55,10 @@ static size_t pvh_start_addr; =20 static void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **e= rrp) { - Object *cpu =3D object_new(MACHINE(x86ms)->cpu_type); + Object *cpu =3D object_new_dynamic(MACHINE(x86ms)->cpu_type, errp); + if (!cpu) { + return; + } =20 if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) { goto out; diff --git a/hw/i386/xen/xen-pvh.c b/hw/i386/xen/xen-pvh.c index f1f02d3311..9aeb4e430b 100644 --- a/hw/i386/xen/xen-pvh.c +++ b/hw/i386/xen/xen-pvh.c @@ -28,7 +28,7 @@ struct XenPVHx86State { static DeviceState *xen_pvh_cpu_new(MachineState *ms, int64_t apic_id) { - Object *cpu =3D object_new(ms->cpu_type); + Object *cpu =3D object_new_dynamic(ms->cpu_type, &error_abort); =20 object_property_add_child(OBJECT(ms), "cpu[*]", cpu); object_property_set_uint(cpu, "apic-id", apic_id, &error_fatal); diff --git a/hw/intc/xics.c b/hw/intc/xics.c index e893363dc9..2f06193bcd 100644 --- a/hw/intc/xics.c +++ b/hw/intc/xics.c @@ -377,7 +377,10 @@ Object *icp_create(Object *cpu, const char *type, XICS= Fabric *xi, Error **errp) { Object *obj; =20 - obj =3D object_new(type); + obj =3D object_new_dynamic(type, errp); + if (!obj) { + return NULL; + } object_property_add_child(cpu, type, obj); object_unref(obj); object_property_set_link(obj, ICP_PROP_XICS, OBJECT(xi), &error_abort); diff --git a/hw/mips/cps.c b/hw/mips/cps.c index 13046628cd..b8407bb191 100644 --- a/hw/mips/cps.c +++ b/hw/mips/cps.c @@ -74,7 +74,8 @@ static void mips_cps_realize(DeviceState *dev, Error **er= rp) } =20 for (int i =3D 0; i < s->num_vp; i++) { - MIPSCPU *cpu =3D MIPS_CPU(object_new(s->cpu_type)); + MIPSCPU *cpu =3D MIPS_CPU(object_new_dynamic(s->cpu_type, + &error_abort)); CPUMIPSState *env =3D &cpu->env; =20 object_property_set_bool(OBJECT(cpu), "big-endian", s->cpu_is_bige= ndian, diff --git a/hw/pci-host/pnv_phb.c b/hw/pci-host/pnv_phb.c index d4c118d443..6e833835d6 100644 --- a/hw/pci-host/pnv_phb.c +++ b/hw/pci-host/pnv_phb.c @@ -131,7 +131,10 @@ static void pnv_phb_realize(DeviceState *dev, Error **= errp) g_assert_not_reached(); } =20 - phb->backend =3D object_new(phb_typename); + phb->backend =3D object_new_dynamic(phb_typename, errp); + if (!phb->backend) { + return; + } object_property_add_child(OBJECT(dev), "phb-backend", phb->backend); =20 /* Passthrough child device properties to the proxy device */ diff --git a/hw/ppc/e500.c b/hw/ppc/e500.c index 46261223f3..8ba34f0972 100644 --- a/hw/ppc/e500.c +++ b/hw/ppc/e500.c @@ -939,7 +939,7 @@ void ppce500_init(MachineState *machine) PowerPCCPU *cpu; CPUState *cs; =20 - cpu =3D POWERPC_CPU(object_new(machine->cpu_type)); + cpu =3D POWERPC_CPU(object_new_dynamic(machine->cpu_type, &error_f= atal)); env =3D &cpu->env; cs =3D CPU(cpu); =20 diff --git a/hw/ppc/pnv.c b/hw/ppc/pnv.c index f0f0d7567d..75420c9413 100644 --- a/hw/ppc/pnv.c +++ b/hw/ppc/pnv.c @@ -1459,7 +1459,7 @@ static void pnv_chip_power8_instance_init(Object *obj) chip8->num_phbs =3D pcc->num_phbs; =20 for (i =3D 0; i < chip8->num_phbs; i++) { - Object *phb =3D object_new(TYPE_PNV_PHB); + Object *phb =3D object_new_dynamic(TYPE_PNV_PHB, &error_fatal); =20 /* * We need the chip to parent the PHB to allow the DT @@ -2376,7 +2376,7 @@ static void pnv_chip_core_realize(PnvChip *chip, Erro= r **errp) continue; } =20 - pnv_core =3D PNV_CORE(object_new(typename)); + pnv_core =3D PNV_CORE(object_new_dynamic(typename, &error_fatal)); =20 snprintf(core_name, sizeof(core_name), "core[%d]", core_hwid); object_property_add_child(OBJECT(chip), core_name, OBJECT(pnv_core= )); diff --git a/hw/ppc/pnv_core.c b/hw/ppc/pnv_core.c index a30693990b..4c62103021 100644 --- a/hw/ppc/pnv_core.c +++ b/hw/ppc/pnv_core.c @@ -361,7 +361,10 @@ static void pnv_core_realize(DeviceState *dev, Error *= *errp) PowerPCCPU *cpu; PnvCPUState *pnv_cpu; =20 - obj =3D object_new(typename); + obj =3D object_new_dynamic(typename, &local_err); + if (!obj) { + goto err; + } cpu =3D POWERPC_CPU(obj); =20 pc->threads[i] =3D POWERPC_CPU(obj); diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c index 5c02037c56..ca71242fb0 100644 --- a/hw/ppc/spapr.c +++ b/hw/ppc/spapr.c @@ -2693,7 +2693,7 @@ static void spapr_init_cpus(SpaprMachineState *spapr) } =20 if (i < boot_cores_nr) { - Object *core =3D object_new(type); + Object *core =3D object_new_dynamic(type, &error_fatal); int nr_threads =3D smp_threads; =20 /* Handle the partially filled core for older machine types */ diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c index ada439e831..aa9704e7ea 100644 --- a/hw/ppc/spapr_cpu_core.c +++ b/hw/ppc/spapr_cpu_core.c @@ -298,7 +298,10 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc,= int i, Error **errp) PowerPCCPU *cpu; CPUPPCState *env; =20 - obj =3D object_new(scc->cpu_type); + obj =3D object_new_dynamic(scc->cpu_type, errp); + if (!obj) { + return NULL; + } =20 cs =3D CPU(obj); cpu =3D POWERPC_CPU(obj); diff --git a/hw/ppc/spapr_drc.c b/hw/ppc/spapr_drc.c index 1484e3209d..56835ebf2b 100644 --- a/hw/ppc/spapr_drc.c +++ b/hw/ppc/spapr_drc.c @@ -554,7 +554,7 @@ static void drc_unrealize(DeviceState *d) SpaprDrc *spapr_dr_connector_new(Object *owner, const char *type, uint32_t id) { - SpaprDrc *drc =3D SPAPR_DR_CONNECTOR(object_new(type)); + SpaprDrc *drc =3D SPAPR_DR_CONNECTOR(object_new_dynamic(type, &error_f= atal)); g_autofree char *prop_name =3D NULL; =20 drc->id =3D id; diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c index fe03f716f3..5441dc4c32 100644 --- a/hw/s390x/s390-virtio-ccw.c +++ b/hw/s390x/s390-virtio-ccw.c @@ -55,9 +55,15 @@ static Error *pv_mig_blocker; static S390CPU *s390x_new_cpu(const char *typename, uint32_t core_id, Error **errp) { - S390CPU *cpu =3D S390_CPU(object_new(typename)); + Object *cpuobj =3D object_new_dynamic(typename, errp); + S390CPU *cpu =3D NULL; S390CPU *ret =3D NULL; =20 + if (!cpuobj) { + return NULL; + } + + cpu =3D S390_CPU(cpuobj); if (!object_property_set_int(OBJECT(cpu), "core-id", core_id, errp)) { goto out; } diff --git a/hw/sparc/leon3.c b/hw/sparc/leon3.c index 6aaa04cb19..519169af1e 100644 --- a/hw/sparc/leon3.c +++ b/hw/sparc/leon3.c @@ -277,7 +277,7 @@ static void leon3_generic_hw_init(MachineState *machine) =20 for (i =3D 0; i < machine->smp.cpus; i++) { /* Init CPU */ - cpu =3D SPARC_CPU(object_new(machine->cpu_type)); + cpu =3D SPARC_CPU(object_new_dynamic(machine->cpu_type, &error_fat= al)); qdev_init_gpio_in_named(DEVICE(cpu), leon3_start_cpu, "start_cpu",= 1); qdev_init_gpio_in_named(DEVICE(cpu), leon3_set_pil_in, "pil", 1); qdev_realize(DEVICE(cpu), NULL, &error_fatal); diff --git a/hw/sparc/sun4m.c b/hw/sparc/sun4m.c index d52e6a7213..c7e47af1d5 100644 --- a/hw/sparc/sun4m.c +++ b/hw/sparc/sun4m.c @@ -802,7 +802,7 @@ static void cpu_devinit(const char *cpu_type, unsigned = int id, SPARCCPU *cpu; CPUSPARCState *env; =20 - cpu =3D SPARC_CPU(object_new(cpu_type)); + cpu =3D SPARC_CPU(object_new_dynamic(cpu_type, &error_fatal)); env =3D &cpu->env; =20 qemu_register_reset(sun4m_cpu_reset, cpu); diff --git a/hw/sparc64/sparc64.c b/hw/sparc64/sparc64.c index 3091cde586..9e032e6124 100644 --- a/hw/sparc64/sparc64.c +++ b/hw/sparc64/sparc64.c @@ -272,7 +272,7 @@ SPARCCPU *sparc64_cpu_devinit(const char *cpu_type, uin= t64_t prom_addr) uint32_t stick_frequency =3D 100 * 1000000; uint32_t hstick_frequency =3D 100 * 1000000; =20 - cpu =3D SPARC_CPU(object_new(cpu_type)); + cpu =3D SPARC_CPU(object_new_dynamic(cpu_type, &error_fatal)); qdev_init_gpio_in_named(DEVICE(cpu), sparc64_cpu_set_ivec_irq, "ivec-irq", IVEC_MAX); qdev_realize(DEVICE(cpu), NULL, &error_fatal); diff --git a/hw/vfio/common.c b/hw/vfio/common.c index dcef44fe55..9799fd8627 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1550,7 +1550,11 @@ bool vfio_attach_device(char *name, VFIODevice *vbas= edev, =20 =20 if (!vbasedev->mdev) { - hiod =3D HOST_IOMMU_DEVICE(object_new(ops->hiod_typename)); + Object *obj =3D object_new_dynamic(ops->hiod_typename, errp); + if (!obj) { + return false; + } + hiod =3D HOST_IOMMU_DEVICE(obj); vbasedev->hiod =3D hiod; } =20 diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 9ccdb639ac..6642d2f79b 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -419,6 +419,7 @@ static VFIOContainer *vfio_create_container(int fd, VFI= OGroup *group, { int iommu_type; const char *vioc_name; + Object *obj; VFIOContainer *container; =20 iommu_type =3D vfio_get_iommu_type(fd, errp); @@ -432,7 +433,11 @@ static VFIOContainer *vfio_create_container(int fd, VF= IOGroup *group, =20 vioc_name =3D vfio_get_iommu_class_name(iommu_type); =20 - container =3D VFIO_IOMMU_LEGACY(object_new(vioc_name)); + obj =3D object_new_dynamic(vioc_name, errp); + if (!obj) { + return NULL; + } + container =3D VFIO_IOMMU_LEGACY(obj); container->fd =3D fd; container->iommu_type =3D iommu_type; return container; diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c index 4a8e269fef..a32855659e 100644 --- a/qom/qom-qmp-cmds.c +++ b/qom/qom-qmp-cmds.c @@ -203,7 +203,10 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const = char *typename, if (object_class_is_abstract(klass)) { object_class_property_iter_init(&iter, klass); } else { - obj =3D object_new(typename); + obj =3D object_new_dynamic(typename, errp); + if (!obj) { + return NULL; + } object_property_iter_init(&iter, obj); } while ((prop =3D object_property_iter_next(&iter))) { diff --git a/target/arm/arm-qmp-cmds.c b/target/arm/arm-qmp-cmds.c index 3cc8cc738b..61024f480d 100644 --- a/target/arm/arm-qmp-cmds.c +++ b/target/arm/arm-qmp-cmds.c @@ -150,7 +150,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(C= puModelExpansionType type, } } =20 - obj =3D object_new(object_class_get_name(oc)); + obj =3D object_new_dynamic(object_class_get_name(oc), errp); + if (!obj) { + return NULL; + } =20 if (model->props) { Visitor *visitor; diff --git a/target/loongarch/loongarch-qmp-cmds.c b/target/loongarch/loong= arch-qmp-cmds.c index 782fd511fd..5f565e2236 100644 --- a/target/loongarch/loongarch-qmp-cmds.c +++ b/target/loongarch/loongarch-qmp-cmds.c @@ -83,7 +83,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(Cpu= ModelExpansionType type, return NULL; } =20 - obj =3D object_new(object_class_get_name(oc)); + obj =3D object_new_dynamic(object_class_get_name(oc), errp); + if (!obj) { + return NULL; + } =20 expansion_info =3D g_new0(CpuModelExpansionInfo, 1); expansion_info->model =3D g_malloc0(sizeof(*expansion_info->model)); diff --git a/target/mips/cpu.c b/target/mips/cpu.c index d0a43b6d5c..1bf872c3e0 100644 --- a/target/mips/cpu.c +++ b/target/mips/cpu.c @@ -648,7 +648,7 @@ MIPSCPU *mips_cpu_create_with_clock(const char *cpu_typ= e, Clock *cpu_refclk, { DeviceState *cpu; =20 - cpu =3D DEVICE(object_new(cpu_type)); + cpu =3D DEVICE(object_new_dynamic(cpu_type, &error_abort)); qdev_connect_clock_in(cpu, "clk-in", cpu_refclk); object_property_set_bool(OBJECT(cpu), "big-endian", is_big_endian, &error_abort); diff --git a/target/riscv/riscv-qmp-cmds.c b/target/riscv/riscv-qmp-cmds.c index d363dc318d..61f30b6bc6 100644 --- a/target/riscv/riscv-qmp-cmds.c +++ b/target/riscv/riscv-qmp-cmds.c @@ -188,7 +188,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(C= puModelExpansionType type, return NULL; } =20 - obj =3D object_new(object_class_get_name(oc)); + obj =3D object_new_dynamic(object_class_get_name(oc), errp); + if (!obj) { + return NULL; + } =20 riscv_check_if_cpu_available(RISCV_CPU(obj), &local_err); if (local_err !=3D NULL) { diff --git a/target/xtensa/cpu.c b/target/xtensa/cpu.c index 6f9039abae..3e036a1191 100644 --- a/target/xtensa/cpu.c +++ b/target/xtensa/cpu.c @@ -204,7 +204,7 @@ XtensaCPU *xtensa_cpu_create_with_clock(const char *cpu= _type, Clock *cpu_refclk) { DeviceState *cpu; =20 - cpu =3D DEVICE(object_new(cpu_type)); + cpu =3D DEVICE(object_new_dynamic(cpu_type, &error_abort)); qdev_connect_clock_in(cpu, "clk-in", cpu_refclk); qdev_realize(cpu, NULL, &error_abort); =20 diff --git a/tests/unit/check-qom-interface.c b/tests/unit/check-qom-interf= ace.c index c99be97ed8..e4532ae814 100644 --- a/tests/unit/check-qom-interface.c +++ b/tests/unit/check-qom-interface.c @@ -13,6 +13,7 @@ =20 #include "qom/object.h" #include "qemu/module.h" +#include "qapi/error.h" =20 =20 #define TYPE_TEST_IF "test-interface" @@ -67,7 +68,7 @@ static const TypeInfo intermediate_impl_info =3D { =20 static void test_interface_impl(const char *type) { - Object *obj =3D object_new(type); + Object *obj =3D object_new_dynamic(type, &error_abort); TestIf *iobj =3D TEST_IF(obj); TestIfClass *ioc =3D TEST_IF_GET_CLASS(iobj); =20 diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c index f9bccb56ab..f4893d5f24 100644 --- a/tests/unit/test-smp-parse.c +++ b/tests/unit/test-smp-parse.c @@ -1008,7 +1008,7 @@ static void machine_full_topo_class_init(ObjectClass = *oc, void *data) static void test_generic_valid(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1027,7 +1027,7 @@ static void test_generic_valid(const void *opaque) static void test_generic_invalid(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1046,7 +1046,7 @@ static void test_generic_invalid(const void *opaque) static void test_with_modules(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1096,7 +1096,7 @@ static void test_with_modules(const void *opaque) static void test_with_dies(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1146,7 +1146,7 @@ static void test_with_dies(const void *opaque) static void test_with_modules_dies(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1207,7 +1207,7 @@ static void test_with_modules_dies(const void *opaque) static void test_with_clusters(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1257,7 +1257,7 @@ static void test_with_clusters(const void *opaque) static void test_with_books(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1307,7 +1307,7 @@ static void test_with_books(const void *opaque) static void test_with_drawers(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1357,7 +1357,7 @@ static void test_with_drawers(const void *opaque) static void test_with_drawers_books(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; @@ -1418,7 +1418,7 @@ static void test_with_drawers_books(const void *opaqu= e) static void test_full_topo(const void *opaque) { const char *machine_type =3D opaque; - Object *obj =3D object_new(machine_type); + Object *obj =3D object_new_dynamic(machine_type, &error_abort); MachineState *ms =3D MACHINE(obj); MachineClass *mc =3D MACHINE_GET_CLASS(obj); SMPTestData data =3D {}; --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691614; cv=none; d=zohomail.com; s=zohoarc; b=mZQWH8ykMRxb8hIMJYXE7mDZHne9nA7XpFRngHXuE0MHPK9yA6fQHV6QTAOrE6jEHrkYa9D6jreT2xf0Oj0Qw+PDCWIsWtsOFcwY4QHB7guCj/YAyWPmgGa6IldNTl4+NnpLzYleV+1qPcaF1Y2ba/Uo1C9/drNIj7eFr3YFNLU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691614; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=84cmAwRbFYPTBLNlP3GH4So0+a6lDgsXUfrxy8UhKvE=; b=d/LGADasbd44v/P6H52RfT/1hePAQA0Rxbwo30HxiOch+UTV1MCY7i9AtEHlkY3bGUvZFWSG7EXuZ7iwpe/KvYTw9obN1sh2ga099HvHeYXNRb/3rTlbtU20pjYdgZST/MpQtKHNRIdpC22EeBrrx5amJqqr01e7OMVcE3V99RI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691614973184.3631725414084; Fri, 15 Nov 2024 09:26:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC052-0004OM-M9; Fri, 15 Nov 2024 12:26:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04m-0004CP-UB for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:46 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04l-0001XA-EN for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:44 -0500 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-682-F1av6H7BPSG7P0Lc0s6N-w-1; Fri, 15 Nov 2024 12:25:41 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 52E2A1953944 for ; Fri, 15 Nov 2024 17:25:40 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 416C93003B76; Fri, 15 Nov 2024 17:25:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691542; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=84cmAwRbFYPTBLNlP3GH4So0+a6lDgsXUfrxy8UhKvE=; b=IX5WGuXaQ1LdzT0/k6k+R6wc582PZwEindHpwbnXtiKRRvx4GUA2kF2fPhK/Wopzdg+WMZ lIBJl3GBjb+XJJ6SLhrEGiHAmqqH27xFHUzq+tAoZDd9o9AYRpDhsnK8mzR7GMhdU8q9Dk Ux1f5Blw1KKJnuIiTFE4/oSWXrvLbek= X-MC-Unique: F1av6H7BPSG7P0Lc0s6N-w-1 X-Mimecast-MFC-AGG-ID: F1av6H7BPSG7P0Lc0s6N-w From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 6/9] qom: enforce use of static, const string with object_new() Date: Fri, 15 Nov 2024 17:25:18 +0000 Message-ID: <20241115172521.504102-7-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691616494116600 Since object_new() will assert(), it should only be used in scenarios where the caller knows exactly what type it is asking to be created, and can thus be confident in avoiding abstract types. Enforce this by using a macro wrapper which types to paste "" to the type name. This will generate a compile error if not passed a static const string, forcing callers to use object_new_dynamic() instead. Reviewed-by: Peter Xu Signed-off-by: Daniel P. Berrang=C3=A9 --- include/qom/object.h | 12 +++++++++++- qom/object.c | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 4fc01336c4..2d5a0d84b5 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -637,7 +637,17 @@ Object *object_new_with_class(ObjectClass *klass, Erro= r **errp); * * Returns: The newly allocated and instantiated object. */ -Object *object_new(const char *typename); + +/* + * NB, object_new_internal is just an internal helper, wrapped by + * the object_new() macro which prevents invokation unless given + * a static, const string. + * + * Code should call object_new(), or object_new_dynamic(), not + * object_new_internal(). + */ +Object *object_new_internal(const char *typename); +#define object_new(typename) object_new_internal(typename "") =20 /** * object_new_dynamic: diff --git a/qom/object.c b/qom/object.c index 42ef40a1fd..ee56710348 100644 --- a/qom/object.c +++ b/qom/object.c @@ -799,7 +799,8 @@ Object *object_new_with_class(ObjectClass *klass, Error= **errp) return object_new_with_type(klass->type, errp); } =20 -Object *object_new(const char *typename) +/* Only to be called via the 'object_new' macro */ +Object *object_new_internal(const char *typename) { TypeImpl *ti =3D type_get_or_load_by_name(typename, &error_fatal); =20 --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691639; cv=none; d=zohomail.com; s=zohoarc; b=IAN4SDgilStjwGjCXOqEbyoh2UEfar4SrjOAIoQ+VhmHiXu3hqyCMkV++MgEeGnd38PzxBndy45nWdO+iVrIZhi1zPE/+LcaUa5PDkyvMrkudmWIN/yBhsLXwQva6M8GNXU7bv4u3HkuCvtGWeUnFA8dtrukYXdINCdo0dJI9oY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691639; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=n5te+I35ljUxz/CFLWZVSbhZ0wWO/u4MF+EMMpULpxA=; b=nkSZ9I/gZAyzAgjMZo6tdOE3s4+lzg5GoVGhK2zPP6Icsr456HRx4JOaXoyA5rJcyli9m14sSoBoTXbJ0DN7dsdbMHv5jgW6apKXlYPiKzGdwiIYyjxW36PansQeeQDmgVbe9wAbQIUg5A48DasmxT3etfmRjulueUtPcY7nc1I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691639415663.7344799748303; Fri, 15 Nov 2024 09:27:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC052-0004P9-T5; Fri, 15 Nov 2024 12:26:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04p-0004D1-AY for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:48 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04n-0001XY-OW for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:47 -0500 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-82-mKMbOOZFPGipbOp0Xbbq0w-1; Fri, 15 Nov 2024 12:25:43 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0E5771955F25 for ; Fri, 15 Nov 2024 17:25:43 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CB3C83003B71; Fri, 15 Nov 2024 17:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691545; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n5te+I35ljUxz/CFLWZVSbhZ0wWO/u4MF+EMMpULpxA=; b=Et/xCu1ouLxtX74vHO8A2h0t/6Xpfcj7RTo/QVeII6OZoKklapL/mSAjDEOO5jdE9pChSu VZFMYh/HFCnGPWVUimxngl3YdzAqytphnCMS2CBYsT0qY6JtYDqI4jL2M28qN7oHpjgHwJ k8ZyrlNXwpJU5PmRQfbleMkNgh9agHc= X-MC-Unique: mKMbOOZFPGipbOp0Xbbq0w-1 X-Mimecast-MFC-AGG-ID: mKMbOOZFPGipbOp0Xbbq0w From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 7/9] qom: introduce qdev_new_dynamic() Date: Fri, 15 Nov 2024 17:25:19 +0000 Message-ID: <20241115172521.504102-8-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691640577116600 qdev_new() has a failure scenario where it will assert() if given an abstract type. Callers which are creating qdevs based on user input, or unknown/untrusted type names, must manually check the result of qdev_class_is_abstract() before calling qdev_new() to propagate an Error, instead of asserting. Introduce a qdev_new_dynamic() method which is a counterpart to qdev_new() that directly returns an Error, instead of asserting. This new method is to be used where the typename is specified dynamically by code separate from the immediate caller. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Peter Xu --- hw/core/qdev.c | 5 +++++ include/hw/qdev-core.h | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 7fcbbe431b..eceba33222 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -149,6 +149,11 @@ DeviceState *qdev_new(const char *name) return DEVICE(object_new_dynamic(name, &error_abort)); } =20 +DeviceState *qdev_new_dynamic(const char *name, Error **errp) +{ + return DEVICE(object_new_dynamic(name, errp)); +} + static QTAILQ_HEAD(, DeviceListener) device_listeners =3D QTAILQ_HEAD_INITIALIZER(device_listeners); =20 diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 020417d027..566c5ef277 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -435,14 +435,41 @@ compat_props_add(GPtrArray *arr, * qdev_new: Create a device on the heap * @name: device type to create (we assert() that this type exists) * + * This method should be used where @name is statically specified + * from a const string at build time, where the caller does not expect + * failure to be possible. + * * This only allocates the memory and initializes the device state * structure, ready for the caller to set properties if they wish. * The device still needs to be realized. * + * If an instance of @name is not permitted to be instantiated, an + * assert will be raised. This can happen if @name is abstract. + * * Return: a derived DeviceState object with a reference count of 1. */ DeviceState *qdev_new(const char *name); =20 +/** + * qdev_new_dynamic: Create a device on the heap + * @name: device type to create (we assert() that this type exists) + * @errp: pointer to be filled with error details on failure + * + * This method must be used where @name is dynamically chosen + * at runtime, which has the possibility of unexpected choices leading + * to failures. + * + * This only allocates the memory and initializes the device state + * structure, ready for the caller to set properties if they wish. + * The device still needs to be realized. + * + * If an instance of @name is not permitted to be instantiated, an + * error will be reported. This can happen if @name is abstract. + * + * Return: a derived DeviceState object with a reference count of 1. + */ +DeviceState *qdev_new_dynamic(const char *name, Error **errp); + /** * qdev_is_realized() - check if device is realized * @dev: The device to check. --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691599; cv=none; d=zohomail.com; s=zohoarc; b=l242ZwSM8gSmTo5qCbTkMyt2xvEhBIeCRFdr4MIVWrhxv96YFXuYPtkGWMkBzQvolcKEvUu2Sin/e+1YLRCnBtGPJIQOolTx372k2I63Sxf1E5X+/vK09mYciTrycnzH+hMAT2jsy26O9X5oHmwb0QaN4o9nshICD3Z4qH+QpIA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691599; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=j8dMChsOuS9jlDOz1J3yQmZCZaUyvVQsjl8ffWhr/CU=; b=MQ2xFugAinQ+r+YF6FD/WKoRqzfF/8y1KCK2a0FQxo1mN/5r5HFO0zDidnB/GuUca6mUw08l1+77uf1eikWXPcleKkZ9YeJC+fz4xojwsYyfjjRVUzKQpjLQ4z9I1cX2fCDbbS05uM8JUh1ioXIhU1C+VQj9QdmsMH7mvHx+jh8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691599512505.73125059918095; Fri, 15 Nov 2024 09:26:39 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC05C-0004Sa-C7; Fri, 15 Nov 2024 12:26:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04s-0004Dn-DH for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04q-0001Xp-9K for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:50 -0500 Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-146-GOKYSBTGN8ycXl4Zi5f1Mw-1; Fri, 15 Nov 2024 12:25:46 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 59BAC195608C for ; Fri, 15 Nov 2024 17:25:45 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9B89C3003B74; Fri, 15 Nov 2024 17:25:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=j8dMChsOuS9jlDOz1J3yQmZCZaUyvVQsjl8ffWhr/CU=; b=Qw+4Kv/ytGkgb9n9X2vguu01nU/lmzboH02CTk/8jncvauPCpIxfoj9VIOPnZqB/K7P+ph Z73+3gAGtQSP/4rjgWsHe81ZUBHPb56R6vwjoaQTM83gt2aL9iQxnakQ8+h7zW3qGkJ/kJ xBIG2A+Lqjq8XmqappK/WTXW79ZxwOU= X-MC-Unique: GOKYSBTGN8ycXl4Zi5f1Mw-1 X-Mimecast-MFC-AGG-ID: GOKYSBTGN8ycXl4Zi5f1Mw From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 8/9] convert code to qdev_new_dynamic() where appropriate Date: Fri, 15 Nov 2024 17:25:20 +0000 Message-ID: <20241115172521.504102-9-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691600794116600 In cases where qdev_new() is not being passed a static, const string, the caller cannot be sure what type they are instantiating. There is a risk that instantiation could fail, if it is an abstract type. Convert such cases over to use qdev_new_dynamic() such that they are forced to expect failure. In some cases failure can be easily propagated, but in others using &error_abort or &error_fatal will maintain existing behaviour. Reviewed-by: Peter Xu Signed-off-by: Daniel P. Berrang=C3=A9 --- hw/arm/aspeed.c | 2 +- hw/arm/npcm7xx_boards.c | 2 +- hw/arm/sbsa-ref.c | 4 ++-- hw/arm/vexpress.c | 2 +- hw/arm/virt.c | 4 ++-- hw/audio/soundhw.c | 2 +- hw/block/xen-block.c | 7 ++++++- hw/core/sysbus.c | 2 +- hw/i2c/core.c | 2 +- hw/isa/isa-bus.c | 2 +- hw/pci/pci.c | 2 +- hw/ppc/pnv.c | 2 +- hw/s390x/s390-virtio-ccw.c | 2 +- hw/scsi/scsi-bus.c | 5 ++++- hw/ssi/ssi.c | 2 +- include/hw/usb.h | 2 +- include/qom/object.h | 4 ++-- net/net.c | 4 ++-- system/qdev-monitor.c | 5 ++++- 19 files changed, 34 insertions(+), 23 deletions(-) diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c index 71196b0a4b..19dbcee64a 100644 --- a/hw/arm/aspeed.c +++ b/hw/arm/aspeed.c @@ -320,7 +320,7 @@ void aspeed_board_init_flashes(AspeedSMCState *s, const= char *flashtype, DriveInfo *dinfo =3D drive_get(IF_MTD, 0, unit0 + i); DeviceState *dev; =20 - dev =3D qdev_new(flashtype); + dev =3D qdev_new_dynamic(flashtype, &error_fatal); if (dinfo) { qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo)); } diff --git a/hw/arm/npcm7xx_boards.c b/hw/arm/npcm7xx_boards.c index e229efb447..098beeab63 100644 --- a/hw/arm/npcm7xx_boards.c +++ b/hw/arm/npcm7xx_boards.c @@ -83,7 +83,7 @@ static void npcm7xx_connect_flash(NPCM7xxFIUState *fiu, i= nt cs_no, DeviceState *flash; qemu_irq flash_cs; =20 - flash =3D qdev_new(flash_type); + flash =3D qdev_new_dynamic(flash_type, &error_fatal); if (dinfo) { qdev_prop_set_drive(flash, "drive", blk_by_legacy_dinfo(dinfo)); } diff --git a/hw/arm/sbsa-ref.c b/hw/arm/sbsa-ref.c index a0006c9af0..12e0a70981 100644 --- a/hw/arm/sbsa-ref.c +++ b/hw/arm/sbsa-ref.c @@ -419,7 +419,7 @@ static void create_its(SBSAMachineState *sms) const char *itsclass =3D its_class_name(); DeviceState *dev; =20 - dev =3D qdev_new(itsclass); + dev =3D qdev_new_dynamic(itsclass, &error_fatal); =20 object_property_set_link(OBJECT(dev), "parent-gicv3", OBJECT(sms->gic), &error_abort); @@ -438,7 +438,7 @@ static void create_gic(SBSAMachineState *sms, MemoryReg= ion *mem) =20 gictype =3D gicv3_class_name(); =20 - sms->gic =3D qdev_new(gictype); + sms->gic =3D qdev_new_dynamic(gictype, &error_fatal); qdev_prop_set_uint32(sms->gic, "revision", 3); qdev_prop_set_uint32(sms->gic, "num-cpu", smp_cpus); /* diff --git a/hw/arm/vexpress.c b/hw/arm/vexpress.c index 98ad6299a8..e13c66b838 100644 --- a/hw/arm/vexpress.c +++ b/hw/arm/vexpress.c @@ -239,7 +239,7 @@ static void init_cpus(MachineState *ms, const char *cpu= _type, * this must happen after the CPUs are created because a15mpcore_priv * wires itself up to the CPU's generic_timer gpio out lines. */ - dev =3D qdev_new(privdev); + dev =3D qdev_new_dynamic(privdev, &error_fatal); qdev_prop_set_uint32(dev, "num-cpu", smp_cpus); busdev =3D SYS_BUS_DEVICE(dev); sysbus_realize_and_unref(busdev, &error_fatal); diff --git a/hw/arm/virt.c b/hw/arm/virt.c index f80ab50d41..57b1735380 100644 --- a/hw/arm/virt.c +++ b/hw/arm/virt.c @@ -715,7 +715,7 @@ static void create_its(VirtMachineState *vms) return; } =20 - dev =3D qdev_new(itsclass); + dev =3D qdev_new_dynamic(itsclass, &error_fatal); =20 object_property_set_link(OBJECT(dev), "parent-gicv3", OBJECT(vms->gic), &error_abort); @@ -791,7 +791,7 @@ static void create_gic(VirtMachineState *vms, MemoryReg= ion *mem) default: g_assert_not_reached(); } - vms->gic =3D qdev_new(gictype); + vms->gic =3D qdev_new_dynamic(gictype, &error_fatal); qdev_prop_set_uint32(vms->gic, "revision", revision); qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus); /* Note that the num-irq property counts both internal and external diff --git a/hw/audio/soundhw.c b/hw/audio/soundhw.c index d18fd9fa05..17ed3a8084 100644 --- a/hw/audio/soundhw.c +++ b/hw/audio/soundhw.c @@ -132,7 +132,7 @@ void soundhw_init(void) } =20 if (c->typename) { - DeviceState *dev =3D qdev_new(c->typename); + DeviceState *dev =3D qdev_new_dynamic(c->typename, &error_fatal); qdev_prop_set_string(dev, "audiodev", audiodev_id); qdev_realize_and_unref(dev, bus, &error_fatal); } else { diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c index aed1d5c330..fd5fa7b6e7 100644 --- a/hw/block/xen-block.c +++ b/hw/block/xen-block.c @@ -1034,6 +1034,7 @@ static void xen_block_device_create(XenBackendInstanc= e *backend, XenDevice *xendev =3D NULL; const char *type; XenBlockDevice *blockdev; + DeviceState *dev; =20 if (qemu_strtoul(name, NULL, 10, &number)) { error_setg(errp, "failed to parse name '%s'", name); @@ -1075,7 +1076,11 @@ static void xen_block_device_create(XenBackendInstan= ce *backend, goto fail; } =20 - xendev =3D XEN_DEVICE(qdev_new(type)); + dev =3D qdev_new_dynamic(type, errp); + if (!dev) { + goto fail; + } + xendev =3D XEN_DEVICE(dev); blockdev =3D XEN_BLOCK_DEVICE(xendev); =20 if (!object_property_set_str(OBJECT(xendev), "vdev", vdev, diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c index e64d99c8ed..e472a969f1 100644 --- a/hw/core/sysbus.c +++ b/hw/core/sysbus.c @@ -221,7 +221,7 @@ DeviceState *sysbus_create_varargs(const char *name, qemu_irq irq; int n; =20 - dev =3D qdev_new(name); + dev =3D qdev_new_dynamic(name, &error_fatal); s =3D SYS_BUS_DEVICE(dev); sysbus_realize_and_unref(s, &error_fatal); if (addr !=3D (hwaddr)-1) { diff --git a/hw/i2c/core.c b/hw/i2c/core.c index 4cf30b2c86..a430f4e767 100644 --- a/hw/i2c/core.c +++ b/hw/i2c/core.c @@ -369,7 +369,7 @@ I2CSlave *i2c_slave_new(const char *name, uint8_t addr) { DeviceState *dev; =20 - dev =3D qdev_new(name); + dev =3D qdev_new_dynamic(name, &error_fatal); qdev_prop_set_uint8(dev, "address", addr); return I2C_SLAVE(dev); } diff --git a/hw/isa/isa-bus.c b/hw/isa/isa-bus.c index 8aaf44a3ef..34330547cb 100644 --- a/hw/isa/isa-bus.c +++ b/hw/isa/isa-bus.c @@ -155,7 +155,7 @@ int isa_register_portio_list(ISADevice *dev, =20 ISADevice *isa_new(const char *name) { - return ISA_DEVICE(qdev_new(name)); + return ISA_DEVICE(qdev_new_dynamic(name, &error_fatal)); } =20 ISADevice *isa_create_simple(ISABus *bus, const char *name) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 1416ae202c..51338320c1 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2186,7 +2186,7 @@ static PCIDevice *pci_new_internal(int devfn, bool mu= ltifunction, { DeviceState *dev; =20 - dev =3D qdev_new(name); + dev =3D qdev_new_dynamic(name, &error_fatal); qdev_prop_set_int32(dev, "addr", devfn); qdev_prop_set_bit(dev, "multifunction", multifunction); return PCI_DEVICE(dev); diff --git a/hw/ppc/pnv.c b/hw/ppc/pnv.c index 75420c9413..e81c562967 100644 --- a/hw/ppc/pnv.c +++ b/hw/ppc/pnv.c @@ -1115,7 +1115,7 @@ static void pnv_init(MachineState *machine) pnv->chips =3D g_new0(PnvChip *, pnv->num_chips); for (i =3D 0; i < pnv->num_chips; i++) { char chip_name[32]; - Object *chip =3D OBJECT(qdev_new(chip_typename)); + Object *chip =3D OBJECT(qdev_new_dynamic(chip_typename, &error_fat= al)); uint64_t chip_ram_size =3D pnv_chip_get_ram_size(pnv, i); =20 pnv->chips[i] =3D PNV_CHIP(chip); diff --git a/hw/s390x/s390-virtio-ccw.c b/hw/s390x/s390-virtio-ccw.c index 5441dc4c32..b276b5e77f 100644 --- a/hw/s390x/s390-virtio-ccw.c +++ b/hw/s390x/s390-virtio-ccw.c @@ -242,7 +242,7 @@ static void s390_create_sclpconsole(SCLPDevice *sclp, BusState *ev_fac_bus =3D sclp_get_event_facility_bus(ef); DeviceState *dev; =20 - dev =3D qdev_new(type); + dev =3D qdev_new_dynamic(type, &error_fatal); object_property_add_child(OBJECT(ef), type, OBJECT(dev)); qdev_prop_set_chr(dev, "chardev", chardev); qdev_realize_and_unref(dev, ev_fac_bus, &error_fatal); diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 53eff5dd3d..23be8ebca4 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -396,7 +396,10 @@ SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, Bl= ockBackend *blk, driver =3D "scsi-hd"; } } - dev =3D qdev_new(driver); + dev =3D qdev_new_dynamic(driver, errp); + if (!dev) { + return NULL; + } name =3D g_strdup_printf("legacy[%d]", unit); object_property_add_child(OBJECT(bus), name, OBJECT(dev)); g_free(name); diff --git a/hw/ssi/ssi.c b/hw/ssi/ssi.c index 3f357e8f16..712bb1781c 100644 --- a/hw/ssi/ssi.c +++ b/hw/ssi/ssi.c @@ -141,7 +141,7 @@ bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bu= s, Error **errp) =20 DeviceState *ssi_create_peripheral(SSIBus *bus, const char *name) { - DeviceState *dev =3D qdev_new(name); + DeviceState *dev =3D qdev_new_dynamic(name, &error_fatal); =20 ssi_realize_and_unref(dev, bus, &error_fatal); return dev; diff --git a/include/hw/usb.h b/include/hw/usb.h index bb778cb844..bb90098d39 100644 --- a/include/hw/usb.h +++ b/include/hw/usb.h @@ -581,7 +581,7 @@ void usb_pcap_data(USBPacket *p, bool setup); =20 static inline USBDevice *usb_new(const char *name) { - return USB_DEVICE(qdev_new(name)); + return USB_DEVICE(qdev_new_dynamic(name, &error_fatal)); } =20 static inline bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Erro= r **errp) diff --git a/include/qom/object.h b/include/qom/object.h index 2d5a0d84b5..4e660da84a 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -654,7 +654,7 @@ Object *object_new_internal(const char *typename); * @typename: The name of the type of the object to instantiate. * @errp: pointer to be filled with error details on failure * - * This method should be used where @typename is dynamically chosen + * This method must be used where @typename is dynamically chosen * at runtime, which has the possibility of unexpected choices leading * to failures. * @@ -663,7 +663,7 @@ Object *object_new_internal(const char *typename); * the last reference is dropped. * * If an instance of @typename is not permitted to be instantiated, an - * error will be raised. This can happen if @typename is abstract. + * error will be reported. This can happen if @typename is abstract. * * Returns: The newly allocated and instantiated object. */ diff --git a/net/net.c b/net/net.c index fbbfe602a4..fa89ec8e03 100644 --- a/net/net.c +++ b/net/net.c @@ -1175,7 +1175,7 @@ DeviceState *qemu_create_nic_device(const char *typen= ame, bool match_default, return NULL; } =20 - dev =3D qdev_new(typename); + dev =3D qdev_new_dynamic(typename, &error_fatal); qdev_set_nic_properties(dev, nd); return dev; } @@ -1225,7 +1225,7 @@ void qemu_create_nic_bus_devices(BusState *bus, const= char *parent_type, continue; } =20 - dev =3D qdev_new(model); + dev =3D qdev_new_dynamic(model, &error_fatal); qdev_set_nic_properties(dev, nd); qdev_realize_and_unref(dev, bus, &error_fatal); } diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c index 4c09b38ffb..3138192cf8 100644 --- a/system/qdev-monitor.c +++ b/system/qdev-monitor.c @@ -686,7 +686,10 @@ DeviceState *qdev_device_add_from_qdict(const QDict *o= pts, } =20 /* create device */ - dev =3D qdev_new(driver); + dev =3D qdev_new_dynamic(driver, errp); + if (!dev) { + return NULL; + } =20 /* Check whether the hotplug is allowed by the machine */ if (phase_check(PHASE_MACHINE_READY)) { --=20 2.46.0 From nobody Sat Nov 23 18:14:20 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1731691610; cv=none; d=zohomail.com; s=zohoarc; b=WX9QE8xi1x2jutwG0MKKfkE9OfICcSzSn+DQ8h8orOJ6AZbXfv11BbofNfkHglViTjjsUj6PAZBYciMme0Rmh0xp9uuJFXO73D0beSrZPLEhMqQmDw9vi9Q+ONvw747yeUFzvmn1RlnkOF4ML82TPhBLQwyb4R2atLE1neterXI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1731691610; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HH5f2yuM2bMECV8KwaAKjBVmFwnaT6DkSO6d1yN6l0Y=; b=VjKgEMv6+v+UKZAQIc1vXdGUzXcq72yGtTXpQJiEkCYilA4lsJuORF8eLGHRJhG3eJpnYYP2yZIYzaAD73D8EE5ZkrNWJLe6t11ocQGVlGpoR6gk5eJTScann2Vr7ID4iKqK+EZFoh7ezh8rAVsz3HSm8b61RHFY/ueS/r4kfFI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1731691610339391.99324445094373; Fri, 15 Nov 2024 09:26:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tC05G-0004WQ-Ad; Fri, 15 Nov 2024 12:26:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04u-0004HA-65 for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tC04s-0001Y1-MR for qemu-devel@nongnu.org; Fri, 15 Nov 2024 12:25:51 -0500 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-301-QrVR8gzLPAC3-9tcF04eSQ-1; Fri, 15 Nov 2024 12:25:48 -0500 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 95FC31953956 for ; Fri, 15 Nov 2024 17:25:47 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.102]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E76DA3003B71; Fri, 15 Nov 2024 17:25:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731691549; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HH5f2yuM2bMECV8KwaAKjBVmFwnaT6DkSO6d1yN6l0Y=; b=FwzzXAz2dnVrx0FhgYYidaxJtqIZPT/VJWmMdTiPqjnFTj1J6CbN36GInfsClMyQSzHxJ5 1PLNgsdQp2WWknG2jkTZjsTxgQ5hkA5ZrkpfhJfWWNQc/0cazPgDw648otLgk24Q8WwakM GE0HXAmq1D8iyLf/i3LPvVcXqip2IEc= X-MC-Unique: QrVR8gzLPAC3-9tcF04eSQ-1 X-Mimecast-MFC-AGG-ID: QrVR8gzLPAC3-9tcF04eSQ From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , Markus Armbruster , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v3 9/9] hw: enforce use of static, const string with qdev_new() Date: Fri, 15 Nov 2024 17:25:21 +0000 Message-ID: <20241115172521.504102-10-berrange@redhat.com> In-Reply-To: <20241115172521.504102-1-berrange@redhat.com> References: <20241115172521.504102-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.12, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.658, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1731691612472116600 Since qdev_new() will assert(), it should only be used in scenarios where the caller knows exactly what type it is asking to be created, and can thus be confident in avoiding abstract types. Enforce this by using a macro wrapper which types to paste "" to the type name. This will generate a compile error if not passed a static const string, forcing callers to use qdev_new_dynamic() instead. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Peter Xu --- hw/core/qdev.c | 3 ++- include/hw/qdev-core.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index eceba33222..968fa33a95 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -144,7 +144,8 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bu= s, Error **errp) return true; } =20 -DeviceState *qdev_new(const char *name) +/* Only to be called via the 'qdev_new' macro */ +DeviceState *qdev_new_internal(const char *name) { return DEVICE(object_new_dynamic(name, &error_abort)); } diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 566c5ef277..335dcd31b0 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -448,7 +448,17 @@ compat_props_add(GPtrArray *arr, * * Return: a derived DeviceState object with a reference count of 1. */ -DeviceState *qdev_new(const char *name); + +/* + * NB, qdev_new_internal is just an internal helper, wrapped by + * the qdev_new() macro which prevents invokation unless given + * a static, const string. + * + * Code should call qdev_new(), or qdev_new_dynamic(), not + * qdev_new_internal(). + */ +DeviceState *qdev_new_internal(const char *name); +#define qdev_new(name) qdev_new_internal(name "") =20 /** * qdev_new_dynamic: Create a device on the heap --=20 2.46.0