]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
rust/vmstate: Fix type check for varray in vmstate_struct
authorZhao Liu <zhao1.liu@intel.com>
Tue, 18 Mar 2025 13:02:09 +0000 (21:02 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 21 Mar 2025 11:56:00 +0000 (12:56 +0100)
When pass a varray to vmstate_struct, the `type` parameter should be the
type of the element in the varray, for example:

vmstate_struct!(HPETState, timers, [0 .. num_timers], VMSTATE_HPET_TIMER,
BqlRefCell<HPETTimer>).with_version_id(0)

But this breaks current type check, because it checks the type of
`field`, which is an array type (for the above example, type of timers
is [BqlRefCell<HPETTimer>; 32], not BqlRefCell<HPETTimer>).

But the current assert_field_type() can no longer be extended to include
new arguments, so a variant of it (a second macro containing the
`num = $num:ident` parameter) had to be added to handle array cases.

In this new macro, it not only checks the type of element, but also
checks whether the `num` (number of elements in varray) is out of range.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250318130219.1799170-6-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
rust/qemu-api/src/assertions.rs
rust/qemu-api/src/vmstate.rs

index bba38cfda11e24c9425d1358bb9e29ee4dfe8d09..eb12e9499a720c1081a1688864dc7ec142eaa9e8 100644 (file)
@@ -91,6 +91,21 @@ macro_rules! assert_field_type {
             }
         };
     };
+
+    ($t:ty, $i:tt, $ti:ty, num = $num:ident) => {
+        const _: () = {
+            #[allow(unused)]
+            fn assert_field_type(v: $t) {
+                fn types_must_be_equal<T, U>(_: T)
+                where
+                    T: $crate::assertions::EqType<Itself = U>,
+                {
+                }
+                let index: usize = v.$num.try_into().unwrap();
+                types_must_be_equal::<_, &$ti>(&v.$i[index]);
+            }
+        };
+    };
 }
 
 /// Assert that an expression matches a pattern.  This can also be
index e2a1f7a97aaef785173a5e6c674153a3e60bc75c..9d9cdda993ce506e086f4a547a2878124f04f546 100644 (file)
@@ -447,7 +447,7 @@ macro_rules! vmstate_struct {
                 .as_ptr() as *const ::std::os::raw::c_char,
             $(num_offset: $crate::offset_of!($struct_name, $num),)?
             offset: {
-                $crate::assert_field_type!($struct_name, $field_name, $type);
+                $crate::assert_field_type!($struct_name, $field_name, $type $(, num = $num)?);
                 $crate::offset_of!($struct_name, $field_name)
             },
             size: ::core::mem::size_of::<$type>(),