Skip to content

Quantifier predicates that read restricted types through captured references are rejected under -Z valid-value-checks #5193

Description

@feliperodri

With -Z valid-value-checks, a quantifier predicate that reads a value of a restricted type (bool, char, a function pointer, ...) through a captured reference is rejected:

#[kani::proof]
fn reads_through_captured_reference() {
    let flags: [bool; 2] = kani::any();
    let flags = &flags;
    assert!(kani::forall!(|i in (0, 2)| flags[i] || !flags[i]));
}
error: quantifier predicates containing safety checks are not supported with -Z valid-value-checks or -Z uninit-checks

Why it happens. The fix for #3762 and #5190 (branch valid-value-untyped-writes, PR to come) stops checking untyped writes (write_bytes, copies, stores through a pointer to an unrestricted type) and checks the typed reads of those bytes instead, as the language specifies. A read through a pointer may read bytes written that way, so it gets a check. Quantifier predicates can't contain checks, so Kani rejects them. Before that change, these reads were never checked, which missed UB such as an invalid char copied in with ptr::copy and read back.

What still works. Values the closure captures by value or by move aren't checked, since a closure's environment is built by a typed aggregate. Reads of integers and other types without validity requirements aren't checked either.

What this blocks. Quantifiers over captured slices or arrays of restricted types under -Z valid-value-checks, which is the common way to state a property of every element.

Tests on that branch:

  • tests/ui/quantifier-safety-checks/captured_reference.rs records the rejection.
  • tests/ui/quantifier-safety-checks/indirect_call.rs now captures its function pointer by move, so it still reaches the "indirect function call" error it tests.

Possible directions:

  1. Hoist the check out of the predicate: before the quantifier, check that the memory the predicate reads has no untyped bytes. For a slice, that's a check over its range; the check itself can then be expressed with a quantifier.
  2. Leave predicates unchecked, and check the captured memory where the closure is created.
  3. Support the check inside the quantifier encoding, if CBMC's quantifier support allows a guarded validity test there.

Option 1 keeps the reads sound and seems the most direct. Input welcome on which fits the quantifier encoding best.

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

Metadata

Metadata

Assignees

Labels

Z-Quantifiers[E] Unsupported Construct

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions