Skip to content

Use impl_flags! in Rust Binder#4

Open
jahnavi-MN496 wants to merge 4 commits into
Darksonn:rust-binder-basefrom
jahnavi-MN496:b4/rust_binder_impl_flags
Open

Use impl_flags! in Rust Binder#4
jahnavi-MN496 wants to merge 4 commits into
Darksonn:rust-binder-basefrom
jahnavi-MN496:b4/rust_binder_impl_flags

Conversation

@jahnavi-MN496

Copy link
Copy Markdown

No description provided.

In the current Rust Binder driver, internal state variables (thread
looper states, deferred work, and transaction configurations) are
represented as raw integers and manipulated using manual bitwise
operations.

This approach lacks type safety. Because the compiler treats all
integers identically, it is possible to pass a thread looper flag
into a function expecting a transaction flag without triggering
compile-time warnings. These cross-contamination errors compile
cleanly but can cause runtime bugs or undefined behavior.

This patch series resolves this issue by migrating these raw integer
bitmaps (`defer_work`, `looper_flags`, `flags`) to strongly-typed
bitmasks using the `kernel::impl_flags!` macro. Functions now accept
specific, distinct types rather than generic integers, preventing
flags from being mixed up. This transition also replaces manual
bitwise arithmetic with readable, safe methods.

Based on top of:
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git

# Describe the purpose of this series. The information you put here
# will be used by the project maintainer to make a decision whether
# your patches should be reviewed, and in what priority order. Please be
# very detailed and link to any relevant discussions or sites that the
# maintainer can review to better understand your proposed changes. If you
# only have a single patch in your series, the contents of the cover
# letter will be appended to the "under-the-cut" portion of the patch.

# Lines starting with # will be removed from the cover letter. You can
# use them to add notes or reminders to yourself. If you want to use
# markdown headers in your cover letter, start the line with ">#".

# You can add trailers to the cover letter. Any email addresses found in
# these trailers will be added to the addresses specified/generated
# during the b4 send stage. You can also run "b4 prep --auto-to-cc" to
# auto-populate the To: and Cc: trailers based on the code being
# modified.

Change-Id: I8375178251d97e7ad784a6ccd8c108c0d2d2b2eb
Signed-off-by: Jahnavi MN <jahnavimn@google.com>

--- b4-submit-tracking ---
# This section is used internally by b4 prep for tracking purposes.
{
  "series": {
    "revision": 1,
    "change-id": "20260715-b4-rust_binder_impl_flags-e53b4ebca85d",
    "prefixes": []
  }
}
Comment thread drivers/android/binder/transaction.rs Outdated

if self.flags & old.flags & (TF_ONE_WAY | TF_UPDATE_TXN) != (TF_ONE_WAY | TF_UPDATE_TXN) {
let required = TransactionFlag::OneWay | TransactionFlag::UpdateTxn;
if !self.flags.contains_all(required) || !old.flags.contains_all(required) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a super minor thing, but I would find it easier to read like this.

Suggested change
if !self.flags.contains_all(required) || !old.flags.contains_all(required) {
if !(self.flags.contains_all(required) && old.flags.contains_all(required)) {

Comment thread drivers/android/binder/transaction.rs Outdated
#[inline]
pub(crate) fn is_oneway(&self) -> bool {
self.flags & TF_ONE_WAY != 0
self.flags.contains(TransactionFlag::OneWay)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check flags.contains(TransactionFlag::OneWay) super often. It might be nice to add a helper function called is_oneway() on TransactionFlags type to make these calls even shorter and easier to read.

- Define `DeferWorks(u8)` and `DeferWork` enum using `bit_u8` offsets.
- Change `ProcessInner.defer_work` type from `u8` to `DeferWorks`.
- Update `Process::release()` and `Process::flush()` to check for empty
  states using `DeferWorks::empty()`.
- Update the workqueue runner to inspect flags using `.contains()`.
- Define `LooperFlags(u32)` and `LooperFlag` enum with 7 variants.
- Change `InnerThread.looper_flags` type to `LooperFlags`.
- Update looper state transitions and checks to use type-safe methods.
- Convert `looper_flags` to `u32` for hex formatting in `debug_print`.
- Define `TransactionFlags(u32)` and `TransactionFlag` with 4 variants.
- Change flags field type to `TransactionFlags` in structs.
- Add `is_oneway` helper on `TransactionFlags` to simplify checks.
- Update `can_replace` logic to use type-safe combined flag checks.
- Convert `flags` to `u32` for FFI boundaries and logging.
@jahnavi-MN496
jahnavi-MN496 force-pushed the b4/rust_binder_impl_flags branch 3 times, most recently from c0f5662 to 3602828 Compare July 16, 2026 05:15

@Darksonn Darksonn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to ensure that the commit messages:

  1. Have your Signed-off-by tag.
  2. Do not have a Change-Id.

Comment on lines +79 to +84
/// Represents a single deferred work category.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeferWork {
Flush = bit_u8(0),
Release = bit_u8(1),
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this code I am thinking "why do you have to write 0 and 1 on these? Can't you just leave them out and let the macro pick bits for me". After all, with a normal macro you can just write:

enum MyMacro {
    Variant1,
    Variant2,
}

I know the macro doesn't support this yet, but maybe it should?

Totally optional if you want to implement that or not.

@Darksonn Darksonn changed the title B4/rust binder impl flags Use impl_flags! in Rust Binder Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants