qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust/qemu-macros: Convert bit value to u8 within #[property]
@ 2025-10-14 15:02 Zhao Liu
  2025-10-14 15:09 ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Zhao Liu @ 2025-10-14 15:02 UTC (permalink / raw)
  To: Paolo Bonzini, Manos Pitsidianakis; +Cc: qemu-devel, qemu-rust, Zhao Liu

For bit property, make the type conversion within the #[property] macro
so that users do not need to handle the conversion.

Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 rust/hw/timer/hpet/src/device.rs | 2 +-
 rust/qemu-macros/src/lib.rs      | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/rust/hw/timer/hpet/src/device.rs b/rust/hw/timer/hpet/src/device.rs
index 86638c076666..23f2eefd1cd9 100644
--- a/rust/hw/timer/hpet/src/device.rs
+++ b/rust/hw/timer/hpet/src/device.rs
@@ -539,7 +539,7 @@ pub struct HPETState {
     // Internal state
     /// Capabilities that QEMU HPET supports.
     /// bit 0: MSI (or FSB) support.
-    #[property(rename = "msi", bit = HPET_FLAG_MSI_SUPPORT_SHIFT as u8, default = false)]
+    #[property(rename = "msi", bit = HPET_FLAG_MSI_SUPPORT_SHIFT, default = false)]
     flags: u32,
 
     /// Offset of main counter relative to qemu clock.
diff --git a/rust/qemu-macros/src/lib.rs b/rust/qemu-macros/src/lib.rs
index 3e21b67b4719..2a7454da2416 100644
--- a/rust/qemu-macros/src/lib.rs
+++ b/rust/qemu-macros/src/lib.rs
@@ -271,7 +271,10 @@ macro_rules! str_to_c_str {
                 name: ::std::ffi::CStr::as_ptr(#prop_name),
                 info: #qdev_prop,
                 offset: ::core::mem::offset_of!(#name, #field_name) as isize,
-                bitnr: #bitnr,
+                bitnr: {
+                    const { assert!(#bitnr >= 0 && #bitnr <= u8::MAX as _, "bit exceeds u8 range"); }
+                    #bitnr as u8
+                },
                 set_default: #set_default,
                 defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: #defval as u64 },
                 ..::common::Zeroable::ZERO
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust/qemu-macros: Convert bit value to u8 within #[property]
  2025-10-14 15:02 [PATCH] rust/qemu-macros: Convert bit value to u8 within #[property] Zhao Liu
@ 2025-10-14 15:09 ` Paolo Bonzini
  2025-10-14 15:39   ` Zhao Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Paolo Bonzini @ 2025-10-14 15:09 UTC (permalink / raw)
  To: Zhao Liu, Manos Pitsidianakis; +Cc: qemu-devel, qemu-rust

On 10/14/25 17:02, Zhao Liu wrote:
> diff --git a/rust/qemu-macros/src/lib.rs b/rust/qemu-macros/src/lib.rs
> index 3e21b67b4719..2a7454da2416 100644
> --- a/rust/qemu-macros/src/lib.rs
> +++ b/rust/qemu-macros/src/lib.rs
> @@ -271,7 +271,10 @@ macro_rules! str_to_c_str {
>                   name: ::std::ffi::CStr::as_ptr(#prop_name),
>                   info: #qdev_prop,
>                   offset: ::core::mem::offset_of!(#name, #field_name) as isize,
> -                bitnr: #bitnr,
> +                bitnr: {
> +                    const { assert!(#bitnr >= 0 && #bitnr <= u8::MAX as _, "bit exceeds u8 range"); }

The check for the upper limit must be "#bitnr < $field_ty::BITS as _", 
for example rejecting 32 for an u32 field.

Also, the tests need to be updated.

Thanks for remembering about this change!

Paolo

> +                    #bitnr as u8
> +                },
>                   set_default: #set_default,
>                   defval: ::hwcore::bindings::Property__bindgen_ty_1 { u: #defval as u64 },
>                   ..::common::Zeroable::ZERO



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust/qemu-macros: Convert bit value to u8 within #[property]
  2025-10-14 15:09 ` Paolo Bonzini
@ 2025-10-14 15:39   ` Zhao Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Zhao Liu @ 2025-10-14 15:39 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Manos Pitsidianakis, qemu-devel, qemu-rust

On Tue, Oct 14, 2025 at 05:09:34PM +0200, Paolo Bonzini wrote:
> Date: Tue, 14 Oct 2025 17:09:34 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: Re: [PATCH] rust/qemu-macros: Convert bit value to u8 within
>  #[property]
> 
> On 10/14/25 17:02, Zhao Liu wrote:
> > diff --git a/rust/qemu-macros/src/lib.rs b/rust/qemu-macros/src/lib.rs
> > index 3e21b67b4719..2a7454da2416 100644
> > --- a/rust/qemu-macros/src/lib.rs
> > +++ b/rust/qemu-macros/src/lib.rs
> > @@ -271,7 +271,10 @@ macro_rules! str_to_c_str {
> >                   name: ::std::ffi::CStr::as_ptr(#prop_name),
> >                   info: #qdev_prop,
> >                   offset: ::core::mem::offset_of!(#name, #field_name) as isize,
> > -                bitnr: #bitnr,
> > +                bitnr: {
> > +                    const { assert!(#bitnr >= 0 && #bitnr <= u8::MAX as _, "bit exceeds u8 range"); }
> 
> The check for the upper limit must be "#bitnr < $field_ty::BITS as _", for
> example rejecting 32 for an u32 field.

Yes, good point, MAX is not enough.

> Also, the tests need to be updated.

Will do.

Regards,
Zhao



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-14 15:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-14 15:02 [PATCH] rust/qemu-macros: Convert bit value to u8 within #[property] Zhao Liu
2025-10-14 15:09 ` Paolo Bonzini
2025-10-14 15:39   ` Zhao Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).