Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] rust: bitfield: allow_overlap zeroes mask and hides unrelated
  overlaps
- [Low] rust: bitfield: missing docs for overlap check and allow_overlap
- [Low] rust: bitfield: unchecked field attr doc forwarding and misnamed
  @lohi_chk
--

commit 4bf24af3cd86e778bdc829070073575d0ee69450
Author: Boming Kong <[email protected]>

rust: bitfield: implement compile-time bitfield! overlap check

This commit adds compile-time overlap checking to bitfield! (and register!
by inheritance), introduces a per-field #[allow_overlap] opt-out, and
updates nova-core MMU v2 page table definitions and bitfield tests.

> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index 8d7d781753363..fe67cfe9a44ad 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -359,7 +359,7 @@ unsafe impl $crate::mem::AsReprMut for $name {}
>      // Definitions requiring knowledge of individual fields: private and 
> public field accessors,
>      // and `Debug` implementation.
>      (@fields $vis:vis $name:ident $storage:ty {
> -        $($(#[doc = $doc:expr])* $hi:literal:$lo:literal $field:ident
> +        $($(#[$attr:ident $(= $doc:expr)?])* $hi:literal:$lo:literal 
> $field:ident
>              $(?=> $try_into_type:ty)?
>              $(=> $into_type:ty)?
>          ;
> @@ -371,7 +371,7 @@ impl $name {
>          $(
>          $crate::bitfield!(@private_field_accessors $vis $name $storage : 
> $hi:$lo $field);
>          $crate::bitfield!(
> -            @public_field_accessors $(#[doc = $doc])* $vis $name $storage : 
> $hi:$lo $field
> +            @public_field_accessors $($(#[doc = $doc])?)* $vis $name 
> $storage : $hi:$lo $field
>              $(?=> $try_into_type)?
>              $(=> $into_type)?
>          );
> @@ -379,6 +379,9 @@ impl $name {
>          }
>  
>          $crate::bitfield!(@debug $name { $($field;)* });
> +        $crate::bitfield!(@lohi_chk $name $storage {
> +            $($(#[$attr $(= $doc)?])* $field;)*
> +        });
>      };

[Severity: Low]
Should @fields match doc = $doc:expr and allow_overlap explicitly instead
of rewriting any key-value attribute into #[doc = $doc]?  A malformed
#[allow_overlap = true] gets expanded as #[doc = true] on every public
accessor, causing spurious doc errors alongside the @allow_overlap error.

Also, @lohi_chk is a misleading name since it checks mask overlap rather
than lo/hi ordering, and both @allow_overlap and @lohi_chk omit the
descriptive comments present on the other helper arms in bitfield!.

[ ... ]

> @@ -540,6 +543,35 @@ const fn [<__with_ $field>](
>          );
>      };
>  
> +    (@allow_overlap doc = $doc:expr) => { false };
> +    (@allow_overlap allow_overlap) => { true };

[Severity: Low]
Should the bitfield! syntax documentation in rust/kernel/bitfield.rs (and
rust/kernel/io/register.rs, which inherits this behavior) be updated to
describe the default compile-time overlap check and the #[allow_overlap]
field attribute?

> +    (@lohi_chk $name:ident $storage:ty {
> +        $($(#[$attr:ident $(= $doc:expr)?])* $field:ident;)*
> +    }) => {
> +        ::kernel::macros::paste!(
> +        const _: () = {
> +            let masks: &[$storage] = &[
> +                $(if false $(|| $crate::bitfield!(@allow_overlap $attr $(= 
> $doc)?))* {
> +                    0
> +                } else {
> +                    $name::[<$field:upper _MASK>]
> +                }),*
> +            ];
> +            let err_msg: &[&str] = &[
> +                $(concat!(stringify!($field), " overlaps a previous 
> field")),*
> +            ];
> +            let mut used: $storage = 0;
> +            let mut i = 0;
> +            while i < masks.len() {
> +                ::core::assert!(used & masks[i] == 0, "{}", err_msg[i]);
> +                used |= masks[i];
> +                i += 1;
> +            }
> +        };
> +        );
> +    };

[Severity: Medium]
Does zeroing the mask for #[allow_overlap] fields disable overlap checks
for unrelated fields in both directions?

Because the mask becomes 0, the annotated field is never checked against
earlier fields, and none of its bits are recorded in used for later
unannotated fields.  In nova-core's Pde, table_frame_sys (53:8) carries
#[allow_overlap], so a typo colliding with no_ats (5:5) would compile.

In the same Pde, table_frame_vid (32:8) and peer_id (35:33) leave bits
53:36 unset in used, so a new unannotated field there would silently
overlap table_frame_sys.  Scoping the opt-out to intended overlapping
partners or a mutually exclusive group would keep checking the other bits.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to