Skip to content
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

Fix Stacked Borrow violations in BptreeMap #125

Merged
merged 8 commits into from
Oct 2, 2024

Conversation

preston-evans98
Copy link
Contributor

@preston-evans98 preston-evans98 commented Oct 1, 2024

Background

This PR fixes numerous violations of Miri's Stacked Borrow checker in BptreeMap.

In almost all cases, these violations appear to be inconsequential. Casting back-and-forth between * const T and &T appears to interfere with Miri's analysis even though such casts should not be able to introduce data races. Unfortunately, fixing the obvious violations of Stacked Borrows lead to the discovery of one instance of possible UB in RangeMutIter. This PR does not fix that behavior - it merely fixes enough violations to make that behavior discoverable via Miri. Further analysis is required to determine if RangeMutIter is actually sound.

This PR also does not fix any violations of Stacked Borrows in types other than BptreeMap. In the short term, I'm maintaining a fork of the crate which exposes only the functionality that I've been able to show is free from UB.

Description of Changes

This PR introduces no net new functionality. Instead, changes can be grouped into 3 buckets:

  1. Removing unneeded conversions between shared and mutable references which could trigger UB or cause spurious Stacked Borrows violations.

    • The best examples of this are the leaf_ref and branch_ref macros, which returned &mut T but were nearly always coerced to &T immediately afterward. Since creating an aliased &mut T is UB even if the alias is never dereferenced, this behavior was very dangerous. And indeed, it dig trigger Miri's anlysis in several cases. In this PR, I've added parallel versions of these macros which directly yield &T.
  2. Adding variants of existing functions that operate on raw pointers instead of shared references. These new functions allow us to avoid casts between &T and *const T.

    • A good example of this kind of change is the min_raw function, which is exactly like min except that it takes a *const Self instead of &self and returns a *const K instead of &K.
  3. Restructuring tests to avoid coercing from *const T to &T and back by simply storing the *const T in a separate variable rather than shadowing it.

    • A good example of this kind of change is test_bptree2_node_leaf_remove_order.

Copy link
Member

@Firstyear Firstyear left a comment

Choose a reason for hiding this comment

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

One minor question but this looks excellent. Thank you so much. I had always meant to come back and re-run miri to update it based on newer safety properties but alas, time is always the enemy :)

This change is super appreciated, and if you have others I'd love to review them too.

src/internals/bptree/node.rs Outdated Show resolved Hide resolved
@preston-evans98
Copy link
Contributor Author

Happy to do it! I'll continue as I have availability - but, as you as you say, time is always the enemy :)

@Firstyear
Copy link
Member

Yep, I get that. Anyway, this is so very appreciated, thank you!

@Firstyear Firstyear merged commit b32cb4c into kanidm:master Oct 2, 2024
2 of 3 checks passed
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