From nobody Fri Apr 4 04:13:26 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1742302446; cv=none; d=zohomail.com; s=zohoarc; b=TXtTH0MgcTQWTnA47GTGGhNb9UzohDMq6ax2J/gsV9lNWCEbA5tBQHXHfe6fneC17piz+5wL6ANhmvKj1Udj+GEwHrtyxl6NH7xyKA0ycTgSoAvgj1uffAuOcmOdZInP1BqwI9hhNbiuXfJZZjzlIJBkLCC3X1eyy7Ctx9Rj1Ag= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742302446; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=56Nwx303SNr9vUA37/9bcBe6IQj9divimq9+YpBea5k=; b=Ia2FfZNucwTyWMWNUJjw3QSiksvCDTvvFWhPlfi+M0XwfABI6yAQSfXbKTjf8A49cePtVcjv/Dg5qaUyAyGbCnIa4q8uzdi8g53YnT/broNicBBu4aNssAKkg//L572rZo+L1ytDqPTI7YvHo2i4ZFm6zK1iUahEqJ81XTyAch8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174230244668896.57509319495682; Tue, 18 Mar 2025 05:54:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tuWIx-0001dx-NN; Tue, 18 Mar 2025 08:44:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tuWHj-00008n-38; Tue, 18 Mar 2025 08:43:10 -0400 Received: from mgamail.intel.com ([198.175.65.12]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tuWHZ-0002wP-Pn; Tue, 18 Mar 2025 08:43:06 -0400 Received: from orviesa009.jf.intel.com ([10.64.159.149]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2025 05:42:27 -0700 Received: from liuzhao-optiplex-7080.sh.intel.com ([10.239.160.39]) by orviesa009.jf.intel.com with ESMTP; 18 Mar 2025 05:42:25 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1742301778; x=1773837778; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WRV9q3I4BxY6pISOntlXgPbEbz688/th6OA3C2ous9s=; b=UGljLdq4pcoVZHOQEiJQKIm3h5uPN4ga5PiO8vVB2m3HN6osBTd5kOrj K2Az//LN93iZp90R5C1JFWqhSIiL0AyyngDhYL+bndV5+YHKC1pRd3C4n kCrXUhkBjnYUDNgQImtD+g/0tAHzeNMvJCKlejze9N28bn8I0A3FROEvT cNp5J/eneWFe40YWQBnsPoHWaSFk8Zqg3s8cpUkbteuPp5glzhWYGitVT H5EGPlGH2D8j5HUqFgzU159qIvKVv9M+U3M0KIxrSReCdn0n6HkgMjrS8 tIFx1FUxXN5IEqNJtTfrdJHreDgvoftux8i8feDYgU19bsZAY3cOpsVYj w==; X-CSE-ConnectionGUID: +HX6NsniRjmYNoe9YyFu6g== X-CSE-MsgGUID: K1XBAH4NTy+j9iJgc79dmw== X-IronPort-AV: E=McAfee;i="6700,10204,11377"; a="54824637" X-IronPort-AV: E=Sophos;i="6.14,256,1736841600"; d="scan'208";a="54824637" X-CSE-ConnectionGUID: hkA7GN48R3GwxLiP1fQ8tg== X-CSE-MsgGUID: tVRXNhRiRUGJc5b1PLdXzw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.14,256,1736841600"; d="scan'208";a="121980485" From: Zhao Liu To: Paolo Bonzini Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org, Zhao Liu Subject: [PATCH v3 13/15] rust/vmstate: Add unit test for pointer case Date: Tue, 18 Mar 2025 21:02:17 +0800 Message-Id: <20250318130219.1799170-14-zhao1.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250318130219.1799170-1-zhao1.liu@intel.com> References: <20250318130219.1799170-1-zhao1.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.12; envelope-from=zhao1.liu@intel.com; helo=mgamail.intel.com X-Spam_score_int: -46 X-Spam_score: -4.7 X-Spam_bar: ---- X-Spam_report: (-4.7 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.332, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1742302448986019100 Content-Type: text/plain; charset="utf-8" Add 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. Signed-off-by: Zhao Liu --- rust/qemu-api/tests/vmstate_tests.rs | 115 ++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 4 deletions(-) diff --git a/rust/qemu-api/tests/vmstate_tests.rs b/rust/qemu-api/tests/vms= tate_tests.rs index fbdc3ee955e4..9fcaad6a01ac 100644 --- a/rust/qemu-api/tests/vmstate_tests.rs +++ b/rust/qemu-api/tests/vmstate_tests.rs @@ -2,15 +2,16 @@ // Author(s): Zhao Liu // SPDX-License-Identifier: GPL-2.0-or-later =20 -use std::{ffi::CStr, mem::size_of, slice}; +use std::{ffi::CStr, mem::size_of, ptr::NonNull, slice}; =20 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_buffe= r, 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); } + +// =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D Test VMSTATE_FOOC =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +// 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<> a= rray is almost impossible. + +impl_vmstate_forward!(FooCWrapper); + +#[repr(C)] +#[derive(qemu_api_macros::offsets)] +struct FooC { + ptr: *const i32, + ptr_a: NonNull, + arr_ptr: [Box; FOO_ARRAY_MAX], + arr_ptr_wrap: FooCWrapper, +} + +static VMSTATE_FOOC: VMStateDescription =3D 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_FO= OA, NonNull) + vmstate_unused!(size_of::>()), + vmstate_of!(FooC, arr_ptr), + vmstate_of!(FooC, arr_ptr_wrap), + }, + ..Zeroable::ZERO +}; + +#[test] +fn test_vmstate_macro_fooc() { + let foo_fields: &[VMStateField] =3D unsafe { slice::from_raw_parts(VMS= TATE_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 =3D size_of::<*const i32>(); + let size_ptr_a =3D size_of::>(); + + // 3rd VMStateField ("arr_ptr") in VMSTATE_FOOC (corresponding to + // VMSTATE_ARRAY_OF_POINTER) + offset =3D offset + size_ptr_a; + let size_arr_ptr =3D size_of::>(); + 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 =3D offset + size_of::>() * FOO_ARRAY_MAX; + let size_arr_ptr_wrap =3D size_of::>(); + 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); +} --=20 2.34.1