CBMC
path_storage.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Path Storage
4 
5 Author: Kareem Khazem <karkhaz@karkhaz.com>
6 
7 \*******************************************************************/
8 
9 #include "path_storage.h"
10 
11 #include <sstream>
12 
13 #include <util/cmdline.h>
14 #include <util/exit_codes.h>
15 #include <util/make_unique.h>
16 
19 {
20  return nondet_symbol_exprt{"symex::nondet" + std::to_string(nondet_count++),
21  std::move(type),
22  std::move(location)};
23 }
24 
25 // _____________________________________________________________________________
26 // path_lifot
27 
29 {
30  last_peeked = paths.end();
31  --last_peeked;
32  return paths.back();
33 }
34 
36 {
37  paths.push_back(path);
38 }
39 
41 {
42  PRECONDITION(last_peeked != paths.end());
43  paths.erase(last_peeked);
44  last_peeked = paths.end();
45 }
46 
47 std::size_t path_lifot::size() const
48 {
49  return paths.size();
50 }
51 
53 {
54  paths.clear();
55 }
56 
57 // _____________________________________________________________________________
58 // path_fifot
59 
61 {
62  return paths.front();
63 }
64 
66 {
67  paths.push_back(path);
68 }
69 
71 {
72  paths.pop_front();
73 }
74 
75 std::size_t path_fifot::size() const
76 {
77  return paths.size();
78 }
79 
81 {
82  paths.clear();
83 }
84 
85 // _____________________________________________________________________________
86 // path_strategy_choosert
87 
88 static const std::map<
89  const std::string,
90  std::pair<
91  const std::string,
92  const std::function<std::unique_ptr<path_storaget>()>>>
94  {{"lifo",
95  {" lifo next instruction is pushed before\n"
96  " goto target; paths are popped in\n"
97  " last-in, first-out order. Explores\n"
98  " the program tree depth-first.\n",
99  []() { // NOLINT(whitespace/braces)
100  return util_make_unique<path_lifot>();
101  }}},
102  {"fifo",
103  {" fifo next instruction is pushed before\n"
104  " goto target; paths are popped in\n"
105  " first-in, first-out order. Explores\n"
106  " the program tree breadth-first.\n",
107  []() { // NOLINT(whitespace/braces)
108  return util_make_unique<path_fifot>();
109  }}}});
110 
111 std::string show_path_strategies()
112 {
113  std::stringstream ss;
114  for(auto &pair : path_strategies)
115  ss << pair.second.first;
116  return ss.str();
117 }
118 
120 {
121  return "lifo";
122 }
123 
124 bool is_valid_path_strategy(const std::string strategy)
125 {
126  return path_strategies.find(strategy) != path_strategies.end();
127 }
128 
129 std::unique_ptr<path_storaget> get_path_strategy(const std::string strategy)
130 {
131  auto found = path_strategies.find(strategy);
132  INVARIANT(
133  found != path_strategies.end(), "Unknown strategy '" + strategy + "'.");
134  return found->second.second();
135 }
136 
138  const cmdlinet &cmdline,
139  optionst &options,
140  message_handlert &message_handler)
141 {
142  messaget log(message_handler);
143  if(cmdline.isset("paths"))
144  {
145  options.set_option("paths", true);
146  std::string strategy = cmdline.get_value("paths");
147  if(!is_valid_path_strategy(strategy))
148  {
149  log.error() << "Unknown strategy '" << strategy
150  << "'. Pass the --show-symex-strategies flag to list "
151  "available strategies."
152  << messaget::eom;
154  }
155  options.set_option("exploration-strategy", strategy);
156  }
157  else
158  {
159  options.set_option("exploration-strategy", default_path_strategy());
160  }
161 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
path_lifot::private_peek
patht & private_peek() override
Definition: path_storage.cpp:28
nondet_symbol_exprt
Expression to hold a nondeterministic choice.
Definition: std_expr.h:241
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:30
path_fifot::push
void push(const patht &) override
Add a path to resume to the storage.
Definition: path_storage.cpp:65
optionst
Definition: options.h:22
typet
The type of an expression, extends irept.
Definition: type.h:28
path_lifot::paths
std::list< patht > paths
Definition: path_storage.h:175
optionst::set_option
void set_option(const std::string &option, const bool value)
Definition: options.cpp:28
path_fifot::clear
void clear() override
Clear all saved paths.
Definition: path_storage.cpp:80
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:58
messaget::eom
static eomt eom
Definition: message.h:297
path_lifot::clear
void clear() override
Clear all saved paths.
Definition: path_storage.cpp:52
path_storage.h
Storage of symbolic execution paths to resume.
parse_path_strategy_options
void parse_path_strategy_options(const cmdlinet &cmdline, optionst &options, message_handlert &message_handler)
add paths and exploration-strategy option, suitable to be invoked from front-ends.
Definition: path_storage.cpp:137
cmdlinet
Definition: cmdline.h:20
make_unique.h
messaget::error
mstreamt & error() const
Definition: message.h:399
default_path_strategy
std::string default_path_strategy()
Definition: path_storage.cpp:119
path_fifot::private_peek
patht & private_peek() override
Definition: path_storage.cpp:60
path_fifot::size
std::size_t size() const override
How many paths does this storage contain?
Definition: path_storage.cpp:75
symex_nondet_generatort::operator()
nondet_symbol_exprt operator()(typet type, source_locationt location)
Definition: path_storage.cpp:18
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:48
path_fifot::private_pop
void private_pop() override
Definition: path_storage.cpp:70
message_handlert
Definition: message.h:27
path_storaget::patht
Information saved at a conditional goto to resume execution.
Definition: path_storage.h:41
path_lifot::last_peeked
std::list< path_storaget::patht >::iterator last_peeked
Definition: path_storage.h:174
source_locationt
Definition: source_location.h:18
path_lifot::private_pop
void private_pop() override
Definition: path_storage.cpp:40
cmdline.h
exit_codes.h
path_fifot::paths
std::list< patht > paths
Definition: path_storage.h:191
CPROVER_EXIT_USAGE_ERROR
#define CPROVER_EXIT_USAGE_ERROR
A usage error is returned when the command line is invalid or conflicting.
Definition: exit_codes.h:33
path_lifot::push
void push(const patht &) override
Add a path to resume to the storage.
Definition: path_storage.cpp:35
show_path_strategies
std::string show_path_strategies()
suitable for displaying as a front-end help message
Definition: path_storage.cpp:111
path_lifot::size
std::size_t size() const override
How many paths does this storage contain?
Definition: path_storage.cpp:47
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
symex_nondet_generatort::nondet_count
std::size_t nondet_count
Definition: path_storage.h:28
is_valid_path_strategy
bool is_valid_path_strategy(const std::string strategy)
is there a factory constructor for the named strategy?
Definition: path_storage.cpp:124
path_strategies
static const std::map< const std::string, std::pair< const std::string, const std::function< std::unique_ptr< path_storaget >)> > > path_strategies({{"lifo", {" lifo next instruction is pushed before\n" " goto target; paths are popped in\n" " last-in, first-out order. Explores\n" " the program tree depth-first.\n", []() { return util_make_unique< path_lifot >();}}}, {"fifo", {" fifo next instruction is pushed before\n" " goto target; paths are popped in\n" " first-in, first-out order. Explores\n" " the program tree breadth-first.\n", []() { return util_make_unique< path_fifot >();}}}})
get_path_strategy
std::unique_ptr< path_storaget > get_path_strategy(const std::string strategy)
Ensure that is_valid_strategy() returns true for a particular string before calling this function on ...
Definition: path_storage.cpp:129