-
Notifications
You must be signed in to change notification settings - Fork 277
Enforce __CPROVER_loop_invariant contracts in goto-instrument #5884
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
feliperodri
merged 4 commits into
diffblue:develop
from
padhi-forks:loop_invariant_contract_fix
Mar 5, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4472173
Renamed contract application functions
SaswatPadhi 1b2cdc6
Minor refactoring and documentation changes
SaswatPadhi aede6f9
Enforce __CPROVER_loop_invariant contracts
SaswatPadhi 45b6d72
Improved regression tests for invariant contract
SaswatPadhi 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^\[main.1\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.assertion.1\] .* assertion r == 0: FAILURE$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
This test is expected to fail along the code path where r is an even integer | ||
before entering the loop. | ||
The program is simply incompatible with the desired property in this case -- | ||
there is no loop invariant that can establish the required assertion. |
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,25 @@ | ||
#include <assert.h> | ||
|
||
int main() | ||
{ | ||
int r; | ||
__CPROVER_assume(r >= 0); | ||
|
||
while(r > 0) | ||
__CPROVER_loop_invariant(r >= 0) | ||
{ | ||
--r; | ||
if(r <= 1) | ||
{ | ||
break; | ||
} | ||
else | ||
{ | ||
--r; | ||
} | ||
} | ||
|
||
assert(r == 0 || r == 1); | ||
|
||
return 0; | ||
} |
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,11 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.assertion.1\] .* assertion r == 0 || r == 1: SUCCESS$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
This test checks that conditionals and `break` are correctly handled. |
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,25 @@ | ||
#include <assert.h> | ||
|
||
int main() | ||
{ | ||
int r; | ||
__CPROVER_assume(r >= 0); | ||
|
||
while(r > 0) | ||
__CPROVER_loop_invariant(r >= 0) | ||
{ | ||
--r; | ||
if(r < 5) | ||
{ | ||
continue; | ||
} | ||
else | ||
{ | ||
--r; | ||
} | ||
} | ||
|
||
assert(r == 0); | ||
|
||
return 0; | ||
} |
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,11 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.assertion.1\] .* assertion r == 0: SUCCESS$ | ||
^VERIFICATION SUCCESSFUL$ | ||
SaswatPadhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- | ||
This test checks that conditionals and `continue` are correctly handled. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.assertion.1\] .* assertion 0 <= r && r < 5: SUCCESS$ | ||
^\[main.assertion.2\] .* assertion \*\(arr\[r\]\) == pivot: SUCCESS$ | ||
^\[main.assertion.3\] .* assertion 0 < r ==> arr0 <= pivot: SUCCESS$ | ||
^\[main.assertion.4\] .* assertion 1 < r ==> arr1 <= pivot: SUCCESS$ | ||
^\[main.assertion.5\] .* assertion 2 < r ==> arr2 <= pivot: SUCCESS$ | ||
^\[main.assertion.6\] .* assertion 3 < r ==> arr3 <= pivot: SUCCESS$ | ||
^\[main.assertion.7\] .* assertion 4 < r ==> arr4 <= pivot: SUCCESS$ | ||
^\[main.assertion.8\] .* assertion 0 > r ==> arr0 >= pivot: SUCCESS$ | ||
^\[main.assertion.9\] .* assertion 1 > r ==> arr1 >= pivot: SUCCESS$ | ||
^\[main.assertion.10\] .* assertion 2 > r ==> arr2 >= pivot: SUCCESS$ | ||
^\[main.assertion.11\] .* assertion 3 > r ==> arr3 >= pivot: SUCCESS$ | ||
^\[main.assertion.12\] .* assertion 4 > r ==> arr4 >= pivot: SUCCESS$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
This test checks the invariant contracts on a larger problem --- in this case, | ||
the partition function of quicksort, applied to a fixed-length array. | ||
This serves as a stop-gap test until issues to do with quantifiers and | ||
side-effects in loop invariants are fixed. |
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,23 @@ | ||
#include <assert.h> | ||
|
||
int main() | ||
{ | ||
int r, n, x, y; | ||
__CPROVER_assume(n > 0 && x == y); | ||
|
||
for(r = 0; r < n; ++r) | ||
__CPROVER_loop_invariant(0 <= r && r <= n && x == y + r) | ||
{ | ||
x++; | ||
} | ||
while(r > 0) | ||
__CPROVER_loop_invariant(r >= 0 && x == y + n + (n - r)) | ||
{ | ||
y--; | ||
r--; | ||
} | ||
|
||
assert(x == y + 2 * n); | ||
|
||
return 0; | ||
} |
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,13 @@ | ||
CORE | ||
main.c | ||
--enforce-all-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.1\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.3\] .* Check loop invariant before entry: SUCCESS$ | ||
^\[main.4\] .* Check that loop invariant is preserved: SUCCESS$ | ||
^\[main.assertion.1\] .* assertion x == y \+ 2 \* n: SUCCESS$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
This test checks that multiple loops and `for` loops are correctly handled. |
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.