-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.cpp
262 lines (210 loc) · 7.44 KB
/
sync.cpp
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
/* sync.cpp
*
* Copyright (C) 2017 Alexandre Luiz Brisighello Filho
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <iostream>
#include "sync.h"
#include "lock_hash.h"
#include "thread.h"
#include "log.h"
// Used to only allow one thread to sync
PIN_MUTEX sync_mutex;
// Thread creation structures.
pthread_t pthread_tid;
THREADID pin_tid;
int create_done;
THREADID creator_pin_tid;
REENTRANT_LOCK create_lock;
// Basically, after a few seconds, something
// should happen, or it's locked due unsupported functions.
VOID static watcher(VOID *arg)
{
while(1) {
PIN_Sleep(WATCHER_SLEEP * 1000);
PIN_MutexLock(&sync_mutex);
if(thread_has_advanced() <= 0) {
cerr << "[Pinocchio] Execution is stopped, likely due unsupported locking function" << std::endl;
fail();
}
PIN_MutexUnlock(&sync_mutex);
}
}
void sync_init(int pram)
{
PIN_MutexInit(&sync_mutex);
// Initialize thread creation structures. Avoids some nasty locks by
// allowing only one creation per time.
pthread_tid = 0;
pin_tid = 0;
create_done = 0;
create_lock.busy = 0;
create_lock.locked = NULL;
// Lastly, init thread, trace bank and exec tracker structures and start watcher.
if(pram > 0) {
THREADID watcher_tid = PIN_SpawnInternalThread(watcher, 0, 0, NULL);
log_init(watcher_tid);
} else {
// If using time based, there is no watcher, just give log_init an invalid value.
log_init(MAX_THREADS + 1);
}
thread_init(pram);
}
void sync(ACTION *action)
{
// Only one thread should be working at each time.
PIN_MutexLock(&sync_mutex);
switch(action->action_type) {
case ACTION_DONE:
// Thread has finished one step, just mark as waiting.
// Note: time-based won't even reach here.
thread_sleep(&all_threads[action->tid]);
break;
// Thread creation works with the following rules:
// - One thread creating at a time
// - Create end should wait for starting thread to
// actually start (pin_create -> ACTION_REGISTER)
// - thread_tid should come from create_end, as value
// could change on function. But pointer should be
// found on start, as it also might be changed.
case ACTION_REGISTER:
// Thread 0 ins't created by pthread_create, but always existed.
// Don't wait or do any black magic on that regard.
if(action->tid > 0) {
thread_start(&all_threads[action->tid], &all_threads[creator_pin_tid]);
pin_tid = action->tid;
// If done > 0, ACTION_AFTER_CREATE already done, must release it,
// update my own create_value and go on. If not, save pin_tid
// for later use and mark myself as locked.
if(create_done > 0) {
all_threads[pin_tid].create_value = pthread_tid;
create_done = 0;
handle_reentrant_exit(&create_lock, action->tid);
} else {
create_done = 1;
}
} else {
// Thread start for action->tid = 0
thread_start(&all_threads[action->tid], NULL);
}
break;
case ACTION_BEFORE_CREATE:
handle_reentrant_start(&create_lock, action->tid);
// Save current instruction count from thread creator.
creator_pin_tid = action->tid;
break;
case ACTION_AFTER_CREATE:
// Save pthread tid and check if ACTION_REGISTER already arrived,
// if yes, mark pthread_tid and release create_lock
pthread_tid = ((pthread_t)action->arg.p_1);
if(create_done > 0) {
all_threads[pin_tid].create_value = pthread_tid;
create_done = 0;
handle_reentrant_exit(&create_lock, action->tid);
} else {
create_done = 1;
}
break;
case ACTION_FINI:
// Mark as finished
thread_finish(&all_threads[action->tid]);
// Free any join locked thread, thread 0 shouldn't be joined
if(action->tid > 0) {
THREAD_INFO *t = handle_thread_exit(all_threads[action->tid].create_value);
for(; t != NULL; t = t->next_lock) {
thread_unlock(t, &all_threads[action->tid]);
}
}
// Check if all threads have finished
if(thread_all_finished() == 1) {
DEBUG(cerr << "[Sync] Program finished." << std::endl);
return;
}
break;
case ACTION_BEFORE_JOIN:
handle_before_join((pthread_t)action->arg.p_1, action->tid);
break;
// Mutex events should be treated by lock_hash, unlock thread once done.
case ACTION_LOCK_DESTROY:
handle_lock_init(action->arg.p_1);
break;
case ACTION_LOCK_INIT:
handle_lock_init(action->arg.p_1);
break;
case ACTION_LOCK:
handle_lock(action->arg.p_1, action->tid);
break;
case ACTION_TRY_LOCK:
// Pass the value back to try_lock function.
action->arg.i = handle_try_lock(action->arg.p_1);
break;
case ACTION_UNLOCK:
handle_unlock(action->arg.p_1, action->tid);
break;
case ACTION_SEM_DESTROY:
handle_semaphore_destroy(action->arg.p_1);
break;
case ACTION_SEM_GETVALUE:
action->arg.i = handle_semaphore_getvalue(action->arg.p_1);
break;
case ACTION_SEM_INIT:
handle_semaphore_init(action->arg.p_1, action->arg.i);
break;
case ACTION_SEM_POST:
handle_semaphore_post(action->arg.p_1, action->tid);
break;
case ACTION_SEM_TRYWAIT:
// Pass the value back to sem_trywait function.
action->arg.i = handle_semaphore_trywait(action->arg.p_1);
break;
case ACTION_SEM_WAIT:
handle_semaphore_wait(action->arg.p_1, action->tid);
break;
case ACTION_RWLOCK_INIT:
handle_rwlock_init(action->arg.p_1);
break;
case ACTION_RWLOCK_DESTROY:
handle_rwlock_destroy(action->arg.p_1);
break;
case ACTION_RWLOCK_RDLOCK:
handle_rwlock_rdlock(action->arg.p_1, action->tid);
break;
case ACTION_RWLOCK_TRYRDLOCK:
action->arg.i = handle_rwlock_tryrdlock(action->arg.p_1, action->tid);
break;
case ACTION_RWLOCK_WRLOCK:
handle_rwlock_wrlock(action->arg.p_1, action->tid);
break;
case ACTION_RWLOCK_TRYWRLOCK:
action->arg.i = handle_rwlock_trywrlock(action->arg.p_1, action->tid);
break;
case ACTION_RWLOCK_UNLOCK:
handle_rwlock_unlock(action->arg.p_1, action->tid);
break;
case ACTION_COND_BROADCAST:
handle_cond_broadcast(action->arg.p_1, action->tid);
break;
case ACTION_COND_DESTROY:
handle_cond_destroy(action->arg.p_1);
break;
case ACTION_COND_INIT:
handle_cond_init(action->arg.p_1);
break;
case ACTION_COND_SIGNAL:
handle_cond_signal(action->arg.p_1, action->tid);
break;
case ACTION_COND_WAIT:
handle_cond_wait(action->arg.p_1, action->arg.p_2, action->tid);
break;
}
// Once the big switch has finished, all threads are updated.
// Try to release whoever possible.
thread_try_release_all();
// Release sync_mutex so other thread can sync.
PIN_MutexUnlock(&sync_mutex);
// Should sleep here if not synced.
PIN_SemaphoreWait(&all_threads[action->tid].active);
}