Debian Codegraph @

src/repr/proofs.rs

raw
1 use super::capacity::{Capacity, MAX_VALUE};
2 use super::{Repr, HEAP_MASK, LENGTH_MASK, MAX_SIZE};
3
4 /// Verify that every valid UTF-8 byte sequence that fits inline survives the unsafe
5 /// `InlineBuffer` to `Repr` conversion without changing its contents or metadata.
6 #[kani::proof]
7 fn inline_repr_roundtrip() {
8 let bytes: [u8; MAX_SIZE] = kani::any();
9 let len: usize = kani::any();
10
11 kani::assume(len <= MAX_SIZE);
12 let input = &bytes[..len];
13 kani::assume(core::str::from_utf8(input).is_ok());
14
15 // SAFETY: The preceding assumption restricts this proof to valid UTF-8 inputs.
16 let text = unsafe { core::str::from_utf8_unchecked(input) };
17 let repr = Repr::new(text).expect("an inline Repr does not allocate");
18
19 assert_eq!(repr.as_str().as_bytes(), input);
20 assert_eq!(repr.len(), len);
21 assert_eq!(repr.capacity(), MAX_SIZE);
22 assert!(!repr.is_heap_allocated());
23 assert!(!repr.is_static_str());
24 assert!(repr.last_byte() < HEAP_MASK);
25
26 if len < MAX_SIZE {
27 assert_eq!(repr.last_byte(), len as u8 | LENGTH_MASK);
28 } else {
29 // A full inline buffer stores string data in its final byte. Valid UTF-8 guarantees that
30 // byte cannot overlap with the range reserved for length markers and discriminants.
31 assert_eq!(repr.last_byte(), bytes[MAX_SIZE - 1]);
32 assert!(repr.last_byte() < LENGTH_MASK);
33 }
34
35 kani::cover!(len == 0);
36 kani::cover!(len == MAX_SIZE);
37 }
38
39 /// Verify the reversible portion of `Capacity`'s packed representation for every value it can
40 /// encode inline.
41 #[kani::proof]
42 fn capacity_roundtrip() {
43 let original: usize = kani::any();
44 kani::assume(original <= MAX_VALUE);
45
46 let encoded = Capacity::new(original);
47 assert!(!encoded.is_heap());
48
49 // SAFETY: The assumption above restricts `encoded` to the inline capacity range.
50 let decoded = unsafe { encoded.as_usize() };
51 assert_eq!(decoded, original);
52
53 kani::cover!(original == 0);
54 kani::cover!(original == MAX_VALUE);
55 }
56
57 /// Exercise the concrete values at both ends of the packed capacity representation, including
58 /// the 32-bit sentinel used when the value itself must be stored on the heap.
59 #[kani::proof]
60 fn capacity_boundaries() {
61 let zero = Capacity::new(0);
62 assert!(!zero.is_heap());
63 // SAFETY: Zero is in the inline capacity range.
64 assert_eq!(unsafe { zero.as_usize() }, 0);
65
66 let max = Capacity::new(MAX_VALUE);
67 assert!(!max.is_heap());
68 // SAFETY: `MAX_VALUE` is the upper end of the inline capacity range.
69 assert_eq!(unsafe { max.as_usize() }, MAX_VALUE);
70
71 #[cfg(target_pointer_width = "32")]
72 {
73 assert!(Capacity::new(MAX_VALUE + 1).is_heap());
74 assert!(Capacity::new(usize::MAX).is_heap());
75 }
76 }
77
78 /// Verify that a value outside the 64-bit packed representation's domain is rejected before it
79 /// can violate the unsafe encoding assumption in `Capacity::new`.
80 #[cfg(target_pointer_width = "64")]
81 #[kani::proof]
82 #[kani::should_panic]
83 fn capacity_rejects_usize_max() {
84 let _ = Capacity::new(usize::MAX);
85 }