Skip to content

Commit 8e8381c

Browse files
author
Aalok Thakkar
committed
Added support for loop_entry history variable.
1. Created a new predicate __CPROVER_loop_entry which can be used in the loop invariant to keep track of the value of a variable at the start of the loop. 2. Modified the code contract to support this history variable.
1 parent e4369b6 commit 8e8381c

File tree

18 files changed

+220
-26
lines changed

18 files changed

+220
-26
lines changed

regression/contracts/history-pointer-replace-03/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ main.c
33
--replace-all-calls-with-contracts
44
^EXIT=(1|64)$
55
^SIGNAL=0$
6+
^main.c.* error: __CPROVER_old expressions are not allowed in this context$
67
^CONVERSION ERROR$
7-
error: __CPROVER_old expressions are not allowed in __CPROVER_requires clauses
88
--
99
--
1010
Verification:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
typedef struct
5+
{
6+
int *n;
7+
} s;
8+
9+
void main()
10+
{
11+
int *x1, y1, z1;
12+
*x1 = &z1;
13+
14+
while(y1 > 0)
15+
__CPROVER_loop_invariant(*x1 == __CPROVER_loop_entry(*x1))
16+
{
17+
--y1;
18+
*x1 = *x1 + 1;
19+
*x1 = *x1 - 1;
20+
}
21+
assert(*x1 == &z1);
22+
23+
int x2, y2, z2;
24+
x2 = z2;
25+
26+
while(y2 > 0)
27+
__CPROVER_loop_invariant(x2 == __CPROVER_loop_entry(x2))
28+
{
29+
--y2;
30+
x2 = x2 + 1;
31+
x2 = x2 - 1;
32+
}
33+
assert(x2 == z2);
34+
35+
int y3;
36+
s *s1, *s2;
37+
s2->n = malloc(sizeof(int));
38+
s1->n = s2->n;
39+
40+
while(y3 > 0)
41+
__CPROVER_loop_invariant(*s1 == __CPROVER_loop_entry(*s1))
42+
{
43+
--y3;
44+
s1->n = s1->n + 1;
45+
s1->n = s1->n - 1;
46+
}
47+
48+
assert(s1->n == s2->n);
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assertion.1\] .* assertion \*x1 == &z1: SUCCESS$
9+
^\[main.3\] .* Check loop invariant before entry: SUCCESS$
10+
^\[main.4\] .* Check that loop invariant is preserved: SUCCESS$
11+
^\[main.assertion.2\] .* assertion x2 == z2: SUCCESS$
12+
^\[main.5\] .* Check loop invariant before entry: SUCCESS$
13+
^\[main.6\] .* Check that loop invariant is preserved: SUCCESS$
14+
^\[main.assertion.3\] .* assertion s1->n == s2->n: SUCCESS$
15+
^VERIFICATION SUCCESSFUL$
16+
--
17+
--
18+
This test checks that __CPROVER_loop_entry is supported.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
3+
void main()
4+
{
5+
int *x, y, z;
6+
7+
*x = &z;
8+
9+
while(y > 0)
10+
__CPROVER_loop_invariant(*x == __CPROVER_old(*x))
11+
{
12+
--y;
13+
*x = *x + 1;
14+
*x = *x - 1;
15+
}
16+
assert(*x == &z);
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^main.c.* error: __CPROVER_old expressions are not allowed in this context$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test ensures that __CPROVER_old cannot be used for loop contracts.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void foo(int *x) __CPROVER_assigns(*x)
2+
__CPROVER_ensures(*x == __CPROVER_loop_entry(*x) + 5)
3+
{
4+
*x = *x + 5;
5+
}
6+
7+
int main()
8+
{
9+
int n;
10+
foo(&n);
11+
12+
return 0;
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^main.c.* error: __CPROVER_loop_entry expressions are not allowed in this context$
5+
^CONVERSION ERROR$
6+
^EXIT=(1|64)$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test ensures that __CPROVER_loop_entry cannot be
11+
used for function contracts.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
3+
void main()
4+
{
5+
int x, y, z;
6+
x = z;
7+
8+
while(y > 0)
9+
__CPROVER_loop_invariant(x == __CPROVER_loop_entry(x))
10+
{
11+
--y;
12+
x = x + 1;
13+
x = x - 2;
14+
}
15+
assert(x == z);
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^EXIT=(10|6)$
5+
^SIGNAL=0$
6+
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.2\] .* Check that loop invariant is preserved: FAILURE$
8+
^\[main.assertion.1\] .* assertion x == z: SUCCESS$
9+
^VERIFICATION FAILED$
10+
--
11+
--
12+
This test checks that __CPROVER_loop_entry is sound.

src/ansi-c/c_expr.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ inline side_effect_expr_overflowt &to_side_effect_expr_overflow(exprt &expr)
202202

203203
/// \brief A class for an expression that indicates the pre-function-call
204204
/// value of an expression passed as a parameter to a function
205-
class old_exprt : public unary_exprt
205+
class history_exprt : public unary_exprt
206206
{
207207
public:
208-
explicit old_exprt(exprt variable) : unary_exprt(ID_old, std::move(variable))
208+
explicit history_exprt(exprt variable, const irep_idt &id)
209+
: unary_exprt(id, std::move(variable))
209210
{
210211
}
211212

@@ -215,10 +216,12 @@ class old_exprt : public unary_exprt
215216
}
216217
};
217218

218-
inline const old_exprt &to_old_expr(const exprt &expr)
219+
inline const history_exprt &
220+
to_history_expr(const exprt &expr, const irep_idt &id)
219221
{
220-
PRECONDITION(expr.id() == ID_old);
221-
auto &ret = static_cast<const old_exprt &>(expr);
222+
PRECONDITION(id == ID_old || id == ID_loop_entry);
223+
PRECONDITION(expr.id() == id);
224+
auto &ret = static_cast<const history_exprt &>(expr);
222225
validate_expr(ret);
223226
return ret;
224227
}

0 commit comments

Comments
 (0)