forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
CONTRACTS: add doc for loop assigns inference #4
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -500,6 +500,79 @@ int foo() | |
| } | ||
| ``` | ||
|
|
||
| ## Loop Assigns Inference | ||
|
|
||
| When loop invariants clauses are specified but loop assigns are not, CBMC will infer | ||
| loop assigns clauses and use them to apply loop contracts. The idea of the inference | ||
| is to include all the left hand side of assignments in the loop. | ||
qinheping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| For example, in the loop in the following function, we assume that only the loop | ||
| invariants `i <= SIZE` is specified. Then CBMC will infer loop assigns targets `i`, `j` | ||
qinheping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| and `__CPROVER_object_whole(b)` for the loop. | ||
|
|
||
| ``` | ||
| int j; | ||
|
|
||
| void set_j(int i) | ||
| { | ||
| j = i; | ||
| } | ||
|
|
||
| void incr(int *n) | ||
| { | ||
| (*n)++; | ||
| } | ||
|
|
||
| void test_loop_assigns_inference() | ||
| { | ||
| int *b = malloc(SIZE * sizeof(int)); | ||
| for(unsigned i = 0; i < SIZE; incr(&i)) | ||
| // __CPROVER_assigns(i, j, __CPROVER_object_whole(b)) | ||
| __CPROVER_loop_invariant(i <= SIZE) | ||
| { | ||
| int k = i + 1; | ||
| set_j(i); | ||
| b[j] = 1; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The inference algorithm consist of three stages: | ||
| 1. function inlining, | ||
| 2. collecting assigns targets with local-may-alias analysis, | ||
| 3. assigns targets widening. | ||
|
|
||
| We do the function inlining first so that we can infer those assigns targets | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the body of a called unit is not available, but that unit does have contracts, and --replace-all-with-contract has been specified? If you can't inline what happens? Can you get the info you need from the contract of the called unit alone? |
||
| hidden in the function call, for example, `j` is assigned to in `set_j`. | ||
|
|
||
| Then we collect all targets of assignments in the loop after inlining. In the | ||
| `test_loop_assigns_inference` example, there are five assignments in the loop. | ||
| 1. `n = &i`, with assign target `n`. `n` will not be included in the inferred | ||
| set as it is a loop local. | ||
| 2. `*n++`, with assign target `*n`. We add `i` to the inferred set as `&i` | ||
| aliasing `n` according to the above assignment. | ||
| 3. `k = i + 1`, with assign target `k`. `k` is also a loop local, and will not | ||
| be added to the inferred set. | ||
| 4. `j = i` with assign target `j`. So we add `j` to the inferred set. | ||
| 5. `b[j] = 1` with assign target `b[j]`. So we add `b[j]` to the inferred set. | ||
|
|
||
| At last, we widen `b[j]` to `__CPROVER_object_whole(b)` as its index `j` is | ||
| non constant. | ||
|
|
||
| ### Limitation | ||
|
|
||
| The main limitation of the inference algorithm is that the local-may-alias | ||
| analysis we use is field insensitive, hence it is inaccurate in the cases | ||
| with struct fields. | ||
|
|
||
| As an example, for assignment `ptr = box.ptr`, we cannot determine that `ptr` | ||
| aliases `box.ptr`. And hence if we later write to `*ptr`, we will fail to | ||
| infer the assigns target `__CPROVER_object_whole(box.ptr)`. | ||
|
|
||
| However, the failed inference not result in unsound result. | ||
qinheping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| That is, CBMC will report assignability-checks failure when the inferred | ||
| assigns clauses are not accurate. | ||
|
|
||
| ## Additional Resources | ||
|
|
||
| - @ref contracts-functions | ||
|
|
||
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.