Skip to content

Audit all assertions that Kani emits and ensure we do not continue execution past a panic - #1428

Merged
tedinski merged 10 commits into
model-checking:mainfrom
tedinski:assertions-audit
Aug 18, 2022
Merged

tedinski merged 10 commits into
model-checking:mainfrom
tedinski:assertions-audit

Conversation

@tedinski

@tedinski tedinski commented Jul 29, 2022 •

Copy link
Copy Markdown
Contributor

Description of changes:

Audit all assertions that Kani generates, and ensure we consistently use cbmc's assert-assume pattern.

  1. Concentrates everything assertion/assumption/etc fully in assert.rs
  2. There are a few PropertyClasses that I'm not sure if we want necessarily. Out of scope for this PR however.

Resolved issues:

Call-outs:

Testing:

  • How is this change tested? existing tests, some of which required changes due to new behavior

  • Is this a refactor change? yes but also no since wrong-behaviors were identified

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@tedinski
tedinski requested a review from a team as a code owner July 29, 2022 21:05

@adpaco adpaco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wanted to give some feedback before you do the remaining work here.

My impression is that SafetyCheck was an early attempt to identify all the checks the Kani compiler introduces during code generation. In the case of intrinsics, you could replace most "special" cases with that. But we also check for arithmetic overflows, so when to use ArithmeticOverflow over SafetyCheck?

Assertion,
/// Another instrinsic check.
///
/// SPECIAL BEHAVIORL None TODO: Why should this exist?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Saw this one is being used for all checks in exact_div. In principle, if we decided to keep it, I think it should be used only for the cases where an exact division cannot be done. Other cases (division by zero, division does not overflow) are better represented by ArithmeticOverflow. At the same time, these are all being introduced by Kani, so we could get replace all of them with SafetyCheck.

ExpectFail,
/// Another instrinsic check.
///
/// SPECIAL BEHAVIOR: None TODO: Why should this exist?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is another instance of a check that we include to detect undefined behavior coming from intrinsics. I think that's the purpose of SafetyCheck (i.e., checks added by Kani to detect the violation of safety conditions), so we could use that instead.

@celinval celinval left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a few notes on the variants.

/// E.g.: User assertions, compiler invariant checks
/// Ordinary (Rust) assertions and panics.
///
/// SPECIAL BEHAVIOR: None, this is the default type of assertion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would like to note that these are checks that would fail in a concrete execution (i.e.: that is either introduced by the compiler or the user). The executable trace feature that @sanjit-bhat just merged relies on that to know which failures can be reproduced.

UnsupportedConstruct,
/// When Rust determines code is unreachable, this is the `assert(false)` we emit.
///
/// SPECIAL BEHAVIOR: None TODO: Why should this exist?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this one could be part of SanityCheck.

@tedinski

Copy link
Copy Markdown
Contributor Author

Here are my notes on the assertions audit.

  1. I believe it's complete. We should now be consistently asserting on things.
  2. I've decided the PropertyClass cleanup is out of scope for this PR. I have TODO notes in the comments towards that, but disentangling that is a cleanliness issue without significant impact at this point.

I chased assertion/assumption/etc related code up from the base types in cprover_bindings to ensure everything in Kani goes through assert.rs. Then I examined every use of things in assert.rs. Here's the new state of things:

BuiltinFn

  • Abort - dead code
  • Assert - dead code
  • CProverAssume - dead code
  • CProverCover - Used by Stmt::cover... which is dead code

StmtBody

  • Assert - Stmt::assert
  • Assume - Stmt::assume
  • ::assert - assert.rs codegen_* only
  • ::assert_false - assert.rs codegen_* only
  • ::assume - assert.rs codegen_* only
  • ::cover - dead code

assert.rs

  • codegen_assert - outside this file, used once: "expect_fail" hook (we don't yet have kani::check)
  • codegen_assume - outside this file, used once: "assume" hook
  • codegen_assert_assume - used in:
    • "assert" hook
    • TerminatorKind::assert
    • many intrinsics' checks
  • codegen_assert_assume_false - used in:
    • SafetyCheck: "unstable vtable comparison"
    • TerminatorKind::Unreachable
  • codegen_panic - used in "panic" hook only
  • codegen_fatal_error - used in intrinsics:
    • "abort"
    • "transmute uninhabited" SafetyCheck
    • Unsupported intrinsic
    • Safety checks for assert_inhabited _uninit_valid _zero_valid
  • codegen_cover (only used in below right now)
  • codegen_cover_loc - only used in:
    • "assert" hook
    • TerminatorKind::assert
  • codegen_unimplemented_expr - used in operand.rs, rvalue.rs
  • codegen_unimplemented_stmt - used in intrinsic.rs, rvalue.rs, statement.rs
  • codegen_mimic_unimplemented - used in functions we skip codegenning, and TerminatorKind::Resume and Abort (which we should never actually hit because we don't "unwind" on panics)
  • codegen_sanity - used in:
    • codegen_vtable_size
    • codegen_end_call
    • codegen_virtual_funcall

I think there's a followup task for some renaming that could be done here. codegen_assert should now be codegen_check and codegen_assert_assume can then take codegen_assert. But that can be for the future. I think it'd make this PR worse, as it'd confuse what's happening with some of the changes.

@tedinski tedinski changed the title Partial progress towards completing an assertions audit Audit all assertions that Kani emits and ensure we do not continue execution past a panic Aug 17, 2022
}

/// Generates a CBMC assumption.
pub fn codegen_assume(&self, cond: Expr, loc: Location) -> Stmt {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't use self? What's the value of this over just Stmt:assume? Is it that this keeps things regular?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes: Several of these don't actually use self but some could in the future. I thought about this and decided that it might be best to keep them all regular. (In fact, the weirdest one was introducing codegen_assume, but I felt that having NO references to the cprover_bindings assert/assume/etc stuff outside of this file was a consistency worth having...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could also just use pure functions without the self. I do agree that for consistency sake, we might as well not use Stmt directly.


// Since `cond` might have side effects, assign it to a temporary
// variable so that it's evaluated once, then assert and assume it
// TODO: I don't think `cond` can have side effects, this is MIR, it's going to be temps

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we just assert that?

pub fn is_side_effect(&self) -> bool {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This code bothered me, but it's a distraction from the point of this PR, but I couldn't help but shove a comment in there for later...

.as_stmt(Location::none());
let unimplemented = ctx.codegen_unimplemented_stmt(
format!("drop_in_place for {}", drop_sym_name).as_str(),
Location::none(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No location to use here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm fixing missing locations in some of the statement stuff, but apparently here is vtable generation we don't really have a location?

let body = self.codegen_mimic_unimplemented(
&readable_name,
loc,
// There actually are links to specific issues in `should_skip_current_fn`...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we keep a map from fn-name to url?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Make should_skip_current_fn

    fn should_skip_current_fn(&self) -> Option<&str> {
        let current_fn_name =
            with_no_trimmed_paths!(self.tcx.def_path_str(self.current_fn().instance().def_id()));
        // We cannot use self.current_fn().readable_name() because it gives the monomorphized type, which is more difficult to match on.
        // Ideally we should not rely on the pretty-printed name here. Tracked here: https://github.com/model-checking/kani/issues/1374
        match current_fn_name.as_str() {
            // https://github.com/model-checking/kani/issues/202
            "fmt::ArgumentV1::<'a>::as_usize" => Some("https://github.com/model-checking/kani/issues/202"),
           // ...
            _ => None,
        }
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought about sneaking this change in here but there are some other un-linked skips in there, and it seemed like too much to figure out now.

@tedinski

Copy link
Copy Markdown
Contributor Author

I opened a tracking issue for PropertyClass stuff: #1551

Propagated locations a bit more, fixed some comments.

@danielsn danielsn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A couple places where we could still use tracking issues, and some work for future PRs.
Otherwise LGTM.

@tedinski
tedinski merged commit ab0f2ad into model-checking:main Aug 18, 2022
@tedinski tedinski added this to the Create Kani Assess tool milestone Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

index out of bounds exceptions don't assume(false) Overflow checks should abort the execution Assertions audit

4 participants