Skip to content

Commit 316196b

Browse files
authored
Merge branch 'main' into core-transmutation-challenge-2024-06
2 parents 3db5b5a + 614eb77 commit 316196b

File tree

20 files changed

+438
-14
lines changed

20 files changed

+438
-14
lines changed

.github/workflows/kani.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ jobs:
2626
matrix:
2727
# Kani does not support windows.
2828
os: [ubuntu-latest, macos-latest]
29+
include:
30+
- os: ubuntu-latest
31+
base: ubuntu
32+
- os: macos-latest
33+
base: macos
2934
steps:
3035
- name: Checkout Library
3136
uses: actions/checkout@v4
@@ -41,6 +46,11 @@ jobs:
4146
path: kani
4247
ref: features/verify-rust-std
4348

49+
- name: Setup Dependencies
50+
working-directory: kani
51+
run: |
52+
./scripts/setup/${{ matrix.base }}/install_deps.sh
53+
4454
- name: Build `Kani`
4555
working-directory: kani
4656
run: |
@@ -52,5 +62,6 @@ jobs:
5262
env:
5363
RUST_BACKTRACE: 1
5464
run: |
55-
kani verify-std -Z unstable-options ./library --target-dir "target"
65+
kani verify-std -Z unstable-options ./library --target-dir ${{ runner.temp }} -Z function-contracts \
66+
-Z mem-predicates -Z ptr-to-ref-cast-checks
5667

.github/workflows/rustc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ jobs:
6161
6262
- name: Run tests
6363
working-directory: upstream
64+
env:
65+
# Avoid error due to unexpected `cfg`.
66+
RUSTFLAGS: "--check-cfg cfg(kani) --check-cfg cfg(feature,values(any()))"
6467
run: |
6568
./configure --set=llvm.download-ci-llvm=true
6669
./x test --stage 0 library/std

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Session.vim
2626
*.rlib
2727
*.rmeta
2828
*.mir
29+
Cargo.lock
2930

3031
## Temporary files
3132
*~

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Rust std-lib verification
22

3+
[![Rust Tests](https://github.com/model-checking/verify-rust-std/actions/workflows/rustc.yml/badge.svg)](https://github.com/model-checking/verify-rust-std/actions/workflows/rustc.yml)
4+
[![Build Book](https://github.com/model-checking/verify-rust-std/actions/workflows/book.yml/badge.svg)](https://github.com/model-checking/verify-rust-std/actions/workflows/book.yml)
5+
6+
37
This repository is a fork of the official Rust programming
48
language repository, created solely to verify the Rust standard
59
library. It should not be used as an alternative to the official

doc/book.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ runnable = false
2020

2121
[output.linkcheck]
2222

23-
2423
[rust]
2524
edition = "2021"

doc/src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [General Rules](./general-rules.md)
66
- [Challenge Template](./template.md)
7+
- [Tool application](./todo.md)
78

89
- [Verification Tools](./tools.md)
910
- [Kani](./tools/kani.md)
@@ -12,5 +13,5 @@
1213
---
1314

1415
# Challenges
15-
- [Coming soon](./todo.md)
1616
- [Core Transmutation](./core-transmutation.md)
17+
- [Memory safety of core intrinsics](./intrinsics-memory.md)

doc/src/general-rules.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NOTE: This work is not officially affiliated, or endorsed by the Rust project or
1010
tracking issue where contributors can add comments and ask clarification questions.
1111
You can find the list of [open challenges here](https://github.com/model-checking/verify-rust-std/labels/Challenge).
1212

13-
**Solutions:** Solutions to a problem should be submitted as a single Pull Request (PR) to this repository.
13+
**Solutions:** Solutions to a problem should be submitted as a single Pull Request (PR) to this repository.
1414
The solution should run as part of the CI.
1515
See more details about [minimum requirements for each solution](general-rules.md#solution-requirements).
1616

@@ -33,7 +33,7 @@ well as to track the status of the challenge.
3333

3434
A proposed solution to a verification problem will only **be reviewed** if all the minimum requirements below are met:
3535

36-
* Each contribution or attempt should be submitted via a pull request to be analyzed by reviewers.
36+
* Each contribution or attempt should be submitted via a pull request to be analyzed by reviewers.
3737
* By submitting the solution, participants confirm that they can use, modify, copy, and redistribute their contribution,
3838
under the terms of their choice.
3939
* The contribution must be automated and should be checked and pass as part of the PR checks.
@@ -56,7 +56,7 @@ The type of obstacles users face may depend on which part of the standard librar
5656
Everyone is welcome to submit new challenge proposals for review by our committee.
5757
Follow the following steps to create a new proposal:
5858

59-
1. Create a tracking issue using the Issue template [Challenge Proposal](todo.md) for your challenge.
59+
1. Create a tracking issue using the Issue template [Challenge Proposal](template.md) for your challenge.
6060
2. In your fork of this repository do the following:
6161
1. Copy the template file (`book/src/challenge_template.md`) to `book/src/challenges/<ID_NUMBER>-<challenge-name>.md`.
6262
2. Fill in the details according to the template instructions.

doc/src/intrinsics-memory.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Challenge 2: Verify the memory safery of core intrinsics using raw pointers
2+
3+
- **Status:** Open
4+
- **Tracking Issue:** [Link to issue](https://github.com/model-checking/verify-rust-std/issues/16)
5+
- **Start date:** *24/06/12*
6+
- **End date:** *24/12/10*
7+
8+
-------------------
9+
10+
11+
## Goal
12+
13+
Annotate Rust core::intrinsics functions that manipulate raw pointers with their safety contract. Verify their usage in the standard library is in fact safe.
14+
15+
### Success Criteria
16+
17+
1. All the following intrinsic functions must be annotated with safety contracts.
18+
2. Any fallback intrinsic implementation must be verified.
19+
3. For intrinsics modeled in the tool of choice, explain how their implementation matches the intrinsics definition. This can either be done in the PR description or as an entry to the contest book as part of the “Tools” chapter.
20+
4. For each function, contestants must state clearly the list of assumptions for each proof, how the proofs can be audited, and the list of (implicit and explicit) properties that are guaranteed.
21+
5. The verification of each intrinsic should ensure all the documented safety conditions are met, and that meeting them is enough to guarantee safe usage.
22+
23+
24+
Intrinsic functions to be annotated with safety contracts
25+
26+
| Function | Location |
27+
|---------|---------|
28+
|typed_swap | core::intrisics |
29+
|vtable_size| core::intrisics |
30+
|vtable_align| core::intrisics |
31+
|copy_nonoverlapping| core::intrisics |
32+
|copy| core::intrisics |
33+
|write_bytes| core::intrisics |
34+
|size_of_val| core::intrisics |
35+
|arith_offset| core::intrisics |
36+
|volatile_copy_nonoverlapping_memory| core::intrisics |
37+
|volatile_copy_memory| core::intrisics |
38+
|volatile_set_memory| core::intrisics |
39+
|volatile_load| core::intrisics |
40+
|volatile_store| core::intrisics |
41+
|unaligned_volatile_load| core::intrisics |
42+
|unaligned_volatile_store| core::intrisics |
43+
|compare_bytes| core::intrisics |
44+
|min_align_of_val| core::intrisics |
45+
|ptr_offset_from| core::intrisics |
46+
|ptr_offset_from_unsigned| core::intrisics |
47+
|read_via_copy| core::intrisics |
48+
|write_via_move| core::intrisics |
49+
50+
51+
All the following usages of intrinsics were proven safe:
52+
53+
| Function | Location |
54+
|---------|---------|
55+
|copy_from_slice | core::slice |
56+
|parse_u64_into | std::fmt |
57+
|swap | core::mem |
58+
|align_of_val | core::mem |
59+
|zeroed | core::mem::maybe_uninit |
60+
61+
62+
63+
Annotate and verify all the functions that below that expose intrinsics with safety contracts
64+
65+
| Function | Location |
66+
|---------|---------|
67+
|copy_from_slice | std::ptr |
68+
|parse_u64_into | std::ptr |
69+
|swap | std::ptr |
70+
|align_of_val | std::ptr |
71+
|zeroed | std::ptr |
72+
73+
74+
75+
### List of UBs
76+
77+
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):
78+
79+
* Invoking undefined behavior via compiler intrinsics.
80+
* Accessing (loading from or storing to) a place that is dangling or based on a misaligned pointer.
81+
* Reading from uninitialized memory except for padding or unions.
82+
* Mutating immutable bytes.
83+
* Producing an invalid value
84+
85+
86+
Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](general-rules.md)
87+
in addition to the ones listed above.

doc/src/template.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
# Challenge Template
1+
# Challenge XXXX[^challenge_id]: Challenge Title
2+
3+
- **Status:** *One of the following: [Open | Resolved | Expired]*
4+
- **Solution:** *Option field to point to the PR that solved this challenge.*
5+
- **Tracking Issue:** *Link to issue*
6+
- **Start date:** *YY/MM/DD*
7+
- **End date:** *YY/MM/DD*
8+
9+
-------------------
10+
11+
12+
## Goal
13+
14+
*Describe the goal of this challenge with 1-2 sentences.*
15+
16+
## Motivation
17+
18+
*Explain why this is a challenge that should be prioritized. Consider using a motivating example.*
19+
20+
## Description
21+
22+
*Describe the challenge in more details.*
23+
24+
### Assumptions
25+
26+
*Mention any assumption that users may make. Example, "assuming the usage of Stacked Borrows".*
27+
28+
### Success Criteria
29+
30+
*Here are some examples of possible criteria:*
31+
32+
All the following unsafe functions must be annotated with safety contracts and the contracts have been verified:
33+
34+
| Function | Location |
35+
|---------|---------|
36+
| abc | def |
37+
38+
At least N of the following usages were proven safe:
39+
40+
| Function | Location |
41+
|---------|---------|
42+
| xyz | 123 |
43+
44+
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):
45+
46+
*List of UBs*
47+
48+
Note: All solutions to verification challenges need to satisfy the criteria established in the [challenge book](general-rules.md)
49+
in addition to the ones listed above.
50+
51+
[^challenge_id]: The number of the challenge sorted by publication date.

doc/src/tools.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ please see the [Tool Application](general-rules.md#tool-applications) section.
99

1010
## Approved tools:
1111

12-
| Tool | CI Status |
13-
|-----------|-----------|
14-
| Kani | TODO |
12+
| Tool | CI Status |
13+
|---------------------|-------|
14+
| Kani Rust Verifier | [![Kani](https://github.com/model-checking/verify-rust-std/actions/workflows/kani.yml/badge.svg)](https://github.com/model-checking/verify-rust-std/actions/workflows/kani.yml) |
1515

1616

1717

doc/src/tools/kani.md

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,118 @@
11
# Kani Rust Verifier
22

3-
The Kani Rust Verifier is a bit-precise model checker for Rust.
4-
This page will give more details on how to use Kani to verify the standard library.
5-
You can find more informations about how to install and use Kani in the
6-
[Kani book](https://model-checking.github.io/kani/).
3+
The [Kani Rust Verifier](https://github.com/model-checking/kani) is a bit-precise model checker for Rust.
4+
Kani is designed to prove safety properties in your code as well as
5+
the absence of some forms of undefined behavior. It uses model checking under the hood to ensure that
6+
Rust programs adhere to user specified properties.
7+
8+
You can find more information about how to install in [this section of the Kani book](https://model-checking.github.io/kani/install-guide.html).
9+
10+
## Usage
11+
12+
Consider a Rust function that takes an integer and returns its absolute value and
13+
a Kani proof that invokes the function that you want to verify
14+
15+
``` rust
16+
fn abs_diff(a: i32, b: i32) -> i32 {
17+
if a > b {
18+
a - b
19+
} else {
20+
b - a
21+
}
22+
}
23+
24+
#[cfg(kani)]
25+
#[kani::proof]
26+
fn harness() {
27+
let a = kani::any::<i32>();
28+
let b = kani::any::<i32>();
29+
let result = abs_diff(a, b);
30+
kani::assert(result >= 0, "Result should always be more than 0");}
31+
```
32+
33+
Running the command `cargo kani` in your cargo crate will give the result
34+
35+
```
36+
RESULTS:
37+
Check 1: abs_diff.assertion.1
38+
- Status: FAILURE
39+
- Description: "attempt to subtract with overflow"
40+
- Location: src/main.rs:5:9 in function abs_diff
41+
42+
... Other properties that might have failed or succeeded.
43+
44+
Summary:
45+
Verification failed for - harness
46+
Complete - 0 successfully verified harnesses, 1 failures, 1 total.
47+
```
48+
49+
50+
## Using Kani to verify the Rust Standard Library
51+
52+
To aid the Rust Standard Library verification effort, Kani provides a sub-command out of the box to help you get started.
53+
54+
### Step 1
55+
56+
Modify your local copy of the Rust Standard Library by writing proofs for the functions/methods that you want to verify.
57+
58+
For example, insert this short blob into your copy of the library. This blob imports the building-block APIs such as
59+
`assert`, `assume`, `proof` and [function-contracts](https://github.com/model-checking/kani/blob/main/rfc/src/rfcs/0009-function-contracts.md) such as `proof_for_contract` and `fake_function`.
60+
61+
``` rust
62+
#[cfg(kani)]
63+
kani_core::kani_lib!(core);
764

65+
#[cfg(kani)]
66+
#[unstable(feature = "kani", issue = "none")]
67+
pub mod verify {
68+
use crate::kani;
69+
70+
#[kani::proof]
71+
pub fn harness() {
72+
kani::assert(true, "yay");
73+
}
74+
75+
#[kani::proof_for_contract(trivial_function)]
76+
fn dummy_proof() {
77+
trivial_function(true);
78+
}
79+
80+
#[kani::requires(x == true)]
81+
fn trivial_function(x: bool) -> bool {
82+
x
83+
}
84+
}
85+
```
86+
87+
### Step 2
88+
89+
Run the following command in your local terminal:
90+
91+
`kani verify-std -Z unstable-options "path/to/library" --target-dir "/path/to/target" -Z function-contracts -Z stubbing`.
92+
93+
The command `kani verify-std` is a sub-command of the `kani`. This specific sub-command is used to verify the Rust Standard Library with the following arguments.
94+
95+
- `"path/to/library"`: This argument specifies the path to the modified Rust Standard Library that was prepared earlier in the script.
96+
- `--target-dir "path/to/target"`: This optional argument sets the target directory where Kani will store its output and intermediate files.
97+
98+
Apart from these, you can use your regular `kani-args` such as `-Z function-contracts` and `-Z stubbing` depending on your verification needs.
99+
For more details on Kani's features, refer to [the features section in the Kani Book](https://model-checking.github.io/kani/reference/attributes.html)
100+
101+
### Step 3
102+
103+
After running the command, you can expect an output that looks like this:
104+
105+
```
106+
SUMMARY:
107+
** 0 of 1 failed
108+
109+
VERIFICATION:- SUCCESSFUL
110+
Verification Time: 0.017101772s
111+
112+
Complete - 2 successfully verified harnesses, 0 failures, 2 total.
113+
```
114+
115+
## More details
116+
117+
You can find more information about how to install and how you can customize your use of Kani in the
118+
[Kani book](https://model-checking.github.io/kani/).

0 commit comments

Comments
 (0)