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
|
#include <init/stdlib.h>
#include <init/tests/main.h>
#include <shared/flags.h>
#include <shared/syscalls.h>
#define argify(str) str, sizeof(str) - 1
#define test_fail() do { \
printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \
return; \
} while (0)
#define assert(cond) if (!(cond)) test_fail();
static void run_forked(void (*fn)()) {
if (!_syscall_fork(0, NULL)) {
fn();
_syscall_exit(0);
} else {
/* successful tests must return 0
* TODO add a better fail msg */
if (_syscall_await() != 0) test_fail();
}
}
static void test_await(void) {
/* creates 16 child processes, each returning a different value. then checks
* if await() returns every value exactly once */
int ret;
int counts[16] = {0};
for (int i = 0; i < 16; i++)
if (!_syscall_fork(0, NULL))
_syscall_exit(i);
while ((ret = _syscall_await()) != ~0) {
assert(0 <= ret && ret < 16);
counts[ret]++;
}
for (int i = 0; i < 16; i++)
assert(counts[i] == 1);
}
static void test_faults(void) {
/* tests what happens when child processes fault.
* expected behavior: parent processes still manages to finish, and it can
* reap all its children */
int await_cnt = 0;
if (!_syscall_fork(0, NULL)) { // invalid memory access
asm volatile("movb $69, 0" ::: "memory");
printf("this shouldn't happen");
_syscall_exit(-1);
}
if (!_syscall_fork(0, NULL)) { // #GP
asm volatile("hlt" ::: "memory");
printf("this shouldn't happen");
_syscall_exit(-1);
}
while (_syscall_await() != ~0) await_cnt++;
assert(await_cnt == 2);
}
static void test_interrupted_fs(void) {
handle_t h;
if (_syscall_fork(FORK_NEWFS, &h)) { /* child */
// TODO make a similar test with all 0s passed to fs_wait
struct fs_wait_response res;
_syscall_fs_wait(NULL, 0, &res);
_syscall_exit(0);
} else { /* parent */
_syscall_mount(h, "/", 1);
int ret = _syscall_open("/", 1);
// the handler quits while handling that call - but this syscall should return anyways
_syscall_exit(ret < 0 ? 0 : -1);
}
}
static void test_orphaned_fs(void) {
handle_t h;
if (_syscall_fork(FORK_NEWFS, &h)) { /* child */
_syscall_exit(0);
} else { /* parent */
_syscall_mount(h, "/", 1);
int ret = _syscall_open("/", 1);
// no handler will ever be available to handle this call - the syscall should instantly return
_syscall_exit(ret < 0 ? 0 : -1);
}
}
static void test_memflag(void) {
void *page = (void*)0x77777000;
_syscall_memflag(page, 4096, MEMFLAG_PRESENT); // allocate page
memset(page, 77, 4096); // write to it
_syscall_memflag(page, 4096, 0); // free it
if (!_syscall_fork(0, NULL)) {
memset(page, 11, 4096); // should segfault
_syscall_exit(0);
} else {
assert(_syscall_await() != 0); // test if the process crashed
}
_syscall_memflag((void*)0x100000, 4096, 0); // try to free kernel memory
// TODO the kernel shouldn't even be mapped in userland
}
static void test_malloc(void) {
// not really a test
void *p1, *p2, *p3;
p1 = malloc(420);
printf("p1 = 0x%x\n", p1);
p2 = malloc(1024);
printf("p2 = 0x%x\n", p2);
free(p2);
p2 = malloc(256);
printf("p2 = 0x%x\n", p2);
free(p2);
p2 = malloc(4096);
printf("p2 = 0x%x\n", p2);
free(p2);
free(p1);
}
static void stress_fork(void) {
/* run a lot of processes */
for (size_t i = 0; i < 2048; i++) {
if (!_syscall_fork(0, NULL)) _syscall_exit(0);
_syscall_await();
}
}
void test_all(void) {
run_forked(test_await);
run_forked(test_faults);
run_forked(test_interrupted_fs);
run_forked(test_orphaned_fs);
run_forked(test_memflag);
run_forked(test_malloc);
// run_forked(stress_fork);
}
|