Require error handling for dynamically created objects
Subsystems (6)
PATCH v2 1/8 qom: refactor checking abstract property when creating instances
In reply to Daniel P. Berrangé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.
--- qom/object.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-)
qom/object.c +20 -10
@@ -559,14 +559,20 @@ static void object_class_property_init_all(Object *obj) } } -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);g_assert (type->instance_size >=sizeof (Object)); -g_assert (type->abstract == false);g_assert (size >= type->instance_size); +if (type->abstract) { +error_setg (errp,"Abstract type '%s' cannot be instantiated" , + type->name); +return false; + } +memset (obj,0 , type->instance_size); obj->class = 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; }void object_initialize (void *data,size_t size,const char *typename) {TypeImpl *type =type_get_or_load_by_name (typename, &error_fatal); -object_initialize_with_type (data, size, type); +object_initialize_with_type (data, size, type, &error_abort); } bool object_initialize_child_with_props(Object *parentobj,
@@ -753,7 +761,7 @@ typedef union { } qemu_max_align_t;#endif -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 = qemu_vfree; } -object_initialize_with_type (obj, size, type); +if (!object_initialize_with_type (obj, size, type, errp)) { +g_free (obj); +return NULL ; + } obj->free = obj_free;return obj;
@@ -785,14 +796,14 @@ static Object *object_new_with_type(Type type)Object *object_new_with_class (ObjectClass *klass) { -return object_new_with_type (klass->type); +return object_new_with_type (klass->type, &error_abort); }Object *object_new (const char *typename) {TypeImpl *ti =type_get_or_load_by_name (typename, &error_fatal); -return object_new_with_type (ti); +return object_new_with_type (ti, &error_abort); }
@@ -829,11 +840,10 @@ Object *object_new_with_propv(const char *typename,return NULL ; } -if (object_class_is_abstract (klass)) { -error_setg (errp,"object type '%s' is abstract" , typename); + obj =object_new_with_type (klass->type, errp); +if (!obj) {return NULL ; } - obj =object_new_with_type (klass->type);if (!object_set_propv (obj, errp, vargs)) { goto error;
Signature · 1 line
2.46.0
PATCH v2 2/8 qom: allow failure of object_new_with_class
In reply to Daniel P. Berrangé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().
--- accel/accel-user.c | 4 +++- hw/core/qdev.c | 2 +- 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 +++++-- 12 files changed, 50 insertions(+), 26 deletions(-)
accel/accel-user.c +3 -1
@@ -9,6 +9,7 @@#include "qemu/osdep.h" #include "qemu/accel.h" +#include "qapi/error.h" AccelState *current_accel (void ) {
@@ -18,7 +19,8 @@ AccelState *current_accel(void)AccelClass *ac =accel_find ("tcg" );g_assert (ac !=NULL ); - accel =ACCEL (object_new_with_class (OBJECT_CLASS (ac))); + accel =ACCEL (object_new_with_class (OBJECT_CLASS (ac), + &error_abort)); }return accel; }
hw/core/qdev.c +1 -1
@@ -155,7 +155,7 @@ DeviceState *qdev_try_new(const char *name)if (!oc) {return NULL ; } -return DEVICE (object_new_with_class (oc)); +return DEVICE (object_new_with_class (oc, &error_abort)); } static QTAILQ_HEAD(, DeviceListener) device_listeners
include/qom/object.h +7 -2
@@ -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);/** * object_new:
net/net.c +2 -1
@@ -948,7 +948,8 @@ GPtrArray *qemu_get_nic_models(const char *device_type)* create this property during instance_init, so we have to create * a temporary instance here to be able to check it. */ -Object *obj =object_new_with_class (OBJECT_CLASS (dc)); +Object *obj =object_new_with_class (OBJECT_CLASS (dc), + &error_abort);if (object_property_find (obj,"netdev" )) {g_ptr_array_add (nic_models, (gpointer )name); }
qom/object.c +2 -2
@@ -794,9 +794,9 @@ static Object *object_new_with_type(Type type, Error **errp)return obj; } -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); }Object *object_new (const char *typename)
qom/object_interfaces.c +3 -4
@@ -102,13 +102,12 @@ Object *user_creatable_add_type(const char *type, const char *id,return NULL ; } -if (object_class_is_abstract (klass)) { -error_setg (errp,"object type '%s' is abstract" , type); +assert (qdict); + obj =object_new_with_class (klass, errp); +if (!obj) {return NULL ; } -assert (qdict); - obj =object_new_with_class (klass);object_set_properties_from_qdict (obj, qdict, v, &local_err);if (local_err) { goto out;
qom/qom-qmp-cmds.c +6 -5
@@ -134,14 +134,15 @@ ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,return NULL ; } -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 ; } - obj =object_new_with_class (klass); + obj =object_new_with_class (klass, errp); +if (!obj) { +return NULL ; + }object_property_iter_init (&iter, obj);while ((prop =object_property_iter_next (&iter))) {
system/vl.c +4 -2
@@ -2117,7 +2117,8 @@ static void qemu_create_machine(QDict *qdict)MachineClass *machine_class =select_machine (qdict, &error_fatal);object_set_machine_compat_props (machine_class->compat_props); - current_machine =MACHINE (object_new_with_class (OBJECT_CLASS (machine_class))); + current_machine =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, QemuOpts *opts, Error **errp) } goto bad; } - accel =ACCEL (object_new_with_class (OBJECT_CLASS (ac))); + accel =ACCEL (object_new_with_class (OBJECT_CLASS (ac), + &error_fatal));object_apply_compat_props (OBJECT (accel)); qemu_opt_foreach(opts, accelerator_set_property, accel,
target/i386/cpu-apic.c +7 -1
@@ -43,12 +43,18 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp) {APICCommonState *apic;APICCommonClass *apic_class =apic_get_class (errp); +Object *apicobj;if (!apic_class) {return ; } - cpu->apic_state =DEVICE (object_new_with_class (OBJECT_CLASS (apic_class))); + apicobj =object_new_with_class (OBJECT_CLASS (apic_class), + errp); +if (!apicobj) { +return ; + } + cpu->apic_state =DEVICE (apicobj);object_property_add_child (OBJECT (cpu),"lapic" ,OBJECT (cpu->apic_state));object_unref (OBJECT (cpu->apic_state));
target/i386/cpu-sysemu.c +8 -3
@@ -156,15 +156,20 @@ static X86CPU *x86_cpu_from_model(const char *model, QObject *props, {X86CPU *xc =NULL ;X86CPUClass *xcc; +Object *xcobj;Error *err =NULL ; xcc =X86_CPU_CLASS (cpu_class_by_name (TYPE_X86_CPU , model));if (xcc ==NULL ) { -error_setg (&err,"CPU model '%s' not found" , model); - goto out; +error_setg (errp,"CPU model '%s' not found" , model); +return NULL ; } - xc =X86_CPU (object_new_with_class (OBJECT_CLASS (xcc))); + xcobj =object_new_with_class (OBJECT_CLASS (xcc), errp); +if (!xcobj) { +return NULL ; + } + xc =X86_CPU (xcobj);if (props) {object_apply_props (OBJECT (xc), props, props_arg_name, &err);if (err) {
target/i386/cpu.c +2 -2
@@ -5973,7 +5973,7 @@ static GSList *get_sorted_cpu_model_list(void)static char *x86_cpu_class_get_model_id (X86CPUClass *xc) { -Object *obj =object_new_with_class (OBJECT_CLASS (xc)); +Object *obj =object_new_with_class (OBJECT_CLASS (xc), &error_abort);char *r =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(X86CPUClass *xcc,return ; } - xc =X86_CPU (object_new_with_class (OBJECT_CLASS (xcc))); + xc =X86_CPU (object_new_with_class (OBJECT_CLASS (xcc), &error_abort));x86_cpu_expand_features (xc, &err);if (err) {
target/s390x/cpu_models_sysemu.c +5 -2
@@ -69,7 +69,7 @@ static void create_cpu_model_list(ObjectClass *klass, void *opaque)if (cpu_list_data->model) {Object *obj;S390CPU *sc; - obj =object_new_with_class (klass); + obj =object_new_with_class (klass, &error_abort); sc =S390_CPU (obj);if (sc->model) { info->has_unavailable_features = true;
@@ -116,7 +116,10 @@ static void cpu_model_from_info(S390CPUModel *model, const CpuModelInfo *info,error_setg (errp,"The CPU definition '%s' requires KVM" , info->name);return ; } - obj =object_new_with_class (oc); + obj =object_new_with_class (oc, errp); +if (!obj) { +return ; + } cpu =S390_CPU (obj);if (!cpu->model) {
Signature · 1 line
2.46.0
PATCH v2 3/8 qom: introduce object_new_dynamic()
In reply to Daniel P. Berrangé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.
--- include/qom/object.h | 27 +++++++++++++++++++++++++++ qom/object.c | 9 +++++++++ 2 files changed, 36 insertions(+)
include/qom/object.h +27 -0
@@ -624,14 +624,41 @@ Object *object_new_with_class(ObjectClass *klass, Error **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); +/** +* 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.
qom/object.c +9 -0
@@ -806,6 +806,15 @@ Object *object_new(const char *typename)return object_new_with_type (ti, &error_abort); } +Object *object_new_dynamic (const char *typename,Error **errp) +{ +TypeImpl *ti =type_get_or_load_by_name (typename, errp); +if (!ti) { +return NULL ; + } + +return object_new_with_type (ti, errp); +} Object *object_new_with_props(const char *typename, Object *parent,
Signature · 1 line
2.46.0
PATCH v2 6/8 qom: introduce qdev_new_dynamic()
In reply to Daniel P. Berrangé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.
Do likewise with qdev_try_new_dynamic() as a counterpart to qdev_try_new().
--- hw/core/qdev.c | 14 +++++++++++ include/hw/qdev-core.h | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
hw/core/qdev.c +14 -0
@@ -149,6 +149,11 @@ DeviceState *qdev_new(const char *name)return DEVICE (object_new_dynamic (name, &error_abort)); } +DeviceState *qdev_new_dynamic (const char *name,Error **errp) +{ +return DEVICE (object_new_dynamic (name, errp)); +} +DeviceState *qdev_try_new (const char *name) {ObjectClass *oc =module_object_class_by_name (name);
@@ -158,6 +163,15 @@ DeviceState *qdev_try_new(const char *name)return DEVICE (object_new_with_class (oc, &error_abort)); } +DeviceState *qdev_try_new_dynamic (const char *name,Error **errp) +{ +ObjectClass *oc =module_object_class_by_name (name); +if (!oc) { +return NULL ; + } +return DEVICE (object_new_with_class (oc, errp)); +} + static QTAILQ_HEAD(, DeviceListener) device_listeners =QTAILQ_HEAD_INITIALIZER (device_listeners);
include/hw/qdev-core.h +54 -0
80 lines not on this page · raw message
Signature · 1 line
2.46.0
PATCH v2 5/8 qom: enforce use of static, const string with object_new()
In reply to Daniel P. Berrangé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.
--- include/qom/object.h | 12 +++++++++++- qom/object.c | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-)
include/qom/object.h +11 -1
@@ -637,7 +637,17 @@ Object *object_new_with_class(ObjectClass *klass, Error **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 "")/** * object_new_dynamic:
qom/object.c +2 -1
@@ -799,7 +799,8 @@ Object *object_new_with_class(ObjectClass *klass, Error **errp)return object_new_with_type (klass->type, errp); } -Object *object_new (const char *typename) +/* Only to be called via the 'object_new' macro */ +Object *object_new_internal (const char *typename) {TypeImpl *ti =type_get_or_load_by_name (typename, &error_fatal);
Signature · 1 line
2.46.0
v2 cover Require error handling for dynamically created objects
NB, this series is targetting 10.0, NOT for 9.2 freeze.
With code like
Object *obj = object_new(TYPE_BLAH)
the caller can be pretty confident that they will successfully create an object instance of TYPE_BLAH. They know exactly what type has been requested, so it passing an abstract type for example, it is a clear programmer error that they'll get an assertion failure.
Conversely with code like
void somefunc(const char *typename) {
Object * obj = object_new(typename)
...
}all bets are off, because the call of object_new() knows nothing about what 'typename' resolves to. It could easily be an abstract type. As a result, many code paths have added a manual check ahead of time
if (object_class_is_abstract(typename)) {
error_setg(errp, ....)
}...except for where we forget to do this, such as qdev_new().
Overall 'object_new' is a bad design because it is inherantly unsafe to call with unvalidated typenames.
This problem is made worse by the proposal to introduce the idea of 'singleton' classes[1].
Thus, this series suggests a way to improve safety at build time. The core idea is to allow 'object_new' to continue to be used *if-and-only-if* given a static, const string, because that scenario indicates the caller is aware of what type they are creating at build time.
A new 'object_new_dynamic' method is proposed for cases where the typename is dynamically chosen at runtime. This method has an "Error **errp" parameter, which can report when an abstract type is created, leaving the assert()s only for scenarios which are unambiguous programmer errors.
With a little macro magic, we guarantee a compile error is generated if 'object_new' is called with a dynamic type, forcing all potentially unsafe code over to object_new_dynamic.
This is more tractable than adding 'Error **errp' to 'object_new' as only a handful of places use a dynamic type name.
With this series, my objections to Peter Xu's singleton series[1] would be largely nullified.
[1] https://lists.nongnu.org/archive/html/qemu-devel/2024-10/msg05524.html
Changed in v2:
* Removed "RFC" tag
* Converted code in all non-x86_64 targets
* Converted qdev_new to same pattern as object_new
* Ensured test suites work now
Daniel P. Berrangé (8):
qom: refactor checking abstract property when creating instances
qom: allow failure of object_new_with_class
qom: introduce object_new_dynamic()
convert code to object_new_dynamic() where appropriate
qom: enforce use of static, const string with object_new()
qom: introduce qdev_new_dynamic()
convert code to qdev_new_dynamic() where appropriate
hw: enforce use of static, const string with qdev_new()
accel/accel-user.c | 4 +- chardev/char.c | 6 ++- hw/arm/aspeed.c | 6 +-- hw/arm/exynos4210.c | 3 +- hw/arm/highbank.c | 2 +- hw/arm/integratorcp.c | 2 +- hw/arm/mps3r.c | 3 +- hw/arm/npcm7xx_boards.c | 2 +- hw/arm/realview.c | 3 +- hw/arm/sbsa-ref.c | 7 +-- hw/arm/versatilepb.c | 2 +- hw/arm/vexpress.c | 4 +- hw/arm/virt.c | 10 ++-- hw/arm/xilinx_zynq.c | 3 +- hw/audio/soundhw.c | 2 +- hw/block/xen-block.c | 7 ++- hw/core/bus.c | 2 +- hw/core/cpu-common.c | 2 +- hw/core/qdev.c | 24 +++++++-- hw/core/sysbus.c | 2 +- hw/i2c/core.c | 2 +- hw/i386/x86-common.c | 5 +- hw/i386/xen/xen-pvh.c | 2 +- hw/intc/xics.c | 5 +- hw/isa/isa-bus.c | 4 +- hw/mips/cps.c | 3 +- hw/pci-host/pnv_phb.c | 5 +- hw/pci/pci.c | 2 +- hw/ppc/e500.c | 2 +- hw/ppc/pnv.c | 6 +-- 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 | 10 +++- hw/scsi/scsi-bus.c | 5 +- hw/sparc/leon3.c | 2 +- hw/sparc/sun4m.c | 2 +- hw/sparc64/sparc64.c | 2 +- hw/ssi/ssi.c | 2 +- hw/vfio/common.c | 6 ++- hw/vfio/container.c | 7 ++- include/hw/qdev-core.h | 78 ++++++++++++++++++++++++++- include/hw/usb.h | 4 +- include/qom/object.h | 48 +++++++++++++++-- net/net.c | 7 +-- qom/object.c | 44 ++++++++++----- qom/object_interfaces.c | 7 ++- qom/qom-qmp-cmds.c | 16 +++--- system/qdev-monitor.c | 5 +- system/vl.c | 6 ++- target/arm/arm-qmp-cmds.c | 5 +- target/i386/cpu-apic.c | 8 ++- target/i386/cpu-sysemu.c | 11 ++-- target/i386/cpu.c | 4 +- target/loongarch/loongarch-qmp-cmds.c | 5 +- target/mips/cpu.c | 2 +- target/riscv/riscv-qmp-cmds.c | 5 +- target/s390x/cpu_models_sysemu.c | 7 ++- target/xtensa/cpu.c | 2 +- tests/unit/check-qom-interface.c | 3 +- tests/unit/test-smp-parse.c | 20 +++---- 62 files changed, 353 insertions(+), 116 deletions(-)
Signature · 1 line
2.46.0
PATCH v2 7/8 convert code to qdev_new_dynamic() where appropriate
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.
--- 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 | 4 ++-- 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 | 4 ++-- include/qom/object.h | 4 ++-- net/net.c | 4 ++-- system/qdev-monitor.c | 5 ++++- 19 files changed, 36 insertions(+), 25 deletions(-)
hw/arm/aspeed.c +1 -1
@@ -320,7 +320,7 @@ void aspeed_board_init_flashes(AspeedSMCState *s, const char *flashtype,DriveInfo *dinfo =drive_get (IF_MTD ,0 , unit0 + i);DeviceState *dev; - dev =qdev_new (flashtype); + dev =qdev_new_dynamic (flashtype, &error_fatal);if (dinfo) {qdev_prop_set_drive (dev,"drive" ,blk_by_legacy_dinfo (dinfo)); }
hw/arm/npcm7xx_boards.c +1 -1
@@ -83,7 +83,7 @@ static void npcm7xx_connect_flash(NPCM7xxFIUState *fiu, int cs_no,DeviceState *flash;qemu_irq flash_cs; - flash =qdev_new (flash_type); + flash =qdev_new_dynamic (flash_type, &error_fatal);if (dinfo) {qdev_prop_set_drive (flash,"drive" ,blk_by_legacy_dinfo (dinfo)); }
hw/arm/sbsa-ref.c +2 -2
@@ -419,7 +419,7 @@ static void create_its(SBSAMachineState *sms)const char *itsclass =its_class_name ();DeviceState *dev; - dev =qdev_new (itsclass); + dev =qdev_new_dynamic (itsclass, &error_fatal);object_property_set_link (OBJECT (dev),"parent-gicv3" ,OBJECT (sms->gic), &error_abort);
@@ -438,7 +438,7 @@ static void create_gic(SBSAMachineState *sms, MemoryRegion *mem) gictype =gicv3_class_name (); - sms->gic =qdev_new (gictype); + sms->gic =qdev_new_dynamic (gictype, &error_fatal);qdev_prop_set_uint32 (sms->gic,"revision" ,3 );qdev_prop_set_uint32 (sms->gic,"num-cpu" , smp_cpus);/*
hw/arm/vexpress.c +1 -1
@@ -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 =qdev_new (privdev); + dev =qdev_new_dynamic (privdev, &error_fatal);qdev_prop_set_uint32 (dev,"num-cpu" , smp_cpus); busdev =SYS_BUS_DEVICE (dev);sysbus_realize_and_unref (busdev, &error_fatal);
hw/arm/virt.c +2 -2
@@ -715,7 +715,7 @@ static void create_its(VirtMachineState *vms)return ; } - dev =qdev_new (itsclass); + dev =qdev_new_dynamic (itsclass, &error_fatal);object_property_set_link (OBJECT (dev),"parent-gicv3" ,OBJECT (vms->gic), &error_abort);
@@ -791,7 +791,7 @@ static void create_gic(VirtMachineState *vms, MemoryRegion *mem)default :g_assert_not_reached (); } - vms->gic =qdev_new (gictype); + vms->gic =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
hw/audio/soundhw.c +1 -1
@@ -132,7 +132,7 @@ void soundhw_init(void) }if (c->typename) { -DeviceState *dev =qdev_new (c->typename); +DeviceState *dev =qdev_new_dynamic (c->typename, &error_fatal);qdev_prop_set_string (dev,"audiodev" , audiodev_id);qdev_realize_and_unref (dev, bus, &error_fatal); }else {
hw/block/xen-block.c +6 -1
@@ -1034,6 +1034,7 @@ static void xen_block_device_create(XenBackendInstance *backend,XenDevice *xendev =NULL ;const char *type;XenBlockDevice *blockdev; +DeviceState *dev;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(XenBackendInstance *backend, goto fail; } - xendev =XEN_DEVICE (qdev_new (type)); + dev =qdev_new_dynamic (type, errp); +if (!dev) { + goto fail; + } + xendev =XEN_DEVICE (dev); blockdev =XEN_BLOCK_DEVICE (xendev); if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
hw/core/sysbus.c +1 -1
@@ -221,7 +221,7 @@ DeviceState *sysbus_create_varargs(const char *name,qemu_irq irq;int n; - dev =qdev_new (name); + dev =qdev_new_dynamic (name, &error_fatal); s =SYS_BUS_DEVICE (dev);sysbus_realize_and_unref (s, &error_fatal);if (addr != (hwaddr)-1 ) {
hw/i2c/core.c +1 -1
@@ -369,7 +369,7 @@ I2CSlave *i2c_slave_new(const char *name, uint8_t addr) {DeviceState *dev; - dev =qdev_new (name); + dev =qdev_new_dynamic (name, &error_fatal);qdev_prop_set_uint8 (dev,"address" , addr);return I2C_SLAVE (dev); }
hw/isa/isa-bus.c +2 -2
@@ -155,12 +155,12 @@ int isa_register_portio_list(ISADevice *dev,ISADevice *isa_new (const char *name) { -return ISA_DEVICE (qdev_new (name)); +return ISA_DEVICE (qdev_new_dynamic (name, &error_fatal)); }ISADevice *isa_try_new (const char *name) { -return ISA_DEVICE (qdev_try_new (name)); +return ISA_DEVICE (qdev_try_new_dynamic (name, &error_fatal)); }ISADevice *isa_create_simple (ISABus *bus,const char *name)
hw/pci/pci.c +1 -1
@@ -2186,7 +2186,7 @@ static PCIDevice *pci_new_internal(int devfn, bool multifunction, {DeviceState *dev; - dev =qdev_new (name); + dev =qdev_new_dynamic (name, &error_fatal);qdev_prop_set_int32 (dev,"addr" , devfn);qdev_prop_set_bit (dev,"multifunction" , multifunction);return PCI_DEVICE (dev);
hw/ppc/pnv.c +1 -1
@@ -1115,7 +1115,7 @@ static void pnv_init(MachineState *machine) pnv->chips = g_new0(PnvChip *, pnv->num_chips);for (i =0 ; i < pnv->num_chips; i++) {char chip_name[32 ]; -Object *chip =OBJECT (qdev_new (chip_typename)); +Object *chip =OBJECT (qdev_new_dynamic (chip_typename, &error_fatal));uint64_t chip_ram_size =pnv_chip_get_ram_size (pnv, i); pnv->chips[i] =PNV_CHIP (chip);
hw/s390x/s390-virtio-ccw.c +1 -1
@@ -242,7 +242,7 @@ static void s390_create_sclpconsole(SCLPDevice *sclp,BusState *ev_fac_bus =sclp_get_event_facility_bus (ef);DeviceState *dev; - dev =qdev_new (type); + dev =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);
hw/scsi/scsi-bus.c +4 -1
@@ -396,7 +396,10 @@ SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, driver ="scsi-hd" ; } } - dev =qdev_new (driver); + dev =qdev_new_dynamic (driver, errp); +if (!dev) { +return NULL ; + } name =g_strdup_printf ("legacy[%d]" , unit);object_property_add_child (OBJECT (bus), name,OBJECT (dev));g_free (name);
hw/ssi/ssi.c +1 -1
@@ -141,7 +141,7 @@ bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp)DeviceState *ssi_create_peripheral (SSIBus *bus,const char *name) { -DeviceState *dev =qdev_new (name); +DeviceState *dev =qdev_new_dynamic (name, &error_fatal);ssi_realize_and_unref (dev, bus, &error_fatal);return dev;
include/hw/usb.h +2 -2
@@ -581,12 +581,12 @@ void usb_pcap_data(USBPacket *p, bool setup);static inline USBDevice *usb_new (const char *name) { -return USB_DEVICE (qdev_new (name)); +return USB_DEVICE (qdev_new_dynamic (name, &error_fatal)); }static inline USBDevice *usb_try_new (const char *name) { -return USB_DEVICE (qdev_try_new (name)); +return USB_DEVICE (qdev_try_new_dynamic (name, &error_fatal)); }static inline bool usb_realize_and_unref (USBDevice *dev,USBBus *bus,Error **errp)
include/qom/object.h +2 -2
@@ -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. */
net/net.c +2 -2
@@ -1175,7 +1175,7 @@ DeviceState *qemu_create_nic_device(const char *typename, bool match_default,return NULL ; } - dev =qdev_new (typename); + dev =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 ; } - dev =qdev_new (model); + dev =qdev_new_dynamic (model, &error_fatal);qdev_set_nic_properties (dev, nd);qdev_realize_and_unref (dev, bus, &error_fatal); }
system/qdev-monitor.c +4 -1
@@ -686,7 +686,10 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts, }/* create device */ - dev =qdev_new (driver); + dev =qdev_new_dynamic (driver, errp); +if (!dev) { +return NULL ; + }/* Check whether the hotplug is allowed by the machine */ if (phase_check (PHASE_MACHINE_READY )) {
Signature · 1 line
2.46.0
PATCH v2 8/8 hw: enforce use of static, const string with qdev_new()
In reply to Daniel P. Berrangé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.
--- hw/core/qdev.c | 6 ++++-- include/hw/qdev-core.h | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-)
hw/core/qdev.c +4 -2
@@ -144,7 +144,8 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)return true; } -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)); }
@@ -154,7 +155,8 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp)return DEVICE (object_new_dynamic (name, errp)); } -DeviceState *qdev_try_new (const char *name) +/* Only to be called via the 'qdev_try_new' macro */ +DeviceState *qdev_try_new_internal (const char *name) {ObjectClass *oc =module_object_class_by_name (name);if (!oc) {
include/hw/qdev-core.h +22 -2
@@ -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 "")/** * qdev_new_dynamic: Create a device on the heap
@@ -487,7 +497,17 @@ DeviceState *qdev_new_dynamic(const char *name, Error **errp);* 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); + +/* +* NB, qdev_try_new_internal is just an internal helper, wrapped by +* the qdev_try_new() macro which prevents invokation unless given +* a static, const string. +* +* Code should call qdev_try_new(), or qdev_try_new_dynamic(), not +* qdev_try_new_internal(). +*/ +DeviceState *qdev_try_new_internal (const char *name); +#define qdev_try_new (name) qdev_try_new_internal(name "")/** * qdev_try_new_dynamic: Try to create a device on the heap
Signature · 1 line
2.46.0
PATCH v2 4/8 convert code to object_new_dynamic() where appropriate
In reply to Daniel P. Berrangé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.
--- 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(-)
chardev/char.c +5 -1
@@ -996,7 +996,11 @@ static Chardev *chardev_new(const char *id, const char *typename,assert (g_str_has_prefix (typename,"chardev-" ));assert (id); - obj =object_new (typename); + obj =object_new_dynamic (typename, errp); +if (!obj) { +return NULL ; + } + chr =CHARDEV (obj); chr->handover_yank_instance = handover_yank_instance; chr->label =g_strdup (id);
hw/arm/aspeed.c +2 -2
@@ -385,7 +385,7 @@ static void aspeed_machine_init(MachineState *machine)DriveInfo *emmc0 =NULL ;bool boot_emmc; - bmc->soc =ASPEED_SOC (object_new (amc->soc_name)); + bmc->soc =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 =ASPEED_SOC_GET_CLASS (bmc->soc);
@@ -1594,7 +1594,7 @@ static void aspeed_minibmc_machine_init(MachineState *machine) sysclk =clock_new (OBJECT (machine),"SYSCLK" );clock_set_hz (sysclk,SYSCLK_FRQ ); - bmc->soc =ASPEED_SOC (object_new (amc->soc_name)); + bmc->soc =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);
hw/arm/exynos4210.c +2 -1
@@ -554,7 +554,8 @@ static void exynos4210_realize(DeviceState *socdev, Error **errp)int i, n;for (n =0 ; n <EXYNOS4210_NCPUS ; n++) { -Object *cpuobj =object_new (ARM_CPU_TYPE_NAME ("cortex-a9" )); +Object *cpuobj =object_new_dynamic (ARM_CPU_TYPE_NAME ("cortex-a9" ), + &error_fatal);object_property_add_child (OBJECT (s),"cpu[*]" , cpuobj);/* By default A9 CPUs have EL3 enabled. This board does not currently
hw/arm/highbank.c +1 -1
@@ -206,7 +206,7 @@ static void calxeda_init(MachineState *machine, enum cxmachines machine_id)Object *cpuobj;ARMCPU *cpu; - cpuobj =object_new (machine->cpu_type); + cpuobj =object_new_dynamic (machine->cpu_type, &error_abort); cpu =ARM_CPU (cpuobj);object_property_add_child (OBJECT (machine),"cpu[*]" , cpuobj);
hw/arm/integratorcp.c +1 -1
@@ -595,7 +595,7 @@ static void integratorcp_init(MachineState *machine)DriveInfo *dinfo;int i; - cpuobj =object_new (machine->cpu_type); + cpuobj =object_new_dynamic (machine->cpu_type, &error_fatal);/* By default ARM1176 CPUs have EL3 enabled. This board does not * currently support EL3 so the CPU EL3 property is disabled before
hw/arm/mps3r.c +2 -1
@@ -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 ); - mms->cpu[i] =object_new (machine->cpu_type); + mms->cpu[i] =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",
hw/arm/realview.c +2 -1
@@ -116,7 +116,8 @@ static void realview_init(MachineState *machine, }for (n =0 ; n < smp_cpus; n++) { -Object *cpuobj =object_new (machine->cpu_type); +Object *cpuobj =object_new_dynamic (machine->cpu_type, + &error_fatal);/* By default A9,A15 and ARM1176 CPUs have EL3 enabled. This board * does not currently support EL3 so the CPU EL3 property is disabled
hw/arm/sbsa-ref.c +2 -1
@@ -767,7 +767,8 @@ static void sbsa_ref_init(MachineState *machine)break ; } - cpuobj =object_new (possible_cpus->cpus[n].type); + cpuobj =object_new_dynamic (possible_cpus->cpus[n].type, + &error_fatal);object_property_set_int (cpuobj,"mp-affinity" , possible_cpus->cpus[n].arch_id,NULL );
hw/arm/versatilepb.c +1 -1
@@ -205,7 +205,7 @@ static void versatile_init(MachineState *machine, int board_id)exit (1 ); } - cpuobj =object_new (machine->cpu_type); + cpuobj =object_new_dynamic (machine->cpu_type, &error_fatal);/* By default ARM1176 CPUs have EL3 enabled. This board does not * currently support EL3 so the CPU EL3 property is disabled before
hw/arm/vexpress.c +1 -1
@@ -217,7 +217,7 @@ static void init_cpus(MachineState *ms, const char *cpu_type,/* Create the actual CPUs */ for (n =0 ; n < smp_cpus; n++) { -Object *cpuobj =object_new (cpu_type); +Object *cpuobj =object_new_dynamic (cpu_type, &error_abort);if (!secure) {object_property_set_bool (cpuobj,"has_el3" , false,NULL );
hw/arm/virt.c +4 -2
@@ -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 =object_new (possible_cpus->cpus[0 ].type); + cpuobj =object_new_dynamic (possible_cpus->cpus[0 ].type, + &error_fatal); armcpu =ARM_CPU (cpuobj); pa_bits =arm_pamax (armcpu);
@@ -2231,7 +2232,8 @@ static void machvirt_init(MachineState *machine)break ; } - cpuobj =object_new (possible_cpus->cpus[n].type); + cpuobj =object_new_dynamic (possible_cpus->cpus[n].type, + &error_fatal);object_property_set_int (cpuobj,"mp-affinity" , possible_cpus->cpus[n].arch_id,NULL );
hw/arm/xilinx_zynq.c +2 -1
@@ -218,7 +218,8 @@ static void zynq_init(MachineState *machine) }for (n =0 ; n < smp_cpus; n++) { -Object *cpuobj =object_new (machine->cpu_type); +Object *cpuobj =object_new_dynamic (machine->cpu_type, + &error_fatal);object_property_set_int (cpuobj,"midr" ,ZYNQ_BOARD_MIDR , &error_fatal);
hw/core/bus.c +1 -1
@@ -163,7 +163,7 @@ BusState *qbus_new(const char *typename, DeviceState *parent, const char *name) {BusState *bus; - bus =BUS (object_new (typename)); + bus =BUS (object_new_dynamic (typename, &error_abort));qbus_init_internal (bus, parent, name);return bus;
hw/core/cpu-common.c +1 -1
@@ -57,7 +57,7 @@ bool cpu_exists(int64_t id)CPUState *cpu_create (const char *typename) {Error *err =NULL ; -CPUState *cpu =CPU (object_new (typename)); +CPUState *cpu =CPU (object_new_dynamic (typename, &error_abort));if (!qdev_realize (DEVICE (cpu),NULL , &err)) {error_report_err (err);object_unref (OBJECT (cpu));
hw/core/qdev.c +1 -1
@@ -146,7 +146,7 @@ bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp)DeviceState *qdev_new (const char *name) { -return DEVICE (object_new (name)); +return DEVICE (object_new_dynamic (name, &error_abort)); }DeviceState *qdev_try_new (const char *name)
hw/i386/x86-common.c +4 -1
@@ -55,7 +55,10 @@ static size_t pvh_start_addr;static void x86_cpu_new (X86MachineState *x86ms,int64_t apic_id,Error **errp) { -Object *cpu =object_new (MACHINE (x86ms)->cpu_type); +Object *cpu =object_new_dynamic (MACHINE (x86ms)->cpu_type, errp); +if (!cpu) { +return ; + }if (!object_property_set_uint (cpu,"apic-id" , apic_id, errp)) { goto out;
hw/i386/xen/xen-pvh.c +1 -1
@@ -28,7 +28,7 @@ struct XenPVHx86State {static DeviceState *xen_pvh_cpu_new (MachineState *ms,int64_t apic_id) { -Object *cpu =object_new (ms->cpu_type); +Object *cpu =object_new_dynamic (ms->cpu_type, &error_abort);object_property_add_child (OBJECT (ms),"cpu[*]" , cpu);object_property_set_uint (cpu,"apic-id" , apic_id, &error_fatal);
hw/intc/xics.c +4 -1
@@ -377,7 +377,10 @@ Object *icp_create(Object *cpu, const char *type, XICSFabric *xi, Error **errp) {Object *obj; - obj =object_new (type); + obj =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);
hw/mips/cps.c +2 -1
@@ -74,7 +74,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp) }for (int i =0 ; i < s->num_vp; i++) { -MIPSCPU *cpu =MIPS_CPU (object_new (s->cpu_type)); +MIPSCPU *cpu =MIPS_CPU (object_new_dynamic (s->cpu_type, + &error_abort));CPUMIPSState *env = &cpu->env; object_property_set_bool(OBJECT(cpu), "big-endian", s->cpu_is_bigendian,
hw/pci-host/pnv_phb.c +4 -1
@@ -131,7 +131,10 @@ static void pnv_phb_realize(DeviceState *dev, Error **errp)g_assert_not_reached (); } - phb->backend =object_new (phb_typename); + phb->backend =object_new_dynamic (phb_typename, errp); +if (!phb->backend) { +return ; + }object_property_add_child (OBJECT (dev),"phb-backend" , phb->backend);/* Passthrough child device properties to the proxy device */
hw/ppc/e500.c +1 -1
@@ -939,7 +939,7 @@ void ppce500_init(MachineState *machine)PowerPCCPU *cpu;CPUState *cs; - cpu =POWERPC_CPU (object_new (machine->cpu_type)); + cpu =POWERPC_CPU (object_new_dynamic (machine->cpu_type, &error_fatal)); env = &cpu->env; cs =CPU (cpu);
hw/ppc/pnv.c +2 -2
@@ -1459,7 +1459,7 @@ static void pnv_chip_power8_instance_init(Object *obj) chip8->num_phbs = pcc->num_phbs;for (i =0 ; i < chip8->num_phbs; i++) { -Object *phb =object_new (TYPE_PNV_PHB ); +Object *phb =object_new_dynamic (TYPE_PNV_PHB , &error_fatal);/* * We need the chip to parent the PHB to allow the DT
@@ -2376,7 +2376,7 @@ static void pnv_chip_core_realize(PnvChip *chip, Error **errp)continue ; } - pnv_core =PNV_CORE (object_new (typename)); + pnv_core =PNV_CORE (object_new_dynamic (typename, &error_fatal));snprintf (core_name,sizeof (core_name),"core[%d]" , core_hwid);object_property_add_child (OBJECT (chip), core_name,OBJECT (pnv_core));
hw/ppc/pnv_core.c +4 -1
@@ -361,7 +361,10 @@ static void pnv_core_realize(DeviceState *dev, Error **errp)PowerPCCPU *cpu;PnvCPUState *pnv_cpu; - obj =object_new (typename); + obj =object_new_dynamic (typename, &local_err); +if (!obj) { + goto err; + } cpu =POWERPC_CPU (obj); pc->threads[i] =POWERPC_CPU (obj);
hw/ppc/spapr.c +1 -1
@@ -2693,7 +2693,7 @@ static void spapr_init_cpus(SpaprMachineState *spapr) }if (i < boot_cores_nr) { -Object *core =object_new (type); +Object *core =object_new_dynamic (type, &error_fatal);int nr_threads = smp_threads;/* Handle the partially filled core for older machine types */
hw/ppc/spapr_cpu_core.c +4 -1
@@ -298,7 +298,10 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)PowerPCCPU *cpu;CPUPPCState *env; - obj =object_new (scc->cpu_type); + obj =object_new_dynamic (scc->cpu_type, errp); +if (!obj) { +return NULL ; + } cs =CPU (obj); cpu =POWERPC_CPU (obj);
hw/ppc/spapr_drc.c +1 -1
@@ -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 =SPAPR_DR_CONNECTOR (object_new (type)); +SpaprDrc *drc =SPAPR_DR_CONNECTOR (object_new_dynamic (type, &error_fatal)); g_autofree char *prop_name = NULL; drc->id = id;
hw/s390x/s390-virtio-ccw.c +7 -1
@@ -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 =S390_CPU (object_new (typename)); +Object *cpuobj =object_new_dynamic (typename, errp); +S390CPU *cpu =NULL ;S390CPU *ret =NULL ; +if (!cpuobj) { +return NULL ; + } + + cpu =S390_CPU (cpuobj);if (!object_property_set_int (OBJECT (cpu),"core-id" , core_id, errp)) { goto out; }
hw/sparc/leon3.c +1 -1
@@ -277,7 +277,7 @@ static void leon3_generic_hw_init(MachineState *machine)for (i =0 ; i < machine->smp.cpus; i++) {/* Init CPU */ - cpu =SPARC_CPU (object_new (machine->cpu_type)); + cpu =SPARC_CPU (object_new_dynamic (machine->cpu_type, &error_fatal));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);
hw/sparc/sun4m.c +1 -1
@@ -802,7 +802,7 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,SPARCCPU *cpu;CPUSPARCState *env; - cpu =SPARC_CPU (object_new (cpu_type)); + cpu =SPARC_CPU (object_new_dynamic (cpu_type, &error_fatal)); env = &cpu->env;qemu_register_reset (sun4m_cpu_reset, cpu);
hw/sparc64/sparc64.c +1 -1
@@ -272,7 +272,7 @@ SPARCCPU *sparc64_cpu_devinit(const char *cpu_type, uint64_t prom_addr)uint32_t stick_frequency =100 *1000000 ;uint32_t hstick_frequency =100 *1000000 ; - cpu =SPARC_CPU (object_new (cpu_type)); + cpu =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);
hw/vfio/common.c +5 -1
@@ -1550,7 +1550,11 @@ bool vfio_attach_device(char *name, VFIODevice *vbasedev,if (!vbasedev->mdev) { - hiod =HOST_IOMMU_DEVICE (object_new (ops->hiod_typename)); +Object *obj =object_new_dynamic (ops->hiod_typename, errp); +if (!obj) { +return false; + } + hiod =HOST_IOMMU_DEVICE (obj); vbasedev->hiod = hiod; }
hw/vfio/container.c +6 -1
@@ -419,6 +419,7 @@ static VFIOContainer *vfio_create_container(int fd, VFIOGroup *group, {int iommu_type;const char *vioc_name; +Object *obj;VFIOContainer *container; iommu_type =vfio_get_iommu_type (fd, errp);
@@ -432,7 +433,11 @@ static VFIOContainer *vfio_create_container(int fd, VFIOGroup *group, vioc_name =vfio_get_iommu_class_name (iommu_type); - container =VFIO_IOMMU_LEGACY (object_new (vioc_name)); + obj =object_new_dynamic (vioc_name, errp); +if (!obj) { +return NULL ; + } + container =VFIO_IOMMU_LEGACY (obj); container->fd = fd; container->iommu_type = iommu_type;return container;
qom/qom-qmp-cmds.c +4 -1
@@ -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 =object_new (typename); + obj =object_new_dynamic (typename, errp); +if (!obj) { +return NULL ; + }object_property_iter_init (&iter, obj); }while ((prop =object_property_iter_next (&iter))) {
target/arm/arm-qmp-cmds.c +4 -1
@@ -150,7 +150,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type, } } - obj =object_new (object_class_get_name (oc)); + obj =object_new_dynamic (object_class_get_name (oc), errp); +if (!obj) { +return NULL ; + }if (model->props) {Visitor *visitor;
target/loongarch/loongarch-qmp-cmds.c +4 -1
@@ -83,7 +83,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,return NULL ; } - obj =object_new (object_class_get_name (oc)); + obj =object_new_dynamic (object_class_get_name (oc), errp); +if (!obj) { +return NULL ; + } expansion_info =g_new0 (CpuModelExpansionInfo,1 ); expansion_info->model =g_malloc0 (sizeof (*expansion_info->model));
target/mips/cpu.c +1 -1
@@ -648,7 +648,7 @@ MIPSCPU *mips_cpu_create_with_clock(const char *cpu_type, Clock *cpu_refclk, {DeviceState *cpu; - cpu =DEVICE (object_new (cpu_type)); + cpu =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);
target/riscv/riscv-qmp-cmds.c +4 -1
@@ -188,7 +188,10 @@ CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,return NULL ; } - obj =object_new (object_class_get_name (oc)); + obj =object_new_dynamic (object_class_get_name (oc), errp); +if (!obj) { +return NULL ; + }riscv_check_if_cpu_available (RISCV_CPU (obj), &local_err);if (local_err !=NULL ) {
target/xtensa/cpu.c +1 -1
@@ -204,7 +204,7 @@ XtensaCPU *xtensa_cpu_create_with_clock(const char *cpu_type, Clock *cpu_refclk) {DeviceState *cpu; - cpu =DEVICE (object_new (cpu_type)); + cpu =DEVICE (object_new_dynamic (cpu_type, &error_abort));qdev_connect_clock_in (cpu,"clk-in" , cpu_refclk);qdev_realize (cpu,NULL , &error_abort);
tests/unit/check-qom-interface.c +2 -1
@@ -13,6 +13,7 @@#include "qom/object.h" #include "qemu/module.h" +#include "qapi/error.h" #define TYPE_TEST_IF "test-interface"
@@ -67,7 +68,7 @@ static const TypeInfo intermediate_impl_info = {static void test_interface_impl (const char *type) { -Object *obj =object_new (type); +Object *obj =object_new_dynamic (type, &error_abort);TestIf *iobj =TEST_IF (obj);TestIfClass *ioc =TEST_IF_GET_CLASS (iobj);
tests/unit/test-smp-parse.c +10 -10
@@ -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 = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1027,7 +1027,7 @@ static void test_generic_valid(const void *opaque)static void test_generic_invalid (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1046,7 +1046,7 @@ static void test_generic_invalid(const void *opaque)static void test_with_modules (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1096,7 +1096,7 @@ static void test_with_modules(const void *opaque)static void test_with_dies (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -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 = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -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 = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1257,7 +1257,7 @@ static void test_with_clusters(const void *opaque)static void test_with_books (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1307,7 +1307,7 @@ static void test_with_books(const void *opaque)static void test_with_drawers (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -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 = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
@@ -1418,7 +1418,7 @@ static void test_with_drawers_books(const void *opaque)static void test_full_topo (const void *opaque) {const char *machine_type = opaque; -Object *obj =object_new (machine_type); +Object *obj =object_new_dynamic (machine_type, &error_abort);MachineState *ms =MACHINE (obj);MachineClass *mc =MACHINE_GET_CLASS (obj);SMPTestData data = {};
Signature · 1 line
2.46.0
Re: [PATCH v2 1/8] qom: refactor checking abstract property when creating instances
In reply to Daniel P. BerrangéQuoted 10 lines · Daniel P. Berrangé
qom/object.c +1
-1@@ -753,7 +761,7 @@ typedef union { } qemu_max_align_t;#endif -static Object *object_new_with_type (Type type) +static Object *object_new_with_type (Type type,Error **errp) {Object *obj;size_t size, align;
qom/object.c static Object *object_new_with_type(Type type) · line 789
Quoted 6 lines
qom/object.c +2
-1@@ -777,7 +785,10 @@ static Object *object_new_with_type(Type type) obj_free = qemu_vfree; } -object_initialize_with_type (obj, size, type); +if (!object_initialize_with_type (obj, size, type, errp)) { +g_free (obj);
obj_free(obj)?
Quoted 5 lines
qom/object.c +2
-0+return NULL ; + } obj->free = obj_free;return obj;
Signature · 1 line
Peter Xu
Re: [PATCH v2 2/8] qom: allow failure of object_new_with_class
In reply to Daniel P. BerrangéQuoted 11 lines · Daniel P. Berrangé
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_abortif the class parameter choice is not driven by untrusted user input.This conversion allows removal of any
object_class_is_abstract()checks immediately beforeobject_new_with_class().Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v2 3/8] qom: introduce object_new_dynamic()
In reply to Daniel P. BerrangéQuoted 6 lines · Daniel P. Berrangé
object_new()has a failure scenario where it willassert()if given an abstract type. Callers which are creating objects based on user input, or unknown/untrusted type names, must manually check the result ofobject_class_is_abstract()before callingobject_new()to propagate an Error, instead of asserting.
Quoted 6 lines
Introduce a
object_new_dynamic()method which is a counterpart toobject_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é <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v2 4/8] convert code to object_new_dynamic() where appropriate
In reply to Daniel P. BerrangéQuoted 11 lines · Daniel P. Berrangé
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_abortor&error_fatalwill maintain existnig behaviour.Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v2 5/8] qom: enforce use of static, const string with object_new()
In reply to Daniel P. BerrangéQuoted 9 lines · Daniel P. Berrangé
Since
object_new()willassert(), 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.Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v2 6/8] qom: introduce qdev_new_dynamic()
In reply to Daniel P. BerrangéQuoted 6 lines · Daniel P. Berrangé
qdev_new()has a failure scenario where it willassert()if given an abstract type. Callers which are creating qdevs based on user input, or unknown/untrusted type names, must manually check the result ofqdev_class_is_abstract()before callingqdev_new()to propagate an Error, instead of asserting.
Quoted 7 lines
Introduce a
qdev_new_dynamic()method which is a counterpart toqdev_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.Do likewise with
qdev_try_new_dynamic()as a counterpart toqdev_try_new().
Since at it, would it make sense to simply replace qdev_try_new() with qdev_new_dynamic(), assuming it plays similar role of "it can fail" version of qdev_new()?
Then instead of four helpers, we stick with two helpers, one that asserts the qdev new will succeed (qdev_new()), the other one that allows any kind of errors (qdev_new_dynamic()). Then we can drop qdev_try_new() altogether, and avoid adding one more for it too.
The qdev_try_new() four call sites can still pass in errp==NULL, which should be the old behavior, so we don't need to touch isa/usb callers.
PS: looks like usb_try_new() only has one caller.. so maybe prone to be dropped altogether..
Signature · 1 line
Peter Xu
Re: [PATCH v2 7/8] convert code to qdev_new_dynamic() where appropriate
In reply to Daniel P. BerrangéQuoted 11 lines · Daniel P. Berrangé
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_abortor&error_fatalwill maintain existing behaviour.Signed-off-by Daniel P. Berrangé <[email protected]>
Signature · 1 line
Peter Xu
Re: [PATCH v2 1/8] qom: refactor checking abstract property when creating instances
In reply to Peter XuQuoted 14 lines · Peter Xu
Quoted 13 lines · Daniel P. Berrangé
qom/object.c +1
-1@@ -753,7 +761,7 @@ typedef union { } qemu_max_align_t;#endif -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 = qemu_vfree; }
Quoted 5 lines
qom/object.c static Object *object_new_with_type(Type type) · line 789
Quoted 3 lines
qom/object.c +2
-1-object_initialize_with_type (obj, size, type); +if (!object_initialize_with_type (obj, size, type, errp)) { +g_free (obj);
obj_free(obj)?
Opps, yes indeed.
Quoted 10 lines
Quoted 6 lines
qom/object.c +2
-0+return NULL ; + } obj->free = obj_free;return obj;Signature · 1 line
Peter Xu
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v2 6/8] qom: introduce qdev_new_dynamic()
In reply to Peter XuQuoted 12 lines · Peter Xu
Quoted 11 lines · Daniel P. Berrangé
qdev_new()has a failure scenario where it willassert()if given an abstract type. Callers which are creating qdevs based on user input, or unknown/untrusted type names, must manually check the result ofqdev_class_is_abstract()before callingqdev_new()to propagate an Error, instead of asserting.Introduce a
qdev_new_dynamic()method which is a counterpart toqdev_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.
Quoted 6 lines
Quoted 2 lines
Do likewise with
qdev_try_new_dynamic()as a counterpart toqdev_try_new().Since at it, would it make sense to simply replace
qdev_try_new()withqdev_new_dynamic(), assuming it plays similar role of "it can fail" version ofqdev_new()?
That is tricky as callers of qdev_try_new have to distinguish between expected failures and unexpected failures.
That said you made me look at the usage patterns and I concluded these xxx_try_new() methods are a bad design for other reasons, so I have simply eliminated them entirely in v3.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|