Skip to content

Constant subslice is modelled as the whole allocation #4971

Closed

Description

@srivatsansamraj

I tried this code:

const A: &[u8] = &[1, 2, 3, 4];
const S: &[u8] = A.split_at(1).1; // [2, 3, 4]
const T: &str = "abcd".split_at(2).1; // "cd"

#[kani::proof]
fn slice_len() {
    assert!(S.len() == 4);
}

#[kani::proof]
fn str_len() {
    assert!(T.len() == 4);
}

#[kani::proof]
fn slice_first() {
    assert!(S[0] == 1);
}

using the following command line invocation:

kani main.rs

with Kani version: 0.68.0, built from main at 1640445da

I expected to see this happen: all three harnesses fail. Compiled with rustc and run, S is [2, 3, 4] and T is "cd".

Instead, this happened: all three verify.

Complete - 3 successfully verified harnesses, 0 failures, 3 total.

The assertions that hold when the program runs (S.len() == 3, T.len() == 2, S[0] == 2) fail. Constants built with split_first or a slice pattern ([_, rest @ ..]) behave the same way. Slicing A at run time (&A[1..]) is correct.

The Slice arm of codegen_const_ptr (kani-compiler/src/codegen_cprover_gotoc/codegen/operand.rs) takes the length from the whole pointee allocation (data.bytes.len() divided by the element size) and points the data at the start of that allocation, so the pointer's offset into it and the slice's own length are not used. The str arm does the same with raw_bytes(). #3021 is an assertion failure in the same arm.

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

    T-High PriorityZ-Kani Compiler

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions