-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzer.cc
104 lines (87 loc) · 2.72 KB
/
fuzzer.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
#include "fuzzer.h"
#include <stdlib.h>
#include "threads-model.h"
#include "model.h"
#include "action.h"
int Fuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set) {
int random_index = random() % rf_set->size();
return random_index;
}
int Fuzzer::find_idx(SnapVector<ModelAction *> * rf_set, ModelAction* chosen_wr){
int index = -1;
for(uint i = 0; i < rf_set->size(); i++){
ModelAction* curr = (*rf_set)[i];
if(curr == chosen_wr){
index = i;
break;
}
}
return index;
}
Thread * Fuzzer::selectThread(int * threadlist, int numthreads) {
int random_index = random() % numthreads;
int thread = threadlist[random_index];
thread_id_t curr_tid = int_to_id(thread);
return model->get_thread(curr_tid);
}
// select thread by the id picked by scheduler
Thread * Fuzzer::selectThreadbyid(int threadid) {
thread_id_t curr_tid = int_to_id(threadid);
return model->get_thread(curr_tid);
}
Thread * Fuzzer::selectNotify(simple_action_list_t * waiters) {
int numwaiters = waiters->size();
int random_index = random() % numwaiters;
sllnode<ModelAction*> * it = waiters->begin();
while(random_index--)
it=it->getNext();
Thread *thread = model->get_thread(it->getVal());
waiters->erase(it);
return thread;
}
bool Fuzzer::shouldSleep(const ModelAction *sleep) {
return true;
}
bool Fuzzer::shouldWake(const ModelAction *sleep) {
struct timespec currtime;
clock_gettime(CLOCK_MONOTONIC, &currtime);
uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
return ((sleep->get_time()+sleep->get_value()) < lcurrtime);
}
/* Decide whether wait should spuriously fail or not */
bool Fuzzer::waitShouldFail(ModelAction * wait)
{
if ((random() & 1) == 0) {
struct timespec currtime;
clock_gettime(CLOCK_MONOTONIC, &currtime);
uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
// The time after which wait fail spuriously, in nanoseconds
uint64_t time = random() % 1000000;
wait->set_time(time + lcurrtime);
return true;
}
return false;
}
bool Fuzzer::waitShouldWakeUp(const ModelAction * wait)
{
struct timespec currtime;
clock_gettime(CLOCK_MONOTONIC, &currtime);
uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
return (wait->get_time() < lcurrtime);
}
bool Fuzzer::randomizeWaitTime(ModelAction * timed_wait)
{
uint64_t abstime = timed_wait->get_time();
struct timespec currtime;
clock_gettime(CLOCK_MONOTONIC, &currtime);
uint64_t lcurrtime = currtime.tv_sec * 1000000000 + currtime.tv_nsec;
if (abstime <= lcurrtime)
return false;
// Shorten wait time
if ((random() & 1) == 0) {
uint64_t tmp = abstime - lcurrtime;
uint64_t time_to_expire = random() % tmp + lcurrtime;
timed_wait->set_time(time_to_expire);
}
return true;
}