Skip to contentSkip to conversation

vmstate: validate VMStateDescription::fields upon registration

PATCH Superseded Roman Kiryanov 2 participants 3 messages Mar 12 – Mar 13

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vmstate: validate VMStateDescription::fields upon registration

The vmstate_save_state_v() function does not support NULL in VMStateDescription::fields and will crash if one is provided.

This change prevents this situation from happening by explicitly crashing earlier.

---
v2:
- Moved the assert from vmstate_save_state_v to the registration path (vmstate_register_with_alias_id) and the qtest validation path (vmstate_check) to catch missing fields earlier.

---
 migration/savevm.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/savevm.c +5 -0
index 197c89e0e6..20c2b99231 100644
@@ -861,6 +861,8 @@ static void vmstate_check(const VMStateDescription *vmsd)
     const VMStateField *field = vmsd->fields;
     const VMStateDescription * const *subsection = vmsd->subsections;
 
+    assert(field || vmsd->unmigratable);
+
     if (field) {
         while (field->name) {
             if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
@@ -900,6 +902,9 @@ int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
     /* If this triggers, alias support can be dropped for the vmsd. */
     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
 
+    /* If vmsd is migratable it MUST have valid fields. */
+    assert(vmsd->fields || vmsd->unmigratable);
+
     se = g_new0(SaveStateEntry, 1);
     se->version_id = vmsd->version_id;
     se->section_id = savevm_state.global_section_id++;
Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Roman Kiryanov

The vmstate_save_state_v() function does not support NULL in VMStateDescription::fields and will crash if one is provided.

This change prevents this situation from happening by explicitly crashing earlier.

Thanks for the generosity, all kudos to Fabiano. Let's replace this line with:

Quoted 36 lines

---
v2:
- Moved the assert from vmstate_save_state_v to the registration path (vmstate_register_with_alias_id) and the qtest validation path (vmstate_check) to catch missing fields earlier.

---
 migration/savevm.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/savevm.c +5 -0
index 197c89e0e6..20c2b99231 100644
@@ -861,6 +861,8 @@ static void vmstate_check(const VMStateDescription *vmsd)
     const VMStateField *field = vmsd->fields;
     const VMStateDescription * const *subsection = vmsd->subsections;
 
+    assert(field || vmsd->unmigratable);
+
     if (field) {
         while (field->name) {
             if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
@@ -900,6 +902,9 @@ int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
     /* If this triggers, alias support can be dropped for the vmsd. */
     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
 
+    /* If vmsd is migratable it MUST have valid fields. */
+    assert(vmsd->fields || vmsd->unmigratable);
+
     se = g_new0(SaveStateEntry, 1);
     se->version_id = vmsd->version_id;
     se->section_id = savevm_state.global_section_id++;
Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
Signature · 1 line
Peter Xu
#2 lore raw
Quoted 5 lines · Peter Xu

Thanks for the generosity, all kudos to Fabiano. Let's replace this line with:

Sure, I will update the patch. I also noticed VirtioDeviceClass is not covered by v2, so I added another assert into virtio_device_realize. Now virtio-snd hits the assert if I remove unmigratable = 1.