tests: add some normal loads/stores to test data races
[model-checker.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <vector>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads.h"
11
12 #define ACTION_INITIAL_CLOCK 0
13
14 /**
15  * @brief Construct a new ModelAction
16  *
17  * @param type The type of action
18  * @param order The memory order of this action. A "don't care" for non-ATOMIC
19  * actions (e.g., THREAD_* or MODEL_* actions).
20  * @param loc The location that this action acts upon
21  * @param value (optional) A value associated with the action (e.g., the value
22  * read or written). Defaults to a given macro constant, for debugging purposes.
23  * @param thread (optional) The Thread in which this action occurred. If NULL
24  * (default), then a Thread is assigned according to the scheduler.
25  */
26 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
27                 uint64_t value, Thread *thread) :
28         type(type),
29         order(order),
30         location(loc),
31         value(value),
32         reads_from(NULL),
33         seq_number(ACTION_INITIAL_CLOCK),
34         cv(NULL)
35 {
36         Thread *t = thread ? thread : thread_current();
37         this->tid = t->get_id();
38 }
39
40 /** @brief ModelAction destructor */
41 ModelAction::~ModelAction()
42 {
43         /**
44          * We can't free the clock vector:
45          * Clock vectors are snapshotting state. When we delete model actions,
46          * they are at the end of the node list and have invalid old clock
47          * vectors which have already been rolled back to an unallocated state.
48          */
49
50         /*
51          if (cv)
52                 delete cv; */
53 }
54
55 void ModelAction::copy_from_new(ModelAction *newaction)
56 {
57         seq_number = newaction->seq_number;
58 }
59
60 void ModelAction::set_seq_number(modelclock_t num)
61 {
62         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
63         seq_number = num;
64 }
65
66 bool ModelAction::is_relseq_fixup() const
67 {
68         return type == MODEL_FIXUP_RELSEQ;
69 }
70
71 bool ModelAction::is_mutex_op() const
72 {
73         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
74 }
75
76 bool ModelAction::is_lock() const
77 {
78         return type == ATOMIC_LOCK;
79 }
80
81 bool ModelAction::is_unlock() const
82 {
83         return type == ATOMIC_UNLOCK;
84 }
85
86 bool ModelAction::is_trylock() const
87 {
88         return type == ATOMIC_TRYLOCK;
89 }
90
91 bool ModelAction::is_success_lock() const
92 {
93         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
94 }
95
96 bool ModelAction::is_failed_trylock() const
97 {
98         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
99 }
100
101 bool ModelAction::is_read() const
102 {
103         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
104 }
105
106 bool ModelAction::is_write() const
107 {
108         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
109 }
110
111 bool ModelAction::is_rmwr() const
112 {
113         return type == ATOMIC_RMWR;
114 }
115
116 bool ModelAction::is_rmw() const
117 {
118         return type == ATOMIC_RMW;
119 }
120
121 bool ModelAction::is_rmwc() const
122 {
123         return type == ATOMIC_RMWC;
124 }
125
126 bool ModelAction::is_fence() const 
127 {
128         return type == ATOMIC_FENCE;
129 }
130
131 bool ModelAction::is_initialization() const
132 {
133         return type == ATOMIC_INIT;
134 }
135
136 bool ModelAction::is_acquire() const
137 {
138         switch (order) {
139         case std::memory_order_acquire:
140         case std::memory_order_acq_rel:
141         case std::memory_order_seq_cst:
142                 return true;
143         default:
144                 return false;
145         }
146 }
147
148 bool ModelAction::is_release() const
149 {
150         switch (order) {
151         case std::memory_order_release:
152         case std::memory_order_acq_rel:
153         case std::memory_order_seq_cst:
154                 return true;
155         default:
156                 return false;
157         }
158 }
159
160 bool ModelAction::is_seqcst() const
161 {
162         return order==std::memory_order_seq_cst;
163 }
164
165 bool ModelAction::same_var(const ModelAction *act) const
166 {
167         return location == act->location;
168 }
169
170 bool ModelAction::same_thread(const ModelAction *act) const
171 {
172         return tid == act->tid;
173 }
174
175 void ModelAction::copy_typeandorder(ModelAction * act) {
176         this->type = act->type;
177         this->order = act->order;
178 }
179
180 /** This method changes an existing read part of an RMW action into either:
181  *  (1) a full RMW action in case of the completed write or
182  *  (2) a READ action in case a failed action.
183  * @todo  If the memory_order changes, we may potentially need to update our
184  * clock vector.
185  */
186 void ModelAction::process_rmw(ModelAction * act) {
187         this->order=act->order;
188         if (act->is_rmwc())
189                 this->type=ATOMIC_READ;
190         else if (act->is_rmw()) {
191                 this->type=ATOMIC_RMW;
192                 this->value=act->value;
193         }
194 }
195
196 /** The is_synchronizing method should only explore interleavings if:
197  *  (1) the operations are seq_cst and don't commute or
198  *  (2) the reordering may establish or break a synchronization relation.
199  *  Other memory operations will be dealt with by using the reads_from
200  *  relation.
201  *
202  *  @param act is the action to consider exploring a reordering.
203  *  @return tells whether we have to explore a reordering.
204  */
205 bool ModelAction::could_synchronize_with(const ModelAction *act) const
206 {
207         //Same thread can't be reordered
208         if (same_thread(act))
209                 return false;
210
211         // Different locations commute
212         if (!same_var(act))
213                 return false;
214
215         // Explore interleavings of seqcst writes to guarantee total order
216         // of seq_cst operations that don't commute
217         if ((is_write() || act->is_write()) && is_seqcst() && act->is_seqcst())
218                 return true;
219
220         // Explore synchronizing read/write pairs
221         if (is_read() && is_acquire() && act->is_write() && act->is_release())
222                 return true;
223
224         // Otherwise handle by reads_from relation
225         return false;
226 }
227
228 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
229 {
230         //Must be different threads to reorder
231         if (same_thread(act))
232                 return false;
233         
234         //Try to reorder a lock past a successful lock
235         if (act->is_success_lock())
236                 return true;
237         
238         //Try to push a successful trylock past an unlock
239         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
240                 return true;
241
242         return false;
243 }
244
245 /**
246  * Create a new clock vector for this action. Note that this function allows a
247  * user to clobber (and leak) a ModelAction's existing clock vector. A user
248  * should ensure that the vector has already either been rolled back
249  * (effectively "freed") or freed.
250  *
251  * @param parent A ModelAction from which to inherit a ClockVector
252  */
253 void ModelAction::create_cv(const ModelAction *parent)
254 {
255         if (parent)
256                 cv = new ClockVector(parent->cv, this);
257         else
258                 cv = new ClockVector(NULL, this);
259 }
260
261 void ModelAction::set_try_lock(bool obtainedlock) {
262         if (obtainedlock)
263                 value=VALUE_TRYSUCCESS;
264         else
265                 value=VALUE_TRYFAILED;
266 }
267
268 /**
269  * Update the model action's read_from action
270  * @param act The action to read from; should be a write
271  * @return True if this read established synchronization
272  */
273 bool ModelAction::read_from(const ModelAction *act)
274 {
275         ASSERT(cv);
276         reads_from = act;
277         if (act != NULL && this->is_acquire()) {
278                 rel_heads_list_t release_heads;
279                 model->get_release_seq_heads(this, &release_heads);
280                 int num_heads = release_heads.size();
281                 for (unsigned int i = 0; i < release_heads.size(); i++)
282                         if (!synchronize_with(release_heads[i])) {
283                                 model->set_bad_synchronization();
284                                 num_heads--;
285                         }
286                 return num_heads > 0;
287         }
288         return false;
289 }
290
291 /**
292  * Synchronize the current thread with the thread corresponding to the
293  * ModelAction parameter.
294  * @param act The ModelAction to synchronize with
295  * @return True if this is a valid synchronization; false otherwise
296  */
297 bool ModelAction::synchronize_with(const ModelAction *act) {
298         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
299                 return false;
300         model->check_promises(act->get_tid(), cv, act->cv);
301         cv->merge(act->cv);
302         return true;
303 }
304
305 bool ModelAction::has_synchronized_with(const ModelAction *act) const
306 {
307         return cv->has_synchronized_with(act->cv);
308 }
309
310 /**
311  * Check whether 'this' happens before act, according to the memory-model's
312  * happens before relation. This is checked via the ClockVector constructs.
313  * @return true if this action's thread has synchronized with act's thread
314  * since the execution of act, false otherwise.
315  */
316 bool ModelAction::happens_before(const ModelAction *act) const
317 {
318         return act->cv->synchronized_since(this);
319 }
320
321 /** @brief Print nicely-formatted info about this ModelAction */
322 void ModelAction::print() const
323 {
324         const char *type_str, *mo_str;
325         switch (this->type) {
326         case MODEL_FIXUP_RELSEQ:
327                 type_str = "relseq fixup";
328                 break;
329         case THREAD_CREATE:
330                 type_str = "thread create";
331                 break;
332         case THREAD_START:
333                 type_str = "thread start";
334                 break;
335         case THREAD_YIELD:
336                 type_str = "thread yield";
337                 break;
338         case THREAD_JOIN:
339                 type_str = "thread join";
340                 break;
341         case THREAD_FINISH:
342                 type_str = "thread finish";
343                 break;
344         case ATOMIC_READ:
345                 type_str = "atomic read";
346                 break;
347         case ATOMIC_WRITE:
348                 type_str = "atomic write";
349                 break;
350         case ATOMIC_RMW:
351                 type_str = "atomic rmw";
352                 break;
353         case ATOMIC_FENCE:
354                 type_str = "fence";
355                 break;
356         case ATOMIC_RMWR:
357                 type_str = "atomic rmwr";
358                 break;
359         case ATOMIC_RMWC:
360                 type_str = "atomic rmwc";
361                 break;
362         case ATOMIC_INIT:
363                 type_str = "init atomic";
364                 break;
365         case ATOMIC_LOCK:
366                 type_str = "lock";
367                 break;
368         case ATOMIC_UNLOCK:
369                 type_str = "unlock";
370                 break;
371         case ATOMIC_TRYLOCK:
372                 type_str = "trylock";
373                 break;
374         default:
375                 type_str = "unknown type";
376         }
377
378         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
379
380         switch (this->order) {
381         case std::memory_order_relaxed:
382                 mo_str = "relaxed";
383                 break;
384         case std::memory_order_acquire:
385                 mo_str = "acquire";
386                 break;
387         case std::memory_order_release:
388                 mo_str = "release";
389                 break;
390         case std::memory_order_acq_rel:
391                 mo_str = "acq_rel";
392                 break;
393         case std::memory_order_seq_cst:
394                 mo_str = "seq_cst";
395                 break;
396         default:
397                 mo_str = "unknown";
398                 break;
399         }
400
401         printf("(%3d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p  Value: %-12" PRIu64,
402                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
403         if (is_read()) {
404                 if (reads_from)
405                         printf(" Rf: %d", reads_from->get_seq_number());
406                 else
407                         printf(" Rf: ?");
408         }
409         if (cv) {
410                 printf("\t");
411                 cv->print();
412         } else
413                 printf("\n");
414 }