-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrappers.c
269 lines (224 loc) · 8.56 KB
/
wrappers.c
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
/*
Copyright (c) 2015 Forkscan authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define _GNU_SOURCE
#include "alloc.h"
#include <assert.h>
#include <dlfcn.h>
#include "env.h"
#include "forkscan.h"
#include "proc.h"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include "thread.h"
#include "util.h"
/****************************************************************************/
/* Types of functions that get wrapped. */
/****************************************************************************/
typedef int (*pthread_create_t) (pthread_t *,
const pthread_attr_t *,
void *(*)(void *),
void *);
typedef void (*pthread_exit_t) (void *);
typedef int (*pthread_join_t) (pthread_t, void **);
typedef int (*__libc_start_main_t)(int (*) (int, char **, char **),
int, char **,
void (*) (void),
void (*) (void),
void (*) (void),
void (*));
typedef int (*main_t) (int, char **, char **);
/****************************************************************************/
/* Globals. */
/****************************************************************************/
static int g_thread_count = 1;
/****************************************************************************/
/* Wrapped functions. */
/****************************************************************************/
pthread_create_t orig_pthread_create; // Exported for use by the child.
static pthread_exit_t orig_pthread_exit;
pthread_join_t orig_pthread_join; // Exported for use by the child.
static __libc_start_main_t orig_libc_start_main;
static main_t orig_main;
/****************************************************************************/
/* Wrapping function implementations. */
/****************************************************************************/
__attribute__((visibility("default")))
int pthread_create (pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg)
{
thread_data_t *td;
int ret;
pthread_attr_t real_attr;
void *stack;
size_t stacksize;
assert(orig_pthread_create);
if (MAX_THREAD_COUNT < __sync_fetch_and_add(&g_thread_count, 1)) {
// Don't overflow buffers.
forkscan_fatal("Exceeded maximum thread count (%d).\n",
MAX_THREAD_COUNT);
}
// Wrap the user data.
td = forkscan_util_thread_data_new();
if (NULL == td) {
forkscan_fatal("Out of memory.\n");
}
td->user_routine = start_routine;
td->user_arg = arg;
td->is_active = 0;
// If the user hasn't specified a stack, we'll use one of our own.
// Otherwise, we get the bounds of the user's stack and use it as
// our own.
if (NULL == attr) {
ret = pthread_attr_init(&real_attr);
if (0 != ret) {
forkscan_fatal("could not create thread.\n");
}
} else {
real_attr = *attr;
}
ret = pthread_attr_getstack(&real_attr, &stack, &stacksize);
if (0 != ret) {
forkscan_fatal("unable to get stack attributes.\n");
}
if (NULL == stack) {
stack = forkscan_buffer_makestack(&stacksize);
ret = pthread_attr_setstack(&real_attr, stack, stacksize);
if (0 != ret) {
forkscan_fatal("unable to set stack attributes.\n");
}
td->stack_is_ours = 1;
} else {
td->stack_is_ours = 0;
}
td->user_stack_low = (char*)stack;
td->user_stack_high = (char*)stack + stacksize;
td->wait_time_ms = 0;
// Insert the metadata into the global structure.
forkscan_proc_add_thread_data(td);
// Try to create the thread.
ret = orig_pthread_create(thread, &real_attr,
forkscan_thread_base, (void*)td);
if (0 != ret) {
// Ruh, roh! Failed to create a thread. That isn't really our
// problem, though. Just clean up the memory we allocated for
// the thread. The end.
if (td->stack_is_ours) {
forkscan_buffer_freestack(stack);
}
forkscan_util_thread_data_free(td);
}
return ret;
}
__attribute__((noreturn))
static void exit_wrapper (void *retval)
{
assert(orig_pthread_exit);
forkscan_thread_cleanup();
__sync_fetch_and_sub(&g_thread_count, 1);
orig_pthread_exit(retval);
abort(); // Should never get past orig_pthread_exit();
}
__attribute__((visibility("default"), noreturn))
void pthread_exit (void *retval)
{
exit_wrapper(retval);
}
void forkscan_pthread_exit (void *retval)
{
exit_wrapper(retval);
}
__attribute__((visibility("default")))
int pthread_join (pthread_t thread, void **retval)
{
assert(orig_pthread_join);
int ret = orig_pthread_join(thread, retval);
forkscan_util_thread_data_cleanup(thread);
return ret;
}
typedef struct main_args_t main_args_t;
struct main_args_t {
int argc;
char **argv;
char **env;
};
static void *main_thunk (void *arg)
{
int ret;
main_args_t *main_args = (main_args_t*)arg;
ret = orig_main(main_args->argc, main_args->argv, main_args->env);
if (g_forkscan_report_statistics) {
forkscan_print_statistics();
}
exit(ret);
}
static int main_replacement (int argc, char **argv, char **env)
{
thread_data_t *td = forkscan_util_thread_data_new();
mem_range_t stack_data;
main_args_t main_args = { argc, argv, env };
if (NULL == td) {
forkscan_fatal("Out of memory.\n");
}
td->user_routine = main_thunk;
td->user_arg = &main_args;
forkscan_proc_stack_from_addr(&stack_data, (size_t)&stack_data);
td->user_stack_low = (char*)stack_data.low;
// Insert the metadata into the global structure.
forkscan_proc_add_thread_data(td);
forkscan_thread_base(td);
assert(0); // Should not return. It should die in main_thunk().
return 0;
}
__attribute__((visibility("default")))
int __libc_start_main(int (*main) (int, char **, char **),
int argc, char **ubp_av,
void (*init) (void),
void (*fini) (void),
void (*rtld_fini) (void),
void (*stack_end))
{
pthread_t tid;
int ret = orig_pthread_create(&tid, NULL, forkscan_thread, NULL);
if (0 != ret) {
forkscan_fatal("Unable to start garbage collector.\n");
// Does not return.
}
orig_main = main;
return orig_libc_start_main(main_replacement, argc, ubp_av,
init, fini, rtld_fini, stack_end);
}
/****************************************************************************/
/* Replacement routine. */
/****************************************************************************/
/**
* Find the functions that are being wrapped and keep pointers to them so
* they can be called by their respective wrappers. This function gets
* called automatically as soon as the module is loaded.
*/
__attribute__((constructor (101)))
static void do_wrapper_replacement ()
{
orig_pthread_create = dlsym(RTLD_NEXT, "pthread_create");
orig_pthread_exit = dlsym(RTLD_NEXT, "pthread_exit");
orig_pthread_join = dlsym(RTLD_NEXT, "pthread_join");
orig_libc_start_main = dlsym(RTLD_NEXT, "__libc_start_main");
}