Skip to content

Commit 19adf79

Browse files
Fix issues with how we compute DST size (#3687)
This change fixes how we compute size of the object in our mem predicates, and provide user visible methods to try to retrieve the size of the object if known and valid (`checked_size_of_raw` and `checked_align_of_raw`. Fixes #3612 Fixes #3627 ## Call-outs To simplify this PR, I moved the following changes to their own PRs: 1. #3644 2. #3718 I also removed the fix for the intrinsics `size_of_val` and `align_of_val` from this PR, and I will create a follow up PR once this one is merged. --------- Co-authored-by: Carolyn Zech <cmzech@amazon.com>
1 parent 88e6eaf commit 19adf79

File tree

15 files changed

+840
-139
lines changed

15 files changed

+840
-139
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl GotocCtx<'_> {
456456
| TyKind::RigidTy(RigidTy::Dynamic(..)) => {
457457
inner_goto_expr.member("data", &self.symbol_table)
458458
}
459-
TyKind::RigidTy(RigidTy::Adt(..))
459+
TyKind::RigidTy(RigidTy::Adt(..)) | TyKind::RigidTy(RigidTy::Tuple(..))
460460
if self.is_unsized(inner_mir_typ_internal) =>
461461
{
462462
// in tests/kani/Strings/os_str_reduced.rs, we see

kani-compiler/src/codegen_cprover_gotoc/overrides/hooks.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,37 @@ impl GotocHook for Assert {
149149
}
150150
}
151151

152+
struct SafetyCheck;
153+
impl GotocHook for SafetyCheck {
154+
fn hook_applies(&self, _tcx: TyCtxt, _instance: Instance) -> bool {
155+
unreachable!("{UNEXPECTED_CALL}")
156+
}
157+
158+
fn handle(
159+
&self,
160+
gcx: &mut GotocCtx,
161+
_instance: Instance,
162+
mut fargs: Vec<Expr>,
163+
_assign_to: &Place,
164+
target: Option<BasicBlockIdx>,
165+
span: Span,
166+
) -> Stmt {
167+
assert_eq!(fargs.len(), 2);
168+
let cond = fargs.remove(0).cast_to(Type::bool());
169+
let msg = fargs.remove(0);
170+
let msg = gcx.extract_const_message(&msg).unwrap();
171+
let target = target.unwrap();
172+
let caller_loc = gcx.codegen_caller_span_stable(span);
173+
Stmt::block(
174+
vec![
175+
gcx.codegen_assert_assume(cond, PropertyClass::SafetyCheck, &msg, caller_loc),
176+
Stmt::goto(bb_label(target), caller_loc),
177+
],
178+
caller_loc,
179+
)
180+
}
181+
}
182+
152183
struct Check;
153184
impl GotocHook for Check {
154185
fn hook_applies(&self, _tcx: TyCtxt, _instance: Instance) -> bool {
@@ -619,13 +650,14 @@ impl GotocHook for LoopInvariantRegister {
619650
}
620651

621652
pub fn fn_hooks() -> GotocHooks {
622-
let kani_lib_hooks: [(KaniHook, Rc<dyn GotocHook>); 11] = [
623-
(KaniHook::Assert, Rc::new(Assert)),
653+
let kani_lib_hooks = [
654+
(KaniHook::Assert, Rc::new(Assert) as Rc<dyn GotocHook>),
624655
(KaniHook::Assume, Rc::new(Assume)),
625656
(KaniHook::Panic, Rc::new(Panic)),
626657
(KaniHook::Check, Rc::new(Check)),
627658
(KaniHook::Cover, Rc::new(Cover)),
628659
(KaniHook::AnyRaw, Rc::new(Nondet)),
660+
(KaniHook::SafetyCheck, Rc::new(SafetyCheck)),
629661
(KaniHook::IsAllocated, Rc::new(IsAllocated)),
630662
(KaniHook::PointerObject, Rc::new(PointerObject)),
631663
(KaniHook::PointerOffset, Rc::new(PointerOffset)),

kani-compiler/src/kani_middle/abi.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct LayoutOf {
1313
layout: LayoutShape,
1414
}
1515

16-
#[allow(dead_code)] // TODO: Remove this in https://github.com/model-checking/kani/pull/3687
1716
impl LayoutOf {
1817
/// Create the layout structure for the given type
1918
pub fn new(ty: Ty) -> LayoutOf {

kani-compiler/src/kani_middle/intrinsics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
//! This module contains a MIR pass that replaces some intrinsics by rust intrinsics models as
44
//! well as validation logic that can only be added during monomorphization.
5+
//!
6+
//! TODO: Move this code to `[crate::kani_middle::transform::RustcIntrinsicsPass]` since we can do
7+
//! proper error handling after monomorphization.
58
use rustc_index::IndexVec;
69
use rustc_middle::mir::{Body, Const as mirConst, ConstValue, Operand, TerminatorKind};
710
use rustc_middle::mir::{Local, LocalDecl};

kani-compiler/src/kani_middle/kani_functions.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub enum KaniFunction {
4444
pub enum KaniIntrinsic {
4545
#[strum(serialize = "AnyModifiesIntrinsic")]
4646
AnyModifies,
47+
#[strum(serialize = "CheckedAlignOfIntrinsic")]
48+
CheckedAlignOf,
49+
#[strum(serialize = "CheckedSizeOfIntrinsic")]
50+
CheckedSizeOf,
4751
#[strum(serialize = "IsInitializedIntrinsic")]
4852
IsInitialized,
4953
#[strum(serialize = "ValidValueIntrinsic")]
@@ -55,6 +59,8 @@ pub enum KaniIntrinsic {
5559
/// Kani models are Rust functions that model some runtime behavior used by Kani instrumentation.
5660
#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoStaticStr, EnumIter, EnumString, Hash)]
5761
pub enum KaniModel {
62+
#[strum(serialize = "AlignOfDynObjectModel")]
63+
AlignOfDynObject,
5864
#[strum(serialize = "AnyModel")]
5965
Any,
6066
#[strum(serialize = "CopyInitStateModel")]
@@ -85,6 +91,10 @@ pub enum KaniModel {
8591
SetSlicePtrInitialized,
8692
#[strum(serialize = "SetStrPtrInitializedModel")]
8793
SetStrPtrInitialized,
94+
#[strum(serialize = "SizeOfDynObjectModel")]
95+
SizeOfDynObject,
96+
#[strum(serialize = "SizeOfSliceObjectModel")]
97+
SizeOfSliceObject,
8898
#[strum(serialize = "StoreArgumentModel")]
8999
StoreArgument,
90100
#[strum(serialize = "WriteAnySliceModel")]
@@ -121,6 +131,8 @@ pub enum KaniHook {
121131
PointerObject,
122132
#[strum(serialize = "PointerOffsetHook")]
123133
PointerOffset,
134+
#[strum(serialize = "SafetyCheckHook")]
135+
SafetyCheck,
124136
#[strum(serialize = "UntrackedDerefHook")]
125137
UntrackedDeref,
126138
}

0 commit comments

Comments
 (0)