Skip to content
Open
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
40 changes: 22 additions & 18 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ impl GotocCtx<'_, '_> {
self.tcx.dcx().span_warn(rustc_internal::internal(self.tcx, span), msg);
}

/// `StorageLive(var_id)`: see the `StorageLive` and `StorageDead` case of
/// [GotocCtx::codegen_statement].
pub fn codegen_storage_live(&mut self, var_id: Local, location: Location) -> Stmt {
if !self.current_fn().is_address_taken_local(var_id) {
Stmt::skip(location)
} else {
let global_dead_object = cbmc::global_dead_object(&self.symbol_table);
Stmt::assign(
global_dead_object.clone(),
global_dead_object
.clone()
.eq(self
.codegen_local(var_id, location)
.address_of()
.cast_to(global_dead_object.typ().clone()))
.ternary(global_dead_object.typ().null(), global_dead_object),
location,
)
}
}

/// Generate Goto-C for MIR [Statement]s.
/// This does not cover all possible "statements" because MIR distinguishes between ordinary
/// statements and [Terminator]s, which can exclusively appear at the end of a basic block.
Expand Down Expand Up @@ -372,24 +393,7 @@ impl GotocCtx<'_, '_> {
// This approach will also work when there are multiple occurrences of StorageLive (or
// StorageDead) on a path, or across control-flow branches, and even when StorageDead
// occurs without a preceding StorageLive.
StatementKind::StorageLive(var_id) => {
if !self.current_fn().is_address_taken_local(*var_id) {
Stmt::skip(location)
} else {
let global_dead_object = cbmc::global_dead_object(&self.symbol_table);
Stmt::assign(
global_dead_object.clone(),
global_dead_object
.clone()
.eq(self
.codegen_local(*var_id, location)
.address_of()
.cast_to(global_dead_object.typ().clone()))
.ternary(global_dead_object.typ().null(), global_dead_object),
location,
)
}
}
StatementKind::StorageLive(var_id) => self.codegen_storage_live(*var_id, location),
StatementKind::StorageDead(var_id) => {
if !self.current_fn().is_address_taken_local(*var_id) {
Stmt::skip(location)
Expand Down
29 changes: 20 additions & 9 deletions kani-compiler/src/codegen_cprover_gotoc/overrides/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,25 @@ impl GotocHook for LoopInvariantRegister {
let mut stmt = Stmt::goto(bb_label(target.unwrap()), loc)
.with_loop_contracts(func_exp.call(fargs).cast_to(Type::CInteger(CIntType::Bool)));
let current_fn = gcx.current_fn().instance_stable();
let mut storage_live = Vec::new();
if let Some(mut assigns) = gcx.loop_modifies.remove(&(current_fn, instance)) {
// The user wrote a loop modifies clause, possibly without any target. Add the
// locals that the loop contract transformation makes live across iterations of
// this loop, which the user cannot name (without a user clause, CBMC infers the
// clause instead).
let generated =
gcx.transformer.generated_loop_modifies(current_fn, instance).to_vec();
// The storage of such a local can end in the loop body, which can make it the
// dead object (see `codegen_storage_live`) when the latch jumps back to the
// loop head, where the loop havocs it. It is dead there: mark its storage live,
// as its `StorageLive` in the body would. That is one step early: between here
// and the back edge CBMC only checks the invariant and the assigns clause, so an
// invariant that reads the local through a dangling pointer loses its dead-object
// check on this path. A pointer to the local that the loop writes must be in the
// write set, though, so it is havocked at the head, and the step case still flags
// the read (test `loop_modifies_returned_local_dangling`).
storage_live =
Comment thread
feliperodri marked this conversation as resolved.
Resolved
generated.iter().map(|local| gcx.codegen_storage_live(*local, loc)).collect();
Comment on lines +901 to +902

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pointer to such a local that the loop writes must itself be in the write set, so it is havocked at the head, and the step case still flags the read. 9510ceb adds loop_modifies_returned_local_dangling.rs: the invariant reads an added loop-body variable through a pointer after its storage ended, and Kani still reports dereference failure: dead object there. The comment above the StorageLive now says this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dangling-pointer test covers a pointer the loop names (&p in loop_modifies). Two cases it doesn't:

  1. The pointer is stored through a pre-loop pointer the user doesn't name, e.g. *q = &t where q is set before the loop and never written in it.
  2. The pointer is kept in a struct field or Option that the loop writes, but whose root local isn't in the clause.

Either one with the invariant reading through the pointer should fail with dead object. If it can't happen, a short comment saying why would settle it.

assigns.extend(generated.into_iter().map(|local| gcx.codegen_local(local, loc)));
if assigns.is_empty() {
// The clause has no target (e.g. `#[kani::loop_modifies()]`, or only targets
Expand All @@ -913,15 +925,14 @@ impl GotocHook for LoopInvariantRegister {

// Add `free(0)` to make sure the body of `free` won't be dropped to
// satisfy the requirement of DFCC.
Stmt::block(
vec![
BuiltinFn::Free
.call(vec![Expr::pointer_constant(0, Type::void_pointer())], loc)
.as_stmt(loc),
stmt,
],
loc,
)
let mut stmts = vec![
BuiltinFn::Free
.call(vec![Expr::pointer_constant(0, Type::void_pointer())], loc)
.as_stmt(loc),
];
stmts.extend(storage_live);
Comment thread
feliperodri marked this conversation as resolved.
Resolved
stmts.push(stmt);
Stmt::block(stmts, loc)
} else {
// When loop-contracts is not enabled, codegen
// assign_to = true
Expand Down
Loading
Loading