-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.cc
2780 lines (2419 loc) · 84.2 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),
maxinstr(0),
history_(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 simple_action_list_t * get_safe_ptr_action_thread(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;
}
void ModelExecution::print_actset(SnapVector<ModelAction *> * act_set){
int len = act_set->size();
model_print("print act_set : current action set size: %d. - ", len);
for(int i = 0; i < len; i++){
ModelAction * act = (*act_set)[i];
model_print("[action on thread %d, location: %14p, seq_nums: %u ]",
id_to_int(act->get_tid()), act->get_location(), act->get_seq_number());
}
model_print("\n");
}
// // weak memory - func1:
/**
* Update a vector by the new action. Return a variable vector
* @param input_vec The old variable vector
* @param curr The new action
* @return Desired new variable vector
*/
SnapVector<ModelAction*> * ModelExecution::updateVec(SnapVector<ModelAction*> *input_vec, ModelAction* curr){
int len = input_vec->size();
for(int i = 0; i < len; i++){
ModelAction* iteract = (*input_vec)[i];
if(curr->get_location() == iteract->get_location()){
if(iteract->get_seq_number() > curr->get_seq_number()){ // update only when the new action(curr) has larger sequence number
(*input_vec)[i] = iteract;
}
return input_vec;
}
}
input_vec->push_back(curr);
return input_vec;
}
// // weak memory - func2:
// /**
// * a vector saves the newest variable
// * @param Eacc The accumulate vector
// * @param local_vec The local vector on the thread
// * @return Desired vector with newest variable
// */
SnapVector<ModelAction*> * ModelExecution::maxVec(SnapVector<ModelAction*> * Eacc, SnapVector<ModelAction*> *local_vec){
uint Eacc_len = Eacc->size();
uint local_vec_len = local_vec->size();
SnapVector<ModelAction* > * res = new SnapVector<ModelAction *> ();
for(uint i = 0; i < Eacc_len; i++){
ModelAction* act1 = (*Eacc)[i]; // the variable in accumulate vector
res = updateVec(res, act1);
// uint localvec_idx = local_vec->get_index(act1);
// static const uint NoVariable = -1;
// if(localvec_idx != NoVariable){// have this variable
// ModelAction* act2 = (*local_vec)[localvec_idx]; // the same variable
// if(act1->get_seq_number() > act2->get_seq_number()){
// (*local_vec)[localvec_idx] = act1;
// }
// }
// else{
// local_vec->push_back(act1);
// }
}
for(uint i = 0; i < local_vec_len; i++){
ModelAction* act2 = (*local_vec)[i];
res = updateVec(res, act2);
}
return res;
}
// for (it = action_trace.end();it != NULL;it = it->getPrev()) {
// if (counter > length)
// break;
// ModelAction * act = it->getVal();
// list.push_front(act);
// counter++;
// }
// // weak memory implementation test - func3
// /**
// * Iterate all actions on the current thread to build the bag for this action
// * @param rd the read action
// * @param curr the action to iterate(the selected write)
// * @return Desired new variable vector
// */
SnapVector<ModelAction *> * ModelExecution::computeUpdate(ModelAction *rd, ModelAction * curr)
{
ASSERT(rd->is_read()); // the inital read action
ASSERT(curr->is_write()); // the randomly selected write action
SnapVector<ModelAction *> * Eres = new SnapVector<ModelAction *>(); // the result E
SnapVector<ModelAction *> * Eacc = new SnapVector<ModelAction *>(); // the accumulate bag
SnapVector<action_list_t> *thrd_lists = obj_thrd_map.get(curr->get_location()); // get all actions on one thread
// the thread of read action - get local vector
int rd_tid = rd->get_tid();
Thread *rd_thr = get_thread(rd_tid);
SnapVector<ModelAction *> * rd_localvec = rd_thr->get_local_vec();
//model_print("computeUpdate for action %u on thread %d : the localvec on read action's thread, size: %d.\n ", rd->get_seq_number(), rd_tid, rd_localvec->size());
//print_actset(rd_localvec);
// the thread of write action - iteration
// int wr_tid = curr->get_tid(); // get the current thread id
// action_list_t *wr_list = &(*thrd_lists)[wr_tid]; // get the thread of write action
// sllnode<ModelAction *> * rit;
bool before_flag = false;
updateVec(Eacc, curr);
//model_print("first put the write action in Eacc. \n");
//print_actset(Eacc);
//model_print("Start updating the bag for read action %d. \n", rd->get_seq_number());
sllnode<ModelAction*> *it;
for (it = action_trace.end();it != NULL;it = it->getPrev()) { // get all actions before current action
ModelAction *act = it->getVal();
const char *type_str = act->get_type_str();
const char *mo_str = act->get_mo_str();
if(act == curr){
before_flag = true;
//model_print("action before the write:");
}
if(before_flag && act != curr && act->get_tid() == curr->get_tid()){// iterate all actions before the current action
//model_print("\n computeUpdate: iteration action type is %-14s. on thread %d, sequence number is : %d , location: %14p, mo_type is : %7s. \n",
//type_str, id_to_int(act->get_tid()), act->get_seq_number(),act->get_location(), mo_str);
// model_print("(Iteration action seq_num: %u. type: %-14s, location: %14p. threadid: %d",
// act->get_seq_number(), act->get_type_str(), act->get_location(), act->get_tid());
//model_print("value: %" PRIx64 ")\n", act->get_value());
if(act->is_thread_start()){//stop condition 1: reach the start of a thread
//model_print("meet the thread start. \n");
Eres = Eacc;
break;
}
// else if(!act->is_write() && (act->is_read() && !act->checkbag())){
// continue;
// }
else if(act->checkbag()){// stop condtion2: reach an action with bag ( read, sc, fence)
Eacc = maxVec(Eacc, act->get_bag());
Eres = maxVec(Eacc, rd_localvec); // merge the accumulate vector with local vector
//model_print("meet one action with bag. break. ");
break;
}
else if(act->is_write() && act->is_release()){ //is_release includes: release,acq_rel, seq_cst
//model_print("meet a write which is release. ");
Eacc = updateVec(Eacc, act);
Eres = Eacc;
}
// else if(act->is_fence() && act->is_acquire() && act->checkbag()){
// model_print("meet a fence_acquire with bag. ");
// Eacc = maxVec(Eacc, act->get_bag());
// Eres = Eacc;
// break; // stop condition 3: meet a fence_acquire with bag
// }
// else if(act->is_seqcst() &&(act->is_read() || act->is_write())){
// model_print("meet a sc write/read with bag. ");
// Eacc = maxVec(Eacc, act->get_bag());
// Eres = Eacc;
// break; // stop condition 4: meet a fence_acquire with bag
// }
}
}
//model_print("\n");
//model_print("End computeUpdate: iteration bag result: Eres size is %d \n", Eres->size());
//print_actset(Eres);
rd_localvec = maxVec(Eres, rd_localvec);
Eres = rd_localvec;
rd_thr->set_local_vec(rd_localvec);
//model_print("After process read, the thread local vec becomes \t");
//rd_thr->print_local_vec();
rd->set_bag(Eres);
//model_print("After process read, the action set a bag. \t");
//rd->print_bag();
//model_print("\n \n");
return Eres;
}
SnapVector<ModelAction *> * ModelExecution::computeUpdate_fence(ModelAction *fence_acq, ModelAction * fence_rel)
{
ASSERT(fence_acq->is_acquire()); // the inital read action
ASSERT(fence_rel->is_release()); // the randomly selected write action
SnapVector<ModelAction *> * Eres = new SnapVector<ModelAction *>(); // the result E
SnapVector<ModelAction *> * Eacc = new SnapVector<ModelAction *>(); // the accumulate bag
// the thread of read action - get local vector
int acq_tid = fence_acq->get_tid();
Thread *acq_thr = get_thread(acq_tid);
SnapVector<ModelAction *> * acq_localvec = acq_thr->get_local_vec();
//model_print("computeUpdate for fence %u on thread %d : the localvec on read action's thread, size: %d.\n ",
//fence_acq->get_seq_number(), acq_tid, acq_localvec->size());
//print_actset(acq_localvec);
// the thread of write action - iteration
// int wr_tid = curr->get_tid(); // get the current thread id
// action_list_t *wr_list = &(*thrd_lists)[wr_tid]; // get the thread of write action
// sllnode<ModelAction *> * rit;
bool before_flag = false;
//model_print("Start updating the bag for fence_acq action %d. \n", fence_acq->get_seq_number());
sllnode<ModelAction*> *it;
for (it = action_trace.end();it != NULL;it = it->getPrev()) { // get all actions before current action
ModelAction *act = it->getVal();
const char *type_str = act->get_type_str();
const char *mo_str = act->get_mo_str();
if(act == fence_rel){
before_flag = true;
//model_print("action before the fence_release:");
}
if(before_flag && act != fence_rel && act->get_tid() == fence_rel->get_tid()){// iterate all actions before the current action
//model_print("\n computeUpdate: iteration action type is %-14s. on thread %d, sequence number is : %d , location: %14p, mo_type is : %7s. \n",
//type_str, id_to_int(act->get_tid()), act->get_seq_number(),act->get_location(), mo_str);
// model_print("(Iteration action seq_num: %u. type: %-14s, location: %14p. threadid: %d",
// act->get_seq_number(), act->get_type_str(), act->get_location(), act->get_tid());
//model_print("value: %" PRIx64 ")\n", act->get_value());
if(act->is_thread_start()){//stop condition 1: reach the start of a thread
//model_print("meet the thread start. \n");
Eres = Eacc;
break;
}
// else if(!act->is_write() && (act->is_read() && !act->checkbag())){
// continue;
// }
else if(act->checkbag()){// stop condtion2: reach an action with bag(read or sc)
Eacc = maxVec(Eacc, act->get_bag());
Eres = maxVec(Eacc, acq_localvec); // merge the accumulate vector with local vector
//model_print("meet one read with bag. break. ");
break;
}
else if(act->is_write() && act->is_release()){ // is_release include: release, acq_rel, seq_cst
//model_print("meet a write which is release. ");
Eacc = updateVec(Eacc, act);
Eres = Eacc;
}
// else if(act->is_seqcst() && (act->is_read() || act->is_write())){// stop condtion3: reach an sc_action with bag
// Eacc = maxVec(Eacc, act->get_bag());
// Eres = maxVec(Eacc, acq_localvec); // merge the accumulate vector with local vector
// model_print("meet one read with bag. break. ");
// break;
// }
}
}
//model_print("\n");
//model_print("End computeUpdate: iteration bag result: Eres size is %d \n", Eres->size());
//print_actset(Eres);
acq_localvec = maxVec(Eres, acq_localvec);
Eres = acq_localvec;
// acq_thr->set_local_vec(acq_localvec);
// model_print("After process fence, the thread local vec becomes \t");
// acq_thr->print_local_vec();
// fence_acq->set_bag(Eres);
// model_print("After process fence, the action set a bag. \t");
// fence_acq->print_bag();
// model_print("\n \n");
return Eres;
}
// /**
// * 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.
// * weak memory version
// */
bool ModelExecution::process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set, bool read_external)
{
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);
}
SnapVector<ModelAction*> * tmpbag = new SnapVector<ModelAction *> ();
if(curr->is_seqcst()){
//model_print("for seqcst read: first find the last sc and put the bag. \n");
tmpbag = computeBag_sc(curr);
}
// 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();
// }
// weak memory
while(true) {
// step 1 : prepare
ModelAction *rf;
int index;
//model_print("current read action location: %u, threadid : %u \n",
//curr->get_location(),id_to_int(curr->get_tid()));
// step2: get the read action related info
int rd_tid = curr->get_tid();
Thread *rd_thr = get_thread(rd_tid);
//model_print("In process read: current localvec size is %d.\n", rd_thr->get_localvec_size());
//rd_thr->print_local_vec();
// step3: read externally or internally
if(read_external){ // ask to read externally
//model_print("Process read: read externally. \n");
index = fuzzer->selectWrite(curr, rf_set);
rf = (*rf_set)[index]; // a randomly selected write
rd_thr->update_local_vec(rf);
SnapVector<ModelAction *> * tmp_bag = new SnapVector<ModelAction *> ();
updateVec(tmp_bag, rf);
curr->set_bag(tmp_bag); // set the bag of this read with bag
rd_thr->update_local_vec(rf);// update the localvec on this thread based on the write
if(curr->could_synchronize_with(rf)){
//model_print("could synchronize with the write. start looping. \n");
computeUpdate(curr, rf); // it will not change the selection of write - but update local vec
}
if(curr->is_seqcst()){ // if is read_sc one more step
//model_print("for the read_sc, one more step. \n");
curr->set_bag(maxVec(curr->get_bag(), tmpbag));
rd_thr->set_local_vec(maxVec(rd_thr->get_local_vec(), curr->get_bag()));
}
//the same as original c11tester: delete this rf_set
// (*rf_set)[index] = rf_set->back();
// rf_set->pop_back();
}
else{ // ask to use the local vec variable
// for read local: first update the localvec and bag with last sc
if(curr->is_seqcst()){ // if is read_sc one more step
//model_print("for the read_sc, one more step. \n");
curr->set_bag(tmpbag);
rd_thr->set_local_vec(maxVec(rd_thr->get_local_vec(), curr->get_bag()));
}
// then process the read
rf = rd_thr->get_same_location_act(curr); // the local vec doesnot have the variable(location)
//model_print("Process read: read locally. \n");
if(rf){ // the local vec has such variable
//model_print("local vec has such write, seqnum:%d \n", rf->get_seq_number());
index = fuzzer->find_idx(rf_set, rf);
if(index != -1){ // to make sure this variable locally is readable
//model_print("localvec has such variable \n");
rf = (*rf_set)[index];
// (*rf_set)[index] = rf_set->back();
// rf_set->pop_back();
// localvec has the same variable
}
else{
//model_print("localvec has one variable. but not in the rf_set \n");
//model_print("rf_set size is: %u. \n", rf_set->size());
index = fuzzer->selectWrite(curr, rf_set);
rf = (*rf_set)[index];
// (*rf_set)[index] = rf_set->back();
// rf_set->pop_back();
}
}
else{// the local vec has no such variable
//model_print("localvec has no variable. randomly select from rf_set. \n");
//model_print("rf_set size is: %u. \n", rf_set->size());
index = fuzzer->selectWrite(curr, rf_set);
rf = (*rf_set)[index];
// (*rf_set)[index] = rf_set->back();
// rf_set->pop_back();
}
}
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 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.
* c11tester version
*/
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;
}