Skip to content

Commit c5ec306

Browse files
authored
Comment and Refactor a few files (rust-lang#1398)
1 parent b154ec9 commit c5ec306

File tree

6 files changed

+301
-241
lines changed

6 files changed

+301
-241
lines changed

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

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright Kani Contributors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
//! This file contains the code that acts as a wrapper to create the new assert and related statements
4+
//! This module is the central location for handling assertions and assumptions in Kani.
5+
6+
use crate::codegen_cprover_gotoc::utils;
57
use crate::codegen_cprover_gotoc::GotocCtx;
6-
use cbmc::goto_program::{Expr, Location, Stmt};
8+
use cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt};
9+
use rustc_span::Span;
710
use std::convert::AsRef;
811
use strum_macros::{AsRefStr, EnumString};
912

@@ -39,6 +42,7 @@ impl PropertyClass {
3942
}
4043

4144
impl<'tcx> GotocCtx<'tcx> {
45+
/// Generates a CBMC assertion. Note: Does _NOT_ assume.
4246
pub fn codegen_assert(
4347
&self,
4448
cond: Expr,
@@ -51,6 +55,7 @@ impl<'tcx> GotocCtx<'tcx> {
5155
Stmt::assert(cond, property_name, message, loc)
5256
}
5357

58+
/// Generates a CBMC assertion, followed by an assumption of the same condition.
5459
pub fn codegen_assert_assume(
5560
&self,
5661
cond: Expr,
@@ -66,6 +71,7 @@ impl<'tcx> GotocCtx<'tcx> {
6671
)
6772
}
6873

74+
/// A shorthand for generating a CBMC assert-false. TODO: This should probably be eliminated!
6975
pub fn codegen_assert_false(
7076
&self,
7177
property_class: PropertyClass,
@@ -76,4 +82,49 @@ impl<'tcx> GotocCtx<'tcx> {
7682
let property_name = property_class.as_str();
7783
Stmt::assert_false(property_name, message, loc)
7884
}
85+
86+
/// Kani hooks function calls to `panic` and calls this intead.
87+
pub fn codegen_panic(&self, span: Option<Span>, fargs: Vec<Expr>) -> Stmt {
88+
// CBMC requires that the argument to the assertion must be a string constant.
89+
// If there is one in the MIR, use it; otherwise, explain that we can't.
90+
assert!(!fargs.is_empty(), "Panic requires a string message");
91+
let msg = utils::extract_const_message(&fargs[0]).unwrap_or(String::from(
92+
"This is a placeholder message; Kani doesn't support message formatted at runtime",
93+
));
94+
95+
self.codegen_fatal_error(PropertyClass::Assertion, &msg, span)
96+
}
97+
98+
/// Generate code for fatal error which should trigger an assertion failure and abort the
99+
/// execution.
100+
pub fn codegen_fatal_error(
101+
&self,
102+
property_class: PropertyClass,
103+
msg: &str,
104+
span: Option<Span>,
105+
) -> Stmt {
106+
let loc = self.codegen_caller_span(&span);
107+
Stmt::block(
108+
vec![
109+
self.codegen_assert_false(property_class, msg, loc),
110+
BuiltinFn::Abort.call(vec![], loc).as_stmt(loc),
111+
],
112+
loc,
113+
)
114+
}
115+
116+
/// Generate code to cover the given condition at the current location
117+
pub fn codegen_cover(&self, cond: Expr, msg: &str, span: Option<Span>) -> Stmt {
118+
let loc = self.codegen_caller_span(&span);
119+
// Should use Stmt::cover, but currently this doesn't work with CBMC
120+
// unless it is run with '--cover cover' (see
121+
// https://github.com/diffblue/cbmc/issues/6613). So for now use
122+
// assert(!cond).
123+
self.codegen_assert(cond.not(), PropertyClass::Cover, msg, loc)
124+
}
125+
126+
/// Generate code to cover the current location
127+
pub fn codegen_cover_loc(&self, msg: &str, span: Option<Span>) -> Stmt {
128+
self.codegen_cover(Expr::bool_true(), msg, span)
129+
}
79130
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// Copyright Kani Contributors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
//! This file contains functions related to codegenning MIR blocks into gotoc
5-
64
use crate::codegen_cprover_gotoc::GotocCtx;
75
use rustc_middle::mir::{BasicBlock, BasicBlockData};
86

97
impl<'tcx> GotocCtx<'tcx> {
8+
/// Generates Goto-C for a basic block.
9+
///
10+
/// A MIR basic block consists of 0 or more statements followed by a terminator.
11+
///
12+
/// This function does not return a value, but mutates state with
13+
/// `self.current_fn_mut().push_onto_block(...)`
1014
pub fn codegen_block(&mut self, bb: BasicBlock, bbd: &BasicBlockData<'tcx>) {
1115
self.current_fn_mut().set_current_bb(bb);
1216
let label: String = self.current_fn().find_label(&bb);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'tcx> GotocCtx<'tcx> {
2626
trace!(operand=?o, "codegen_operand");
2727
match o {
2828
Operand::Copy(d) | Operand::Move(d) =>
29-
// TODO: move shouldn't be the same as copy
29+
// TODO: move is an opportunity to poison/nondet the original memory.
3030
{
3131
let projection =
3232
unwrap_or_return_codegen_unimplemented!(self, self.codegen_place(d));

0 commit comments

Comments
 (0)