Skip to content

Commit 70a0fcc

Browse files
committed
Merge remote-tracking branch 'origin/main' into layout-harnesses
2 parents c534529 + 359fe20 commit 70a0fcc

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

doc/src/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@
1818
- [Pointer Arithmetic](./challenges/0003-pointer-arithmentic.md)
1919
- [Memory safety of BTreeMap's `btree::node` module](./challenges/0004-btree-node.md)
2020
- [Inductive data type](./challenges/0005-linked-list.md)
21+
- [Contracts for SmallSort](./challenges/0008-smallsort.md)
22+
- [Memory safety of String](./challenges/0010-string.md)

doc/src/challenges/0008-smallsort.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Challenge 8: Contracts for SmallSort
2+
3+
- **Status:** Open
4+
- **Tracking Issue:** [Link to issue](https://github.com/model-checking/verify-rust-std/issues/56)
5+
- **Start date:** *2024-08-17*
6+
- **End date:** *2024-12-10*
7+
8+
-------------------
9+
10+
11+
## Goal
12+
13+
The implementations of the traits `StableSmallSortTypeImpl`, `UnstableSmallSortTypeImpl`, and `UnstableSmallSortFreezeTypeImpl` in the `smallsort` [module](https://github.com/rust-lang/rust/blob/master/library/core/src/slice/sort/shared/smallsort.rs) of the Rust standard library are the sorting
14+
algorithms optimized for slices with small lengths.
15+
In this challenge, the goal is to, first prove the memory safety of the public functions in the `smallsort` module, and, second, write contracts for them to
16+
show that the sorting algorithms actually sort the slices.
17+
18+
### Success Criteria
19+
20+
Prove absence of undefined behavior of the following public functions.
21+
22+
1. `<T as slice::sort::shared::smallsort::StableSmallSortTypeImpl>::small_sort`
23+
2. `<T as slice::sort::shared::smallsort::UnstableSmallSortTypeImpl>::small_sort`
24+
3. `<T as slice::sort::shared::smallsort::UnstableSmallSortFreezeTypeImpl>::small_sort`
25+
4. `slice::sort::shared::smallsort::swap_if_less`
26+
5. `slice::sort::shared::smallsort::insertion_sort_shift_left`
27+
6. `slice::sort::shared::smallsort::sort4_stable`
28+
7. `slice::sort::shared::smallsort::has_efficient_in_place_swap`
29+
30+
Write contracts for the following public functions that show that they actually sort the slices.
31+
32+
1. `<T as slice::sort::shared::smallsort::StableSmallSortTypeImpl>::small_sort`
33+
2. `<T as slice::sort::shared::smallsort::UnstableSmallSortTypeImpl>::small_sort`
34+
3. `<T as slice::sort::shared::smallsort::UnstableSmallSortFreezeTypeImpl>::small_sort`
35+
36+
The memory safety and the contracts of the above listed functions must be verified
37+
for all possible slices with arbitrary valid length.
38+
39+
Note that most of the functions listed above call functions that contain loops.
40+
Function contracts and loop contracts of those callee functions may be required.
41+
42+
### List of UBs
43+
44+
In addition to any properties called out as `SAFETY` comments in the source
45+
code,
46+
all proofs must automatically ensure the absence of the following [undefined behaviors](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md):
47+
48+
* Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer.
49+
* Reading from uninitialized memory.
50+
* Mutating immutable bytes.
51+
* Producing an invalid value
52+
53+
Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md)
54+
in addition to the ones listed above.

doc/src/challenges/0010-string.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Challenge X: Memory safety of String
2+
3+
- **Status:** Open
4+
- **Tracking Issue:** [Link to issue](https://github.com/model-checking/verify-rust-std/issues/61)
5+
- **Start date:** *2024-08-19*
6+
- **End date:** *2024-12-10*
7+
8+
-------------------
9+
10+
## Goal
11+
12+
In this challenge, the goal is to verify the memory safety of `std::string::String`.
13+
Even though the majority of `String` methods are safe, many of them are safe abstractions over unsafe code.
14+
15+
For instance, the `insert` method is implemented as follows in v1.80.1:
16+
```rust
17+
pub fn insert(&mut self, idx: usize, ch: char) {
18+
assert!(self.is_char_boundary(idx));
19+
let mut bits = [0; 4];
20+
let bits = ch.encode_utf8(&mut bits).as_bytes();
21+
22+
unsafe {
23+
self.insert_bytes(idx, bits);
24+
}
25+
}
26+
```
27+
where `insert_bytes` has the following implementation:
28+
```rust
29+
unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) {
30+
let len = self.len();
31+
let amt = bytes.len();
32+
self.vec.reserve(amt);
33+
34+
unsafe {
35+
ptr::copy(self.vec.as_ptr().add(idx), self.vec.as_mut_ptr().add(idx + amt), len - idx);
36+
ptr::copy_nonoverlapping(bytes.as_ptr(), self.vec.as_mut_ptr().add(idx), amt);
37+
self.vec.set_len(len + amt);
38+
}
39+
}
40+
```
41+
The call to the unsafe `insert_bytes` method (which itself contains unsafe code) makes `insert` susceptible to undefined behavior.
42+
43+
### Success Criteria
44+
45+
Verify the memory safety of all public functions that are safe abstractions over unsafe code:
46+
47+
1. `from_utf16le` (unbounded)
48+
1. `from_utf16le_lossy`(unbounded)
49+
1. `from_utf16be` (unbounded)
50+
1. `from_utf16be_lossy` (unbounded)
51+
1. `pop`
52+
1. `remove`
53+
1. `remove_matches` (unbounded)
54+
1. `retain` (unbounded)
55+
1. `insert`
56+
1. `insert_str` (unbounded)
57+
1. `split_off` (unbounded)
58+
1. `drain`
59+
1. `replace_range` (unbounded)
60+
1. `into_boxed_str`
61+
1. `leak`
62+
63+
Ones marked as unbounded must be verified for any string/slice length.
64+
65+
### List of UBs
66+
67+
All proofs must automatically ensure the absence of the following [undefined behaviors](https://github.com/rust-lang/reference/blob/142b2ed77d33f37a9973772bd95e6144ed9dce43/src/behavior-considered-undefined.md):
68+
69+
* Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer.
70+
* Reading from uninitialized memory.
71+
* Mutating immutable bytes.
72+
* Producing an invalid value
73+
74+
Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](../general-rules.md)
75+
in addition to the ones listed above.

0 commit comments

Comments
 (0)