Skip to content

Commit 63ca035

Browse files
committed
Require unbounded safety and functional correctness across all three rotate strategies
1 parent 3fd261c commit 63ca035

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

‎doc/src/challenges/0036-slice-rotate.md‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ verification is not repeated here.
6565
### Assumptions
6666

6767
- `core::ptr` intrinsics and `core::mem::MaybeUninit` may be assumed correct, and their existing
68-
verified contracts may be reused. Deductive tools may rely on their own specifications of these.
68+
verified contracts may be reused. Deductive tools may rely on their own specifications of these
69+
primitives and on Rust's documented array/tuple layout guarantees (in particular the size and
70+
alignment of the stack buffer `([usize; 32], [T; 0])`).
6971
- The safety precondition of each function is the one in its `# Safety` documentation: the range
7072
`[mid - left, mid + right)` is valid for reading and writing elements of `T`. For non-zero-sized `T`
7173
this includes the usual requirement that the byte size of the range does not exceed `isize::MAX`.
@@ -93,34 +95,24 @@ reading and writing afterwards:
9395
| `ptr_rotate_gcd` | `core::slice::rotate` |
9496
| `ptr_rotate_swap` | `core::slice::rotate` |
9597

96-
Prove the absence of undefined behavior in these functions, including the early returns for
97-
zero-sized `T` and for `left == 0` or `right == 0`, and all three dispatch branches of
98-
`ptr_rotate`. For `ptr_rotate` on inputs that take the memmove branch, and for
99-
`ptr_rotate_memmove` itself, this must hold for arbitrary `left` and `right` (unbounded: no fixed
100-
bound on `left + right` other than the one in the precondition). For the gcd and block-swap
101-
strategies it must hold for every split with `left + right <= B`, with `B >= 24` stated in the
102-
solution (the dispatcher's whole small-total region for the gcd strategy), for the reason given
103-
under functional correctness below; a proof for arbitrary `left` and `right` exceeds this
104-
criterion and is welcome. `const_min` is safe code with no unsafe operations; its result is the
105-
smaller of its arguments, which the dispatch proof may rely on.
98+
Prove the absence of undefined behavior in all four functions for arbitrary `left` and `right`
99+
(unbounded: no fixed bound on `left + right` other than the one in the precondition), including the
100+
early returns for zero-sized `T` and for `left == 0` or `right == 0`, all three dispatch branches of
101+
`ptr_rotate`, and both the default and `optimize_for_size` configurations. `const_min` is safe code
102+
with no unsafe operations; its result is the smaller of its arguments, which the dispatch proof may
103+
rely on.
106104

107105
The verification must hold for generic `T` (what satisfies this clause is to be decided in
108106
[issue #734](https://github.com/model-checking/verify-rust-std/issues/734)). In particular it must
109107
cover zero-sized `T`, sizes that do and do not divide the memmove buffer, alignments above `usize`,
110108
elements larger than the buffer, types with a validity invariant (such as `NonZero`), and types with a
111109
destructor, for each of which the safety argument takes a different branch somewhere in the file.
112110

113-
Prove functional correctness of the rotation: after `ptr_rotate(left, mid, right)`, the element at
114-
each position `j` of `[mid - left, mid + right)` is the element previously at position
115-
`(j + left) mod (left + right)` of that range. This must be proved for each of the three strategies.
116-
For the memmove strategy it must be proved for arbitrary `left` and `right`. For the gcd and
117-
block-swap strategies, whose loop invariants in a bounded model checker cannot name the input
118-
arrangement of a generic `T`, it is enough to prove it for every `left` and `right` with
119-
`left + right <= B`, where `B` is stated in the solution and `B >= 24` (so the whole input range in
120-
which the dispatcher prefers the gcd strategy for small totals is included), for element types whose
121-
values can be told apart. Solutions that prove the rotation postcondition for arbitrary `left` and
122-
`right` for all three strategies (for example with ghost state in a deductive tool) are welcome and
123-
exceed this criterion. The same bound `B` may be used for the safety proof of those two strategies, as stated above.
111+
Prove functional correctness of the rotation for arbitrary `left` and `right` across all three
112+
strategies and `ptr_rotate`: after `ptr_rotate(left, mid, right)`, the element at each position `j`
113+
of `[mid - left, mid + right)` is the element previously at position `(j + left) mod (left + right)`
114+
of that range (equivalently, `append(drop(left, xs), take(left, xs))`), with every element of `T`
115+
preserved (no element duplicated, lost, invalidated, or leaked).
124116

125117
### List of UBs
126118

0 commit comments

Comments
 (0)