Skip to content

Commit ab40ea5

Browse files
committed
Challenge 13: restore the Index<RangeFrom> contract and proof_for_contract at the kani#4865 pin
1 parent a9ddf14 commit ab40ea5

1 file changed

Lines changed: 8 additions & 17 deletions

File tree

‎library/core/src/ffi/c_str.rs‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,12 @@ impl ops::Index<ops::RangeFrom<usize>> for CStr {
794794
type Output = CStr;
795795

796796
#[inline]
797-
// No `#[ensures]` contract on this trait method: `proof_for_contract` on a
798-
// generic trait method isn't resolvable at this Kani pin, which predates the
799-
// resolver fix (model-checking/kani#4865), so the tail-is-a-valid-`CStr` and
800-
// byte-suffix properties are proven in the `check_index_range_from_contract`
801-
// harness instead of as a contract. Restore the contract form once the pin
802-
// includes kani#4865.
797+
// The tail of a valid `CStr` is itself a valid `CStr`; the postcondition
798+
// binds on normal return only — the out-of-bounds panic path is proven
799+
// separately by the `check_index_range_from_out_of_bounds` harness.
800+
#[requires(self.is_safe())]
801+
#[ensures(|result: &&CStr| result.is_safe())]
802+
#[ensures(|result: &&CStr| result.to_bytes_with_nul() == &self.to_bytes_with_nul()[index.start..])]
803803
fn index(&self, index: ops::RangeFrom<usize>) -> &CStr {
804804
let bytes = self.to_bytes_with_nul();
805805
// we need to manually check the starting index to account for the null
@@ -1226,17 +1226,8 @@ mod verify {
12261226
assert!(c_str.is_safe());
12271227
}
12281228

1229-
// impl ops::Index<ops::RangeFrom<usize>> for CStr — in-bounds tail slicing.
1230-
// Verified as a plain proof rather than proof_for_contract: a pfc target on
1231-
// a generic trait's method (Index<RangeFrom<usize>>) isn't resolvable at
1232-
// this Kani pin, which predates the resolver fix (model-checking/kani#4865)
1233-
// — "unable to find implementation of associated function Index::index for
1234-
// CStr". The
1235-
// tail-is-a-valid-`CStr` and byte-suffix postconditions are asserted
1236-
// directly on the result below rather than as method contracts.
1237-
// (Non-generic trait pfc, e.g. CloneToUninit, still resolves and is
1238-
// kept in contract form.)
1239-
#[kani::proof]
1229+
// impl ops::Index<ops::RangeFrom<usize>> for CStr — in-bounds tail slicing
1230+
#[kani::proof_for_contract(<CStr as ops::Index<ops::RangeFrom<usize>>>::index)]
12401231
#[kani::unwind(17)] // bounded at 16: at 32 this harness took 263s.
12411232
fn check_index_range_from_contract() {
12421233
const MAX_SIZE: usize = 16;

0 commit comments

Comments
 (0)