CBMC
goto_synthesizer_parse_options.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 Module: Parse Options Module
3 Author: Qinheping Hu
4 \*******************************************************************/
5 
8 
10 
11 #include <util/exit_codes.h>
12 #include <util/help_formatter.h>
13 #include <util/unicode.h>
14 #include <util/version.h>
15 
22 
23 #include <ansi-c/gcc_version.h>
28 
30 
31 #include <fstream>
32 #include <iostream>
33 
35  int argc,
36  const char **argv)
39  argc,
40  argv,
41  "goto-synthesizer")
42 {
43 }
44 
47 {
48  if(cmdline.isset("version"))
49  {
50  std::cout << CBMC_VERSION << '\n';
51  return CPROVER_EXIT_SUCCESS;
52  }
53 
54  if(cmdline.args.size() != 1 && cmdline.args.size() != 2)
55  {
56  help();
58  }
59 
62 
64 
65  const auto result_get_goto_program = get_goto_program();
66  if(result_get_goto_program != CPROVER_EXIT_SUCCESS)
67  return result_get_goto_program;
68 
69  // configure gcc, if required -- get_goto_program will have set the config
71  {
72  gcc_versiont gcc_version;
73  gcc_version.get("gcc");
74  configure_gcc(gcc_version);
75  }
76 
78 
79  // Get options for the backend verifier and preprocess `goto_model`.
80  const auto &options = get_options();
81 
82  // Synthesize loop invariants and annotate them into `goto_model`
84  const auto &invariant_map = synthesizer.synthesize_all();
85  const auto &assigns_map = synthesizer.get_assigns_map();
86 
87  if(cmdline.isset("dump-loop-contracts"))
88  {
89  // Default output destination is stdout.
90  // Crangler will print the result to screen when the output destination
91  // is "stdout".
92  std::string json_output_str = "stdout";
93  if(cmdline.isset("json-output"))
94  {
95  json_output_str = cmdline.get_value("json-output");
96  }
97 
99  // Output file specified
100  if(cmdline.args.size() == 2)
101  {
102  std::ofstream out(widen_if_needed(cmdline.args[1]));
103 
104  if(!out)
105  {
106  log.error() << "failed to write to '" << cmdline.args[1] << "'";
108  }
110  goto_model, invariant_map, assigns_map, json_output_str, out);
111  }
112  // No output file specified. Print synthesized contracts with std::cout
113  else
114  {
116  goto_model, invariant_map, assigns_map, json_output_str, std::cout);
117  }
118 
119  return CPROVER_EXIT_SUCCESS;
120  }
121  else if(cmdline.isset("json-output"))
122  {
124  "Incompatible option detected",
125  "--json-output must be used with --dump-loop-contracts");
126  }
127 
128  // Annotate loop contracts.
129  annotate_invariants(invariant_map, goto_model);
130  annotate_assigns(assigns_map, goto_model);
131 
132  // Apply loop contracts.
133  std::set<std::string> to_exclude_from_nondet_static(
134  cmdline.get_values("nondet-static-exclude").begin(),
135  cmdline.get_values("nondet-static-exclude").end());
136  code_contractst contracts(
137  goto_model,
138  log,
141 
142  contracts.apply_loop_contracts(to_exclude_from_nondet_static);
143 
144  // recalculate numbers, etc.
146 
147  // add loop ids
149 
150  // label the assertions
152 
153  // recalculate numbers, etc.
155 
156  // write new binary?
157  if(cmdline.args.size() == 2)
158  {
159  log.status() << "Writing GOTO program to '" << cmdline.args[1] << "'"
160  << messaget::eom;
161 
164  else
165  return CPROVER_EXIT_SUCCESS;
166  }
167  else if(cmdline.args.size() < 2)
168  {
170  "Invalid number of positional arguments passed",
171  "[in] [out]",
172  "goto-instrument needs one input and one output file, aside from other "
173  "flags");
174  }
175 
176  help();
178 }
179 
181 {
182  log.status() << "Reading GOTO program from '" << cmdline.args[0] << "'"
183  << messaget::eom;
184 
185  config.set(cmdline);
186 
187  auto result = read_goto_binary(cmdline.args[0], ui_message_handler);
188 
189  if(!result.has_value())
191 
192  goto_model = std::move(result.value());
193 
195  return CPROVER_EXIT_SUCCESS;
196 }
197 
199 {
200  optionst options;
201 
202  // Default options
203  // These options are the same as we run CBMC without any set flag.
204  options.set_option("built-in-assertions", true);
205  options.set_option("propagation", true);
206  options.set_option("simple-slice", true);
207  options.set_option("simplify", true);
208  options.set_option("show-goto-symex-steps", false);
209  options.set_option("show-points-to-sets", false);
210  options.set_option("show-array-constraints", false);
211  options.set_option("built-in-assertions", true);
212  options.set_option("assertions", true);
213  options.set_option("assumptions", true);
214  options.set_option("depth", UINT32_MAX);
215  options.set_option("exploration-strategy", "lifo");
216  options.set_option("symex-cache-dereferences", false);
217  options.set_option("rewrite-rw-ok", true);
218  options.set_option("rewrite-union", true);
219  options.set_option("self-loops-to-assumptions", true);
220 
221  options.set_option("arrays-uf", "auto");
222  if(cmdline.isset("arrays-uf-always"))
223  options.set_option("arrays-uf", "always");
224  else if(cmdline.isset("arrays-uf-never"))
225  options.set_option("arrays-uf", "never");
226 
227  // Generating trace for counterexamples.
228  options.set_option("trace", true);
229 
230  parse_solver_options(cmdline, options);
231 
232  return options;
233 }
234 
237 {
238  std::cout << '\n'
239  << banner_string("Goto-synthesizer", CBMC_VERSION) << '\n'
240  << align_center_with_border("Copyright (C) 2022") << '\n'
241  << align_center_with_border("Qinheping Hu") << '\n'
242  << align_center_with_border("qinhh@amazon.com") << '\n';
243 
244  std::cout << help_formatter(
245  "\n"
246  "Usage: \tPurpose:\n"
247  "\n"
248  " {bgoto-synthesizer} [{y-?}] [{y-h}] [{y--help}] \t show this help\n"
249  " {bgoto-synthesizer} {y--version} \t show version and exit\n"
250  " {bgoto-synthesizer} {uin} {uout} \t synthesize and apply loop"
251  " invariants.\n"
252  "\n"
254  "\n"
255  "Accepts all of cbmc's options plus the following backend "
256  "options:\n" HELP_CONFIG_BACKEND HELP_SOLVER
257  "\n"
258  " {y--arrays-uf-never} \t never turn arrays into uninterpreted functions\n"
259  " {y--arrays-uf-always} \t always turn arrays into uninterpreted"
260  " functions\n"
261  "\n"
262  "Other options:\n"
263  " {y--xml-ui} \t use XML-formatted output\n"
264  " {y--json-ui} \t use JSON-formatted output\n"
265  " {y--verbosity} {u#} \t verbosity level\n"
266  "\n");
267 }
configt config
Definition: config.cpp:25
CBMC Command Line Option Processing.
#define CBMC_OPTIONS
std::string get_value(char option) const
Definition: cmdline.cpp:48
virtual bool isset(char option) const
Definition: cmdline.cpp:30
argst args
Definition: cmdline.h:154
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:119
void apply_loop_contracts(const std::set< std::string > &to_exclude_from_nondet_init={})
Applies loop contract transformations.
Definition: contracts.cpp:1482
bool set(const cmdlinet &cmdline)
Definition: config.cpp:863
void set_from_symbol_table(const symbol_table_baset &)
Definition: config.cpp:1347
struct configt::ansi_ct ansi_c
invariant_mapt synthesize_all() override
This synthesizer guarantees that, with the synthesized loop contracts, all checks in the annotated GO...
std::map< loop_idt, std::set< exprt > > get_assigns_map() const
void get(const std::string &executable)
Definition: gcc_version.cpp:18
void compute_loop_numbers()
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
void help() override
display command line help
goto_synthesizer_parse_optionst(int argc, const char **argv)
int doit() override
invoke main modules
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
static unsigned eval_verbosity(const std::string &user_input, const message_levelt default_verbosity, message_handlert &dest)
Parse a (user-)provided string as a verbosity level and set it as the verbosity of dest.
Definition: message.cpp:105
mstreamt & error() const
Definition: message.h:391
mstreamt & status() const
Definition: message.h:406
@ M_STATUS
Definition: message.h:169
static eomt eom
Definition: message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
void set_option(const std::string &option, const bool value)
Definition: options.cpp:28
ui_message_handlert ui_message_handler
Definition: parse_options.h:45
#define HELP_CONFIG_BACKEND
Definition: config.h:126
Verify and use annotated invariants and pre/post-conditions.
#define HELP_LOOP_CONTRACTS_NO_UNWIND
Definition: contracts.h:42
#define FLAG_LOOP_CONTRACTS_NO_UNWIND
Definition: contracts.h:41
void dump_loop_contracts(goto_modelt &goto_model, const std::map< loop_idt, exprt > &invariant_map, const std::map< loop_idt, std::set< exprt >> &assigns_map, const std::string &json_output_str, std::ostream &out)
#define HELP_DUMP_LOOP_CONTRACTS
Enumerative Loop Contracts Synthesizer.
Document and give macros for the exit codes of CPROVER binaries.
#define CPROVER_EXIT_CONVERSION_FAILED
Failure to convert / write to another format.
Definition: exit_codes.h:62
#define CPROVER_EXIT_USAGE_ERROR
A usage error is returned when the command line is invalid or conflicting.
Definition: exit_codes.h:33
#define CPROVER_EXIT_SUCCESS
Success indicates the required analysis has been performed without error.
Definition: exit_codes.h:16
void configure_gcc(const gcc_versiont &gcc_version)
#define GOTO_SYNTHESIZER_OPTIONS
Help Formatter.
static help_formattert help_formatter(const std::string &s)
void update_max_malloc_size(goto_modelt &goto_model, message_handlert &message_handler)
Update the initial value of __CPROVER_max_malloc_size in case the number of object bits has changed.
Initialize a Goto Program.
Volatile Variables.
std::string align_center_with_border(const std::string &text)
Utility for displaying help centered messages borderered by "* *".
std::string banner_string(const std::string &front_end, const std::string &version)
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.
Read Goto Programs.
void label_properties(goto_modelt &goto_model)
Set the properties to check.
Show the goto functions.
Show the properties.
void parse_solver_options(const cmdlinet &cmdline, optionst &options)
Parse solver-related command-line parameters in cmdline and set corresponding values in options.
#define HELP_SOLVER
preprocessort preprocessor
Definition: config.h:267
Loop contract configurations.
#define widen_if_needed(s)
Definition: unicode.h:28
void annotate_invariants(const invariant_mapt &invariant_map, goto_modelt &goto_model)
Annotate the invariants in invariant_map to their corresponding loops.
Definition: utils.cpp:705
void annotate_assigns(const std::map< loop_idt, std::set< exprt >> &assigns_map, goto_modelt &goto_model)
Annotate the assigns in assigns_map to their corresponding loops.
Definition: utils.cpp:725
const char * CBMC_VERSION
static void write_goto_binary(std::ostream &out, const symbol_table_baset &symbol_table, const goto_functionst &goto_functions, irep_serializationt &irepconverter)
Writes a goto program to disc, using goto binary format.
Write GOTO binaries.