-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibundead.cpp
More file actions
331 lines (300 loc) · 11.1 KB
/
libundead.cpp
File metadata and controls
331 lines (300 loc) · 11.1 KB
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
/* Copyright (C)
* 2017 - Jinpeng Zhou, Jinpeng.Zhou@utsa.edu
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
/**
* @file libundead.cpp
* @brief Main file
* @author Tongping Liu, <http://www.cs.utsa.edu/~tongpingliu/>
* @author Jinpeng Zhou, Jinpeng.Zhou@utsa.edu
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "xthread.hh"
#ifdef ENABLE_PREVENTION
#include "prevention.hh"
#endif
void initializer (void) __attribute__((constructor));
void finalizer (void) __attribute__((destructor));
thread_t *threadsInfo; // threads' data
real_thread_t *threadsInfoReal;
uintptr_t globalStackAddr; // the lowest addr of global stack for all sub-threads
volatile int aliveThreads;
bool isSingleThread;
int mutexUnit;
void* mainTop;
void* textTop;
#ifdef ENABLE_PREVENTION
// real mutex region
my_mutex* realMutexStart;
my_mutex* realMutexEnd;
size_t realMutexIndex;
bool enablePrevention;
#endif
void initializer (void) {
// wrap functions
init_real_functions();
// Threads data allocation
threadsInfo = new thread_t[xdefines::MAX_THREADS];
if(threadsInfo == NULL) {
fprintf(stderr, "Failed to allocate threads data!\n");
abort();
}
threadsInfoReal = new real_thread_t[xdefines::MAX_THREADS];
void* ptr = MM::mmapAllocatePrivate((size_t)xdefines::STACK_SIZE * xdefines::MAX_THREADS);
if(ptr == NULL) {
fprintf(stderr, "Failed to allocate customized stack!\n");
abort();
}
// Global initialization
aliveThreads = 1;
isSingleThread = true;
globalStackAddr = (uintptr_t)ptr;
// ONLY set value here
mutexUnit = 64 * (sizeof(pthread_mutex_t) / 64 + 1);
#ifdef ENABLE_PREVENTION
realMutexStart = (my_mutex*)malloc((size_t)xdefines::MAX_SYNC_OBJ * sizeof(my_mutex));
realMutexEnd = realMutexStart + xdefines::MAX_SYNC_OBJ ;
realMutexIndex = 0;
enablePrevention = false;
prevention::getInstance().initialize();
#endif
xthread::getInstance().initialize();
//fprintf(stderr, "Now we have initialized successfully!\n");
}
void finalizer (void) {
xthread::getInstance().finalize();
delete[] threadsInfo;
#ifdef ENABLE_PREVENTION
free((void*)realMutexStart);
#endif
}
typedef int (*main_fn_t)(int, char**, char**);
extern "C" int __libc_start_main(main_fn_t, int, char**, void (*)(), void (*)(), void (*)(), void*) __attribute__((weak, alias("undead_libc_start_main")));
extern "C" int undead_libc_start_main(main_fn_t main_fn, int argc, char** argv, void (*init)(), void (*fini)(), void (*rtld_fini)(), void* stack_end) {
//Find the real __libc_start_main
auto real_libc_start_main = (decltype(__libc_start_main)*)dlsym(RTLD_NEXT, "__libc_start_main");
mainTop = __builtin_return_address(0);
return real_libc_start_main(main_fn, argc, argv, init, fini, rtld_fini, stack_end);
}
// Intercept the pthread_create function.
int pthread_create(pthread_t * tid, const pthread_attr_t * attr, void *(*start_routine) (void *), void * arg) {
return xthread::getInstance().thread_create(tid, attr, start_routine, arg);
}
int pthread_join(pthread_t tid, void** retval) {
return xthread::getInstance().thread_join(tid, retval);
}
int pthread_mutex_destroy (pthread_mutex_t* mutex) {
#ifdef ENABLE_PREVENTION
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
return WRAP(pthread_mutex_destroy)(real_mutex);
#else
return WRAP(pthread_mutex_destroy)(mutex);
#endif
}
int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) {
int ret;
#ifdef ENABLE_PREVENTION
// get thread index
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
// check corresponding real_mutex
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(real_mutex == mutex) { // if there's no corresponding real _mutex
// assign a new one
size_t index = __atomic_fetch_add(&realMutexIndex, 1, __ATOMIC_RELAXED);
real_mutex = (pthread_mutex_t*)(realMutexStart + index);
setSyncEntry(mutex, real_mutex);
}
uintptr_t redirect = 0; // previous redirection result
#ifndef DISABLE_INIT_CHECK
unsigned long esp;
GET_ESP(esp);
OffsetHashMap * offsetMap = current->initOffsetMap;
uintptr_t offset = (unsigned long)current->stackTop - esp;
void* combined = (void*)((offset) ^ (uintptr_t)mutex);
OffsetInfoList* oil;
if(!offsetMap->find(combined, 8, &oil)) {
// new
oil = new OffsetInfoList;
offsetMap->insert(combined, 8, oil);
} else {
if(oil->hasEntry(offset, mutex, &redirect)) {
// already exit, don't care call stacks
// directly do redirection and return, based on previous result
if(redirect == 0) return WRAP(pthread_mutex_init)(real_mutex, attr);
else *(uintptr_t*)real_mutex = (uintptr_t)redirect;
return 0;
}
}
#endif
// backtrace
void* addr[xdefines::MAX_BACKTRACE_DEPTH] = {NULL};
current->isRecursive = true;
int len = backtrace(addr, xdefines::MAX_BACKTRACE_DEPTH);
current->isRecursive = false;
// initialize the real mutex
if(enablePrevention) {
ret = prevention::getInstance().mutex_init(mutex, real_mutex, attr, current, addr, len, &redirect);
} else {
ret = WRAP(pthread_mutex_init)(real_mutex, attr);
}
#ifndef DISABLE_INIT_CHECK
oil->insertToTail(new OffsetInfo(offset, mutex, redirect));
#endif
#ifdef ENABLE_ANALYZER
// record call stack
if(ret == 0 ) {
//analyzer::getInstance().updateMutexInitCallstack((void*)real_mutex, mutex, current, addr, len);
updateMutexInitCallstack((void*)real_mutex, mutex, current, addr, len);
}
#endif
#else
ret = WRAP(pthread_mutex_init)(mutex, attr);
#endif
return ret;
}
int pthread_mutex_lock(pthread_mutex_t *mutex) {
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
if(current->isRecursive) return WRAP(pthread_mutex_lock)(mutex);
#ifdef ENABLE_PREVENTION
// get corresponding real_mutex
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(enablePrevention) { // or we can skip checking enablePrevention
if(prevention::getInstance().checkInDirection(real_mutex)) {
// this is a special lock with redirection
pthread_mutex_t *realMutex = (pthread_mutex_t*)(*(uintptr_t*)real_mutex);
// record
if(!updateSpecialByLock(current, realMutex, mutex)) return 0;
if(!isSingleThread) updateDependency(current, realMutex);
return WRAP(pthread_mutex_lock)(realMutex);
} else {
// this is not a special lock
if(!isSingleThread) updateDependency(current, mutex);
return WRAP(pthread_mutex_lock)(real_mutex);
}
} else {
if(!isSingleThread) updateDependency(current, mutex);
return WRAP(pthread_mutex_lock)(real_mutex);
}
#else
if(!isSingleThread) updateDependency(current, mutex);
// Acquire the actual mutex.
return WRAP(pthread_mutex_lock)(mutex);
#endif
}
int pthread_mutex_trylock(pthread_mutex_t *mutex) {
int ret;
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
if(current->isRecursive) return WRAP(pthread_mutex_trylock)(mutex);
#ifdef ENABLE_PREVENTION
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(enablePrevention) {
if(prevention::getInstance().checkInDirection(real_mutex)) {
pthread_mutex_t *realMutex = (pthread_mutex_t*)(*(uintptr_t*)real_mutex);
ret = WRAP(pthread_mutex_trylock)(realMutex);
if(ret == 0) {
if(!updateSpecialByLock(current, realMutex, mutex)) return 0;
if(!isSingleThread) updateDependencyByTryLock(current, realMutex);
}
} else {
ret = WRAP(pthread_mutex_trylock)(real_mutex);
if(ret == 0 && !isSingleThread) updateDependencyByTryLock(current, mutex);
}
} else {
ret = WRAP(pthread_mutex_trylock)(real_mutex);
if(ret == 0 && !isSingleThread) updateDependencyByTryLock(current, mutex);
}
#else
ret = WRAP(pthread_mutex_trylock)(mutex);
if(ret == 0 && !isSingleThread) updateDependencyByTryLock(current, mutex);
#endif
return ret;
}
int pthread_mutex_unlock(pthread_mutex_t* mutex) {
int ret;
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
if(current->isRecursive) return WRAP(pthread_mutex_unlock)(mutex);;
#ifdef ENABLE_PREVENTION
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(enablePrevention) {
if(prevention::getInstance().checkInDirection(real_mutex)) {
pthread_mutex_t *realMutex = (pthread_mutex_t*)(*(uintptr_t*)real_mutex);
if(index != 0 && !updateSpecialByUnLock(current, realMutex, mutex)) return 0;
ret = WRAP(pthread_mutex_unlock)(realMutex);
if(!isSingleThread && ret == 0) updateHoldingSetByUnlock(current, realMutex);
} else {
ret = WRAP(pthread_mutex_unlock)(real_mutex);
if(!isSingleThread && ret == 0) updateHoldingSetByUnlock(current, mutex);
}
} else {
ret = WRAP(pthread_mutex_unlock)(real_mutex);
if(!isSingleThread && ret == 0) updateHoldingSetByUnlock(current, mutex);
}
#else
ret = WRAP(pthread_mutex_unlock)(mutex);
if(!isSingleThread && ret == 0) updateHoldingSetByUnlock(current, mutex);
#endif
return ret;
}
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) {
#ifdef ENABLE_PREVENTION
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(enablePrevention) {
if(prevention::getInstance().checkInDirection(real_mutex)) {
pthread_mutex_t* realMutex = (pthread_mutex_t*)(*(uintptr_t*)real_mutex);
updateDependencyWithCond(current, realMutex);
return WRAP(pthread_cond_wait)(cond, realMutex);
} else {
updateDependencyWithCond(current, mutex);
return WRAP(pthread_cond_wait)(cond, real_mutex);
}
} else {
updateDependencyWithCond(current, mutex);
return WRAP(pthread_cond_wait)(cond, real_mutex);
}
#else
return WRAP(pthread_cond_wait)(cond, mutex);
#endif
}
int pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec * abstime) {
#ifdef ENABLE_PREVENTION
int index = getThreadIndexFromStack((uintptr_t)&mutex);
thread_t * current = &threadsInfo[index];
pthread_mutex_t* real_mutex = (pthread_mutex_t*)getSyncEntry(mutex);
if(enablePrevention) {
if(prevention::getInstance().checkInDirection(real_mutex)) {
pthread_mutex_t* realMutex = (pthread_mutex_t*)(*(uintptr_t*)real_mutex);
updateDependencyWithCond(current, realMutex);
return WRAP(pthread_cond_timedwait)(cond, realMutex, abstime);
} else {
updateDependencyWithCond(current, mutex);
return WRAP(pthread_cond_timedwait)(cond, real_mutex, abstime);
}
} else {
updateDependencyWithCond(current, mutex);
return WRAP(pthread_cond_timedwait)(cond, real_mutex, abstime);
}
#else
return WRAP(pthread_cond_timedwait)(cond, mutex, abstime);
#endif
}