Skip to content

Commit d6fe178

Browse files
authored
Merge pull request #6279 from feliperodri/handle-tmp-dead
Skip checking frame conditions for local variables
2 parents b60259a + c305f90 commit d6fe178

File tree

16 files changed

+76
-40
lines changed

16 files changed

+76
-40
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
void foo(int *xp) __CPROVER_assigns(*xp)
2+
{
3+
{
4+
int y;
5+
y = 2;
6+
}
7+
int z = 3;
8+
*xp = 1;
9+
}
10+
11+
int main()
12+
{
13+
int *xp = malloc(sizeof(*xp));
14+
foo(xp);
15+
return 0;
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts _ --pointer-primitive-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Checks whether verification fails when enforcing a contract
10+
for functions, without assigns clauses, that modify an input.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <assert.h>
2+
3+
int x = 0;
4+
5+
void pure() __CPROVER_assigns()
6+
{
7+
int x;
8+
x++;
9+
}
10+
11+
int main()
12+
{
13+
pure();
14+
assert(x == 0);
15+
return 0;
16+
}
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+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.assertion.\d+\] line \d+ assertion x \=\= 0\: SUCCESS$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
--
10+
Checks whether verification correctly distincts local variables
11+
and global variables with same name when checking frame conditions.

regression/contracts/assigns_enforce_scoping_01/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ main.c
33
--enforce-all-contracts
44
^EXIT=10$
55
^SIGNAL=0$
6-
^\[f1.\d+\] line \d+ Check that f1\$\$1\$\$1\$\$b is assignable: SUCCESS$
76
^\[f1.\d+\] line \d+ Check that \*f1\$\$1\$\$1\$\$b is assignable: SUCCESS$
87
^\[f1.\d+\] line \d+ Check that \*b is assignable: FAILURE$
98
^VERIFICATION FAILED$
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#include <assert.h>
2+
#include <stdlib.h>
23

34
int y;
45
double z;
56

6-
void bar(char **c) __CPROVER_assigns(y, z, **c) __CPROVER_ensures(**c == 6)
7+
void bar(char *c) __CPROVER_assigns(y, z, *c) __CPROVER_ensures(*c == 6)
78
{
89
}
910

1011
int main()
1112
{
12-
char **b;
13+
char *b = malloc(sizeof(*b));
1314
bar(b);
14-
assert(**b == 6);
15+
assert(*b == 6);
1516

1617
return 0;
1718
}

regression/contracts/assigns_replace_03/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
--replace-all-calls-with-contracts
3+
--replace-all-calls-with-contracts _ --pointer-primitive-check
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

regression/contracts/assigns_type_checking_valid_cases/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ void foo5(struct buf buffer) __CPROVER_assigns(buffer)
4545
buffer.len = 0;
4646
}
4747

48-
void foo6(struct buf *buffer)
49-
__CPROVER_assigns(buffer->data, *(buffer->data), buffer->len)
48+
void foo6(struct buf *buffer) __CPROVER_assigns(buffer->data, buffer->len)
5049
{
5150
buffer->data = malloc(sizeof(*(buffer->data)));
5251
*(buffer->data) = 1;

regression/contracts/assigns_type_checking_valid_cases/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
CORE
22
main.c
3-
--enforce-all-contracts
3+
--enforce-all-contracts _ --pointer-primitive-check
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[foo1.\d+\] line \d+ Check that a is assignable: SUCCESS$
77
^\[foo10.\d+\] line \d+ Check that buffer\-\>len is assignable: SUCCESS$
8+
^\[foo10.\d+\] line \d+ Check that buffer\-\>aux\.allocated is assignable: SUCCESS$
89
^\[foo2.\d+\] line \d+ Check that b is assignable: SUCCESS$
910
^\[foo3.\d+\] line \d+ Check that b is assignable: SUCCESS$
1011
^\[foo3.\d+\] line \d+ Check that y is assignable: SUCCESS$
@@ -27,7 +28,6 @@ main.c
2728
^\[foo8.\d+\] line \d+ Check that array\[\(.* int\)7\] is assignable: SUCCESS$
2829
^\[foo8.\d+\] line \d+ Check that array\[\(.* int\)8\] is assignable: SUCCESS$
2930
^\[foo8.\d+\] line \d+ Check that array\[\(.* int\)9\] is assignable: SUCCESS$
30-
^\[foo9.\d+\] line \d+ Check that new_array is assignable: SUCCESS$
3131
^\[foo9.\d+\] line \d+ Check that array is assignable: SUCCESS$
3232
^VERIFICATION SUCCESSFUL$
3333
--

regression/contracts/function-calls-05/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ main.c
44
^EXIT=0$
55
^SIGNAL=0$
66
\[postcondition.\d+\] file main.c line \d+ Check ensures clause: SUCCESS
7-
\[foo.\d+\] line \d+ Check that sum is assignable: SUCCESS
87
\[main.assertion.\d+\] line \d+ assertion foo\(\&x, \&y\) \=\= 10: SUCCESS
98
^VERIFICATION SUCCESSFUL$
109
--

regression/contracts/history-pointer-enforce-10/test.desc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ main.c
77
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause\: SUCCESS$
88
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause\: SUCCESS$
99
^\[bar.\d+\] line \d+ Check that p\-\>y is assignable\: SUCCESS$
10-
^\[baz.\d+\] line \d+ Check that pp is assignable\: SUCCESS$
11-
^\[baz.\d+\] line \d+ Check that empty is assignable\: SUCCESS$
1210
^\[baz.\d+\] line \d+ Check that p is assignable\: SUCCESS$
1311
^\[baz.\d+\] line \d+ Check that p is assignable\: SUCCESS$
1412
^\[foo.\d+\] line \d+ Check that \*p\-\>y is assignable\: SUCCESS$

regression/contracts/quantifiers-exists-requires-enforce/test.desc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ main.c
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: SUCCESS$
7-
^\[f1.\d+\] line \d+ Check that found\_four is assignable: SUCCESS$
8-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
9-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
10-
^\[f1.\d+\] line \d+ Check that found\_four is assignable: SUCCESS$
117
^VERIFICATION SUCCESSFUL$
128
--
139
^warning: ignoring

regression/contracts/quantifiers-forall-ensures-enforce/test.desc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ main.c
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: SUCCESS$
7-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
8-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
97
^\[f1.\d+\] line \d+ Check that arr\[\(.*\)i\] is assignable: SUCCESS$
108
^VERIFICATION SUCCESSFUL$
119
--

regression/contracts/quantifiers-forall-requires-enforce/test.desc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ main.c
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: SUCCESS$
7-
^\[f1.\d+\] line \d+ Check that is\_identity is assignable: SUCCESS$
8-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
9-
^\[f1.\d+\] line \d+ Check that i is assignable: SUCCESS$
10-
^\[f1.\d+\] line \d+ Check that is\_identity is assignable: SUCCESS$
117
^VERIFICATION SUCCESSFUL$
128
--
139
^warning: ignoring

regression/contracts/test_array_memory_enforce/test.desc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ main.c
77
\[foo.\d+\] line \d+ Check that \*x is assignable: SUCCESS
88
\[foo.\d+\] line \d+ Check that x\[\(.* int\)5\] is assignable: SUCCESS
99
\[foo.\d+\] line \d+ Check that x\[\(.* int\)9\] is assignable: SUCCESS
10-
\[foo.\d+\] line \d+ Check that y is assignable: SUCCESS
1110
^VERIFICATION SUCCESSFUL$
1211
--
1312
--

src/goto-instrument/contracts/contracts.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void code_contractst::replace_old_parameter(
396396

397397
if(
398398
parameter.id() == ID_dereference || parameter.id() == ID_member ||
399-
parameter.id() == ID_symbol)
399+
parameter.id() == ID_symbol || parameter.id() == ID_ptrmember)
400400
{
401401
auto it = parameter2history.find(parameter);
402402

@@ -675,14 +675,19 @@ void code_contractst::instrument_assign_statement(
675675

676676
const exprt &lhs = instruction_iterator->assign_lhs();
677677

678-
goto_programt alias_assertion;
679-
alias_assertion.add(goto_programt::make_assertion(
680-
assigns_clause.alias_expression(lhs),
681-
instruction_iterator->source_location));
682-
alias_assertion.instructions.back().source_location.set_comment(
683-
"Check that " + from_expr(ns, lhs.id(), lhs) + " is assignable");
684-
insert_before_swap_and_advance(
685-
program, instruction_iterator, alias_assertion);
678+
if(
679+
freely_assignable_symbols.find(lhs.get(ID_identifier)) ==
680+
freely_assignable_symbols.end())
681+
{
682+
goto_programt alias_assertion;
683+
alias_assertion.add(goto_programt::make_assertion(
684+
assigns_clause.alias_expression(lhs),
685+
instruction_iterator->source_location));
686+
alias_assertion.instructions.back().source_location.set_comment(
687+
"Check that " + from_expr(ns, lhs.id(), lhs) + " is assignable");
688+
insert_before_swap_and_advance(
689+
program, instruction_iterator, alias_assertion);
690+
}
686691
}
687692

688693
void code_contractst::instrument_call_statement(
@@ -894,14 +899,6 @@ void code_contractst::check_frame_conditions(
894899

895900
// Create a list of variables that are okay to assign.
896901
std::set<irep_idt> freely_assignable_symbols;
897-
// Add all parameters that are not pointers to the freely assignable set
898-
for(code_typet::parametert param : type.parameters())
899-
{
900-
if(param.type().id() != ID_pointer)
901-
{
902-
freely_assignable_symbols.insert(param.get_identifier());
903-
}
904-
}
905902

906903
// Create temporary variables to hold the assigns
907904
// clause targets before they can be modified.
@@ -917,6 +914,7 @@ void code_contractst::check_frame_conditions(
917914
{
918915
if(instruction_it->is_decl())
919916
{
917+
// Local variables are always freely assignable
920918
freely_assignable_symbols.insert(
921919
instruction_it->get_decl().symbol().get_identifier());
922920

0 commit comments

Comments
 (0)