Skip to content

Commit c514b09

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 be17ef4 commit c514b09

File tree

19 files changed

+242
-18
lines changed

19 files changed

+242
-18
lines changed
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_loop_entry(*x))
11+
{
12+
--y;
13+
*x = *x + 1;
14+
*x = *x - 1;
15+
}
16+
assert(*x == &z);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
^VERIFICATION SUCCESSFUL$
9+
--
10+
--
11+
This test checks that pointer objects are supported for
12+
loop_entry history variable.
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--apply-loop-contracts
4+
^CONVERSION ERROR$
5+
^EXIT=1$
6+
^SIGNAL=0$
7+
--
8+
--
9+
This test ensures that __CPROVER_old cannot be used for loop contracts.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
oid foo(int *x) __CPROVER_assigns(*x)
2+
__CPROVER_ensures(*x == __CPROVER_old(*x) + 5)
3+
{
4+
*x = *x + 5;
5+
}
6+
7+
int main()
8+
{
9+
int n;
10+
foo(&n);
11+
12+
return 0;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts
4+
^PARSING ERROR$
5+
^EXIT=1$
6+
^SIGNAL=0$
7+
--
8+
--
9+
This test ensures that __CPROVER_loop_entry cannot be
10+
used for function contracts.
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_loop_entry(x))
11+
{
12+
--y;
13+
x = x + 1;
14+
x = x - 1;
15+
}
16+
assert(x == z);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
^VERIFICATION SUCCESSFUL$
9+
--
10+
--
11+
This test checks that struct objects are supported for
12+
loop_entry history variable.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <assert.h>
2+
3+
typedef struct
4+
{
5+
int *n;
6+
} s;
7+
8+
void main()
9+
{
10+
int y;
11+
12+
s *s1, *s2;
13+
s2->n = malloc(sizeof(int));
14+
s1->n = s2->n;
15+
16+
while(y > 0)
17+
__CPROVER_loop_invariant(*s1 == __CPROVER_loop_entry(*s1))
18+
{
19+
--y;
20+
s1->n = s1->n + 1;
21+
s1->n = s1->n - 1;
22+
}
23+
24+
assert(s1->n == s2->n);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
^VERIFICATION SUCCESSFUL$
9+
--
10+
--
11+
This test checks that struct objects are supported for
12+
loop_entry history variable.

src/ansi-c/c_expr.h

+8-5
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
}

src/ansi-c/c_typecheck_base.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ void c_typecheck_baset::typecheck_declaration(
731731
typecheck_expr(requires);
732732
implicit_typecast_bool(requires);
733733
disallow_history_variables(requires);
734+
disallow_loop_history_variables(requires);
734735
}
735736
}
736737

@@ -751,6 +752,7 @@ void c_typecheck_baset::typecheck_declaration(
751752
{
752753
typecheck_expr(ensures);
753754
implicit_typecast_bool(ensures);
755+
disallow_loop_history_variables(ensures);
754756
}
755757

756758
if(return_type.id() != ID_empty)

src/ansi-c/c_typecheck_base.h

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class c_typecheck_baset:
146146
virtual void typecheck_start_thread(codet &code);
147147
virtual void typecheck_spec_loop_invariant(codet &code);
148148
virtual void typecheck_spec_decreases(codet &code);
149+
virtual void disallow_function_history_variables(const exprt &expr) const;
149150

150151
bool break_is_allowed;
151152
bool continue_is_allowed;
@@ -207,6 +208,7 @@ class c_typecheck_baset:
207208
virtual exprt
208209
typecheck_shuffle_vector(const side_effect_expr_function_callt &expr);
209210
void disallow_history_variables(const exprt &) const;
211+
void disallow_loop_history_variables(const exprt &) const;
210212

211213
virtual void make_index_type(exprt &expr);
212214
virtual void make_constant(exprt &expr);

src/ansi-c/c_typecheck_code.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ void c_typecheck_baset::typecheck_spec_loop_invariant(codet &code)
820820
{
821821
typecheck_expr(invariant);
822822
implicit_typecast_bool(invariant);
823+
disallow_function_history_variables(invariant);
823824
}
824825
}
825826
}
@@ -836,3 +837,21 @@ void c_typecheck_baset::typecheck_spec_decreases(codet &code)
836837
}
837838
}
838839
}
840+
841+
void c_typecheck_baset::disallow_function_history_variables(
842+
const exprt &expr) const
843+
{
844+
for(auto &op : expr.operands())
845+
{
846+
disallow_function_history_variables(op);
847+
}
848+
849+
if(expr.id() == ID_old)
850+
{
851+
error().source_location = expr.source_location();
852+
error() << CPROVER_PREFIX
853+
"old expressions are not allowed in " CPROVER_PREFIX "invariant clauses"
854+
<< eom;
855+
throw 0;
856+
}
857+
}

src/ansi-c/c_typecheck_expr.cpp

+32-1
Original file line numberDiff line numberDiff line change
@@ -2704,7 +2704,21 @@ exprt c_typecheck_baset::do_special_functions(
27042704
throw 0;
27052705
}
27062706

2707-
old_exprt old_expr(expr.arguments()[0]);
2707+
history_exprt old_expr(expr.arguments()[0], ID_old);
2708+
old_expr.add_source_location() = source_location;
2709+
2710+
return std::move(old_expr);
2711+
}
2712+
else if(identifier == CPROVER_PREFIX "loop_entry")
2713+
{
2714+
if(expr.arguments().size() != 1)
2715+
{
2716+
error().source_location = f_op.source_location();
2717+
error() << identifier << " expects one operand" << eom;
2718+
throw 0;
2719+
}
2720+
2721+
history_exprt old_expr(expr.arguments()[0], ID_loop_entry);
27082722
old_expr.add_source_location() = source_location;
27092723

27102724
return std::move(old_expr);
@@ -3973,3 +3987,20 @@ void c_typecheck_baset::disallow_history_variables(const exprt &expr) const
39733987
throw 0;
39743988
}
39753989
}
3990+
3991+
void c_typecheck_baset::disallow_loop_history_variables(const exprt &expr) const
3992+
{
3993+
for(auto &op : expr.operands())
3994+
{
3995+
disallow_loop_history_variables(op);
3996+
}
3997+
3998+
if(expr.id() == ID_loop_entry)
3999+
{
4000+
error().source_location = expr.source_location();
4001+
error() << CPROVER_PREFIX
4002+
"loop_entry expressions are not allowed in function contracts"
4003+
<< eom;
4004+
throw 0;
4005+
}
4006+
}

src/ansi-c/cprover_builtin_headers.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void __CPROVER_fence(const char *kind, ...);
3737
// contract-related functions
3838
__CPROVER_bool __CPROVER_is_fresh(const void *mem, __CPROVER_size_t size);
3939
void __CPROVER_old(const void *);
40+
void __CPROVER_loop_entry(const void *);
4041

4142
// pointers
4243
__CPROVER_size_t __CPROVER_POINTER_OBJECT(const void *);

src/goto-instrument/contracts/contracts.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void code_contractst::check_apply_loop_contracts(
145145
// H: loop;
146146
// E: ...
147147
// to
148+
// process history variables;
148149
// H: assert(invariant);
149150
// havoc;
150151
// assume(invariant);
@@ -174,6 +175,22 @@ void code_contractst::check_apply_loop_contracts(
174175
return invariant_copy;
175176
};
176177

178+
// Process "loop_entry" history variables
179+
std::map<exprt, exprt> parameter2history;
180+
goto_programt history;
181+
182+
// Find and replace "loop_entry" expression in the "invariant" expression.
183+
replace_history_parameter(
184+
invariant,
185+
parameter2history,
186+
loop_head->source_location,
187+
mode,
188+
history,
189+
ID_loop_entry);
190+
191+
// Create 'loop_entry' history variables
192+
insert_before_swap_and_advance(goto_function.body, loop_head, history);
193+
177194
// Generate: assert(invariant) just before the loop
178195
// We use a block scope to create a temporary assertion,
179196
// and immediately convert it to goto instructions.
@@ -375,24 +392,26 @@ void code_contractst::add_quantified_variable(
375392
}
376393
}
377394

378-
void code_contractst::replace_old_parameter(
395+
void code_contractst::replace_history_parameter(
379396
exprt &expr,
380397
std::map<exprt, exprt> &parameter2history,
381398
source_locationt location,
382399
const irep_idt &mode,
383-
goto_programt &history)
400+
goto_programt &history,
401+
const irep_idt &id)
384402
{
385403
for(auto &op : expr.operands())
386404
{
387-
replace_old_parameter(op, parameter2history, location, mode, history);
405+
replace_history_parameter(
406+
op, parameter2history, location, mode, history, id);
388407
}
389408

390-
if(expr.id() == ID_old)
409+
if(expr.id() == ID_old || expr.id() == ID_loop_entry)
391410
{
392-
DATA_INVARIANT(
393-
expr.operands().size() == 1, CPROVER_PREFIX "old must have one operand");
411+
const auto &parameter = to_history_expr(expr, id).expression();
394412

395-
const auto &parameter = to_old_expr(expr).expression();
413+
DATA_INVARIANT(
414+
expr.operands().size() == 1, "A history variable must have one operand");
396415

397416
if(
398417
parameter.id() == ID_dereference || parameter.id() == ID_member ||
@@ -426,8 +445,8 @@ void code_contractst::replace_old_parameter(
426445
}
427446
else
428447
{
429-
log.error() << CPROVER_PREFIX "old does not currently support "
430-
<< parameter.id() << " expressions." << messaget::eom;
448+
log.error() << "History variables do not support " << parameter.id()
449+
<< " expressions." << messaget::eom;
431450
throw 0;
432451
}
433452
}
@@ -443,7 +462,8 @@ code_contractst::create_ensures_instruction(
443462
goto_programt history;
444463

445464
// Find and replace "old" expression in the "expression" variable
446-
replace_old_parameter(expression, parameter2history, location, mode, history);
465+
replace_history_parameter(
466+
expression, parameter2history, location, mode, history, ID_old);
447467

448468
// Create instructions corresponding to the ensures clause
449469
goto_programt ensures_program;

src/goto-instrument/contracts/contracts.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,13 @@ class code_contractst
219219

220220
/// This function recursively identifies the "old" expressions within expr
221221
/// and replaces them with correspoding history variables.
222-
void replace_old_parameter(
222+
void replace_history_parameter(
223223
exprt &expr,
224224
std::map<exprt, exprt> &parameter2history,
225225
source_locationt location,
226226
const irep_idt &mode,
227-
goto_programt &history);
227+
goto_programt &history,
228+
const irep_idt &id);
228229

229230
/// This function creates and returns an instruction that corresponds to the
230231
/// ensures clause. It also returns a list of instructions related to

0 commit comments

Comments
 (0)