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(goto_model, log);
137  contracts.unwind_transformed_loops =
139  contracts.apply_loop_contracts(to_exclude_from_nondet_static);
140 
141  // recalculate numbers, etc.
143 
144  // add loop ids
146 
147  // label the assertions
149 
150  // recalculate numbers, etc.
152 
153  // write new binary?
154  if(cmdline.args.size() == 2)
155  {
156  log.status() << "Writing GOTO program to '" << cmdline.args[1] << "'"
157  << messaget::eom;
158 
161  else
162  return CPROVER_EXIT_SUCCESS;
163  }
164  else if(cmdline.args.size() < 2)
165  {
167  "Invalid number of positional arguments passed",
168  "[in] [out]",
169  "goto-instrument needs one input and one output file, aside from other "
170  "flags");
171  }
172 
173  help();
175 }
176 
178 {
179  log.status() << "Reading GOTO program from '" << cmdline.args[0] << "'"
180  << messaget::eom;
181 
182  config.set(cmdline);
183 
184  auto result = read_goto_binary(cmdline.args[0], ui_message_handler);
185 
186  if(!result.has_value())
188 
189  goto_model = std::move(result.value());
190 
192  return CPROVER_EXIT_SUCCESS;
193 }
194 
196 {
197  optionst options;
198 
199  // Default options
200  // These options are the same as we run CBMC without any set flag.
201  options.set_option("built-in-assertions", true);
202  options.set_option("propagation", true);
203  options.set_option("simple-slice", true);
204  options.set_option("simplify", true);
205  options.set_option("show-goto-symex-steps", false);
206  options.set_option("show-points-to-sets", false);
207  options.set_option("show-array-constraints", false);
208  options.set_option("built-in-assertions", true);
209  options.set_option("assertions", true);
210  options.set_option("assumptions", true);
211  options.set_option("depth", UINT32_MAX);
212  options.set_option("exploration-strategy", "lifo");
213  options.set_option("symex-cache-dereferences", false);
214  options.set_option("rewrite-rw-ok", true);
215  options.set_option("rewrite-union", true);
216  options.set_option("self-loops-to-assumptions", true);
217 
218  options.set_option("arrays-uf", "auto");
219  if(cmdline.isset("arrays-uf-always"))
220  options.set_option("arrays-uf", "always");
221  else if(cmdline.isset("arrays-uf-never"))
222  options.set_option("arrays-uf", "never");
223 
224  // Generating trace for counterexamples.
225  options.set_option("trace", true);
226 
227  parse_solver_options(cmdline, options);
228 
229  return options;
230 }
231 
234 {
235  std::cout << '\n'
236  << banner_string("Goto-synthesizer", CBMC_VERSION) << '\n'
237  << align_center_with_border("Copyright (C) 2022") << '\n'
238  << align_center_with_border("Qinheping Hu") << '\n'
239  << align_center_with_border("qinhh@amazon.com") << '\n';
240 
241  std::cout << help_formatter(
242  "\n"
243  "Usage: \tPurpose:\n"
244  "\n"
245  " {bgoto-synthesizer} [{y-?}] [{y-h}] [{y--help}] \t show this help\n"
246  " {bgoto-synthesizer} {y--version} \t show version and exit\n"
247  " {bgoto-synthesizer} {uin} {uout} \t synthesize and apply loop"
248  " invariants.\n"
249  "\n"
251  "\n"
252  "Accepts all of cbmc's options plus the following backend "
253  "options:\n" HELP_CONFIG_BACKEND HELP_SOLVER
254  "\n"
255  " {y--arrays-uf-never} \t never turn arrays into uninterpreted functions\n"
256  " {y--arrays-uf-always} \t always turn arrays into uninterpreted"
257  " functions\n"
258  "\n"
259  "Other options:\n"
260  " {y--xml-ui} \t use XML-formatted output\n"
261  " {y--json-ui} \t use JSON-formatted output\n"
262  " {y--verbosity} {u#} \t verbosity level\n"
263  "\n");
264 }
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:151
const std::list< std::string > & get_values(const std::string &option) const
Definition: cmdline.cpp:109
void apply_loop_contracts(const std::set< std::string > &to_exclude_from_nondet_init={})
Applies loop contract transformations.
Definition: contracts.cpp:1472
bool unwind_transformed_loops
Definition: contracts.h:145
bool set(const cmdlinet &cmdline)
Definition: config.cpp:829
void set_from_symbol_table(const symbol_table_baset &)
Definition: config.cpp:1297
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:399
mstreamt & status() const
Definition: message.h:414
@ M_STATISTICS
Definition: message.h:171
static eomt eom
Definition: message.h:297
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:37
#define FLAG_LOOP_CONTRACTS_NO_UNWIND
Definition: contracts.h:36
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:264
#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:700
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:720
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.