cprover
concurrency.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Encoding for Threaded Goto Programs
4 
5 Author: Daniel Kroening
6 
7 Date: October 2012
8 
9 \*******************************************************************/
10 
13 
14 #include "concurrency.h"
15 
16 #include <util/find_symbols.h>
17 #include <util/invariant.h>
18 #include <util/optional.h>
19 #include <util/replace_symbol.h>
20 #include <util/std_expr.h>
21 
22 #include <analyses/is_threaded.h>
23 
25 {
26 public:
28  value_setst &_value_sets,
29  symbol_tablet &_symbol_table):
30  value_sets(_value_sets),
31  symbol_table(_symbol_table)
32  {
33  }
34 
35  void operator()(goto_functionst &goto_functions)
36  {
37  instrument(goto_functions);
38  }
39 
40 protected:
43 
44  void instrument(goto_functionst &goto_functions);
45 
46  void instrument(goto_programt &goto_program);
47 
48  void instrument(exprt &expr);
49 
50  void collect(
51  const goto_programt &goto_program,
52  const is_threadedt &is_threaded);
53 
54  void collect(const exprt &expr);
55 
56  void add_array_symbols();
57 
59  {
60  public:
63  };
64 
66  {
67  public:
70  };
71 
72  typedef std::map<irep_idt, shared_vart> shared_varst;
74 
75  typedef std::map<irep_idt, thread_local_vart> thread_local_varst;
77 };
78 
80 {
81  replace_symbolt replace_symbol;
82 
83  for(const symbol_exprt &s : find_symbols(expr))
84  {
85  shared_varst::const_iterator v_it = shared_vars.find(s.get_identifier());
86 
87  if(v_it != shared_vars.end())
88  {
90  // not yet done: neither array_symbol nor w_index_symbol are actually
91  // initialized anywhere
92  const shared_vart &shared_var = v_it->second;
93  const index_exprt new_expr(
94  *shared_var.array_symbol, *shared_var.w_index_symbol);
95 
96  replace_symbol.insert(s, new_expr);
97  }
98  }
99 }
100 
102  goto_programt &goto_program)
103 {
104  for(goto_programt::instructionst::iterator
105  it=goto_program.instructions.begin();
106  it!=goto_program.instructions.end();
107  it++)
108  {
109  if(it->is_assign())
110  {
111  instrument(it->assign_rhs_nonconst());
112  }
113  else if(it->is_assume() || it->is_assert() || it->is_goto())
114  {
115  exprt cond = it->get_condition();
116  instrument(cond);
117  it->set_condition(cond);
118  }
119  else if(it->is_function_call())
120  {
121  code_function_callt &code = to_code_function_call(it->code_nonconst());
122  instrument(code.function());
123 
124  // instrument(code.lhs(), LHS);
125  for(auto &arg : code.arguments())
126  instrument(arg);
127  }
128  }
129 }
130 
132 {
133  for(const auto &identifier : find_symbol_identifiers(expr))
134  {
136  const symbolt &symbol = ns.lookup(identifier);
137 
138  if(!symbol.is_state_var)
139  continue;
140 
141  if(symbol.is_thread_local)
142  {
143  if(thread_local_vars.find(identifier) != thread_local_vars.end())
144  continue;
145 
146  thread_local_vart &thread_local_var = thread_local_vars[identifier];
147  thread_local_var.type = symbol.type;
148  }
149  else
150  {
151  if(shared_vars.find(identifier) != shared_vars.end())
152  continue;
153 
154  shared_vart &shared_var = shared_vars[identifier];
155  shared_var.type = symbol.type;
156  }
157  }
158 }
159 
161  const goto_programt &goto_program,
162  const is_threadedt &is_threaded)
163 {
164  forall_goto_program_instructions(i_it, goto_program)
165  {
166  if(is_threaded(i_it))
167  i_it->apply([this](const exprt &e) { collect(e); });
168  }
169 }
170 
172 {
173 // for(
174 }
175 
177  goto_functionst &goto_functions)
178 {
180  is_threadedt is_threaded(goto_functions);
181 
182  // this first collects all shared and thread-local variables
183  for(const auto &gf_entry : goto_functions.function_map)
184  collect(gf_entry.second.body, is_threaded);
185 
186  // add array symbols
188 
189  // now instrument
190  for(auto &gf_entry : goto_functions.function_map)
191  instrument(gf_entry.second.body);
192 }
193 
195  value_setst &value_sets,
196  goto_modelt &goto_model)
197 {
198  concurrency_instrumentationt concurrency_instrumentation(
199  value_sets, goto_model.symbol_table);
200  concurrency_instrumentation(goto_model.goto_functions);
201 }
codet representation of a function call statement.
Definition: std_code.h:1213
exprt & function()
Definition: std_code.h:1248
argumentst & arguments()
Definition: std_code.h:1258
optionalt< symbol_exprt > array_symbol
Definition: concurrency.cpp:62
optionalt< symbol_exprt > w_index_symbol
Definition: concurrency.cpp:62
void collect(const goto_programt &goto_program, const is_threadedt &is_threaded)
void operator()(goto_functionst &goto_functions)
Definition: concurrency.cpp:35
concurrency_instrumentationt(value_setst &_value_sets, symbol_tablet &_symbol_table)
Definition: concurrency.cpp:27
std::map< irep_idt, thread_local_vart > thread_local_varst
Definition: concurrency.cpp:75
thread_local_varst thread_local_vars
Definition: concurrency.cpp:76
void instrument(goto_functionst &goto_functions)
symbol_tablet & symbol_table
Definition: concurrency.cpp:42
std::map< irep_idt, shared_vart > shared_varst
Definition: concurrency.cpp:72
Base class for all expressions.
Definition: expr.h:54
A collection of goto functions.
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:33
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:71
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:652
Array index operator.
Definition: std_expr.h:1328
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
Replace expression or type symbols by an expression or type, respectively.
void insert(const class symbol_exprt &old_expr, const exprt &new_expr)
Sets old_expr to be replaced by new_expr if we don't already have a replacement; otherwise does nothi...
Expression to hold a symbol (variable)
Definition: std_expr.h:80
The symbol table.
Definition: symbol_table.h:14
Symbol table entry.
Definition: symbol.h:28
bool is_thread_local
Definition: symbol.h:65
bool is_state_var
Definition: symbol.h:62
typet type
Type of symbol.
Definition: symbol.h:31
The type of an expression, extends irept.
Definition: type.h:28
void concurrency(value_setst &value_sets, goto_modelt &goto_model)
Encoding for Threaded Goto Programs.
void find_symbols(const exprt &src, find_symbols_sett &dest, bool current, bool next)
Add to the set dest the sub-expressions of src with id ID_symbol if current is true,...
std::unordered_set< irep_idt > find_symbol_identifiers(const exprt &src)
Find identifiers of the sub expressions with id ID_symbol.
#define forall_goto_program_instructions(it, program)
Over-approximate Concurrency for Threaded Goto Programs.
nonstd::optional< T > optionalt
Definition: optional.h:35
#define UNIMPLEMENTED
Definition: invariant.h:525
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1324
API to expression classes.