Skip to contentSkip to conversation

add VMSTATE_BOOL

PATCH Applied Gerd Hoffmann 5 participants 11 messages Nov 1, 2010 – Nov 9, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add VMSTATE_BOOL

---
 hw/hw.h  |   14 ++++++++++++++
 savevm.c |   21 +++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)
hw/hw.h +14 -0
index e935364..234c713 100644
@@ -333,6 +333,8 @@ struct VMStateDescription {
     const VMStateSubsection *subsections;
 };
 
+extern const VMStateInfo vmstate_info_bool;
+
 extern const VMStateInfo vmstate_info_int8;
 extern const VMStateInfo vmstate_info_int16;
 extern const VMStateInfo vmstate_info_int32;
@@ -613,6 +615,9 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
     VMSTATE_STRUCT_POINTER_TEST(_field, _state, NULL, _vmsd, _type)
 
+#define VMSTATE_BOOL_V(_f, _s, _v)                                    \
+    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
+
 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
@@ -631,6 +636,9 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
 
+#define VMSTATE_BOOL(_f, _s)                                          \
+    VMSTATE_BOOL_V(_f, _s, 0)
+
 #define VMSTATE_INT8(_f, _s)                                          \
     VMSTATE_INT8_V(_f, _s, 0)
 #define VMSTATE_INT16(_f, _s)                                         \
@@ -685,6 +693,12 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_PTIMER(_f, _s)                                        \
     VMSTATE_PTIMER_V(_f, _s, 0)
 
+#define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
+    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
+
+#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
+    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
+
 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
 
savevm.c +21 -0
index 10057f3..14268ea 100644
@@ -675,6 +675,27 @@ uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
+/* bool */
+
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);
+}
+
+const VMStateInfo vmstate_info_bool = {
+    .name = "bool",
+    .get  = get_bool,
+    .put  = put_bool,
+};
+
 /* 8 bit int */
 
 static int get_int8(QEMUFile *f, void *pv, size_t size)
Signature · 1 line
1.7.1
#2 lore raw
In reply to Gerd Hoffmann
Quoted 41 lines · Gerd Hoffmann
---
 hw/hw.h  |   14 ++++++++++++++
 savevm.c |   21 +++++++++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)
hw/hw.h +8 -0
index e935364..234c713 100644
@@ -333,6 +333,8 @@ struct VMStateDescription {
     const VMStateSubsection *subsections;
 };
 
+extern const VMStateInfo vmstate_info_bool;
+
 extern const VMStateInfo vmstate_info_int8;
 extern const VMStateInfo vmstate_info_int16;
 extern const VMStateInfo vmstate_info_int32;
@@ -613,6 +615,9 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_STRUCT_POINTER(_field, _state, _vmsd, _type)          \
     VMSTATE_STRUCT_POINTER_TEST(_field, _state, NULL, _vmsd, _type)
 
+#define VMSTATE_BOOL_V(_f, _s, _v)                                    \
+    VMSTATE_SINGLE(_f, _s, _v, vmstate_info_bool, bool)
+
 #define VMSTATE_INT8_V(_f, _s, _v)                                    \
     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_int8, int8_t)
 #define VMSTATE_INT16_V(_f, _s, _v)                                   \
@@ -631,6 +636,9 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_UINT64_V(_f, _s, _v)                                  \
     VMSTATE_SINGLE(_f, _s, _v, vmstate_info_uint64, uint64_t)
 
+#define VMSTATE_BOOL(_f, _s)                                          \
+    VMSTATE_BOOL_V(_f, _s, 0)
+
 #define VMSTATE_INT8(_f, _s)                                          \
     VMSTATE_INT8_V(_f, _s, 0)
 #define VMSTATE_INT16(_f, _s)                                         \

hw/hw.h extern const VMStateDescription vmstate_i2c_slave; · line 701

Quoted 9 lines
hw/hw.h +6 -0
@@ -685,6 +693,12 @@ extern const VMStateDescription vmstate_i2c_slave;
 #define VMSTATE_PTIMER(_f, _s)                                        \
     VMSTATE_PTIMER_V(_f, _s, 0)
 
+#define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
+    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
+
+#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
+    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
+

Why don't we pack the bits?

Quoted 8 lines
hw/hw.h +0 -0
 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
 
savevm.c +0 -0
index 10057f3..14268ea 100644

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 684

Quoted 10 lines
savevm.c +7 -0
@@ -675,6 +675,27 @@ uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
+/* bool */
+
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

We must really validate that the value is 0 or 1.
If it's not, we will get undefined behaviour.

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 690

Quoted 6 lines
savevm.c +6 -0
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);

Is there a guarantee that bool is a single byte, BTW?

Quoted 14 lines
savevm.c +8 -0
+}
+
+const VMStateInfo vmstate_info_bool = {
+    .name = "bool",
+    .get  = get_bool,
+    .put  = put_bool,
+};
+
 /* 8 bit int */
 
 static int get_int8(QEMUFile *f, void *pv, size_t size)
Signature · 1 line
1.7.1
#3 lore raw
Quoted 1 line · Michael S. Tsirkin

On Mon, Nov 01, 2010 at 03:51:54PM +0100, Gerd Hoffmann wrote:

[...]

Quoted 8 lines
Quoted 8 lines
savevm.c +0 -0
index 10057f3..14268ea 100644
@@ -675,6 +675,27 @@ uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
Quoted 10 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 684

Quoted 7 lines
savevm.c +7 -0
+/* bool */
+
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

We must really validate that the value is 0 or 1.
If it's not, we will get undefined behaviour.

Indeed.

Quoted 8 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 690

Quoted 6 lines
savevm.c +6 -0
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);

Is there a guarantee that bool is a single byte, BTW?

Nope. Does it matter?

[...]

#4 lore raw
Quoted 10 lines · Markus Armbruster
Quoted 8 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 690

Quoted 6 lines
savevm.c +6 -0
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);

Is there a guarantee that bool is a single byte, BTW?

Nope. Does it matter?

No. In fact I think QEMU makes (or tries to make) the QEMU dump format host-independent, and hence ABI-independent. So, writing a single byte is better than writing sizeof(bool) bytes.

Paolo

#5 lore raw
In reply to Michael S. Tsirkin

Hi,

Quoted 5 lines

hw/hw.h extern const VMStateDescription vmstate_i2c_slave; · line 701

Quoted 3 lines
hw/hw.h +3 -0
+#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
+    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
+

Why don't we pack the bits?

Point being? As long as we don't save *big* arrays of bools it simply isn't worth the effort IMHO. And for big arrays we'll probably wouldn't use bool in the first place ...

Quoted 10 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 684

Quoted 7 lines
savevm.c +7 -0
+/* bool */
+
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

We must really validate that the value is 0 or 1.
If it's not, we will get undefined behaviour.

I disagree.

You indeed have a bug in case your bool ends up with a value being neither 0 nor 1. That is completely independant from savevm/loadvm though, it can trip you up even in case you don't save/load the VM at all.

Quoted 8 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 690

Quoted 6 lines
savevm.c +6 -0
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);

Is there a guarantee that bool is a single byte, BTW?

No. bool must be 0 or 1 though, and a single byte is big enough to keep that information.

cheers,
Gerd

#6 lore raw
Quoted 23 lines · Gerd Hoffmann

Hi,

Quoted 5 lines

hw/hw.h extern const VMStateDescription vmstate_i2c_slave; · line 701

Quoted 3 lines
hw/hw.h +3 -0
+#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
+    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
+

Why don't we pack the bits?

Point being? As long as we don't save *big* arrays of bools it simply isn't worth the effort IMHO. And for big arrays we'll probably wouldn't use bool in the first place ...

Quoted 10 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 684

Quoted 7 lines
savevm.c +7 -0
+/* bool */
+
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

We must really validate that the value is 0 or 1.
If it's not, we will get undefined behaviour.

Quoted 6 lines

I disagree.

You indeed have a bug in case your bool ends up with a value being neither 0 nor 1. That is completely independant from savevm/loadvm though, it can trip you up even in case you don't save/load the VM at all.

I was wrong about undefined behaviour. Sorry. What this implementation does is treat byte value '\0' as boolean false, any other value as true.

I think we should verify that value is 0 or 1 and fail migration otherwise, to make it more robust.

Quoted 9 lines
Quoted 8 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 690

Quoted 6 lines
savevm.c +6 -0
+}
+
+static void put_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    qemu_put_byte(f, *v);

Is there a guarantee that bool is a single byte, BTW?

Quoted 5 lines

No. bool must be 0 or 1 though, and a single byte is big enough to keep that information.

cheers,
Gerd

Right.

#7 lore raw

Hi,

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

savevm.c uint64_t qemu_get_be64(QEMUFile *f) · line 684

Quoted 5 lines
savevm.c +5 -0
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

I think we should verify that value is 0 or 1 and fail migration otherwise, to make it more robust.

I still think such a check doesn't belong into the migration code as such a bug would exist without migration too. And if anything we should check on save not on load, otherwise qemu can write out savevm images which it will refuse to load. I wouldn't call this "robust".

cheers,
Gerd

#8 lore raw
Quoted 11 lines · Gerd Hoffmann

Hi,

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

I think we should verify that value is 0 or 1 and fail migration otherwise, to make it more robust.

Quoted 8 lines

I still think such a check doesn't belong into the migration code as such a bug would exist without migration too. And if anything we should check on save not on load, otherwise qemu can write out savevm images which it will refuse to load. I wouldn't call this "robust".

cheers,
Gerd

I think we should verify on load: e.g. the image could have got corrupted. What, exactly, do you want to check on save?

---

savevm: validate bool values on load

We always save 0 or 1 values for booleans. Validate on input to increase the chance of detecting input corruption.

---

savevm.c +6 -1
index 4e49765..da2fdfa 100644
@@ -680,7 +680,12 @@ uint64_t qemu_get_be64(QEMUFile *f)
 static int get_bool(QEMUFile *f, void *pv, size_t size)
 {
     bool *v = pv;
-    *v = qemu_get_byte(f);
+    uint8_t b;
+    b = qemu_get_byte(f);
+    if (b != (uint8_t)true && b != (uint8_t)false) {
+        return -EINVAL;
+    }
+    *v = b;
     return 0;
 }
 
author NAK
#9 lore raw
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Gerd Hoffmann
   Hi,
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

I think we should verify that value is 0 or 1 and fail migration otherwise, to make it more robust.

I still think such a check doesn't belong into the migration code as such a bug would exist without migration too. And if anything we should check on save not on load, otherwise qemu can write out savevm images which it will refuse to load. I wouldn't call this "robust".

Quoted 5 lines
Quoted 2 lines

cheers,
Gerd

I think we should verify on load: e.g. the image could have got corrupted.

For catching corruption checksums work much better.

Quoted 1 line

What, exactly, do you want to check on save?

I don't want to check anything.

I'm just saying that *if* we are sanity-checking bool to catch bugs it is much more useful to do that when saving.

Quoted 4 lines

savevm: validate bool values on load

We always save 0 or 1 values for booleans. Validate on input to increase the chance of detecting input corruption.

NACK.

cheers,
Gerd

#10 lore raw
Quoted 19 lines · Gerd Hoffmann
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Gerd Hoffmann

Hi,

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_bool(QEMUFile *f, void *pv, size_t size)
+{
+    bool *v = pv;
+    *v = qemu_get_byte(f);
+    return 0;

I think we should verify that value is 0 or 1 and fail migration otherwise, to make it more robust.

I still think such a check doesn't belong into the migration code as such a bug would exist without migration too. And if anything we should check on save not on load, otherwise qemu can write out savevm images which it will refuse to load. I wouldn't call this "robust".

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

cheers,
Gerd

I think we should verify on load: e.g. the image could have got corrupted.

For catching corruption checksums work much better.

Unless there's a bug in software that writes the file, then checksum will match.

Quoted 3 lines
Quoted 1 line

What, exactly, do you want to check on save?

I don't want to check anything.

Why did you suggest it above then?

Quoted 1 line

I'm just saying that *if* we are sanity-checking bool

My patch doesn't check bool. Look at it. I am sanity checking a byte read from file. File can have any values, there is no guarantee that it has the same value that the same version of qemu wrote out.

Quoted 2 lines

to catch bugs
it is much more useful to do that when saving.

There's nothing we *can* check.

	if (v == true || v == false)

is always true according to the language standard. How is it useful to stick always true conditions that compiler will likely remove in code?

Quoted 9 lines
Quoted 4 lines

savevm: validate bool values on load

We always save 0 or 1 values for booleans. Validate on input to increase the chance of detecting input corruption.

NACK.

cheers,
Gerd