Skip to content

-Z valid-value-checks: writing an invalid bool through a *const round-trip or a transmuted &mut u8 is not reported #5021

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 via_const_ptr() {
    let mut b = true;
    let p = &mut b as *mut bool as *const u8 as *mut u8;
    unsafe { *p = 2 };
    let _x = b; // UB: reads an invalid bool
}

#[cfg_attr(kani, kani::proof)]
fn via_transmuted_ref() {
    let mut b = true;
    let r: &mut u8 = unsafe { std::mem::transmute::<&mut bool, &mut u8>(&mut b) };
    *r = 2;
    let _x = b; // UB: reads an invalid bool
}
kani -Z valid-value-checks mut_cast_bypass.rs --harness via_const_ptr --exact
kani -Z valid-value-checks mut_cast_bypass.rs --harness via_transmuted_ref --exact

Expected behavior

Both fail. Reading b after storing 2 into it reads an invalid bool (Rust Reference,
[undefined.validity.bool]: "A bool value must be false (0) or true (1)").

Actual behavior

 ** 0 of 7 failed
VERIFICATION:- SUCCESSFUL
 ** 0 of 9 failed
VERIFICATION:- SUCCESSFUL

With a direct &mut b as *mut bool as *mut u8 cast, the same program fails:

Failed Checks: Kani currently doesn't support checking validity of `mutable cast` for `*mut bool`

Cause

visit_rvalue in kani-compiler/src/kani_middle/transform/check_values.rs:575-634 only inspects
PtrToPtr casts whose destination is *mut U, and compares against the source pointee. The step
*mut bool -> *const u8 is skipped (destination not *mut), and *const u8 -> *mut u8 has a
source pointee with no validity requirements. A transmute of &mut bool to &mut u8 is not
treated as a mutable cast at all; only the destination type of a transmute is checked.

Possibly related: #2998 (validity-checks tracking issue), #3762 (validity after copy/copy_nonoverlapping writes; different mechanism).

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 theissue type on Nov 6, 2026
  5. added
    [C] Bug
    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