Skip to content

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

Description

@JeroenRobben

I found this issue while reviewing Kani with AI assistance and reproduced it with Kani built from the commit below. This report was drafted by AI.

Tested commit: 9461e07 (main), CBMC 6.11.0, toolchain nightly-2026-10-12.

Reproducer

#[cfg_attr(kani, kani::proof)]
fn slice_elem() {
    let bytes = [2u8; 4];
    let p = &bytes as *const [u8] as *const [bool];
    let _x = unsafe { (*p)[1] }; // UB: reads an invalid bool
}
kani -Z valid-value-checks slice_validity.rs --harness slice_elem --exact

Expected behavior

Verification fails: the byte read as bool is 2 (Rust Reference, [undefined.validity.bool]).

Actual behavior

 ** 0 of 7 failed
VERIFICATION:- SUCCESSFUL

Reading the same byte through *(bytes.as_ptr() as *const bool) fails:

Failed Checks: Undefined Behavior: Invalid value of type `bool`

The same happens with slice::from_raw_parts(bytes.as_ptr() as *const bool, 4)[0].

Cause

For a deref of *const [bool], visit_place asks ty_validity_per_offset for the
requirements of [bool]. Its layout is FieldsShape::Array { count: 0 }, which does not match
the count > 0 arm and falls into the "Anything is valid" arm
(kani-compiler/src/kani_middle/transform/check_values.rs:1162-1165). The following Index
projection is treated as safe, so the element read is never checked.

Possibly related: #2998 (validity-checks tracking issue).

Activity

  1. self-assigned this
    on Oct 30, 2026
  2. added
    T-High Priority
    on Nov 6, 2026
  3. added this to the Soundness milestone on Nov 6, 2026
  4. added
    [C] Bug
    on Nov 6, 2026
  5. added theissue type on Nov 6, 2026
  6. added
    [F] Soundness
    Z-Kani Compiler
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

T-High PriorityZ-Kani Compiler[C] Bug[F] Soundness

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions