Skip to content

-Z valid-value-checks only instruments the first check in each basic block #5194

Description

@feliperodri

-Z valid-value-checks instruments only the first check in each basic block. A second invalid read in the same block isn't checked, so the harness verifies:

#[kani::proof]
fn second_read_invalid() {
    let good: u8 = 1;
    let bad: u8 = 2;
    let p = &good as *const u8 as *const bool;
    let q = &bad as *const u8 as *const bool;
    let _a = unsafe { *p };
    let _b = unsafe { *q }; // UB: 2 is not a valid bool
}
$ RUSTFLAGS=-Cdebug-assertions=no kani t.rs -Z valid-value-checks
VERIFICATION:- SUCCESSFUL

With the two reads swapped, the invalid one comes first and is reported. With debug assertions on, rustc's alignment check for each raw-pointer read splits the block, which hides the bug in this example; it doesn't for writes, field accesses or reads through references.

Kani main at 47689dc.

Cause. ValidValuePass::transform (kani-compiler/src/kani_middle/transform/check_values.rs) loops with for bb_idx in 0..new_body.blocks().len(). The comment above it says the length must not be cached, but the range is evaluated once. Inserting a check splits the block and moves the instructions after it to a new block at the end, which the loop never visits.

Fix. Loop with while bb_idx < new_body.blocks().len(). It's on branch valid-value-untyped-writes (PR to come, together with #3762, #5190 and #5021, which depend on it), with a regression test (tests/expected/valid-value-checks/untyped_store.rs, a write and a later read in one block).

Activity

  1. added theissue type on Nov 25, 2026
  2. added
    [F] Soundness
    Z-Kani Compiler
    on Nov 25, 2026
  3. added a commit that references this issue on Nov 25, 2026
    8c6dba5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Z-Kani Compiler[F] Soundness

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions