-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrr_thread.cpp
302 lines (272 loc) · 7.28 KB
/
rr_thread.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
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
/*
* ____ ____ _____
* | _ \| _ \| ___| _ ________
* | |_) | |_) | |_ | | | |_ /_ /
* | _ <| _ <| _|| |_| |/ / / /
* |_| \_\_| \_\_| \__,_/___/___|
*
* Copyright (C) National University of Singapore
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
* RECORDING:
*
* Threads are allowed to run as normal, but context switches may only occur
* during syscalls. This is to prevent switches during unknown states, which
* would significantly complicate replay.
*
* REPLAY:
*
* Uses fibers (see rr_fiber.cpp)
*/
/*
* Linux clone_args struct.
*/
struct clone_args
{
uint64_t flags;
uint64_t pidfd;
uint64_t child_tid;
uint64_t parent_tid;
uint64_t exit_signal;
uint64_t stack;
uint64_t stack_size;
uint64_t tls;
uint64_t set_tid;
uint64_t set_tid_size;
uint64_t cgroup;
};
/*
* Thread state structure.
*/
struct THREAD
{
pid_t tid; // Real thread-ID
int id; // Virtual thread-ID
pid_t *ctid; // CLONE_CHILD_CLEARTID pointer
int *futex; // Waiting on futex?
intptr_t prev_loc; // Previous location (coverage tracking)
sigset_t sigmask; // Signal mask
STATE state; // Thread state
intptr_t fs; // %fs register (TLS)
uint32_t mxcsr; // %mxcsr register
uint8_t xsave[4096]; // xsave state
};
#define THREAD_TLS 0xb8 // Unused %fs offset
#define MAX_CPU 4096 // (Assumed) max CPU number
static int thread_id = 0; // Next thread-ID
static pid_t INFO_pid = 0; // Process-ID
/*
* Thread run lock.
*/
static mutex_t thread_mutex = MUTEX_INITIALIZER;
/*
* Create a new THREAD struct.
*/
static THREAD *thread_new(const STATE *state)
{
THREAD *thread = (THREAD *)xmalloc(sizeof(THREAD));
thread_id++;
thread->id = thread_id;
thread->tid = gettid();
thread->ctid = NULL;
thread->sigmask = 0x0;
if (state != NULL)
memcpy(&thread->state, state, sizeof(thread->state));
else
memset(&thread->state, 0x0, sizeof(thread->state));
thread->fs = 0x0;
memset(thread->xsave, 0x0, sizeof(thread->xsave));
return thread;
}
/*
* Get the THREAD struct for the current thread.
*/
static THREAD *thread_self(void)
{
struct THREAD *self;
asm volatile ("mov %%fs:" STRING(THREAD_TLS) ",%0" : "=a"(self));
return self;
}
/*
* Set the THREAD struct for the current thread.
*/
static void thread_set_self(THREAD *self)
{
asm volatile ("mov %0,%%fs:" STRING(THREAD_TLS) : : "r"(self));
}
/*
* Call before a syscall.
*/
static void THREAD_UNLOCK(void)
{
mutex_unlock(&thread_mutex);
}
/*
* Call after a syscall.
*/
static void THREAD_LOCK(void)
{
if (mutex_lock(&thread_mutex) < 0)
error("failed to lock THREAD mutex: %s", strerror(errno));
}
/*
* Test if the thread lock is held.
*/
static bool THREAD_IS_LOCKED(void)
{
if (mutex_trylock(&thread_mutex) < 0)
return (errno == EBUSY);
mutex_unlock(&thread_mutex);
return false;
}
/*
* Thread start handling.
*/
static void record_start(int id);
extern "C"
{
void thread_start_2(THREAD *self)
{
THREAD_LOCK();
thread_set_self(self);
self->tid = gettid();
self->state.rip += /*sizeof(syscall)=*/2;
asm volatile ("vzeroall");
uint32_t mxcsr = 0x1f80;
asm volatile ("ldmxcsr %0" : : "m"(mxcsr));
if (syscall(SYS_arch_prctl, /*ARCH_SET_CPUID=*/0x1012, 0x0) < 0)
warning("failed to enable cpuid interception "
"(replay may diverge): %s", strerror(errno));
record_start(self->id);
jump(&self->state);
}
void thread_start(void);
asm (
".globl thread_start\n"
".type thread_start,@function\n"
"thread_start:\n"
"pop %rdi\n"
"jmp thread_start_2\n"
);
}
/*
* Thread fork() handler.
*/
static long thread_fork(void)
{
switch (option_fork)
{
case FORK_PARENT:
return INFO_pid+1; // Follow parent
case FORK_CHILD:
mutex_settid(INT_MIN);
return 0; // Follow child
case FORK_FAIL: default:
return -ENOSYS;
}
}
/*
* Thread clone() handler.
*/
static long thread_clone(STATE *state)
{
int flags = (int)state->rdi;
if (flags & /*CLONE_VFORK=*/0x4000)
return -ENOSYS;
uintptr_t *stack = (uintptr_t *)state->rsi;
if (stack == NULL)
return thread_fork();
THREAD *thread = thread_new(state);
thread->state.rsp = (uintptr_t)stack;
thread->state.rax = 0;
stack--;
stack[0] = (uintptr_t)thread;
stack--;
stack[0] = (uintptr_t)thread_start;
if (flags & /*CLONE_CHILD_CLEARTID=*/0x200000)
thread->ctid = (pid_t *)state->r10;
long r = syscall(SYS_clone, state->rdi, (uintptr_t)stack, state->rdx,
state->r10, state->r8, state->r9);
if (r < 0)
{
r = -errno;
xfree(thread);
}
return r;
}
/*
* Thread clone3() handler.
*/
static long thread_clone3(STATE *state)
{
const struct clone_args *args_0 = (struct clone_args *)state->rdi;
struct clone_args args_1;
struct clone_args *args = &args_1;
memcpy(args, args_0, sizeof(*args));
int flags = (int)args->flags;
if (flags & /*CLONE_VFORK=*/0x4000)
return -ENOSYS;
uintptr_t *stack = (uintptr_t *)args->stack;
if (stack == 0x0)
return thread_fork();
size_t stack_size = (size_t)args->stack_size;
if (stack_size <= 2 * sizeof(uintptr_t))
return -ENOSYS;
stack = (uintptr_t *)((uint8_t *)stack + stack_size);
THREAD *thread = thread_new(state);
thread->state.rsp = (uintptr_t)stack;
thread->state.rax = 0;
stack--;
stack[0] = (uintptr_t)thread;
stack--;
stack[0] = (uintptr_t)thread_start;
stack_size -= 2 * sizeof(uintptr_t);
args->stack_size = (uintptr_t)stack_size;
if (flags & /*CLONE_CHILD_CLEARTID=*/0x200000)
thread->ctid = (pid_t *)args->child_tid;
long r = syscall(/*SYS_clone3=*/435, args, sizeof(*args),
state->rdx, state->r10, state->r8, state->r9);
if (r < 0)
{
r = -errno;
xfree(thread);
}
return r;
}
/*
* Thread exit() handler.
*/
static NORETURN void thread_exit(STATE *state)
{
THREAD *self = thread_self();
if (self->ctid != NULL)
*self->ctid = 0x0;
THREAD_UNLOCK();
xfree(self);
syscall(SYS_exit, (int)state->rdi);
while (true)
asm volatile ("ud2");
}
/*
* Thread module initialization.
*/
static void thread_init(void)
{
INFO_pid = getpid();
THREAD *self = thread_new(NULL);
thread_set_self(self);
THREAD_LOCK();
}