Skip to content

Commit 22ad623

Browse files
committed
Add goto-level loop-contract annotation
This commit allows us to parse and annotate loop contracts from JSON files to GOTO models. We first parse the JSON file to get the configuration. Function configurations consist of function id (regex) and a list of loop-contract configurations. Loop-contract configurations consist of: Loop number of the loop we are annotating to. Loop invariants as strings. (optional) Loop assigns as strings. (optional) Loop decreases as strings. (optional) A symbol map that map symbol names in the loop contracts strings to their name in the symbol table of the goto model. Loop contracts mangling consists of four steps. Construct a fake function with a fake loop that contains the loop contracts for parsing the contracts. Parse the fake function and extract the contracts as exprt. Substitute symbols in the extracted exprt with the symbols in the symbol table of the goto model according to the symbol map provided by the user. Typecheck all contract exprt. Annotate all contract exprt to the corresponding loop.
1 parent da48fba commit 22ad623

File tree

24 files changed

+753
-2
lines changed

24 files changed

+753
-2
lines changed

doc/man/goto-instrument.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ check and use loop contracts when provided
10101010
\fB\-loop\-contracts\-no\-unwind\fR
10111011
do not unwind transformed loops
10121012
.TP
1013+
\fB\-loop\-contract\-file\fR \fIfile\fR
1014+
annotate loop contracts from the file to the goto program
1015+
.TP
10131016
\fB\-\-replace\-call\-with\-contract\fR \fIfun\fR
10141017
replace calls to \fIfun\fR with \fIfun\fR's contract
10151018
.TP

regression/contracts-dfcc/dont_skip_cprover_prefixed_vars_fail/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ void foo()
66
for(int i = 10; i > 0; i--)
77
// clang-format off
88
__CPROVER_assigns(i)
9-
__CPROVER_loop_invariant(0 <= i && i <= 10)
109
__CPROVER_decreases(i)
1110
// clang-format on
1211
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"functions": [
3+
{
4+
"foo": [
5+
{
6+
"loop_id": "0",
7+
"assigns": "i",
8+
"invariants": "0 <= i && i <= 10",
9+
"decreases": "i",
10+
"symbol_map": "i,foo::1::1::i"
11+
}
12+
]
13+
}
14+
]
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
main.c
3+
--loop-contract-file test.json --dfcc main --apply-loop-contracts
4+
^\[foo.assigns.\d+\] .* Check that nondet_var is assignable: FAILURE$
5+
^\[foo.assigns.\d+\] .* Check that __VERIFIER_var is assignable: FAILURE$
6+
^\[foo.assigns.\d+\] .* Check that __CPROVER_var is assignable: FAILURE$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
^VERIFICATION FAILED$
10+
--
11+
--
12+
This test checks that program variables with special name prefixes
13+
__CPROVER_, __VERIFIER, or nondet do not escape assigns clause checking.
14+
Using loop contracts from the contract file.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"functions": [
3+
{
4+
"foo": [
5+
{
6+
"loop_id": "0",
7+
"assigns": "i,nondet_var, __VERIFIER_var, __CPROVER_var",
8+
"invariants": "0 <= i && i <= 10",
9+
"decreases": "i",
10+
"symbol_map": "i,foo::1::1::i;
11+
nondet_var,foo::1::nondet_var;
12+
__VERIFIER_var, foo::1::__VERIFIER_var;
13+
__CPROVER_var,foo::1::__CPROVER_var"
14+
}
15+
]
16+
}
17+
]
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CORE
2+
main.c
3+
--loop-contract-file test.json --dfcc main --apply-loop-contracts
4+
^\[foo.assigns.\d+\] .* Check that nondet_var is assignable: SUCCESS$
5+
^\[foo.assigns.\d+\] .* Check that __VERIFIER_var is assignable: SUCCESS$
6+
^\[foo.assigns.\d+\] .* Check that __CPROVER_var is assignable: SUCCESS$
7+
^EXIT=0$
8+
^SIGNAL=0$
9+
^VERIFICATION SUCCESSFUL$
10+
--
11+
--
12+
This test checks that when program variables names have special prefixes
13+
__CPROVER_, __VERIFIER, or nondet, adding them to the write set makes them
14+
assignable.
15+
Using loop contracts from the contract file.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"functions": [
3+
{
4+
"main": [
5+
{
6+
"loop_id": "0",
7+
"invariants": "x[0] == __CPROVER_loop_entry(x[0])",
8+
"symbol_map": "x,main::1::x"
9+
}
10+
]
11+
}
12+
]
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE dfcc-only
2+
main.c
3+
--loop-contract-file test.json --dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^Tracking history of index expressions is not supported yet\.
9+
--
10+
This test checks that `ID_index` expressions are allowed within history variables.
11+
Using loop contracts from the contract file.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
inline int memcmp(const void *s1, const void *s2, unsigned n)
2+
{
3+
int res = 0;
4+
const unsigned char *sc1 = s1, *sc2 = s2;
5+
for(; n != 0; n--)
6+
// clang-format off
7+
__CPROVER_loop_invariant(n <= __CPROVER_loop_entry(n))
8+
__CPROVER_loop_invariant(__CPROVER_same_object(sc1, __CPROVER_loop_entry(sc1)))
9+
__CPROVER_loop_invariant(__CPROVER_same_object(sc2, __CPROVER_loop_entry(sc2)))
10+
__CPROVER_loop_invariant(sc1 <= s1 + __CPROVER_loop_entry(n))
11+
__CPROVER_loop_invariant(sc2 <= s2 + __CPROVER_loop_entry(n))
12+
__CPROVER_loop_invariant(res == 0)
13+
__CPROVER_loop_invariant(sc1 -(const unsigned char*)s1 == sc2 -(const unsigned char*)s2
14+
&& sc1 -(const unsigned char*)s1== __CPROVER_loop_entry(n) - n)
15+
// clang-format on
16+
{
17+
res = (*sc1++) - (*sc2++);
18+
long d1 = sc1 - (const unsigned char *)s1;
19+
long d2 = sc2 - (const unsigned char *)s2;
20+
if(res != 0)
21+
return res;
22+
}
23+
return res;
24+
}
25+
26+
int main()
27+
{
28+
unsigned SIZE;
29+
__CPROVER_assume(1 < SIZE && SIZE < 65536);
30+
unsigned char *a = malloc(SIZE);
31+
unsigned char *b = malloc(SIZE);
32+
memcpy(b, a, SIZE);
33+
assert(memcmp(a, b, SIZE) == 0);
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--dfcc main --apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
This test checks that loop contracts work correctly on memcmp.

0 commit comments

Comments
 (0)