Skip to content

Check slice element reads under -Z valid-value-checks - #5073

Open
kasimte wants to merge 2 commits into
model-checking:mainfrom
kasimte:fix-5022-slice-element-validity
Open

kasimte wants to merge 2 commits into
model-checking:mainfrom
kasimte:fix-5022-slice-element-validity

Conversation

@kasimte

@kasimte kasimte commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #5022.

-Z valid-value-checks did 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/ConstantIndex arm of visit_place when the base is a slice. It fires only on a direct load of the whole element: a reading_value flag (set in visit_operand) restricts the check to Copy/Move operands, 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 matches main, where the Index arm checked nothing.

Testing

tests/expected/valid-value-checks/slice_element.rs: an invalid bool read through *const [bool] fails Invalid value of type `bool` at a concrete and a symbolic index; valid elements, an address-only &raw const borrow, 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.

@kasimte
kasimte requested review from a team as code owners October 27, 2026 18:11
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Oct 27, 2026
@feliperodri
feliperodri requested a balanced review from Copilot October 31, 2026 00:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 Index and ConstantIndex projections.
  • Add regression harnesses for invalid bool reads 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Z valid-value-checks: reading an invalid element through a slice pointer is not reported

2 participants