Skip to content

Commit bcc66cd

Browse files
Document Kani downcast invariants
1 parent debc377 commit bcc66cd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- add Kani proof checking `Bytes::downcast_to_owner` for matching and mismatched owners
45
- avoid flushing empty memory maps in `Section::freeze` to prevent macOS errors
56
- derived zerocopy traits for `SectionHandle` to allow storing handles in `ByteArea` sections
67
- added example demonstrating `ByteArea` with multiple typed sections, concurrent mutations, and freezing or persisting the area

src/bytes.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,4 +656,32 @@ mod verification {
656656
drop(bytes);
657657
assert!(weak.upgrade().is_none());
658658
}
659+
660+
#[kani::proof]
661+
#[kani::unwind(16)]
662+
pub fn check_downcast_to_owner_preserves_data() {
663+
let data: Vec<u8> = Vec::bounded_any::<16>();
664+
kani::assume(data.len() <= 16);
665+
666+
let bytes = Bytes::from_source(data.clone());
667+
668+
// Invariant: when the owner really is a Vec<u8>, downcasting the Bytes
669+
// owner should succeed and recover the same Arc<Vec<u8>> that backs the
670+
// original allocation. This ensures Bytes keeps the concrete owner
671+
// type intact through cloning and slicing operations.
672+
let arc_vec = bytes
673+
.clone()
674+
.downcast_to_owner::<Vec<u8>>()
675+
.expect("downcast to Vec<u8>");
676+
assert_eq!(&*arc_vec, &data);
677+
678+
// Invariant: attempting to downcast to the wrong owner type must fail
679+
// without mutating the Bytes value. The returned Bytes should still
680+
// expose the same data slice so callers can continue using it.
681+
let result = bytes.downcast_to_owner::<String>();
682+
let Err(returned) = result else {
683+
panic!("downcast to String should fail");
684+
};
685+
assert_eq!(returned.as_ref(), data.as_slice());
686+
}
659687
}

0 commit comments

Comments
 (0)