CBMC
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/cprover_prefix.h>
18 #include <util/namespace.h>
19 #include <util/prefix.h>
20 #include <util/symbol_table.h>
21 
22 #include "wmm.h"
23 
24 class goto_functionst;
25 class messaget;
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  const irep_idt &function_id,
121  goto_programt &goto_program,
123  const source_locationt &source_location,
124  const irep_idt &object,
125  const unsigned current_thread,
126  const bool tso_pso_rmo);
127 
128  void assignment(
129  goto_programt &goto_program,
131  const source_locationt &source_location,
132  const irep_idt &id_lhs,
133  const exprt &rhs);
134 
136  goto_programt &goto_program,
138  const source_locationt &source_location,
139  const irep_idt &id_lhs,
140  const irep_idt &id_rhs)
141  {
143  assignment(goto_program, t, source_location, id_lhs,
144  ns.lookup(id_rhs).symbol_expr());
145  }
146 
147  bool track(const irep_idt &id) const
148  {
150 
151  const symbolt &symbol=ns.lookup(id);
152  if(symbol.is_thread_local)
153  return false;
155  return false;
156 
157  return true;
158  }
159 
160  irep_idt choice(const irep_idt &function_id, const std::string &suffix)
161  {
162  const auto maybe_symbol = symbol_table.lookup(function_id);
163  CHECK_RETURN(maybe_symbol);
164  return add(*maybe_symbol, "_weak_choice" + suffix, bool_typet());
165  }
166 
167  bool is_buffered(
168  const namespacet&,
169  const symbol_exprt&,
170  bool is_write);
171 
173  const symbol_exprt&,
174  bool is_write);
175 
176  void weak_memory(
177  value_setst &value_sets,
179  goto_programt &goto_program,
180  memory_modelt model,
181  goto_functionst &goto_functions);
182 
183  void affected_by_delay(
184  value_setst &value_sets,
185  goto_functionst &goto_functions);
186 
188  {
189  protected:
193 
194  /* for thread marking (dynamic) */
195  unsigned current_thread;
196  unsigned coming_from;
197  unsigned max_thread;
198 
199  /* data propagated through the CFG */
200  std::set<irep_idt> past_writes;
201 
202  public:
203  cfg_visitort(shared_bufferst &_shared, symbol_tablet &_symbol_table,
204  goto_functionst &_goto_functions)
205  :shared_buffers(_shared), symbol_table(_symbol_table),
206  goto_functions(_goto_functions)
207  {
208  current_thread = 0;
209  coming_from = 0;
210  max_thread = 0;
211  }
212 
213  void weak_memory(
214  value_setst &value_sets,
215  const irep_idt &function_id,
216  memory_modelt model);
217  };
218 
219 protected:
221 
222  // number of threads interfering
223  unsigned nb_threads;
224 
225  // instrumentations (not to be instrumented again)
226  std::set<irep_idt> instrumentations;
227 
228  // variables (non necessarily shared) affected by reads delay
229 public:
230  std::set<irep_idt> affected_by_delay_set;
231 
232 protected:
233  // for fresh variables
234  unsigned uniq;
235 
236  std::string unique();
237 
238  bool cav11;
239 
240  /* message */
242 
243  irep_idt add(
244  const symbolt &object,
245  const std::string &suffix,
246  const typet &type,
247  bool added_as_instrumentation);
248 
249  irep_idt
250  add(const symbolt &object, const std::string &suffix, const typet &type)
251  {
252  return add(object, suffix, type, true);
253  }
254 
255  /* inserting a non-instrumenting, fresh variable */
257  const symbolt &object,
258  const std::string &suffix,
259  const typet &type)
260  {
261  return add(object, suffix, type, false);
262  }
263 
264  void add_initialization(goto_programt &goto_program);
265 };
266 
267 #endif // CPROVER_GOTO_INSTRUMENT_WMM_SHARED_BUFFERS_H
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
shared_bufferst::write
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
Definition: shared_buffers.cpp:265
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
shared_bufferst::cfg_visitort::symbol_table
symbol_tablet & symbol_table
Definition: shared_buffers.h:191
TSO
@ TSO
Definition: wmm.h:20
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
shared_bufferst::det_flush
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)
Definition: shared_buffers.cpp:327
shared_bufferst::varst::w_buff1_used
irep_idt w_buff1_used
Definition: shared_buffers.h:57
shared_bufferst::set_cav11
void set_cav11(memory_modelt model)
Definition: shared_buffers.h:41
shared_bufferst::is_buffered_in_general
bool is_buffered_in_general(const symbol_exprt &, bool is_write)
Definition: shared_buffers.cpp:966
shared_bufferst
Definition: shared_buffers.h:28
shared_bufferst::add
irep_idt add(const symbolt &object, const std::string &suffix, const typet &type)
Definition: shared_buffers.h:250
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
typet
The type of an expression, extends irept.
Definition: type.h:28
shared_bufferst::varst::read_delayed
irep_idt read_delayed
Definition: shared_buffers.h:70
shared_bufferst::varst::r_buff0_thds
std::vector< irep_idt > r_buff0_thds
Definition: shared_buffers.h:67
shared_bufferst::cycles_r_loc
std::multimap< irep_idt, source_locationt > cycles_r_loc
Definition: shared_buffers.h:85
shared_bufferst::cfg_visitort::goto_functions
goto_functionst & goto_functions
Definition: shared_buffers.h:192
prefix.h
shared_bufferst::symbol_table
class symbol_tablet & symbol_table
Definition: shared_buffers.h:220
shared_bufferst::weak_memory
void weak_memory(value_setst &value_sets, symbol_tablet &symbol_table, goto_programt &goto_program, memory_modelt model, goto_functionst &goto_functions)
shared_bufferst::assignment
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)
Definition: shared_buffers.h:135
exprt
Base class for all expressions.
Definition: expr.h:55
shared_bufferst::cfg_visitort::max_thread
unsigned max_thread
Definition: shared_buffers.h:197
shared_bufferst::add_fresh_var
irep_idt add_fresh_var(const symbolt &object, const std::string &suffix, const typet &type)
Definition: shared_buffers.h:256
bool_typet
The Boolean type.
Definition: std_types.h:35
shared_bufferst::cycles_loc
std::multimap< irep_idt, source_locationt > cycles_loc
Definition: shared_buffers.h:83
shared_bufferst::add_initialization_code
void add_initialization_code(goto_functionst &goto_functions)
Definition: shared_buffers.cpp:125
shared_bufferst::cfg_visitort::current_thread
unsigned current_thread
Definition: shared_buffers.h:195
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
namespace.h
shared_bufferst::assignment
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
Definition: shared_buffers.cpp:140
shared_bufferst::varst::flush_delayed
irep_idt flush_delayed
Definition: shared_buffers.h:64
shared_bufferst::choice
irep_idt choice(const irep_idt &function_id, const std::string &suffix)
Definition: shared_buffers.h:160
shared_bufferst::add
irep_idt add(const symbolt &object, const std::string &suffix, const typet &type, bool added_as_instrumentation)
add a new var for instrumenting the input var
Definition: shared_buffers.cpp:72
shared_bufferst::var_map
var_mapt var_map
Definition: shared_buffers.h:77
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
shared_bufferst::is_buffered
bool is_buffered(const namespacet &, const symbol_exprt &, bool is_write)
Definition: shared_buffers.cpp:932
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
shared_bufferst::message
messaget & message
Definition: shared_buffers.h:241
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
shared_bufferst::shared_bufferst
shared_bufferst(symbol_tablet &_symbol_table, unsigned _nb_threads, messaget &_message)
Definition: shared_buffers.h:31
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
shared_bufferst::varst::w_buff1
irep_idt w_buff1
Definition: shared_buffers.h:54
shared_bufferst::cav11
bool cav11
Definition: shared_buffers.h:238
shared_bufferst::varst::w_buff0_used
irep_idt w_buff0_used
Definition: shared_buffers.h:57
shared_bufferst::affected_by_delay_set
std::set< irep_idt > affected_by_delay_set
Definition: shared_buffers.h:230
shared_bufferst::cfg_visitort::weak_memory
void weak_memory(value_setst &value_sets, const irep_idt &function_id, memory_modelt model)
instruments the program for the pairs detected through the CFG
Definition: shared_buffers.cpp:1051
shared_bufferst::unique
std::string unique()
returns a unique id (for fresh variables)
Definition: shared_buffers.cpp:24
shared_bufferst::cycles
std::set< irep_idt > cycles
Definition: shared_buffers.h:81
shared_bufferst::cfg_visitort::cfg_visitort
cfg_visitort(shared_bufferst &_shared, symbol_tablet &_symbol_table, goto_functionst &_goto_functions)
Definition: shared_buffers.h:203
cprover_prefix.h
shared_bufferst::cfg_visitort::past_writes
std::set< irep_idt > past_writes
Definition: shared_buffers.h:200
memory_modelt
memory_modelt
Definition: wmm.h:17
shared_bufferst::delay_read
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)
Definition: shared_buffers.cpp:176
goto_program.h
source_locationt
Definition: source_location.h:18
shared_bufferst::cfg_visitort::shared_buffers
shared_bufferst & shared_buffers
Definition: shared_buffers.h:190
shared_bufferst::nondet_flush
void nondet_flush(const irep_idt &function_id, 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
Definition: shared_buffers.cpp:433
shared_bufferst::var_mapt
std::map< irep_idt, varst > var_mapt
Definition: shared_buffers.h:76
shared_bufferst::instrumentations
std::set< irep_idt > instrumentations
Definition: shared_buffers.h:226
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
value_setst
Definition: value_sets.h:21
shared_bufferst::cfg_visitort::coming_from
unsigned coming_from
Definition: shared_buffers.h:196
shared_bufferst::varst::type
typet type
Definition: shared_buffers.h:73
symbolt
Symbol table entry.
Definition: symbol.h:27
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
shared_bufferst::varst::w_buff0
irep_idt w_buff0
Definition: shared_buffers.h:54
shared_bufferst::varst::r_buff1_thds
std::vector< irep_idt > r_buff1_thds
Definition: shared_buffers.h:67
symbol_table_baset::lookup
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:95
shared_bufferst::flush_read
void flush_read(goto_programt &goto_program, goto_programt::targett &t, const source_locationt &source_location, const irep_idt &write_object)
flushes read (POWER)
Definition: shared_buffers.cpp:225
shared_bufferst::track
bool track(const irep_idt &id) const
Definition: shared_buffers.h:147
shared_bufferst::operator()
const varst & operator()(const irep_idt &object)
instruments the variable
Definition: shared_buffers.cpp:31
symbol_table.h
Author: Diffblue Ltd.
shared_bufferst::cfg_visitort
Definition: shared_buffers.h:187
wmm.h
shared_bufferst::uniq
unsigned uniq
Definition: shared_buffers.h:234
shared_bufferst::varst::read_delayed_var
irep_idt read_delayed_var
Definition: shared_buffers.h:71
shared_bufferst::varst::mem_tmp
irep_idt mem_tmp
Definition: shared_buffers.h:63
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
shared_bufferst::varst
Definition: shared_buffers.h:49
shared_bufferst::nb_threads
unsigned nb_threads
Definition: shared_buffers.h:223
shared_bufferst::affected_by_delay
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...
Definition: shared_buffers.cpp:1008
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:586
shared_bufferst::add_initialization
void add_initialization(goto_programt &goto_program)
Definition: shared_buffers.cpp:96