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