Skip to content

Commit 431df6a

Browse files
committed
Output C code instead of goto-binary from goto-harness.
Modify goto-harness to output C code that corresponds to the added harness functions and some global values. This has been a feature request.
1 parent aa640fd commit 431df6a

File tree

6 files changed

+70
-18
lines changed

6 files changed

+70
-18
lines changed

regression/goto-harness/chain.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ name=${*:$#}
1212
name=${name%.c}
1313
args=${*:5:$#-5}
1414

15+
input_c_file="${name}.c"
16+
input_goto_binary="${name}.gb"
17+
harness_c_file="${name}-harness.c"
18+
19+
20+
1521
if [[ "${is_windows}" == "true" ]]; then
16-
$goto_cc "${name}.c"
17-
mv "${name}.exe" "${name}.gb"
22+
$goto_cc "$input_c_file"
23+
mv "${name}.exe" "$input_goto_binary"
1824
else
19-
$goto_cc -o "${name}.gb" "${name}.c"
25+
$goto_cc -o "$input_goto_binary" "$input_c_file"
2026
fi
2127

22-
if [ -e "${name}-mod.gb" ] ; then
23-
rm -f "${name}-mod.gb"
28+
if [ -e "$harness_c_file" ] ; then
29+
rm -f "$harness_c_file"
2430
fi
2531

2632
# `# some comment` is an inline comment - basically, cause bash to execute an empty command
27-
$cbmc --show-goto-functions "${name}.gb"
28-
$goto_harness "${name}.gb" "${name}-mod.gb" --harness-function-name $entry_point ${args}
29-
$cbmc --show-goto-functions "${name}-mod.gb"
30-
$cbmc --function $entry_point "${name}-mod.gb" \
33+
$cbmc --show-goto-functions "$input_goto_binary"
34+
$goto_harness "$input_goto_binary" "$harness_c_file" --harness-function-name $entry_point ${args}
35+
$cbmc --show-goto-functions "$harness_c_file"
36+
$cbmc --function $entry_point "$input_c_file" "$harness_c_file" \
3137
--pointer-check `# because we want to see out of bounds errors` \
3238
--unwind 11 `# with the way we set up arrays symex can't figure out loop bounds automatically` \
3339
--unwinding-assertions `# we want to make sure we don't accidentally pass tests because we didn't unwind enough` \

src/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ DIRS = analyses \
88
goto-cc \
99
goto-checker \
1010
goto-diff \
11-
goto-harness \
1211
goto-instrument \
12+
goto-harness \
1313
goto-programs \
1414
goto-symex \
1515
jsil \
@@ -66,7 +66,8 @@ languages: util.dir langapi.dir \
6666
solvers.dir: util.dir
6767

6868
goto-harness.dir: util.dir goto-programs.dir langapi.dir linking.dir \
69-
json.dir json-symtab-language.dir
69+
json.dir json-symtab-language.dir \
70+
goto-instrument.dir
7071

7172
goto-instrument.dir: languages goto-programs.dir pointer-analysis.dir \
7273
goto-symex.dir linking.dir analyses.dir solvers.dir

src/goto-harness/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ generic_includes(goto-harness)
55
target_link_libraries(goto-harness
66
util
77
goto-programs
8+
goto-instrument-lib
89
json
910
json-symtab-language
1011
)

src/goto-harness/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ OBJ += \
1616
../linking/linking$(LIBEXT) \
1717
../json/json$(LIBEXT) \
1818
../json-symtab-language/json-symtab-language$(LIBEXT) \
19+
../ansi-c/ansi-c$(LIBEXT) \
20+
../cpp/cpp$(LIBEXT) \
21+
../goto-instrument/dump_c$(OBJEXT) \
22+
../goto-instrument/goto_program2code$(OBJEXT) \
1923
# Empty last line
2024

2125
INCLUDES= -I ..

src/goto-harness/goto_harness_parse_options.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,57 @@ Author: Diffblue Ltd.
99
#include "goto_harness_parse_options.h"
1010

1111
#include <cstddef>
12+
#include <fstream>
1213
#include <set>
1314
#include <string>
15+
#include <unordered_set>
1416
#include <utility>
1517

18+
#include <goto-instrument/dump_c.h>
1619
#include <goto-programs/goto_convert.h>
1720
#include <goto-programs/goto_model.h>
1821
#include <goto-programs/read_goto_binary.h>
22+
#include <goto-programs/show_symbol_table.h>
1923
#include <goto-programs/write_goto_binary.h>
24+
#include <langapi/mode.h>
2025
#include <util/config.h>
2126
#include <util/exception_utils.h>
2227
#include <util/exit_codes.h>
28+
#include <util/expr_iterator.h>
2329
#include <util/invariant.h>
2430
#include <util/version.h>
2531

2632
#include "function_call_harness_generator.h"
2733
#include "goto_harness_generator_factory.h"
2834
#include "memory_snapshot_harness_generator.h"
2935

36+
std::unordered_set<irep_idt>
37+
get_symbol_names_from_goto_model(const goto_modelt &goto_model)
38+
{
39+
auto symbols = std::unordered_set<irep_idt>{};
40+
std::transform(
41+
goto_model.get_symbol_table().begin(),
42+
goto_model.get_symbol_table().end(),
43+
std::inserter(symbols, symbols.end()),
44+
[](const std::pair<const irep_idt, symbolt> &key_value_pair) {
45+
return key_value_pair.first;
46+
});
47+
return symbols;
48+
}
49+
50+
static void filter_goto_model(
51+
goto_modelt &goto_model_with_harness,
52+
const std::unordered_set<irep_idt> &goto_model_without_harness_symbols)
53+
{
54+
for(auto &symbol : goto_model_with_harness.get_symbol_table())
55+
{
56+
if(
57+
goto_model_without_harness_symbols.count(symbol.first) == 1 &&
58+
symbol.second.is_function())
59+
goto_model_with_harness.unload(symbol.first);
60+
}
61+
}
62+
3063
// The basic idea is that this module is handling the following
3164
// sequence of events:
3265
// 1. Initialise a goto-model by parsing an input (goto) binary
@@ -62,6 +95,8 @@ int goto_harness_parse_optionst::doit()
6295
got_harness_config.in_file + "'"};
6396
}
6497
auto goto_model = std::move(read_goto_binary_result.value());
98+
auto const goto_model_without_harness_symbols =
99+
get_symbol_names_from_goto_model(goto_model);
65100

66101
// This has to be called after the defaults are set up (as per the
67102
// config.set(cmdline) above) otherwise, e.g. the architecture specification
@@ -87,13 +122,15 @@ int goto_harness_parse_optionst::doit()
87122
harness_generator->generate(
88123
goto_model, got_harness_config.harness_function_name);
89124

90-
// Write end result to new goto-binary
91-
if(write_goto_binary(
92-
got_harness_config.out_file, goto_model, ui_message_handler))
93-
{
94-
throw system_exceptiont{"failed to write goto program from file '" +
95-
got_harness_config.out_file + "'"};
96-
}
125+
filter_goto_model(goto_model, goto_model_without_harness_symbols);
126+
auto harness_out = std::ofstream{got_harness_config.out_file};
127+
dump_c(
128+
goto_model.goto_functions,
129+
true,
130+
true,
131+
false,
132+
namespacet{goto_model.get_symbol_table()},
133+
harness_out);
97134

98135
return CPROVER_EXIT_SUCCESS;
99136
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
ansi-c
12
util
23
goto-harness
4+
goto-instrument
35
goto-programs
46
json
57
json-symtab-language
8+
langapi
69
linking

0 commit comments

Comments
 (0)