-
Notifications
You must be signed in to change notification settings - Fork 46
/
test.c
469 lines (401 loc) · 11 KB
/
test.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
*
* testing route
*
* @author Bruce Tan
* @email brucetansh@gmail.com
*
* @author Paul Monigatti
* @email paulmoni@waikato.ac.nz
*
* @create date 2017-08-23 06:13:47
*
*/
#include <sys/ipc.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/debug.h>
#include <ucontext.h>
#include <sys/times.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/wait.h>
#include <sched.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/wait.h>
#define CMD_PROTOTYPE(name) int name(int argc, char**argv)
// Maps strings to function pointers
struct cmd_internal {
int (*handle)(int argc, char **argv);
char *name;
bool unittest;
};
CMD_PROTOTYPE(test_so);
CMD_PROTOTYPE(test_float);
CMD_PROTOTYPE(test_sigsegv);
CMD_PROTOTYPE(test_coroutine);
CMD_PROTOTYPE(test_eintr);
CMD_PROTOTYPE(test_nohandler);
CMD_PROTOTYPE(test_signal);
CMD_PROTOTYPE(test_while);
CMD_PROTOTYPE(run_all);
CMD_PROTOTYPE(test_timer);
CMD_PROTOTYPE(test_times);
CMD_PROTOTYPE(test_pipes);
struct cmd_internal test_commands[] = {
{ test_so, "stack", true },
{ test_float, "float", true },
{ test_coroutine, "coroutine", true },
{ test_sigsegv, "null", true },
{ test_eintr, "eintr", true },
{ test_timer, "timer", true },
{ test_times, "times", true },
{ test_signal, "signal", true },
{ test_pipes, "pipe", true },
{ test_while, "while", false },
{ run_all, "run", false },
{ test_nohandler, NULL, false},
{0}
};
int main(int argc, char **argv){
struct cmd_internal* handler;
if(!argv[1])
return test_nohandler(argc-1, argv+1);
handler = test_commands;
while(handler && handler->name != NULL && strcmp(argv[1], handler->name)) {
handler++;
}
// Run it
return handler->handle(argc-1, argv+1);
}
int test_nohandler(int argc, char** argv){
struct cmd_internal* handler;
handler = test_commands;
printf("Available Test Options\n");
while(handler->name){
printf(" * %s\n",handler->name);
handler++;
}
return 0;
}
int run_all(int argc, char** argv){
struct cmd_internal* handler;
char *handler_argv[2];
int ret;
handler = test_commands;
handler_argv[1] = NULL;
while(handler->name){
if (handler->unittest){
handler_argv[0] = handler->name;
ret = handler->handle(1, handler_argv);
printf("%s return %d\n---\n\n", handler->name, ret);
assert(ret == 0);
}
handler++;
}
return 0;
}
int sig_sum;
void usr_handler(int signum){
sig_sum += signum;
printf("SIGUR1 start\n");
raise(SIGUSR2);
printf("SIGUR1 end\n");
}
void usr2_handler(int signum){
sig_sum += signum;
printf("SIGUR2 start\n");
raise(SIGINT);
printf("SIGUR2 end\n");
}
void int_handler(int signum){
sig_sum += signum;
printf("SIGINT start\n");
raise(SIGTERM);
printf("SIGINT end\n");
}
void term_handler(int signum){
sig_sum += signum;
printf("SIGTERM start\n");
printf("SIGTERM end\n");
}
int test_signal(int argc, char **argv){
struct sigaction sa;
sigset_t set, prevset;
sig_sum = 0;
// Check out what would happen after changing
// the following sigemptyset to sigfillset
// explain why
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
// setup signal handlers
sa.sa_handler = usr_handler;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = usr2_handler;
sigaction(SIGUSR2, &sa, NULL);
sa.sa_handler = int_handler;
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = term_handler;
sigaction(SIGTERM, &sa, NULL);
sigfillset(&set);
sigprocmask(SIG_SETMASK, &set, &prevset);
// send SIGUSR1 to itself
// since SIGUSR1 is currently blocked by sigprocmask
// SIGUSR1 will be pended until sigsuspend
raise(SIGUSR1);
assert(sig_sum == 0);
// check pending signals
sigpending(&set);
assert(sigismember(&set, SIGUSR1));
assert(sig_sum == 0);
// unblock all pending signals
printf("signal handler usr1 should be called after this\n");
sigsuspend(&prevset);
assert(sig_sum == SIGUSR1 + SIGUSR2 + SIGTERM + SIGINT);
return 0;
}
int test_float(int argc, char **argv){
int foo = 0, zero = 0;
signal(SIGFPE, SIG_IGN);
foo = 1 / zero;
printf("dividing foo %d by 0\n", foo);
signal(SIGFPE, SIG_DFL);
return 0;
}
void stack_overflow(int a){
stack_overflow(a);
}
pid_t do_fork(){
return fork();
}
pid_t do_tfork(){
return tfork();
}
#define FORK_LEN 2
int test_so(int argc, char **argv){
pid_t (*fork_function_array[FORK_LEN])() = {do_fork, do_tfork};
int i = 0;
for(i = 0; i < FORK_LEN; i++){
pid_t pid = fork_function_array[i]();
if(!pid){// child
wramp_syscall(WINFO, WINFO_NO_GPF);
printf("Generating stack overflow for %d ....\n", i);
stack_overflow(1);
}else{
int status;
wait(&status);
assert(WIFSIGNALED(status));
assert(WTERMSIG(status) == SIGSEGV);
}
}
return 0;
}
void sigsegv_handler(int signum){
printf("sigsegv handler invoked %d\n", signum);
}
int test_sigsegv(int argc, char **argv){
char *p = (char *)NULL + 1;
int pid;
int result;
if ((pid = tfork()) == 0){
signal(SIGSEGV, sigsegv_handler);
printf("%s", p);
signal(SIGSEGV, SIG_DFL);
exit(0);
}else{
wait(&result);
// printf("%d %d %d\n", result, WEXITSTATUS(result), WIFSIGNALED(result));
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
}
return 0;
}
bool alarm_handler_called = false;
void alarm_handler(int signum){
printf("alarm handler triggered\n");
alarm_handler_called = true;
}
int test_eintr(int argc, char **argv){
int status;
char *endptr;
if(!tfork()){
char __buffer[10];
int ret;
clock_t seconds = 0;
suseconds_t microseconds = 1000;
struct itimerval itv;
memset(&itv, 0, sizeof(itv));
alarm_handler_called = false;
if (argc > 1){
seconds = strtol(argv[1], &endptr, 10);
if (*endptr){
fprintf(stderr, "Invalid number: %s\n", argv[1]);
return 1;
}
}
itv.it_value.tv_sec = seconds;
itv.it_value.tv_usec = microseconds;
signal(SIGALRM, alarm_handler);
setitimer(ITIMER_REAL, &itv, NULL);
ret = read(STDIN_FILENO, __buffer, 10 * sizeof(char));
assert(ret == -1);
assert(errno == EINTR);
assert(alarm_handler_called == true);
exit(0);
}else{
wait(&status);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
}
return 0;
}
int test_timer(int argc, char ** argv){
suseconds_t microseconds = 5000;
struct itimerval itv;
struct sigaction act;
memset(&itv, 0, sizeof(itv));
memset(&act, 0, sizeof(act));
alarm_handler_called = false;
if(!tfork()){
itv.it_value.tv_usec = microseconds;
itv.it_interval = itv.it_value;
act.sa_handler = alarm_handler;
act.sa_flags = SA_RESETHAND;
sigaction(SIGALRM, &act, NULL);
setitimer(ITIMER_REAL, &itv, NULL);
while(1);
}else{
int status;
wait(&status);
assert(alarm_handler_called == true);
assert(WIFSIGNALED(status));
assert(WTERMSIG(status) == SIGALRM);
}
return 0;
}
ucontext_t mcontext;
#define COROUTINE_STACK_SIZE 56
int sum;
void func(int arg) {
printf("Hello World! I'm coroutine %d\n",arg);
sum += arg;
}
int test_coroutine(int argc, char **argv){
int i,j,num = 2;
int count = 1;
int cumulative = 0;
void **thread_stack_op;
ucontext_t *coroutines_list;
ucontext_t *coroutine;
char *endptr;
sum = 0;
if(argc > 1){
num = strtol(argv[1], &endptr, 10);
if (*endptr){
fprintf(stderr, "Invalid number: %s\n", argv[1]);
return 1;
}
}
// ucontext represents the context for each thread
coroutines_list = malloc(sizeof(ucontext_t) * num);
if(coroutines_list == NULL)
goto err;
// thread_stack_op saves the original pointer returned by malloc
// so later we can use it to free the malloced memory
thread_stack_op = malloc(sizeof(int) * num);
if(thread_stack_op == NULL)
goto err_free_coroutines;
coroutine = coroutines_list;
// Allocate stack for each thread
for( i = 0; i < num; i++){
if ((thread_stack_op[i] = malloc(COROUTINE_STACK_SIZE)) != NULL) {
coroutine->uc_stack.ss_sp = thread_stack_op[i];
coroutine->uc_stack.ss_size = COROUTINE_STACK_SIZE;
coroutine->uc_link = &mcontext;
cumulative += count;
makecontext(coroutine,func,1,count++);
coroutine++;
if(i && i % 50 == 0)
putchar('!');
}else{
goto err_free_all;
}
}
putchar('\n');
coroutine = coroutines_list;
// scheduling the coroutines
// note that we are using user thread library,
// so we have to manually schedule all the coroutines.
// Currently the scheduling algorithm is just simple a round robin
for( i = 0; i < num; i++){
swapcontext(&mcontext,coroutine++);
}
assert(sum == cumulative);
err_free_all:
for( j = 0; j < i; j++){
free(thread_stack_op[j]);
}
free(thread_stack_op);
err_free_coroutines:
free(coroutines_list);
err:
if(errno == ENOMEM)
perror("malloc");
return 0;
}
int test_while(int argc, char **argv){
struct timespec ts;
memset(&ts, 0, sizeof(struct timespec));
ts.tv_sec = 1;
while(1){
printf("a");
nanosleep(&ts, NULL);
}
return 0;
}
int test_times(int argc, char **argv){
struct tms tms;
clock_t start, end;
struct timespec req;
start = times(&tms);
assert(start > 0);
assert(tms.tms_utime > 0);
assert(tms.tms_stime > 0);
end = times((struct tms *)1);
assert(end == -1 && errno == EFAULT);
memset(&req, 0, sizeof(struct timespec));
req.tv_nsec = 100000000;
nanosleep(&req, NULL);
end = times(NULL);
assert(end > 0 && end > start);
return 0;
}
int test_pipes(int argc, char *argv[]){
int status, ret;
int pipefd[2];
char buffer[100];
char *wsh_argv[] = {"/bin/wsh", "-c", "echo abc\\\\ndef\\\\nghn | tail -n 2 | grep def", NULL};
assert(pipe(pipefd) == 0);
if (!tfork()){
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
close(pipefd[1]);
execv("/bin/wsh", wsh_argv);
exit(1);
}
wait(&status);
assert(WIFEXITED(status));
assert(WEXITSTATUS(status) == 0);
memset(buffer, 0, 100);
ret = read(pipefd[0], buffer, 100);
assert(ret == 4);
assert(strcmp(buffer, "def\n") == 0);
close(pipefd[0]);
close(pipefd[1]);
return 0;
}