Skip to contentSkip to conversation

Fix various QOM object life-cycle issues

PATCH Superseded Marc-André Lureau 4 participants 59 messages May 4 – May 11

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix various QOM object life-cycle issues

Hi,

After Markus's "Several QOM objects crash on introspection" report, I started writing some unit test.

This series adds a new "qom-tests" qtest command that exercises basic QOM object life-cycle: it instantiates all non-abstract object types, gets/sets their properties, and unrefs them. This quickly surfaces leaks and crashes that could otherwise be triggered at runtime via QMP qom commands.

The bulk of the series fixes the issues found by this test and ASan help. Some of the patches are redundant with patches sent earlier on the ML and marked as RFC, they should naturally be dropped during rebases, but are added for completeness and to make sure CI pass after this series in the meantime.

---
Changes in v2:
- tweak error messages
- drop "qom: skip link property check callback", instead
added two patches to avoid crash on NULL link check, and doc update
- drop "RFC system/ioport" in favour of Peter Xu fix
- added "hw/fsi: move OPBus qbus_init() to instance_init" to address TODO comment
- added "target/riscv: use hash table as set for user_options"
- gather a-b/r-b trailers
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Marc-André Lureau (45):
hw/pci: handle missing bus in prop_pci_busnr_get chardev/char-socket: handle NULL addr in char_socket_get_addr hw/pci-bridge: handle missing parent in prop_pxb_uid_get hw/pci-host/i440fx: handle NULL bus in pci-hole64 getters hw/pci-host/q35: handle NULL bus in pci-hole64 getters hw/remote: check visit return in vfu_object_set_socket hw/ipmi: reject NULL 'bmc' property rather than crash hw/xlnx_dp: reject NULL 'dpdma' property rather than crash qom/object: update doc about NULL values in link properties hw/intc/apic: move checks to realize()
hw/remote: guard listener unregister in finalize backends/cryptodev-lkcf: skip cleanup when not initialized io/net-listener: move mutex init to instance_init net/colo-compare: guard finalize against uninitialized state system/ioport: minor code simplification
ui/console: remove console from global list on finalization hw/i386/x86: free oem_id and oem_table_id on finalization hw/core/machine: free shim_filename on finalization hw/core/resetcontainer: free children array on finalization net/filter: free old values in property setters ui/console-vc: destroy fifo on text console finalization target/i386/sev: add finalize functions and fix leaking setters target/i386/kvm/tdx: free strings in tdx_guest_finalize hw/i386/nitro_enclave: add instance finalize
net/can: free ifname on socketcan finalization backends/igvm-cfg: free filename on finalization scsi/pr-manager-helper: free path on finalization accel/kvm: free device path on finalization
system/qtest: add missing qtest_finalize()
hw/i386/pc: free pcspk on finalization
hw/tpm: free PPI buffer on finalization
hw/fsi: move OPBus address space init to realize
hw/fsi: move OPBus qbus_init() to instance_init hw/gpio/pca9552: fix state_str leak in pca955x_set_led hw/arm/aspeed: free fmc_model and spi_model on finalization hw/arm/sbsa-ref: free unrealized flash devices on finalization hw/arm/virt: free flash devices and OEM strings on finalization hw/loongarch/virt: free flash devices and OEM strings on finalization hw/ppc/spapr: free host_model and host_serial on finalization hw/ppc/pnv: drop extra ref on PHB after adding as child target/riscv: fix general_user_opts hash table leak target/riscv: use hash table as set for user_options hw/riscv/virt: free flash devices and OEM strings on finalization hw/i2c/pmbus: fix undefined behavior in pmbus_direct_mode2data
qtest: add "qom-tests" command

Peter Xu (1):
system/ioport: Fix qom-list-properties crash on portio list obj

 include/qom/object.h                |  8 +++---
 target/riscv/cpu.h                  |  3 +-
 tests/qtest/libqtest.h              |  8 ++++++
 accel/kvm/kvm-all.c                 |  8 ++++++
 backends/cryptodev-lkcf.c           |  4 +++
 backends/igvm-cfg.c                 |  1 +
 chardev/char-socket.c               |  4 +++
 hw/arm/aspeed.c                     |  9 ++++++
 hw/arm/sbsa-ref.c                   | 12 ++++++++
 hw/arm/virt.c                       | 14 ++++++++++
 hw/core/machine.c                   |  1 +
 hw/core/resetcontainer.c            |  3 ++
 hw/display/xlnx_dp.c                |  6 ++++
 hw/fsi/aspeed_apb2opb.c             | 37 +++++++++++++++---------
 hw/gpio/pca9552.c                   |  2 +-
 hw/i2c/pmbus_device.c               |  4 +--
 hw/i386/nitro_enclave.c             | 11 ++++++++
 hw/i386/pc.c                        | 10 +++++++
 hw/i386/x86.c                       |  9 ++++++
 hw/intc/apic_common.c               | 23 ++++++++-------
 hw/ipmi/ipmi.c                      |  9 +++++-
 hw/loongarch/virt.c                 | 14 ++++++++++
 hw/pci-bridge/pci_expander_bridge.c |  8 +++++-
 hw/pci-host/i440fx.c                | 15 ++++++++--
 hw/pci-host/q35.c                   | 15 ++++++++--
 hw/pci/pci.c                        | 11 ++++++--
 hw/ppc/pnv.c                        |  1 +
 hw/ppc/spapr.c                      |  2 ++
 hw/remote/remote-obj.c              |  4 ++-
 hw/remote/vfio-user-obj.c           |  4 ++-
 hw/riscv/virt.c                     | 14 ++++++++++
 hw/tpm/tpm_tis_sysbus.c             |  9 ++++++
 io/net-listener.c                   |  9 +++++-
 net/can/can_socketcan.c             |  8 ++++++
 net/colo-compare.c                  | 31 ++++++++++----------
 net/filter.c                        |  2 ++
 scsi/pr-manager-helper.c            |  1 +
 system/ioport.c                     | 14 +++++++---
 system/qtest.c                      | 56 +++++++++++++++++++++++++++++++++++++
 target/i386/cpu-apic.c              |  6 +---
 target/i386/kvm/tdx.c               |  5 ++++
 target/i386/sev.c                   | 37 ++++++++++++++++++++++++
 target/riscv/cpu.c                  | 51 ++++++++++++++++++---------------
 target/riscv/kvm/kvm-cpu.c          |  6 ++--
 tests/qtest/libqtest.c              |  6 ++++
 tests/qtest/qom-test.c              | 12 ++++++++
 ui/console-vc.c                     |  1 +
 ui/console.c                        |  5 +++-
 48 files changed, 440 insertions(+), 93 deletions(-)

---
base-commit: ac0cc20ad2fe0b8df2e5d9458e90a095ac711ab1
change-id: 20260427-qom-tests-9dcf3b969411

Best regards,

Signature · 1 line
Marc-André Lureau <[email protected]>
#1 lore raw

PATCH v2 1/46 hw/pci: handle missing bus in prop_pci_busnr_get

When called on an unrealized device (e.g. from qmp_qom_list_properties), pci_get_bus() returns NULL since the device has no parent bus. Check for this to avoid a NULL dereference in pci_bus_num().

Fixes df9ac7254fd9 ("hw/pci: Add a busnr property to pci_props and use for acpi/gi")
---
 hw/pci/pci.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/pci/pci.c +9 -2
index a0dbb81fd90..bf13cabd174 100644
@@ -64,10 +64,17 @@ static void pcibus_reset_hold(Object *obj, ResetType type);
 static bool pcie_has_upstream_port(PCIDevice *dev);
 
 static void prop_pci_busnr_get(Object *obj, Visitor *v, const char *name,
-                             void *opaque, Error **errp)
+                               void *opaque, Error **errp)
 {
-    uint8_t busnr = pci_dev_bus_num(PCI_DEVICE(obj));
+    PCIDevice *dev = PCI_DEVICE(obj);
+    PCIBus *bus = pci_get_bus(dev);
+    uint8_t busnr;
 
+    if (!bus) {
+        error_setg(errp, "device not attached to a PCI bus");
+        return;
+    }
+    busnr = pci_bus_num(bus);
     visit_type_uint8(v, name, &busnr, errp);
 }
 
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/46 chardev/char-socket: handle NULL addr in char_socket_get_addr

In reply to Marc-André Lureau

When called on an unrealized chardev (e.g. from qmp_qom_list_properties), s->addr is NULL. Return an error instead of assert().

Fixes 123676e9894f ("char-socket: add 'addr' property")
---
 chardev/char-socket.c | 4 ++++
 1 file changed, 4 insertions(+)
chardev/char-socket.c +4 -0
index e064b105c50..b629575fcf8 100644
@@ -1529,6 +1529,10 @@ char_socket_get_addr(Object *obj, Visitor *v, const char *name,
 {
     SocketChardev *s = SOCKET_CHARDEV(obj);
 
+    if (!s->addr) {
+        error_setg(errp, "socket not connected");
+        return;
+    }
     visit_type_SocketAddress(v, name, &s->addr, errp);
 }
 
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 3/46 hw/pci-bridge: handle missing parent in prop_pxb_uid_get

In reply to Marc-André Lureau

When called on an unrealized pxb bus (e.g. from qmp_qom_list_properties), bus->parent_dev is NULL. The pxb_bus_num() callback dereferences it unconditionally. Check for this to avoid a NULL dereference.

Fixes 97b9cb066e5f ("hw/pci-bridge: Add acpi_uid property to TYPE_PXB_BUS")
---
 hw/pci-bridge/pci_expander_bridge.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/pci-bridge/pci_expander_bridge.c +7 -1
index 11623a5666f..40ffbc4e082 100644
@@ -85,8 +85,14 @@ static uint16_t pxb_bus_numa_node(PCIBus *bus)
 static void prop_pxb_uid_get(Object *obj, Visitor *v, const char *name,
                              void *opaque, Error **errp)
 {
-    uint32_t uid = pci_bus_num(PCI_BUS(obj));
+    PCIBus *bus = PCI_BUS(obj);
+    uint32_t uid;
 
+    if (!bus->parent_dev) {
+        error_setg(errp, "bus not attached to a device");
+        return;
+    }
+    uid = pci_bus_num(bus);
     visit_type_uint32(v, name, &uid, errp);
 }
 
Signature · 1 line
2.54.0
#4 lore raw

PATCH v2 4/46 hw/pci-host/i440fx: handle NULL bus in pci-hole64 getters

In reply to Marc-André Lureau

When called on an unrealized i440FX host bridge (e.g. from qmp_qom_list_properties), h->bus is NULL since the root bus is only created during realize. Guard against this in both pci_hole64_start and pci_hole64_end getters, reporting an error.

---
 hw/pci-host/i440fx.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
hw/pci-host/i440fx.c +13 -2
index e7d638b296c..c1982f7962a 100644
@@ -189,8 +189,14 @@ static void i440fx_pcihost_get_pci_hole64_start(Object *obj, Visitor *v,
                                                 const char *name,
                                                 void *opaque, Error **errp)
 {
-    uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
+    PCIHostState *h = PCI_HOST_BRIDGE(obj);
+    uint64_t hole64_start;
 
+    if (!h->bus) {
+        error_setg(errp, "PCI host bridge not realized");
+        return;
+    }
+    hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
     visit_type_uint64(v, name, &hole64_start, errp);
 }
 
@@ -206,10 +212,15 @@ static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
 {
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
-    uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
+    uint64_t hole64_start;
     Range w64;
     uint64_t value, hole64_end;
 
+    if (!h->bus) {
+        error_setg(errp, "PCI host bridge not realized");
+        return;
+    }
+    hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
     pci_bus_get_w64_range(h->bus, &w64);
     value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
     hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
Signature · 1 line
2.54.0
#5 lore raw

PATCH v2 5/46 hw/pci-host/q35: handle NULL bus in pci-hole64 getters

In reply to Marc-André Lureau

When called on an unrealized Q35 host bridge (e.g. from qmp_qom_list_properties), h->bus is NULL since the root bus is only created during realize. Guard against this in both the pci_hole64_start and pci_hole64_end getters.

---
 hw/pci-host/q35.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
hw/pci-host/q35.c +13 -2
index e85e4227b37..355e81bfa20 100644
@@ -132,8 +132,14 @@ static void q35_host_get_pci_hole64_start(Object *obj, Visitor *v,
                                           const char *name, void *opaque,
                                           Error **errp)
 {
-    uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
+    PCIHostState *h = PCI_HOST_BRIDGE(obj);
+    uint64_t hole64_start;
 
+    if (!h->bus) {
+        error_setg(errp, "PCI host bridge not realized");
+        return;
+    }
+    hole64_start = q35_host_get_pci_hole64_start_value(obj);
     visit_type_uint64(v, name, &hole64_start, errp);
 }
 
@@ -149,10 +155,15 @@ static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
 {
     PCIHostState *h = PCI_HOST_BRIDGE(obj);
     Q35PCIHost *s = Q35_HOST_DEVICE(obj);
-    uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
+    uint64_t hole64_start;
     Range w64;
     uint64_t value, hole64_end;
 
+    if (!h->bus) {
+        error_setg(errp, "PCI host bridge not realized");
+        return;
+    }
+    hole64_start = q35_host_get_pci_hole64_start_value(obj);
     pci_bus_get_w64_range(h->bus, &w64);
     value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
     hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
Signature · 1 line
2.54.0
#6 lore raw

PATCH v2 6/46 hw/remote: check visit return in vfu_object_set_socket

In reply to Marc-André Lureau

vfu_object_set_socket() dereferences o->socket without checking if visit_type_SocketAddress() succeeded. On failure, o->socket remains NULL, leading to a NULL dereference. Check the return value.

Fixes 8f9a9259d32c ("vfio-user: define vfio-user-server object")
---
 hw/remote/vfio-user-obj.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/remote/vfio-user-obj.c +3 -1
index 12ecdab6dea..49bf5ecae0c 100644
@@ -161,7 +161,9 @@ static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name,
 
     o->socket = NULL;
 
-    visit_type_SocketAddress(v, name, &o->socket, errp);
+    if (!visit_type_SocketAddress(v, name, &o->socket, errp)) {
+        return;
+    }
 
     if (o->socket->type != SOCKET_ADDRESS_TYPE_UNIX) {
         error_setg(errp, "vfu: Unsupported socket type - %s",
Signature · 1 line
2.54.0
#7 lore raw

PATCH v2 7/46 hw/ipmi: reject NULL 'bmc' property rather than crash

In reply to Marc-André Lureau

The type-checking macros (IPMI_BMC(val)) which crash on NULL.

---
 hw/ipmi/ipmi.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/ipmi/ipmi.c +8 -1
index b49affaccec..d532d38e556 100644
@@ -95,7 +95,14 @@ static const TypeInfo ipmi_interface_type_info = {
 static void isa_ipmi_bmc_check(const Object *obj, const char *name,
                                Object *val, Error **errp)
 {
-    IPMIBmc *bmc = IPMI_BMC(val);
+    IPMIBmc *bmc;
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
+    bmc = IPMI_BMC(val);
 
     if (bmc->intf)
         error_setg(errp, "BMC object is already in use");
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH v2 07/46] hw/ipmi: reject NULL 'bmc' property rather than crash

Quoted 18 lines · Marc-André Lureau

The type-checking macros (IPMI_BMC(val)) which crash on NULL.

---
 hw/ipmi/ipmi.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/ipmi/ipmi.c +1 -1
index b49affaccec..d532d38e556 100644
@@ -95,7 +95,14 @@ static const TypeInfo ipmi_interface_type_info = {
 static void isa_ipmi_bmc_check(const Object *obj, const char *name,
                                Object *val, Error **errp)
 {
-    IPMIBmc *bmc = IPMI_BMC(val);
+    IPMIBmc *bmc;

hw/ipmi/ipmi.c static const TypeInfo ipmi_interface_type_info = { · line 108

Quoted 10 lines
hw/ipmi/ipmi.c +7 -0
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
+    bmc = IPMI_BMC(val);
 
     if (bmc->intf)
         error_setg(errp, "BMC object is already in use");

I was looking through the code to check where the type macros fall over on NULL pointers, and I couldn't find it, so I did this little test, and as far as I can tell they don't assert, they just return NULL:

hw/i386/pc.c +4 -0
index 2ecad3c503..32569c1f09 100644
@@ -66,6 +66,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include "sev.h"
+#include "hw/ipmi/ipmi.h"
 #include CONFIG_DEVICES
 
 #ifdef CONFIG_XEN_EMU
@@ -1580,6 +1581,9 @@ static void pc_machine_initfn(Object *obj)
     PCMachineState *pcms = PC_MACHINE(obj);
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
 
+    void *foo = IPMI_BMC(NULL);
+    printf("foo = %p\n", foo);
+
 #ifdef CONFIG_VMPORT
     pcms->vmport = ON_OFF_AUTO_AUTO;
 #else

This code runs on the pc machine startup and prints "foo = (nil)" without problems, even on a debug build (which enables CONFIG_QOM_CAST_DEBUG).

Is the problem you saw really with the cast macro, and not with this function's explicit dereference of bmc in "bmc->intf" ?

thanks
-- PMM

#9 lore raw

Re: [PATCH v2 07/46] hw/ipmi: reject NULL 'bmc' property rather than crash

Hi

Quoted 59 lines · Peter Maydell
Quoted 18 lines · Marc-André Lureau

The type-checking macros (IPMI_BMC(val)) which crash on NULL.

---
 hw/ipmi/ipmi.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/ipmi/ipmi.c +1 -1
index b49affaccec..d532d38e556 100644
@@ -95,7 +95,14 @@ static const TypeInfo ipmi_interface_type_info = {
 static void isa_ipmi_bmc_check(const Object *obj, const char *name,
                                Object *val, Error **errp)
 {
-    IPMIBmc *bmc = IPMI_BMC(val);
+    IPMIBmc *bmc;

hw/ipmi/ipmi.c static const TypeInfo ipmi_interface_type_info = { · line 108

Quoted 10 lines
hw/ipmi/ipmi.c +7 -0
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
+    bmc = IPMI_BMC(val);
 
     if (bmc->intf)
         error_setg(errp, "BMC object is already in use");

I was looking through the code to check where the type macros fall over on NULL pointers, and I couldn't find it, so I did this little test, and as far as I can tell they don't assert, they just return NULL:

hw/i386/pc.c +4 -0
index 2ecad3c503..32569c1f09 100644
@@ -66,6 +66,7 @@
 #include "e820_memory_layout.h"
 #include "trace.h"
 #include "sev.h"
+#include "hw/ipmi/ipmi.h"
 #include CONFIG_DEVICES
 
 #ifdef CONFIG_XEN_EMU
@@ -1580,6 +1581,9 @@ static void pc_machine_initfn(Object *obj)
     PCMachineState *pcms = PC_MACHINE(obj);
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
 
+    void *foo = IPMI_BMC(NULL);
+    printf("foo = %p\n", foo);
+
 #ifdef CONFIG_VMPORT
     pcms->vmport = ON_OFF_AUTO_AUTO;
 #else
Quoted 6 lines

This code runs on the pc machine startup and prints "foo = (nil)" without problems, even on a debug build (which enables CONFIG_QOM_CAST_DEBUG).

Is the problem you saw really with the cast macro, and not with this function's explicit dereference of bmc in "bmc->intf" ?

You're right; it's with the "bmc->intf", I'll update the commit message.

#10 lore raw

Re: [PATCH v2 07/46] hw/ipmi: reject NULL 'bmc' property rather than crash

Quoted 40 lines · Marc-André Lureau

Hi

Quoted 35 lines · Peter Maydell
Quoted 18 lines · Marc-André Lureau

The type-checking macros (IPMI_BMC(val)) which crash on NULL.

---
 hw/ipmi/ipmi.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/ipmi/ipmi.c +1 -1
index b49affaccec..d532d38e556 100644
@@ -95,7 +95,14 @@ static const TypeInfo ipmi_interface_type_info = {
 static void isa_ipmi_bmc_check(const Object *obj, const char *name,
                                Object *val, Error **errp)
 {
-    IPMIBmc *bmc = IPMI_BMC(val);
+    IPMIBmc *bmc;

hw/ipmi/ipmi.c static const TypeInfo ipmi_interface_type_info = { · line 108

Quoted 10 lines
hw/ipmi/ipmi.c +7 -0
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
+    bmc = IPMI_BMC(val);
 
     if (bmc->intf)
         error_setg(errp, "BMC object is already in use");

I was looking through the code to check where the type macros fall over on NULL pointers, and I couldn't find it, so I did this little test, and as far as I can tell they don't assert, they just return NULL:

Quoted 5 lines
Quoted 2 lines

Is the problem you saw really with the cast macro, and not with this function's explicit dereference of bmc in "bmc->intf" ?

You're right; it's with the "bmc->intf", I'll update the commit message.

We can also update the code change, because we don't need to split the declaration and initialization of bmc; we can keep the existing "IPMIBmc *bmc = IPMI_BMC(val);".

thanks
-- PMM

#11 lore raw

PATCH v2 8/46 hw/xlnx_dp: reject NULL 'dpdma' property rather than crash

In reply to Marc-André Lureau

The type-checking macros (XLNX_DPDMA(val)) which crash on NULL.

---
 hw/display/xlnx_dp.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/display/xlnx_dp.c +6 -0
index 50e6ef10984..876cb3d6642 100644
@@ -570,6 +570,12 @@ static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val,
                               Error **errp)
 {
     XlnxDPState *s = XLNX_DP(obj);
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
     if (s->console) {
         DisplaySurface *surface = qemu_console_surface(s->console);
         XlnxDPDMAState *dma = XLNX_DPDMA(val);
Signature · 1 line
2.54.0
#12 lore raw

Re: [PATCH v2 08/46] hw/xlnx_dp: reject NULL 'dpdma' property rather than crash

Quoted 15 lines · Marc-André Lureau

The type-checking macros (XLNX_DPDMA(val)) which crash on NULL.

---
 hw/display/xlnx_dp.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/display/xlnx_dp.c +0 -0
index 50e6ef10984..876cb3d6642 100644
@@ -570,6 +570,12 @@ static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val,
                               Error **errp)
 {

hw/display/xlnx_dp.c static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val, · line 581

Quoted 10 lines
hw/display/xlnx_dp.c +6 -0
     XlnxDPState *s = XLNX_DP(obj);
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
+
     if (s->console) {
         DisplaySurface *surface = qemu_console_surface(s->console);
         XlnxDPDMAState *dma = XLNX_DPDMA(val);

What fails here? XLNX_DPDMA(val) should handle NULL, as mentioned on the other patch. And xlnx_dpdma_set_host_data_location() checks for a NULL argument.

thanks
-- PMM

#13 lore raw

Re: [PATCH v2 08/46] hw/xlnx_dp: reject NULL 'dpdma' property rather than crash

Hi

Quoted 24 lines · Peter Maydell
Quoted 21 lines · Marc-André Lureau

The type-checking macros (XLNX_DPDMA(val)) which crash on NULL.

---
 hw/display/xlnx_dp.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/display/xlnx_dp.c +5 -0
index 50e6ef10984..876cb3d6642 100644
@@ -570,6 +570,12 @@ static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val,
                               Error **errp)
 {
     XlnxDPState *s = XLNX_DP(obj);
+
+    if (!val) {
+        error_setg(errp, "%s cannot be set to NULL", name);
+        return;
+    }
Quoted 9 lines

hw/display/xlnx_dp.c static void xlnx_dp_set_dpdma(const Object *obj, const char *name, Object *val, · line 581

Quoted 4 lines
hw/display/xlnx_dp.c +1 -0
+
     if (s->console) {
         DisplaySurface *surface = qemu_console_surface(s->console);
         XlnxDPDMAState *dma = XLNX_DPDMA(val);

What fails here? XLNX_DPDMA(val) should handle NULL, as mentioned on the other patch. And xlnx_dpdma_set_host_data_location() checks for a NULL argument.

Right, it doesn't crash. I did it mechanically because you reported it as one of the custom add_link setters.
I'll drop it.

Thanks

#14 lore raw

PATCH v2 9/46 qom/object: update doc about NULL values in link properties

In reply to Marc-André Lureau
---
 include/qom/object.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qom/object.h +4 -4
index 510885218ba..0a82e160cd0 100644
@@ -1714,10 +1714,10 @@ void object_property_allow_set_link(const Object *obj, const char *name,
  *
  * Links form the graph in the object model.
  *
- * The @check() callback is invoked when
- * object_property_set_link() is called and can raise an error to prevent the
- * link being set.  If @check is NULL, the property is read-only
- * and cannot be set.
+ * The @check() callback is invoked when object_property_set_link() is called
+ * and can raise an error to prevent the link being set. If @check is NULL, the
+ * property is read-only and cannot be set. Care must be taken to handle NULL
+ * values for @val.
  *
  * Ownership of the pointer that @child points to is transferred to the
  * link property.  The reference count for *@child is
Signature · 1 line
2.54.0
Reviewed-by on v2 9/46
#15 lore raw

Re: [PATCH v2 09/46] qom/object: update doc about NULL values in link properties

Quoted 12 lines · Marc-André Lureau
---
 include/qom/object.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qom/object.h +0 -0
index 510885218ba..0a82e160cd0 100644
@@ -1714,10 +1714,10 @@ void object_property_allow_set_link(const Object *obj, const char *name,
  *

include/qom/object.h void object_property_allow_set_link(const Object *obj, const char *name, · line 1720

Quoted 10 lines
include/qom/object.h +4 -4
  * Links form the graph in the object model.
  *
- * The @check() callback is invoked when
- * object_property_set_link() is called and can raise an error to prevent the
- * link being set.  If @check is NULL, the property is read-only
- * and cannot be set.
+ * The @check() callback is invoked when object_property_set_link() is called
+ * and can raise an error to prevent the link being set. If @check is NULL, the
+ * property is read-only and cannot be set. Care must be taken to handle NULL
+ * values for @val.

I think we could perhaps expand this a bit to be more explicit about the lurking bug potential here:

Remember that the usual QOM type-casting macros will assert on NULL, so you need to either explicitly NULL check before using the cast macro, or use object_dynamic_cast() instead.

include/qom/object.h void object_property_allow_set_link(const Object *obj, const char *name, · line 1723

Quoted 3 lines
include/qom/object.h +0 -0
  *
  * Ownership of the pointer that @child points to is transferred to the
  * link property.  The reference count for *@child is

Either way

thanks
-- PMM

#16 lore raw

Re: [PATCH v2 09/46] qom/object: update doc about NULL values in link properties

Quoted 29 lines · Peter Maydell
Quoted 12 lines · Marc-André Lureau
---
 include/qom/object.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/qom/object.h +0 -0
index 510885218ba..0a82e160cd0 100644
@@ -1714,10 +1714,10 @@ void object_property_allow_set_link(const Object *obj, const char *name,
  *

include/qom/object.h void object_property_allow_set_link(const Object *obj, const char *name, · line 1720

Quoted 10 lines
include/qom/object.h +4 -4
  * Links form the graph in the object model.
  *
- * The @check() callback is invoked when
- * object_property_set_link() is called and can raise an error to prevent the
- * link being set.  If @check is NULL, the property is read-only
- * and cannot be set.
+ * The @check() callback is invoked when object_property_set_link() is called
+ * and can raise an error to prevent the link being set. If @check is NULL, the
+ * property is read-only and cannot be set. Care must be taken to handle NULL
+ * values for @val.

I think we could perhaps expand this a bit to be more explicit about the lurking bug potential here:

Quoted 3 lines

Remember that the usual QOM type-casting macros will assert on NULL, so you need to either explicitly NULL check before using the cast macro, or use object_dynamic_cast() instead.

Actually I think this is wrong, see my comment on patch 7.

-- PMM

#17 lore raw

PATCH v2 10/46 hw/intc/apic: move checks to realize()

In reply to Marc-André Lureau

apic_common_set_id() dereferences s->cpu to check for x2APIC support when the APIC ID is >= 255. On a standalone APIC object that has not been attached to a CPU, s->cpu is NULL, causing a segfault.

To solve this, move validation during realize().

Fixes b5ee0468e9d2 ("apic: add support for x2APIC mode")
---
 hw/intc/apic_common.c  | 23 +++++++++++++----------
 target/i386/cpu-apic.c |  6 +-----
 2 files changed, 14 insertions(+), 15 deletions(-)
hw/intc/apic_common.c +13 -10
index bf4abc21d7b..49c03a5bcee 100644
@@ -257,6 +257,19 @@ static void apic_common_realize(DeviceState *dev, Error **errp)
     static DeviceState *vapic;
     uint32_t instance_id = s->initial_apic_id;
 
+    if (!s->cpu) {
+        error_setg(errp, "APIC is not attached to a CPU");
+        return;
+    }
+
+    if (s->initial_apic_id >= 255 &&
+        !cpu_has_x2apic_feature(&s->cpu->env)) {
+        error_setg(errp, "APIC ID %d requires x2APIC feature in CPU",
+                   s->initial_apic_id);
+        error_append_hint(errp, "Try x2apic=on in -cpu.\n");
+        return;
+    }
+
     /* Normally initial APIC ID should be no more than hundreds */
     assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
 
@@ -410,7 +423,6 @@ static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
     APICCommonState *s = APIC_COMMON(obj);
     DeviceState *dev = DEVICE(obj);
     uint32_t value;
-    Error *local_err = NULL;
 
     if (dev->realized) {
         qdev_prop_set_after_realize(dev, name, errp);
@@ -421,15 +433,6 @@ static void apic_common_set_id(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    if (value >= 255 && !cpu_has_x2apic_feature(&s->cpu->env)) {
-        error_setg(&local_err,
-                   "APIC ID %d requires x2APIC feature in CPU",
-                   value);
-        error_append_hint(&local_err, "Try x2apic=on in -cpu.\n");
-        error_propagate(errp, local_err);
-        return;
-    }
-
     s->initial_apic_id = value;
     s->id = (uint8_t)value;
 }
target/i386/cpu-apic.c +1 -5
index eaa10ad2a3d..f4f7f6b4d67 100644
@@ -57,11 +57,7 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
     cpu->apic_state->cpu = cpu;
     cpu->apic_state->apicbase = APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE;
 
-    /*
-     * apic_common_set_id needs to check if the CPU has x2APIC
-     * feature in case APIC ID >= 255, so we need to set cpu->apic_state->cpu
-     * before setting APIC ID
-     */
+    /* cpu must be set before realize, which validates the APIC ID */
     qdev_prop_set_uint32(DEVICE(cpu->apic_state), "id", cpu->apic_id);
 }
 
Signature · 1 line
2.54.0
#18 lore raw

PATCH v2 11/46 hw/remote: guard listener unregister in finalize

In reply to Marc-André Lureau

Guard the unregister by checking whether the listener callback was set, which only happens right before registration.

This is probably better addressed by "[PATCH] qdev: test if DeviceListener is in use". Just adding this patch for completeness.

Fixes c7d80c7c1d9 ("multi-process: Associate fd of a PCIDevice with its object")
---
 hw/remote/remote-obj.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/remote/remote-obj.c +3 -1
index 86192dc8dad..4e74ae76158 100644
@@ -154,7 +154,9 @@ static void remote_object_finalize(Object *obj)
     RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
     RemoteObject *o = REMOTE_OBJECT(obj);
 
-    device_listener_unregister(&o->listener);
+    if (o->listener.unrealize) {
+        device_listener_unregister(&o->listener);
+    }
 
     if (o->ioc) {
         qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
Signature · 1 line
2.54.0
Reviewed-by on v2 11/46
#19 lore raw

Re: [PATCH v2 11/46] hw/remote: guard listener unregister in finalize

Confidential - Oracle Restricted \Including External Recipients

Confidential - Oracle Restricted \Including External Recipients

From: Marc-André Lureau <[email protected]>
Date: Monday, May 4, 2026 at 7:36 AM
To: [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; Marc-André Lureau <[email protected]>; Jag Raman <[email protected]>
Subject: [PATCH v2 11/46] hw/remote: guard listener unregister in finalize

Guard the unregister by checking whether the listener callback was set, which only happens right before registration.

This is probably better addressed by "[PATCH] qdev: test if DeviceListener is in use". Just adding this patch for completeness.

Fixes c7d80c7c1d9 ("multi-process: Associate fd of a PCIDevice with its object")

Looks good to me.

---
 hw/remote/remote-obj.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/remote/remote-obj.c +3 -1
index 86192dc8dad..4e74ae76158 100644
@@ -154,7 +154,9 @@ static void remote_object_finalize(Object *obj)
     RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
     RemoteObject *o = REMOTE_OBJECT(obj);
 
-    device_listener_unregister(&o->listener);
+    if (o->listener.unrealize) {
+        device_listener_unregister(&o->listener);
+    }
 
     if (o->ioc) {
         qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);

--
2.54.0

#20 lore raw

PATCH v2 12/46 backends/cryptodev-lkcf: skip cleanup when not initialized

In reply to Marc-André Lureau

cryptodev_lkcf_cleanup() locks a mutex that is only initialized during the init vfunc (called at realize time). When the backend is destroyed without ever being realized, the mutex is uninitialized and the lock aborts.

Return early from cleanup when the backend was never started.

Note: it looks like cryptodev init/cleanup callbacks should rather be regular complete/finalize overrides (calling the parent method).

Fixes 39fff6f3e8b3 ("cryptodev: Add a lkcf-backend for cryptodev")
---
 backends/cryptodev-lkcf.c | 4 ++++
 1 file changed, 4 insertions(+)
backends/cryptodev-lkcf.c +4 -0
index 40c7bd3c5a0..3fe29d3104c 100644
@@ -255,6 +255,10 @@ static void cryptodev_lkcf_cleanup(CryptoDevBackend *backend, Error **errp)
     CryptoDevBackendClient *cc;
     CryptoDevLKCFTask *task, *next;
 
+    if (!cryptodev_backend_is_ready(backend)) {
+        return;
+    }
+
     qemu_mutex_lock(&lkcf->mutex);
     lkcf->running = false;
     qemu_mutex_unlock(&lkcf->mutex);
Signature · 1 line
2.54.0
#21 lore raw

PATCH v2 13/46 io/net-listener: move mutex init to instance_init

In reply to Marc-André Lureau

The QIONetListener mutex is initialized in the convenience constructor qio_net_listener_new() rather than in an instance_init. This means a bare object_new(TYPE_QIO_NET_LISTENER) produces an object with an uninitialized mutex, but instance_finalize unconditionally calls qemu_mutex_destroy() on it, which aborts.

Move the mutex initialization to a proper instance_init so that init and finalize are always paired regardless of how the object is created.

This patch is just added for completeness, but "qio: Fix qom-list-properties crash on net listener object" from Peter Xu is basically the same.

Fixes 9d86181874a ("qio: Protect NetListener callback with mutex")
---
 io/net-listener.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
io/net-listener.c +8 -1
index 9410d72da9c..1fd0f6cb5ab 100644
@@ -38,7 +38,6 @@ QIONetListener *qio_net_listener_new(void)
     QIONetListener *listener;
 
     listener = QIO_NET_LISTENER(object_new(TYPE_QIO_NET_LISTENER));
-    qemu_mutex_init(&listener->lock);
     return listener;
 }
 
@@ -440,6 +439,13 @@ qio_net_listener_get_local_address(QIONetListener *listener, size_t n,
     return qio_channel_socket_get_local_address(sioc, errp);
 }
 
+static void qio_net_listener_instance_init(Object *obj)
+{
+    QIONetListener *listener = QIO_NET_LISTENER(obj);
+
+    qemu_mutex_init(&listener->lock);
+}
+
 static void qio_net_listener_finalize(Object *obj)
 {
     QIONetListener *listener = QIO_NET_LISTENER(obj);
@@ -463,6 +469,7 @@ static const TypeInfo qio_net_listener_info = {
     .parent = TYPE_OBJECT,
     .name = TYPE_QIO_NET_LISTENER,
     .instance_size = sizeof(QIONetListener),
+    .instance_init = qio_net_listener_instance_init,
     .instance_finalize = qio_net_listener_finalize,
 };
 
Signature · 1 line
2.54.0
#22 lore raw

PATCH v2 14/46 net/colo-compare: guard finalize against uninitialized state

In reply to Marc-André Lureau

colo_compare_finalize() assumes the object was fully set up by colo_compare_complete(), but a bare object_new() followed by object_unref() skips the complete callback entirely.

This causes two crashes:
- qemu_mutex_destroy on the static event_mtx which was never initialized (colo_compare_active is false)
- qemu_bh_delete(NULL) and iothread dereference when s->iothread is NULL

Guard the event_mtx teardown with colo_compare_active, and the iothread-dependent cleanup with an s->iothread NULL check.

This is an alternative to patch "colo-compare: Fix QMP qom-list-properties crashing", sent earlier, hence the RFC.

Fixes 45942b79b9f8 ("net/colo-compare.c: Check that colo-compare is active")
---
 net/colo-compare.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)
net/colo-compare.c +15 -16
index c356419d6a8..be819db06c9 100644
@@ -1416,7 +1416,7 @@ static void colo_compare_finalize(Object *obj)
             break;
         }
     }
-    if (QTAILQ_EMPTY(&net_compares)) {
+    if (colo_compare_active && QTAILQ_EMPTY(&net_compares)) {
         colo_compare_active = false;
         qemu_mutex_destroy(&event_mtx);
         qemu_cond_destroy(&event_complete_cond);
@@ -1431,30 +1431,29 @@ static void colo_compare_finalize(Object *obj)
     }
 
     colo_compare_timer_del(s);
+    g_clear_pointer(&s->event_bh, qemu_bh_delete);
 
-    qemu_bh_delete(s->event_bh);
+    if (s->iothread) {
+        AioContext *ctx = iothread_get_aio_context(s->iothread);
 
-    AioContext *ctx = iothread_get_aio_context(s->iothread);
-    AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
-    if (s->notify_dev) {
-        AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
-    }
+        AIO_WAIT_WHILE(ctx, !s->out_sendco.done);
+        if (s->notify_dev) {
+            AIO_WAIT_WHILE(ctx, !s->notify_sendco.done);
+        }
+
+        /* Release all unhandled packets after compare thread exited */
+        g_queue_foreach(&s->conn_list, colo_flush_packets, s);
+        AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
 
-    /* Release all unhandled packets after compare thead exited */
-    g_queue_foreach(&s->conn_list, colo_flush_packets, s);
-    AIO_WAIT_WHILE(NULL, !s->out_sendco.done);
+        object_unref(OBJECT(s->iothread));
+    }
 
     g_queue_clear(&s->conn_list);
     g_queue_clear(&s->out_sendco.send_list);
     if (s->notify_dev) {
         g_queue_clear(&s->notify_sendco.send_list);
     }
-
-    if (s->connection_track_table) {
-        g_hash_table_destroy(s->connection_track_table);
-    }
-
-    object_unref(OBJECT(s->iothread));
+    g_clear_pointer(&s->connection_track_table, g_hash_table_destroy);
 
     g_free(s->pri_indev);
     g_free(s->sec_indev);
Signature · 1 line
2.54.0
#23 lore raw

PATCH v2 15/46 system/ioport: minor code simplification

In reply to Marc-André Lureau

Drop needless memset() and replace g_malloc0() with g_new().

---
 system/ioport.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
system/ioport.c +1 -2
index 9209bff2eab..303fabf063e 100644
@@ -230,9 +230,8 @@ static void portio_list_add_1(PortioList *piolist,
     mrpio = MEMORY_REGION_PORTIO_LIST(
                 object_new(TYPE_MEMORY_REGION_PORTIO_LIST));
     mrpio->portio_opaque = piolist->opaque;
-    mrpio->ports = g_malloc0(sizeof(MemoryRegionPortio) * (count + 1));
+    mrpio->ports = g_new0(MemoryRegionPortio, count + 1);
     memcpy(mrpio->ports, pio_init, sizeof(MemoryRegionPortio) * count);
-    memset(mrpio->ports + count, 0, sizeof(MemoryRegionPortio));
 
     /* Adjust the offsets to all be zero-based for the region.  */
     for (i = 0; i < count; ++i) {
Signature · 1 line
2.54.0
#24 lore raw

PATCH v2 16/46 system/ioport: Fix qom-list-properties crash on portio list obj

In reply to Marc-André Lureau

From: Peter Xu <[email protected]>

Currently qom-list-properties QMP command will crash when querying the portio list MR object. It's because its finalize() assumes full initialization done in portio_list_add_1().

Provide a simple fix for now to avoid the crash. There is chance for a longer term fix, ideally MR should be initialized in instance_init().

However that'll need more work, and that should also be done with cleaning the hard-coded MR operations in portio_list_add_1(). To be explored.

---
 system/ioport.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
system/ioport.c +9 -2
index 303fabf063e..0a1b80f2994 100644
@@ -345,8 +345,15 @@ static void memory_region_portio_list_finalize(Object *obj)
 {
     MemoryRegionPortioList *mrpio = MEMORY_REGION_PORTIO_LIST(obj);
 
-    object_unref(&mrpio->mr);
-    g_free(mrpio->ports);
+    /*
+     * This check makes sure any random object_new() (without doing the
+     * rest inits in portio_list_add_1()) will not crash when finalizing.
+     * One example is QMP command qom-list-properties.
+     */
+    if (mrpio->ports) {
+        object_unref(&mrpio->mr);
+        g_free(mrpio->ports);
+    }
 }
 
 static const TypeInfo memory_region_portio_list_info = {
Signature · 1 line
2.54.0
#25 lore raw

PATCH v2 17/46 ui/console: remove console from global list on finalization

In reply to Marc-André Lureau

This commit removes the QemuConsole from the global "consoles" list when it is finalized, fixing use-after-free on throw-away objects.

Reproducer: QMP command qom-list-properties with typename "qemu-text-console", "qemu-fixed-text-console" or "qemu-graphic-console".

The assertions added ensure that dcls, gl_block, and the dump_queue are empty before removal, confirming the console is in a clean state. This is left to handle correctly in a future series for hot-unplug case.

---
 ui/console.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
ui/console.c +4 -1
index 799d61ec1a5..2adffb6f04d 100644
@@ -390,10 +390,13 @@ qemu_console_finalize(Object *obj)
 {
     QemuConsole *c = QEMU_CONSOLE(obj);
 
-    /* TODO: check this code path, and unregister from consoles */
+    /* TODO: fix hot-unplug support of consoles */
+    assert(c->gl_block == 0);
+    assert(qemu_co_queue_empty(&c->dump_queue));
     g_clear_pointer(&c->surface, qemu_free_displaysurface);
     g_clear_pointer(&c->gl_unblock_timer, timer_free);
     g_clear_pointer(&c->ui_timer, timer_free);
+    QTAILQ_REMOVE(&consoles, c, next);
 }
 
 static void
Signature · 1 line
2.54.0
#26 lore raw

PATCH v2 18/46 hw/i386/x86: free oem_id and oem_table_id on finalization

In reply to Marc-André Lureau

x86_machine_initfn allocates oem_id and oem_table_id via g_strndup, but no instance_finalize existed for x86_machine_info, so these strings were never freed when the object was destroyed.

Add x86_machine_finalize to release both fields.

Fixes d07b22863b8e ("acpi: Move setters/getters of oem fields to X86MachineState")
---
 hw/i386/x86.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/i386/x86.c +9 -0
index 01872cba073..dc7f0d56b01 100644
@@ -372,6 +372,14 @@ static void x86_machine_initfn(Object *obj)
     x86ms->above_4g_mem_start = 4 * GiB;
 }
 
+static void x86_machine_finalize(Object *obj)
+{
+    X86MachineState *x86ms = X86_MACHINE(obj);
+
+    g_free(x86ms->oem_id);
+    g_free(x86ms->oem_table_id);
+}
+
 static void x86_machine_class_init(ObjectClass *oc, const void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -445,6 +453,7 @@ static const TypeInfo x86_machine_info = {
     .abstract = true,
     .instance_size = sizeof(X86MachineState),
     .instance_init = x86_machine_initfn,
+    .instance_finalize = x86_machine_finalize,
     .class_size = sizeof(X86MachineClass),
     .class_init = x86_machine_class_init,
     .interfaces = (const InterfaceInfo[]) {
Signature · 1 line
2.54.0
#27 lore raw

PATCH v2 19/46 hw/core/machine: free shim_filename on finalization

In reply to Marc-André Lureau

machine_set_shim allocates shim_filename via g_strdup, but machine_finalize did not free it.

Fixes a5bd044b1579 ("x86/loader: add -shim option")
---
 hw/core/machine.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/machine.c +1 -0
index 1b661fd36ae..6a7fb312eb6 100644
@@ -1282,6 +1282,7 @@ static void machine_finalize(Object *obj)
     MachineState *ms = MACHINE(obj);
 
     machine_free_boot_config(ms);
+    g_free(ms->shim_filename);
     g_free(ms->kernel_filename);
     g_free(ms->initrd_filename);
     g_free(ms->kernel_cmdline);
Signature · 1 line
2.54.0
#28 lore raw

PATCH v2 20/46 hw/core/resetcontainer: free children array on finalization

In reply to Marc-André Lureau

resettable_container_init allocates a GPtrArray for children, but resettable_container_finalize was empty and never freed it.

Fixes 4c046ce37af0 ("hw/core: Add ResetContainer which holds objects implementing Resettable")
---
 hw/core/resetcontainer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/core/resetcontainer.c +3 -0
index ef84aa2374a..a4a6476a036 100644
@@ -66,6 +66,9 @@ static void resettable_container_init(Object *obj)
 
 static void resettable_container_finalize(Object *obj)
 {
+    ResettableContainer *rc = RESETTABLE_CONTAINER(obj);
+
+    g_ptr_array_unref(rc->children);
 }
 
 static void resettable_container_class_init(ObjectClass *klass,
Signature · 1 line
2.54.0
#29 lore raw

PATCH v2 21/46 net/filter: free old values in property setters

In reply to Marc-André Lureau

netfilter_set_position and netfilter_set_netdev_id overwrote their respective fields with g_strdup without freeing the previous value, leaking memory on each property write.

Fixes fdccce459621 ("init/cleanup of netfilter object")
---
 net/filter.c | 2 ++
 1 file changed, 2 insertions(+)
net/filter.c +2 -0
index c7cc6615dc9..389f3b0bfef 100644
@@ -130,6 +130,7 @@ static void netfilter_set_netdev_id(Object *obj, const char *str, Error **errp)
 {
     NetFilterState *nf = NETFILTER(obj);
 
+    g_free(nf->netdev_id);
     nf->netdev_id = g_strdup(str);
 }
 
@@ -182,6 +183,7 @@ static void netfilter_set_position(Object *obj, const char *str, Error **errp)
 {
     NetFilterState *nf = NETFILTER(obj);
 
+    g_free(nf->position);
     nf->position = g_strdup(str);
 }
 
Signature · 1 line
2.54.0
#30 lore raw

PATCH v2 22/46 ui/console-vc: destroy fifo on text console finalization

In reply to Marc-André Lureau

qemu_text_console_init creates a fifo8 via fifo8_create, but qemu_text_console_finalize never called fifo8_destroy, leaking the underlying buffer.

Fixes 0c9d0641ac ("ui/console: replace QEMUFIFO with Fifo8")
---
 ui/console-vc.c | 1 +
 1 file changed, 1 insertion(+)
ui/console-vc.c +1 -0
index 6163e21d2c6..3e00d2588c3 100644
@@ -1082,6 +1082,7 @@ qemu_text_console_finalize(Object *obj)
 {
     QemuTextConsole *s = QEMU_TEXT_CONSOLE(obj);
 
+    fifo8_destroy(&s->out_fifo);
     QTAILQ_REMOVE(&vt100s, &s->vt, list);
 }
 
Signature · 1 line
2.54.0
#31 lore raw

PATCH v2 23/46 target/i386/sev: add finalize functions and fix leaking setters

In reply to Marc-André Lureau

None of the three SEV types (sev-common, sev-guest, sev-snp-guest) had instance_finalize functions, so all string fields allocated during init or by property setters were leaked on object destruction.

Add sev_common_finalize, sev_guest_finalize, and sev_snp_guest_finalize to free the allocated fields.

Also fix sev_common_set_sev_device, sev_guest_set_dh_cert_file, and sev_guest_set_session_file to free the old value before replacing it.

Fixes a9b4942f485b ("target/i386: add Secure Encrypted Virtualization (SEV) object")
---
 target/i386/sev.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
target/i386/sev.c +37 -0
index 261b6aaa37b..3c2734ad83b 100644
@@ -2527,6 +2527,7 @@ sev_common_get_sev_device(Object *obj, Error **errp)
 static void
 sev_common_set_sev_device(Object *obj, const char *value, Error **errp)
 {
+    g_free(SEV_COMMON(obj)->sev_device);
     SEV_COMMON(obj)->sev_device = g_strdup(value);
 }
 
@@ -2831,12 +2832,21 @@ sev_common_instance_init(Object *obj)
     QTAILQ_INIT(&sev_common->launch_vmsa);
 }
 
+static void
+sev_common_finalize(Object *obj)
+{
+    SevCommonState *sev_common = SEV_COMMON(obj);
+
+    g_free(sev_common->sev_device);
+}
+
 /* sev guest info common to sev/sev-es/sev-snp */
 static const TypeInfo sev_common_info = {
     .parent = TYPE_X86_CONFIDENTIAL_GUEST,
     .name = TYPE_SEV_COMMON,
     .instance_size = sizeof(SevCommonState),
     .instance_init = sev_common_instance_init,
+    .instance_finalize = sev_common_finalize,
     .class_size = sizeof(SevCommonStateClass),
     .class_init = sev_common_class_init,
     .abstract = true,
@@ -2856,6 +2866,7 @@ sev_guest_get_dh_cert_file(Object *obj, Error **errp)
 static void
 sev_guest_set_dh_cert_file(Object *obj, const char *value, Error **errp)
 {
+    g_free(SEV_GUEST(obj)->dh_cert_file);
     SEV_GUEST(obj)->dh_cert_file = g_strdup(value);
 }
 
@@ -2870,6 +2881,7 @@ sev_guest_get_session_file(Object *obj, Error **errp)
 static void
 sev_guest_set_session_file(Object *obj, const char *value, Error **errp)
 {
+    g_free(SEV_GUEST(obj)->session_file);
     SEV_GUEST(obj)->session_file = g_strdup(value);
 }
 
@@ -2937,12 +2949,23 @@ sev_guest_instance_init(Object *obj)
     sev_guest->legacy_vm_type = ON_OFF_AUTO_AUTO;
 }
 
+static void
+sev_guest_finalize(Object *obj)
+{
+    SevGuestState *sev_guest = SEV_GUEST(obj);
+
+    g_free(sev_guest->dh_cert_file);
+    g_free(sev_guest->session_file);
+    g_free(sev_guest->measurement);
+}
+
 /* guest info specific sev/sev-es */
 static const TypeInfo sev_guest_info = {
     .parent = TYPE_SEV_COMMON,
     .name = TYPE_SEV_GUEST,
     .instance_size = sizeof(SevGuestState),
     .instance_init = sev_guest_instance_init,
+    .instance_finalize = sev_guest_finalize,
     .class_init = sev_guest_class_init,
 };
 
@@ -3195,6 +3218,19 @@ sev_snp_guest_instance_init(Object *obj)
     sev_snp_guest->kvm_start_conf.policy = DEFAULT_SEV_SNP_POLICY;
 }
 
+static void
+sev_snp_guest_finalize(Object *obj)
+{
+    SevSnpGuestState *sev_snp_guest = SEV_SNP_GUEST(obj);
+
+    g_free(sev_snp_guest->guest_visible_workarounds);
+    g_free(sev_snp_guest->id_block_base64);
+    g_free(sev_snp_guest->id_block);
+    g_free(sev_snp_guest->id_auth_base64);
+    g_free(sev_snp_guest->id_auth);
+    g_free(sev_snp_guest->host_data);
+}
+
 /* guest info specific to sev-snp */
 static const TypeInfo sev_snp_guest_info = {
     .parent = TYPE_SEV_COMMON,
@@ -3202,6 +3238,7 @@ static const TypeInfo sev_snp_guest_info = {
     .instance_size = sizeof(SevSnpGuestState),
     .class_init = sev_snp_guest_class_init,
     .instance_init = sev_snp_guest_instance_init,
+    .instance_finalize = sev_snp_guest_finalize,
 };
 
 static void
Signature · 1 line
2.54.0
#32 lore raw

PATCH v2 24/46 target/i386/kvm/tdx: free strings in tdx_guest_finalize

In reply to Marc-André Lureau

tdx_guest_finalize was empty, so mrconfigid, mrowner, and mrownerconfig set by property setters were never freed.

Fixes d05a0858cf87 ("i386/tdx: Support user configurable mrconfigid/mrowner/mrownerconfig")
---
 target/i386/kvm/tdx.c | 5 +++++
 1 file changed, 5 insertions(+)
target/i386/kvm/tdx.c +5 -0
index 4714c9d514e..df46fce7693 100644
@@ -1585,6 +1585,11 @@ static void tdx_guest_init(Object *obj)
 
 static void tdx_guest_finalize(Object *obj)
 {
+    TdxGuest *tdx = TDX_GUEST(obj);
+
+    g_free(tdx->mrconfigid);
+    g_free(tdx->mrowner);
+    g_free(tdx->mrownerconfig);
 }
 
 static ResettableState *tdx_reset_state(Object *obj)
Signature · 1 line
2.54.0
#33 lore raw

PATCH v2 25/46 hw/i386/nitro_enclave: add instance finalize

In reply to Marc-André Lureau

Property setter strings (vsock, id, parent_role, parent_id) were never freed because nitro_enclave_machine_info had no instance_finalize.

Fixes f1826463d2e8 ("machine/nitro-enclave: New machine type for AWS Nitro Enclaves")
---
 hw/i386/nitro_enclave.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/i386/nitro_enclave.c +11 -0
index a29f0044d0e..1ce9ee3358a 100644
@@ -337,11 +337,22 @@ static void nitro_enclave_class_init(ObjectClass *oc, const void *data)
                                           "Set parent instance identifier");
 }
 
+static void nitro_enclave_machine_finalize(Object *obj)
+{
+    NitroEnclaveMachineState *nems = NITRO_ENCLAVE_MACHINE(obj);
+
+    g_free(nems->vsock);
+    g_free(nems->id);
+    g_free(nems->parent_role);
+    g_free(nems->parent_id);
+}
+
 static const TypeInfo nitro_enclave_machine_info = {
     .name          = TYPE_NITRO_ENCLAVE_MACHINE,
     .parent        = TYPE_MICROVM_MACHINE,
     .instance_size = sizeof(NitroEnclaveMachineState),
     .instance_init = nitro_enclave_machine_initfn,
+    .instance_finalize = nitro_enclave_machine_finalize,
     .class_size    = sizeof(NitroEnclaveMachineClass),
     .class_init    = nitro_enclave_class_init,
 };
Signature · 1 line
2.54.0
#34 lore raw

PATCH v2 26/46 net/can: free ifname on socketcan finalization

In reply to Marc-André Lureau

can_host_socketcan_set_if allocates ifname via g_strdup, but no instance_finalize existed to free it.

Fixes ea15ea8a7c67 ("net/can: support for connecting to Linux host SocketCAN interface.")
---
 net/can/can_socketcan.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/can/can_socketcan.c +8 -0
index 8a57ae07178..be67ed7f5a9 100644
@@ -319,11 +319,19 @@ static void can_host_socketcan_class_init(ObjectClass *klass,
     chc->disconnect = can_host_socketcan_disconnect;
 }
 
+static void can_host_socketcan_finalize(Object *obj)
+{
+    CanHostSocketCAN *c = CAN_HOST_SOCKETCAN(obj);
+
+    g_free(c->ifname);
+}
+
 static const TypeInfo can_host_socketcan_info = {
     .parent = TYPE_CAN_HOST,
     .name = TYPE_CAN_HOST_SOCKETCAN,
     .instance_size = sizeof(CanHostSocketCAN),
     .instance_init = can_host_socketcan_instance_init,
+    .instance_finalize = can_host_socketcan_finalize,
     .class_init = can_host_socketcan_class_init,
 };
 
Signature · 1 line
2.54.0
#35 lore raw

PATCH v2 27/46 backends/igvm-cfg: free filename on finalization

In reply to Marc-André Lureau

set_igvm allocates filename via g_strdup, but igvm_cfg_finalize did not free it.

Fixes c1d466d267cf ("backends/igvm: Add IGVM loader and configuration")
---
 backends/igvm-cfg.c | 1 +
 1 file changed, 1 insertion(+)
backends/igvm-cfg.c +1 -0
index 64589ca34f2..e1f09855f66 100644
@@ -108,4 +108,5 @@ static void igvm_cfg_finalize(Object *obj)
     if (igvm->file >= 0) {
         igvm_free(igvm->file);
     }
+    g_free(igvm->filename);
 }
Signature · 1 line
2.54.0
#36 lore raw

PATCH v2 28/46 scsi/pr-manager-helper: free path on finalization

In reply to Marc-André Lureau

set_path allocates path via g_strdup, but pr_manager_helper_instance_finalize did not free it.

Fixes 9bad2a6b9d0a ("scsi: add persistent reservation manager using qemu-pr-helper")
---
 scsi/pr-manager-helper.c | 1 +
 1 file changed, 1 insertion(+)
scsi/pr-manager-helper.c +1 -0
index f6454cd80de..53432e6d606 100644
@@ -284,6 +284,7 @@ static void pr_manager_helper_instance_finalize(Object *obj)
 {
     PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
 
+    g_free(pr_mgr->path);
     object_unref(OBJECT(pr_mgr->ioc));
     qemu_mutex_destroy(&pr_mgr->lock);
 }
Signature · 1 line
2.54.0
#37 lore raw

PATCH v2 29/46 accel/kvm: free device path on finalization

In reply to Marc-André Lureau

kvm_set_device allocates device via g_strdup, but no instance_finalize existed for the KVM accelerator type.

Fixes aef158b093b9 ("Add class property to configure KVM device node to use")
---
 accel/kvm/kvm-all.c | 8 ++++++++
 1 file changed, 8 insertions(+)
accel/kvm/kvm-all.c +8 -0
index 92af42503b1..96f90ebb240 100644
@@ -4342,10 +4342,18 @@ static void kvm_accel_class_init(ObjectClass *oc, const void *data)
     kvm_arch_accel_class_init(oc);
 }
 
+static void kvm_accel_finalize(Object *obj)
+{
+    KVMState *s = KVM_STATE(obj);
+
+    g_free(s->device);
+}
+
 static const TypeInfo kvm_accel_type = {
     .name = TYPE_KVM_ACCEL,
     .parent = TYPE_ACCEL,
     .instance_init = kvm_accel_instance_init,
+    .instance_finalize = kvm_accel_finalize,
     .class_init = kvm_accel_class_init,
     .instance_size = sizeof(KVMState),
 };
Signature · 1 line
2.54.0
#38 lore raw

PATCH v2 30/46 system/qtest: add missing qtest_finalize()

In reply to Marc-André Lureau

Free owned resources on object finalization.

Fixes 6ba7ada3559e ("qtest: add a QOM object for qtest")
---
 system/qtest.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
system/qtest.c +10 -0
index cf90cd53adb..a79d10d1361 100644
@@ -1020,10 +1020,20 @@ static void qtest_class_init(ObjectClass *oc, const void *data)
                                   qtest_get_log, qtest_set_log);
 }
 
+static void qtest_finalize(Object *obj)
+{
+    QTest *q = QTEST(obj);
+
+    g_free(q->chr_name);
+    g_free(q->log);
+    object_unref(q->chr);
+}
+
 static const TypeInfo qtest_info = {
     .name = TYPE_QTEST,
     .parent = TYPE_OBJECT,
     .class_init = qtest_class_init,
+    .instance_finalize = qtest_finalize,
     .instance_size = sizeof(QTest),
     .interfaces = (const InterfaceInfo[]) {
         { TYPE_USER_CREATABLE },
Signature · 1 line
2.54.0
#40 lore raw

PATCH v2 31/46 hw/i386/pc: free pcspk on finalization

In reply to Marc-André Lureau

Add pc_machine_finalize() to unref the pcspk device when it was never realized. Once realized, the bus owns it and no action is needed.

Fixes 6b8d1416482f ("audio: create pcspk device early")
---
 hw/i386/pc.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/i386/pc.c +10 -0
index 2ecad3c503f..7b6ad97e5a9 100644
@@ -1610,6 +1610,15 @@ static void pc_machine_initfn(Object *obj)
     }
 }
 
+static void pc_machine_finalize(Object *obj)
+{
+    PCMachineState *pcms = PC_MACHINE(obj);
+
+    if (pcms->pcspk && !qdev_is_realized(DEVICE(pcms->pcspk))) {
+        object_unref(OBJECT(pcms->pcspk));
+    }
+}
+
 static void pc_machine_reset(MachineState *machine, ResetType type)
 {
     CPUState *cs;
@@ -1748,6 +1757,7 @@ static const TypeInfo pc_machine_info = {
     .abstract = true,
     .instance_size = sizeof(PCMachineState),
     .instance_init = pc_machine_initfn,
+    .instance_finalize = pc_machine_finalize,
     .class_size = sizeof(PCMachineClass),
     .class_init = pc_machine_class_init,
     .interfaces = (const InterfaceInfo[]) {
Signature · 1 line
2.54.0
#41 lore raw

PATCH v2 32/46 hw/tpm: free PPI buffer on finalization

In reply to Marc-André Lureau

The PPI buffer is allocated with qemu_memalign() in instance_init but never freed when the device is destroyed.

Fixes 46cd2c1050f0 ("hw/tpm: add PPI support to tpm-tis-device for ARM64 virt")
---
 hw/tpm/tpm_tis_sysbus.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/tpm/tpm_tis_sysbus.c +9 -0
index 6bec30c36fc..3984228c42f 100644
@@ -150,11 +150,20 @@ static void tpm_tis_sysbus_class_init(ObjectClass *klass, const void *data)
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 }
 
+static void tpm_tis_sysbus_finalize(Object *obj)
+{
+    TPMStateSysBus *sbdev = TPM_TIS_SYSBUS(obj);
+    TPMState *s = &sbdev->state;
+
+    qemu_vfree(s->ppi.buf);
+}
+
 static const TypeInfo tpm_tis_sysbus_info = {
     .name = TYPE_TPM_TIS_SYSBUS,
     .parent = TYPE_DYNAMIC_SYS_BUS_DEVICE,
     .instance_size = sizeof(TPMStateSysBus),
     .instance_init = tpm_tis_sysbus_initfn,
+    .instance_init = tpm_tis_sysbus_finalize,
     .class_init  = tpm_tis_sysbus_class_init,
     .interfaces = (const InterfaceInfo[]) {
         { TYPE_TPM_IF },
Signature · 1 line
2.54.0
#42 lore raw

PATCH v2 33/46 hw/fsi: move OPBus address space init to realize

In reply to Marc-André Lureau

The OPBus instance_init initializes an AddressSpace, registering it in the global address_spaces list. When a bare OPBus object is created and destroyed (e.g. by qom-tests), there is no finalize to remove the stale entry, leading to a heap-use-after-free when a subsequent flatviews_reset iterates the list.

Move address_space_init to the bus realize callback and add the corresponding address_space_destroy in unrealize, following the NubusBus pattern. Also fix the memory_region_init owner from NULL to the OPBus object, so the MR is properly parented instead of dangling under the "unattached" container.

Fixes eb04c35da2c0 ("hw/fsi: Aspeed APB2OPB & On-chip peripheral bus")
---
 hw/fsi/aspeed_apb2opb.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
hw/fsi/aspeed_apb2opb.c +23 -1
index b9d72f3ecf6..36092468391 100644
@@ -348,15 +348,37 @@ static void fsi_opb_init(Object *o)
 {
     OPBus *opb = OP_BUS(o);
 
-    memory_region_init(&opb->mr, 0, TYPE_FSI_OPB, UINT32_MAX);
+    memory_region_init(&opb->mr, o, TYPE_FSI_OPB, UINT32_MAX);
+}
+
+static void fsi_opb_realize(BusState *bus, Error **errp)
+{
+    OPBus *opb = OP_BUS(bus);
+
     address_space_init(&opb->as, &opb->mr, TYPE_FSI_OPB);
 }
 
+static void fsi_opb_unrealize(BusState *bus)
+{
+    OPBus *opb = OP_BUS(bus);
+
+    address_space_destroy(&opb->as);
+}
+
+static void fsi_opb_class_init(ObjectClass *klass, const void *data)
+{
+    BusClass *bc = BUS_CLASS(klass);
+
+    bc->realize = fsi_opb_realize;
+    bc->unrealize = fsi_opb_unrealize;
+}
+
 static const TypeInfo opb_info = {
     .name = TYPE_OP_BUS,
     .parent = TYPE_BUS,
     .instance_init = fsi_opb_init,
     .instance_size = sizeof(OPBus),
+    .class_init = fsi_opb_class_init,
 };
 
 static void fsi_opb_register_types(void)
Signature · 1 line
2.54.0
Reviewed-by on v2 33/46
#43 lore raw

Re: [PATCH v2 33/46] hw/fsi: move OPBus address space init to realize

Quoted 52 lines · Marc-André Lureau

The OPBus instance_init initializes an AddressSpace, registering it in the global address_spaces list. When a bare OPBus object is created and destroyed (e.g. by qom-tests), there is no finalize to remove the stale entry, leading to a heap-use-after-free when a subsequent flatviews_reset iterates the list.

Move address_space_init to the bus realize callback and add the corresponding address_space_destroy in unrealize, following the NubusBus pattern. Also fix the memory_region_init owner from NULL to the OPBus object, so the MR is properly parented instead of dangling under the "unattached" container.

Fixes eb04c35da2c0 ("hw/fsi: Aspeed APB2OPB & On-chip peripheral bus")
---
  hw/fsi/aspeed_apb2opb.c | 24 +++++++++++++++++++++++-
  1 file changed, 23 insertions(+), 1 deletion(-)
hw/fsi/aspeed_apb2opb.c +21 -1
index b9d72f3ecf6..36092468391 100644
@@ -348,15 +348,37 @@ static void fsi_opb_init(Object *o)
  {
      OPBus *opb = OP_BUS(o);
 
-    memory_region_init(&opb->mr, 0, TYPE_FSI_OPB, UINT32_MAX);
+    memory_region_init(&opb->mr, o, TYPE_FSI_OPB, UINT32_MAX);
+}
+
+static void fsi_opb_realize(BusState *bus, Error **errp)
+{
+    OPBus *opb = OP_BUS(bus);
+
      address_space_init(&opb->as, &opb->mr, TYPE_FSI_OPB);
  }
 
+static void fsi_opb_unrealize(BusState *bus)
+{
+    OPBus *opb = OP_BUS(bus);
+
+    address_space_destroy(&opb->as);
+}
+
+static void fsi_opb_class_init(ObjectClass *klass, const void *data)
+{
+    BusClass *bc = BUS_CLASS(klass);
+
+    bc->realize = fsi_opb_realize;
+    bc->unrealize = fsi_opb_unrealize;
+}

hw/fsi/aspeed_apb2opb.c static void fsi_opb_init(Object *o) · line 384

Quoted 10 lines
hw/fsi/aspeed_apb2opb.c +2 -0
+
  static const TypeInfo opb_info = {
      .name = TYPE_OP_BUS,
      .parent = TYPE_BUS,
      .instance_init = fsi_opb_init,
      .instance_size = sizeof(OPBus),
+    .class_init = fsi_opb_class_init,
  };
 
  static void fsi_opb_register_types(void)

Thanks,

C.

#44 lore raw

PATCH v2 34/46 hw/fsi: move OPBus qbus_init() to instance_init

In reply to Marc-André Lureau

Remove the TODO comment that documented the workaround, as it is no longer needed.

---
 hw/fsi/aspeed_apb2opb.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
hw/fsi/aspeed_apb2opb.c +1 -12
index 36092468391..058abc86452 100644
@@ -273,6 +273,7 @@ static void fsi_aspeed_apb2opb_init(Object *o)
     for (i = 0; i < ASPEED_FSI_NUM; i++) {
         object_initialize_child(o, "fsi-master[*]", &s->fsi[i],
                                 TYPE_FSI_MASTER);
+        qbus_init(&s->opb[i], sizeof(s->opb[i]), TYPE_OP_BUS, DEVICE(s), NULL);
     }
 }
 
@@ -282,18 +283,6 @@ static void fsi_aspeed_apb2opb_realize(DeviceState *dev, Error **errp)
     AspeedAPB2OPBState *s = ASPEED_APB2OPB(dev);
     int i;
 
-    /*
-     * TODO: The OPBus model initializes the OPB address space in
-     * the .instance_init handler and this is problematic for test
-     * device-introspect-test. To avoid a memory corruption and a QEMU
-     * crash, qbus_init() should be called from realize(). Something to
-     * improve. Possibly, OPBus could also be removed.
-     */
-    for (i = 0; i < ASPEED_FSI_NUM; i++) {
-        qbus_init(&s->opb[i], sizeof(s->opb[i]), TYPE_OP_BUS, DEVICE(s),
-                  NULL);
-    }
-
     sysbus_init_irq(sbd, &s->irq);
 
     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_apb2opb_ops, s,
Signature · 1 line
2.54.0
Reviewed-by on v2 34/46
#45 lore raw

Re: [PATCH v2 34/46] hw/fsi: move OPBus qbus_init() to instance_init

Quoted 14 lines · Marc-André Lureau

Remove the TODO comment that documented the workaround, as it is no longer needed.

---
  hw/fsi/aspeed_apb2opb.c | 13 +------------
  1 file changed, 1 insertion(+), 12 deletions(-)
hw/fsi/aspeed_apb2opb.c +0 -0
index 36092468391..058abc86452 100644

hw/fsi/aspeed_apb2opb.c static void fsi_aspeed_apb2opb_init(Object *o) · line 276

Quoted 4 lines
hw/fsi/aspeed_apb2opb.c +1 -0
@@ -273,6 +273,7 @@ static void fsi_aspeed_apb2opb_init(Object *o)
      for (i = 0; i < ASPEED_FSI_NUM; i++) {
          object_initialize_child(o, "fsi-master[*]", &s->fsi[i],
                                  TYPE_FSI_MASTER);
+        qbus_init(&s->opb[i], sizeof(s->opb[i]), TYPE_OP_BUS, DEVICE(s), NULL);

nice !

Quoted 13 lines
hw/fsi/aspeed_apb2opb.c +0 -5
      }
  }
 
@@ -282,18 +283,6 @@ static void fsi_aspeed_apb2opb_realize(DeviceState *dev, Error **errp)
      AspeedAPB2OPBState *s = ASPEED_APB2OPB(dev);
      int i;
 
-    /*
-     * TODO: The OPBus model initializes the OPB address space in
-     * the .instance_init handler and this is problematic for test
-     * device-introspect-test. To avoid a memory corruption and a QEMU
-     * crash, qbus_init() should be called from realize(). Something to

hw/fsi/aspeed_apb2opb.c static void fsi_aspeed_apb2opb_realize(DeviceState *dev, Error **errp) · line 288

Quoted 10 lines
hw/fsi/aspeed_apb2opb.c +0 -7
-     * improve. Possibly, OPBus could also be removed.
-     */
-    for (i = 0; i < ASPEED_FSI_NUM; i++) {
-        qbus_init(&s->opb[i], sizeof(s->opb[i]), TYPE_OP_BUS, DEVICE(s),
-                  NULL);
-    }
-
      sysbus_init_irq(sbd, &s->irq);
 
      memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_apb2opb_ops, s,

Thanks,

C.

#46 lore raw

PATCH v2 35/46 hw/gpio/pca9552: fix state_str leak in pca955x_set_led

In reply to Marc-André Lureau

visit_type_str() allocates state_str, but the function never frees it on any code path. Use g_autofree to ensure it is freed on return.

Fixes a90d8f84674d ("misc/pca9552: Add qom set and get")
---
 hw/gpio/pca9552.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/gpio/pca9552.c +1 -1
index dd3f1536b65..472d8ad9571 100644
@@ -342,7 +342,7 @@ static void pca955x_set_led(Object *obj, Visitor *v, const char *name,
     PCA955xState *s = PCA955X(obj);
     int led, rc, reg, val;
     uint8_t state;
-    char *state_str;
+    g_autofree char *state_str = NULL;
 
     if (!visit_type_str(v, name, &state_str, errp)) {
         return;
Signature · 1 line
2.54.0
#47 lore raw

PATCH v2 36/46 hw/arm/aspeed: free fmc_model and spi_model on finalization

In reply to Marc-André Lureau

The fmc_model and spi_model strings are allocated via g_strdup in property setters but never freed when the machine object is destroyed.

Fixes 9820e52fbef7 ("hw/arm/aspeed: Add machine properties to define the flash models")
---
 hw/arm/aspeed.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/arm/aspeed.c +9 -0
index a3db3406c5f..a48c4420583 100644
@@ -419,12 +419,21 @@ static void aspeed_machine_class_init(ObjectClass *oc, const void *data)
     aspeed_machine_class_props_init(oc);
 }
 
+static void aspeed_machine_instance_finalize(Object *obj)
+{
+    AspeedMachineState *bmc = ASPEED_MACHINE(obj);
+
+    g_free(bmc->fmc_model);
+    g_free(bmc->spi_model);
+}
+
 static const TypeInfo aspeed_machine_types[] = {
     {
         .name          = TYPE_ASPEED_MACHINE,
         .parent        = TYPE_MACHINE,
         .instance_size = sizeof(AspeedMachineState),
         .instance_init = aspeed_machine_instance_init,
+        .instance_finalize = aspeed_machine_instance_finalize,
         .class_size    = sizeof(AspeedMachineClass),
         .class_init    = aspeed_machine_class_init,
         .abstract      = true,
Signature · 1 line
2.54.0
#48 lore raw

PATCH v2 37/46 hw/arm/sbsa-ref: free unrealized flash devices on finalization

In reply to Marc-André Lureau

Flash devices are created with qdev_new() in instance_init and added as children, but the initial reference from qdev_new() is only dropped by sysbus_realize_and_unref() during machine init. When the machine object is destroyed before realization (e.g. during qtest device introspection), the flash devices leak.

Fixes e9fdf453240e ("hw/arm: Add arm SBSA reference machine, devices part")
---
 hw/arm/sbsa-ref.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
hw/arm/sbsa-ref.c +12 -0
index 52c35e10c2d..484b90053e8 100644
@@ -892,6 +892,17 @@ static void sbsa_ref_instance_init(Object *obj)
     sbsa_flash_create(sms);
 }
 
+static void sbsa_ref_instance_finalize(Object *obj)
+{
+    SBSAMachineState *sms = SBSA_MACHINE(obj);
+
+    for (int i = 0; i < ARRAY_SIZE(sms->flash); i++) {
+        if (sms->flash[i] && !qdev_is_realized(DEVICE(sms->flash[i]))) {
+            object_unref(OBJECT(sms->flash[i]));
+        }
+    }
+}
+
 static void sbsa_ref_class_init(ObjectClass *oc, const void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -930,6 +941,7 @@ static const TypeInfo sbsa_ref_info = {
     .name          = TYPE_SBSA_MACHINE,
     .parent        = TYPE_MACHINE,
     .instance_init = sbsa_ref_instance_init,
+    .instance_finalize = sbsa_ref_instance_finalize,
     .class_init    = sbsa_ref_class_init,
     .instance_size = sizeof(SBSAMachineState),
     .interfaces    = aarch64_machine_interfaces,
Signature · 1 line
2.54.0
#49 lore raw

PATCH v2 38/46 hw/arm/virt: free flash devices and OEM strings on finalization

In reply to Marc-André Lureau

Flash devices created in instance_init via qdev_new() hold an extra reference that is only dropped on sysbus_realize_and_unref(). When the machine is destroyed before realization, the flash objects leak. Also, the oem_id and oem_table_id strings from g_strndup() are never freed.

Fixes 602b458201ff ("acpi: Permit OEM ID and OEM table ID fields to be changed")
---
 hw/arm/virt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/arm/virt.c +14 -0
index 77891f08200..9510d9eea73 100644
@@ -4031,6 +4031,19 @@ static void virt_instance_init(Object *obj)
     cxl_machine_init(obj, &vms->cxl_devices_state);
 }
 
+static void virt_instance_finalize(Object *obj)
+{
+    VirtMachineState *vms = VIRT_MACHINE(obj);
+
+    for (int i = 0; i < ARRAY_SIZE(vms->flash); i++) {
+        if (vms->flash[i] && !qdev_is_realized(DEVICE(vms->flash[i]))) {
+            object_unref(OBJECT(vms->flash[i]));
+        }
+    }
+    g_free(vms->oem_id);
+    g_free(vms->oem_table_id);
+}
+
 static const TypeInfo virt_machine_info = {
     .name          = TYPE_VIRT_MACHINE,
     .parent        = TYPE_MACHINE,
@@ -4039,6 +4052,7 @@ static const TypeInfo virt_machine_info = {
     .class_size    = sizeof(VirtMachineClass),
     .class_init    = virt_machine_class_init,
     .instance_init = virt_instance_init,
+    .instance_finalize = virt_instance_finalize,
     .interfaces = (const InterfaceInfo[]) {
          { TYPE_HOTPLUG_HANDLER },
          { }
Signature · 1 line
2.54.0
#50 lore raw

PATCH v2 39/46 hw/loongarch/virt: free flash devices and OEM strings on finalization

In reply to Marc-André Lureau

Flash devices created in virt_initfn() via qdev_new() hold an extra reference that is only dropped on sysbus_realize_and_unref(). When the machine is destroyed before realization, the flash objects leak. Also, the oem_id and oem_table_id strings from g_strndup() are never freed.

Fixes 445c9c645 ("hw/loongarch/virt: Allow user to customize OEM ID and OEM table ID")
---
 hw/loongarch/virt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/loongarch/virt.c +14 -0
index 2fc15261309..188fbc86c0c 100644
@@ -1535,6 +1535,19 @@ static void virt_class_init(ObjectClass *oc, const void *data)
                                           "for PCI MMIO");
 }
 
+static void virt_instance_finalize(Object *obj)
+{
+    LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(obj);
+
+    for (int i = 0; i < ARRAY_SIZE(lvms->flash); i++) {
+        if (lvms->flash[i] && !qdev_is_realized(DEVICE(lvms->flash[i]))) {
+            object_unref(OBJECT(lvms->flash[i]));
+        }
+    }
+    g_free(lvms->oem_id);
+    g_free(lvms->oem_table_id);
+}
+
 static const TypeInfo virt_machine_types[] = {
     {
         .name           = TYPE_LOONGARCH_VIRT_MACHINE,
@@ -1542,6 +1555,7 @@ static const TypeInfo virt_machine_types[] = {
         .instance_size  = sizeof(LoongArchVirtMachineState),
         .class_init     = virt_class_init,
         .instance_init  = virt_initfn,
+        .instance_finalize = virt_instance_finalize,
         .interfaces = (const InterfaceInfo[]) {
          { TYPE_HOTPLUG_HANDLER },
          { }
Signature · 1 line
2.54.0
#51 lore raw

PATCH v2 40/46 hw/ppc/spapr: free host_model and host_serial on finalization

In reply to Marc-André Lureau

The host_model and host_serial strings are allocated via g_strdup in property setters but never freed when the machine is destroyed.

Fixes 27461d69a0f ("ppc: add host-serial and host-model machine attributes (CVE-2019-8934)")
---
 hw/ppc/spapr.c | 2 ++
 1 file changed, 2 insertions(+)
hw/ppc/spapr.c +2 -0
index 428b491a7df..5c673bbf0dd 100644
@@ -3517,6 +3517,8 @@ static void spapr_machine_finalizefn(Object *obj)
     SpaprMachineState *spapr = SPAPR_MACHINE(obj);
 
     g_free(spapr->kvm_type);
+    g_free(spapr->host_model);
+    g_free(spapr->host_serial);
 }
 
 void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
Signature · 1 line
2.54.0
#52 lore raw

PATCH v2 41/46 hw/ppc/pnv: drop extra ref on PHB after adding as child

In reply to Marc-André Lureau

object_new() returns an object with refcount 1, and object_property_add_child() adds another reference. The initial reference must be dropped so the parent becomes the sole owner, otherwise the PHB objects leak when the chip is destroyed.

Fixes 0d512c7120a2 ("ppc/pnv: turn chip8->phbs[] into a PnvPHB* array")
---
 hw/ppc/pnv.c | 1 +
 1 file changed, 1 insertion(+)
hw/ppc/pnv.c +1 -0
index 9ed918fa6a1..8306754d9ad 100644
@@ -1696,6 +1696,7 @@ static void pnv_chip_power8_instance_init(Object *obj)
              */
             object_property_add_child(obj, "phb[*]", phb);
             chip8->phbs[i] = PNV_PHB(phb);
+            object_unref(phb);
         }
     }
 
Signature · 1 line
2.54.0
#53 lore raw

PATCH v2 42/46 target/riscv: fix general_user_opts hash table leak

In reply to Marc-André Lureau

The global general_user_opts hash table is recreated on every riscv_cpu_init() call, leaking the previous one.

Furthermore, the CPU settings should be associated with their instance and not global.

Add a finalize() to free associated instances.

Fixes d167a2247ede ("target/riscv: move 'pmu-mask' and 'pmu-num' to riscv_cpu_properties[]")
---
 target/riscv/cpu.h         |  3 ++-
 target/riscv/cpu.c         | 51 +++++++++++++++++++++++++++-------------------
 target/riscv/kvm/kvm-cpu.c |  6 +++---
 3 files changed, 35 insertions(+), 25 deletions(-)
target/riscv/cpu.h +2 -1
index 81c41e34296..55a26aeb5be 100644
@@ -546,6 +546,7 @@ struct ArchCPU {
     uint32_t pmu_avail_ctrs;
     /* Mapping of events to counters */
     GHashTable *pmu_event_ctr_map;
+    GHashTable *user_options;
     const GPtrArray *decoders;
 };
 
@@ -619,7 +620,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                         bool probe, uintptr_t retaddr);
 char *riscv_isa_string(RISCVCPU *cpu);
 int riscv_cpu_max_xlen(RISCVCPUClass *mcc);
-bool riscv_cpu_option_set(const char *optname);
+bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname);
 
 #ifndef CONFIG_USER_ONLY
 void riscv_cpu_do_interrupt(CPUState *cpu);
target/riscv/cpu.c +30 -21

164 lines not on this page · raw message

target/riscv/kvm/kvm-cpu.c +3 -3
index b047ffa9c0c..e0241870ada 100644
@@ -2025,7 +2025,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     if (cpu->cfg.ext_zicbom &&
-        riscv_cpu_option_set(kvm_cbom_blocksize.name)) {
+        riscv_cpu_option_set(cpu, kvm_cbom_blocksize.name)) {
 
         reg.id = KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CONFIG,
                                         kvm_cbom_blocksize.kvm_reg_id);
@@ -2044,7 +2044,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     if (cpu->cfg.ext_zicboz &&
-        riscv_cpu_option_set(kvm_cboz_blocksize.name)) {
+        riscv_cpu_option_set(cpu, kvm_cboz_blocksize.name)) {
 
         reg.id = KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CONFIG,
                                         kvm_cboz_blocksize.kvm_reg_id);
@@ -2063,7 +2063,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     /* Users are setting vlen, not vlenb */
-    if (riscv_has_ext(env, RVV) && riscv_cpu_option_set("vlen")) {
+    if (riscv_has_ext(env, RVV) && riscv_cpu_option_set(cpu, "vlen")) {
         if (!kvm_v_vlenb.supported) {
             error_setg(errp, "Unable to set 'vlenb': register not supported");
             return;
Signature · 1 line
2.54.0
#54 lore raw

Re: [PATCH v2 42/46] target/riscv: fix general_user_opts hash table leak

Hi

Quoted 201 lines · Marc-André Lureau

The global general_user_opts hash table is recreated on every riscv_cpu_init() call, leaking the previous one.

Furthermore, the CPU settings should be associated with their instance and not global.

Add a finalize() to free associated instances.

Fixes d167a2247ede ("target/riscv: move 'pmu-mask' and 'pmu-num' to riscv_cpu_properties[]")
---
 target/riscv/cpu.h         |  3 ++-
 target/riscv/cpu.c         | 51 +++++++++++++++++++++++++++-------------------
 target/riscv/kvm/kvm-cpu.c |  6 +++---
 3 files changed, 35 insertions(+), 25 deletions(-)
target/riscv/cpu.h +2 -1
index 81c41e34296..55a26aeb5be 100644
@@ -546,6 +546,7 @@ struct ArchCPU {
     uint32_t pmu_avail_ctrs;
     /* Mapping of events to counters */
     GHashTable *pmu_event_ctr_map;
+    GHashTable *user_options;
     const GPtrArray *decoders;
 };
 
@@ -619,7 +620,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                         bool probe, uintptr_t retaddr);
 char *riscv_isa_string(RISCVCPU *cpu);
 int riscv_cpu_max_xlen(RISCVCPUClass *mcc);
-bool riscv_cpu_option_set(const char *optname);
+bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname);
 
 #ifndef CONFIG_USER_ONLY
 void riscv_cpu_do_interrupt(CPUState *cpu);
target/riscv/cpu.c +20 -21
index ce15a17c37d..be9a3ac13e7 100644
@@ -27,6 +27,7 @@
 #include "qapi/error.h"
 #include "qapi/visitor.h"
 #include "qemu/error-report.h"
+#include "qemu/timer.h"
 #include "hw/core/qdev-properties.h"
 #include "hw/core/qdev-prop-internal.h"
 #include "migration/vmstate.h"
@@ -59,18 +60,16 @@ bool riscv_cpu_is_32bit(RISCVCPU *cpu)
     return riscv_cpu_mxl(&cpu->env) == MXL_RV32;
 }
 
-/* Hash that stores general user set numeric options */
-static GHashTable *general_user_opts;
-
-static void cpu_option_add_user_setting(const char *optname, uint32_t value)
+static void cpu_option_add_user_setting(RISCVCPU *cpu, const char *optname,
+                                        uint32_t value)
 {
-    g_hash_table_insert(general_user_opts, (gpointer)optname,
+    g_hash_table_insert(cpu->user_options, (gpointer)optname,
                         GUINT_TO_POINTER(value));
 }
 
-bool riscv_cpu_option_set(const char *optname)
+bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname)
 {
-    return g_hash_table_contains(general_user_opts, optname);
+    return g_hash_table_contains(cpu->user_options, optname);
 }
 
 #ifndef CONFIG_USER_ONLY
@@ -1103,7 +1102,7 @@ static void riscv_cpu_init(Object *obj)
                             "riscv.cpu.rnmi", RNMI_MAX);
 #endif /* CONFIG_USER_ONLY */
 
-    general_user_opts = g_hash_table_new(g_str_hash, g_str_equal);
+    cpu->user_options = g_hash_table_new(g_str_hash, g_str_equal);
 
     /*
      * The timer and performance counters extensions were supported
@@ -1453,7 +1452,7 @@ static void prop_pmu_num_set(Object *obj, Visitor *v, const char *name,
 
     warn_report("\"pmu-num\" property is deprecated; use \"pmu-mask\"");
     cpu->cfg.pmu_mask = pmu_mask;
-    cpu_option_add_user_setting("pmu-mask", pmu_mask);
+    cpu_option_add_user_setting(cpu, "pmu-mask", pmu_mask);
 }
 
 static void prop_pmu_num_get(Object *obj, Visitor *v, const char *name,
@@ -1495,7 +1494,7 @@ static void prop_pmu_mask_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.pmu_mask = value;
 }
 
@@ -1527,7 +1526,7 @@ static void prop_mmu_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.mmu = value;
 }
 
@@ -1559,7 +1558,7 @@ static void prop_pmp_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.pmp = value;
 }
 
@@ -1599,7 +1598,7 @@ static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.pmp_regions = value;
 }
 
@@ -1637,7 +1636,7 @@ static void prop_pmp_granularity_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.pmp_granularity = value;
 }
 
@@ -1710,7 +1709,7 @@ static void prop_priv_spec_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, priv_version);
+    cpu_option_add_user_setting(cpu, name, priv_version);
     cpu->env.priv_ver = priv_version;
 }
 
@@ -1744,7 +1743,7 @@ static void prop_vext_spec_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, VEXT_VERSION_1_00_0);
+    cpu_option_add_user_setting(cpu, name, VEXT_VERSION_1_00_0);
     cpu->env.vext_ver = VEXT_VERSION_1_00_0;
 }
 
@@ -1787,7 +1786,7 @@ static void prop_vlen_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.vlenb = value >> 3;
 }
 
@@ -1828,7 +1827,7 @@ static void prop_elen_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.elen = value;
 }
 
@@ -1864,7 +1863,7 @@ static void prop_cbom_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.cbom_blocksize = value;
 }
 
@@ -1900,7 +1899,7 @@ static void prop_cbop_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.cbop_blocksize = value;
 }
 
@@ -1936,7 +1935,7 @@ static void prop_cboz_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(name, value);
+    cpu_option_add_user_setting(cpu, name, value);
     cpu->cfg.cboz_blocksize = value;
 }
 

target/riscv/cpu.c void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename) · line 2981

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -2974,6 +2973,15 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
     DEFINE_RISCV_CPU(type_name, parent_type_name,             \
         .profile = &(profile_))
 
+static void riscv_cpu_instance_finalize(Object *obj)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+
+    g_clear_pointer(&cpu->pmu_timer, timer_free);
+    g_clear_pointer(&cpu->pmu_event_ctr_map, g_hash_table_destroy);

Those 2 should be guarded with
#ifndef CONFIG_USER_ONLY

fixed

Quoted 49 lines
target/riscv/cpu.c +4 -0
+    g_clear_pointer(&cpu->user_options, g_hash_table_destroy);
+}
+
 static const TypeInfo riscv_cpu_type_infos[] = {
     {
         .name = TYPE_RISCV_CPU,
@@ -2981,6 +2989,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .instance_size = sizeof(RISCVCPU),
         .instance_align = __alignof(RISCVCPU),
         .instance_init = riscv_cpu_init,
+        .instance_finalize = riscv_cpu_instance_finalize,
         .abstract = true,
         .class_size = sizeof(RISCVCPUClass),
         .class_init = riscv_cpu_common_class_init,
target/riscv/kvm/kvm-cpu.c +3 -4
index b047ffa9c0c..e0241870ada 100644
@@ -2025,7 +2025,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     if (cpu->cfg.ext_zicbom &&
-        riscv_cpu_option_set(kvm_cbom_blocksize.name)) {
+        riscv_cpu_option_set(cpu, kvm_cbom_blocksize.name)) {
 
         reg.id = KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CONFIG,
                                         kvm_cbom_blocksize.kvm_reg_id);
@@ -2044,7 +2044,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     if (cpu->cfg.ext_zicboz &&
-        riscv_cpu_option_set(kvm_cboz_blocksize.name)) {
+        riscv_cpu_option_set(cpu, kvm_cboz_blocksize.name)) {
 
         reg.id = KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_CONFIG,
                                         kvm_cboz_blocksize.kvm_reg_id);
@@ -2063,7 +2063,7 @@ void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
     }
 
     /* Users are setting vlen, not vlenb */
-    if (riscv_has_ext(env, RVV) && riscv_cpu_option_set("vlen")) {
+    if (riscv_has_ext(env, RVV) && riscv_cpu_option_set(cpu, "vlen")) {
         if (!kvm_v_vlenb.supported) {
             error_setg(errp, "Unable to set 'vlenb': register not supported");
             return;
 
--

2.54.0

#55 lore raw

PATCH v2 43/46 target/riscv: use hash table as set for user_options

In reply to Marc-André Lureau

The values stored in user_options are never retrieved, only key presence is checked. Use g_hash_table_add() instead of g_hash_table_insert() and drop the unused value parameter.

---
 target/riscv/cpu.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
target/riscv/cpu.c +15 -17
index be9a3ac13e7..52b89e274f4 100644
@@ -60,11 +60,9 @@ bool riscv_cpu_is_32bit(RISCVCPU *cpu)
     return riscv_cpu_mxl(&cpu->env) == MXL_RV32;
 }
 
-static void cpu_option_add_user_setting(RISCVCPU *cpu, const char *optname,
-                                        uint32_t value)
+static void cpu_option_add_user_setting(RISCVCPU *cpu, const char *optname)
 {
-    g_hash_table_insert(cpu->user_options, (gpointer)optname,
-                        GUINT_TO_POINTER(value));
+    g_hash_table_add(cpu->user_options, (gpointer)optname);
 }
 
 bool riscv_cpu_option_set(RISCVCPU *cpu, const char *optname)
@@ -1452,7 +1450,7 @@ static void prop_pmu_num_set(Object *obj, Visitor *v, const char *name,
 
     warn_report("\"pmu-num\" property is deprecated; use \"pmu-mask\"");
     cpu->cfg.pmu_mask = pmu_mask;
-    cpu_option_add_user_setting(cpu, "pmu-mask", pmu_mask);
+    cpu_option_add_user_setting(cpu, "pmu-mask");
 }
 
 static void prop_pmu_num_get(Object *obj, Visitor *v, const char *name,
@@ -1494,7 +1492,7 @@ static void prop_pmu_mask_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.pmu_mask = value;
 }
 
@@ -1526,7 +1524,7 @@ static void prop_mmu_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.mmu = value;
 }
 
@@ -1558,7 +1556,7 @@ static void prop_pmp_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.pmp = value;
 }
 
@@ -1598,7 +1596,7 @@ static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.pmp_regions = value;
 }
 
@@ -1636,7 +1634,7 @@ static void prop_pmp_granularity_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.pmp_granularity = value;
 }
 
@@ -1709,7 +1707,7 @@ static void prop_priv_spec_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, priv_version);
+    cpu_option_add_user_setting(cpu, name);
     cpu->env.priv_ver = priv_version;
 }
 
@@ -1743,7 +1741,7 @@ static void prop_vext_spec_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, VEXT_VERSION_1_00_0);
+    cpu_option_add_user_setting(cpu, name);
     cpu->env.vext_ver = VEXT_VERSION_1_00_0;
 }
 
@@ -1786,7 +1784,7 @@ static void prop_vlen_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.vlenb = value >> 3;
 }
 
@@ -1827,7 +1825,7 @@ static void prop_elen_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.elen = value;
 }
 
@@ -1863,7 +1861,7 @@ static void prop_cbom_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.cbom_blocksize = value;
 }
 
@@ -1899,7 +1897,7 @@ static void prop_cbop_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.cbop_blocksize = value;
 }
 
@@ -1935,7 +1933,7 @@ static void prop_cboz_blksize_set(Object *obj, Visitor *v, const char *name,
         return;
     }
 
-    cpu_option_add_user_setting(cpu, name, value);
+    cpu_option_add_user_setting(cpu, name);
     cpu->cfg.cboz_blocksize = value;
 }
 
Signature · 1 line
2.54.0
#56 lore raw

PATCH v2 44/46 hw/riscv/virt: free flash devices and OEM strings on finalization

In reply to Marc-André Lureau

Add instance_finalize to free the two pflash devices (when unrealized) and the OEM ID strings allocated during instance_init. Fixes leaks found by ASan.

Fixes 71eb522c4063 ("riscv/virt: Add the PFlash CFI01 device")
---
 hw/riscv/virt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/riscv/virt.c +14 -0
index a1c323e66df..315049bc86c 100644
@@ -1745,6 +1745,19 @@ static void virt_machine_init(MachineState *machine)
     qemu_add_machine_init_done_notifier(&s->machine_done);
 }
 
+static void virt_machine_instance_finalize(Object *obj)
+{
+    RISCVVirtState *s = RISCV_VIRT_MACHINE(obj);
+
+    for (int i = 0; i < ARRAY_SIZE(s->flash); i++) {
+        if (s->flash[i] && !qdev_is_realized(DEVICE(s->flash[i]))) {
+            object_unref(OBJECT(s->flash[i]));
+        }
+    }
+    g_free(s->oem_id);
+    g_free(s->oem_table_id);
+}
+
 static void virt_machine_instance_init(Object *obj)
 {
     RISCVVirtState *s = RISCV_VIRT_MACHINE(obj);
@@ -1984,6 +1997,7 @@ static const TypeInfo virt_machine_typeinfo = {
     .parent     = TYPE_MACHINE,
     .class_init = virt_machine_class_init,
     .instance_init = virt_machine_instance_init,
+    .instance_finalize = virt_machine_instance_finalize,
     .instance_size = sizeof(RISCVVirtState),
     .interfaces = (const InterfaceInfo[]) {
          { TYPE_HOTPLUG_HANDLER },
Signature · 1 line
2.54.0
#57 lore raw

PATCH v2 45/46 hw/i2c/pmbus: fix undefined behavior in pmbus_direct_mode2data

In reply to Marc-André Lureau

The intermediate result of (Y * 10^-R - b) / m can be negative when the bias (b) is large and the raw register value is small (e.g. zero on an uninitialized device). Assigning that negative double to uint32_t is undefined behavior, caught by UBSan/clang.

Use a double intermediate and clamp negative results to zero.

Fixes 3746d5c15e70 ("hw/i2c: add support for PMBus")
---
 hw/i2c/pmbus_device.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/i2c/pmbus_device.c +2 -2
index 853dc4b4342..861f5b4fb63 100644
@@ -23,8 +23,8 @@ uint16_t pmbus_data2direct_mode(PMBusCoefficients c, uint32_t value)
 uint32_t pmbus_direct_mode2data(PMBusCoefficients c, uint16_t value)
 {
     /* X = (Y * 10^-R - b) / m */
-    uint32_t x = (value / pow(10, c.R) - c.b) / c.m;
-    return x;
+    double x = (value / pow(10, c.R) - c.b) / c.m;
+    return (x > 0) ? (uint32_t)x : 0;
 }
 
 uint16_t pmbus_data2linear_mode(uint16_t value, int exp)
Signature · 1 line
2.54.0
#58 lore raw

PATCH v2 46/46 qtest: add "qom-tests" command

In reply to Marc-André Lureau

Add a new "qom-tests" to exercise basic object lifecycle. Instantiate all non-abstract objects, get and set properties and unref.

This should quickly find leaks and other related issues that are eventually triggerable at run-time with QMP qom commands.

---
 tests/qtest/libqtest.h |  8 ++++++++
 system/qtest.c         | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qtest/libqtest.c |  6 ++++++
 tests/qtest/qom-test.c | 12 ++++++++++++
 4 files changed, 72 insertions(+)
tests/qtest/libqtest.h +8 -0
index 9c118c89ca0..02d65ecb65d 100644
@@ -426,6 +426,14 @@ char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
 
 void qtest_module_load(QTestState *s, const char *prefix, const char *libname);
 
+/**
+ * qtest_qom_tests:
+ * @s: #QTestState instance to operate on.
+ *
+ * Run QOM property get/set round-trip tests on all non-abstract types.
+ */
+void qtest_qom_tests(QTestState *s);
+
 /**
  * qtest_get_irq:
  * @s: #QTestState instance to operate on.
system/qtest.c +46 -0

58 lines not on this page · raw message

tests/qtest/libqtest.c +6 -0
index 116a8a32583..1d77d003112 100644
@@ -1138,6 +1138,12 @@ void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
     qtest_rsp(s);
 }
 
+void qtest_qom_tests(QTestState *s)
+{
+    qtest_sendf(s, "qom-tests\n");
+    qtest_rsp(s);
+}
+
 static int64_t qtest_clock_rsp(QTestState *s)
 {
     gchar **words;
tests/qtest/qom-test.c +12 -0
index 6421f2d9d9f..cf4c6b5add5 100644
@@ -227,6 +227,17 @@ static void add_machine_test_case(const char *mname)
     g_free(path);
 }
 
+static void test_qom_qtests(void)
+{
+    QTestState *qts;
+
+    qts = qtest_initf("-machine none");
+
+    qtest_qom_tests(qts);
+
+    qtest_quit(qts);
+}
+
 int main(int argc, char **argv)
 {
     char *v_env = getenv("V");
@@ -238,6 +249,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     qtest_cb_for_every_machine(add_machine_test_case, g_test_quick());
+    qtest_add_func("qom/qom-qtests", test_qom_qtests);
 
     return g_test_run();
 }
Signature · 1 line
2.54.0