Skip to content

Commit 12edec0

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 1d8c9ea commit 12edec0

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
@@ -31,9 +31,13 @@ struct function_call_harness_generatort::implt
3131
irep_idt harness_function_name;
3232
symbol_tablet *symbol_table;
3333
goto_functionst *goto_functions;
34+
bool nondet_globals = false;
3435

3536
/// \see goto_harness_generatort::generate
3637
void generate(goto_modelt &goto_model, const irep_idt &harness_function_name);
38+
/// Iterate over the symbol table and generate initialisation code for
39+
/// globals into the function body.
40+
void generate_nondet_globals(code_blockt &function_body);
3741
/// Non-deterministically initialise the parameters of the entry function
3842
/// and insert function call to the passed code block.
3943
void setup_parameters_and_call_entry_function(code_blockt &function_body);
@@ -64,6 +68,10 @@ void function_call_harness_generatort::handle_option(
6468
{
6569
p_impl->function = require_exactly_one_value(option, values);
6670
}
71+
else if(option == FUNCTION_HARNESS_GENERATOR_NONDET_GLOBALS_OPT)
72+
{
73+
p_impl->nondet_globals = true;
74+
}
6775
else
6876
{
6977
throw invalid_command_line_argument_exceptiont{
@@ -121,10 +129,29 @@ void function_call_harness_generatort::implt::generate(
121129
// create body for the function
122130
code_blockt function_body{};
123131

132+
generate_nondet_globals(function_body);
124133
setup_parameters_and_call_entry_function(function_body);
125134
add_harness_function_to_goto_model(std::move(function_body));
126135
}
127136

137+
void function_call_harness_generatort::implt::generate_nondet_globals(
138+
code_blockt &function_body)
139+
{
140+
if(nondet_globals)
141+
{
142+
for(const auto &symbol_table_entry : *symbol_table)
143+
{
144+
const auto &symbol = symbol_table_entry.second;
145+
if(
146+
symbol.is_static_lifetime && symbol.is_lvalue &&
147+
!has_prefix(id2string(symbol.name), CPROVER_PREFIX))
148+
{
149+
generate_initialisation_code_for(function_body, symbol.symbol_expr());
150+
}
151+
}
152+
}
153+
}
154+
128155
void function_call_harness_generatort::implt::generate_initialisation_code_for(
129156
code_blockt &block,
130157
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)