Skip to content

Commit cb20917

Browse files
Merge pull request #5087 from xbauch/feature/better-harness
Improve harness generated code readability
2 parents 7a70b13 + 9ca0824 commit cb20917

File tree

6 files changed

+748
-352
lines changed

6 files changed

+748
-352
lines changed

regression/goto-harness/pointer-to-array-function-parameters-multi-arg-wrong/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ test.c
33
--harness-type call-function --function is_prefix_of --treat-pointer-as-array string --treat-pointer-as-array prefix --associated-array-size string:string_size --associated-array-size prefix:prefix_size --max-array-size 5
44
^EXIT=10$
55
^SIGNAL=0$
6-
\[is_prefix_of.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside object bounds in prefix\[\(signed( long)* int\)ix\]: FAILURE
6+
\[is_prefix_of.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside dynamic object bounds in prefix\[\(signed( long)* int\)ix\]: FAILURE
77
VERIFICATION FAILED
88
--
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
test.c
33
--harness-type call-function --function test --treat-pointer-as-array arr
4-
\[test.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside object bounds in arr\[\(signed( long)* int\)0\]: SUCCESS
5-
\[test.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside object bounds in arr\[\(signed( long)* int\)10\]: FAILURE
4+
\[test.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside dynamic object bounds in arr\[\(signed( long)* int\)0\]: SUCCESS
5+
\[test.pointer_dereference.\d+\] line \d+ dereference failure: pointer outside dynamic object bounds in arr\[\(signed( long)* int\)10\]: FAILURE
66
^EXIT=10$
77
^SIGNAL=0$
88
--

src/goto-harness/function_call_harness_generator.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ Author: Diffblue Ltd.
1010

1111
#include <util/allocate_objects.h>
1212
#include <util/arith_tools.h>
13+
#include <util/c_types.h>
1314
#include <util/exception_utils.h>
14-
#include <util/prefix.h>
1515
#include <util/std_code.h>
1616
#include <util/std_expr.h>
1717
#include <util/string2int.h>
1818
#include <util/string_utils.h>
1919
#include <util/ui_message.h>
2020

21-
#include <goto-programs/goto_convert.h>
21+
#include <goto-programs/goto_convert_functions.h>
2222
#include <goto-programs/goto_model.h>
2323

2424
#include <algorithm>
@@ -54,6 +54,8 @@ struct function_call_harness_generatort::implt
5454
std::map<irep_idt, irep_idt> function_argument_to_associated_array_size;
5555
std::map<irep_idt, irep_idt> function_parameter_to_associated_array_size;
5656

57+
std::set<symbol_exprt> global_pointers;
58+
5759
/// \see goto_harness_generatort::generate
5860
void generate(goto_modelt &goto_model, const irep_idt &harness_function_name);
5961
/// Iterate over the symbol table and generate initialisation code for
@@ -195,6 +197,11 @@ void function_call_harness_generatort::implt::generate(
195197

196198
generate_nondet_globals(function_body);
197199
call_function(arguments, function_body);
200+
for(const auto &global_pointer : global_pointers)
201+
{
202+
function_body.add(code_function_callt{
203+
recursive_initialization->get_free_function(), {global_pointer}});
204+
}
198205
add_harness_function_to_goto_model(std::move(function_body));
199206
}
200207

@@ -213,10 +220,7 @@ void function_call_harness_generatort::implt::generate_nondet_globals(
213220
for(const auto &symbol_table_entry : *symbol_table)
214221
{
215222
const auto &symbol = symbol_table_entry.second;
216-
if(
217-
symbol.is_static_lifetime && symbol.is_lvalue &&
218-
symbol.type.id() != ID_code &&
219-
!has_prefix(id2string(symbol.name), CPROVER_PREFIX))
223+
if(recursive_initialization->is_initialization_allowed(symbol))
220224
{
221225
globals.push_back(symbol.symbol_expr());
222226
}
@@ -232,7 +236,10 @@ void function_call_harness_generatort::implt::generate_initialisation_code_for(
232236
code_blockt &block,
233237
const exprt &lhs)
234238
{
235-
recursive_initialization->initialize(lhs, 0, {}, block);
239+
recursive_initialization->initialize(
240+
lhs, from_integer(0, signed_int_type()), block);
241+
if(lhs.type().id() == ID_pointer)
242+
global_pointers.insert(to_symbol_expr(lhs));
236243
}
237244

238245
void function_call_harness_generatort::validate_options(
@@ -300,14 +307,7 @@ void function_call_harness_generatort::implt::
300307
symbol_table->lookup_ref(harness_function_name);
301308
goto_functions->function_map[harness_function_name].type =
302309
to_code_type(generated_harness.type);
303-
auto &body = goto_functions->function_map[harness_function_name].body;
304-
goto_convert(
305-
static_cast<const codet &>(generated_harness.value),
306-
*symbol_table,
307-
body,
308-
*message_handler,
309-
function_to_call.mode);
310-
body.add(goto_programt::make_end_function());
310+
goto_convert(*symbol_table, *goto_functions, *message_handler);
311311
}
312312

313313
code_function_callt::argumentst
@@ -377,6 +377,8 @@ void function_call_harness_generatort::implt::call_function(
377377
for(auto const &argument : arguments)
378378
{
379379
generate_initialisation_code_for(function_body, argument);
380+
if(argument.type().id() == ID_pointer)
381+
global_pointers.insert(to_symbol_expr(argument));
380382
}
381383
code_function_callt function_call{function_to_call.symbol_expr(),
382384
std::move(arguments)};

src/goto-harness/memory_snapshot_harness_generator.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ Author: Daniel Poetzl
1111
#include "memory_snapshot_harness_generator.h"
1212
#include "memory_snapshot_harness_generator_options.h"
1313

14-
#include <goto-programs/goto_convert.h>
14+
#include <goto-programs/goto_convert_functions.h>
1515

1616
#include <json/json_parser.h>
1717

1818
#include <json-symtab-language/json_symbol_table.h>
1919

20+
#include <util/arith_tools.h>
21+
#include <util/c_types.h>
2022
#include <util/exception_utils.h>
2123
#include <util/fresh_symbol.h>
2224
#include <util/message.h>
@@ -231,7 +233,7 @@ code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
231233
for(auto pair : snapshot)
232234
{
233235
const auto name = id2string(pair.first);
234-
if(name.find(CPROVER_PREFIX) != 0)
236+
if(!has_prefix(name, CPROVER_PREFIX))
235237
ordered_snapshot_symbols.push_back(pair);
236238
}
237239

@@ -249,7 +251,22 @@ code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
249251
pointer_depth(right.second.symbol_expr().type());
250252
});
251253

252-
code_blockt code;
254+
code_blockt code{};
255+
256+
// add initialization for existing globals
257+
for(const auto &pair : goto_model.symbol_table)
258+
{
259+
const auto &global_symbol = pair.second;
260+
if(
261+
global_symbol.is_static_lifetime && global_symbol.is_lvalue &&
262+
global_symbol.type.id() != ID_code)
263+
{
264+
auto symeexr = global_symbol.symbol_expr();
265+
if(symeexr.type() == global_symbol.value.type())
266+
code.add(code_assignt{symeexr, global_symbol.value});
267+
}
268+
}
269+
253270
for(const auto &pair : ordered_snapshot_symbols)
254271
{
255272
const symbolt &snapshot_symbol = pair.second;
@@ -275,7 +292,9 @@ code_blockt memory_snapshot_harness_generatort::add_assignments_to_globals(
275292
else
276293
{
277294
recursive_initialization.initialize(
278-
fresh_or_snapshot_symbol.symbol_expr(), 0, {}, code);
295+
fresh_or_snapshot_symbol.symbol_expr(),
296+
from_integer(0, size_type()),
297+
code);
279298
}
280299
}
281300
return code;
@@ -316,12 +335,7 @@ void memory_snapshot_harness_generatort::
316335
harness_function.type = to_code_type(function.type);
317336

318337
goto_convert(
319-
to_code_block(to_code(function.value)),
320-
goto_model.symbol_table,
321-
harness_function.body,
322-
message_handler,
323-
function.mode);
324-
338+
goto_model.symbol_table, goto_model.goto_functions, message_handler);
325339
harness_function.body.add(goto_programt::make_end_function());
326340
}
327341

@@ -379,6 +393,7 @@ void memory_snapshot_harness_generatort::generate(
379393

380394
const symbolt *called_function_symbol =
381395
symbol_table.lookup(entry_location.function_name);
396+
recursive_initialization_config.mode = called_function_symbol->mode;
382397

383398
// introduce a symbol for a Boolean variable to indicate the point at which
384399
// the function initialisation is completed

0 commit comments

Comments
 (0)