Skip to contentSkip to conversation

hw/core/qdev: Increase qdev_realize() kindness

PATCH Superseded Philippe Mathieu-Daudé 3 participants 7 messages Jun 20, 2020 – Jul 6, 2020

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/core/qdev: Increase qdev_realize() kindness

Since commit 510ef98dca5, qdev_realize() aborts if bus-less device is realized on a bus. Be kind with the developer by displaying a hint about what is wrong.

---
 hw/core/qdev.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/core/qdev.c +5 -2
index 2131c7f951..dd3c90d37a 100644
@@ -392,8 +392,11 @@ bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
 
     if (bus) {
         qdev_set_parent_bus(dev, bus);
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }
 
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
Signature · 1 line
2.21.3
#1 lore raw

ping?

Quoted 28 lines · Philippe Mathieu-Daudé

Since commit 510ef98dca5, qdev_realize() aborts if bus-less device is realized on a bus. Be kind with the developer by displaying a hint about what is wrong.

---
 hw/core/qdev.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/core/qdev.c +5 -2
index 2131c7f951..dd3c90d37a 100644
@@ -392,8 +392,11 @@ bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp)
 
     if (bus) {
         qdev_set_parent_bus(dev, bus);
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }
 
     object_property_set_bool(OBJECT(dev), true, "realized", &err);
#2 lore raw
In reply to Philippe Mathieu-Daudé

hw/core/qdev.c bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) · line 400

Quoted 9 lines · Philippe Mathieu-Daudé
hw/core/qdev.c +5 -2
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }

Since there is an errp, should we use it and be even kinder?

Paolo

#3 lore raw
Quoted 11 lines · Paolo Bonzini

hw/core/qdev.c bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) · line 400

Quoted 9 lines · Philippe Mathieu-Daudé
hw/core/qdev.c +5 -2
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }
Quoted 1 line

Since there is an errp, should we use it and be even kinder?

This is a programming error, not an user triggerable condition, so I'm not sure. IOW this must not happen, but if it does, then the error message helps the developer to notice the problem without having to use gdb.

#4 lore raw

Are we sure that qdev_realize is never called with user-provided input? If it's a programming error, the call chain will end up passing &error_abort anyway, won't it?

Paolo

Il dom 5 lug 2020, 12:05 Philippe Mathieu-Daudé <[email protected]> ha scritto:

Quoted 20 lines · Paolo Bonzini
Quoted 11 lines · Philippe Mathieu-Daudé

hw/core/qdev.c bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) · line 400

Quoted 9 lines
hw/core/qdev.c +5 -2
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }
Quoted 1 line

Since there is an errp, should we use it and be even kinder?

This is a programming error, not an user triggerable condition, so I'm not sure. IOW this must not happen, but if it does, then the error message helps the developer to notice the problem without having to use gdb.

#5 lore raw
Quoted 1 line · Paolo Bonzini

Are we sure that qdev_realize is never called with user-provided input?

I am not sure, but ...

Quoted 2 lines

If it's a programming error, the call chain will end up passing &error_abort anyway, won't it?

... this is a good point :)

Quoted 25 lines

Paolo

Il dom 5 lug 2020, 12:05 Philippe Mathieu-Daudé <[email protected] <mailto:[email protected]>> ha scritto:

    On 7/5/20 7:46 AM, Paolo Bonzini wrote:
    > On 20/06/20 17:38, Philippe Mathieu-Daudé wrote:
    >> -    } else {
    >> -        assert(!DEVICE_GET_CLASS(dev)->bus_type);
    >> +    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
    >> +        error_report("%s: Unexpected bus '%s' for device '%s'",
    >> +                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
    >> +                     object_get_typename(OBJECT(dev)));
    >> +        abort();
    >>      }
    >> 
    >
    > Since there is an errp, should we use it and be even kinder?

    This is a programming error, not an user triggerable condition,
    so I'm not sure. IOW this must not happen, but if it does, then
    the error message helps the developer to notice the problem without
    having to use gdb.
#6 lore raw
In reply to Paolo Bonzini
Quoted 1 line · Paolo Bonzini

Are we sure that qdev_realize is never called with user-provided input? If

The only way to call qdev_realize() with a user-provided bus is -device / device_add via qdev_device_add(). qdev_device_add() carefully checks the user-provided bus before passing it to qdev_realize().

Quoted 2 lines

it's a programming error, the call chain will end up passing &error_abort anyway, won't it?

Correct.

Quoted 24 lines

Paolo

Il dom 5 lug 2020, 12:05 Philippe Mathieu-Daudé <[email protected]> ha scritto:

Quoted 18 lines · Paolo Bonzini
Quoted 11 lines · Philippe Mathieu-Daudé

hw/core/qdev.c bool qdev_realize(DeviceState *dev, BusState *bus, Error **errp) · line 400

Quoted 9 lines
hw/core/qdev.c +5 -2
-    } else {
-        assert(!DEVICE_GET_CLASS(dev)->bus_type);
+    } else if (DEVICE_GET_CLASS(dev)->bus_type) {
+        error_report("%s: Unexpected bus '%s' for device '%s'",
+                     __func__, DEVICE_GET_CLASS(dev)->bus_type,
+                     object_get_typename(OBJECT(dev)));
+        abort();
     }
Quoted 1 line

Since there is an errp, should we use it and be even kinder?

This is a programming error, not an user triggerable condition, so I'm not sure. IOW this must not happen, but if it does, then the error message helps the developer to notice the problem without having to use gdb.

I don't bother with reporting impossible errors nicely. Statement, not objection.