Skip to content

stm32xx-i2c: remove I2cControl function table #2121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

cbiffle
Copy link
Collaborator

@cbiffle cbiffle commented Jun 30, 2025

It turns out that, in practice, we only ever provide a single function for each of wfi and enable here. This makes the table itself an unnecessary level of indirection, which has some deleterious effects:

  • It opens the possibility for different I2cControl structs containing different function pointers to be passed to each call, complicating analysis. (Searching for this phenomenon was what led me to notice that we only ever use one implementation.)

  • It makes it a lot harder to recognize use of syscalls (or misuse of syscalls) in the I2C layer.

  • It routes important parts of the I2C stack through indirect function calls, defeating stack analysis.

  • It adds a bunch of code that isn't really necessary, as it turns out.

This commit removes the dispatch table, inlining the existing functions into their callsites (and introducing a utility function for the wfi behavior).

@cbiffle cbiffle requested review from bcantrill and mkeeter June 30, 2025 19:34
It turns out that, in practice, we only ever provide a single function
for each of `wfi` and `enable` here. This makes the table itself an
unnecessary level of indirection, which has some deleterious effects:

- It opens the possibility for _different_ I2cControl structs containing
  _different_ function pointers to be passed to each call, complicating
  analysis. (Searching for this phenomenon was what led me to notice
  that we only ever use one implementation.)

- It makes it a lot harder to recognize use of syscalls (or misuse of
  syscalls) in the I2C layer.

- It routes important parts of the I2C stack through indirect function
  calls, defeating stack analysis.

- It adds a bunch of code that isn't really necessary, as it turns out.

This commit removes the dispatch table, inlining the existing functions
into their callsites (and introducing a utility function for the wfi
behavior).
/// This function, like `userlib::hl`, assumes that notification bit 31 is safe
/// to use for the timer. If `notification` also has bit 31 set, weird things
/// will happen, don't do that.
fn wfi_raw(event_mask: u32, timeout: I2cTimeout) -> I2cControlResult {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The docstring is now outdated (refers to notification rather than event_mask)

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