generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 135
Loop Contracts Annotation for While-Loop #3151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
qinheping
merged 62 commits into
model-checking:main
from
qinheping:features/loop-contracts-annotation
Oct 15, 2024
Merged
Changes from 32 commits
Commits
Show all changes
62 commits
Select commit
Hold shift + click to select a range
0e49d50
Loop Contracts Annotation for While-Loop
qinheping a5cc42c
Bump dependencies and Kani's version to 0.50.0 (#3148)
celinval 537eefc
Upgrade toolchain to 2024-04-18 and improve toolchain workflow (#3149)
celinval 4806dac
Automatic toolchain upgrade to nightly-2024-04-19 (#3150)
github-actions[bot] 55e5f0d
Stabilize cover statement and update contracts RFC (#3091)
celinval 0e6c192
Automatic toolchain upgrade to nightly-2024-04-20 (#3154)
github-actions[bot] dd6e60f
Bump tests/perf/s2n-quic from `2d5e891` to `5f88e54` (#3140)
dependabot[bot] a542ede
Automatic cargo update to 2024-04-22 (#3157)
github-actions[bot] 3cf110c
Automatic toolchain upgrade to nightly-2024-04-21 (#3158)
github-actions[bot] 4be4214
Bump tests/perf/s2n-quic from `5f88e54` to `9730578` (#3159)
dependabot[bot] e5b0a2a
Fix cargo audit error (#3160)
jaisnan b244770
Fix cbmc-update CI job (#3156)
tautschnig ec29ffd
Automatic cargo update to 2024-04-29 (#3165)
github-actions[bot] 1371195
Bump tests/perf/s2n-quic from `9730578` to `1436af7` (#3166)
dependabot[bot] 5d64679
Do not assume that ZST-typed symbols refer to unique objects (#3134)
tautschnig 7bc0cb8
Fix copyright check for `expected` tests (#3170)
adpaco-aws df2c1fb
Remove kani::Arbitrary from the modifies contract instrumentation (#3…
feliperodri 5edab5d
Annotate loop contracts as statement expression
qinheping c8ecefe
Automatic cargo update to 2024-05-06 (#3172)
github-actions[bot] bda1f3c
Bump tests/perf/s2n-quic from `1436af7` to `6dd41e0` (#3174)
dependabot[bot] eaf5b42
Avoid unnecessary uses of Location::none() (#3173)
tautschnig cf5a8f9
Update Rust dependencies (#3175)
karkhaz 584a3de
Bump Kani version to 0.51.0 (#3176)
karkhaz 195c453
Merge branch 'main' into features/loop-contracts-annotation
qinheping 52878c5
Remove leftover code
qinheping 6547dcd
Remove unused import
qinheping 319a8b9
Fix format
qinheping 2569af0
Loop invariants should be operands of named subs
qinheping 36c7628
Merge branch 'main' into features/loop-contracts-annotation
qinheping 159e6ad
Allow cloned reachability checks
qinheping fbd17d6
Fix format
qinheping 1a81de2
Fix format
qinheping 3596dbe
Apply Adrian's comments
qinheping 5210107
Use with_loop_contracts
qinheping 54168fd
Fix tests
qinheping def6b97
Merge branch 'main' into features/loop-contracts-annotation
qinheping 64b66d3
Fix format
qinheping 0b05968
Refactor the loop contracts with closures
qinheping 3624655
Add missing copyright
qinheping 3a481a0
Merge branch 'main' into features/loop-contracts-annotation
qinheping 68554df
Use proc_macro_error2
qinheping 75a82d5
Provide locals to ty()
qinheping 20b8de0
Do loop contracts transformation only for harnesses
qinheping c82684e
Merge branch 'main' into features/loop-contracts-annotation
qinheping 90c1c5c
Move loop-contracts-hook and add more documentation
qinheping 608baeb
Fix format
qinheping 4617a22
Refactor to avoid violating borrow check
qinheping 4337947
Merge branch 'main' into features/loop-contracts-annotation
qinheping 077985d
Fix format
qinheping ab4f484
Fix format
qinheping 04492fb
Ignore loop contract macros when loop contracts disabled
qinheping 7920f91
Fix copyright
qinheping cf52ca9
Merge branch 'main' into features/loop-contracts-annotation
qinheping 52f94a8
Add checkks for unspport invariants
qinheping c912349
Merge branch 'main' into features/loop-contracts-annotation
qinheping aa31528
Code simplification
qinheping faf50e9
Add more tests
qinheping 3c1a64a
Use DFCC
qinheping 5cf2ec1
Merge branch 'main' into features/loop-contracts-annotation
qinheping f008223
Fix format
qinheping 8b90842
Merge branch 'main' into features/loop-contracts-annotation
qinheping b1ef46d
Add expected
qinheping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
140 changes: 140 additions & 0 deletions
140
kani-compiler/src/codegen_cprover_gotoc/context/loop_contracts_ctx.rs
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| use crate::codegen_cprover_gotoc::codegen::bb_label; | ||
| use cbmc::goto_program::{CIntType, Expr, Location, Stmt, StmtBody, Type}; | ||
| use stable_mir::mir::BasicBlockIdx; | ||
| use std::collections::HashSet; | ||
|
|
||
| pub struct LoopContractsCtx { | ||
| /// the GOTO block compiled from the corresponding loop invariants | ||
| invariants_block: Vec<Stmt>, | ||
| /// Which codegen state | ||
| stage: LoopContractsStage, | ||
| /// If enable loop contracts | ||
| loop_contracts_enabled: bool, | ||
| /// Seen basic block indexes. Used to decide if a jump is backward | ||
| seen_bbidx: HashSet<BasicBlockIdx>, | ||
| /// Current unused bbidx label | ||
| current_bbidx_label: Option<String>, | ||
| /// The lhs of evaluation of the loop invariant | ||
| loop_invariant_lhs: Option<Stmt>, | ||
| } | ||
|
|
||
| /// We define two states: | ||
| /// 1. loop invariants block | ||
| /// In this state, we push all codegen stmts into the invariant block. | ||
| /// We enter this state when codegen for `KaniLoopInvariantBegin`. | ||
| /// We exit this state when codegen for `KaniLoopInvariantEnd`. | ||
qinheping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// 2. loop latch block | ||
| /// In this state, we codegen a statement expression from the | ||
| /// invariant_block annotate the statement expression to the named sub | ||
| /// of the next backward jumping we codegen. | ||
| /// We enter this state when codegen for `KaniLoopInvariantEnd`. | ||
| /// We exit this state when codegen for the first backward jumping. | ||
| #[allow(dead_code)] | ||
qinheping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[derive(Debug, PartialEq)] | ||
| enum LoopContractsStage { | ||
| /// Codegen for user code as usual | ||
| UserCode, | ||
| /// Codegen for loop invariants | ||
| InvariantBlock, | ||
| /// Codegen for loop latch node | ||
| FindingLatchNode, | ||
| } | ||
|
|
||
| /// Constructor | ||
| impl LoopContractsCtx { | ||
| pub fn new(loop_contracts_enabled: bool) -> Self { | ||
| Self { | ||
| invariants_block: Vec::new(), | ||
| stage: LoopContractsStage::UserCode, | ||
| loop_contracts_enabled, | ||
| seen_bbidx: HashSet::new(), | ||
| current_bbidx_label: None, | ||
| loop_invariant_lhs: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Getters | ||
| impl LoopContractsCtx { | ||
| pub fn loop_contracts_enabled(&self) -> bool { | ||
| self.loop_contracts_enabled | ||
| } | ||
|
|
||
| /// decide if a GOTO with `target` is backward jump | ||
| pub fn is_loop_latch(&self, target: &BasicBlockIdx) -> bool { | ||
| self.stage == LoopContractsStage::FindingLatchNode && self.seen_bbidx.contains(target) | ||
| } | ||
| } | ||
|
|
||
| /// Setters | ||
| impl LoopContractsCtx { | ||
| /// Returns the current block as a statement expression. | ||
| /// Exit loop latch block. | ||
| pub fn extract_block(&mut self, loc: Location) -> Expr { | ||
| assert!(self.loop_invariant_lhs.is_some()); | ||
| self.stage = LoopContractsStage::UserCode; | ||
| self.invariants_block.push(self.loop_invariant_lhs.as_ref().unwrap().clone()); | ||
|
|
||
| // The first statement is the GOTO in the rhs of __kani_loop_invariant_begin() | ||
| // Ignore it | ||
| self.invariants_block.remove(0); | ||
|
|
||
| Expr::statement_expression( | ||
| std::mem::take(&mut self.invariants_block), | ||
| Type::CInteger(CIntType::Bool), | ||
| loc, | ||
| ) | ||
| .cast_to(Type::bool()) | ||
| .and(Expr::bool_true()) | ||
| } | ||
|
|
||
| /// Push the `s` onto the block if it is in the loop invariant block | ||
| /// and return `skip`. Otherwise, do nothing and return `s`. | ||
| pub fn push_onto_block(&mut self, s: Stmt) -> Stmt { | ||
| if self.stage == LoopContractsStage::InvariantBlock { | ||
| // Attach the lable to the first Stmt in that block and reset it. | ||
qinheping marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let to_push = if self.current_bbidx_label.is_none() { | ||
| s.clone() | ||
| } else { | ||
| s.clone().with_label(self.current_bbidx_label.clone().unwrap()) | ||
| }; | ||
| self.current_bbidx_label = None; | ||
|
|
||
| match s.body() { | ||
| StmtBody::Assign { lhs, rhs: _ } => { | ||
| let lhs_stmt = lhs.clone().as_stmt(*s.location()); | ||
| self.loop_invariant_lhs = Some(lhs_stmt.clone()); | ||
| self.invariants_block.push(to_push); | ||
| } | ||
| _ => { | ||
| self.invariants_block.push(to_push); | ||
| } | ||
| }; | ||
| Stmt::skip(*s.location()) | ||
| } else { | ||
| s | ||
| } | ||
| } | ||
|
|
||
| pub fn enter_loop_invariant_block(&mut self) { | ||
| assert!(self.invariants_block.is_empty()); | ||
| self.stage = LoopContractsStage::InvariantBlock; | ||
| } | ||
|
|
||
| pub fn exit_loop_invariant_block(&mut self) { | ||
| self.stage = LoopContractsStage::FindingLatchNode; | ||
| } | ||
|
|
||
| /// Enter a new function, reset the seen_bbidx set | ||
| pub fn enter_new_function(&mut self) { | ||
| self.seen_bbidx = HashSet::new() | ||
| } | ||
|
|
||
| pub fn add_new_seen_bbidx(&mut self, bbidx: BasicBlockIdx) { | ||
| self.seen_bbidx.insert(bbidx); | ||
| self.current_bbidx_label = Some(bb_label(bbidx)); | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| mod current_fn; | ||
| mod goto_ctx; | ||
| mod loop_contracts_ctx; | ||
| mod vtable_ctx; | ||
|
|
||
| pub use goto_ctx::GotocCtx; | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.