Skip to content

Commit ffcd2a8

Browse files
avanhattdanielsn
authored andcommitted
Separate RMC sanity check function (rust-lang#201)
Co-authored-by: Daniel Schwartz-Narbonne <[email protected]>
1 parent da7c325 commit ffcd2a8

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

compiler/rustc_codegen_llvm/src/gotoc/cbmc/goto_program/stmt.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ impl Stmt {
187187
Stmt::assert(Expr::bool_false(), msg, loc)
188188
}
189189

190+
/// A __CPROVER_assert to sanity check expected components of code
191+
/// generation. If users see these assertions fail, something in the
192+
/// translation to Gotoc has gone wrong, and we want them to file an issue.
193+
pub fn assert_sanity_check(expect_true: Expr, message: &str, url: &str, loc: Location) -> Stmt {
194+
let assert_msg =
195+
format!("Code generation sanity check: {}. Please report failures:\n{}", message, url);
196+
197+
Stmt::block(
198+
vec![
199+
// Assert our expected true expression.
200+
Stmt::assert(expect_true.clone(), &assert_msg, loc.clone()),
201+
// If expect_true is false, assume false to block any further
202+
// exploration of this path.
203+
Stmt::assume(expect_true, loc.clone()),
204+
],
205+
loc,
206+
)
207+
}
208+
190209
/// `__CPROVER_assume(cond);`
191210
pub fn assume(cond: Expr, loc: Location) -> Self {
192211
assert!(cond.typ().is_bool(), "Assume expected bool, got {:?}", cond);

compiler/rustc_codegen_llvm/src/gotoc/cbmc/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
//! Useful utilities for CBMC
44
5+
/// RMC bug report URL, for asserts/errors
6+
pub const BUG_REPORT_URL: &str =
7+
"https://github.com/model-checking/rmc/issues/new?template=bug_report.md";
8+
59
/// The aggregate name used in CBMC for aggregates of type `n`.
610
pub fn aggr_name(n: &str) -> String {
711
format!("tag-{}", n)

compiler/rustc_codegen_llvm/src/gotoc/rvalue.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
use super::cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Symbol, Type};
4-
use super::cbmc::utils::aggr_name;
4+
use super::cbmc::utils::{aggr_name, BUG_REPORT_URL};
55
use super::cbmc::MachineModel;
66
use super::metadata::*;
77
use super::typ::{is_pointer, pointee_type};
@@ -788,10 +788,9 @@ impl<'tcx> GotocCtx<'tcx> {
788788
let temp_var = self.gen_temp_variable(ty, Location::none()).to_expr();
789789
let decl = Stmt::decl(temp_var.clone(), None, Location::none());
790790
let check = Expr::eq(Expr::object_size(temp_var.address_of()), vt_size.clone());
791-
792-
// TODO: Add an rmc_sanity_check function https://github.com/model-checking/rmc/issues/200
793791
let assert_msg = format!("Correct CBMC vtable size for {:?}", operand_type.kind());
794-
let size_assert = Stmt::assert(check, &assert_msg, Location::none());
792+
let size_assert =
793+
Stmt::assert_sanity_check(check, &assert_msg, BUG_REPORT_URL, Location::none());
795794
Stmt::block(vec![decl, size_assert], Location::none())
796795
}
797796

0 commit comments

Comments
 (0)