CBMC
lazy_goto_model.cpp
Go to the documentation of this file.
1 
5 
6 #include "lazy_goto_model.h"
7 
8 #include <util/config.h>
9 #include <util/exception_utils.h>
11 #include <util/options.h>
12 
15 
16 #include <langapi/mode.h>
17 
18 #include "java_bytecode_language.h"
19 
20 #ifdef _MSC_VER
21 # include <util/unicode.h>
22 #endif
23 
24 #include <langapi/language.h>
25 
26 #include <fstream>
27 
30  post_process_functiont post_process_function,
31  post_process_functionst post_process_functions,
32  can_generate_function_bodyt driver_program_can_generate_function_body,
33  generate_function_bodyt driver_program_generate_function_body,
34  message_handlert &message_handler)
35  : goto_model(new goto_modelt()),
36  symbol_table(goto_model->symbol_table),
37  goto_functions(
38  goto_model->goto_functions.function_map,
39  language_files,
40  symbol_table,
41  [this](
42  const irep_idt &function_name,
44  journalling_symbol_tablet &journalling_symbol_table) -> void {
45  goto_model_functiont model_function(
46  journalling_symbol_table,
47  goto_model->goto_functions,
48  function_name,
49  function);
50  this->post_process_function(model_function, *this);
51  },
52  driver_program_can_generate_function_body,
53  driver_program_generate_function_body,
54  message_handler),
55  post_process_function(post_process_function),
56  post_process_functions(post_process_functions),
57  driver_program_can_generate_function_body(
58  driver_program_can_generate_function_body),
59  driver_program_generate_function_body(
60  driver_program_generate_function_body),
61  message_handler(message_handler)
62 {
63  language_files.set_message_handler(message_handler);
64 }
65 
67  : goto_model(std::move(other.goto_model)),
68  symbol_table(goto_model->symbol_table),
69  goto_functions(
70  goto_model->goto_functions.function_map,
71  language_files,
72  symbol_table,
73  [this](
74  const irep_idt &function_name,
76  journalling_symbol_tablet &journalling_symbol_table) -> void {
77  goto_model_functiont model_function(
78  journalling_symbol_table,
79  goto_model->goto_functions,
80  function_name,
81  function);
82  this->post_process_function(model_function, *this);
83  },
84  other.driver_program_can_generate_function_body,
85  other.driver_program_generate_function_body,
86  other.message_handler),
87  language_files(std::move(other.language_files)),
88  post_process_function(other.post_process_function),
89  post_process_functions(other.post_process_functions),
90  message_handler(other.message_handler)
91 {
92 }
94 
115  const std::vector<std::string> &files,
116  const optionst &options)
117 {
119 
120  if(files.empty() && config.java.main_class.empty())
121  {
123  "no program provided",
124  "source file names",
125  "one or more paths to a goto binary or a source file in a supported "
126  "language");
127  }
128 
129  std::list<std::string> binaries, sources;
130 
131  for(const auto &file : files)
132  {
134  binaries.push_back(file);
135  else
136  sources.push_back(file);
137  }
138 
139  if(sources.empty() && !config.java.main_class.empty())
140  {
141  // We assume it's Java.
142  const std::string filename = "";
143  language_filet &lf = add_language_file(filename);
144  lf.language = get_language_from_mode(ID_java);
145  CHECK_RETURN(lf.language != nullptr);
146 
147  languaget &language = *lf.language;
149  language.set_language_options(options);
150 
151  msg.status() << "Parsing ..." << messaget::eom;
152 
153  if(dynamic_cast<java_bytecode_languaget &>(language).parse())
154  {
155  throw invalid_input_exceptiont("PARSING ERROR");
156  }
157 
158  msg.status() << "Converting" << messaget::eom;
159 
161  {
162  throw invalid_input_exceptiont("CONVERSION ERROR");
163  }
164  }
165  else
166  {
168  sources, options, language_files, symbol_table, message_handler);
169  }
170 
172  throw incorrect_goto_program_exceptiont{"failed to read/link goto model"};
173 
176  symbol_table,
177  [this](const irep_idt &id) { goto_functions.unload(id); },
178  options,
179  false,
181 
182  // stupid hack
184 }
185 
188 {
190  symbol_tablet::symbolst::size_type new_table_size =
191  symbol_table.symbols.size();
192  do
193  {
194  table_size = new_table_size;
195 
196  // Find symbols that correspond to functions
197  std::vector<irep_idt> fn_ids_to_convert;
198  for(const auto &named_symbol : symbol_table.symbols)
199  {
200  if(named_symbol.second.is_function())
201  fn_ids_to_convert.push_back(named_symbol.first);
202  }
203 
204  for(const irep_idt &symbol_name : fn_ids_to_convert)
206 
207  // Repeat while new symbols are being added in case any of those are
208  // stubbed functions. Even stubs can create new stubs while being
209  // converted if they are special stubs (e.g. string functions)
210  new_table_size = symbol_table.symbols.size();
211  } while(new_table_size != table_size);
212 
213  goto_model->goto_functions.compute_location_numbers();
214 }
215 
217 {
219  journalling_symbol_tablet j_symbol_table =
221  if(language_files.final(j_symbol_table))
222  {
223  msg.error() << "CONVERSION ERROR" << messaget::eom;
224  return true;
225  }
226  for(const irep_idt &updated_symbol_id : j_symbol_table.get_updated())
227  {
228  if(j_symbol_table.lookup_ref(updated_symbol_id).is_function())
229  {
230  // Re-convert any that already exist
231  goto_functions.unload(updated_symbol_id);
232  goto_functions.ensure_function_loaded(updated_symbol_id);
233  }
234  }
235 
237 
239 }
240 
242 {
244 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
exception_utils.h
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
lazy_goto_functions_mapt::can_produce_function
bool can_produce_function(const key_type &name) const
Determines if this lazy GOTO functions map can produce a body for the given function.
Definition: lazy_goto_functions_map.h:123
lazy_goto_modelt::symbol_table
symbol_tablet & symbol_table
Reference to symbol_table in the internal goto_model.
Definition: lazy_goto_model.h:260
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
configt::javat::main_class
irep_idt main_class
Definition: config.h:328
java_bytecode_language.h
journalling_symbol_tablet::wrap
static journalling_symbol_tablet wrap(symbol_table_baset &base_symbol_table)
Definition: journalling_symbol_table.h:80
lazy_goto_functions_mapt::post_process_function
const post_process_functiont post_process_function
Definition: lazy_goto_functions_map.h:75
optionst
Definition: options.h:22
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
language_filest::typecheck
bool typecheck(symbol_tablet &symbol_table, const bool keep_file_local=false)
Definition: language_file.cpp:83
lazy_goto_modelt::goto_functions
const lazy_goto_functions_mapt goto_functions
Definition: lazy_goto_model.h:263
messaget::status
mstreamt & status() const
Definition: message.h:414
lazy_goto_modelt::add_language_file
language_filet & add_language_file(const std::string &filename)
Definition: lazy_goto_model.h:186
invalid_input_exceptiont
Thrown when user-provided input cannot be processed.
Definition: exception_utils.h:162
lazy_goto_modelt::language_files
language_filest language_files
Definition: lazy_goto_model.h:264
language_filet
Definition: language_file.h:41
get_language_from_mode
std::unique_ptr< languaget > get_language_from_mode(const irep_idt &mode)
Get the language corresponding to the given mode.
Definition: mode.cpp:51
journalling_symbol_tablet::get_updated
const changesett & get_updated() const
Definition: journalling_symbol_table.h:150
goto_modelt
Definition: goto_model.h:25
journalling_symbol_table.h
Author: Diffblue Ltd.
lazy_goto_functions_mapt::unload
void unload(const key_type &name) const
Definition: lazy_goto_functions_map.h:129
mode.h
options.h
messaget::eom
static eomt eom
Definition: message.h:297
lazy_goto_modelt::can_produce_function
bool can_produce_function(const irep_idt &id) const override
Determines if this model can produce a body for the given function.
Definition: lazy_goto_model.cpp:241
lazy_goto_modelt::message_handler
message_handlert & message_handler
Logging helper field.
Definition: lazy_goto_model.h:273
lazy_goto_model.h
Author: Diffblue Ltd.
languaget::set_language_options
virtual void set_language_options(const optionst &)
Set language-specific options.
Definition: language.h:41
incorrect_goto_program_exceptiont
Thrown when a goto program that's being processed is in an invalid format, for example passing the wr...
Definition: exception_utils.h:91
initialize_from_source_files
void initialize_from_source_files(const std::list< std::string > &sources, const optionst &options, language_filest &language_files, symbol_tablet &symbol_table, message_handlert &message_handler)
Populate symbol_table from sources by parsing and type checking via language_files.
Definition: initialize_goto_model.cpp:60
lazy_goto_modelt::finalize
bool finalize()
Definition: lazy_goto_model.cpp:216
messaget::error
mstreamt & error() const
Definition: message.h:399
read_objects_and_link
bool read_objects_and_link(const std::list< std::string > &file_names, goto_modelt &dest, message_handlert &message_handler)
Reads object files and updates the config if any files were read.
Definition: read_goto_binary.cpp:291
initialize_goto_model.h
lazy_goto_modelt
A GOTO model that produces function bodies on demand.
Definition: lazy_goto_model.h:42
symbolt::is_function
bool is_function() const
Definition: symbol.h:100
language.h
messaget::set_message_handler
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:179
lazy_goto_modelt::goto_model
std::unique_ptr< goto_modelt > goto_model
Definition: lazy_goto_model.h:256
message_handlert
Definition: message.h:27
goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:23
lazy_goto_modelt::initialize
void initialize(const std::vector< std::string > &files, const optionst &options)
Performs initial symbol table and language_filest initialization from a given commandline and parsed ...
Definition: lazy_goto_model.cpp:114
dstringt::empty
bool empty() const
Definition: dstring.h:88
language_filet::language
std::unique_ptr< languaget > language
Definition: language_file.h:47
read_goto_binary.h
lazy_goto_modelt::post_process_functions
const post_process_functionst post_process_functions
Definition: lazy_goto_model.h:268
config
configt config
Definition: config.cpp:25
language_filest::final
bool final(symbol_table_baset &symbol_table)
Definition: language_file.cpp:178
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
language_filest::clear
void clear()
Definition: language_file.h:136
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
lazy_goto_functions_mapt::ensure_function_loaded
void ensure_function_loaded(const key_type &name) const
Definition: lazy_goto_functions_map.h:134
configt::java
struct configt::javat java
unicode.h
languaget
Definition: language.h:37
is_goto_binary
bool is_goto_binary(const std::string &filename, message_handlert &message_handler)
Definition: read_goto_binary.cpp:192
config.h
lazy_goto_modelt::load_all_functions
void load_all_functions() const
Eagerly loads all functions from the symbol table.
Definition: lazy_goto_model.cpp:187
size_type
unsignedbv_typet size_type()
Definition: c_types.cpp:68
java_bytecode_languaget
Definition: java_bytecode_language.h:274
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
configt::set_object_bits_from_symbol_table
void set_object_bits_from_symbol_table(const symbol_tablet &)
Sets the number of bits used for object addresses.
Definition: config.cpp:1314
journalling_symbol_tablet
A symbol table wrapper that records which entries have been updated/removed.
Definition: journalling_symbol_table.h:35
goto_model_functiont
Interface providing access to a single function in a GOTO model, plus its associated symbol table.
Definition: goto_model.h:182
set_up_custom_entry_point
void set_up_custom_entry_point(language_filest &language_files, symbol_tablet &symbol_table, const std::function< void(const irep_idt &)> &unload, const optionst &options, bool try_mode_lookup, message_handlert &message_handler)
Process the "function" option in options to prepare a custom entry point to replace __CPROVER_start.
Definition: initialize_goto_model.cpp:117
lazy_goto_modelt::lazy_goto_modelt
lazy_goto_modelt(post_process_functiont post_process_function, post_process_functionst post_process_functions, can_generate_function_bodyt driver_program_can_generate_function_body, generate_function_bodyt driver_program_generate_function_body, message_handlert &message_handler)
Construct a lazy GOTO model, supplying callbacks that customise its behaviour.