Skip to content

Commit 146406b

Browse files
authored
Merge branch 'main' into dhvani_mem
2 parents a93a1b0 + 27a9931 commit 146406b

5 files changed

Lines changed: 73 additions & 20 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Tool Application
3+
about: Submit a new tool application
4+
title: "[Tool Application] "
5+
labels: Tool Application
6+
---
7+
8+
<!--
9+
Please see https://model-checking.github.io/verify-rust-std/tool_template.html for the application template.
10+
-->

doc/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- [4: Memory safety of BTreeMap's `btree::node` module](./challenges/0004-btree-node.md)
2020
- [5: Verify functions iterating over inductive data type: `linked_list`](./challenges/0005-linked-list.md)
2121
- [6: Safety of `NonNull`](./challenges/0006-nonnull.md)
22-
- [7: Safety of Methods for Atomic Types and `ReentrantLock`](./challenges/0007-atomic-types.md)
22+
- [7: Safety of Methods for Atomic Types & Atomic Intrinsics](./challenges/0007-atomic-types.md)
2323
- [8: Contracts for SmallSort](./challenges/0008-smallsort.md)
2424
- [9: Safe abstractions for `core::time::Duration`](./challenges/0009-duration.md)
2525
- [10: Memory safety of String](./challenges/0010-string.md)

doc/src/general-rules.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,17 @@ Follow the following steps to create a new proposal:
6969

7070
## Tool Applications
7171

72-
Solutions must be automated using one of the tools previously approved and listed [here](tools.md#approved-tools):
73-
74-
* Any new tool that participants want to enable will require an application using the [tool application template](./tool_template.md).
75-
* The tool will be analyzed by an independent committee consisting of members from the Rust open-source developers and AWS
76-
* A new tool application should clearly specify the differences to existing techniques and provide sufficient background
77-
of why this is needed.
78-
* The tool application should also include mechanisms to audit its implementation and correctness.
79-
* Once the tool is approved, the participant needs to create a PR that creates a new action that runs the
80-
std library verification using the new tool, as well as an entry to the “Approved Tools” section of this book.
81-
* Once the PR is merged, the tool is considered integrated.
82-
* The repository will be updated periodically, which can impact the tool capacity to analyze the new version of the repository.
83-
I.e., the action may no longer pass after an update.
84-
This will not impact the approval status of the tool, however,
85-
new solutions that want to employ the tool may need to ensure the action is passing first.
72+
Solutions must be automated using one of the tools previously approved and listed [here](tools.md#approved-tools).
73+
To use a new tool, participants must first submit an application for it.
74+
75+
* To submit a tool application, open a new issue in this repository using the "Tool Application" issue template.
76+
* The committee will review the application. Once a committee member approves the application, the participant needs to create a PR with:
77+
* A new workflow that runs the tool against the standard library.
78+
* A new entry to the “Approved Tools” section of this book.
79+
* Once this PR is merged, the tool is considered integrated, and the tool application issue will be closed.
80+
81+
The repository will be updated periodically, which can impact a tool's capacity to analyze the new version of the repository (i.e., the workflow may no longer pass after an update).
82+
If it is determined that the tool requires changes and such changes cannot be provided in a timely fashion the tool's approval may be revoked.
8683

8784
## Committee Applications
8885

doc/src/tool_template.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ _Please enter a description for your tool and any information you deem relevant.
1515
* [ ] Is the tool under development?
1616
* [ ] Will you or your team be able to provide support for the tool?
1717

18+
## Comparison to Other Approved Tools
19+
_Describe how this tool compares to the other approved tools. For example, are there certain properties that this tool can prove that the other tools cannot? The comparison does not need to be exhaustive; the purpose of this section is for the committee to understand the salient differences, and how this tool would fit into the larger effort._
20+
1821
## Licenses
1922
_Please list the license(s) that are used by your tool, and if to your knowledge they conflict with the Rust standard library license(s)._
2023

@@ -24,8 +27,12 @@ _Please list the license(s) that are used by your tool, and if to your knowledge
2427
2. \[Second Step\]
2528
3. \[and so on...\]
2629

27-
## Artifacts
28-
_If there are noteworthy examples of using the tool to perform verification, please include them in this section.Links, papers, etc._
30+
## Artifacts & Audit Mechanisms
31+
_If there are noteworthy examples of using the tool to perform verification, please include them in this section. Links, papers, etc._
32+
_Also include mechanisms for the committee to audit the implementation and correctness of this tool (e.g., regression tests)._
33+
34+
## Versioning
35+
_Please describe how you version the tool._
2936

30-
## CI & Versioning
31-
_Please describe how you version the tool and how it will be supported in CI pipelines._
37+
## CI
38+
_If your tool is approved, you will be responsible merging a workflow into this repository that runs your tool against the standard library. For an example, see the Kani workflow (.github/workflows/kani.yml). Describe, at a high level, how your workflow will operate. (E.g., how will you package the tool to run in CI, how will you identify which proofs to run?)._

library/core/src/ffi/c_str.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ impl CStr {
456456
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
457457
#[rustc_const_stable(feature = "const_cstr_unchecked", since = "1.59.0")]
458458
#[rustc_allow_const_fn_unstable(const_eval_select)]
459+
// Preconditions: Null-terminated and no intermediate null bytes
460+
#[requires(!bytes.is_empty() && bytes[bytes.len() - 1] == 0 && !bytes[..bytes.len()-1].contains(&0))]
461+
// Postcondition: The resulting CStr satisfies the same conditions as preconditions
462+
#[ensures(|result| result.is_safe())]
459463
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
460464
const_eval_select!(
461465
@capture { bytes: &[u8] } -> &CStr:
@@ -779,6 +783,8 @@ impl AsRef<CStr> for CStr {
779783
#[unstable(feature = "cstr_internals", issue = "none")]
780784
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_cstr_from_ptr", since = "1.81.0"))]
781785
#[rustc_allow_const_fn_unstable(const_eval_select)]
786+
#[requires(is_null_terminated(ptr))]
787+
#[ensures(|&result| result < isize::MAX as usize && unsafe { *ptr.add(result) } == 0)]
782788
const unsafe fn strlen(ptr: *const c_char) -> usize {
783789
const_eval_select!(
784790
@capture { s: *const c_char = ptr } -> usize:
@@ -910,6 +916,25 @@ mod verify {
910916
}
911917
}
912918

919+
// pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr
920+
#[kani::proof_for_contract(CStr::from_bytes_with_nul_unchecked)]
921+
#[kani::unwind(33)]
922+
fn check_from_bytes_with_nul_unchecked() {
923+
const MAX_SIZE: usize = 32;
924+
let string: [u8; MAX_SIZE] = kani::any();
925+
let slice = kani::slice::any_slice_of_array(&string);
926+
927+
// Kani assumes that the input slice is null-terminated and contains
928+
// no intermediate null bytes
929+
let c_str = unsafe { CStr::from_bytes_with_nul_unchecked(slice) };
930+
// Kani ensures that the output CStr holds the CStr safety invariant
931+
932+
// Correctness check
933+
let bytes = c_str.to_bytes();
934+
let len = bytes.len();
935+
assert_eq!(bytes, &slice[..len]);
936+
}
937+
913938
// pub fn bytes(&self) -> Bytes<'_>
914939
#[kani::proof]
915940
#[kani::unwind(32)]
@@ -972,6 +997,7 @@ mod verify {
972997
assert!(c_str.is_safe());
973998
}
974999

1000+
// pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError>
9751001
#[kani::proof]
9761002
#[kani::unwind(17)]
9771003
fn check_from_bytes_with_nul() {
@@ -1042,6 +1068,18 @@ mod verify {
10421068
assert!(c_str.is_safe());
10431069
}
10441070

1071+
// const unsafe fn strlen(ptr: *const c_char) -> usize
1072+
#[kani::proof_for_contract(super::strlen)]
1073+
#[kani::unwind(33)]
1074+
fn check_strlen_contract() {
1075+
const MAX_SIZE: usize = 32;
1076+
let mut string: [u8; MAX_SIZE] = kani::any();
1077+
let ptr = string.as_ptr() as *const c_char;
1078+
1079+
unsafe { super::strlen(ptr); }
1080+
}
1081+
1082+
// pub const unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr
10451083
#[kani::proof_for_contract(CStr::from_ptr)]
10461084
#[kani::unwind(33)]
10471085
fn check_from_ptr_contract() {
@@ -1052,6 +1090,7 @@ mod verify {
10521090
unsafe { CStr::from_ptr(ptr); }
10531091
}
10541092

1093+
// pub const fn is_empty(&self) -> bool
10551094
#[kani::proof]
10561095
#[kani::unwind(33)]
10571096
fn check_is_empty() {
@@ -1064,5 +1103,5 @@ mod verify {
10641103
let expected_is_empty = bytes.len() == 0;
10651104
assert_eq!(expected_is_empty, c_str.is_empty());
10661105
assert!(c_str.is_safe());
1067-
}
1106+
}
10681107
}

0 commit comments

Comments
 (0)