Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
- [27: Verify atomically reference-counted Cell implementation](./challenges/0027-arc.md)
- [28: Verify float to decimal conversion module](./challenges/0028-flt2dec.md)
- [29: Safety of `boxed`](./challenges/0029-boxed.md)
- [31: Verify the safety of `BinaryHeap` functions](./challenges/0031-binary-heap.md)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll renumber the file, title and SUMMARY.md entry to the next free number once this is ready to merge.

92 changes: 92 additions & 0 deletions doc/src/challenges/0031-binary-heap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Challenge 31: Verify the safety of `BinaryHeap` functions

- **Status:** *Open*
- **Solution:** *Option field to point to the PR that solved this challenge.*
- **Tracking Issue:** [#746](https://github.com/model-checking/verify-rust-std/issues/746)
- **Start date:** *2026/11/03*
- **Reward:** *TBD*

-------------------


## Goal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the template has Description and Assumptions sections, which this proposal doesn't. A short Description and an Assumptions section (for example "Vec's own safety may be assumed") would match the other challenges. The path in parentheses could also be a link, as in other challenges.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated! i've added description & assumptions sections, and linked the path


Verify the safety of `BinaryHeap` functions in [binary_heap/mod.rs](https://github.com/model-checking/verify-rust-std/blob/main/library/alloc/src/collections/binary_heap/mod.rs).

## Motivation

`BinaryHeap` is the priority queue of the standard library. To keep its elements in heap order, it moves them with raw pointer reads and copies, at positions chosen by comparing elements with `Ord`. `Ord` is safe code written by users and can return any result, so these moves must be safe even when the comparisons are inconsistent.

## Description

This challenge verifies the safety of `BinaryHeap`'s sift functions, `PeekMut`, the `TrustedLen` sorted iterators, and the safe methods built on them. The sifts move elements through `Hole`, which reads one element out with `ptr::read`, copies others into the gap with `ptr::copy_nonoverlapping` at positions chosen by `Ord`, and writes the element back in `Drop`. `PeekMut` shrinks the length to 1 while a `&mut T` to the top element is out.

The other `unsafe impl`s on `IntoIter` (`TrustedFused`, `SourceIter`, `InPlaceIterable`, `AsVecIntoIter`) delegate to `vec::IntoIter` and are out of scope for this challenge.

### Assumptions

* `Vec`'s implementation may be assumed correct, and its existing verified contracts may be reused.
* A panicking `Ord` is out of scope: solutions need not verify unwinding through `Drop for Hole`.

### Success Criteria

Write and prove the contract for the safety of the following unsafe functions:

| Function |
|---------|
|Hole::new|
|Hole::get|
|Hole::move_to|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: the Hole functions are the right core: Hole::new reads the element out with ptr::read, move_to and Drop write with ptr::copy_nonoverlapping, and every sift goes through them. Specifying their contracts first is what makes the safe methods tractable.

|sift_up|
|sift_down_range|
|sift_down|
|sift_down_to_bottom|

Prove that the following implementations of the unsafe trait `TrustedLen` are correct, that is, that `size_hint` returns the exact number of remaining elements:

| Implementation |
|---------|
|TrustedLen for IntoIterSorted|
|TrustedLen for DrainSorted|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: IntoIter also has unsafe impls with safety obligations: TrustedFused, SourceIter (with unsafe fn as_inner), InPlaceIterable and AsVecIntoIter, which feed Vec's in-place collection. Are they in scope, or covered by the Vec challenges? A sentence saying which would prevent solutions from guessing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be out of scope, which is now said explicitly in the description

The other `unsafe impl`s on `IntoIter`
(`TrustedFused`, `SourceIter`, `InPlaceIterable`, `AsVecIntoIter`)
delegate to `vec::IntoIter` and are out of scope for this challenge.


Prove the absence of undefined behavior for the following safe abstractions:

| Function |
|---------|
|push|
|pop|
|peek_mut|
|PeekMut::deref|
|PeekMut::deref_mut|
|PeekMut::pop|
|PeekMut::refresh|
|Drop for PeekMut|
|rebuild|
|rebuild_tail|
|append|
|into_sorted_vec|
|From<Vec<T>>|
|IntoIterSorted::next|
|DrainSorted::next|
|Drop for Hole|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Hole restores the slice in drop so that a panic in Ord::cmp in the middle of a sift doesn't leave a duplicated or missing element. Line 70 requires safety for any Ord, but doesn't say whether that includes one that panics. Both approved tools stop at panics today: Kani reports a reachable panic as a failure and doesn't model unwinding, and VeriFast's proofs here run with -ignore_unwind_paths. Could you state whether unwinding through Hole::drop must be verified, or that panicking Ord is out of scope? Either is fine, but solutions need to know which.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be out of scope. I've added this to the list of assumptions:

* A panicking `Ord` is out of scope: solutions need not verify unwinding through `Drop for Hole`.


The unsafe functions `from_raw_vec` and `as_mut_slice` require callers to keep the contents a max-heap. This is a correctness requirement, not a safety one, so the functions above must be safe even when the contents are not a max-heap.

The verification must be unbounded---it must hold for heaps of arbitrary length.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: worth keeping. rebuild_tail branches on self.len() <= 2048, so bounded proofs would miss a whole code path.


The verification must hold for generic type `T` (what satisfies this clause is to be decided in [issue #734](https://github.com/model-checking/verify-rust-std/issues/734)).

The verification must hold for any implementation of `Ord` for `T`, including ones that return inconsistent results.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: this is the requirement that gives the challenge its value; a model of Ord that may return anything is exactly what a proof should assume.


### List of UBs

All proofs must automatically ensure the absence of the following undefined behaviors [ref](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md):

* Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer.
* Invoking undefined behavior via compiler intrinsics.
* Reading from uninitialized memory except for padding or unions.
* Producing an invalid value
Comment on lines +81 to +88

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Hole relies on ptr::copy_nonoverlapping and ptr::read, whose preconditions (non-overlapping, valid, aligned) are intrinsic UB rather than plain dangling accesses. Please add "Invoking undefined behavior via compiler intrinsics", as Challenges 2 and 29 (Box) do. Leaving out "mutating immutable bytes" is right here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated! the list of UBs now has this line:

* Invoking undefined behavior via compiler intrinsics.



Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md)
in addition to the ones listed above.
Loading