-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.cc
2026 lines (1810 loc) · 58.1 KB
/
execution.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <algorithm>
#include <new>
#include <stdarg.h>
#include <errno.h>
#include "model.h"
#include "execution.h"
#include "action.h"
#include "schedule.h"
#include "common.h"
#include "clockvector.h"
#include "cyclegraph.h"
#include "datarace.h"
#include "threads-model.h"
#include "bugmessage.h"
#include "fuzzer.h"
#ifdef COLLECT_STAT
static unsigned int atomic_load_count = 0;
static unsigned int atomic_store_count = 0;
static unsigned int atomic_rmw_count = 0;
static unsigned int atomic_fence_count = 0;
static unsigned int atomic_lock_count = 0;
static unsigned int atomic_trylock_count = 0;
static unsigned int atomic_unlock_count = 0;
static unsigned int atomic_notify_count = 0;
static unsigned int atomic_wait_count = 0;
static unsigned int atomic_timedwait_count = 0;
#endif
/**
* Structure for holding small ModelChecker members that should be snapshotted
*/
struct model_snapshot_members {
model_snapshot_members() :
/* First thread created will have id INITIAL_THREAD_ID */
next_thread_id(INITIAL_THREAD_ID),
used_sequence_numbers(0),
bugs(),
asserted(false)
{ }
~model_snapshot_members() {
for (unsigned int i = 0;i < bugs.size();i++)
delete bugs[i];
bugs.clear();
}
unsigned int next_thread_id;
modelclock_t used_sequence_numbers;
SnapVector<bug_message *> bugs;
/** @brief Incorrectly-ordered synchronization was made */
bool asserted;
SNAPSHOTALLOC
};
/** @brief Constructor */
ModelExecution::ModelExecution(ModelChecker *m, Scheduler *scheduler) :
model(m),
params(NULL),
scheduler(scheduler),
thread_map(2), /* We'll always need at least 2 threads */
pthread_map(0),
pthread_counter(2),
action_trace(),
obj_map(),
condvar_waiters_map(),
obj_thrd_map(),
obj_wr_thrd_map(),
obj_last_sc_map(),
mutex_map(),
cond_map(),
thrd_last_action(1),
thrd_last_fence_release(),
priv(new struct model_snapshot_members ()),
mo_graph(new CycleGraph()),
fuzzer(new Fuzzer()),
isfinished(false),
instrnum(0)
{
/* Initialize a model-checker thread, for special ModelActions */
model_thread = new Thread(get_next_id());
add_thread(model_thread);
fuzzer->register_engine(m, this);
scheduler->register_engine(this);
#ifdef TLS
pthread_key_create(&pthreadkey, tlsdestructor);
#endif
}
/** @brief Destructor */
ModelExecution::~ModelExecution()
{
for (unsigned int i = INITIAL_THREAD_ID;i < get_num_threads();i++)
delete get_thread(int_to_id(i));
delete mo_graph;
delete priv;
}
int ModelExecution::get_execution_number() const
{
return model->get_execution_number();
}
static SnapVector<action_list_t> * get_safe_ptr_vect_action(HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> * hash, void * ptr)
{
SnapVector<action_list_t> *tmp = hash->get(ptr);
if (tmp == NULL) {
tmp = new SnapVector<action_list_t>();
hash->put(ptr, tmp);
}
return tmp;
}
static simple_action_list_t * get_safe_ptr_action(HashTable<const void *, simple_action_list_t *, uintptr_t, 2> * hash, void * ptr)
{
simple_action_list_t *tmp = hash->get(ptr);
if (tmp == NULL) {
tmp = new simple_action_list_t();
hash->put(ptr, tmp);
}
return tmp;
}
static SnapVector<simple_action_list_t> * get_safe_ptr_vect_action(HashTable<const void *, SnapVector<simple_action_list_t> *, uintptr_t, 2> * hash, void * ptr)
{
SnapVector<simple_action_list_t> *tmp = hash->get(ptr);
if (tmp == NULL) {
tmp = new SnapVector<simple_action_list_t>();
hash->put(ptr, tmp);
}
return tmp;
}
/**
* When vectors of action lists are reallocated due to resize, the root address of
* action lists may change. Hence we need to fix the parent pointer of the children
* of root.
*/
static void fixup_action_list(SnapVector<action_list_t> * vec)
{
for (uint i = 0;i < vec->size();i++) {
action_list_t * list = &(*vec)[i];
if (list != NULL)
list->fixupParent();
}
}
#ifdef COLLECT_STAT
static inline void record_atomic_stats(ModelAction * act)
{
switch (act->get_type()) {
case ATOMIC_WRITE:
atomic_store_count++;
break;
case ATOMIC_RMW:
atomic_load_count++;
break;
case ATOMIC_READ:
atomic_rmw_count++;
break;
case ATOMIC_FENCE:
atomic_fence_count++;
break;
case ATOMIC_LOCK:
atomic_lock_count++;
break;
case ATOMIC_TRYLOCK:
atomic_trylock_count++;
break;
case ATOMIC_UNLOCK:
atomic_unlock_count++;
break;
case ATOMIC_NOTIFY_ONE:
case ATOMIC_NOTIFY_ALL:
atomic_notify_count++;
break;
case ATOMIC_WAIT:
atomic_wait_count++;
break;
case ATOMIC_TIMEDWAIT:
atomic_timedwait_count++;
default:
return;
}
}
void print_atomic_accesses()
{
model_print("atomic store count: %u\n", atomic_store_count);
model_print("atomic load count: %u\n", atomic_load_count);
model_print("atomic rmw count: %u\n", atomic_rmw_count);
model_print("atomic fence count: %u\n", atomic_fence_count);
model_print("atomic lock count: %u\n", atomic_lock_count);
model_print("atomic trylock count: %u\n", atomic_trylock_count);
model_print("atomic unlock count: %u\n", atomic_unlock_count);
model_print("atomic notify count: %u\n", atomic_notify_count);
model_print("atomic wait count: %u\n", atomic_wait_count);
model_print("atomic timedwait count: %u\n", atomic_timedwait_count);
}
#endif
/** @return a thread ID for a new Thread */
thread_id_t ModelExecution::get_next_id()
{
return priv->next_thread_id++;
}
/** @return the number of user threads created during this execution */
unsigned int ModelExecution::get_num_threads() const
{
return priv->next_thread_id;
}
/** @return a sequence number for a new ModelAction */
modelclock_t ModelExecution::get_next_seq_num()
{
return ++priv->used_sequence_numbers;
}
/** @return a sequence number for a new ModelAction */
modelclock_t ModelExecution::get_curr_seq_num()
{
return priv->used_sequence_numbers;
}
/** Restore the last used sequence number when actions of a thread are postponed by Fuzzer */
void ModelExecution::restore_last_seq_num()
{
priv->used_sequence_numbers--;
}
/**
* @brief Should the current action wake up a given thread?
*
* @param curr The current action
* @param thread The thread that we might wake up
* @return True, if we should wake up the sleeping thread; false otherwise
*/
bool ModelExecution::should_wake_up(const ModelAction * asleep) const
{
/* The sleep is literally sleeping */
switch (asleep->get_type()) {
case THREAD_SLEEP:
if (fuzzer->shouldWake(asleep))
return true;
break;
case ATOMIC_WAIT:
case ATOMIC_TIMEDWAIT:
if (fuzzer->waitShouldWakeUp(asleep))
return true;
break;
default:
return false;
}
return false;
}
void ModelExecution::wake_up_sleeping_actions()
{
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
thread_id_t tid = int_to_id(i);
if (scheduler->is_sleep_set(tid)) {
Thread *thr = get_thread(tid);
ModelAction * pending = thr->get_pending();
if (should_wake_up(pending)) {
/* Remove this thread from sleep set */
scheduler->remove_sleep(thr);
if (pending->is_sleep()) {
thr->set_wakeup_state(true);
} else if (pending->is_wait()) {
thr->set_wakeup_state(true);
/* Remove this thread from list of waiters */
simple_action_list_t *waiters = get_safe_ptr_action(&condvar_waiters_map, pending->get_location());
for (sllnode<ModelAction *> * rit = waiters->begin();rit != NULL;rit=rit->getNext()) {
if (rit->getVal()->get_tid() == tid) {
waiters->erase(rit);
break;
}
}
/* Set ETIMEDOUT error */
if (pending->is_timedwait())
thr->set_return_value(ETIMEDOUT);
}
}
}
}
}
void ModelExecution::assert_bug(const char *msg)
{
priv->bugs.push_back(new bug_message(msg));
set_assert();
}
/** @return True, if any bugs have been reported for this execution */
bool ModelExecution::have_bug_reports() const
{
return priv->bugs.size() != 0;
}
SnapVector<bug_message *> * ModelExecution::get_bugs() const
{
return &priv->bugs;
}
/**
* Check whether the current trace has triggered an assertion which should halt
* its execution.
*
* @return True, if the execution should be aborted; false otherwise
*/
bool ModelExecution::has_asserted() const
{
return priv->asserted;
}
/**
* Trigger a trace assertion which should cause this execution to be halted.
* This can be due to a detected bug or due to an infeasibility that should
* halt ASAP.
*/
void ModelExecution::set_assert()
{
priv->asserted = true;
}
/**
* Check if we are in a deadlock. Should only be called at the end of an
* execution, although it should not give false positives in the middle of an
* execution (there should be some ENABLED thread).
*
* @return True if program is in a deadlock; false otherwise
*/
bool ModelExecution::is_deadlocked() const
{
bool blocking_threads = false;
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
thread_id_t tid = int_to_id(i);
if (is_enabled(tid))
return false;
Thread *t = get_thread(tid);
if (!t->is_model_thread() && t->get_pending())
blocking_threads = true;
}
return blocking_threads;
}
/**
* Check if this is a complete execution. That is, have all thread completed
* execution (rather than exiting because sleep sets have forced a redundant
* execution).
*
* @return True if the execution is complete.
*/
bool ModelExecution::is_complete_execution() const
{
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++)
if (is_enabled(int_to_id(i)))
return false;
return true;
}
ModelAction * ModelExecution::convertNonAtomicStore(void * location) {
uint64_t value = *((const uint64_t *) location);
modelclock_t storeclock;
thread_id_t storethread;
getStoreThreadAndClock(location, &storethread, &storeclock);
setAtomicStoreFlag(location);
ModelAction * act = new ModelAction(NONATOMIC_WRITE, memory_order_relaxed, location, value, get_thread(storethread));
act->set_seq_number(storeclock);
add_normal_write_to_lists(act);
add_write_to_lists(act);
w_modification_order(act);
return act;
}
/**
* Processes a read model action.
* @param curr is the read model action to process.
* @param rf_set is the set of model actions we can possibly read from
* @return True if the read can be pruned from the thread map list.
*/
bool ModelExecution::process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set)
{
SnapVector<ModelAction *> * priorset = new SnapVector<ModelAction *>();
bool hasnonatomicstore = hasNonAtomicStore(curr->get_location());
if (hasnonatomicstore) {
ModelAction * nonatomicstore = convertNonAtomicStore(curr->get_location());
rf_set->push_back(nonatomicstore);
}
// Remove writes that violate read modification order
/*
uint i = 0;
while (i < rf_set->size()) {
ModelAction * rf = (*rf_set)[i];
if (!r_modification_order(curr, rf, NULL, NULL, true)) {
(*rf_set)[i] = rf_set->back();
rf_set->pop_back();
} else
i++;
}*/
while(true) {
int index = fuzzer->selectWrite(curr, rf_set);
ModelAction *rf = (*rf_set)[index];
ASSERT(rf);
bool canprune = false;
if (r_modification_order(curr, rf, priorset, &canprune)) {
for(unsigned int i=0;i<priorset->size();i++) {
mo_graph->addEdge((*priorset)[i], rf);
}
read_from(curr, rf);
get_thread(curr)->set_return_value(rf->get_write_value());
delete priorset;
//Update acquire fence clock vector
ClockVector * hbcv = get_hb_from_write(rf);
if (hbcv != NULL)
get_thread(curr)->get_acq_fence_cv()->merge(hbcv);
return canprune && (curr->get_type() == ATOMIC_READ);
}
priorset->clear();
(*rf_set)[index] = rf_set->back();
rf_set->pop_back();
}
}
/**
* Processes a lock, trylock, or unlock model action. @param curr is
* the read model action to process.
*
* The try lock operation checks whether the lock is taken. If not,
* it falls to the normal lock operation case. If so, it returns
* fail.
*
* The lock operation has already been checked that it is enabled, so
* it just grabs the lock and synchronizes with the previous unlock.
*
* The unlock operation has to re-enable all of the threads that are
* waiting on the lock.
*
* @return True if synchronization was updated; false otherwise
*/
bool ModelExecution::process_mutex(ModelAction *curr)
{
cdsc::mutex *mutex = curr->get_mutex();
struct cdsc::mutex_state *state = NULL;
if (mutex)
state = mutex->get_state();
switch (curr->get_type()) {
case ATOMIC_TRYLOCK: {
bool success = !state->locked;
curr->set_try_lock(success);
if (!success) {
get_thread(curr)->set_return_value(0);
break;
}
get_thread(curr)->set_return_value(1);
}
//otherwise fall into the lock case
case ATOMIC_LOCK: {
//TODO: FIND SOME BETTER WAY TO CHECK LOCK INITIALIZED OR NOT
//if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock)
// assert_bug("Lock access before initialization");
// TODO: lock count for recursive mutexes
state->locked = get_thread(curr);
ModelAction *unlock = get_last_unlock(curr);
//synchronize with the previous unlock statement
if (unlock != NULL) {
synchronize(unlock, curr);
return true;
}
break;
}
case ATOMIC_WAIT: {
Thread *curr_thrd = get_thread(curr);
/* wake up the other threads */
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
Thread *t = get_thread(int_to_id(i));
if (t->waiting_on() == curr_thrd && t->get_pending()->is_lock())
scheduler->wake(t);
}
/* unlock the lock - after checking who was waiting on it */
state->locked = NULL;
/* disable this thread */
simple_action_list_t * waiters = get_safe_ptr_action(&condvar_waiters_map, curr->get_location());
waiters->push_back(curr);
curr_thrd->set_pending(curr); // Forbid this thread to stash a new action
if (fuzzer->waitShouldFail(curr)) // If wait should fail spuriously,
scheduler->add_sleep(curr_thrd); // place this thread into THREAD_SLEEP_SET
else
scheduler->sleep(curr_thrd);
break;
}
case ATOMIC_TIMEDWAIT: {
Thread *curr_thrd = get_thread(curr);
if (!fuzzer->randomizeWaitTime(curr)) {
curr_thrd->set_return_value(ETIMEDOUT);
return false;
}
/* wake up the other threads */
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
Thread *t = get_thread(int_to_id(i));
if (t->waiting_on() == curr_thrd && t->get_pending()->is_lock())
scheduler->wake(t);
}
/* unlock the lock - after checking who was waiting on it */
state->locked = NULL;
/* disable this thread */
simple_action_list_t * waiters = get_safe_ptr_action(&condvar_waiters_map, curr->get_location());
waiters->push_back(curr);
curr_thrd->set_pending(curr); // Forbid this thread to stash a new action
scheduler->add_sleep(curr_thrd);
break;
}
case ATOMIC_UNLOCK: {
// TODO: lock count for recursive mutexes
/* wake up the other threads */
Thread *curr_thrd = get_thread(curr);
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
Thread *t = get_thread(int_to_id(i));
if (t->waiting_on() == curr_thrd && t->get_pending()->is_lock())
scheduler->wake(t);
}
/* unlock the lock - after checking who was waiting on it */
state->locked = NULL;
break;
}
case ATOMIC_NOTIFY_ALL: {
simple_action_list_t *waiters = get_safe_ptr_action(&condvar_waiters_map, curr->get_location());
//activate all the waiting threads
for (sllnode<ModelAction *> * rit = waiters->begin();rit != NULL;rit=rit->getNext()) {
Thread * thread = get_thread(rit->getVal());
if (thread->get_state() != THREAD_COMPLETED)
scheduler->wake(thread);
thread->set_wakeup_state(true);
}
waiters->clear();
break;
}
case ATOMIC_NOTIFY_ONE: {
simple_action_list_t *waiters = get_safe_ptr_action(&condvar_waiters_map, curr->get_location());
if (waiters->size() != 0) {
Thread * thread = fuzzer->selectNotify(waiters);
if (thread->get_state() != THREAD_COMPLETED)
scheduler->wake(thread);
thread->set_wakeup_state(true);
}
break;
}
default:
ASSERT(0);
}
return false;
}
/**
* Process a write ModelAction
* @param curr The ModelAction to process
* @return True if the mo_graph was updated or promises were resolved
*/
void ModelExecution::process_write(ModelAction *curr)
{
w_modification_order(curr);
get_thread(curr)->set_return_value(VALUE_NONE);
}
/**
* Process a fence ModelAction
* @param curr The ModelAction to process
* @return True if synchronization was updated
*/
void ModelExecution::process_fence(ModelAction *curr)
{
/*
* fence-relaxed: no-op
* fence-release: only log the occurence (not in this function), for
* use in later synchronization
* fence-acquire (this function): search for hypothetical release
* sequences
* fence-seq-cst: MO constraints formed in {r,w}_modification_order
*/
if (curr->is_acquire()) {
curr->get_cv()->merge(get_thread(curr)->get_acq_fence_cv());
}
}
/**
* @brief Process the current action for thread-related activity
*
* Performs current-action processing for a THREAD_* ModelAction. Proccesses
* may include setting Thread status, completing THREAD_FINISH/THREAD_JOIN
* synchronization, etc. This function is a no-op for non-THREAD actions
* (e.g., ATOMIC_{READ,WRITE,RMW,LOCK}, etc.)
*
* @param curr The current action
* @return True if synchronization was updated or a thread completed
*/
void ModelExecution::process_thread_action(ModelAction *curr)
{
switch (curr->get_type()) {
case THREAD_CREATE: {
thrd_t *thrd = (thrd_t *)curr->get_location();
struct thread_params *params = (struct thread_params *)curr->get_value();
Thread *th = new Thread(get_next_id(), thrd, params->func, params->arg, get_thread(curr));
curr->set_thread_operand(th);
add_thread(th);
th->set_creation(curr);
break;
}
case PTHREAD_CREATE: {
(*(uint32_t *)curr->get_location()) = pthread_counter++;
struct pthread_params *params = (struct pthread_params *)curr->get_value();
Thread *th = new Thread(get_next_id(), NULL, params->func, params->arg, get_thread(curr));
curr->set_thread_operand(th);
add_thread(th);
th->set_creation(curr);
if ( pthread_map.size() < pthread_counter )
pthread_map.resize( pthread_counter );
pthread_map[ pthread_counter-1 ] = th;
break;
}
case THREAD_JOIN: {
Thread *blocking = curr->get_thread_operand();
ModelAction *act = get_last_action(blocking->get_id());
synchronize(act, curr);
break;
}
case PTHREAD_JOIN: {
Thread *blocking = curr->get_thread_operand();
ModelAction *act = get_last_action(blocking->get_id());
synchronize(act, curr);
break;
}
case THREADONLY_FINISH:
case THREAD_FINISH: {
Thread *th = get_thread(curr);
if (curr->get_type() == THREAD_FINISH &&
th == model->getInitThread()) {
th->complete();
setFinished();
break;
}
/* Wake up any joining threads */
for (unsigned int i = MAIN_THREAD_ID;i < get_num_threads();i++) {
Thread *waiting = get_thread(int_to_id(i));
if (waiting->waiting_on() == th &&
waiting->get_pending()->is_thread_join())
scheduler->wake(waiting);
}
th->complete();
break;
}
case THREAD_START: {
break;
}
case THREAD_SLEEP: {
Thread *th = get_thread(curr);
th->set_pending(curr);
scheduler->add_sleep(th);
break;
}
default:
break;
}
}
/**
* Initialize the current action by performing one or more of the following
* actions, as appropriate: merging RMWR and RMWC/RMW actions,
* manipulating backtracking sets, allocating and
* initializing clock vectors, and computing the promises to fulfill.
*
* @param curr The current action, as passed from the user context; may be
* freed/invalidated after the execution of this function, with a different
* action "returned" its place (pass-by-reference)
* @return True if curr is a newly-explored action; false otherwise
*/
bool ModelExecution::initialize_curr_action(ModelAction **curr)
{
if ((*curr)->is_rmwc() || (*curr)->is_rmw()) {
ModelAction *newcurr = process_rmw(*curr);
delete *curr;
*curr = newcurr;
return false;
} else {
ModelAction *newcurr = *curr;
newcurr->set_seq_number(get_next_seq_num());
/* Always compute new clock vector */
newcurr->create_cv(get_parent_action(newcurr->get_tid()));
/* Assign most recent release fence */
newcurr->set_last_fence_release(get_last_fence_release(newcurr->get_tid()));
return true; /* This was a new ModelAction */
}
}
/**
* @brief Establish reads-from relation between two actions
*
* Perform basic operations involved with establishing a concrete rf relation,
* including setting the ModelAction data and checking for release sequences.
*
* @param act The action that is reading (must be a read)
* @param rf The action from which we are reading (must be a write)
*
* @return True if this read established synchronization
*/
void ModelExecution::read_from(ModelAction *act, ModelAction *rf)
{
ASSERT(rf);
ASSERT(rf->is_write());
act->set_read_from(rf);
if (act->is_acquire()) {
ClockVector *cv = get_hb_from_write(rf);
if (cv == NULL)
return;
act->get_cv()->merge(cv);
}
}
/**
* @brief Synchronizes two actions
*
* When A synchronizes with B (or A --sw-> B), B inherits A's clock vector.
* This function performs the synchronization as well as providing other hooks
* for other checks along with synchronization.
*
* @param first The left-hand side of the synchronizes-with relation
* @param second The right-hand side of the synchronizes-with relation
* @return True if the synchronization was successful (i.e., was consistent
* with the execution order); false otherwise
*/
bool ModelExecution::synchronize(const ModelAction *first, ModelAction *second)
{
if (*second < *first) {
ASSERT(0); //This should not happend
return false;
}
return second->synchronize_with(first);
}
/**
* @brief Check whether a model action is enabled.
*
* Checks whether an operation would be successful (i.e., is a lock already
* locked, or is the joined thread already complete).
*
* For yield-blocking, yields are never enabled.
*
* @param curr is the ModelAction to check whether it is enabled.
* @return a bool that indicates whether the action is enabled.
*/
bool ModelExecution::check_action_enabled(ModelAction *curr) {
switch (curr->get_type()) {
case ATOMIC_LOCK: {
cdsc::mutex *lock = curr->get_mutex();
struct cdsc::mutex_state *state = lock->get_state();
if (state->locked) {
Thread *lock_owner = (Thread *)state->locked;
Thread *curr_thread = get_thread(curr);
if (lock_owner == curr_thread && state->type == PTHREAD_MUTEX_RECURSIVE) {
return true;
}
return false;
}
break;
}
case THREAD_JOIN:
case PTHREAD_JOIN: {
Thread *blocking = curr->get_thread_operand();
if (!blocking->is_complete()) {
return false;
}
break;
}
case THREAD_SLEEP: {
if (!fuzzer->shouldSleep(curr))
return false;
break;
}
default:
return true;
}
return true;
}
/**
* This is the heart of the model checker routine. It performs model-checking
* actions corresponding to a given "current action." Among other processes, it
* calculates reads-from relationships, updates synchronization clock vectors,
* forms a memory_order constraints graph, and handles replay/backtrack
* execution when running permutations of previously-observed executions.
*
* @param curr The current action to process
* @return The ModelAction that is actually executed; may be different than
* curr
*/
ModelAction * ModelExecution::check_current_action(ModelAction *curr)
{
ASSERT(curr);
bool newly_explored = initialize_curr_action(&curr);
DBG();
wake_up_sleeping_actions();
if(curr->in_count()){
instrnum++;
// model_print("current we have %d instrnums. \n", instrnum);
}
SnapVector<ModelAction *> * rf_set = NULL;
bool canprune = false;
/* Build may_read_from set for newly-created actions */
if (curr->is_read() && newly_explored) {
rf_set = build_may_read_from(curr);
canprune = process_read(curr, rf_set);
delete rf_set;
} else
ASSERT(rf_set == NULL);
/* Add the action to lists if not the second part of a rmw */
if (newly_explored) {
#ifdef COLLECT_STAT
record_atomic_stats(curr);
#endif
add_action_to_lists(curr, canprune);
}
if (curr->is_write())
add_write_to_lists(curr);
process_thread_action(curr);
if (curr->is_write())
process_write(curr);
if (curr->is_fence())
process_fence(curr);
if (curr->is_mutex_op())
process_mutex(curr);
return curr;
}
/** Close out a RMWR by converting previous RMWR into a RMW or READ. */
ModelAction * ModelExecution::process_rmw(ModelAction *act) {
ModelAction *lastread = get_last_action(act->get_tid());
lastread->process_rmw(act);
if (act->is_rmw()) {
mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
}
return lastread;
}
/**
* @brief Updates the mo_graph with the constraints imposed from the current
* read.
*
* Basic idea is the following: Go through each other thread and find
* the last action that happened before our read. Two cases:
*
* -# The action is a write: that write must either occur before
* the write we read from or be the write we read from.
* -# The action is a read: the write that that action read from
* must occur before the write we read from or be the same write.
*
* @param curr The current action. Must be a read.
* @param rf The ModelAction or Promise that curr reads from. Must be a write.
* @param check_only If true, then only check whether the current action satisfies
* read modification order or not, without modifiying priorset and canprune.
* False by default.
* @return True if modification order edges were added; false otherwise
*/
bool ModelExecution::r_modification_order(ModelAction *curr, const ModelAction *rf,
SnapVector<ModelAction *> * priorset, bool * canprune)
{
SnapVector<action_list_t> *thrd_lists = obj_thrd_map.get(curr->get_location());
ASSERT(curr->is_read());
/* Last SC fence in the current thread */
ModelAction *last_sc_fence_local = get_last_seq_cst_fence(curr->get_tid(), NULL);
int tid = curr->get_tid();
/* Need to ensure thrd_lists is big enough because we have not added the curr actions yet. */
if ((int)thrd_lists->size() <= tid) {
uint oldsize = thrd_lists->size();
thrd_lists->resize(priv->next_thread_id);
for(uint i = oldsize;i < priv->next_thread_id;i++)
new (&(*thrd_lists)[i]) action_list_t();
fixup_action_list(thrd_lists);
}
ModelAction *prev_same_thread = NULL;
/* Iterate over all threads */
for (unsigned int i = 0;i < thrd_lists->size();i++, tid = (((unsigned int)(tid+1)) == thrd_lists->size()) ? 0 : tid + 1) {
/* Last SC fence in thread tid */
ModelAction *last_sc_fence_thread_local = NULL;
if (i != 0)
last_sc_fence_thread_local = get_last_seq_cst_fence(int_to_id(tid), NULL);
/* Last SC fence in thread tid, before last SC fence in current thread */
ModelAction *last_sc_fence_thread_before = NULL;
if (last_sc_fence_local)
last_sc_fence_thread_before = get_last_seq_cst_fence(int_to_id(tid), last_sc_fence_local);
//Only need to iterate if either hb has changed for thread in question or SC fence after last operation...
if (prev_same_thread != NULL &&
(prev_same_thread->get_cv()->getClock(tid) == curr->get_cv()->getClock(tid)) &&
(last_sc_fence_thread_local == NULL || *last_sc_fence_thread_local < *prev_same_thread)) {
continue;
}
/* Iterate over actions in thread, starting from most recent */
action_list_t *list = &(*thrd_lists)[tid];
sllnode<ModelAction *> * rit;
for (rit = list->end();rit != NULL;rit=rit->getPrev()) {
ModelAction *act = rit->getVal();
/* Skip curr */
if (act == curr)
continue;
/* Don't want to add reflexive edges on 'rf' */
if (act->equals(rf)) {
if (act->happens_before(curr))
break;
else
continue;
}
if (act->is_write()) {
/* C++, Section 29.3 statement 5 */
if (curr->is_seqcst() && last_sc_fence_thread_local &&
*act < *last_sc_fence_thread_local) {
if (mo_graph->checkReachable(rf, act))
return false;
priorset->push_back(act);
break;
}
/* C++, Section 29.3 statement 4 */
else if (act->is_seqcst() && last_sc_fence_local &&
*act < *last_sc_fence_local) {
if (mo_graph->checkReachable(rf, act))
return false;
priorset->push_back(act);
break;
}
/* C++, Section 29.3 statement 6 */
else if (last_sc_fence_thread_before &&
*act < *last_sc_fence_thread_before) {
if (mo_graph->checkReachable(rf, act))
return false;
priorset->push_back(act);
break;
}
}
/*
* Include at most one act per-thread that "happens
* before" curr
*/
if (act->happens_before(curr)) {