CBMC
goto_harness_parse_options.cpp
Go to the documentation of this file.
1 /******************************************************************\
2 
3 Module: goto_harness_parse_options
4 
5 Author: Diffblue Ltd.
6 
7 \******************************************************************/
8 
10 
11 #include <util/config.h>
12 #include <util/exception_utils.h>
13 #include <util/exit_codes.h>
14 #include <util/invariant.h>
15 #include <util/make_unique.h>
16 #include <util/suffix.h>
17 #include <util/version.h>
18 
22 
23 #include <goto-instrument/dump_c.h>
24 
28 
29 #include <algorithm>
30 #include <fstream>
31 #include <iostream>
32 #include <set>
33 #include <string>
34 #include <unordered_set>
35 #include <utility>
36 
37 std::unordered_set<irep_idt>
39 {
40  auto symbols = std::unordered_set<irep_idt>{};
42  goto_model.get_symbol_table().begin(),
43  goto_model.get_symbol_table().end(),
44  std::inserter(symbols, symbols.end()),
45  [](const std::pair<const irep_idt, symbolt> &key_value_pair) {
46  return key_value_pair.first;
47  });
48  return symbols;
49 }
50 
51 static void filter_goto_model(
52  goto_modelt &goto_model_with_harness,
53  const std::unordered_set<irep_idt> &goto_model_without_harness_symbols)
54 {
55  for(auto const &symbol_id : goto_model_without_harness_symbols)
56  {
57  auto &symbol =
58  goto_model_with_harness.symbol_table.get_writeable_ref(symbol_id);
59  if(symbol.is_function())
60  {
61  // We don’t want bodies of functions that already existed in the
62  // symbol table (i.e. not generated by us)
63  goto_model_with_harness.unload(symbol_id);
64  if(symbol.is_file_local)
65  {
66  goto_model_with_harness.symbol_table.remove(symbol_id);
67  }
68  }
69  else if(!symbol.is_type && symbol.is_file_local)
70  {
71  // We don’t want file local symbols from an existing goto model
72  // except types / typedefs, which also apparently get marked
73  // file local sometimes.
74  goto_model_with_harness.symbol_table.remove(symbol_id);
75  }
76  else if(!symbol.is_type && symbol.is_static_lifetime)
77  {
78  // if it has static lifetime and is *not* a type it is a global variable
79  // We keep around other global variables in case we want to initialise
80  // them, but mark them as extern so we don't duplicate their definitions
81  symbol.value = nil_exprt{};
82  symbol.is_extern = true;
83  }
84  }
85 }
86 
87 // The basic idea is that this module is handling the following
88 // sequence of events:
89 // 1. Initialise a goto-model by parsing an input (goto) binary
90 // 2. Initialise the harness generator (with some config) that will handle
91 // the mutation of the goto-model. The generator should create a new
92 // function that can be called by `cbmc --function`. The generated function
93 // should implement the behaviour of the harness (What exactly this means
94 // depends on the configuration)
95 // 3. Write the end result of that process to the output binary
96 
98 {
99  if(cmdline.isset("version"))
100  {
101  std::cout << CBMC_VERSION << '\n';
102  return CPROVER_EXIT_SUCCESS;
103  }
104 
105  auto got_harness_config = handle_common_options();
106  auto factory = make_factory();
107 
108  auto factory_options = collect_generate_factory_options();
109 
110  // This just sets up the defaults (and would interpret options such as --32).
111  config.set(cmdline);
112 
113  // Normally we would register language front-ends here but as goto-harness
114  // only works on goto binaries, we don't need to
115 
116  // Read goto binary into goto-model
117  auto read_goto_binary_result =
118  read_goto_binary(got_harness_config.in_file, ui_message_handler);
119  if(!read_goto_binary_result.has_value())
120  {
121  throw deserialization_exceptiont{"failed to read goto program from file '" +
122  got_harness_config.in_file + "'"};
123  }
124  auto goto_model = std::move(read_goto_binary_result.value());
125  auto const goto_model_without_harness_symbols =
127 
128  // This has to be called after the defaults are set up (as per the
129  // config.set(cmdline) above) otherwise, e.g. the architecture specification
130  // will be unknown.
131  config.set_from_symbol_table(goto_model.symbol_table);
132 
133  if(goto_model.symbol_table.has_symbol(
134  got_harness_config.harness_function_name))
135  {
137  "harness function `" +
138  id2string(got_harness_config.harness_function_name) +
139  "` already in "
140  "the symbol table",
142  }
143 
144  // Initialise harness generator
145  auto harness_generator = factory.factory(
146  got_harness_config.harness_type, factory_options, goto_model);
147  CHECK_RETURN(harness_generator != nullptr);
148 
149  harness_generator->generate(
150  goto_model, got_harness_config.harness_function_name);
151 
152  if(has_suffix(got_harness_config.out_file, ".c"))
153  {
154  filter_goto_model(goto_model, goto_model_without_harness_symbols);
155  auto harness_out = std::ofstream{got_harness_config.out_file};
156  dump_c(
157  goto_model.goto_functions,
158  true,
159  true,
160  false,
161  namespacet{goto_model.get_symbol_table()},
162  harness_out);
163  }
164  else
165  {
167  got_harness_config.out_file, goto_model, log.get_message_handler());
168  }
169 
170  return CPROVER_EXIT_SUCCESS;
171 }
172 
174 {
175  std::cout
176  << '\n'
177  << banner_string("Goto-Harness", CBMC_VERSION) << '\n'
178  << align_center_with_border("Copyright (C) 2019") << '\n'
179  << align_center_with_border("Diffblue Ltd.") << '\n'
180  << align_center_with_border("info@diffblue.com") << '\n'
181  << '\n'
182  << "Usage: Purpose:\n"
183  << '\n'
184  << " goto-harness [-?] [-h] [--help] show help\n"
185  << " goto-harness --version show version\n"
186  << " goto-harness <in> <out> --harness-function-name <name> --harness-type "
187  "<harness-type> [harness options]\n"
188  << "\n"
189  << "<in> goto binary to read from\n"
190  << "<out> file to write the harness to\n"
191  << " the harness is printed as C code, if <out> "
192  "has a .c suffix,\n"
193  " else a goto binary including the harness is "
194  "generated\n"
195  << "--harness-function-name the name of the harness function to "
196  "generate\n"
197  << "--harness-type one of the harness types listed below\n"
198  << "\n\n"
201 }
202 
204  int argc,
205  const char *argv[])
207  argc,
208  argv,
209  std::string("GOTO-HARNESS ") + CBMC_VERSION}
210 {
211 }
212 
215 {
216  goto_harness_configt goto_harness_config{};
217 
218  // This just checks the positional arguments to be 2.
219  // Options are not in .args
220  if(cmdline.args.size() != 2)
221  {
222  help();
224  "need to specify both input and output file names (may be "
225  "the same)",
226  "<in goto binary> <output C file or goto binary>"};
227  }
228 
229  goto_harness_config.in_file = cmdline.args[0];
230  goto_harness_config.out_file = cmdline.args[1];
231 
233  {
235  "required option not set", "--" GOTO_HARNESS_GENERATOR_TYPE_OPT};
236  }
237  goto_harness_config.harness_type =
239 
240  // Read the name of the harness function to generate
242  {
244  "required option not set",
246  }
247  goto_harness_config.harness_function_name = {
249 
250  return goto_harness_config;
251 }
252 
254 {
255  auto factory = goto_harness_generator_factoryt{};
256  factory.register_generator("call-function", [this]() {
257  return util_make_unique<function_call_harness_generatort>(
259  });
260 
261  factory.register_generator("initialize-with-memory-snapshot", [this]() {
262  return util_make_unique<memory_snapshot_harness_generatort>(
264  });
265 
266  return factory;
267 }
268 
271 {
272  auto const common_options =
273  std::set<std::string>{"version",
276 
278 
279  for(auto const &option : cmdline.option_names())
280  {
281  if(common_options.find(option) == common_options.end())
282  {
283  factory_options.insert({option, cmdline.get_values(option.c_str())});
284  }
285  }
286 
287  return factory_options;
288 }
cmdlinet::args
argst args
Definition: cmdline.h:145
exception_utils.h
GOTO_HARNESS_GENERATOR_HARNESS_FUNCTION_NAME_OPT
#define GOTO_HARNESS_GENERATOR_HARNESS_FUNCTION_NAME_OPT
Definition: goto_harness_generator_factory.h:22
goto_modelt::unload
void unload(const irep_idt &name)
Definition: goto_model.h:68
parse_options_baset::ui_message_handler
ui_message_handlert ui_message_handler
Definition: parse_options.h:45
parse_options_baset
Definition: parse_options.h:19
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:30
GOTO_HARNESS_GENERATOR_TYPE_OPT
#define GOTO_HARNESS_GENERATOR_TYPE_OPT
Definition: goto_harness_generator_factory.h:21
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
transform
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
Definition: abstract_value_object.cpp:159
write_goto_binary
bool write_goto_binary(std::ostream &out, const symbol_tablet &symbol_table, const goto_functionst &goto_functions, irep_serializationt &irepconverter)
Writes a goto program to disc, using goto binary format.
Definition: write_goto_binary.cpp:24
MEMORY_SNAPSHOT_HARNESS_GENERATOR_HELP
#define MEMORY_SNAPSHOT_HARNESS_GENERATOR_HELP
Definition: memory_snapshot_harness_generator_options.h:34
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition: exception_utils.h:79
goto_harness_parse_optionst::goto_harness_parse_optionst
goto_harness_parse_optionst(int argc, const char *argv[])
Definition: goto_harness_parse_options.cpp:203
function_call_harness_generator.h
goto_harness_generator_factory.h
invariant.h
filter_goto_model
static void filter_goto_model(goto_modelt &goto_model_with_harness, const std::unordered_set< irep_idt > &goto_model_without_harness_symbols)
Definition: goto_harness_parse_options.cpp:51
goto_model.h
goto_modelt
Definition: goto_model.h:25
goto_harness_generator_factoryt::register_generator
void register_generator(std::string generator_name, build_generatort build_generator)
register a new goto-harness generator with the given name.
Definition: goto_harness_generator_factory.cpp:17
get_symbol_names_from_goto_model
std::unordered_set< irep_idt > get_symbol_names_from_goto_model(const goto_modelt &goto_model)
Definition: goto_harness_parse_options.cpp:38
version.h
goto_harness_parse_optionst::collect_generate_factory_options
goto_harness_generator_factoryt::generator_optionst collect_generate_factory_options()
Gather all the options that are not handled by handle_common_options().
Definition: goto_harness_parse_options.cpp:270
has_suffix
bool has_suffix(const std::string &s, const std::string &suffix)
Definition: suffix.h:17
write_goto_binary.h
symbol_tablet::begin
virtual iteratort begin() override
Definition: symbol_table.h:108
goto_harness_generator_factoryt::generator_optionst
std::map< std::string, std::list< std::string > > generator_optionst
Definition: goto_harness_generator_factory.h:40
goto_harness_parse_options.h
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
CBMC_VERSION
const char * CBMC_VERSION
make_unique.h
GOTO_HARNESS_OPTIONS
#define GOTO_HARNESS_OPTIONS
Definition: goto_harness_parse_options.h:21
banner_string
std::string banner_string(const std::string &front_end, const std::string &version)
Definition: parse_options.cpp:174
symbol_tablet::end
virtual iteratort end() override
Definition: symbol_table.h:112
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:121
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
dump_c
void dump_c(const goto_functionst &src, const bool use_system_headers, const bool use_all_headers, const bool include_harness, const namespacet &ns, std::ostream &out)
Definition: dump_c.cpp:1591
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:48
goto_harness_generator_factoryt
helper to select harness type by name.
Definition: goto_harness_generator_factory.h:33
goto_harness_parse_optionst::goto_harness_configt
Definition: goto_harness_parse_options.h:40
configt::set_from_symbol_table
void set_from_symbol_table(const symbol_tablet &)
Definition: config.cpp:1254
goto_harness_parse_optionst::help
void help() override
Definition: goto_harness_parse_options.cpp:173
read_goto_binary.h
symbol_table_baset::remove
bool remove(const irep_idt &name)
Remove a symbol from the symbol table.
Definition: symbol_table_base.cpp:27
cmdlinet::option_names
option_namest option_names() const
Pseudo-object that can be used to iterate over options in this cmdlinet (should not outlive this)
Definition: cmdline.cpp:161
memory_snapshot_harness_generator.h
config
configt config
Definition: config.cpp:25
goto_harness_parse_optionst::make_factory
goto_harness_generator_factoryt make_factory()
Setup the generator factory.
Definition: goto_harness_parse_options.cpp:253
parse_options_baset::log
messaget log
Definition: parse_options.h:46
goto_harness_parse_optionst::handle_common_options
goto_harness_configt handle_common_options()
Handle command line arguments that are common to all harness generators.
Definition: goto_harness_parse_options.cpp:214
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:184
suffix.h
goto_harness_parse_optionst::doit
int doit() override
Definition: goto_harness_parse_options.cpp:97
configt::set
bool set(const cmdlinet &cmdline)
Definition: config.cpp:798
exit_codes.h
goto_modelt::get_symbol_table
const symbol_tablet & get_symbol_table() const override
Accessor to get the symbol table.
Definition: goto_model.h:77
config.h
read_goto_binary
static bool read_goto_binary(const std::string &filename, symbol_tablet &, goto_functionst &, message_handlert &)
Read a goto binary from a file, but do not update config.
Definition: read_goto_binary.cpp:61
CPROVER_EXIT_SUCCESS
#define CPROVER_EXIT_SUCCESS
Success indicates the required analysis has been performed without error.
Definition: exit_codes.h:16
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition: exception_utils.h:50
dump_c.h
cmdlinet::get_values
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:109
parse_options_baset::cmdline
cmdlinet cmdline
Definition: parse_options.h:28
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
FUNCTION_HARNESS_GENERATOR_HELP
#define FUNCTION_HARNESS_GENERATOR_HELP
Definition: function_harness_generator_options.h:41
align_center_with_border
std::string align_center_with_border(const std::string &text)
Utility for displaying help centered messages borderered by "* *".
Definition: parse_options.cpp:161