-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigfuz.c
315 lines (268 loc) · 7.54 KB
/
sigfuz.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
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
/*
* Copyright 2018, Breno Leitao, IBM Corp.
* Licensed under GPLv2.
*
* This is a Powerpc signal fuzzer.
*/
#include <stdio.h>
#include <sys/types.h>
#include <limits.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ucontext.h>
#include <sys/mman.h>
#include <pthread.h>
#define MSR_TS_S_LG 33 /* Trans Mem state: Suspended */
#define MSR_TS_T_LG 34 /* Trans Mem state: Active */
#define __MASK(X) (1UL<<(X))
#define MSR_TM __MASK(MSR_TM_LG) /* Transactional Mem Available */
#define MSR_TS_S __MASK(MSR_TS_S_LG) /* Transaction Suspended */
#define MSR_TS_T __MASK(MSR_TS_T_LG) /* Transaction Suspended */
#define COUNT_MAX 1000 /* Number of interactions */
#define THREADS 1
#define ARG_MESS_WITH_TM_AT 0x1
#define ARG_MESS_WITH_TM_BEFORE 0x2
#define ARG_MESS_WITH_MSR_AT 0x4
#define ARG_FOREVER 0x10
#define ARG_BOOM ARG_MESS_WITH_TM_AT | ARG_MESS_WITH_TM_BEFORE | ARG_MESS_WITH_MSR_AT
static int args;
static int nthread = THREADS;
static int count_max = COUNT_MAX;
/* checkpoint context */
ucontext_t *ckuc;
/* Returns a 64-bits random number */
long long r(){
long long f = rand();
f = f<<32;
f |= rand();
return f;
}
/*
* Set a pointer (ptr) value (nbytes wide) with random number, with
* 1/chance probability
*/
int set_random(void *ptr, int chance, int nbytes)
{
long long v;
if ((rand() % chance) == 0) {
switch (nbytes) {
case 1:
v = rand() % UCHAR_MAX + 1;
*(char *)ptr = v;
break;
case 2:
v = rand() % USHRT_MAX + 1;
*(short *)ptr = v;
break;
case 4:
v = rand() % UINT_MAX + 1;
*(int *)ptr = v;
break;
case 8:
v = r();
*(long long *)ptr = v;
break;
default:
printf("Wrong value\n");
exit(-1);
}
return 1;
}
return 0;
}
/* Return true with 1/x probability */
int one_in_chance(int x)
{
return rand()%x == 0;
}
/* Change TM states */
void mess_with_tm()
{
/* Starts a transaction 33% of the time */
if (one_in_chance(3)) {
asm ("tbegin. ;"
"beq 8 ;");
/* And suspended half of them */
if (one_in_chance(2))
asm("tsuspend. ;");
}
/* Terminate 5% of them */
if (one_in_chance(20))
asm("tend. ;");
}
/* Signal handler that will be invoked with raise() */
void trap_signal_handler(int signo, siginfo_t *si, void *uc)
{
ucontext_t *ucp = uc;
ucp->uc_link = ckuc;
/*
* Set uc_link in three possible ways:
* - Setting a single 'int' in the whole chunk
* - Cloning ucp into uc_link
* - Allocating a new memory chunk
*/
if (one_in_chance(3))
memset(ucp->uc_link, rand(), sizeof(ucontext_t));
else if (one_in_chance(2))
memcpy(ucp->uc_link, uc, sizeof(ucontext_t));
else {
free(ckuc);
ckuc = malloc(sizeof(ucontext_t));
ucp->uc_link = ckuc;
madvise(ucp->uc_link, sizeof(ucontext_t), MADV_DONTNEED);
}
if (args & ARG_MESS_WITH_MSR_AT) {
/* Changing the checkpointed registers */
if (one_in_chance(4))
ucp->uc_link->uc_mcontext.gp_regs[PT_MSR] |= MSR_TS_S;
else
if (one_in_chance(2))
ucp->uc_link->uc_mcontext.gp_regs[PT_MSR] |=
MSR_TS_T;
else
ucp->uc_link->uc_mcontext.gp_regs[PT_MSR] |=
MSR_TS_T | MSR_TS_S;
/* Checking the current register context */
if (one_in_chance(2))
ucp->uc_mcontext.gp_regs[PT_MSR] |= MSR_TS_S;
else
if (one_in_chance(2))
ucp->uc_mcontext.gp_regs[PT_MSR] |= MSR_TS_T;
else
ucp->uc_mcontext.gp_regs[PT_MSR] |=
MSR_TS_T | MSR_TS_S;
}
/* 1/100 of the runs mess up with MSR */
set_random(&ucp->uc_mcontext.gp_regs[PT_MSR], 10, 8);
set_random(&ucp->uc_link->uc_mcontext.gp_regs[PT_MSR], 10, 8);
set_random(&ucp->uc_mcontext.gp_regs[PT_NIP], 10, 8);
set_random(&ucp->uc_link->uc_mcontext.gp_regs[PT_NIP], 10, 8);
ucp->uc_mcontext.gp_regs[PT_TRAP] = r();
ucp->uc_mcontext.gp_regs[PT_DSISR] = r();
ucp->uc_mcontext.gp_regs[PT_DAR] = r();
ucp->uc_mcontext.gp_regs[PT_ORIG_R3] = r();
ucp->uc_mcontext.gp_regs[PT_XER] = r();
ucp->uc_mcontext.gp_regs[PT_RESULT] = r();
ucp->uc_mcontext.gp_regs[PT_SOFTE] = r();
ucp->uc_mcontext.gp_regs[PT_DSCR] = r();
ucp->uc_mcontext.gp_regs[PT_CTR] = r();
ucp->uc_mcontext.gp_regs[PT_LNK] = r();
ucp->uc_mcontext.gp_regs[PT_CCR] = r();
ucp->uc_mcontext.gp_regs[PT_REGS_COUNT] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_TRAP] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_DSISR] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_DAR] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_ORIG_R3] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_XER] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_RESULT] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_SOFTE] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_DSCR] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_CTR] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_LNK] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_CCR] = r();
ucp->uc_link->uc_mcontext.gp_regs[PT_REGS_COUNT] = r();
if (args & ARG_MESS_WITH_TM_BEFORE) {
mess_with_tm();
}
}
void seg_signal_handler(int signo, siginfo_t *si, void *uc)
{
exit(0);
}
void *tm_trap_test(void *thrid)
{
struct sigaction trap_sa, seg_sa;
int i = 0;
ckuc = malloc(sizeof(ucontext_t));
trap_sa.sa_flags = SA_SIGINFO;
trap_sa.sa_sigaction = trap_signal_handler;
seg_sa.sa_flags = SA_SIGINFO;
seg_sa.sa_sigaction = seg_signal_handler;
/* The signal handler will enable MSR_TS */
sigaction(SIGUSR1, &trap_sa, NULL);
/* If it does not crash, it will segfault, avoid it to retest */
sigaction(SIGSEGV, &seg_sa, NULL);
while (i < count_max) {
pid_t t = fork();
if (t == 0) {
/* Once seed per process */
srand(time(NULL) + getpid());
if (args & ARG_MESS_WITH_TM_AT) {
mess_with_tm();
}
raise(SIGUSR1);
exit(0);
} else {
int ret;
wait(&ret);
}
if (!(args & ARG_FOREVER))
i++;
}
return NULL;
}
int tm_signal_force_msr(void)
{
pthread_t *threads;
threads = malloc(nthread*sizeof(pthread_t));
int rc, t;
for(t=0; t<nthread; t++){
rc = pthread_create(&threads[t], NULL, tm_trap_test, (void *)&t);
}
for (t=0; t<nthread; t++) {
rc = pthread_join(threads[t], NULL);
}
free(threads);
return 0;
}
void show_help(char *name)
{
printf("%s: Sigfuzzer for powerpc\n", name);
printf("Usage:\n");
printf("\t-b\tMess with TM before raising a SIGUSR1 signal\n");
printf("\t-a\tMess with TM after raising a SIGUSR1 signal\n");
printf("\t-m\tMess with MSR[TS] bits at signal handler machine context\n");
printf("\t-x\tMess with everything above\n");
printf("\t-f\tRun forever and does not exit\n");
printf("\t-i\tAmount of interactions. (Default = %d)\n", COUNT_MAX);
printf("\t-t\tAmount of threads. (Default = %d)\n", THREADS);
exit(-1);
}
int main(int argc, char **argv)
{
int opt;
while ((opt = getopt(argc, argv, "bamxt:fi:h")) != -1) {
if (opt == 'b') {
printf("Mess with TM before signal\n");
args |= ARG_MESS_WITH_TM_BEFORE;
} else if (opt == 'a') {
printf("Mess with TM at signal handler\n");
args |= ARG_MESS_WITH_TM_AT;
} else if (opt == 'm') {
printf("Mess with MSR[TS] bits at signal handler machine context\n");
args |= ARG_MESS_WITH_MSR_AT;
} else if (opt == 'x') {
printf("Running complete fuzzer\n");
args |= ARG_BOOM;
} else if (opt == 't') {
nthread = atoi(optarg);
printf("Threads := %d\n", nthread);
} else if (opt == 'f') {
args |= ARG_FOREVER;
printf("Press ^C to stop\n");
} else if (opt == 'i') {
count_max = atoi(optarg);
printf("Running for %d interactions\n", count_max);
} else if (opt == 'h') {
show_help(argv[0]);
}
}
if (args)
tm_signal_force_msr();
else
show_help(argv[0]);
}