qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Put Canary End of List in VMSTATE
@ 2022-11-28 13:08 Juan Quintela
  2022-11-28 13:08 ` [PATCH 1/2] migration: Add canary to VMSTATE_END_OF_LIST Juan Quintela
  2022-11-28 13:08 ` [PATCH 2/2] migration: Perform vmsd structure check during tests Juan Quintela
  0 siblings, 2 replies; 4+ messages in thread
From: Juan Quintela @ 2022-11-28 13:08 UTC (permalink / raw)
  To: qemu-devel; +Cc: Juan Quintela, Dr. David Alan Gilbert

Hi

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(-)

-- 
2.38.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] migration: Add canary to VMSTATE_END_OF_LIST
  2022-11-28 13:08 [PATCH 0/2] Put Canary End of List in VMSTATE Juan Quintela
@ 2022-11-28 13:08 ` Juan Quintela
  2022-11-28 13:08 ` [PATCH 2/2] migration: Perform vmsd structure check during tests Juan Quintela
  1 sibling, 0 replies; 4+ messages in thread
From: Juan Quintela @ 2022-11-28 13:08 UTC (permalink / raw)
  To: qemu-devel
  Cc: Juan Quintela, Dr. David Alan Gilbert, Peter Maydell,
	Philippe Mathieu-Daudé

From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

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.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 include/migration/vmstate.h | 7 ++++++-
 migration/savevm.c          | 1 +
 migration/vmstate.c         | 2 ++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h
index ad24aa1934..527ead2831 100644
--- a/include/migration/vmstate.h
+++ b/include/migration/vmstate.h
@@ -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);
diff --git a/migration/savevm.c b/migration/savevm.c
index a0cdb714f7..3ad81359ab 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -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) {
diff --git a/migration/vmstate.c b/migration/vmstate.c
index 924494bda3..83ca4c7d3e 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -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);
-- 
2.38.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] migration: Perform vmsd structure check during tests
  2022-11-28 13:08 [PATCH 0/2] Put Canary End of List in VMSTATE Juan Quintela
  2022-11-28 13:08 ` [PATCH 1/2] migration: Add canary to VMSTATE_END_OF_LIST Juan Quintela
@ 2022-11-28 13:08 ` Juan Quintela
  2022-11-28 13:38   ` Peter Maydell
  1 sibling, 1 reply; 4+ messages in thread
From: Juan Quintela @ 2022-11-28 13:08 UTC (permalink / raw)
  To: qemu-devel; +Cc: Juan Quintela, Dr. David Alan Gilbert, Peter Maydell

From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>

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

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 migration/savevm.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/migration/savevm.c b/migration/savevm.c
index 3ad81359ab..653087f0bb 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -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);
+            exit(-1);
+        }
+    }
+
+    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;
-- 
2.38.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] migration: Perform vmsd structure check during tests
  2022-11-28 13:08 ` [PATCH 2/2] migration: Perform vmsd structure check during tests Juan Quintela
@ 2022-11-28 13:38   ` Peter Maydell
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2022-11-28 13:38 UTC (permalink / raw)
  To: Juan Quintela; +Cc: qemu-devel, Dr. David Alan Gilbert

On Mon, 28 Nov 2022 at 13:09, Juan Quintela <quintela@redhat.com> wrote:
>
> From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
>
> Perform a check on vmsd structures during test runs in the hope
> of catching any missing terminators and other simple screwups.
>
> Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
> Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
> Reviewed-by: Juan Quintela <quintela@redhat.com>
> Signed-off-by: Juan Quintela <quintela@redhat.com>

> +/*
> + * 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);

This is always a bug, right, so what prevents us from assert()ing it?

thanks
-- PMM


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-28 13:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-28 13:08 [PATCH 0/2] Put Canary End of List in VMSTATE Juan Quintela
2022-11-28 13:08 ` [PATCH 1/2] migration: Add canary to VMSTATE_END_OF_LIST Juan Quintela
2022-11-28 13:08 ` [PATCH 2/2] migration: Perform vmsd structure check during tests Juan Quintela
2022-11-28 13:38   ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).