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 Tree Borrows & Stacked Borrows Violations #7

Merged
merged 7 commits into from
Oct 19, 2023

Conversation

DropDemBits
Copy link
Contributor

Fixes #6

This resolves all TB violations, and with 6a64bfd also resolves all SB violations. I separated out the commit fixing the SB violations since it's still sound under TB without it, and in the event that a TB or TB-like model becomes the default for miri, that commit can easily be reverted.

This also adds miri to the CI workflow to hopefully catch any future UB.

Should still not cause undefined behaviour even after clearing the list.
Deriving a mutable pointer from an immutable reference results in a `Frozen`
mutable pointer, which is UB to write through. Since all of the contexts
that a mutable pointer is needed also have a mutable reference available,
we can derive the pointer from the mutable reference instead.

This fixes TB violations.
Makes it more clear that we're comparing against the end marker without needing
to think through what the cast is doing.
The issue with the original `containing_record{,_mut}` methods under SB is that
the pointer derived from the list entry reference is only valid for the range
of list entry, and not the containing record itself. By deferring the raw
pointer dereference to once we have a pointer to the containing record, the
resultant reference is valid for the whole record.

This fixes all violations under SB.
Copy link
Owner

@ColinFinck ColinFinck left a comment

Choose a reason for hiding this comment

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

Thank you very much @DropDemBits for looking into this, and coming up with such a detailed PR and comprehensible commit messages!
I know for a long time that I needed to look into this, but didn't find the time so far.

I have reviewed your PR and found just two minor things.
Please let me know what you think about the outstanding suggestions.

nt-list/src/list/base.rs Outdated Show resolved Hide resolved
nt-list/src/list/base.rs Outdated Show resolved Hide resolved
This was a left-over from when `end_marker` was used to also get a mutable
pointer to the end marker. The extra mutable cast isn't needed as it gets
coerced back into an immutable pointer, so we can remove it.
Since `containing_record{,_mut}` is the only use of these functions, they can
be inlined to simplify things. As a bonus, this also makes the implementation
nicely parallel with the `Nt{,SingleListHead}::entry` function which goes the
other way.
@DropDemBits
Copy link
Contributor Author

DropDemBits commented Oct 16, 2023

Addressed both outstanding suggestions (took me a bit to figure out how to word the commit messages 😅).

Copy link
Owner

@ColinFinck ColinFinck left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thank you again for taking care of this 👍

@ColinFinck ColinFinck merged commit 94f5e70 into ColinFinck:master Oct 19, 2023
2 checks passed
@ColinFinck
Copy link
Owner

Now released as 0.2.1 and 0.3.0

@DropDemBits DropDemBits deleted the fix-tb-sb-violations branch November 9, 2023 12:37
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.

This library violates stacked borrows (TM) rules
2 participants