Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Critical validity-checking issues reject valid address-only raw borrows, partial reads, and nested assignments by validating values that are not actually read.
2 open issues
What changed in this PR
This PR addresses #5022 by extending Kani’s -Z valid-value-checks instrumentation to check indexed slice elements, whose validity is not checked by a whole-slice dereference.
Changes:
- Add element validity checks for slice
IndexandConstantIndexprojections. - Add regression harnesses for invalid
boolreads at concrete and symbolic indices, plus a valid-element control. - Record expected verification results for all three harnesses.
| File | Description |
|---|---|
tests/expected/valid-value-checks/slice_element.rs |
Adds slice-element validity regression harnesses. |
tests/expected/valid-value-checks/slice_element.expected |
Specifies two invalid-value failures and one successful verification. |
kani-compiler/src/kani_middle/transform/check_values.rs |
Instruments indexed slice elements with validity checks. |
Review effort: Balanced
The Index/ConstantIndex arm fired for any non-mutating use and for nested writes, reporting false positives: an address-of (`&raw const (*p)[i]`) loads no value, a partial read (`(*p)[i][j]`) reads only a subplace, and a nested write (`(*p)[i][j] = x`) validated old contents. Track whether the operand reads the value (Copy/Move) and fire only on a direct read of the whole element. Add positive regressions for each case.
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
3 open issues
2 resolved since last review
Review effort: Lite
| let validity = ty_validity_per_offset(self.tcx, &self.machine, elem_ty, 0); | ||
| match validity { | ||
| Ok(ranges) if !ranges.is_empty() => { | ||
| self.push_target(SourceOp::BytesValidity { | ||
| target_ty: elem_ty, | ||
| rvalue: Rvalue::Use( | ||
| Operand::Copy(Place { | ||
| local: place.local, | ||
| projection: place.projection[..=idx].to_vec(), | ||
| }), | ||
| WithRetag::No, | ||
| ), | ||
| ranges, | ||
| }) | ||
| } | ||
| Err(_msg) => self.push_target(SourceOp::UnsupportedCheck { | ||
| check: "slice element read".to_string(), | ||
| ty: elem_ty, | ||
| }), | ||
| _ => {} |
| fn visit_operand(&mut self, operand: &Operand, location: Location) { | ||
| // `Copy`/`Move` read the operand's value; other uses (`&raw`, `&`, stores) don't. | ||
| let prev = self.reading_value; | ||
| self.reading_value = matches!(operand, Operand::Copy(_) | Operand::Move(_)); | ||
| self.super_operand(operand, location); | ||
| self.reading_value = prev; | ||
| } | ||
|
|
| }) | ||
| } | ||
| Err(_msg) => self.push_target(SourceOp::UnsupportedCheck { | ||
| check: "slice element read".to_string(), |
feliperodri
left a comment
There was a problem hiding this comment.
The fix covers the gap, and the replies show the address-only and deeper-projection cases are handled.
Before merge:
- This is a soundness gap, so please add
[F] Soundnessand[C] Bug. BytesValidityalways builds anOperand::Copy, and the test only readsbools, which areCopy. Please test a move of a non-Copyelement, or handleMoveexplicitly.- Nit: keep the
_msgreason fromty_validity_per_offsetsoUnsupportedCheckreports stay actionable. compile-timer-shortfailed. Please confirm it's the same CI issue as the other PRs.


Resolves #5022.
-Z valid-value-checksdid not report an invalid value read as one element through a slice pointer. A slice[T]carries no static length, so the whole-slice dereference checks no elements, and the indexed read was checked nowhere.The fix checks the element in the
Index/ConstantIndexarm ofvisit_placewhen the base is a slice. It fires only on a direct load of the whole element: areading_valueflag (set invisit_operand) restricts the check toCopy/Moveoperands, and the index must be the last projection. So an address-of, a partial sub-element read, and a nested write are not falsely rejected. A symbolic index makes CBMC cover every element, so no length loop is needed.A nested invalid sub-element read (e.g.
(*p)[i][j]) is still not reported; this matchesmain, where theIndexarm checked nothing.Testing
tests/expected/valid-value-checks/slice_element.rs: an invalidboolread through*const [bool]failsInvalid value of type `bool`at a concrete and a symbolic index; valid elements, an address-only&raw constborrow, a partial read, and a nested write all verify.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.