CBMC
memory_snapshot_harness_generator.h
Go to the documentation of this file.
1 /******************************************************************\
2 
3 Module: Harness to initialise memory from memory snapshot
4 
5 Author: Daniel Poetzl
6 
7 \******************************************************************/
8 
9 #ifndef CPROVER_GOTO_HARNESS_MEMORY_SNAPSHOT_HARNESS_GENERATOR_H
10 #define CPROVER_GOTO_HARNESS_MEMORY_SNAPSHOT_HARNESS_GENERATOR_H
11 
13 
14 #include "goto_harness_generator.h"
16 
17 #include <list>
18 #include <string>
19 
20 class goto_functionst;
21 class message_handlert;
22 class symbol_table_baset;
23 
29 {
30 public:
33  {
34  }
35 
47  void generate(goto_modelt &goto_model, const irep_idt &harness_function_name)
48  override;
49 
50 protected:
54  {
56  std::optional<unsigned> location_number;
57 
61  {
62  }
65  unsigned location_number)
67  {
68  }
69 
76  const goto_programt::instructionst &instructions) const;
77  };
78 
83  entry_goto_locationt parse_goto_location(const std::string &cmdl_option);
84 
88  {
90  unsigned line_number;
91 
95  {
96  }
97 
103  std::pair<goto_programt::const_targett, size_t>
105  const goto_programt::instructionst &instructions) const;
106  };
107 
112  entry_source_locationt parse_source_location(const std::string &cmdl_option);
113 
117  {
120 
121  entry_locationt() = default;
122  explicit entry_locationt(
126  {
127  }
128  };
129 
134  {
135  size_t distance;
139 
141  {
142  }
143 
144  void match_up(
145  const size_t &candidate_distance,
146  const irep_idt &candidate_function_name,
147  const goto_programt::const_targett &candidate_instruction)
148  {
149  if(match_found && distance <= candidate_distance)
150  return;
151 
152  match_found = true;
153  distance = candidate_distance;
154  function_name = candidate_function_name;
155  instruction = candidate_instruction;
156  }
157  };
158 
165  entry_locationt initialize_entry_via_goto(
166  const entry_goto_locationt &entry_goto_location,
167  const goto_functionst &goto_functions);
168 
175  entry_locationt initialize_entry_via_source(
176  const entry_source_locationt &entry_source_location,
177  const goto_functionst &goto_functions);
178 
182  void handle_option(
183  const std::string &option,
184  const std::list<std::string> &values) override;
185 
192  void validate_options(const goto_modelt &goto_model) override;
193 
197  void get_memory_snapshot(
198  const std::string &file,
199  symbol_table_baset &snapshot) const;
200 
231  void add_init_section(
232  const symbol_exprt &func_init_done_var,
233  goto_modelt &goto_model) const;
234 
240  const symbolt &fresh_symbol_copy(
241  const symbolt &snapshot_symbol,
242  symbol_table_baset &symbol_table) const;
243 
256  const symbol_table_baset &snapshot,
257  goto_modelt &goto_model) const;
258 
264  const symbolt &called_function_symbol,
265  code_blockt &code) const;
266 
272  goto_modelt &goto_model,
273  const symbolt &function) const;
274 
278  size_t pointer_depth(const typet &t) const;
279 
280  template <typename Adder>
281  void collect_references(const exprt &expr, Adder &&add_reference) const
282  {
283  if(expr.id() == ID_symbol)
284  add_reference(to_symbol_expr(expr).get_identifier());
285  for(const auto &operand : expr.operands())
286  {
287  collect_references(operand, add_reference);
288  }
289  }
290 
293  template <typename Key>
294  struct preordert
295  {
296  public:
297  using relationt = std::multimap<Key, Key>;
298  using keyst = std::set<Key>;
299 
302  {
303  }
304 
305  template <typename T>
306  void sort(
307  const std::vector<std::pair<Key, T>> &input,
308  std::vector<std::pair<Key, T>> &output)
309  {
310  std::unordered_map<Key, T> searchable_input;
311  using valuet = std::pair<Key, T>;
312 
313  for(const auto &item : input)
314  {
315  searchable_input[item.first] = item.second;
316  }
317  auto associate_key_with_t =
318  [&searchable_input](const Key &key) -> std::optional<valuet> {
319  if(searchable_input.count(key) != 0)
320  return valuet(key, searchable_input[key]);
321  else
322  return {};
323  };
324  auto push_to_output = [&output](const valuet &value) {
325  output.push_back(value);
326  };
327  for(const auto &item : input)
328  {
329  dfs(item, associate_key_with_t, push_to_output);
330  }
331  }
332 
333  private:
335 
338 
339  template <typename Value, typename Map, typename Handler>
340  void dfs(Value &&node, Map &&key_to_t, Handler &&handle)
341  {
342  PRECONDITION(seen.empty() && inserted.empty());
343  dfs_inner(node, key_to_t, handle);
344  seen.clear();
345  inserted.clear();
346  }
347 
348  template <typename Value, typename Map, typename Handler>
349  void dfs_inner(Value &&node, Map &&key_to_t, Handler &&handle)
350  {
351  const Key &key = node.first;
352  if(seen.count(key) == 0)
353  {
354  seen.insert(key);
355  auto key_range = preorder_relation.equal_range(key);
356  for(auto it = key_range.first; it != key_range.second; ++it)
357  {
358  auto maybe_value = key_to_t(it->second);
359  if(maybe_value.has_value())
360  dfs_inner(*maybe_value, key_to_t, handle);
361  }
362  }
363  if(inserted.count(key) != 0)
364  return;
365  handle(node);
366  inserted.insert(key);
367  }
368  };
369 
371  std::string memory_snapshot_file;
374  std::unordered_set<irep_idt> variables_to_havoc;
375 
378 
380 
382 };
383 
384 #endif // CPROVER_GOTO_HARNESS_MEMORY_SNAPSHOT_HARNESS_GENERATOR_H
A codet representing sequential composition of program statements.
Definition: std_code.h:130
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
operandst & operands()
Definition: expr.h:94
A collection of goto functions.
instructionst::const_iterator const_targett
Definition: goto_program.h:615
std::list< instructiont > instructionst
Definition: goto_program.h:612
const irep_idt & id() const
Definition: irep.h:388
Generates a harness which first assigns global variables with values from a given memory snapshot and...
void add_init_section(const symbol_exprt &func_init_done_var, goto_modelt &goto_model) const
Modify the entry-point function to start from the user-specified initial location.
entry_locationt entry_location
data to initialize the entry function
void insert_harness_function_into_goto_model(goto_modelt &goto_model, const symbolt &function) const
Insert the function into the symbol table (and the goto functions map) of the goto_model.
const symbolt & fresh_symbol_copy(const symbolt &snapshot_symbol, symbol_table_baset &symbol_table) const
Introduce a new symbol into symbol_table with the same name and type as snapshot_symbol.
entry_locationt initialize_entry_via_goto(const entry_goto_locationt &entry_goto_location, const goto_functionst &goto_functions)
Find and return the entry instruction (requested by the user as goto location: function name + locati...
entry_source_locationt parse_source_location(const std::string &cmdl_option)
Parse a command line option to extract the user specified entry source location.
entry_locationt initialize_entry_via_source(const entry_source_locationt &entry_source_location, const goto_functionst &goto_functions)
Find and return the entry instruction (requested by the user as source location: file name + line num...
code_blockt add_assignments_to_globals(const symbol_table_baset &snapshot, goto_modelt &goto_model) const
For each global symbol in the snapshot symbol table either: 1) add code_assignt assigning a value fro...
memory_snapshot_harness_generatort(message_handlert &message_handler)
void handle_option(const std::string &option, const std::list< std::string > &values) override
Collect the memory-snapshot specific cmdline options (one at a time)
entry_goto_locationt parse_goto_location(const std::string &cmdl_option)
Parse a command line option to extract the user specified entry goto location.
void collect_references(const exprt &expr, Adder &&add_reference) const
void generate(goto_modelt &goto_model, const irep_idt &harness_function_name) override
The main function of this harness, consists of the following:
void add_call_with_nondet_arguments(const symbolt &called_function_symbol, code_blockt &code) const
Create as many non-deterministic arguments as there are arguments of the called_function_symbol and a...
recursive_initialization_configt recursive_initialization_config
void get_memory_snapshot(const std::string &file, symbol_table_baset &snapshot) const
Parse the snapshot JSON file and initialise the symbol table.
void validate_options(const goto_modelt &goto_model) override
Check that user options make sense: On their own, e.g.
std::string memory_snapshot_file
data to store the command-line options
size_t pointer_depth(const typet &t) const
Recursively compute the pointer depth.
Expression to hold a symbol (variable)
Definition: std_expr.h:131
The symbol table base class interface.
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:29
Concrete Goto Program.
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272
User provided goto location: function name and (maybe) location number; the structure wraps this opti...
goto_programt::const_targett find_first_corresponding_instruction(const goto_programt::instructionst &instructions) const
Returns the first goto_programt::instructiont represented by this goto location, i....
entry_goto_locationt(irep_idt function_name, unsigned location_number)
Wraps the information needed to identify the entry point.
entry_locationt(irep_idt function_name, goto_programt::const_targett start_instruction)
User provided source location: file name and line number; the structure wraps this option with a pars...
std::pair< goto_programt::const_targett, size_t > find_first_corresponding_instruction(const goto_programt::instructionst &instructions) const
Returns the first goto_programt::instructiont represented by this source location,...
void sort(const std::vector< std::pair< Key, T >> &input, std::vector< std::pair< Key, T >> &output)
void dfs(Value &&node, Map &&key_to_t, Handler &&handle)
void dfs_inner(Value &&node, Map &&key_to_t, Handler &&handle)
Wraps the information for source location match candidates.
void match_up(const size_t &candidate_distance, const irep_idt &candidate_function_name, const goto_programt::const_targett &candidate_instruction)