Skip to contentSkip to conversation

Put Canary End of List in VMSTATE

PATCH New Juan Quintela 1 participant 3 messages Nov 29, 2022

lore
Share & tools
lore
#0 lore raw

v2 cover Put Canary End of List in VMSTATE

Hi

Changes from yesterday submission:
- change exit by g_assert_not_reached()

[Previous submission]
This are a patch series from David from January. The changes that I did:
- rebases
- change assert() in vmstate_check() to and if and printf.

Please, Review.

Dr. David Alan Gilbert (2):
migration: Add canary to VMSTATE_END_OF_LIST
migration: Perform vmsd structure check during tests

 include/migration/vmstate.h |  7 +++++-
 migration/savevm.c          | 43 +++++++++++++++++++++++++++++++++++++
 migration/vmstate.c         |  2 ++
 3 files changed, 51 insertions(+), 1 deletion(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH v2 1/2 migration: Add canary to VMSTATE_END_OF_LIST

From: "Dr. David Alan Gilbert" <[email protected]>

We fairly regularly forget VMSTATE_END_OF_LIST markers off descriptions; given that the current check is only for ->name being NULL, sometimes we get unlucky and the code apparently works and no one spots the error.

Explicitly add a flag, VMS_END that should be set, and assert it is set during the traversal.

Note: This can't go in until we update the copy of vmstate.h in slirp.

---
 include/migration/vmstate.h | 7 ++++++-
 migration/savevm.c          | 1 +
 migration/vmstate.c         | 2 ++
 3 files changed, 9 insertions(+), 1 deletion(-)
include/migration/vmstate.h +6 -1
index ad24aa1934..527ead2831 100644
@@ -147,6 +147,9 @@ enum VMStateFlags {
      * VMStateField.struct_version_id to tell which version of the
      * structure we are referencing to use. */
     VMS_VSTRUCT           = 0x8000,
+
+    /* Marker for end of list */
+    VMS_END = 0x10000
 };
 
 typedef enum {
@@ -1161,7 +1164,9 @@ extern const VMStateInfo vmstate_info_qlist;
     VMSTATE_UNUSED_BUFFER(_test, 0, _size)
 
 #define VMSTATE_END_OF_LIST()                                         \
-    {}
+    {                     \
+        .flags = VMS_END, \
+    }
 
 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                        void *opaque, int version_id);
migration/savevm.c +1 -0
index a0cdb714f7..3ad81359ab 100644
@@ -585,6 +585,7 @@ static void dump_vmstate_vmsd(FILE *out_file,
             field++;
             first = false;
         }
+        assert(field->flags == VMS_END);
         fprintf(out_file, "\n%*s]", indent, "");
     }
     if (vmsd->subsections != NULL) {
migration/vmstate.c +2 -0
index 924494bda3..83ca4c7d3e 100644
@@ -154,6 +154,7 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
         }
         field++;
     }
+    assert(field->flags == VMS_END);
     ret = vmstate_subsection_load(f, vmsd, opaque);
     if (ret != 0) {
         return ret;
@@ -408,6 +409,7 @@ int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
         }
         field++;
     }
+    assert(field->flags == VMS_END);
 
     if (vmdesc) {
         json_writer_end_array(vmdesc);
Signature · 1 line
2.38.1
#2 lore raw

PATCH v2 2/2 migration: Perform vmsd structure check during tests

In reply to Juan Quintela

From: "Dr. David Alan Gilbert" <[email protected]>

Perform a check on vmsd structures during test runs in the hope of catching any missing terminators and other simple screwups.

---
 migration/savevm.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
migration/savevm.c +42 -0
index 3ad81359ab..e6c3fc6614 100644
@@ -66,6 +66,7 @@
 #include "net/announce.h"
 #include "qemu/yank.h"
 #include "yank_functions.h"
+#include "sysemu/qtest.h"
 
 const unsigned int postcopy_ram_discard_version;
 
@@ -804,6 +805,42 @@ void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
     }
 }
 
+/*
+ * Perform some basic checks on vmsd's at registration
+ * time.
+ */
+static void vmstate_check(const VMStateDescription *vmsd)
+{
+    const VMStateField *field = vmsd->fields;
+    const VMStateDescription **subsection = vmsd->subsections;
+
+    if (field) {
+        while (field->name) {
+            if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
+                /* Recurse to sub structures */
+                vmstate_check(field->vmsd);
+            }
+            /* Carry on */
+            field++;
+        }
+        /* Check for the end of field list canary */
+        if (field->flags != VMS_END) {
+            error_report("VMSTATE not ending with VMS_END: %s", vmsd->name);
+            g_assert_not_reached();
+        }
+    }
+
+    while (subsection && *subsection) {
+        /*
+         * The name of a subsection should start with the name of the
+         * current object.
+         */
+        assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name)));
+        vmstate_check(*subsection);
+        subsection++;
+    }
+}
+
 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
                                    const VMStateDescription *vmsd,
                                    void *opaque, int alias_id,
@@ -849,6 +886,11 @@ int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
     } else {
         se->instance_id = instance_id;
     }
+
+    /* Perform a recursive sanity check during the test runs */
+    if (qtest_enabled()) {
+        vmstate_check(vmsd);
+    }
     assert(!se->compat || se->instance_id == 0);
     savevm_state_handler_insert(se);
     return 0;
Signature · 1 line
2.38.1