Skip to contentSkip to conversation

migration: Add canary to VMSTATE_END_OF_LIST

PATCH In review Dr. David Alan Gilbert (git) 3 participants 7 messages Jan 12, 2022 – Jan 13, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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/vmstate.c         | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)
include/migration/vmstate.h +6 -1
index 017c03675c..b50708e57a 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 {
@@ -1163,7 +1166,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/vmstate.c +2 -0
index 05f87cdddc..181ba08c7d 100644
@@ -160,6 +160,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;
@@ -413,6 +414,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.34.1
#1 lore raw
Quoted 9 lines · Dr. David Alan Gilbert (git)

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.

Does 'make check' definitely do the traversal for all vmstate structs, or do we need to add a "sanity check them all on startup" bit of test code ?

thanks
-- PMM

#2 lore raw
Quoted 9 lines · * Peter Maydell ([email protected]) wrote:
Quoted 7 lines · Dr. David Alan Gilbert (git)

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.

Quoted 6 lines
Quoted 2 lines

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

Does 'make check' definitely do the traversal for all vmstate structs, or do we need to add a "sanity check them all on startup" bit of test code ?

Oh I doubt it does; some vmsd's are conditional on guest state, many are only on particular machine types.

I think the closest we have to being able to walk the tree, is --dump-vmstate - although you need to call that for each machine type. (I forgot to add the canary check in the dump-vmstate code, I'll fix that).

Dave

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
#3 lore raw
Quoted 6 lines · Dr. David Alan Gilbert
Quoted 3 lines · * Peter Maydell ([email protected]) wrote:

Does 'make check' definitely do the traversal for all vmstate structs, or do we need to add a "sanity check them all on startup" bit of test code ?

Quoted 7 lines

Oh I doubt it does; some vmsd's are conditional on guest state, many are only on particular machine types.

I think the closest we have to being able to walk the tree, is --dump-vmstate - although you need to call that for each machine type. (I forgot to add the canary check in the dump-vmstate code, I'll fix that).

We could have vmstate_register_with_alias_id() iterate through and assert presence of the right terminator (probably only if qtest enabled, or some other suitable condition). Then the existing tests that do the basic "check we can instantiate every device and initialize every board model" would run that code and catch most missing terminator cases, I think.

-- PMM

#4 lore raw
Quoted 6 lines · Peter Maydell

We could have vmstate_register_with_alias_id() iterate through and assert presence of the right terminator (probably only if qtest enabled, or some other suitable condition). Then the existing tests that do the basic "check we can instantiate every device and initialize every board model" would run that code and catch most missing terminator cases, I think.

Agreed. How about assert it even without qtest? We do tons of assertion for programming errors anyway in QEMU.

Thanks,

Signature · 1 line
Peter Xu
#5 lore raw
Quoted 11 lines · Peter Xu
Quoted 6 lines · Peter Maydell

We could have vmstate_register_with_alias_id() iterate through and assert presence of the right terminator (probably only if qtest enabled, or some other suitable condition). Then the existing tests that do the basic "check we can instantiate every device and initialize every board model" would run that code and catch most missing terminator cases, I think.

Agreed. How about assert it even without qtest? We do tons of assertion for programming errors anyway in QEMU.

I don't inherently object, but in this case to do the assertion we'd need to do a scan over the fields arrays which we wouldn't otherwise need to, so the cost of the assert is not simply the compare-and-branch but also the loop over the array. If that's not significant in terms of start-up time costs we can just go ahead and do it (which would be nicer for debugging and making it really obvious to people writing new devices) but my remark above was a gesture towards "maybe we need to not do it for normal startup"..

-- PMM

#6 lore raw
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Peter Xu
Quoted 6 lines · Peter Maydell

We could have vmstate_register_with_alias_id() iterate through and assert presence of the right terminator (probably only if qtest enabled, or some other suitable condition). Then the existing tests that do the basic "check we can instantiate every device and initialize every board model" would run that code and catch most missing terminator cases, I think.

Agreed. How about assert it even without qtest? We do tons of assertion for programming errors anyway in QEMU.

Quoted 9 lines

I don't inherently object, but in this case to do the assertion we'd need to do a scan over the fields arrays which we wouldn't otherwise need to, so the cost of the assert is not simply the compare-and-branch but also the loop over the array. If that's not significant in terms of start-up time costs we can just go ahead and do it (which would be nicer for debugging and making it really obvious to people writing new devices) but my remark above was a gesture towards "maybe we need to not do it for normal startup"..

Hmm.. Then how about put it into a "#ifdef CONFIG_DEBUG"?

We may need some extra lines in configure, though:

if test "$debug" = "yes"; then
echo "CONFIG_DEBUG=y" >> $config_host_mak
fi

PS: I'm a bit surprised we don't have CONFIG_DEBUG already..

Thanks,

Signature · 1 line
Peter Xu