-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsandbox.cpp
224 lines (171 loc) · 5.42 KB
/
sandbox.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
#include "include/sandbox.hpp"
#include "include/eval.hpp"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <random>
#include <sys/resource.h>
#include <sys/mman.h>
#include <unistd.h>
#include <zconf.h>
#include <wait.h>
Sandbox::Sandbox() {
this->isDebug = false;
this->ready = false;
std::cout << "Loading sandbox ..." << std::endl;
int page_size = 0xfff;
int geno_sz = 100;
int page_sz = getpagesize();
int pages = geno_sz/page_sz + 1;
this->TIMEOUT = 2;
this->pool_sz = pages * page_sz;
this->pool = (char *)malloc(this->pool_sz);
if (this->pool == NULL) {
std::cout << "The Sandbox cannot allocate the pool size of " << this->pool_sz << std::endl;
return;
}
if (mprotect((void *)((unsigned long)this->pool & ~(page_sz-1)), this->pool_sz+page_sz, PROT_EXEC|PROT_WRITE|PROT_READ) < 0) {
std::cerr << "The sandbox couldn't unprotect the pool" << std::endl;
perror("mprotect");
return;
}
this->clear();
this->ready = true;
std::cout << "sandbox ready." << std::endl;
}
Sandbox::~Sandbox() {
if (this->pool != NULL)
free(this->pool);
this->pool = 0;
std::cout << "sandbox unloaded. :(" << std::endl;
}
void Sandbox::debug() {
this->isDebug = true;
}
void Sandbox::clear(void) {
int TRAP = 0xcc;
int NEAR_RET = 0xc3;
int FAR_RET = 0xcb;
/*
char *exit = "\x31\xc0" // xor %eax,%eax
"\x40" // inc %eax
"\x89\xc3" // mov %eax,%ebx
"\xcd\x80"; // int $0x80
*/
memset(this->pool, NEAR_RET, this->pool_sz);
}
void Sandbox::load(char *code, unsigned long len) {
char NEAR_RET = 0xC3;
char FAR_RET = 0xCB;
if (len > this->pool_sz-5) {
std::cout << "Sandbox: Aborting load, code too long, len: " << len << std::endl;
return;
}
this->clear();
memcpy(this->pool, code, len);
memset(this->pool+len, NEAR_RET, 5);
}
void Sandbox::launch(void) {
void (*fptr)() = (void(*)())this->pool;
fptr();
}
int pipefd[2];
void pipe_alarm(int sig) {
char cfitness[6] = "err";
std::cout << "alarm!!" << std::endl;
write(pipefd[1], cfitness, 4);
close(pipefd[1]);
}
void Sandbox::run(Genotype *geno) {
int pid, stat, sz;
char cfitness[6];
this->load(geno->read(), geno->size());
geno->set_fitness(0);
if (pipe(pipefd) == -1) {
perror("pipe");
return;
}
//geno->save("run.gen.bin");
pid = fork();
if (pid<0) {
std::cout << "Sandbox: fork failed!!" << std::endl;
close(pipefd[0]);
close(pipefd[1]);
return;
}
if (pid==0) { // GDB debug: set follow-fork-mode child
// setsid();
pid = getpid();
// prepare signals
sigaction(SIGSEGV, NULL, NULL);
alarm(this->TIMEOUT);
this->launch();
// std::cout << "from child" << std::endl;
/*
struct rusage *r_usage;
if (getrusage(RUSAGE_SELF, r_usage) ==0) {
r_usage.
}*/
Eval eval(pid);
sprintf(cfitness, "%f", eval.get_fitness());
// send the fitness through a pipe
write(pipefd[1], cfitness, strlen(cfitness));
close(pipefd[1]);
exit(1);
} else {
//std::cout << "waiting child" << std::endl;
wait(&stat); //pid(pid);
//unsigned long id = rand() % 1000;
unsigned long result = 0;
if (WCOREDUMP(stat)) {
if (this->isDebug)
std::cout << " EXECUTION CRASHED!! " << std::endl;
result = this->RES_CRASH;
geno->set_fitness(0);
//kill(pid, SIGKILL);
//kill(pid, SIGHUP);
//geno->show();
//geno->save("coredump.gen");
} else {
if (WIFSIGNALED(stat)) {
if (WTERMSIG(stat)) {
// SIGALARM timeout
if (this->isDebug)
std::cout << " EXECUTION TIMEOUT!! " << std::endl;
result = this->RES_TIMEOUT;
geno->set_fitness(1);
//kill(pid, SIGKILL);
//kill(pid, SIGHUP);
}
}
if (WIFEXITED(stat)) {
signal(SIGALRM, pipe_alarm);
memset(cfitness, 0, sizeof(cfitness));
alarm(this->TIMEOUT);
sz = read(pipefd[0], cfitness, 4);
alarm(0);
signal(SIGALRM, NULL);
if (strcmp(cfitness, "err")==0) {
std::cout << "err received!!" << std::endl;
cfitness[0] = '0';
cfitness[1] = 0x00;
}
geno->set_fitness(2 + std::stof(cfitness));
if (this->isDebug)
std::cout << " EXECUTION OK!!" << std::endl;
result = this->RES_OK;
} else {
if (this->isDebug)
std::cout << "signaled" << std::endl;
//Eval eval(pid);
//geno->set_fitness(eval.get_fitness());
//std::cout << " EXECUTION SIGNALED" << std::endl;
result = this->RES_UNKNOWN;
//kill(pid, SIGKILL);
//kill(pid, SIGHUP);
}
}
}
close(pipefd[0]);
close(pipefd[1]);
}