qom: fix objects with improper parent type

Some objects accidentally inherit ObjectClass instead of Object.
They compile silently but may crash after downcasting.

In this patch, we introduce a coccinelle script to find broken
declarations and fix them manually with proper base type.

Signed-off-by: Sergey Nizovtsev <snizovtsev@gmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
stable-6.0
Paolo Bonzini 2020-10-07 12:23:56 -04:00
parent d9f24bf572
commit 22fb6eb571
5 changed files with 30 additions and 3 deletions

View File

@ -2462,6 +2462,7 @@ F: include/monitor/qdev.h
F: include/qom/
F: qapi/qom.json
F: qapi/qdev.json
F: scripts/coccinelle/qom-parent-type.cocci
F: softmmu/qdev-monitor.c
F: qom/
F: tests/check-qom-interface.c

View File

@ -19,7 +19,7 @@
OBJECT_DECLARE_SIMPLE_TYPE(VmGenIdState, VMGENID)
struct VmGenIdState {
DeviceClass parent_obj;
DeviceState parent_obj;
QemuUUID guid; /* The 128-bit GUID seen by the guest */
uint8_t vmgenid_addr_le[8]; /* Address of the GUID (little-endian) */
};

View File

@ -24,7 +24,7 @@ DECLARE_INSTANCE_CHECKER(VMCoreInfoState, VMCOREINFO,
typedef struct fw_cfg_vmcoreinfo FWCfgVMCoreInfo;
struct VMCoreInfoState {
DeviceClass parent_obj;
DeviceState parent_obj;
bool has_vmcoreinfo;
FWCfgVMCoreInfo vmcoreinfo;

View File

@ -35,7 +35,7 @@
OBJECT_DECLARE_TYPE(CanHostState, CanHostClass, CAN_HOST)
struct CanHostState {
ObjectClass oc;
Object oc;
CanBusState *bus;
CanBusClientState bus_client;

View File

@ -0,0 +1,26 @@
// Highlight object declarations that don't look like object class but
// accidentally inherit from it.
@match@
identifier obj_t, fld;
type parent_t =~ ".*Class$";
@@
struct obj_t {
parent_t fld;
...
};
@script:python filter depends on match@
obj_t << match.obj_t;
@@
is_class_obj = obj_t.endswith('Class')
cocci.include_match(not is_class_obj)
@replacement depends on filter@
identifier match.obj_t, match.fld;
type match.parent_t;
@@
struct obj_t {
* parent_t fld;
...
};