This is an archive of the discontinued Mercurial Phabricator instance.

rust: fix unsound `OwningDirstateMap`
ClosedPublic

Authored by Alphare on Apr 4 2022, 11:30 AM.

Details

Summary

As per the previous patch, OwningDirstateMap is unsound. Self-referential
structs are difficult to implement correctly in Rust since the compiler is
free to move structs around as much as it wants to. They are also very rarely
needed in practice, so the state-of-the-art on how they should be done within
the Rust rules is still a bit new.

The crate ouroboros is an attempt at providing a safe way (in the Rust sense)
of declaring self-referential structs. It is getting a lot attention and was
improved very quickly when soundness issues were found in the past: rather than
relying on our own (limited) review circle, we might as well use the de-facto
common crate to fix this problem. This will give us a much better chance of
finding issues should any new ones be discovered as well as the benefit of
fewer unsafe APIs of our own.

I was starting to think about how I would present a safe API to the old struct
but soon realized that the callback-based approach was already done in
ouroboros, along with a lot more care towards refusing incorrect structs.

In short: we don't return a mutable reference to the DirstateMap anymore, we
expect users of its API to pass a FnOnce that takes the map as an argument.
This allows our OwningDirstateMap to control the input and output lifetimes
of the code that modifies it to prevent such issues.

Changing to ouroboros meant changing every API with it, but it is relatively
low churn in the end. It correctly identified the example buggy modification of
copy_map_insert outlined in the previous patch as violating the borrow rules.

Diff Detail

Repository
rHG Mercurial
Branch
stable
Lint
No Linters Available
Unit
No Unit Test Coverage

Event Timeline

Alphare created this revision.Apr 4 2022, 11:30 AM
martinvonz added inline comments.
rust/hg-core/src/dirstate_tree/dirstate_map.rs
989

this lifetime doesn't seem to be needed anymore

rust/hg-core/src/dirstate_tree/owning.rs
75

delete unused lifetime

75

call this with_map_mut() now that it takes a callback?

86

nit: can drop this explicit lifetime, right?

rust/hg-core/src/dirstate_tree/status.rs
43

nit: call the lifetime something useful (like 'dirstate or 'map)?

rust/rhg/src/commands/status.rs
237

nit: maybe call it StatusResult instead? it's a Result, and it's not returned from anywhere

maybe also rename OwningDirstateMap::status() to with_status()

Alphare updated this revision to Diff 32758.Apr 4 2022, 1:38 PM
Alphare marked 5 inline comments as done.Apr 4 2022, 1:41 PM
Alphare added inline comments.
rust/hg-core/src/dirstate_tree/owning.rs
75

I called it with_dmap_mut wince with_map_mut is the generated private function and I think a small layer of encapsulation is better than exposing the field and/or method directly.

baymax updated this revision to Diff 32763.Apr 4 2022, 2:33 PM

✅ refresh by Heptapod after a successful CI run (🐙 💚)
⚠ This patch is intended for stable ⚠

This revision was not accepted when it landed; it landed in state Needs Review.
This revision was automatically updated to reflect the committed changes.