Skip to content

Commit 9efca43

Browse files
Add option to generate function body to goto-instrument
1 parent 1e7f2bc commit 9efca43

File tree

19 files changed

+586
-2
lines changed

19 files changed

+586
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
3+
void should_not_be_replaced(void)
4+
{
5+
__CPROVER_assume(0);
6+
}
7+
8+
void should_be_generated(void);
9+
10+
int main(void)
11+
{
12+
int flag;
13+
int does_not_get_reached = 0;
14+
if(flag)
15+
{
16+
should_not_be_replaced();
17+
assert(does_not_get_reached);
18+
}
19+
should_be_generated();
20+
return 0;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--generate-function-body '(?!__).*' --replace-function-body-options assert-false
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
^\[main.assertion.1\] assertion does_not_get_reached: SUCCESS$
8+
^\[should_be_generated.assertion.1\] assertion FALSE: FAILURE$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
3+
void crashes_program(void);
4+
5+
int main(void)
6+
{
7+
int flag;
8+
if(flag)
9+
{
10+
crashes_program();
11+
assert(0);
12+
}
13+
return 0;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--replace-function-body crashes_program --replace-function-body-options assert-false-assume-false
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
^\[main.assertion.1\] assertion 0: SUCCESS$
8+
^\[crashes_program.assertion.1\] assertion FALSE: FAILURE$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
void do_not_call_this(void);
2+
3+
int main(void)
4+
{
5+
do_not_call_this();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--replace-function-body do_not_call_this --replace-function-body-options assert-false
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[do_not_call_this.assertion.1\] assertion FALSE: FAILURE$
7+
--
8+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <assert.h>
2+
3+
void will_not_return(void);
4+
5+
int main(void)
6+
{
7+
will_not_return();
8+
assert(0);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--replace-function-body will_not_return --replace-function-body-options assume-false
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
3+
int global = 10;
4+
const int constant_global = 10;
5+
6+
void touches_globals(void);
7+
8+
int main(void)
9+
{
10+
touches_globals();
11+
assert(global == 10);
12+
assert(constant_global == 10);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--replace-function-body touches_globals --replace-function-body-options 'havoc,globals:(?!__).*'
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
^\[main.assertion.1\] assertion global == 10: FAILURE$
8+
^\[main.assertion.2\] assertion constant_global == 10: SUCCESS$
9+
--
10+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
3+
void touches_parameter(int *param, const int *const_param);
4+
5+
int main(void)
6+
{
7+
int parameter = 10;
8+
int constant_parameter = 10;
9+
touches_parameter(&parameter, &constant_parameter);
10+
assert(parameter == 10);
11+
assert(constant_parameter == 10);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--replace-function-body touches_parameter --replace-function-body-options 'havoc,params:.*'
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
^\[main.assertion.1\] assertion parameter == 10: FAILURE$
8+
^\[main.assertion.2\] assertion constant_parameter == 10: SUCCESS$
9+
--
10+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <assert.h>
2+
3+
int this_returns_ten(void)
4+
{
5+
return 10;
6+
}
7+
8+
int main(void)
9+
{
10+
assert(this_returns_ten() == 10);
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--replace-function-body this_returns_ten --replace-function-body-options nondet-return
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
^warning: ignoring

src/goto-instrument/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ SRC = accelerate/accelerate.cpp \
6161
undefined_functions.cpp \
6262
uninitialized.cpp \
6363
unwind.cpp \
64+
replace_function_bodies.cpp \
6465
wmm/abstract_event.cpp \
6566
wmm/cycle_collection.cpp \
6667
wmm/data_dp.cpp \

src/goto-instrument/goto_instrument_parse_options.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Author: Daniel Kroening, [email protected]
100100
#include "undefined_functions.h"
101101
#include "remove_function.h"
102102
#include "splice_call.h"
103+
#include "replace_function_bodies.h"
103104

104105
void goto_instrument_parse_optionst::eval_verbosity()
105106
{
@@ -1448,6 +1449,30 @@ void goto_instrument_parse_optionst::instrument_goto_program()
14481449
throw 0;
14491450
}
14501451

1452+
if(
1453+
cmdline.isset("replace-function-body") ||
1454+
cmdline.isset("generate-function-body"))
1455+
{
1456+
if(
1457+
cmdline.isset("replace-function-body") &&
1458+
cmdline.isset("generate-function-body"))
1459+
{
1460+
throw "Can only use one of --replace-function-body or --generate-function-body";
1461+
}
1462+
bool only_generate = cmdline.isset("generate-function-body");
1463+
std::regex function_regex = std::regex(
1464+
only_generate ? cmdline.get_value("generate-function-body")
1465+
: cmdline.get_value("replace-function-body"));
1466+
status() << "Replacing function bodies" << eom;
1467+
replace_function_bodies(
1468+
goto_model.goto_functions,
1469+
goto_model.symbol_table,
1470+
function_regex,
1471+
only_generate,
1472+
cmdline.get_value("replace-function-body-options"),
1473+
*this);
1474+
}
1475+
14511476
// recalculate numbers, etc.
14521477
goto_model.goto_functions.update();
14531478
}
@@ -1522,9 +1547,21 @@ void goto_instrument_parse_optionst::help()
15221547
" --check-invariant function instruments invariant checking function\n"
15231548
" --remove-pointers converts pointer arithmetic to base+offset expressions\n" // NOLINT(*)
15241549
" --splice-call caller,callee prepends a call to callee in the body of caller\n" // NOLINT(*)
1550+
" --undefined-function-is-assume-false\n"
15251551
// NOLINTNEXTLINE(whitespace/line_length)
1526-
" --undefined-function-is-assume-false\n" // NOLINTNEXTLINE(whitespace/line_length)
15271552
" convert each call to an undefined function to assume(false)\n"
1553+
" --replace-function-body <regex>\n"
1554+
// NOLINTNEXTLINE(whitespace/line_length)
1555+
" Replace bodies of function matching regex\n"
1556+
" --generate-function-body <regex>\n"
1557+
// NOLINTNEXTLINE(whitespace/line_length)
1558+
" Like replace-function-body, but ignore functions that already have bodies\n"
1559+
" --replace-function-body-options <option>\n"
1560+
// NOLINTNEXTLINE(whitespace/line_length)
1561+
" One of empty, assert-false, assume-false, nondet-return\n"
1562+
" assert-false-assume-false and\n"
1563+
// NOLINTNEXTLINE(whitespace/line_length)
1564+
" havoc[,params:<regex>][,globals:<regex>]"
15281565
"\n"
15291566
"Loop transformations:\n"
15301567
" --k-induction <k> check loops with k-induction\n"

src/goto-instrument/goto_instrument_parse_options.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ Author: Daniel Kroening, [email protected]
8686
"(undefined-function-is-assume-false)" \
8787
"(remove-function-body):"\
8888
"(splice-call):" \
89-
OPT_REMOVE_CALLS_NO_BODY
89+
OPT_REMOVE_CALLS_NO_BODY \
90+
"(replace-function-body):" \
91+
"(generate-function-body):" \
92+
"(replace-function-body-options):"
93+
9094
// clang-format on
9195

9296
class goto_instrument_parse_optionst:

0 commit comments

Comments
 (0)