[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 07/17] rust/vmstate: Fix type check for varray in vmstate_struct
From: |
Zhao Liu |
Subject: |
[PATCH 07/17] rust/vmstate: Fix type check for varray in vmstate_struct |
Date: |
Mon, 17 Mar 2025 23:12:26 +0800 |
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>, version = 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>
---
rust/qemu-api/src/assertions.rs | 15 +++++++++++++++
rust/qemu-api/src/vmstate.rs | 2 +-
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs
index 104dec39774e..176060e32acd 100644
--- a/rust/qemu-api/src/assertions.rs
+++ b/rust/qemu-api/src/assertions.rs
@@ -91,6 +91,21 @@ fn types_must_be_equal<T, U>(_: T)
}
};
};
+
+ ($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
diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
index 5af75b4777e9..8adef175634a 100644
--- a/rust/qemu-api/src/vmstate.rs
+++ b/rust/qemu-api/src/vmstate.rs
@@ -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>(),
--
2.34.1
- [PATCH 00/17] rust/vmstate: Clean up, fix, enhance & test, Zhao Liu, 2025/03/17
- [PATCH 01/17] rust/vmstate: Remove unnecessary unsafe, Zhao Liu, 2025/03/17
- [PATCH 02/17] rust/vmstate: Fix num_offset in vmstate macros, Zhao Liu, 2025/03/17
- [PATCH 03/17] rust/vmstate: Add a prefix separator ", " for the array field in vmstate macros, Zhao Liu, 2025/03/17
- [PATCH 05/17] rust/vmstate: Fix num field when varray flags are set, Zhao Liu, 2025/03/17
- [PATCH 06/17] rust/vmstate: Fix size field of VMStateField with VMS_ARRAY_OF_POINTER flag, Zhao Liu, 2025/03/17
- [PATCH 07/17] rust/vmstate: Fix type check for varray in vmstate_struct,
Zhao Liu <=
- [PATCH 09/17] rust/vmstate: Fix unnecessary VMState bound of with_varray_flag(), Zhao Liu, 2025/03/17
- [PATCH 04/17] rust/vmstate: Use ident instead of expr to parse vmsd in vmstate_struct macro, Zhao Liu, 2025/03/17
- [PATCH 10/17] rust/vmstate: Relax array check when build varray in vmstate_struct, Zhao Liu, 2025/03/17
- [PATCH 12/17] rust/vmstate: Support version field in vmstate macros, Zhao Liu, 2025/03/17
- [PATCH 13/17] rust/vmstate: Support vmstate_validate, Zhao Liu, 2025/03/17