From nobody Sat Nov 23 19:57:16 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=1730390067; cv=none; d=zohomail.com; s=zohoarc; b=R0VCDnWbHI6ZHqY99EwkBuDiFYqkPYd0U+6iijaEGD/fLKympkUfBIzW/8+PNgNs5Dl5xOnXQebhO85ja995ixZbytVQm9LEIOkq/yKX60lyLww9gVW/J2QvQUEkb/ob2y3lB1I4lQ3qBp2dcQ72YdCSRZ323Mle/t7mAD2Pv98= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1730390067; 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=jx77iETQYs06DrQ8yZJRqTrlVINZpRE4CtA3A5HTGPg=; b=MkUTLLbkxn9CEQf5ZzkJRaAmRGqIItT+mQiq2YW2pwNqgI5SIC1C5YDf2xQPAjpN42zhAh9yKY6LPca7ObaysYg2DYyNN/vWNo77HRl3YmB8EHoUeY2gSKIHZRBSn/bzWee5UCYD71D/BIJuCvn1DpGdapyvWtGYH5SIPd0JNWI= 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 1730390066797435.2728786349858; Thu, 31 Oct 2024 08:54:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t6XUw-0005Na-P5; Thu, 31 Oct 2024 11:54:10 -0400 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 1t6XUv-0005My-73 for qemu-devel@nongnu.org; Thu, 31 Oct 2024 11:54:09 -0400 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 1t6XUt-0006KV-5S for qemu-devel@nongnu.org; Thu, 31 Oct 2024 11:54:08 -0400 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-159-Vm1DpznDO8KEu6coV4aH0g-1; Thu, 31 Oct 2024 11:54:00 -0400 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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 16D83195609D for ; Thu, 31 Oct 2024 15:54:00 +0000 (UTC) Received: from toolbox.redhat.com (unknown [10.42.28.18]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6909D1956054; Thu, 31 Oct 2024 15:53:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730390046; 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=jx77iETQYs06DrQ8yZJRqTrlVINZpRE4CtA3A5HTGPg=; b=FdbAXTx5kBv63+yxTh+vXoX37P4uNeWQdDOwFZA+VVmoxf5gba6bg2VPl9iWKSa63QOxJ0 cPNrTiZ8mp+vuwUtDWDmOTmz8jEa18tzNXoofrAEiXfrv4D19NMJ1bMCn72wonVQxyH8RO GqHqO8rCrf1m8fZyyvdxwQh5z4fMrTs= X-MC-Unique: Vm1DpznDO8KEu6coV4aH0g-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Peter Xu , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [RFC 3/5] convert code to object_new_dynamic() where appropriate Date: Thu, 31 Oct 2024 15:53:48 +0000 Message-ID: <20241031155350.3240361-4-berrange@redhat.com> In-Reply-To: <20241031155350.3240361-1-berrange@redhat.com> References: <20241031155350.3240361-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.0 on 10.30.177.40 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: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.366, 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.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1730390069652116600 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 maintainers existing behaviour. Signed-off-by: Daniel P. Berrang=C3=A9 --- chardev/char.c | 5 ++++- hw/core/bus.c | 2 +- hw/core/cpu-common.c | 2 +- hw/core/qdev.c | 4 ++-- hw/i386/x86-common.c | 5 ++++- hw/i386/xen/xen-pvh.c | 2 +- hw/vfio/common.c | 6 +++++- hw/vfio/container.c | 6 +++++- qom/object_interfaces.c | 7 ++----- qom/qom-qmp-cmds.c | 15 +++++++++------ tests/unit/check-qom-interface.c | 3 ++- tests/unit/test-smp-parse.c | 20 ++++++++++---------- 12 files changed, 46 insertions(+), 31 deletions(-) diff --git a/chardev/char.c b/chardev/char.c index a1722aa076..4de9a496a2 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -996,7 +996,10 @@ 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); + if (!(obj =3D object_new_dynamic(typename, errp))) { + return NULL; + } + =20 chr =3D CHARDEV(obj); chr->handover_yank_instance =3D handover_yank_instance; chr->label =3D g_strdup(id); 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 db36f54d91..a143d8b721 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -162,7 +162,7 @@ DeviceState *qdev_new(const char *name) error_report("unknown type '%s'", name); abort(); } - return DEVICE(object_new(name)); + return DEVICE(object_new_dynamic(name, &error_abort)); } =20 DeviceState *qdev_try_new(const char *name) @@ -170,7 +170,7 @@ DeviceState *qdev_try_new(const char *name) if (!module_object_class_by_name(name)) { return NULL; } - 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 b86c38212e..b75443ec26 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/vfio/common.c b/hw/vfio/common.c index 36d0cf6585..d57097a194 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..5964009a3c 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,10 @@ 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)); + if (!(obj =3D object_new_dynamic(vioc_name, NULL))) { + return NULL; + } + container =3D VFIO_IOMMU_LEGACY(obj); container->fd =3D fd; container->iommu_type =3D iommu_type; return container; diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c index e0833c8bfe..756fca3649 100644 --- a/qom/object_interfaces.c +++ b/qom/object_interfaces.c @@ -102,13 +102,10 @@ 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); + if (!(obj =3D object_new_dynamic(type, errp))) { return NULL; } - - assert(qdict); - obj =3D object_new(type); 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 e91a235347..b1590231d7 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 type '%s' is not a device", + typename); return NULL; } =20 - obj =3D object_new(typename); + if (!(obj =3D object_new_dynamic(typename, errp))) { + return NULL; + } =20 object_property_iter_init(&iter, obj); while ((prop =3D object_property_iter_next(&iter))) { @@ -202,7 +203,9 @@ ObjectPropertyInfoList *qmp_qom_list_properties(const c= har *typename, if (object_class_is_abstract(klass)) { object_class_property_iter_init(&iter, klass); } else { - obj =3D object_new(typename); + if (!(obj =3D object_new_dynamic(typename, errp))) { + return NULL; + } object_property_iter_init(&iter, obj); } while ((prop =3D object_property_iter_next(&iter))) { 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