-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loop Contracts Annotation for While-Loop (#3151)
This PR introduce the loop contracts annotation for while-loops using proc-macro. A while loop of the form ``` rust #[kani::loop_invariant(inv)] while guard { body } ``` is annotated as ``` rust #[inline(never)] #[kanitool::fn_marker = "kani_register_loop_contract"] const fn kani_register_loop_contract_id<T, F: FnOnce() -> T>(f: F) -> T { unreachable!() } while kani_register_loop_contract_id(|| -> bool {inv}) && guard { loop_body; } ``` We then replace the function body of the register function `kani_register_loop_contract_id` by a call to its function argument `f`. In the loop-contract transformation, we move the calls to register functions to a new basic block as new loop latches and redirect all loop latches (for loops containing `continue` there can be multiple latches pointing to the same loop head) to the new loop latches to make sure that 1. each loop contain only one loop latch; 2. the terminator of the loop latch is a call to the register function. In detail, we transform ```ignore loop_head_block: { loop_head_stmts _v = kani_register_loop_contract(move args) -> [return: next_idx]; } ... loop_body_blocks ... ori_loop_latch_block: { loop_latch_stmts goto -> loop_head_block; } ``` to blocks ```ignore // loop head block loop_head_block: { _v = true goto -> next_idx } ... loop_body_blocks ... ori_loop_latch_block: { loop_latch_stmts goto -> new_loop_latch_block; } new_loop_latch_block: { _v = kani_register_loop_contract(move args) -> [return: next_idx]; } ``` The register functions will be transformed to ```ignore bb0: { _0 = closure@fn as std::ops::FnOnce::call_once(move _1, ()) -> [return: bb1, unwind unreachable]; } bb1: { return; } ``` At the end, the call to the register function will be codegened as backward goto with loop contracts annotated. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Felipe R. Monteiro <[email protected]> Co-authored-by: Celina G. Val <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jaisurya Nanduri <[email protected]> Co-authored-by: Michael Tautschnig <[email protected]> Co-authored-by: Adrian Palacios <[email protected]> Co-authored-by: Zyad Hassan <[email protected]> Co-authored-by: tautschnig <[email protected]> Co-authored-by: Felipe R. Monteiro <[email protected]> Co-authored-by: Kareem Khazem <[email protected]>
- Loading branch information
1 parent
4558e39
commit 056d4bc
Showing
26 changed files
with
924 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.