| author | |
| committer | |
| log | 33fc8d5777abe6deb47c09f062df2b910cd5d0cb |
| tree | 7886b550a18c7e4bd2f4a0a2d76a0f1b4401b712 |
| parent | d1248829c53385b698b3081b8db058854239cc0c |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
A revision that moved or dropped a root kept the old root's reference
counted; nothing moves one today, but commit_as declares a moved root, so it
now gives up the old root's reference and takes one for an object that was
already reachable. The seal's root check is now tested with a root that
names another object the page holds rather than a corrupted identity.
Refs #152
Assisted-by: claude-opus-5.53 files changed, 57 insertions(+), 4 deletions(-)
crates/onestore/src/section.rs+13-3| ... | @@ -1626,15 +1626,24 @@ mod tests { | ... | @@ -1626,15 +1626,24 @@ mod tests { |
| 1626 | } | 1626 | } |
| 1627 | 1627 | ||
| 1628 | #[test] | 1628 | #[test] |
| 1629 | fn a_seal_refuses_a_root_it_did_not_add() { | 1629 | fn a_seal_refuses_a_root_naming_another_object() { |
| 1630 | thread_local! { | ||
| 1631 | static OTHER: std::cell::Cell<ExGuid> = const { std::cell::Cell::new(ExGuid { | ||
| 1632 | guid: [0; 16], | ||
| 1633 | n: 0, | ||
| 1634 | }) }; | ||
| 1635 | } | ||
| 1636 | // The new metadata root declared as the page's content root, an object it holds. | ||
| 1630 | fn root(transaction: &mut Transaction) { | 1637 | fn root(transaction: &mut Transaction) { |
| 1631 | let header = 0x8000705a_u32.to_le_bytes(); | 1638 | let header = 0x8000705a_u32.to_le_bytes(); |
| 1632 | let at = transaction | 1639 | let at = 4 + transaction |
| 1633 | .append | 1640 | .append |
| 1634 | .windows(4) | 1641 | .windows(4) |
| 1635 | .position(|w| w == header) | 1642 | .position(|w| w == header) |
| 1636 | .unwrap(); | 1643 | .unwrap(); |
| 1637 | transaction.append[at + 4] ^= 1; | 1644 | let mut other = Vec::new(); |
| 1645 | OTHER.get().encode(&mut other); | ||
| 1646 | transaction.append[at..at + 20].copy_from_slice(&other); | ||
| 1638 | } | 1647 | } |
| 1639 | let source = crate::create_section("root.one", "Original", "Author").unwrap(); | 1648 | let source = crate::create_section("root.one", "Original", "Author").unwrap(); |
| 1640 | let space = page_spaces(&source)[0]; | 1649 | let space = page_spaces(&source)[0]; |
| ... | @@ -1642,6 +1651,7 @@ mod tests { | ... | @@ -1642,6 +1651,7 @@ mod tests { |
| 1642 | let arena = Arena::default(); | 1651 | let arena = Arena::default(); |
| 1643 | let mut section = Section::open(&arena, source.clone()).unwrap(); | 1652 | let mut section = Section::open(&arena, source.clone()).unwrap(); |
| 1644 | section.attribute(space, "Author", Vec::new()).unwrap(); | 1653 | section.attribute(space, "Author", Vec::new()).unwrap(); |
| 1654 | OTHER.set(section.active(space).unwrap().live.revision.roots[&1]); | ||
| 1645 | TAMPER.set(tamper); | 1655 | TAMPER.set(tamper); |
| 1646 | let sealed = section.seal(); | 1656 | let sealed = section.seal(); |
| 1647 | TAMPER.set(None); | 1657 | TAMPER.set(None); |
crates/onestore/src/write.rs+9-1| ... | @@ -1034,6 +1034,7 @@ impl<'a> LiveRevision<'a> { | ... | @@ -1034,6 +1034,7 @@ impl<'a> LiveRevision<'a> { |
| 1034 | .filter(|(role, id)| self.revision.roots.get(role) != Some(id)) | 1034 | .filter(|(role, id)| self.revision.roots.get(role) != Some(id)) |
| 1035 | .map(|(role, id)| (*role, *id)) | 1035 | .map(|(role, id)| (*role, *id)) |
| 1036 | .collect(); | 1036 | .collect(); |
| 1037 | let before: BTreeSet<ExGuid> = self.revision.roots.values().copied().collect(); | ||
| 1037 | self.revision.roots = roots; | 1038 | self.revision.roots = roots; |
| 1038 | let mut changed = BTreeSet::new(); | 1039 | let mut changed = BTreeSet::new(); |
| 1039 | // Whether each object whose count may move was reachable before. | 1040 | // Whether each object whose count may move was reachable before. |
| ... | @@ -1060,10 +1061,17 @@ impl<'a> LiveRevision<'a> { | ... | @@ -1060,10 +1061,17 @@ impl<'a> LiveRevision<'a> { |
| 1060 | message: "Object is the root of multiple roles", | 1061 | message: "Object is the root of multiple roles", |
| 1061 | }); | 1062 | }); |
| 1062 | } | 1063 | } |
| 1064 | // Being a root counts as one reference, which a moved or dropped root gives up. | ||
| 1065 | removed.extend( | ||
| 1066 | before | ||
| 1067 | .iter() | ||
| 1068 | .filter(|id| !roots.contains(id) && self.incoming.contains_key(id)) | ||
| 1069 | .copied(), | ||
| 1070 | ); | ||
| 1063 | added.extend( | 1071 | added.extend( |
| 1064 | roots | 1072 | roots |
| 1065 | .into_iter() | 1073 | .into_iter() |
| 1066 | .filter(|id| !self.incoming.contains_key(id)), | 1074 | .filter(|id| !self.incoming.contains_key(id) || !before.contains(id)), |
| 1067 | ); | 1075 | ); |
| 1068 | // Attaching before detaching keeps a moved subtree from being walked twice. | 1076 | // Attaching before detaching keeps a moved subtree from being walked twice. |
| 1069 | while let Some(id) = added.pop() { | 1077 | while let Some(id) = added.pop() { |
crates/onestore/src/write/tests.rs+35| ... | @@ -970,3 +970,38 @@ fn character_formatting_preserves_inheritance_and_associated_data() { | ... | @@ -970,3 +970,38 @@ fn character_formatting_preserves_inheritance_and_associated_data() { |
| 970 | } | 970 | } |
| 971 | } | 971 | } |
| 972 | } | 972 | } |
| 973 | |||
| 974 | #[test] | ||
| 975 | fn moving_or_dropping_a_root_keeps_reference_counts() { | ||
| 976 | use super::LiveRevision; | ||
| 977 | use crate::{RevisionIndex, Store}; | ||
| 978 | use std::collections::BTreeMap; | ||
| 979 | let source = crate::create_section("roots.one", "Original", "Author").unwrap(); | ||
| 980 | let store = Store::parse(&source).unwrap(); | ||
| 981 | let index = RevisionIndex::parse(&store).unwrap(); | ||
| 982 | let revision = index | ||
| 983 | .spaces | ||
| 984 | .keys() | ||
| 985 | .filter_map(|space| index.resolve_active(*space).ok()) | ||
| 986 | .find(|revision| revision.roots.len() == 2) | ||
| 987 | .unwrap(); | ||
| 988 | let (first, second) = (revision.roots[&1], revision.roots[&2]); | ||
| 989 | let other = *revision.objects[&first] | ||
| 990 | .references() | ||
| 991 | .unwrap() | ||
| 992 | .objects | ||
| 993 | .first() | ||
| 994 | .unwrap(); | ||
| 995 | assert_ne!(other, second); | ||
| 996 | let mut live = LiveRevision::new(revision, 1).unwrap(); | ||
| 997 | let moved = BTreeMap::from([(1, first), (2, other)]); | ||
| 998 | let commit = live.commit_as(Vec::new(), moved).unwrap(); | ||
| 999 | assert!(!commit.checkpoint); | ||
| 1000 | assert_eq!(commit.roots, BTreeMap::from([(2, other)])); | ||
| 1001 | assert_eq!(live.incoming, live.revision.reference_counts().unwrap()); | ||
| 1002 | let commit = live | ||
| 1003 | .commit_as(Vec::new(), BTreeMap::from([(1, first)])) | ||
| 1004 | .unwrap(); | ||
| 1005 | assert!(commit.checkpoint); | ||
| 1006 | assert_eq!(live.incoming, live.revision.reference_counts().unwrap()); | ||
| 1007 | } |