Skip to content

Commit b80eea6

Browse files
Add option to generate function body to goto-instrument
1 parent ed505fe commit b80eea6

File tree

19 files changed

+586
-2
lines changed

19 files changed

+586
-2
lines changed
Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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$
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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$
Lines changed: 6 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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
Lines changed: 9 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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
Lines changed: 13 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 10 additions & 0 deletions
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
Lines changed: 11 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Author: Daniel Kroening, [email protected]
9999
#include "undefined_functions.h"
100100
#include "remove_function.h"
101101
#include "splice_call.h"
102+
#include "replace_function_bodies.h"
102103

103104
void goto_instrument_parse_optionst::eval_verbosity()
104105
{
@@ -1447,6 +1448,30 @@ void goto_instrument_parse_optionst::instrument_goto_program()
14471448
throw 0;
14481449
}
14491450

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

src/goto-instrument/goto_instrument_parse_options.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ Author: Daniel Kroening, [email protected]
8484
"(show-threaded)(list-calls-args)(print-path-lengths)" \
8585
"(undefined-function-is-assume-false)" \
8686
"(remove-function-body):"\
87-
"(splice-call):"
87+
"(splice-call):" \
88+
"(replace-function-body):" \
89+
"(generate-function-body):" \
90+
"(replace-function-body-options):"
91+
8892
// clang-format on
8993

9094
class goto_instrument_parse_optionst:

0 commit comments

Comments
 (0)