Repository navigation
Add hooks for handling pre and post conditions in function contracts. - #1511
Conversation
…gs for checking contracts and replacing functions with contracts
celinval
left a comment
There was a problem hiding this comment.
I have a question about usability. What will happen by default? Are we going to check the contracts or ignore them?
Do you have any documentation on how all the pieces fit together? What exactly happens when the user annotates their function with a contract annotation? A bit more context will help figuring out what you are trying to accomplish.
| span: Option<Span>, | ||
| ) -> Stmt { | ||
| assert_eq!(fargs.len(), 1); | ||
| let cond = fargs.remove(0).cast_to(Type::bool()); |
There was a problem hiding this comment.
I would suggest you to implement something like:
struct PostCondition {
// Should we apply this hook (i.e.: either enforce or check precondition).
enabled: bool,
}
impl<'tcx> GotocHook<'tcx> for Postcondition {
fn hook_applies(&self, tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
self.enabled && matches_function(tcx, instance, "KaniPostcondition")
}
fn handle (/*args*/) {
if tcx.queries.get_enforce_contracts() {
fargs.add(Expr::string_constant("Post-condition failed");
Assert::handle(tcx, instance, fargs, assign_to, target, span);
} else {
Assume::handle(tcx, instance, fargs, assign_to, target, span);
}
}
}There was a problem hiding this comment.
We want to convert the postcondition as follows -
- to an
assertwhen a function is being checked against its contract (calling Kani with the--enforce-contractsflag) - to an
assumeif the function is being replaced with its contract (calling Kani with the--replace-with-contractsflag) - To an empty statement, otherwise (calling Kani without any flags)
In this snippet, enabled must be set to true if tcx.queries.get_enforce_contracts() == true or tcx.queries.get_replace_with_contracts() == true. Where can I set this value? Also, how do I ensure that in the last case (that is call Kani normally without any contract checking/replacing flags) the inlined kani::postcondition gets converted to an empty line?
There was a problem hiding this comment.
I've refactored my code to use Assert.handle(..) and Assume.handle(..) within the same if-else statement for now.
|
|
||
| impl<'tcx> GotocHook<'tcx> for ReplaceFunctionBody { | ||
| fn hook_applies(&self, tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool { | ||
| matches_function(tcx, instance, "KaniReplaceFunctionBody") |
There was a problem hiding this comment.
I am not sure I understand how this hook would work. Who is calling this function? Note that the hooks get applied at a function call level not function body code generation.
There was a problem hiding this comment.
The macro expansion stage would call this function. The code would look something like this -
if kani::replace_function_body() {
let ret = kani::any();
} else {
let ret = .... //call body closure.
}
| // } else { | ||
| // // The function contract is being ignored in this case. | ||
| // Stmt::block(vec![Stmt::goto(tcx.current_fn().find_label(&target), loc)], loc) | ||
| // } |
There was a problem hiding this comment.
Did you commit commented code intentionally?
There was a problem hiding this comment.
Oops, didn't mean to do that. I've removed them now, thanks! 👍
celinval
left a comment
There was a problem hiding this comment.
Looks good! Just one comment. Please address before pushing.
| let target = target.unwrap(); | ||
| let pe = unwrap_or_return_codegen_unimplemented_stmt!(tcx, tcx.codegen_place(&assign_to)) | ||
| .goto_expr; | ||
| if tcx.queries.get_replace_with_contracts() { |
There was a problem hiding this comment.
maybe replace this by:
let value = if tcx.queries.get_replace_with_contracts() { Expr::c_true() } else { Expr::c_false() };
Stmt::block(
vec![
pe.assign(value, loc),
Stmt::goto(tcx.current_fn().find_label(&target), loc),
],
loc,
)
|
Thanks for reviewing, @celinval! ❤️ I've made the last change you suggested. |
…model-checking#1511) * Add hooks for handling preconditions and postconditions; Add kani flags for checking contracts and replacing functions with contracts * Rewrite using Assert, Assume * Remove comment * Refactor if-else statement Co-authored-by: Monal Narasimhamurthy <[email protected]>
Description of changes:
Background: During macro expansion (yet to be implemented),
#[kani::requires(x)]will get inlined askani::precondition(x)in the function body before the actual function body begins. Similarly,#[kani::ensures(x)]will get inlined askani::postcondition(x)at the end of the function body. The actual function body will get wrapped in an inner function that gets called and the corresponding return value is returned from the wrapper function.This change includes -
precondition(x)calls intoassertorassumestatements depending on whether a function contract is being checked or if a function is being replaced by its contract.postcondition(x)calls intoassertorassumestatements.--enforce-contractsto checks all function contracts.--replace-with-contractsfor replacing functions with their contracts, if a contract exists.Resolved issues:
Resolves #1501
Call-outs:
Testing:
How is this change tested?
By running Kani regression tests. Need to have end-to-end pipeline before adding additional test cases. Will not merge into main until we have these tests.
Is this a refactor change?
No
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.