Skip to content

Commit ef7e94d

Browse files
committed
Adds test to cover contracts and function pointers support
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent 98f56a4 commit ef7e94d

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <assert.h>
2+
#include <stddef.h>
3+
4+
int x = 0;
5+
6+
void foo(int *y)
7+
{
8+
*y = 1;
9+
}
10+
11+
int *baz()
12+
{
13+
return &x;
14+
}
15+
16+
void bar(void (*fun_ptr)(), int *x) __CPROVER_requires(fun_ptr != NULL)
17+
__CPROVER_requires(x != NULL) __CPROVER_assigns(*x) __CPROVER_ensures(*x == 1)
18+
{
19+
*(baz()) = 1;
20+
fun_ptr(x);
21+
}
22+
23+
int main()
24+
{
25+
assert(x == 0);
26+
void (*fun_ptr)() = foo;
27+
bar(fun_ptr, &x);
28+
assert(x == 1);
29+
return 0;
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.c
3+
--enforce-contract bar
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[postcondition.\d+\] file main.c line \d+ Check ensures clause: SUCCESS$
7+
^\[bar.\d+\] line \d+ Check that \*return\_value\_baz is assignable: SUCCESS$
8+
^\[foo.\d+\] line \d+ Check that \*y is assignable: SUCCESS$
9+
^VERIFICATION SUCCESSFUL$
10+
--
11+
Checks whether CBMC properly instrument functions with function pointers
12+
during contract enforcement.

src/goto-instrument/contracts/contracts.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -673,19 +673,8 @@ void code_contractst::instrument_call_statement(
673673
"The first argument of instrument_call_statement should always be "
674674
"a function call");
675675

676-
irep_idt called_function;
677-
if(instruction_it->call_function().id() == ID_dereference)
678-
{
679-
called_function =
680-
to_symbol_expr(
681-
to_dereference_expr(instruction_it->call_function()).pointer())
682-
.get_identifier();
683-
}
684-
else
685-
{
686-
called_function =
687-
to_symbol_expr(instruction_it->call_function()).get_identifier();
688-
}
676+
irep_idt called_function =
677+
to_symbol_expr(instruction_it->call_function()).get_identifier();
689678

690679
if(called_function == "malloc")
691680
{

0 commit comments

Comments
 (0)