rust/vmstate: Clean up, fix, enhance & test
Subsystems (2)
v3 cover rust/vmstate: Clean up, fix, enhance & test
Hi,
This is my v3, based on aa90f1161bb17 ("Merge tag 'migration-20250314-pull -request' of https://gitlab.com/farosas/qemu into staging") at master branch.
Compared with v2:
* the patch 10 drops with_exist_check() and with_validate_flag().
* patch 15 is newly added to clen up VMStateFlags in vmstate_clock (which is inspired by setting flags in vmstate_validate).
Patch Summary
=============
Patch 1-9: Clean up & fix for vmstate_of & vmstate_struct, where the
issues are catched by unit tests.
Patch 10: Add vmstate_validate support, and vmstate_validate can accept
safe "test" callback.
Patch 11-14: Add unit test to cover as much as possible cases to be
compatible with C version macros.
* Note while in principle Rust's vmstate pattern doesn't have to match the C version, the C vmstate macros are rich enough to cover as much logic as possible. So checking against the C version is the most effective way to detect the error.
Change Log
==========
Changes since v2:
* Drop with_exist_check() and with_validate_flag().
* Clean up incomplete crate path of VMStateFlags in vmstate_clock.
Changes since v1:
* Drop the patch about prefix separator ", " before [0 .. num] in vmstate macros.
* Drop the pacth which changed "vmsd" to "ident" type in macros.
* Drop the pacth about adding "version" argument.
* Add a with_exist_check() method to help add callback in future.
* Drop addr_of & public VMStateFlags API in unit test.
* Clean up TODO comment.
Best Regards,
Zhao
---
Zhao Liu (15):
rust/vmstate: Remove unnecessary unsafe
rust/vmstate: Fix num_offset in vmstate macros rust/vmstate: Fix num field when varray flags are set rust/vmstate: Fix size field of VMStateField with VMS_ARRAY_OF_POINTER
flag
rust/vmstate: Fix type check for varray in vmstate_struct rust/vmstate: Fix "cannot infer type" error in vmstate_struct rust/vmstate: Fix unnecessary VMState bound of with_varray_flag() rust/vmstate: Relax array check when build varray in vmstate_struct rust/vmstate: Re-implement VMState trait for timer binding rust/vmstate: Support vmstate_validate
rust/vmstate: Add unit test for vmstate_of macro rust/vmstate: Add unit test for vmstate_{of|struct} macro rust/vmstate: Add unit test for pointer case
rust/vmstate: Add unit test for vmstate_validate rust/vmstate: Include complete crate path of VMStateFlags in
vmstate_clock
rust/hw/char/pl011/src/device_class.rs | 8 +- rust/qemu-api/meson.build | 5 +- rust/qemu-api/src/assertions.rs | 15 + rust/qemu-api/src/vmstate.rs | 100 ++++-- rust/qemu-api/tests/tests.rs | 2 + rust/qemu-api/tests/vmstate_tests.rs | 447 +++++++++++++++++++++++++ 6 files changed, 554 insertions(+), 23 deletions(-) create mode 100644 rust/qemu-api/tests/vmstate_tests.rs
Signature · 1 line
2.34.1
PATCH v3 1/15 rust/vmstate: Remove unnecessary unsafe
Remove the unsafe block of vmsd, because vmsd (passed to vmstate_struct) is defined in Rust side now, and it doesn't need unsafe.
--- rust/qemu-api/src/vmstate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/qemu-api/src/vmstate.rs +1 -1
@@ -447,7 +447,7 @@ macro_rules! vmstate_struct { }, size: ::core::mem::size_of::<$type>(), flags: $crate::bindings::VMStateFlags::VMS_STRUCT, - vmsd: unsafe { $vmsd }, + vmsd: $vmsd, ..$crate::zeroable::Zeroable::ZERO $( .with_varray_flag($crate::call_func_with_field!( $crate::vmstate::vmstate_varray_flag,
Signature · 1 line
2.34.1
PATCH v3 2/15 rust/vmstate: Fix num_offset in vmstate macros
In reply to Zhao Liunum_offset is a member of VMStateField, and there's no need to use "." to access this field in a VMStateField instance.
--- rust/qemu-api/src/vmstate.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
rust/qemu-api/src/vmstate.rs +2 -2
@@ -208,7 +208,7 @@ macro_rules! vmstate_of { .as_bytes() .as_ptr() as *const ::std::os::raw::c_char, offset: $crate::offset_of!($struct_name, $field_name), - $(.num_offset: $crate::offset_of!($struct_name, $num),)? + $(num_offset: $crate::offset_of!($struct_name, $num),)? // The calls to `call_func_with_field!` are the magic that // computes most of the VMStateField from the type of the field. info: $crate::info_enum_to_ref!($crate::call_func_with_field!(
@@ -440,7 +440,7 @@ macro_rules! vmstate_struct { name: ::core::concat!(::core::stringify!($field_name), "\0") .as_bytes() .as_ptr() as *const ::std::os::raw::c_char, - $(.num_offset: $crate::offset_of!($struct_name, $num),)? + $(num_offset: $crate::offset_of!($struct_name, $num),)? offset: { $crate::assert_field_type!($struct_name, $field_name, $type ); $crate::offset_of!($struct_name, $field_name)
Signature · 1 line
2.34.1
PATCH v3 3/15 rust/vmstate: Fix num field when varray flags are set
In reply to Zhao LiuArray type vmstate has the VMStateField with num equals its length.
When the varray vmstate is built based a array type, the num field should be cleaned to 0, because varray uses num_offset instead of num to store elements number information.
--- rust/qemu-api/src/vmstate.rs | 1 + 1 file changed, 1 insertion(+)
rust/qemu-api/src/vmstate.rs +1 -0
@@ -275,6 +275,7 @@ pub const fn with_varray_flag<T: VMState>(mut self, flag: VMStateFlags) -> VMStaassert! ((self.flags.0 &VMStateFlags ::VMS_ARRAY .0 ) !=0 ); self.flags =VMStateFlags (self.flags.0 & !VMStateFlags ::VMS_ARRAY .0 ); self.flags =VMStateFlags (self.flags.0 | flag.0 ); + self.num =0 ;// varray uses num_offset instead of num. self }
Signature · 1 line
2.34.1
PATCH v3 4/15 rust/vmstate: Fix size field of VMStateField with VMS_ARRAY_OF_POINTER flag
In reply to Zhao LiuThe size field of the VMStateField with VMS_ARRAY_OF_POINTER flag should stores the size of pointer, which depends on platform.
Currently, *const, *mut, NonNull, Box<> and their wrapper are supported, and they have the same size as usize.
Store the size (of usize) when VMS_ARRAY_OF_POINTER flag is set.
The size may be changed when more smart pointers are supported, but now the size of "usize" is enough.
--- rust/qemu-api/src/vmstate.rs | 4 ++++ 1 file changed, 4 insertions(+)
rust/qemu-api/src/vmstate.rs +4 -0
@@ -256,6 +256,10 @@ pub const fn with_array_flag(mut self, num: usize) -> Self {if (self.flags.0 &VMStateFlags ::VMS_POINTER .0 ) !=0 { self.flags =VMStateFlags (self.flags.0 & !VMStateFlags ::VMS_POINTER .0 ); self.flags =VMStateFlags (self.flags.0 |VMStateFlags ::VMS_ARRAY_OF_POINTER .0 ); +// VMS_ARRAY_OF_POINTER flag stores the size of pointer. +// FIXME: *const, *mut, NonNull and Box<> have the same size as usize. +// Resize if more smart pointers are supported. + self.size = std::mem::size_of ::<usize >(); } self.flags =VMStateFlags (self.flags.0 & !VMStateFlags ::VMS_SINGLE .0 ); self.flags =VMStateFlags (self.flags.0 |VMStateFlags ::VMS_ARRAY .0 );
Signature · 1 line
2.34.1
PATCH v3 5/15 rust/vmstate: Fix type check for varray in vmstate_struct
In reply to Zhao LiuWhen 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.
--- rust/qemu-api/src/assertions.rs | 15 +++++++++++++++ rust/qemu-api/src/vmstate.rs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-)
rust/qemu-api/src/assertions.rs +15 -0
@@ -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
rust/qemu-api/src/vmstate.rs +1 -1
@@ -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>(),
Signature · 1 line
2.34.1
PATCH v3 6/15 rust/vmstate: Fix "cannot infer type" error in vmstate_struct
In reply to Zhao LiuRust cannot infer the type (it should be VMStateField) after Zeroable::ZERO, which cause the compiling error.
To fix this error, call with_varray_flag() after VMStateField's initialization.
--- rust/qemu-api/src/vmstate.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
rust/qemu-api/src/vmstate.rs +9 -7
@@ -453,13 +453,15 @@ macro_rules! vmstate_struct { size: ::core::mem::size_of::<$type>(), flags: $crate::bindings::VMStateFlags::VMS_STRUCT, vmsd: $vmsd, - ..$crate::zeroable::Zeroable::ZERO $( - .with_varray_flag ($crate::call_func_with_field!( - $crate ::vmstate::vmstate_varray_flag, - $struct_name, - $num)) - $(.with_varray_multiply($factor))?)? - } + ..$crate::zeroable::Zeroable ::ZERO + } $(.with_varray_flag( + $crate::call_func_with_field!( + $crate ::vmstate::vmstate_varray_flag, + $struct_name, + $num + ) + ) + $(.with_varray_multiply($factor))?)? }; }
Signature · 1 line
2.34.1
PATCH v3 7/15 rust/vmstate: Fix unnecessary VMState bound of with_varray_flag()
In reply to Zhao LiuThe VMState type bound is not used in with_varray_flag().
And for vmstate_struct, Rust cannot infer the type of num from the call_func_with_field(), so this causes the compiling error because it complains "cannot satisfy _: VMState" in with_varray_flag().
Note Rust can infer the type in vmstate_of macro so that with_varray_flag() can work at there. It is possible that the different initialization ways in the two macros cause differences in Rust's type inference.
But in fact, the VMState type bound is not used in with_varray_flag() and vmstate_varray_flag() has already checked the VMState type, it's safe to drop VMState bound of with_varray_flag(), which can fix the above compiling error.
--- rust/qemu-api/src/vmstate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/qemu-api/src/vmstate.rs +1 -1
@@ -275,7 +275,7 @@ pub const fn with_pointer_flag(mut self) -> Self { }#[must_use] -pub const fn with_varray_flag <T :VMState >(mut self, flag:VMStateFlags ) ->VMStateField { +pub const fn with_varray_flag (mut self, flag:VMStateFlags ) ->VMStateField {assert! ((self.flags.0 &VMStateFlags ::VMS_ARRAY .0 ) !=0 ); self.flags =VMStateFlags (self.flags.0 & !VMStateFlags ::VMS_ARRAY .0 ); self.flags =VMStateFlags (self.flags.0 | flag.0 );
Signature · 1 line
2.34.1
PATCH v3 8/15 rust/vmstate: Relax array check when build varray in vmstate_struct
In reply to Zhao LiuThe varry of structure created by vmstate_struct is different with vmstate_of. This is because vmstate_struct uses the vmsd to traverse the vmstates of structure's fields, rather than treating the structure directly as a well-defined vmstate.
Therefore, there's no need to check array flag when building varray by vmstate_struct.
--- rust/qemu-api/src/vmstate.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
rust/qemu-api/src/vmstate.rs +9 -3
@@ -275,14 +275,20 @@ pub const fn with_pointer_flag(mut self) -> Self { }#[must_use] -pub const fn with_varray_flag (mut self, flag:VMStateFlags ) ->VMStateField { -assert! ((self.flags.0 &VMStateFlags ::VMS_ARRAY .0 ) !=0 ); +pub const fn with_varray_flag_unchecked (mut self, flag:VMStateFlags ) ->VMStateField { self.flags =VMStateFlags (self.flags.0 & !VMStateFlags ::VMS_ARRAY .0 ); self.flags =VMStateFlags (self.flags.0 | flag.0 ); self.num =0 ;// varray uses num_offset instead of num. self } +#[must_use] +#[allow(unused_mut)] +pub const fn with_varray_flag (mut self, flag:VMStateFlags ) ->VMStateField { +assert! ((self.flags.0 &VMStateFlags ::VMS_ARRAY .0 ) !=0 ); + self.with_varray_flag_unchecked (flag) + } +#[must_use] pub const fn with_varray_multiply (mut self, num:u32 ) ->VMStateField {assert! (num <=0x7FFF_FFFFu32 );
@@ -454,7 +460,7 @@ macro_rules! vmstate_struct { flags: $crate::bindings::VMStateFlags::VMS_STRUCT, vmsd: $vmsd, ..$crate::zeroable::Zeroable ::ZERO - } $(.with_varray_flag( + } $(.with_varray_flag_unchecked( $crate::call_func_with_field!( $crate::vmstate::vmstate_varray_flag, $struct_name,
Signature · 1 line
2.34.1
PATCH v3 9/15 rust/vmstate: Re-implement VMState trait for timer binding
In reply to Zhao LiuAt present, Rust side has a timer binding "timer::Timer", so the vmstate for timer should base on that binding instead of the raw "binding::QEMUTimer".
It's possible to apply impl_vmstate_transparent for cell::Opaque and then impl_vmstate_forward for timer::Timer. But binding::QEMUTimer shouldn't be used directly, so that vmstate for such raw timer type is useless.
Thus, apply impl_vmstate_scalar for timer::Timer. And since Opaque<> is useful, apply impl_vmstate_transparent for cell::Opaque as well.
--- rust/qemu-api/src/vmstate.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
rust/qemu-api/src/vmstate.rs +3 -7
@@ -27,12 +27,7 @@use core::{marker::PhantomData , mem, ptr::NonNull };pub use crate ::bindings::{VMStateDescription ,VMStateField }; -use crate ::{ - bindings::{self,VMStateFlags }, - prelude::*, - qom::Owned , - zeroable::Zeroable , -}; +use crate ::{bindings::VMStateFlags , prelude::*, qom::Owned , zeroable::Zeroable };/// This macro is used to call a function with a generic argument bound /// to the type of a field. The function must take a
@@ -344,6 +339,7 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* {impl_vmstate_transparent! (std::pin::Pin <T >where T :VMState );impl_vmstate_transparent! (crate ::cell::BqlCell <T >where T :VMState );impl_vmstate_transparent! (crate ::cell::BqlRefCell <T >where T :VMState ); +impl_vmstate_transparent! (crate ::cell::Opaque <T >where T :VMState );#[macro_export] macro_rules! impl_vmstate_bitsized {
@@ -390,7 +386,7 @@ unsafe impl VMState for $type {impl_vmstate_scalar! (vmstate_info_uint16,u16 ,VMS_VARRAY_UINT16 );impl_vmstate_scalar! (vmstate_info_uint32,u32 ,VMS_VARRAY_UINT32 );impl_vmstate_scalar! (vmstate_info_uint64,u64 ); -impl_vmstate_scalar! (vmstate_info_timer, bindings::QEMUTimer ); +impl_vmstate_scalar! (vmstate_info_timer,crate ::timer::Timer );// Pointer types using the underlying type's VMState plus VMS_POINTER // Note that references are not supported, though references to cells
Signature · 1 line
2.34.1
PATCH v3 10/15 rust/vmstate: Support vmstate_validate
In reply to Zhao LiuIn C version, VMSTATE_VALIDATE accepts the function pointer, which is used to check if some conditions of structure could meet, although the C version macro doesn't accept any structure as the opaque type.
But it's hard to integrate VMSTATE_VALIDAE into vmstate_struct, a new macro has to be introduced to specifically handle the case corresponding to VMSTATE_VALIDATE.
One of the difficulties is inferring the type of a callback by its name test_fn. We can't directly use test_fn as a parameter of test_cb_builder__() to get its type "F", because in this way, Rust compiler will be too conservative on drop check and complain "the destructor for this type cannot be evaluated in constant functions".
Fortunately, PhantomData<T> could help in this case, because it is considered to never have a destructor, no matter its field type [*].
The phantom__() in the call_func_with_field macro provides a good example of using PhantomData to infer type. So copy this idea and apply it to the vmstate_validate macro.
[*]: https://doc.rust-lang.org/std/ops/trait.Drop.html#drop-check
---
Changes since v1:
* Rename with_exist_check() to with_validate_flag().
* Add a with_exist_check() method to help add field_exists callback, though it can't be used in static VMStateDescription definition but will be useful for vmstate builder.
Changes since v2:
* Drop with_exist_check() and with_validate_flag().
--- rust/qemu-api/src/vmstate.rs | 52 +++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-)
rust/qemu-api/src/vmstate.rs +51 -1
@@ -25,9 +25,12 @@//! functionality that is missing from `vmstate_of!`. use core::{marker::PhantomData , mem, ptr::NonNull }; +use std::os::raw::{c_int, c_void};pub use crate ::bindings::{VMStateDescription ,VMStateField }; -use crate ::{bindings::VMStateFlags , prelude::*, qom::Owned , zeroable::Zeroable }; +use crate ::{ + bindings::VMStateFlags , callbacks::FnCall , prelude::*, qom::Owned , zeroable::Zeroable , +};/// This macro is used to call a function with a generic argument bound /// to the type of a field. The function must take a
@@ -508,6 +511,53 @@ macro_rules! vmstate_fields { }} } +pub extern "C" fn rust_vms_test_field_exists <T ,F :for <'a>FnCall <(&'aT ,u8 ),bool >>( + opaque: *mut c_void , + version_id:c_int , +) ->bool { +let owner: &T =unsafe { &*(opaque.cast ::<T >()) }; +let version:u8 = version_id.try_into ().unwrap (); +// SAFETY: the opaque was passed as a reference to `T`. +F ::call ((owner, version)) +} + +pub type VMSFieldExistCb =unsafe extern "C" fn ( + opaque: *mut std::os::raw::c_void , + version_id: std::os::raw::c_int , +) ->bool ; + +#[doc(alias = "VMSTATE_VALIDATE" )] +#[macro_export] +macro_rules! vmstate_validate { + ($struct_name:ty, $test_name:expr, $test_fn:expr $(,)?) => { + $crate::bindings::VMStateField { + name: ::std::ffi::CStr ::as_ptr ($test_name), + field_exists: { +const fn test_cb_builder__ < +T , +F :for <'a> $crate::callbacks::FnCall <(&'aT ,u8 ),bool >, + >( + _phantom: ::core::marker::PhantomData <F >, + ) -> $crate::vmstate::VMSFieldExistCb { +let _: () =F ::ASSERT_IS_SOME ; + $crate::vmstate::rust_vms_test_field_exists ::<T ,F > + } + +const fn phantom__ <T >(_: &T ) -> ::core::marker::PhantomData <T > { + ::core::marker::PhantomData + } +Some (test_cb_builder__ ::<$struct_name,_ >(phantom__ (&$test_fn))) + }, + flags: $crate::bindings::VMStateFlags ( + $crate::bindings::VMStateFlags ::VMS_MUST_EXIST .0 + | $crate::bindings::VMStateFlags ::VMS_ARRAY .0 , + ), + num:0 ,// 0 elements: no data, only run test_fn callback + ..$crate::zeroable::Zeroable ::ZERO + } + }; +} +/// A transparent wrapper type for the `subsections` field of /// [`VMStateDescription`]. ///
Signature · 1 line
2.34.1
PATCH v3 11/15 rust/vmstate: Add unit test for vmstate_of macro
In reply to Zhao LiuThe vmstate has too many combinations of VMStateFlags and VMStateField. Currently, the best way to test is to ensure that the Rust vmstate definition is consistent with the (possibly corresponding) C version.
Add a unit test to cover some patterns accepted by vmstate_of macro, which correspond to the following C version macros:
* VMSTATE_U16
* VMSTATE_UNUSED
* VMSTATE_VARRAY_UINT16_UNSAFE
* VMSTATE_VARRAY_MULTIPLY
Note: Because vmstate_info_* are defined in vmstate-types.c, it's necessary to link libmigration to rust unit tests. In the future, maybe it's possible to spilt libmigration from rust_qemu_api_objs.
--- rust/qemu-api/meson.build | 5 +- rust/qemu-api/tests/tests.rs | 2 + rust/qemu-api/tests/vmstate_tests.rs | 119 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 rust/qemu-api/tests/vmstate_tests.rs
rust/qemu-api/meson.build +3 -2
@@ -58,7 +58,8 @@ rust_qemu_api_objs = static_library( libchardev.extract_all_objects(recursive: false), libcrypto.extract_all_objects(recursive: false), libauthz.extract_all_objects(recursive: false), - libio.extract_all_objects(recursive: false)]) + libio.extract_all_objects(recursive: false), + libmigration.extract_all_objects(recursive: false)]) rust_qemu_api_deps = declare_dependency( dependencies: [ qom_ss.dependencies(),
@@ -71,7 +72,7 @@ rust_qemu_api_deps = declare_dependency( test('rust-qemu-api-integration', executable( 'rust-qemu-api-integration', - 'tests/tests.rs', + files('tests/tests.rs', 'tests/vmstate_tests.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_args: ['--test'], install: false,
rust/qemu-api/tests/tests.rs +2 -0
@@ -17,6 +17,8 @@ zeroable::Zeroable, }; +mod vmstate_tests; +// Test that macros can compile. pub static VMSTATE :VMStateDescription =VMStateDescription { name:c_str! ("name" ).as_ptr (),
rust/qemu-api/tests/vmstate_tests.rs new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 12/15 rust/vmstate: Add unit test for vmstate_{of|struct} macro
In reply to Zhao LiuAdd a unit test to cover some patterns accepted by vmstate_of and vmstate_struct macros, which correspond to the following C version macros:
* VMSTATE_BOOL_V
* VMSTATE_U64
* VMSTATE_STRUCT_VARRAY_UINT8
* (no C version) MULTIPLY variant of VMSTATE_STRUCT_VARRAY_UINT32
* VMSTATE_ARRAY
--- rust/qemu-api/tests/vmstate_tests.rs | 138 ++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-)
rust/qemu-api/tests/vmstate_tests.rs +136 -2
@@ -5,10 +5,14 @@use std::{ffi::CStr , mem::size_of, slice};use qemu_api::{ - bindings::{vmstate_info_int8, vmstate_info_uint8, vmstate_info_unused_buffer,VMStateFlags }, + bindings::{ + vmstate_info_bool, vmstate_info_int64, vmstate_info_int8, vmstate_info_uint64, + vmstate_info_uint8, vmstate_info_unused_buffer,VMStateFlags , + }, c_str, + cell::BqlCell , vmstate::{VMStateDescription ,VMStateField }, - vmstate_fields, vmstate_of, vmstate_unused, + vmstate_fields, vmstate_of, vmstate_struct, vmstate_unused, zeroable::Zeroable , };
@@ -117,3 +121,133 @@ fn test_vmstate_macro_fooa() {// The last VMStateField in VMSTATE_FOOA. assert_eq! (foo_fields[4 ].flags,VMStateFlags ::VMS_END ); } + +// =========================== Test VMSTATE_FOOB =========================== +// Test the use cases of the vmstate macro, corresponding to the following C +// macro variants: +// * VMSTATE_FOOB: +// - VMSTATE_BOOL_V +// - VMSTATE_U64 +// - VMSTATE_STRUCT_VARRAY_UINT8 +// - (no C version) MULTIPLY variant of VMSTATE_STRUCT_VARRAY_UINT32 +// - VMSTATE_ARRAY +#[repr( C )] +#[derive(qemu_api_macros::offsets)] +struct FooB { + arr_a: [FooA ;FOO_ARRAY_MAX ], + num_a:u8 , + arr_a_mul: [FooA ;FOO_ARRAY_MAX ], + num_a_mul:u32 , + wrap:BqlCell <u64 >, + val:bool , +// FIXME: Use Timer array. Now we can't since it's hard to link savevm.c to test. + arr_i64: [i64 ;FOO_ARRAY_MAX ], +} + +static VMSTATE_FOOB :VMStateDescription =VMStateDescription { + name:c_str! ("foo_b" ).as_ptr (), + version_id:2 , + minimum_version_id:1 , + fields:vmstate_fields! { +vmstate_of! (FooB , val).with_version_id (2 ), +vmstate_of! (FooB , wrap), +vmstate_struct! (FooB , arr_a[0 .. num_a], &VMSTATE_FOOA ,FooA ).with_version_id (1 ), +vmstate_struct! (FooB , arr_a_mul[0 .. num_a_mul *32 ], &VMSTATE_FOOA ,FooA ).with_version_id (2 ), +vmstate_of! (FooB , arr_i64), + }, + ..Zeroable ::ZERO +}; + +#[test] +fn test_vmstate_macro_foob () { +let foo_fields: &[VMStateField ] =unsafe { slice::from_raw_parts (VMSTATE_FOOB .fields,6 ) }; + +// 1st VMStateField ("val") in VMSTATE_FOOB (corresponding to VMSTATE_BOOL_V) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[0 ].name) }.to_bytes_with_nul (), +b"val\0" + ); +assert_eq! (foo_fields[0 ].offset,136 ); +assert_eq! (foo_fields[0 ].num_offset,0 ); +assert_eq! (foo_fields[0 ].info,unsafe { &vmstate_info_bool }); +assert_eq! (foo_fields[0 ].version_id,2 ); +assert_eq! (foo_fields[0 ].size,1 ); +assert_eq! (foo_fields[0 ].num,0 ); +assert_eq! (foo_fields[0 ].flags,VMStateFlags ::VMS_SINGLE ); +assert_eq! (foo_fields[0 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[0 ].field_exists.is_none (),true ); + +// 2nd VMStateField ("wrap") in VMSTATE_FOOB (corresponding to VMSTATE_U64) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[1 ].name) }.to_bytes_with_nul (), +b"wrap\0" + ); +assert_eq! (foo_fields[1 ].offset,128 ); +assert_eq! (foo_fields[1 ].num_offset,0 ); +assert_eq! (foo_fields[1 ].info,unsafe { &vmstate_info_uint64 }); +assert_eq! (foo_fields[1 ].version_id,0 ); +assert_eq! (foo_fields[1 ].size,8 ); +assert_eq! (foo_fields[1 ].num,0 ); +assert_eq! (foo_fields[1 ].flags,VMStateFlags ::VMS_SINGLE ); +assert_eq! (foo_fields[1 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[1 ].field_exists.is_none (),true ); + +// 3rd VMStateField ("arr_a") in VMSTATE_FOOB (corresponding to +// VMSTATE_STRUCT_VARRAY_UINT8) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[2 ].name) }.to_bytes_with_nul (), +b"arr_a\0" + ); +assert_eq! (foo_fields[2 ].offset,0 ); +assert_eq! (foo_fields[2 ].num_offset,60 ); +assert_eq! (foo_fields[2 ].info.is_null (),true );// VMSTATE_STRUCT_VARRAY_UINT8 doesn't set info field. +assert_eq! (foo_fields[2 ].version_id,1 ); +assert_eq! (foo_fields[2 ].size,20 ); +assert_eq! (foo_fields[2 ].num,0 ); +assert_eq! ( + foo_fields[2 ].flags.0 , +VMStateFlags ::VMS_STRUCT .0 |VMStateFlags ::VMS_VARRAY_UINT8 .0 + ); +assert_eq! (foo_fields[2 ].vmsd, &VMSTATE_FOOA ); +assert_eq! (foo_fields[2 ].field_exists.is_none (),true ); + +// 4th VMStateField ("arr_a_mul") in VMSTATE_FOOB (corresponding to +// (no C version) MULTIPLY variant of VMSTATE_STRUCT_VARRAY_UINT32) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[3 ].name) }.to_bytes_with_nul (), +b"arr_a_mul\0" + ); +assert_eq! (foo_fields[3 ].offset,64 ); +assert_eq! (foo_fields[3 ].num_offset,124 ); +assert_eq! (foo_fields[3 ].info.is_null (),true );// VMSTATE_STRUCT_VARRAY_UINT8 doesn't set info field. +assert_eq! (foo_fields[3 ].version_id,2 ); +assert_eq! (foo_fields[3 ].size,20 ); +assert_eq! (foo_fields[3 ].num,32 ); +assert_eq! ( + foo_fields[3 ].flags.0 , +VMStateFlags ::VMS_STRUCT .0 + |VMStateFlags ::VMS_VARRAY_UINT32 .0 + |VMStateFlags ::VMS_MULTIPLY_ELEMENTS .0 + ); +assert_eq! (foo_fields[3 ].vmsd, &VMSTATE_FOOA ); +assert_eq! (foo_fields[3 ].field_exists.is_none (),true ); + +// 5th VMStateField ("arr_i64") in VMSTATE_FOOB (corresponding to +// VMSTATE_ARRAY) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[4 ].name) }.to_bytes_with_nul (), +b"arr_i64\0" + ); +assert_eq! (foo_fields[4 ].offset,144 ); +assert_eq! (foo_fields[4 ].num_offset,0 ); +assert_eq! (foo_fields[4 ].info,unsafe { &vmstate_info_int64 }); +assert_eq! (foo_fields[4 ].version_id,0 ); +assert_eq! (foo_fields[4 ].size,8 ); +assert_eq! (foo_fields[4 ].num,FOO_ARRAY_MAX as i32 ); +assert_eq! (foo_fields[4 ].flags,VMStateFlags ::VMS_ARRAY ); +assert_eq! (foo_fields[4 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[4 ].field_exists.is_none (),true ); + +// The last VMStateField in VMSTATE_FOOB. +assert_eq! (foo_fields[5 ].flags,VMStateFlags ::VMS_END ); +}
Signature · 1 line
2.34.1
PATCH v3 13/15 rust/vmstate: Add unit test for pointer case
In reply to Zhao LiuAdd a unit test to cover some patterns accepted by vmstate_of macro, which correspond to the following C version macros:
* VMSTATE_POINTER
* VMSTATE_ARRAY_OF_POINTER
Note: Currently, vmstate_struct can't handle the pointer to structure case. Leave this case as a FIXME and use vmstate_unused as a place holder.
--- rust/qemu-api/tests/vmstate_tests.rs | 115 ++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 4 deletions(-)
rust/qemu-api/tests/vmstate_tests.rs +111 -4
@@ -2,15 +2,16 @@// Author(s): Zhao Liu <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -use std::{ffi::CStr , mem::size_of, slice}; +use std::{ffi::CStr , mem::size_of, ptr::NonNull , slice};use qemu_api::{ bindings::{ - vmstate_info_bool, vmstate_info_int64, vmstate_info_int8, vmstate_info_uint64, - vmstate_info_uint8, vmstate_info_unused_buffer,VMStateFlags , + vmstate_info_bool, vmstate_info_int32, vmstate_info_int64, vmstate_info_int8, + vmstate_info_uint64, vmstate_info_uint8, vmstate_info_unused_buffer,VMStateFlags , }, c_str, - cell::BqlCell , + cell::{BqlCell ,Opaque }, + impl_vmstate_forward, vmstate::{VMStateDescription ,VMStateField }, vmstate_fields, vmstate_of, vmstate_struct, vmstate_unused, zeroable::Zeroable ,
@@ -251,3 +252,109 @@ fn test_vmstate_macro_foob() {// The last VMStateField in VMSTATE_FOOB. assert_eq! (foo_fields[5 ].flags,VMStateFlags ::VMS_END ); } + +// =========================== Test VMSTATE_FOOC =========================== +// Test the use cases of the vmstate macro, corresponding to the following C +// macro variants: +// * VMSTATE_FOOC: +// - VMSTATE_POINTER +// - VMSTATE_ARRAY_OF_POINTER +struct FooCWrapper ([Opaque <*mut u8 >;FOO_ARRAY_MAX ]);// Though Opaque<> array is almost impossible. + +impl_vmstate_forward! (FooCWrapper ); + +#[repr( C )] +#[derive(qemu_api_macros::offsets)] +struct FooC { + ptr: *const i32 , + ptr_a:NonNull <FooA >, + arr_ptr: [Box <u8 >;FOO_ARRAY_MAX ], + arr_ptr_wrap:FooCWrapper , +} + +static VMSTATE_FOOC :VMStateDescription =VMStateDescription { + name:c_str! ("foo_c" ).as_ptr (), + version_id:3 , + minimum_version_id:1 , + fields:vmstate_fields! { +vmstate_of! (FooC , ptr).with_version_id (2 ), +// FIXME: Currently vmstate_struct doesn't support the pointer to structure. +// VMSTATE_STRUCT_POINTER: vmstate_struct!(FooC, ptr_a, VMSTATE_FOOA, NonNull<FooA>) +vmstate_unused! (size_of ::<NonNull <FooA >>()), +vmstate_of! (FooC , arr_ptr), +vmstate_of! (FooC , arr_ptr_wrap), + }, + ..Zeroable ::ZERO +}; + +#[test] +fn test_vmstate_macro_fooc () { +let foo_fields: &[VMStateField ] =unsafe { slice::from_raw_parts (VMSTATE_FOOC .fields,6 ) }; + +// 1st VMStateField ("ptr") in VMSTATE_FOOC (corresponding to VMSTATE_POINTER) +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[0 ].name) }.to_bytes_with_nul (), +b"ptr\0" + ); +assert_eq! (foo_fields[0 ].offset,0 ); +assert_eq! (foo_fields[0 ].num_offset,0 ); +assert_eq! (foo_fields[0 ].info,unsafe { &vmstate_info_int32 }); +assert_eq! (foo_fields[0 ].version_id,2 ); +assert_eq! (foo_fields[0 ].size,4 ); +assert_eq! (foo_fields[0 ].num,0 ); +assert_eq! ( + foo_fields[0 ].flags.0 , +VMStateFlags ::VMS_SINGLE .0 |VMStateFlags ::VMS_POINTER .0 + ); +assert_eq! (foo_fields[0 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[0 ].field_exists.is_none (),true ); + +// 2nd VMStateField ("unused") in VMSTATE_FOOC +let mut offset =size_of ::<*const i32 >(); +let size_ptr_a =size_of ::<NonNull <FooA >>(); + +// 3rd VMStateField ("arr_ptr") in VMSTATE_FOOC (corresponding to +// VMSTATE_ARRAY_OF_POINTER) + offset = offset + size_ptr_a; +let size_arr_ptr =size_of ::<Box <[FooB ;FOO_ARRAY_MAX ]>>(); +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[2 ].name) }.to_bytes_with_nul (), +b"arr_ptr\0" + ); +assert_eq! (foo_fields[2 ].offset, offset); +assert_eq! (foo_fields[2 ].num_offset,0 ); +assert_eq! (foo_fields[2 ].info,unsafe { &vmstate_info_uint8 }); +assert_eq! (foo_fields[2 ].version_id,0 ); +assert_eq! (foo_fields[2 ].size, size_arr_ptr); +assert_eq! (foo_fields[2 ].num,FOO_ARRAY_MAX as i32 ); +assert_eq! ( + foo_fields[2 ].flags.0 , +VMStateFlags ::VMS_ARRAY .0 |VMStateFlags ::VMS_ARRAY_OF_POINTER .0 + ); +assert_eq! (foo_fields[2 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[2 ].field_exists.is_none (),true ); + +// 4th VMStateField ("arr_ptr_wrap") in VMSTATE_FOOC (corresponding to +// VMSTATE_ARRAY_OF_POINTER) + offset = offset +size_of ::<Box <u8 >>() *FOO_ARRAY_MAX ; +let size_arr_ptr_wrap =size_of ::<Opaque <*mut u8 >>(); +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[3 ].name) }.to_bytes_with_nul (), +b"arr_ptr_wrap\0" + ); +assert_eq! (foo_fields[3 ].offset, offset); +assert_eq! (foo_fields[3 ].num_offset,0 ); +assert_eq! (foo_fields[2 ].info,unsafe { &vmstate_info_uint8 }); +assert_eq! (foo_fields[3 ].version_id,0 ); +assert_eq! (foo_fields[3 ].size, size_arr_ptr_wrap); +assert_eq! (foo_fields[3 ].num,FOO_ARRAY_MAX as i32 ); +assert_eq! ( + foo_fields[2 ].flags.0 , +VMStateFlags ::VMS_ARRAY .0 |VMStateFlags ::VMS_ARRAY_OF_POINTER .0 + ); +assert_eq! (foo_fields[3 ].vmsd.is_null (),true ); +assert_eq! (foo_fields[3 ].field_exists.is_none (),true ); + +// The last VMStateField in VMSTATE_FOOC. +assert_eq! (foo_fields[4 ].flags,VMStateFlags ::VMS_END ); +}
Signature · 1 line
2.34.1
PATCH v3 14/15 rust/vmstate: Add unit test for vmstate_validate
In reply to Zhao LiuAdd a unit test for vmstate_validate, which corresponds to the C version
macro: VMSTATE_VALIDATE.
--- rust/qemu-api/tests/vmstate_tests.rs | 91 +++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-)
rust/qemu-api/tests/vmstate_tests.rs +89 -2
@@ -2,7 +2,7 @@// Author(s): Zhao Liu <[email protected]> // SPDX-License-Identifier: GPL-2.0-or-later -use std::{ffi::CStr , mem::size_of, ptr::NonNull , slice}; +use std::{ffi::CStr , mem::size_of, os::raw::c_void, ptr::NonNull , slice};use qemu_api::{ bindings::{
@@ -13,7 +13,7 @@ cell::{BqlCell, Opaque}, impl_vmstate_forward, vmstate::{VMStateDescription, VMStateField}, - vmstate_fields, vmstate_of, vmstate_struct, vmstate_unused, + vmstate_fields, vmstate_of, vmstate_struct, vmstate_unused, vmstate_validate, zeroable::Zeroable, };
@@ -358,3 +358,90 @@ fn test_vmstate_macro_fooc() {// The last VMStateField in VMSTATE_FOOC. assert_eq! (foo_fields[4 ].flags,VMStateFlags ::VMS_END ); } + +// =========================== Test VMSTATE_FOOD =========================== +// Test the use cases of the vmstate macro, corresponding to the following C +// macro variants: +// * VMSTATE_FOOD: +// - VMSTATE_VALIDATE + +// Add more member fields when vmstate_of/vmstate_struct support "test" +// parameter. +struct FooD ; + +impl FooD { +fn validate_food_0 (&self, _version_id:u8 ) ->bool { +true + } + +fn validate_food_1 (_state: &FooD , _version_id:u8 ) ->bool { +false + } +} + +fn validate_food_2 (_state: &FooD , _version_id:u8 ) ->bool { +true +} + +static VMSTATE_FOOD :VMStateDescription =VMStateDescription { + name:c_str! ("foo_d" ).as_ptr (), + version_id:3 , + minimum_version_id:1 , + fields:vmstate_fields! { +vmstate_validate! (FooD ,c_str! ("foo_d_0" ),FooD ::validate_food_0), +vmstate_validate! (FooD ,c_str! ("foo_d_1" ),FooD ::validate_food_1), +vmstate_validate! (FooD ,c_str! ("foo_d_2" ), validate_food_2), + }, + ..Zeroable ::ZERO +}; + +#[test] +fn test_vmstate_macro_food () { +let foo_fields: &[VMStateField ] =unsafe { slice::from_raw_parts (VMSTATE_FOOD .fields,4 ) }; +let mut foo_d =FooD ; +let foo_d_p = &mut foo_das *mut _ as *mut c_void ; + +// 1st VMStateField in VMSTATE_FOOD +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[0 ].name) }.to_bytes_with_nul (), +b"foo_d_0\0" + ); +assert_eq! (foo_fields[0 ].offset,0 ); +assert_eq! (foo_fields[0 ].num_offset,0 ); +assert_eq! (foo_fields[0 ].info.is_null (),true ); +assert_eq! (foo_fields[0 ].version_id,0 ); +assert_eq! (foo_fields[0 ].size,0 ); +assert_eq! (foo_fields[0 ].num,0 ); +assert_eq! ( + foo_fields[0 ].flags.0 , +VMStateFlags ::VMS_ARRAY .0 |VMStateFlags ::VMS_MUST_EXIST .0 + ); +assert_eq! (foo_fields[0 ].vmsd.is_null (),true ); +assert_eq! ( +unsafe { foo_fields[0 ].field_exists.unwrap ()(foo_d_p,0 ) }, +true + ); + +// 2nd VMStateField in VMSTATE_FOOD +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[1 ].name) }.to_bytes_with_nul (), +b"foo_d_1\0" + ); +assert_eq! ( +unsafe { foo_fields[1 ].field_exists.unwrap ()(foo_d_p,1 ) }, +false + ); + +// 3rd VMStateField in VMSTATE_FOOD +assert_eq! ( +unsafe {CStr ::from_ptr (foo_fields[2 ].name) }.to_bytes_with_nul (), +b"foo_d_2\0" + ); +assert_eq! ( +unsafe { foo_fields[2 ].field_exists.unwrap ()(foo_d_p,2 ) }, +true + ); + +// The last VMStateField in VMSTATE_FOOD. +assert_eq! (foo_fields[3 ].flags,VMStateFlags ::VMS_END ); +}
Signature · 1 line
2.34.1
PATCH v3 15/15 rust/vmstate: Include complete crate path of VMStateFlags in vmstate_clock
In reply to Zhao LiuThe use of "bindings::*" masks incomplete path of VMStateFlags.
Include complete crate path of VMStateFlags in vmstate_clock, and clean up "bindings::*" in device_class.rs of pl011.
---
Changes since v2:
* A new commit.
--- rust/hw/char/pl011/src/device_class.rs | 8 ++++++-- rust/qemu-api/src/vmstate.rs | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-)
rust/hw/char/pl011/src/device_class.rs +6 -2
@@ -8,8 +8,12 @@ };use qemu_api::{ - bindings::*, c_str, prelude::*, vmstate_clock, vmstate_fields, vmstate_of, vmstate_struct, - vmstate_subsections, vmstate_unused, zeroable::Zeroable , + bindings::{qdev_prop_bool, qdev_prop_chr}, + c_str, + prelude::*, + vmstate::VMStateDescription , + vmstate_clock, vmstate_fields, vmstate_of, vmstate_struct, vmstate_subsections, vmstate_unused, + zeroable::Zeroable , };use crate ::device::{PL011Registers ,PL011State };
rust/qemu-api/src/vmstate.rs +4 -1
@@ -487,7 +487,10 @@ macro_rules! vmstate_clock { $crate::offset_of!($struct_name, $field_name) }, size: ::core::mem::size_of::<*const $crate::qdev::Clock>(), - flags: VMStateFlags(VMStateFlags::VMS_STRUCT.0 | VMStateFlags::VMS_POINTER.0), + flags: $crate::bindings::VMStateFlags( + $crate::bindings::VMStateFlags::VMS_STRUCT.0 + | $crate::bindings::VMStateFlags::VMS_POINTER.0, + ), vmsd: unsafe { ::core::ptr::addr_of!($crate::bindings::vmstate_clock) }, ..$crate::zeroable::Zeroable ::ZERO }
Signature · 1 line
2.34.1