Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/repo.rs @ 49000:dd6b67d5c256 stable
rust: fix unsound `OwningDirstateMap`
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.
Differential Revision: https://phab.mercurial-scm.org/D12429
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Tue, 05 Apr 2022 10:55:28 +0200 |
parents | 7f633432ca92 |
children | 12adf8c695ed dd2503a63d33 |
comparison
equal
deleted
inserted
replaced
48999:cfd270d83169 | 49000:dd6b67d5c256 |
---|---|
1 use crate::changelog::Changelog; | 1 use crate::changelog::Changelog; |
2 use crate::config::{Config, ConfigError, ConfigParseError}; | 2 use crate::config::{Config, ConfigError, ConfigParseError}; |
3 use crate::dirstate::DirstateParents; | 3 use crate::dirstate::DirstateParents; |
4 use crate::dirstate_tree::dirstate_map::DirstateMap; | |
5 use crate::dirstate_tree::on_disk::Docket as DirstateDocket; | 4 use crate::dirstate_tree::on_disk::Docket as DirstateDocket; |
6 use crate::dirstate_tree::owning::OwningDirstateMap; | 5 use crate::dirstate_tree::owning::OwningDirstateMap; |
7 use crate::errors::HgResultExt; | 6 use crate::errors::HgResultExt; |
8 use crate::errors::{HgError, IoResultExt}; | 7 use crate::errors::{HgError, IoResultExt}; |
9 use crate::exit_codes; | 8 use crate::exit_codes; |
338 self.dirstate_parents.set(docket.parents()); | 337 self.dirstate_parents.set(docket.parents()); |
339 self.dirstate_data_file_uuid | 338 self.dirstate_data_file_uuid |
340 .set(Some(docket.uuid.to_owned())); | 339 .set(Some(docket.uuid.to_owned())); |
341 let data_size = docket.data_size(); | 340 let data_size = docket.data_size(); |
342 let metadata = docket.tree_metadata(); | 341 let metadata = docket.tree_metadata(); |
343 let mut map = if let Some(data_mmap) = self | 342 if let Some(data_mmap) = self |
344 .hg_vfs() | 343 .hg_vfs() |
345 .mmap_open(docket.data_filename()) | 344 .mmap_open(docket.data_filename()) |
346 .io_not_found_as_none()? | 345 .io_not_found_as_none()? |
347 { | 346 { |
348 OwningDirstateMap::new_empty(data_mmap) | 347 OwningDirstateMap::new_v2(data_mmap, data_size, metadata) |
349 } else { | 348 } else { |
350 OwningDirstateMap::new_empty(Vec::new()) | 349 OwningDirstateMap::new_v2(Vec::new(), data_size, metadata) |
351 }; | 350 } |
352 let (on_disk, placeholder) = map.get_pair_mut(); | 351 } else { |
353 *placeholder = DirstateMap::new_v2(on_disk, data_size, metadata)?; | 352 let (map, parents) = |
354 Ok(map) | 353 OwningDirstateMap::new_v1(dirstate_file_contents)?; |
355 } else { | 354 self.dirstate_parents.set(parents); |
356 let mut map = OwningDirstateMap::new_empty(dirstate_file_contents); | |
357 let (on_disk, placeholder) = map.get_pair_mut(); | |
358 let (inner, parents) = DirstateMap::new_v1(on_disk)?; | |
359 self.dirstate_parents | |
360 .set(parents.unwrap_or(DirstateParents::NULL)); | |
361 *placeholder = inner; | |
362 Ok(map) | 355 Ok(map) |
363 } | 356 } |
364 } | 357 } |
365 | 358 |
366 pub fn dirstate_map( | 359 pub fn dirstate_map( |