Fork handler mitigations
[c11tester.git] / schedule.cc
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "threads-model.h"
5 #include "schedule.h"
6 #include "common.h"
7 #include "model.h"
8 #include "nodestack.h"
9 #include "execution.h"
10 #include "fuzzer.h"
11
12 /**
13  * Format an "enabled_type_t" for printing
14  * @param e The type to format
15  * @param str The output character array
16  */
17 void enabled_type_to_string(enabled_type_t e, char *str)
18 {
19         const char *res;
20         switch (e) {
21         case THREAD_DISABLED:
22                 res = "disabled";
23                 break;
24         case THREAD_ENABLED:
25                 res = "enabled";
26                 break;
27         case THREAD_SLEEP_SET:
28                 res = "sleep";
29                 break;
30         default:
31                 ASSERT(0);
32                 res = NULL;
33                 break;
34         }
35         strcpy(str, res);
36 }
37
38 /** Constructor */
39 Scheduler::Scheduler() :
40         execution(NULL),
41         enabled(NULL),
42         enabled_len(0),
43         curr_thread_index(0),
44         current(NULL)
45 {
46 }
47
48 /**
49  * @brief Register the ModelExecution engine
50  * @param execution The ModelExecution which is controlling execution
51  */
52 void Scheduler::register_engine(ModelExecution *execution)
53 {
54         this->execution = execution;
55 }
56
57 void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
58         int threadid = id_to_int(t->get_id());
59         if (threadid >= enabled_len) {
60                 enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
61                 memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
62                 if (enabled != NULL) {
63                         memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
64                         snapshot_free(enabled);
65                 }
66                 enabled = new_enabled;
67                 enabled_len = threadid + 1;
68         }
69         enabled[threadid] = enabled_status;
70 }
71
72 /**
73  * @brief Check if a Thread is currently enabled
74  *
75  * Check if a Thread is currently enabled. "Enabled" includes both
76  * THREAD_ENABLED and THREAD_SLEEP_SET.
77  * @param t The Thread to check
78  * @return True if the Thread is currently enabled
79  */
80 bool Scheduler::is_enabled(const Thread *t) const
81 {
82         return is_enabled(t->get_id());
83 }
84
85 /**
86  * @brief Check if a Thread is currently enabled
87  *
88  * Check if a Thread is currently enabled. "Enabled" includes both
89  * THREAD_ENABLED and THREAD_SLEEP_SET.
90  * @param tid The ID of the Thread to check
91  * @return True if the Thread is currently enabled
92  */
93 bool Scheduler::is_enabled(thread_id_t tid) const
94 {
95         int i = id_to_int(tid);
96         return (i >= enabled_len) ? false : (enabled[i] != THREAD_DISABLED);
97 }
98
99 /**
100  * @brief Check if a Thread is currently in the sleep set
101  * @param t The Thread to check
102  * @return True if the Thread is currently enabled
103  */
104 bool Scheduler::is_sleep_set(const Thread *t) const
105 {
106         return get_enabled(t) == THREAD_SLEEP_SET;
107 }
108
109 /**
110  * @brief Check if execution is stuck with no enabled threads and some sleeping
111  * thread
112  * @return True if no threads are enabled an some thread is in the sleep set;
113  * false otherwise
114  */
115 bool Scheduler::all_threads_sleeping() const
116 {
117         bool sleeping = false;
118         for (int i = 0;i < enabled_len;i++)
119                 if (enabled[i] == THREAD_ENABLED)
120                         return false;
121                 else if (enabled[i] == THREAD_SLEEP_SET)
122                         sleeping = true;
123         return sleeping;
124 }
125
126 enabled_type_t Scheduler::get_enabled(const Thread *t) const
127 {
128         int id = id_to_int(t->get_id());
129         ASSERT(id < enabled_len);
130         return enabled[id];
131 }
132
133 /**
134  * Add a Thread to the sleep set.
135  * @param t The Thread to add
136  */
137 void Scheduler::add_sleep(Thread *t)
138 {
139         DEBUG("thread %d\n", id_to_int(t->get_id()));
140         set_enabled(t, THREAD_SLEEP_SET);
141 }
142
143 /**
144  * Remove a Thread from the sleep set.
145  * @param t The Thread to remove
146  */
147 void Scheduler::remove_sleep(Thread *t)
148 {
149         DEBUG("thread %d\n", id_to_int(t->get_id()));
150         set_enabled(t, THREAD_ENABLED);
151 }
152
153 /**
154  * Add a Thread to the scheduler's ready list.
155  * @param t The Thread to add
156  */
157 void Scheduler::add_thread(Thread *t)
158 {
159         DEBUG("thread %d\n", id_to_int(t->get_id()));
160         ASSERT(!t->is_model_thread());
161         set_enabled(t, THREAD_ENABLED);
162 }
163
164 /**
165  * Remove a given Thread from the scheduler.
166  * @param t The Thread to remove
167  */
168 void Scheduler::remove_thread(Thread *t)
169 {
170         if (current == t)
171                 current = NULL;
172         set_enabled(t, THREAD_DISABLED);
173 }
174
175 /**
176  * Prevent a Thread from being scheduled. The sleeping Thread should be
177  * re-awoken via Scheduler::wake.
178  * @param thread The Thread that should sleep
179  */
180 void Scheduler::sleep(Thread *t)
181 {
182         set_enabled(t, THREAD_DISABLED);
183         t->set_state(THREAD_BLOCKED);
184 }
185
186 /**
187  * Wake a Thread up that was previously waiting (see Scheduler::wait)
188  * @param t The Thread to wake up
189  */
190 void Scheduler::wake(Thread *t)
191 {
192         ASSERT(!t->is_model_thread());
193         set_enabled(t, THREAD_ENABLED);
194         t->set_state(THREAD_READY);
195 }
196
197 /**
198  * @brief Select a Thread to run via round-robin
199  *
200  * @param n The current Node, holding priority information for the next thread
201  * selection
202  *
203  * @return The next Thread to run
204  */
205 Thread * Scheduler::select_next_thread(Node *n)
206 {
207         int avail_threads = 0;
208         int thread_list[enabled_len];
209         for (int i = 0;i< enabled_len;i++) {
210                 if (enabled[i] == THREAD_ENABLED)
211                         thread_list[avail_threads++] = i;
212         }
213
214         if (avail_threads == 0)
215                 return NULL;// No threads availablex
216
217         Thread * thread = execution->getFuzzer()->selectThread(n, thread_list, avail_threads);
218         curr_thread_index = id_to_int(thread->get_id());
219         return thread;
220 }
221
222 void Scheduler::set_scheduler_thread(thread_id_t tid) {
223         curr_thread_index=id_to_int(tid);
224 }
225
226 /**
227  * @brief Set the current "running" Thread
228  * @param t Thread to run
229  */
230 void Scheduler::set_current_thread(Thread *t)
231 {
232         ASSERT(!t || !t->is_model_thread());
233
234         current = t;
235         if (DBG_ENABLED())
236                 print();
237 }
238
239 /**
240  * @return The currently-running Thread
241  */
242 Thread * Scheduler::get_current_thread() const
243 {
244         ASSERT(!current || !current->is_model_thread());
245         return current;
246 }
247
248 /**
249  * Print debugging information about the current state of the scheduler. Only
250  * prints something if debugging is enabled.
251  */
252 void Scheduler::print() const
253 {
254         int curr_id = current ? id_to_int(current->get_id()) : -1;
255
256         model_print("Scheduler: ");
257         for (int i = 0;i < enabled_len;i++) {
258                 char str[20];
259                 enabled_type_to_string(enabled[i], str);
260                 model_print("[%i: %s%s]", i, i == curr_id ? "current, " : "", str);
261         }
262         model_print("\n");
263 }