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 
12 #include <list>
13 #include <string>
14 
15 #include "goto_harness_generator.h"
17 
19 
20 #include <util/message.h>
21 #include <util/optional.h>
22 
28 {
29 public:
32  {
33  }
34 
46  void generate(goto_modelt &goto_model, const irep_idt &harness_function_name)
47  override;
48 
49 protected:
53  {
56 
57  entry_goto_locationt() = delete;
60  {
61  }
64  unsigned location_number)
66  {
67  }
68 
75  const goto_programt::instructionst &instructions) const;
76  };
77 
82  entry_goto_locationt parse_goto_location(const std::string &cmdl_option);
83 
87  {
89  unsigned line_number;
90 
91  entry_source_locationt() = delete;
94  {
95  }
96 
102  std::pair<goto_programt::const_targett, size_t>
104  const goto_programt::instructionst &instructions) const;
105  };
106 
111  entry_source_locationt parse_source_location(const std::string &cmdl_option);
112 
116  {
119 
120  entry_locationt() = default;
121  explicit entry_locationt(
125  {
126  }
127  };
128 
133  {
134  size_t distance;
138 
140  {
141  }
142 
143  void match_up(
144  const size_t &candidate_distance,
145  const irep_idt &candidate_function_name,
146  const goto_programt::const_targett &candidate_instruction)
147  {
148  if(match_found && distance <= candidate_distance)
149  return;
150 
151  match_found = true;
152  distance = candidate_distance;
153  function_name = candidate_function_name;
154  instruction = candidate_instruction;
155  }
156  };
157 
164  entry_locationt initialize_entry_via_goto(
165  const entry_goto_locationt &entry_goto_location,
166  const goto_functionst &goto_functions);
167 
174  entry_locationt initialize_entry_via_source(
175  const entry_source_locationt &entry_source_location,
176  const goto_functionst &goto_functions);
177 
181  void handle_option(
182  const std::string &option,
183  const std::list<std::string> &values) override;
184 
191  void validate_options(const goto_modelt &goto_model) override;
192 
196  void
197  get_memory_snapshot(const std::string &file, symbol_tablet &snapshot) const;
198 
229  void add_init_section(
230  const symbol_exprt &func_init_done_var,
231  goto_modelt &goto_model) const;
232 
238  const symbolt &fresh_symbol_copy(
239  const symbolt &snapshot_symbol,
240  symbol_tablet &symbol_table) const;
241 
254  const symbol_tablet &snapshot,
255  goto_modelt &goto_model) const;
256 
262  const symbolt &called_function_symbol,
263  code_blockt &code) const;
264 
270  goto_modelt &goto_model,
271  const symbolt &function) const;
272 
276  size_t pointer_depth(const typet &t) const;
277 
278  template <typename Adder>
279  void collect_references(const exprt &expr, Adder &&add_reference) const
280  {
281  if(expr.id() == ID_symbol)
282  add_reference(to_symbol_expr(expr).get_identifier());
283  for(const auto &operand : expr.operands())
284  {
285  collect_references(operand, add_reference);
286  }
287  }
288 
291  template <typename Key>
292  struct preordert
293  {
294  public:
295  using relationt = std::multimap<Key, Key>;
296  using keyst = std::set<Key>;
297 
300  {
301  }
302 
303  template <typename T>
304  void sort(
305  const std::vector<std::pair<Key, T>> &input,
306  std::vector<std::pair<Key, T>> &output)
307  {
308  std::unordered_map<Key, T> searchable_input;
309  using valuet = std::pair<Key, T>;
310 
311  for(const auto &item : input)
312  {
313  searchable_input[item.first] = item.second;
314  }
315  auto associate_key_with_t =
316  [&searchable_input](const Key &key) -> optionalt<valuet> {
317  if(searchable_input.count(key) != 0)
318  return valuet(key, searchable_input[key]);
319  else
320  return {};
321  };
322  auto push_to_output = [&output](const valuet &value) {
323  output.push_back(value);
324  };
325  for(const auto &item : input)
326  {
327  dfs(item, associate_key_with_t, push_to_output);
328  }
329  }
330 
331  private:
333 
336 
337  template <typename Value, typename Map, typename Handler>
338  void dfs(Value &&node, Map &&key_to_t, Handler &&handle)
339  {
340  PRECONDITION(seen.empty() && inserted.empty());
341  dfs_inner(node, key_to_t, handle);
342  seen.clear();
343  inserted.clear();
344  }
345 
346  template <typename Value, typename Map, typename Handler>
347  void dfs_inner(Value &&node, Map &&key_to_t, Handler &&handle)
348  {
349  const Key &key = node.first;
350  if(seen.count(key) == 0)
351  {
352  seen.insert(key);
353  auto key_range = preorder_relation.equal_range(key);
354  for(auto it = key_range.first; it != key_range.second; ++it)
355  {
356  auto maybe_value = key_to_t(it->second);
357  if(maybe_value.has_value())
358  dfs_inner(*maybe_value, key_to_t, handle);
359  }
360  }
361  if(inserted.count(key) != 0)
362  return;
363  handle(node);
364  inserted.insert(key);
365  }
366  };
367 
369  std::string memory_snapshot_file;
372  std::unordered_set<irep_idt> variables_to_havoc;
373 
376 
378 
380 };
381 
382 #endif // CPROVER_GOTO_HARNESS_MEMORY_SNAPSHOT_HARNESS_GENERATOR_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
memory_snapshot_harness_generatort::initialize_entry_via_goto
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...
Definition: memory_snapshot_harness_generator.cpp:470
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:129
memory_snapshot_harness_generatort::entry_locationt::entry_locationt
entry_locationt()=default
memory_snapshot_harness_generatort::source_location_matcht::match_up
void match_up(const size_t &candidate_distance, const irep_idt &candidate_function_name, const goto_programt::const_targett &candidate_instruction)
Definition: memory_snapshot_harness_generator.h:143
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
memory_snapshot_harness_generatort::parse_goto_location
entry_goto_locationt parse_goto_location(const std::string &cmdl_option)
Parse a command line option to extract the user specified entry goto location.
Definition: memory_snapshot_harness_generator.cpp:424
memory_snapshot_harness_generatort::preordert::relationt
std::multimap< Key, Key > relationt
Definition: memory_snapshot_harness_generator.h:295
memory_snapshot_harness_generatort::preordert::inserted
keyst inserted
Definition: memory_snapshot_harness_generator.h:335
typet
The type of an expression, extends irept.
Definition: type.h:28
recursive_initialization.h
memory_snapshot_harness_generatort::preordert::preorder_relation
const relationt & preorder_relation
Definition: memory_snapshot_harness_generator.h:332
optional.h
goto_programt::instructionst
std::list< instructiont > instructionst
Definition: goto_program.h:584
memory_snapshot_harness_generatort::handle_option
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)
Definition: memory_snapshot_harness_generator.cpp:28
memory_snapshot_harness_generatort::entry_source_locationt::find_first_corresponding_instruction
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,...
Definition: memory_snapshot_harness_generator.cpp:548
memory_snapshot_harness_generatort::source_location_matcht
Wraps the information for source location match candidates.
Definition: memory_snapshot_harness_generator.h:132
goto_model.h
memory_snapshot_harness_generatort::entry_source_locationt::entry_source_locationt
entry_source_locationt(irep_idt file_name, unsigned line_number)
Definition: memory_snapshot_harness_generator.h:92
exprt
Base class for all expressions.
Definition: expr.h:55
memory_snapshot_harness_generatort::memory_snapshot_file
std::string memory_snapshot_file
data to store the command-line options
Definition: memory_snapshot_harness_generator.h:369
goto_modelt
Definition: goto_model.h:25
goto_harness_generator.h
memory_snapshot_harness_generatort::validate_options
void validate_options(const goto_modelt &goto_model) override
Check that user options make sense: On their own, e.g.
Definition: memory_snapshot_harness_generator.cpp:107
memory_snapshot_harness_generatort::parse_source_location
entry_source_locationt parse_source_location(const std::string &cmdl_option)
Parse a command line option to extract the user specified entry source location.
Definition: memory_snapshot_harness_generator.cpp:450
memory_snapshot_harness_generatort::recursive_initialization_config
recursive_initialization_configt recursive_initialization_config
Definition: memory_snapshot_harness_generator.h:379
memory_snapshot_harness_generatort::preordert::dfs_inner
void dfs_inner(Value &&node, Map &&key_to_t, Handler &&handle)
Definition: memory_snapshot_harness_generator.h:347
memory_snapshot_harness_generatort::entry_goto_locationt::entry_goto_locationt
entry_goto_locationt(irep_idt function_name)
Definition: memory_snapshot_harness_generator.h:58
goto_harness_generatort
Definition: goto_harness_generator.h:41
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
memory_snapshot_harness_generatort::entry_locationt::function_name
irep_idt function_name
Definition: memory_snapshot_harness_generator.h:117
memory_snapshot_harness_generatort::message_handler
message_handlert & message_handler
Definition: memory_snapshot_harness_generator.h:377
memory_snapshot_harness_generatort
Generates a harness which first assigns global variables with values from a given memory snapshot and...
Definition: memory_snapshot_harness_generator.h:27
memory_snapshot_harness_generatort::entry_goto_locationt
User provided goto location: function name and (maybe) location number; the structure wraps this opti...
Definition: memory_snapshot_harness_generator.h:52
memory_snapshot_harness_generatort::add_assignments_to_globals
code_blockt add_assignments_to_globals(const symbol_tablet &snapshot, goto_modelt &goto_model) const
For each global symbol in the snapshot symbol table either: 1) add code_assignt assigning a value fro...
Definition: memory_snapshot_harness_generator.cpp:207
memory_snapshot_harness_generatort::entry_goto_locationt::location_number
optionalt< unsigned > location_number
Definition: memory_snapshot_harness_generator.h:55
memory_snapshot_harness_generatort::entry_source_locationt::file_name
irep_idt file_name
Definition: memory_snapshot_harness_generator.h:88
memory_snapshot_harness_generatort::source_location_matcht::function_name
irep_idt function_name
Definition: memory_snapshot_harness_generator.h:135
memory_snapshot_harness_generatort::memory_snapshot_harness_generatort
memory_snapshot_harness_generatort(message_handlert &message_handler)
Definition: memory_snapshot_harness_generator.h:30
get_identifier
static optionalt< smt_termt > get_identifier(const exprt &expr, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_handle_identifiers, const std::unordered_map< exprt, smt_identifier_termt, irep_hash > &expression_identifiers)
Definition: smt2_incremental_decision_procedure.cpp:328
memory_snapshot_harness_generatort::entry_locationt
Wraps the information needed to identify the entry point.
Definition: memory_snapshot_harness_generator.h:115
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
memory_snapshot_harness_generatort::entry_locationt::start_instruction
goto_programt::const_targett start_instruction
Definition: memory_snapshot_harness_generator.h:118
memory_snapshot_harness_generatort::entry_goto_locationt::entry_goto_locationt
entry_goto_locationt(irep_idt function_name, unsigned location_number)
Definition: memory_snapshot_harness_generator.h:62
memory_snapshot_harness_generatort::preordert::seen
keyst seen
Definition: memory_snapshot_harness_generator.h:334
memory_snapshot_harness_generatort::fresh_symbol_copy
const symbolt & fresh_symbol_copy(const symbolt &snapshot_symbol, symbol_tablet &symbol_table) const
Introduce a new symbol into symbol_table with the same name and type as snapshot_symbol.
Definition: memory_snapshot_harness_generator.cpp:182
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
memory_snapshot_harness_generatort::add_init_section
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.
Definition: memory_snapshot_harness_generator.cpp:151
irept::id
const irep_idt & id() const
Definition: irep.h:396
message_handlert
Definition: message.h:27
memory_snapshot_harness_generatort::source_location_matcht::instruction
goto_programt::const_targett instruction
Definition: memory_snapshot_harness_generator.h:136
memory_snapshot_harness_generatort::variables_to_havoc
std::unordered_set< irep_idt > variables_to_havoc
Definition: memory_snapshot_harness_generator.h:372
memory_snapshot_harness_generatort::preordert::dfs
void dfs(Value &&node, Map &&key_to_t, Handler &&handle)
Definition: memory_snapshot_harness_generator.h:338
memory_snapshot_harness_generatort::add_call_with_nondet_arguments
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...
Definition: memory_snapshot_harness_generator.cpp:288
memory_snapshot_harness_generatort::initial_source_location_line
std::string initial_source_location_line
Definition: memory_snapshot_harness_generator.h:371
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
memory_snapshot_harness_generatort::source_location_matcht::source_location_matcht
source_location_matcht()
Definition: memory_snapshot_harness_generator.h:139
memory_snapshot_harness_generatort::get_memory_snapshot
void get_memory_snapshot(const std::string &file, symbol_tablet &snapshot) const
Parse the snapshot JSON file and initialise the symbol table.
Definition: memory_snapshot_harness_generator.cpp:326
memory_snapshot_harness_generatort::entry_source_locationt
User provided source location: file name and line number; the structure wraps this option with a pars...
Definition: memory_snapshot_harness_generator.h:86
memory_snapshot_harness_generatort::initial_goto_location_line
std::string initial_goto_location_line
Definition: memory_snapshot_harness_generator.h:370
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
memory_snapshot_harness_generatort::entry_source_locationt::line_number
unsigned line_number
Definition: memory_snapshot_harness_generator.h:89
memory_snapshot_harness_generatort::generate
void generate(goto_modelt &goto_model, const irep_idt &harness_function_name) override
The main function of this harness, consists of the following:
Definition: memory_snapshot_harness_generator.cpp:368
memory_snapshot_harness_generatort::entry_goto_locationt::function_name
irep_idt function_name
Definition: memory_snapshot_harness_generator.h:54
memory_snapshot_harness_generatort::entry_location
entry_locationt entry_location
data to initialize the entry function
Definition: memory_snapshot_harness_generator.h:375
symbolt
Symbol table entry.
Definition: symbol.h:27
memory_snapshot_harness_generatort::collect_references
void collect_references(const exprt &expr, Adder &&add_reference) const
Definition: memory_snapshot_harness_generator.h:279
memory_snapshot_harness_generatort::entry_goto_locationt::find_first_corresponding_instruction
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....
Definition: memory_snapshot_harness_generator.cpp:532
memory_snapshot_harness_generatort::entry_locationt::entry_locationt
entry_locationt(irep_idt function_name, goto_programt::const_targett start_instruction)
Definition: memory_snapshot_harness_generator.h:121
memory_snapshot_harness_generatort::entry_goto_locationt::entry_goto_locationt
entry_goto_locationt()=delete
recursive_initialization_configt
Definition: recursive_initialization.h:26
memory_snapshot_harness_generatort::pointer_depth
size_t pointer_depth(const typet &t) const
Recursively compute the pointer depth.
Definition: memory_snapshot_harness_generator.cpp:199
memory_snapshot_harness_generatort::initialize_entry_via_source
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...
Definition: memory_snapshot_harness_generator.cpp:499
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
exprt::operands
operandst & operands()
Definition: expr.h:94
memory_snapshot_harness_generatort::preordert::sort
void sort(const std::vector< std::pair< Key, T >> &input, std::vector< std::pair< Key, T >> &output)
Definition: memory_snapshot_harness_generator.h:304
memory_snapshot_harness_generatort::preordert::keyst
std::set< Key > keyst
Definition: memory_snapshot_harness_generator.h:296
memory_snapshot_harness_generatort::source_location_matcht::distance
size_t distance
Definition: memory_snapshot_harness_generator.h:134
message.h
memory_snapshot_harness_generatort::source_location_matcht::match_found
bool match_found
Definition: memory_snapshot_harness_generator.h:137
memory_snapshot_harness_generatort::preordert
Simple structure for linearising posets.
Definition: memory_snapshot_harness_generator.h:292
memory_snapshot_harness_generatort::insert_harness_function_into_goto_model
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.
Definition: memory_snapshot_harness_generator.cpp:307
memory_snapshot_harness_generatort::preordert::preordert
preordert(const relationt &preorder_relation)
Definition: memory_snapshot_harness_generator.h:298
memory_snapshot_harness_generatort::entry_source_locationt::entry_source_locationt
entry_source_locationt()=delete