Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,11 @@ impl GotocCtx<'_, '_> {
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
self.codegen_constant_index(before, *offset, *min_length, *from_end)
}
// Best effort to codegen subslice projection.
// Full support to be added in
// https://github.com/model-checking/kani/issues/707
ProjectionElem::Subslice { from, to, from_end } => {
// https://rust-lang.github.io/rfcs/2359-subslice-pattern-syntax.html
match before.mir_typ().kind() {
// Sub-array binding is not supported yet; full support to be added in
// https://github.com/model-checking/kani/issues/707
TyKind::RigidTy(RigidTy::Array(ty, len)) => {
let len = len.eval_target_usize().unwrap();
let subarray_len = if *from_end {
Expand Down Expand Up @@ -582,10 +581,13 @@ impl GotocCtx<'_, '_> {
let index = Expr::int_constant(*from, Type::ssize_t());
let from_elem = before.goto_expr.index(index);
let data = from_elem.address_of();
let fat_ptr = slice_fat_ptr(goto_type, data, len, &self.symbol_table);
let fat_ptr =
slice_fat_ptr(goto_type, data.clone(), len, &self.symbol_table);
// As after a `Deref` of a slice fat pointer, the place is the slice itself
// (its data pointer), since runtime MIR can project further into it.
ProjectedPlace::try_new(
fat_ptr.clone(),
TypeOrVariant::Type(ptr_typ),
data,
TypeOrVariant::Type(typ),
Some(fat_ptr),
Some(ptr_typ),
self,
Expand Down
71 changes: 71 additions & 0 deletions tests/kani/SubSlice/projection_after_subslice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
// compile-flags: -Zmir-opt-level=2

//! Test projections applied to a subslice bound by a slice pattern. With `-Zmir-opt-level=2`,
//! rustc folds the subslice reference into the place that uses it, e.g. `(*_1)[1:][0 of 1]`,
//! which used to crash Kani. See https://github.com/model-checking/kani/issues/2682.

/// `ConstantIndex` after `Subslice`.
fn tail_first(x: &[u8]) -> Option<u8> {
match x {
[_, tail @ ..] if !tail.is_empty() => Some(tail[0]),
_ => None,
}
}

/// `ConstantIndex { from_end: true }` after `Subslice`.
fn tail_last(x: &[u8]) -> Option<u8> {
match x {
[_, tail @ ..] => match tail {
[.., last] => Some(*last),
[] => None,
},
[] => None,
}
}

/// `Index` after `Subslice`.
fn tail_at(x: &[u8], i: usize) -> Option<u8> {
match x {
[_, tail @ ..] if i < tail.len() => Some(tail[i]),
_ => None,
}
}

/// `Subslice` after `Subslice`: the inner subslice's length and first element.
fn tail_tail(x: &[u8]) -> (usize, Option<u8>) {
match x {
[_, tail @ ..] => match tail {
[_, rest @ .., _] => (rest.len(), rest.first().copied()),
_ => (0, None),
},
[] => (0, None),
}
}

#[kani::proof]
fn check_subslice_projections() {
let arr: [u8; 4] = kani::any();
let len: usize = kani::any_where(|l| *l <= 4);
let x = &arr[..len];
let i: usize = kani::any();
assert_eq!(tail_first(x), if len >= 2 { Some(arr[1]) } else { None });
assert_eq!(tail_last(x), if len >= 2 { Some(arr[len - 1]) } else { None });
assert_eq!(tail_at(x, i), if len >= 1 && i < len - 1 { Some(arr[i + 1]) } else { None });
let rest_first = if len >= 4 { Some(arr[2]) } else { None };
assert_eq!(tail_tail(x), if len >= 3 { (len - 3, rest_first) } else { (0, None) });
}

/// The reproducer from the issue.
#[kani::proof]
fn check_issue_2682() {
let x: &[isize] = &[1, 2, 3, 4, 5];
if !x.is_empty() {
let el = match x {
&[1, ref tail @ ..] => &tail[0],
_ => unreachable!(),
};
assert_eq!(*el, 2);
}
}
28 changes: 28 additions & 0 deletions tests/kani/SubSlice/subslice_dst_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Test a reference to a subslice of the trailing slice field of an unsized struct. The subslice
//! place used to be represented by its fat pointer, and taking a reference to it made codegen
//! panic with "Can't take address of Expr".

pub struct Wrapper<T: ?Sized> {
pub n: u8,
pub data: T,
}

fn data_tail(w: &Wrapper<[u8]>) -> &[u8] {
match w.data {
[_, ref tail @ ..] => tail,
[] => &[],
}
}

#[kani::proof]
fn check_dst_field_subslice() {
let concrete: Wrapper<[u8; 3]> = Wrapper { n: 0, data: kani::any() };
let w: &Wrapper<[u8]> = &concrete;
let tail = data_tail(w);
assert_eq!(tail.len(), 2);
assert_eq!(tail[0], concrete.data[1]);
assert_eq!(tail[1], concrete.data[2]);
}
Loading