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