Skip to content

Commit 457a2b5

Browse files
authored
Merge branch 'model-checking:main' into c-0011-core-nums-yenyunw-unsafe-ints
2 parents fbcf49e + 85a59bc commit 457a2b5

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

doc/src/tools/kani.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Here is a short tutorial of how to use Kani to verify proofs for the standard li
5858
Create a local copy of the [model-checking fork](https://github.com/model-checking/verify-rust-std) of the Rust Standard Library. The fork comes with Kani configured, so all you'll need to do is to call Kani's building-block APIs (such as
5959
`assert`, `assume`, `proof` and [function-contracts](https://github.com/model-checking/kani/blob/main/rfc/src/rfcs/0009-function-contracts.md) such as `modifies`, `requires` and `ensures`) directly.
6060

61+
6162
For example, insert this module into an existing file in the core library, like `library/core/src/hint.rs` or `library/core/src/error.rs` in your copy of the library. This is just for the purpose of getting started, so you can insert in any existing file in the core library if you have other preferences.
6263

6364
``` rust
@@ -97,7 +98,7 @@ The command `kani verify-std` is a sub-command of the `kani`. This specific sub-
9798
- `"path/to/library"`: This argument specifies the path to the modified Rust Standard Library that was prepared earlier in the script. For example, `./library` or `/home/ubuntu/verify-rust-std/library`
9899
- `--target-dir "path/to/target"`: This optional argument sets the target directory where Kani will store its output and intermediate files. For example, `/tmp` or `/tmp/verify-std`
99100

100-
Apart from these, you can use your regular `kani-args` such as `-Z function-contracts` and `-Z stubbing` depending on your verification needs.
101+
Apart from these, you can use your regular `kani-args` such as `-Z function-contracts`, `-Z stubbing` and `-Z mem-predicates` depending on your verification needs. If you run into a Kani error that says `Use of unstable feature`, add the corresponding feature with `-Z` to the command line.
101102
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)
102103

103104
### Step 3 - Check verification result
@@ -143,6 +144,15 @@ Verification Time: 0.01885804s
143144
Complete - 1 successfully verified harnesses, 0 failures, 1 total.
144145
```
145146

147+
Now you can write proof harnesses to verify specific functions in the library.
148+
The current convention is to keep proofs in the same module file of the verification target.
149+
To run Kani for an individual proof, use `--harness [harness_function_name]`.
150+
Note that Kani will batch run all proofs in the library folder if you do not supply the `--harness` flag.
151+
If Kani returns the error `no harnesses matched the harness filter`, you can give the full name of the harness.
152+
For example, to run the proof harness named `check_new` in `library/core/src/ptr/unique.rs`, use
153+
`--harness ptr::unique::verify::check_new`. To run all proofs in `unique.rs`, use `--harness ptr::unique::verify`.
154+
To find the full name of a harness, check the Kani output and find the line starting with `Checking harness [harness full name]`.
155+
146156
## More details
147157

148158
You can find more information about how to install and how you can customize your use of Kani in the

library/core/src/ptr/non_null.rs

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ use crate::ptr::Unique;
88
use crate::slice::{self, SliceIndex};
99
use crate::ub_checks::assert_unsafe_precondition;
1010
use crate::{fmt, hash, intrinsics, ptr};
11+
use safety::{ensures, requires};
12+
13+
14+
#[cfg(kani)]
15+
use crate::kani;
1116

1217
/// `*mut T` but non-zero and [covariant].
1318
///
@@ -192,6 +197,8 @@ impl<T: ?Sized> NonNull<T> {
192197
#[stable(feature = "nonnull", since = "1.25.0")]
193198
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
194199
#[inline]
200+
#[requires(!ptr.is_null())]
201+
#[ensures(|result| result.as_ptr() == ptr)]
195202
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
196203
// SAFETY: the caller must guarantee that `ptr` is non-null.
197204
unsafe {
@@ -221,6 +228,8 @@ impl<T: ?Sized> NonNull<T> {
221228
#[stable(feature = "nonnull", since = "1.25.0")]
222229
#[rustc_const_unstable(feature = "const_nonnull_new", issue = "93235")]
223230
#[inline]
231+
#[ensures(|result| result.is_some() == !ptr.is_null())]
232+
#[ensures(|result| result.is_none() || result.expect("ptr is null!").as_ptr() == ptr)]
224233
pub const fn new(ptr: *mut T) -> Option<Self> {
225234
if !ptr.is_null() {
226235
// SAFETY: The pointer is already checked and is not null
@@ -1770,3 +1779,28 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
17701779
unsafe { NonNull { pointer: reference as *const T } }
17711780
}
17721781
}
1782+
1783+
#[cfg(kani)]
1784+
#[unstable(feature="kani", issue="none")]
1785+
mod verify {
1786+
use super::*;
1787+
use crate::ptr::null_mut;
1788+
1789+
// pub const unsafe fn new_unchecked(ptr: *mut T) -> Self
1790+
#[kani::proof_for_contract(NonNull::new_unchecked)]
1791+
pub fn non_null_check_new_unchecked() {
1792+
let raw_ptr = kani::any::<usize>() as *mut i32;
1793+
unsafe {
1794+
let _ = NonNull::new_unchecked(raw_ptr);
1795+
}
1796+
}
1797+
1798+
// pub const unsafe fn new(ptr: *mut T) -> Option<Self>
1799+
#[kani::proof_for_contract(NonNull::new)]
1800+
pub fn non_null_check_new() {
1801+
let mut x: i32 = kani::any();
1802+
let xptr = &mut x;
1803+
let maybe_null_ptr = if kani::any() { xptr as *mut i32 } else { null_mut() };
1804+
let _ = NonNull::new(maybe_null_ptr);
1805+
}
1806+
}

0 commit comments

Comments
 (0)