Skip to content

Commit 39c62dd

Browse files
committed
Check frame conditions on deallocating
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 6d86395 commit 39c62dd

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

regression/contracts/assigns_enforce_18/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ void foo(int *xp, int *xq, int a) __CPROVER_assigns(*xp)
1414
y = -1;
1515
}
1616

17-
void bar(int *a, int *b) __CPROVER_assigns(a, *b)
17+
void bar(int *a, int *b) __CPROVER_assigns(*a, *b)
1818
{
1919
free(a);
2020
*b = 0;
2121
}
2222

23+
void baz(int *a, int *c) __CPROVER_assigns(*a)
24+
{
25+
free(c);
26+
*a = 0;
27+
}
28+
2329
int main()
2430
{
2531
int z = 0;
@@ -31,5 +37,7 @@ int main()
3137
int b = 1;
3238
bar(a, &b);
3339
assert(b == 0);
40+
int *c = malloc(sizeof(*c));
41+
baz(&y, c);
3442
return 0;
3543
}

regression/contracts/assigns_enforce_18/test.desc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ main.c
33
--enforce-all-contracts _ --pointer-primitive-check
44
^EXIT=10$
55
^SIGNAL=0$
6+
^\[bar.\d+\] line \d+ Check that \*a is assignable: SUCCESS$
67
^\[bar.\d+\] line \d+ Check that \*b is assignable: SUCCESS$
8+
^\[baz.\d+\] line \d+ Check that \*c is assignable: FAILURE$
9+
^\[baz.\d+\] line \d+ Check that \*a is assignable: SUCCESS$
710
^\[foo.\d+\] line \d+ Check that a is assignable: SUCCESS$
811
^\[foo.\d+\] line \d+ Check that yp is assignable: SUCCESS$
912
^\[foo.\d+\] line \d+ Check that z is assignable: SUCCESS$
1013
^\[foo.\d+\] line \d+ Check that \*xp is assignable: SUCCESS$
1114
^\[foo.\d+\] line \d+ Check that y is assignable: FAILURE$
12-
^.* 1 of \d+ failed \(\d+ iteration.*\)$
15+
^.* 2 of \d+ failed \(\d+ iteration.*\)$
1316
^VERIFICATION FAILED$
1417
--
1518
^\[foo.\d+\] line \d+ Check that a is assignable: FAILURE$

src/goto-instrument/contracts/contracts.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,19 @@ void code_contractst::instrument_call_statement(
743743
}
744744
else if(called_name == "free")
745745
{
746-
assigns_clause.remove_from_local_write_set(dereference_exprt(
747-
to_typecast_expr(instruction_iterator->call_arguments().front()).op()));
746+
goto_programt alias_assertion;
747+
const exprt &lhs_dereference = dereference_exprt(
748+
to_typecast_expr(instruction_iterator->call_arguments().front()).op());
749+
alias_assertion.add(goto_programt::make_assertion(
750+
assigns_clause.generate_containment_check(lhs_dereference),
751+
instruction_iterator->source_location));
752+
alias_assertion.instructions.back().source_location.set_comment(
753+
"Check that " + from_expr(ns, lhs_dereference.id(), lhs_dereference) +
754+
" is assignable");
755+
assigns_clause.remove_from_local_write_set(lhs_dereference);
756+
assigns_clause.remove_from_global_write_set(lhs_dereference);
757+
insert_before_swap_and_advance(
758+
program, instruction_iterator, alias_assertion);
748759
return;
749760
}
750761

0 commit comments

Comments
 (0)