Skip to content

-Z valid-value-checks: invalid enum discriminant not reported when the enum is not a scalar or scalar pair #5023

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

#[repr(C)]
struct Raw { a: u32, b: u32, c: u32 }

#[allow(dead_code)]
#[repr(u8)]
enum G { A(u32, u32), B } // tag at byte 0, size 12

#[cfg_attr(kani, kani::proof)]
fn aggregate_enum() {
    let raw = Raw { a: 7, b: 0, c: 0 };
    let g = unsafe { (&raw as *const Raw as *const G).read() }; // UB: tag 7
    std::mem::forget(g);
}
kani -Z valid-value-checks enum_tag.rs --harness aggregate_enum --exact

Expected behavior

Verification fails. The value read has tag 7, which is not a discriminant of G (Rust Reference,
[undefined.validity.enum]: "An enum must have a valid discriminant"). Kani's soundness page lists
"enum discriminants are in range" among the -Z valid-value-checks checks.

Actual behavior

 ** 0 of 6 failed
VERIFICATION:- SUCCESSFUL

The same read into #[repr(u8)] enum F { A(u8), B(u8) } (a ScalarPair layout) fails:

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

Cause

For a directly tagged enum, ty_validity_per_offset
(kani-compiler/src/kani_middle/transform/check_values.rs:1073-1097) takes the enum's requirement
from ValidValueReq::try_from_ty, which returns None for ValueRepr::Aggregate
(check_values.rs:222-226). The fields of G (u32) have no requirements either, so the result
is an empty list and nothing is checked. (If a field had requirements, the same code would report
"Unsupported Enum" instead.) The tag's scalar and offset are available from
VariantsShape::Multiple { tag, tag_field, .. } and could be used instead.

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