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

Document and restrict some unsafe code in gix-pack #1137

Merged
merged 6 commits into from
Nov 29, 2023

Conversation

martinvonz
Copy link
Contributor

Thanks to @cramertj for highlighting these issues in our internal review of unsafe code at Google.

I found it confusing that `node` is not a `Node` but an `Item`.
The `Node` type is only used in `resolve.rs`, and using it safely
depends on using the child items correctly, so let's define it there.
The `Clone` impl allows two `ItemSliceSend` to be backed by the same
slice. That makes the behavior of `get_mut()` harder to reason about
because we have to consider clones of the `ItemSliceSend` to determine
if it's safe. By making it `Sync`, we make it more obvious that
multiple threads referring to the same instance need to be considered.

I renamed the type to `ItemSliceSync` since it's now also `Sync`.

We could make the type also check the bounds and that an individual
element has not been borrowed more than once. Then we could make
`get_mut()` not `unsafe`.
`Node` is only safe if it doesn't repeat any indexes into the
`ItemSliceSync`. Let's document this by giving it an unsafe
constructor. To enforce that, I also moved it into a private module.
@Byron
Copy link
Member

Byron commented Nov 29, 2023

Thanks a lot for your help with this!

This portion of the code has seen a lot of revisions, probably more than anything else, which once more shows how hard it is to get unsafe code right. And for better or worse, it's hard to get training with it as well and the Rustonomicon only gets you this far.

@Byron Byron merged commit 115c993 into GitoxideLabs:main Nov 29, 2023
18 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