cprover
shared_buffers.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_GOTO_INSTRUMENT_WMM_SHARED_BUFFERS_H
11 #define CPROVER_GOTO_INSTRUMENT_WMM_SHARED_BUFFERS_H
12 
13 #include <map>
14 #include <set>
15 
17 #include <util/namespace.h>
18 #include <util/cprover_prefix.h>
19 #include <util/prefix.h>
20 #include <util/message.h>
21 
22 #include "wmm.h"
23 
24 class symbol_tablet;
25 class goto_functionst;
26 class value_setst;
27 
29 {
30 public:
31  shared_bufferst(symbol_tablet &_symbol_table, unsigned _nb_threads,
32  messaget &_message):
33  symbol_table(_symbol_table),
34  nb_threads(_nb_threads+1),
35  uniq(0),
36  cav11(false),
37  message(_message)
38  {
39  }
40 
42  {
43  if(model!=TSO)
44  throw "sorry, CAV11 only available for TSO";
45  cav11 = true;
46  }
47 
48  /* instrumentation of a variable */
49  class varst
50  {
51  public:
52  // Older stuff has the higher index.
53  // Shared buffer.
55 
56  // Are those places empty?
58 
59  // Delays write buffer flush: just to make some swaps between mem and buff
60  // -- this is to model lhs := rhs with rhs reading in the buffer without
61  // affecting the memory (Note: we model lhs := rhs by rhs := ..., then
62  // lhs := rhs)
65 
66  // Thread: Was it me who wrote at this place?
67  std::vector<irep_idt> r_buff0_thds, r_buff1_thds;
68 
69  // for delayed read:
72 
74  };
75 
76  typedef std::map<irep_idt, varst> var_mapt;
78 
79  /* instructions in violation cycles (to instrument): */
80  // variables in the cycles
81  std::set<irep_idt> cycles;
82  // events instrumented: var->locations in the code
83  std::multimap<irep_idt, source_locationt> cycles_loc;
84  // events in cycles: var->locations (for read instrumentations)
85  std::multimap<irep_idt, source_locationt> cycles_r_loc;
86 
87  const varst &operator()(const irep_idt &object);
88 
89  void add_initialization_code(goto_functionst &goto_functions);
90 
91  void delay_read(
92  goto_programt &goto_program,
94  const source_locationt &source_location,
95  const irep_idt &read_object,
96  const irep_idt &write_object);
97 
98  void flush_read(
99  goto_programt &goto_program,
101  const source_locationt &source_location,
102  const irep_idt &write_object);
103 
104  void write(
105  goto_programt &goto_program,
107  const source_locationt &source_location,
108  const irep_idt &object,
109  goto_programt::instructiont &original_instruction,
110  const unsigned current_thread);
111 
112  void det_flush(
113  goto_programt &goto_program,
115  const source_locationt &source_location,
116  const irep_idt &object,
117  const unsigned current_thread);
118 
119  void nondet_flush(
120  goto_programt &goto_program,
122  const source_locationt &source_location,
123  const irep_idt &object,
124  const unsigned current_thread,
125  const bool tso_pso_rmo);
126 
127  void assignment(
128  goto_programt &goto_program,
130  const source_locationt &source_location,
131  const irep_idt &id_lhs,
132  const exprt &rhs);
133 
135  goto_programt &goto_program,
137  const source_locationt &source_location,
138  const irep_idt &id_lhs,
139  const irep_idt &id_rhs)
140  {
142  assignment(goto_program, t, source_location, id_lhs,
143  ns.lookup(id_rhs).symbol_expr());
144  }
145 
146  bool track(const irep_idt &id) const
147  {
149 
150  const symbolt &symbol=ns.lookup(id);
151  if(symbol.is_thread_local)
152  return false;
154  return false;
155 
156  return true;
157  }
158 
159  irep_idt choice(const irep_idt &function, const std::string &suffix)
160  {
161  const auto maybe_symbol=symbol_table.lookup(function);
162  const std::string function_base_name =
163  maybe_symbol
164  ? id2string(maybe_symbol->base_name)
165  : "main";
166  return add(function_base_name+"_weak_choice",
167  function_base_name+"_weak_choice", suffix, bool_typet());
168  }
169 
170  bool is_buffered(
171  const namespacet&,
172  const symbol_exprt&,
173  bool is_write);
174 
176  const symbol_exprt&,
177  bool is_write);
178 
179  void weak_memory(
180  value_setst &value_sets,
182  goto_programt &goto_program,
183  memory_modelt model,
184  goto_functionst &goto_functions);
185 
186  void affected_by_delay(
187  value_setst &value_sets,
188  goto_functionst &goto_functions);
189 
191  {
192  protected:
196 
197  /* for thread marking (dynamic) */
198  unsigned current_thread;
199  unsigned coming_from;
200  unsigned max_thread;
201 
202  /* data propagated through the CFG */
203  std::set<irep_idt> past_writes;
204 
205  public:
206  cfg_visitort(shared_bufferst &_shared, symbol_tablet &_symbol_table,
207  goto_functionst &_goto_functions)
208  :shared_buffers(_shared), symbol_table(_symbol_table),
209  goto_functions(_goto_functions)
210  {
211  current_thread = 0;
212  coming_from = 0;
213  max_thread = 0;
214  }
215 
216  void weak_memory(
217  value_setst &value_sets,
218  const irep_idt &function,
219  memory_modelt model);
220  };
221 
222 protected:
224 
225  // number of threads interfering
226  unsigned nb_threads;
227 
228  // instrumentations (not to be instrumented again)
229  std::set<irep_idt> instrumentations;
230 
231  // variables (non necessarily shared) affected by reads delay
232 public:
233  std::set<irep_idt> affected_by_delay_set;
234 
235 protected:
236  // for fresh variables
237  unsigned uniq;
238 
239  std::string unique();
240 
241  bool cav11;
242 
243  /* message */
245 
246  irep_idt add(
247  const irep_idt &object,
248  const irep_idt &base_name,
249  const std::string &suffix,
250  const typet &type,
251  bool added_as_instrumentation);
252 
254  const irep_idt &object,
255  const irep_idt &base_name,
256  const std::string &suffix,
257  const typet &type)
258  {
259  return add(object, base_name, suffix, type, true);
260  }
261 
262  /* inserting a non-instrumenting, fresh variable */
264  const irep_idt &object,
265  const irep_idt &base_name,
266  const std::string &suffix,
267  const typet &type)
268  {
269  return add(object, base_name, suffix, type, false);
270  }
271 
272  void add_initialization(goto_programt &goto_program);
273 };
274 
275 #endif // CPROVER_GOTO_INSTRUMENT_WMM_SHARED_BUFFERS_H
void add_initialization_code(goto_functionst &goto_functions)
The type of an expression, extends irept.
Definition: type.h:27
messaget & message
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_thread_local
Definition: symbol.h:65
memory models
#define CPROVER_PREFIX
void weak_memory(value_setst &value_sets, const irep_idt &function, memory_modelt model)
instruments the program for the pairs detected through the CFG
shared_bufferst(symbol_tablet &_symbol_table, unsigned _nb_threads, messaget &_message)
bool track(const irep_idt &id) const
void add_initialization(goto_programt &goto_program)
The Boolean type.
Definition: std_types.h:28
irep_idt choice(const irep_idt &function, const std::string &suffix)
Symbol table entry.
Definition: symbol.h:27
std::string unique()
returns a unique id (for fresh variables)
void affected_by_delay(value_setst &value_sets, goto_functionst &goto_functions)
analysis over the goto-program which computes in affected_by_delay_set the variables (non necessarily...
bool is_buffered_in_general(const symbol_exprt &, bool is_write)
irep_idt add(const irep_idt &object, const irep_idt &base_name, const std::string &suffix, const typet &type)
void set_cav11(memory_modelt model)
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:178
std::set< irep_idt > instrumentations
goto_functionst & goto_functions
void write(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &object, goto_programt::instructiont &original_instruction, const unsigned current_thread)
instruments write
memory_modelt
Definition: wmm.h:17
instructionst::iterator targett
Definition: goto_program.h:414
std::map< irep_idt, varst > var_mapt
std::multimap< irep_idt, source_locationt > cycles_r_loc
cfg_visitort(shared_bufferst &_shared, symbol_tablet &_symbol_table, goto_functionst &_goto_functions)
std::set< irep_idt > past_writes
The symbol table.
Definition: symbol_table.h:19
void weak_memory(value_setst &value_sets, symbol_tablet &symbol_table, goto_programt &goto_program, memory_modelt model, goto_functionst &goto_functions)
void flush_read(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &write_object)
flushes read (POWER)
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
shared_bufferst & shared_buffers
std::vector< irep_idt > r_buff1_thds
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
const varst & operator()(const irep_idt &object)
instruments the variable
A collection of goto functions.
irep_idt add_fresh_var(const irep_idt &object, const irep_idt &base_name, const std::string &suffix, const typet &type)
Class that provides messages with a built-in verbosity &#39;level&#39;.
Definition: message.h:144
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:35
void assignment(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &id_lhs, const irep_idt &id_rhs)
std::multimap< irep_idt, source_locationt > cycles_loc
irep_idt add(const irep_idt &object, const irep_idt &base_name, const std::string &suffix, const typet &type, bool added_as_instrumentation)
add a new var for instrumenting the input var
std::vector< irep_idt > r_buff0_thds
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
void nondet_flush(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &object, const unsigned current_thread, const bool tso_pso_rmo)
instruments read
bool is_buffered(const namespacet &, const symbol_exprt &, bool is_write)
Base class for all expressions.
Definition: expr.h:54
Definition: wmm.h:20
void det_flush(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &object, const unsigned current_thread)
flush buffers (instruments fence)
std::set< irep_idt > cycles
Expression to hold a symbol (variable)
Definition: std_expr.h:143
class symbol_tablet & symbol_table
std::set< irep_idt > affected_by_delay_set
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
void delay_read(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &read_object, const irep_idt &write_object)
delays a read (POWER)
void assignment(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &id_lhs, const exprt &rhs)
add an assignment in the goto-program
Concrete Goto Program.
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166