Repository navigation
Description
Runbook Link
Proposal PR: #774
Summary
This challenge asks for a verification of in-place slice rotation in
library/core/src/slice/rotate.rs, which backs the safe, stable (and now const) methods
<[T]>::rotate_left and <[T]>::rotate_right. All of the work happens in one private unsafe
function, ptr_rotate, which picks one of three raw-pointer strategies at run time from the size of
T and the split point: a memmove through a 256-byte MaybeUninit stack buffer, the gcd (juggling)
cycle walk, and the Gries--Mills block swap.
The safety argument at every pointer operation rests on arithmetic facts that exist only as prose
comments: that the gcd walk never leaves [0, left + right), that the value computed as a side
effect of the first cycle really is the cycle count and therefore bounds the start offsets of the
remaining cycles, and that the block swaps stay inside the range while mid walks back and forth.
An off-by-one in any of them writes outside the slice that safe code handed to rotate_left, or
duplicates or loses a value of a type with a destructor. No soundness bug is known in this file; the
case for the challenge is that the argument is subtle, serves a safe API, and has no machine-checked
statement. No existing challenge lists a function of core::slice::rotate.
Scope
core::slice::rotate: the four unsafe functionsptr_rotate,ptr_rotate_memmove,
ptr_rotate_gcd,ptr_rotate_swap, plus the safeconst_minhelper whose value the dispatcher's
reasoning depends on.- Both default and
optimize_for_sizeconfigurations. - Generic
T, in particular zero-sizedT, element sizes that do and do not divide the memmove
buffer, alignments aboveusize, elements larger than the buffer, types with a validity invariant
(NonZero), and types with a destructor — each of which takes a different branch somewhere in the
file. - Not in scope, with the reasons on the page:
rotate_left/rotate_right's panic path for a split
larger than the length (a plainassert!before any unsafe code), the rotation-like block moves in
core::slice::sort(they do not call this file), strategy-selection performance, and the
core::ptr/MaybeUninitprimitives themselves (Challenges 2 and 3).
What a solution must show (short form; the page is authoritative)
- Safety contracts for all four unsafe functions stating the range stays valid for reading and
writing, from the documented# Safetyprecondition; a strategy's contract may record the side
condition under which the dispatcher really calls it. - Unbounded absence of UB for arbitrary
leftandrightacross all four functions, including the
early returns for zero-sizedTand forleft == 0orright == 0, all three dispatch branches,
and both default andoptimize_for_sizeconfigurations. - Unbounded functional correctness across all three strategies and
ptr_rotate: after rotation the
element at positionjis the one previously at(j + left) mod (left + right)(equivalently
append(drop(left, xs), take(left, xs))), preserving every element ofTwithout duplication,
loss, invalidation, or leaks.
Activity