-
Notifications
You must be signed in to change notification settings - Fork 274
CONTRACTS: Add goto-level loop-contract annotation #7788
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 1 commit into
diffblue:develop
from
qinheping:features/goto-level-loop-contracts
Jul 13, 2023
+834
−3
Merged
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
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
15 changes: 15 additions & 0 deletions
15
regression/contracts-dfcc/dont_skip_cprover_prefixed_vars_fail/test.json
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,15 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"foo": [ | ||
{ | ||
"loop_id": "0", | ||
"assigns": "i", | ||
"invariants": "0 <= i && i <= 10", | ||
"decreases": "i", | ||
"symbol_map": "i,foo::1::1::i" | ||
} | ||
] | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
regression/contracts-dfcc/dont_skip_cprover_prefixed_vars_fail/test_contracts_file.desc
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 | ||
--loop-contracts-file test.json --dfcc main --apply-loop-contracts | ||
^\[foo.assigns.\d+\] .* Check that nondet_var is assignable: FAILURE$ | ||
^\[foo.assigns.\d+\] .* Check that __VERIFIER_var is assignable: FAILURE$ | ||
^\[foo.assigns.\d+\] .* Check that __CPROVER_var is assignable: FAILURE$ | ||
^EXIT=10$ | ||
^SIGNAL=0$ | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This test checks that program variables with special name prefixes | ||
__CPROVER_, __VERIFIER, or nondet do not escape assigns clause checking. | ||
Using loop contracts from the contracts file. |
18 changes: 18 additions & 0 deletions
18
regression/contracts-dfcc/dont_skip_cprover_prefixed_vars_pass/test.json
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,18 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"foo": [ | ||
{ | ||
"loop_id": "0", | ||
"assigns": "i,nondet_var, __VERIFIER_var, __CPROVER_var", | ||
"invariants": "0 <= i && i <= 10", | ||
"decreases": "i", | ||
"symbol_map": "i,foo::1::1::i; | ||
nondet_var,foo::1::nondet_var; | ||
__VERIFIER_var, foo::1::__VERIFIER_var; | ||
__CPROVER_var,foo::1::__CPROVER_var" | ||
} | ||
] | ||
} | ||
] | ||
} |
15 changes: 15 additions & 0 deletions
15
regression/contracts-dfcc/dont_skip_cprover_prefixed_vars_pass/test_contracts_file.desc
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,15 @@ | ||
CORE | ||
main.c | ||
--loop-contracts-file test.json --dfcc main --apply-loop-contracts | ||
^\[foo.assigns.\d+\] .* Check that nondet_var is assignable: SUCCESS$ | ||
^\[foo.assigns.\d+\] .* Check that __VERIFIER_var is assignable: SUCCESS$ | ||
^\[foo.assigns.\d+\] .* Check that __CPROVER_var is assignable: SUCCESS$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test checks that when program variables names have special prefixes | ||
__CPROVER_, __VERIFIER, or nondet, adding them to the write set makes them | ||
assignable. | ||
Using loop contracts from the contracts file. |
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 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"main": [ | ||
{ | ||
"loop_id": "0", | ||
"invariants": "x[0] == __CPROVER_loop_entry(x[0])", | ||
"symbol_map": "x,main::1::x" | ||
} | ||
] | ||
} | ||
] | ||
} |
11 changes: 11 additions & 0 deletions
11
regression/contracts-dfcc/history-index/test_contracts_file.desc
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 dfcc-only | ||
main.c | ||
--loop-contracts-file test.json --dfcc main --apply-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
^Tracking history of index expressions is not supported yet\. | ||
-- | ||
This test checks that `ID_index` expressions are allowed within history variables. | ||
Using loop contracts from the contracts file. |
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,42 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
#define SIZE 8 | ||
|
||
struct blob | ||
{ | ||
char *data; | ||
}; | ||
|
||
void main() | ||
{ | ||
foo(); | ||
} | ||
|
||
void foo() | ||
{ | ||
struct blob *b = malloc(sizeof(struct blob)); | ||
b->data = malloc(SIZE); | ||
|
||
b->data[5] = 0; | ||
for(unsigned i = 0; i < SIZE; i++) | ||
// clang-format off | ||
// clang-format on | ||
{ | ||
b->data[i] = 1; | ||
} | ||
} | ||
|
||
void foo1() | ||
{ | ||
struct blob *b = malloc(sizeof(struct blob)); | ||
b->data = malloc(SIZE); | ||
|
||
b->data[5] = 0; | ||
for(unsigned i = 0; i < SIZE; i++) | ||
// clang-format off | ||
// clang-format on | ||
{ | ||
b->data[i] = 1; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
regression/contracts-dfcc/loop_contracts_file_fail/test_matching_more_than_one_function.desc
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,10 @@ | ||
CORE | ||
main.c | ||
--loop-contracts-file test_matching_more_than_one_function.json --dfcc main --apply-loop-contracts | ||
^function regex .* matches more than one function | ||
^EXIT=6$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
This test checks that loop contracts wrangler correctly error out | ||
when there is more than one matched function. |
13 changes: 13 additions & 0 deletions
13
regression/contracts-dfcc/loop_contracts_file_fail/test_matching_more_than_one_function.json
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 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"foo.*": [ | ||
{ | ||
"loop_id": "0", | ||
"invariants": "i <= 8", | ||
"symbol_map": "i, foo::1::1::i" | ||
} | ||
] | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
regression/contracts-dfcc/loop_contracts_file_fail/test_matching_no_function.desc
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,10 @@ | ||
CORE | ||
main.c | ||
--loop-contracts-file test_matching_no_function.json --dfcc main --apply-loop-contracts | ||
^function .* matches no function | ||
^EXIT=6$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
This test checks that loop contracts wrangler correctly error out | ||
when there is no matched function. |
13 changes: 13 additions & 0 deletions
13
regression/contracts-dfcc/loop_contracts_file_fail/test_matching_no_function.json
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 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"bar": [ | ||
{ | ||
"loop_id": "0", | ||
"invariants": "i <= 8", | ||
"symbol_map": "i, foo::1::1::i" | ||
} | ||
] | ||
} | ||
] | ||
} |
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,34 @@ | ||
inline int memcmp(const char *s1, const char *s2, unsigned n) | ||
{ | ||
int res = 0; | ||
const unsigned char *sc1 = s1, *sc2 = s2; | ||
for(; n != 0; n--) | ||
// clang-format off | ||
__CPROVER_loop_invariant(n <= __CPROVER_loop_entry(n)) | ||
__CPROVER_loop_invariant(__CPROVER_same_object(sc1, __CPROVER_loop_entry(sc1))) | ||
__CPROVER_loop_invariant(__CPROVER_same_object(sc2, __CPROVER_loop_entry(sc2))) | ||
__CPROVER_loop_invariant(sc1 <= s1 + __CPROVER_loop_entry(n)) | ||
__CPROVER_loop_invariant(sc2 <= s2 + __CPROVER_loop_entry(n)) | ||
__CPROVER_loop_invariant(res == 0) | ||
__CPROVER_loop_invariant(sc1 -(const unsigned char*)s1 == sc2 -(const unsigned char*)s2 | ||
&& sc1 -(const unsigned char*)s1== __CPROVER_loop_entry(n) - n) | ||
// clang-format on | ||
{ | ||
res = (*sc1++) - (*sc2++); | ||
long d1 = sc1 - (const unsigned char *)s1; | ||
long d2 = sc2 - (const unsigned char *)s2; | ||
if(res != 0) | ||
return res; | ||
} | ||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
unsigned SIZE; | ||
__CPROVER_assume(1 < SIZE && SIZE < 65536); | ||
unsigned char *a = malloc(SIZE); | ||
unsigned char *b = malloc(SIZE); | ||
memcpy(b, a, SIZE); | ||
assert(memcmp(a, b, SIZE) == 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 gcc-only | ||
main.c | ||
--dfcc main --apply-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test checks that loop contracts work correctly on memcmp. | ||
This test fails the CI job check-vs-2019-cmake-build-and-test with | ||
the following error. | ||
`Function '_errno' must exist in the model.` | ||
|
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,19 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"memcmp": [ | ||
{ | ||
"loop_id": "0", | ||
"invariants": "(n <= __CPROVER_loop_entry(n))&&(__CPROVER_same_object(sc1, __CPROVER_loop_entry(sc1)))&& | ||
(__CPROVER_same_object(sc2, __CPROVER_loop_entry(sc2)))&& | ||
(sc1 <= s1 + __CPROVER_loop_entry(n))&& | ||
(sc2 <= s2 + __CPROVER_loop_entry(n))&& | ||
(res == 0)&& | ||
(sc1 -(const unsigned char*)s1 == sc2 -(const unsigned char*)s2 | ||
&& sc1 -(const unsigned char*)s1== __CPROVER_loop_entry(n) - n)", | ||
"symbol_map": "n,memcmp::n; sc1,memcmp::1::sc1; sc2,memcmp::1::sc2; res,memcmp::1::res; s1, memcmp::s1; s2, memcmp::s2" | ||
} | ||
] | ||
} | ||
] | ||
} |
13 changes: 13 additions & 0 deletions
13
regression/contracts-dfcc/loop_contracts_memcmp/test_contracts_file.desc
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 gcc-only | ||
main.c | ||
--loop-contracts-file test.json --dfcc main --apply-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test checks that loop contracts work correctly on memcmp. | ||
Using loop contracts from the contracts file. | ||
This test fails the CI job check-vs-2019-cmake-build-and-test with | ||
the following error. | ||
`Function '_errno' must exist in the model.` |
14 changes: 14 additions & 0 deletions
14
regression/contracts-dfcc/variant_multi_instruction_loop_head/test.json
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 @@ | ||
{ | ||
"functions": [ | ||
{ | ||
"main": [ | ||
{ | ||
"loop_id": "0", | ||
"invariants": "0 <= *y && *y <= 10", | ||
"decreases": "10-x", | ||
"symbol_map": "y,main::1::y;x,main::1::x" | ||
} | ||
] | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
regression/contracts-dfcc/variant_multi_instruction_loop_head/test_contracts_file.desc
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,17 @@ | ||
CORE dfcc-only | ||
main.c | ||
--loop-contracts-file test.json --dfcc main --apply-loop-contracts | ||
^\[main.loop_assigns.\d+\] line 6 Check assigns clause inclusion for loop .*: SUCCESS$ | ||
^\[main.loop_invariant_base.\d+\] line 6 Check invariant before entry for loop .*: SUCCESS$ | ||
^\[main.loop_invariant_step.\d+\] line 6 Check invariant after step for loop .*: SUCCESS$ | ||
^\[main.loop_step_unwinding.\d+\] line 6 Check step was unwound for loop .*: SUCCESS$ | ||
^\[main.loop_decreases.\d+\] line 6 Check variant decreases after step for loop .*: SUCCESS$ | ||
^\[main.assigns.\d+\] line \d+ Check that x is assignable: SUCCESS$ | ||
^VERIFICATION SUCCESSFUL$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
-- | ||
This test checks that decreases clause is properly initialized | ||
when the loop head and loop invariant compilation emit several goto instructions. | ||
Using loop contracts from the contracts file. |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate as to why this test is GCC-specific?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use void pointer in this benchmark. Typechecking void pointer will trigger the error https://github.com/diffblue/cbmc/blob/develop/src/ansi-c/c_typecheck_expr.cpp#L4152 under VISUAL_STUDIO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just use a char pointer instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, using char pointers will lead to a bug of DFCC.
There is a function
_errno
in the symbol table but not in the function map. I will keep this test as gcc-only and open an issue to track this bug.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#7807