Skip to content

Commit e29180b

Browse files
Add option to non-det globals to goto-harness
Add ability to non-deterministically initialise globals in the function call harness generator. Co-authored-by: Fotis Koutoulakis <[email protected]> Co-authored-by: Hannes Steffenhagen <[email protected]>
1 parent 6cc190d commit e29180b

File tree

6 files changed

+58
-0
lines changed

6 files changed

+58
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int a_global;
2+
3+
void entry_function(void)
4+
{
5+
assert(a_global == 0);
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function entry_function
4+
^\[entry_function.assertion.1\] line \d+ assertion a_global == 0: SUCCESS$
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int a_global;
2+
3+
void entry_function(void)
4+
{
5+
assert(a_global == 0);
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
main.c
3+
--harness-type call-function --function entry_function --nondet-globals
4+
^\[entry_function.assertion.1\] line \d+ assertion a_global == 0: FAILURE$
5+
^VERIFICATION FAILED$
6+
^EXIT=10$
7+
^SIGNAL=0$

src/goto-harness/function_call_harness_generator.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ struct function_call_harness_generatort::implt
3030
irep_idt harness_function_name;
3131
symbol_tablet *symbol_table;
3232
goto_functionst *goto_functions;
33+
bool nondet_globals = false;
3334

3435
/// \see goto_harness_generatort::generate
3536
void generate(goto_modelt &goto_model, const irep_idt &harness_function_name);
37+
/// Iterate over the symbol table and generate initialisation code for
38+
/// globals into the function body.
39+
void generate_nondet_globals(code_blockt &function_body);
3640
/// Non-deterministically initialise the parameters of the entry function
3741
/// and insert function call to the passed code block.
3842
void setup_parameters_and_call_entry_function(code_blockt &function_body);
@@ -63,6 +67,10 @@ void function_call_harness_generatort::handle_option(
6367
{
6468
p_impl->function = require_exactly_one_value(option, values);
6569
}
70+
else if(option == FUNCTION_HARNESS_GENERATOR_NONDET_GLOBALS_OPT)
71+
{
72+
p_impl->nondet_globals = true;
73+
}
6674
else
6775
{
6876
throw invalid_command_line_argument_exceptiont{
@@ -120,10 +128,29 @@ void function_call_harness_generatort::implt::generate(
120128
// create body for the function
121129
code_blockt function_body{};
122130

131+
generate_nondet_globals(function_body);
123132
setup_parameters_and_call_entry_function(function_body);
124133
add_harness_function_to_goto_model(std::move(function_body));
125134
}
126135

136+
void function_call_harness_generatort::implt::generate_nondet_globals(
137+
code_blockt &function_body)
138+
{
139+
if(nondet_globals)
140+
{
141+
for(const auto &symbol_table_entry : *symbol_table)
142+
{
143+
const auto &symbol = symbol_table_entry.second;
144+
if(
145+
symbol.is_static_lifetime && symbol.is_lvalue &&
146+
!has_prefix(id2string(symbol.name), "__CPROVER_"))
147+
{
148+
generate_initialisation_code_for(function_body, symbol.symbol_expr());
149+
}
150+
}
151+
}
152+
}
153+
127154
void function_call_harness_generatort::implt::generate_initialisation_code_for(
128155
code_blockt &block,
129156
const exprt &lhs)

src/goto-harness/function_harness_generator_options.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ Author: Diffblue Ltd.
1010
#define CPROVER_GOTO_HARNESS_FUNCTION_HARNESS_GENERATOR_OPTIONS_H
1111

1212
#define FUNCTION_HARNESS_GENERATOR_FUNCTION_OPT "function"
13+
#define FUNCTION_HARNESS_GENERATOR_NONDET_GLOBALS_OPT "nondet-globals"
1314

1415
// clang-format off
1516
#define FUNCTION_HARNESS_GENERATOR_OPTIONS \
1617
"(" FUNCTION_HARNESS_GENERATOR_FUNCTION_OPT "):" \
18+
"(" FUNCTION_HARNESS_GENERATOR_NONDET_GLOBALS_OPT ")" \
19+
// FUNCTION_HARNESS_GENERATOR_OPTIONS
1720

1821
// clang-format on
1922

@@ -22,6 +25,8 @@ Author: Diffblue Ltd.
2225
"function harness generator (--harness-type call-function)\n\n" \
2326
"--" FUNCTION_HARNESS_GENERATOR_FUNCTION_OPT \
2427
" the function the harness should call\n" \
28+
"--" FUNCTION_HARNESS_GENERATOR_NONDET_GLOBALS_OPT \
29+
" set global variables to non-deterministic values in harness\n" \
2530
// FUNCTION_HARNESS_GENERATOR_HELP
2631

2732
// clang-format on

0 commit comments

Comments
 (0)