Description:
[grader] better test case for timeout, changed where check_mem_usage is called to reduce inefficiency git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@167 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r43:b03203ec084a - - 2 files changed: 25 inserted, 11 deleted

@@ -1,663 +1,659
1 /*
1 /*
2 * A Simple Testing Sandbox
2 * A Simple Testing Sandbox
3 *
3 *
4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
5 */
5 */
6
6
7 #define _LARGEFILE64_SOURCE
7 #define _LARGEFILE64_SOURCE
8 //#define _GNU_SOURCE
8 //#define _GNU_SOURCE
9
9
10 #include <errno.h>
10 #include <errno.h>
11 #include <stdio.h>
11 #include <stdio.h>
12 #include <fcntl.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
13 #include <stdlib.h>
14 #include <string.h>
14 #include <string.h>
15 #include <stdarg.h>
15 #include <stdarg.h>
16 #include <unistd.h>
16 #include <unistd.h>
17 #include <getopt.h>
17 #include <getopt.h>
18 #include <time.h>
18 #include <time.h>
19 #include <sys/wait.h>
19 #include <sys/wait.h>
20 #include <sys/user.h>
20 #include <sys/user.h>
21 #include <sys/time.h>
21 #include <sys/time.h>
22 #include <sys/ptrace.h>
22 #include <sys/ptrace.h>
23 #include <sys/signal.h>
23 #include <sys/signal.h>
24 #include <sys/sysinfo.h>
24 #include <sys/sysinfo.h>
25 #include <sys/syscall.h>
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
26 #include <sys/resource.h>
27
27
28 #define NONRET __attribute__((noreturn))
28 #define NONRET __attribute__((noreturn))
29 #define UNUSED __attribute__((unused))
29 #define UNUSED __attribute__((unused))
30
30
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
31 static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
32 static int timeout;
32 static int timeout;
33 static int pass_environ;
33 static int pass_environ;
34 static int use_wall_clock;
34 static int use_wall_clock;
35 static int file_access;
35 static int file_access;
36 static int verbose;
36 static int verbose;
37 static int memory_limit;
37 static int memory_limit;
38 static int allow_times;
38 static int allow_times;
39 static char *redir_stdin, *redir_stdout;
39 static char *redir_stdin, *redir_stdout;
40 static char *set_cwd;
40 static char *set_cwd;
41
41
42 static pid_t box_pid;
42 static pid_t box_pid;
43 static int is_ptraced;
43 static int is_ptraced;
44 static volatile int timer_tick;
44 static volatile int timer_tick;
45 static time_t start_time;
45 static time_t start_time;
46 static int ticks_per_sec;
46 static int ticks_per_sec;
47
47
48 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
48 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ > 0
49 /* glibc 2.1 or newer -> has lseek64 */
49 /* glibc 2.1 or newer -> has lseek64 */
50 #define long_seek(f,o,w) lseek64(f,o,w)
50 #define long_seek(f,o,w) lseek64(f,o,w)
51 #else
51 #else
52 /* Touching clandestine places in glibc */
52 /* Touching clandestine places in glibc */
53 extern loff_t llseek(int fd, loff_t pos, int whence);
53 extern loff_t llseek(int fd, loff_t pos, int whence);
54 #define long_seek(f,o,w) llseek(f,o,w)
54 #define long_seek(f,o,w) llseek(f,o,w)
55 #endif
55 #endif
56
56
57 static void NONRET
57 static void NONRET
58 box_exit(void)
58 box_exit(void)
59 {
59 {
60 if (box_pid > 0)
60 if (box_pid > 0)
61 {
61 {
62 if (is_ptraced)
62 if (is_ptraced)
63 ptrace(PTRACE_KILL, box_pid);
63 ptrace(PTRACE_KILL, box_pid);
64 kill(-box_pid, SIGKILL);
64 kill(-box_pid, SIGKILL);
65 kill(box_pid, SIGKILL);
65 kill(box_pid, SIGKILL);
66 }
66 }
67 exit(1);
67 exit(1);
68 }
68 }
69
69
70 static void
70 static void
71 box_kill(void)
71 box_kill(void)
72 {
72 {
73 if (box_pid > 0)
73 if (box_pid > 0)
74 {
74 {
75 if (is_ptraced)
75 if (is_ptraced)
76 ptrace(PTRACE_KILL, box_pid);
76 ptrace(PTRACE_KILL, box_pid);
77 kill(-box_pid, SIGKILL);
77 kill(-box_pid, SIGKILL);
78 kill(box_pid, SIGKILL);
78 kill(box_pid, SIGKILL);
79 }
79 }
80 }
80 }
81
81
82 static void NONRET __attribute__((format(printf,1,2)))
82 static void NONRET __attribute__((format(printf,1,2)))
83 die(char *msg, ...)
83 die(char *msg, ...)
84 {
84 {
85 va_list args;
85 va_list args;
86 va_start(args, msg);
86 va_start(args, msg);
87 vfprintf(stderr, msg, args);
87 vfprintf(stderr, msg, args);
88 fputc('\n', stderr);
88 fputc('\n', stderr);
89 box_exit();
89 box_exit();
90 }
90 }
91
91
92 static void __attribute__((format(printf,1,2)))
92 static void __attribute__((format(printf,1,2)))
93 die_report(char *msg, ...)
93 die_report(char *msg, ...)
94 {
94 {
95 va_list args;
95 va_list args;
96 va_start(args, msg);
96 va_start(args, msg);
97 vfprintf(stderr, msg, args);
97 vfprintf(stderr, msg, args);
98 fputc('\n', stderr);
98 fputc('\n', stderr);
99 box_kill();
99 box_kill();
100 }
100 }
101
101
102 static void __attribute__((format(printf,1,2)))
102 static void __attribute__((format(printf,1,2)))
103 log(char *msg, ...)
103 log(char *msg, ...)
104 {
104 {
105 va_list args;
105 va_list args;
106 va_start(args, msg);
106 va_start(args, msg);
107 if (verbose)
107 if (verbose)
108 {
108 {
109 vfprintf(stderr, msg, args);
109 vfprintf(stderr, msg, args);
110 fflush(stderr);
110 fflush(stderr);
111 }
111 }
112 va_end(args);
112 va_end(args);
113 }
113 }
114
114
115 static void
115 static void
116 valid_filename(unsigned long addr)
116 valid_filename(unsigned long addr)
117 {
117 {
118 char namebuf[4096], *p, *end;
118 char namebuf[4096], *p, *end;
119 static int mem_fd;
119 static int mem_fd;
120
120
121 if (!file_access)
121 if (!file_access)
122 die("File access forbidden.");
122 die("File access forbidden.");
123 if (file_access >= 9)
123 if (file_access >= 9)
124 return;
124 return;
125
125
126 if (!mem_fd)
126 if (!mem_fd)
127 {
127 {
128 sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
128 sprintf(namebuf, "/proc/%d/mem", (int) box_pid);
129 mem_fd = open(namebuf, O_RDONLY);
129 mem_fd = open(namebuf, O_RDONLY);
130 if (mem_fd < 0)
130 if (mem_fd < 0)
131 die("open(%s): %m", namebuf);
131 die("open(%s): %m", namebuf);
132 }
132 }
133 p = end = namebuf;
133 p = end = namebuf;
134 do
134 do
135 {
135 {
136 if (p >= end)
136 if (p >= end)
137 {
137 {
138 int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
138 int remains = PAGE_SIZE - (addr & (PAGE_SIZE-1));
139 int l = namebuf + sizeof(namebuf) - end;
139 int l = namebuf + sizeof(namebuf) - end;
140 if (l > remains)
140 if (l > remains)
141 l = remains;
141 l = remains;
142 if (!l)
142 if (!l)
143 die("Access to file with name too long.");
143 die("Access to file with name too long.");
144 if (long_seek(mem_fd, addr, SEEK_SET) < 0)
144 if (long_seek(mem_fd, addr, SEEK_SET) < 0)
145 die("long_seek(mem): %m");
145 die("long_seek(mem): %m");
146 remains = read(mem_fd, end, l);
146 remains = read(mem_fd, end, l);
147 if (remains < 0)
147 if (remains < 0)
148 die("read(mem): %m");
148 die("read(mem): %m");
149 if (!remains)
149 if (!remains)
150 die("Access to file with name out of memory.");
150 die("Access to file with name out of memory.");
151 end += l;
151 end += l;
152 addr += l;
152 addr += l;
153 }
153 }
154 }
154 }
155 while (*p++);
155 while (*p++);
156
156
157 log("[%s] ", namebuf);
157 log("[%s] ", namebuf);
158 if (file_access >= 3)
158 if (file_access >= 3)
159 return;
159 return;
160 if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
160 if (!strchr(namebuf, '/') && strcmp(namebuf, ".."))
161 return;
161 return;
162 if (file_access >= 2)
162 if (file_access >= 2)
163 {
163 {
164 if ((!strncmp(namebuf, "/etc/", 5) ||
164 if ((!strncmp(namebuf, "/etc/", 5) ||
165 !strncmp(namebuf, "/lib/", 5) ||
165 !strncmp(namebuf, "/lib/", 5) ||
166 !strncmp(namebuf, "/usr/lib/", 9))
166 !strncmp(namebuf, "/usr/lib/", 9))
167 && !strstr(namebuf, ".."))
167 && !strstr(namebuf, ".."))
168 return;
168 return;
169 if (!strcmp(namebuf, "/dev/null") ||
169 if (!strcmp(namebuf, "/dev/null") ||
170 !strcmp(namebuf, "/dev/zero") ||
170 !strcmp(namebuf, "/dev/zero") ||
171 !strcmp(namebuf, "/proc/meminfo") ||
171 !strcmp(namebuf, "/proc/meminfo") ||
172 !strcmp(namebuf, "/proc/self/stat") ||
172 !strcmp(namebuf, "/proc/self/stat") ||
173 !strncmp(namebuf, "/usr/share/zoneinfo/", 20))
173 !strncmp(namebuf, "/usr/share/zoneinfo/", 20))
174 return;
174 return;
175 }
175 }
176 die("Forbidden access to file `%s'.", namebuf);
176 die("Forbidden access to file `%s'.", namebuf);
177 }
177 }
178
178
179 static int
179 static int
180 valid_syscall(struct user *u)
180 valid_syscall(struct user *u)
181 {
181 {
182 switch (u->regs.orig_eax)
182 switch (u->regs.orig_eax)
183 {
183 {
184 case __NR_execve:
184 case __NR_execve:
185 {
185 {
186 static int exec_counter;
186 static int exec_counter;
187 return !exec_counter++;
187 return !exec_counter++;
188 }
188 }
189 case __NR_open:
189 case __NR_open:
190 case __NR_creat:
190 case __NR_creat:
191 case __NR_unlink:
191 case __NR_unlink:
192 case __NR_oldstat:
192 case __NR_oldstat:
193 case __NR_access:
193 case __NR_access:
194 case __NR_oldlstat:
194 case __NR_oldlstat:
195 case __NR_truncate:
195 case __NR_truncate:
196 case __NR_stat:
196 case __NR_stat:
197 case __NR_lstat:
197 case __NR_lstat:
198 case __NR_truncate64:
198 case __NR_truncate64:
199 case __NR_stat64:
199 case __NR_stat64:
200 case __NR_lstat64:
200 case __NR_lstat64:
201 valid_filename(u->regs.ebx);
201 valid_filename(u->regs.ebx);
202 return 1;
202 return 1;
203 case __NR_exit:
203 case __NR_exit:
204 case __NR_read:
204 case __NR_read:
205 case __NR_write:
205 case __NR_write:
206 case __NR_close:
206 case __NR_close:
207 case __NR_lseek:
207 case __NR_lseek:
208 case __NR_getpid:
208 case __NR_getpid:
209 case __NR_getuid:
209 case __NR_getuid:
210 case __NR_oldfstat:
210 case __NR_oldfstat:
211 case __NR_dup:
211 case __NR_dup:
212 case __NR_brk:
212 case __NR_brk:
213 case __NR_getgid:
213 case __NR_getgid:
214 case __NR_geteuid:
214 case __NR_geteuid:
215 case __NR_getegid:
215 case __NR_getegid:
216 case __NR_dup2:
216 case __NR_dup2:
217 case __NR_ftruncate:
217 case __NR_ftruncate:
218 case __NR_fstat:
218 case __NR_fstat:
219 case __NR_personality:
219 case __NR_personality:
220 case __NR__llseek:
220 case __NR__llseek:
221 case __NR_readv:
221 case __NR_readv:
222 case __NR_writev:
222 case __NR_writev:
223 case __NR_getresuid:
223 case __NR_getresuid:
224 #ifdef __NR_pread64
224 #ifdef __NR_pread64
225 case __NR_pread64:
225 case __NR_pread64:
226 case __NR_pwrite64:
226 case __NR_pwrite64:
227 #else
227 #else
228 case __NR_pread:
228 case __NR_pread:
229 case __NR_pwrite:
229 case __NR_pwrite:
230 #endif
230 #endif
231 case __NR_ftruncate64:
231 case __NR_ftruncate64:
232 case __NR_fstat64:
232 case __NR_fstat64:
233 case __NR_fcntl:
233 case __NR_fcntl:
234 case __NR_fcntl64:
234 case __NR_fcntl64:
235 case __NR_mmap:
235 case __NR_mmap:
236 case __NR_munmap:
236 case __NR_munmap:
237 case __NR_ioctl:
237 case __NR_ioctl:
238 case __NR_uname:
238 case __NR_uname:
239 case 252:
239 case 252:
240 case 243:
240 case 243:
241 return 1;
241 return 1;
242 // case __NR_time:
242 // case __NR_time:
243 case __NR_alarm:
243 case __NR_alarm:
244 // case __NR_pause:
244 // case __NR_pause:
245 case __NR_signal:
245 case __NR_signal:
246 case __NR_fchmod:
246 case __NR_fchmod:
247 case __NR_sigaction:
247 case __NR_sigaction:
248 case __NR_sgetmask:
248 case __NR_sgetmask:
249 case __NR_ssetmask:
249 case __NR_ssetmask:
250 case __NR_sigsuspend:
250 case __NR_sigsuspend:
251 case __NR_sigpending:
251 case __NR_sigpending:
252 case __NR_getrlimit:
252 case __NR_getrlimit:
253 case __NR_getrusage:
253 case __NR_getrusage:
254 case __NR_gettimeofday:
254 case __NR_gettimeofday:
255 case __NR_select:
255 case __NR_select:
256 case __NR_readdir:
256 case __NR_readdir:
257 case __NR_setitimer:
257 case __NR_setitimer:
258 case __NR_getitimer:
258 case __NR_getitimer:
259 case __NR_sigreturn:
259 case __NR_sigreturn:
260 case __NR_mprotect:
260 case __NR_mprotect:
261 case __NR_sigprocmask:
261 case __NR_sigprocmask:
262 case __NR_getdents:
262 case __NR_getdents:
263 case __NR_getdents64:
263 case __NR_getdents64:
264 case __NR__newselect:
264 case __NR__newselect:
265 case __NR_fdatasync:
265 case __NR_fdatasync:
266 case __NR_mremap:
266 case __NR_mremap:
267 case __NR_poll:
267 case __NR_poll:
268 case __NR_getcwd:
268 case __NR_getcwd:
269 case __NR_nanosleep:
269 case __NR_nanosleep:
270 case __NR_rt_sigreturn:
270 case __NR_rt_sigreturn:
271 case __NR_rt_sigaction:
271 case __NR_rt_sigaction:
272 case __NR_rt_sigprocmask:
272 case __NR_rt_sigprocmask:
273 case __NR_rt_sigpending:
273 case __NR_rt_sigpending:
274 case __NR_rt_sigtimedwait:
274 case __NR_rt_sigtimedwait:
275 case __NR_rt_sigqueueinfo:
275 case __NR_rt_sigqueueinfo:
276 case __NR_rt_sigsuspend:
276 case __NR_rt_sigsuspend:
277 case __NR_mmap2:
277 case __NR_mmap2:
278 case __NR__sysctl:
278 case __NR__sysctl:
279 return (filter_syscalls == 1);
279 return (filter_syscalls == 1);
280 case __NR_times:
280 case __NR_times:
281 return allow_times;
281 return allow_times;
282 case __NR_kill:
282 case __NR_kill:
283 if (u->regs.ebx == box_pid)
283 if (u->regs.ebx == box_pid)
284 die("Commited suicide by signal %d.", (int)u->regs.ecx);
284 die("Commited suicide by signal %d.", (int)u->regs.ecx);
285 return 0;
285 return 0;
286 default:
286 default:
287 return 0;
287 return 0;
288 }
288 }
289 }
289 }
290
290
291 static void
291 static void
292 signal_alarm(int unused UNUSED)
292 signal_alarm(int unused UNUSED)
293 {
293 {
294 /* Time limit checks are synchronous, so we only schedule them there. */
294 /* Time limit checks are synchronous, so we only schedule them there. */
295 timer_tick = 1;
295 timer_tick = 1;
296
296
297 //NOTE: do not use alarm, changed to setitimer for precision
297 //NOTE: do not use alarm, changed to setitimer for precision
298 // alarm(1);
298 // alarm(1);
299 }
299 }
300
300
301 static void
301 static void
302 signal_int(int unused UNUSED)
302 signal_int(int unused UNUSED)
303 {
303 {
304 /* Interrupts are fatal, so no synchronization requirements. */
304 /* Interrupts are fatal, so no synchronization requirements. */
305 die("Interrupted.");
305 die("Interrupted.");
306 }
306 }
307
307
308 static void
308 static void
309 check_timeout(void)
309 check_timeout(void)
310 {
310 {
311 int sec;
311 int sec;
312
312
313 if (use_wall_clock)
313 if (use_wall_clock)
314 sec = time(NULL) - start_time;
314 sec = time(NULL) - start_time;
315 else
315 else
316 {
316 {
317 char buf[4096], *x;
317 char buf[4096], *x;
318 int c, utime, stime;
318 int c, utime, stime;
319 static int proc_status_fd;
319 static int proc_status_fd;
320 if (!proc_status_fd)
320 if (!proc_status_fd)
321 {
321 {
322 sprintf(buf, "/proc/%d/stat", (int) box_pid);
322 sprintf(buf, "/proc/%d/stat", (int) box_pid);
323 proc_status_fd = open(buf, O_RDONLY);
323 proc_status_fd = open(buf, O_RDONLY);
324 if (proc_status_fd < 0)
324 if (proc_status_fd < 0)
325 die("open(%s): %m", buf);
325 die("open(%s): %m", buf);
326 }
326 }
327 lseek(proc_status_fd, 0, SEEK_SET);
327 lseek(proc_status_fd, 0, SEEK_SET);
328 if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
328 if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
329 die("read on /proc/$pid/stat: %m");
329 die("read on /proc/$pid/stat: %m");
330 if (c >= (int) sizeof(buf) - 1)
330 if (c >= (int) sizeof(buf) - 1)
331 die("/proc/$pid/stat too long");
331 die("/proc/$pid/stat too long");
332 buf[c] = 0;
332 buf[c] = 0;
333 x = buf;
333 x = buf;
334 while (*x && *x != ' ')
334 while (*x && *x != ' ')
335 x++;
335 x++;
336 while (*x == ' ')
336 while (*x == ' ')
337 x++;
337 x++;
338 if (*x++ != '(')
338 if (*x++ != '(')
339 die("proc syntax error 1");
339 die("proc syntax error 1");
340 while (*x && (*x != ')' || x[1] != ' '))
340 while (*x && (*x != ')' || x[1] != ' '))
341 x++;
341 x++;
342 while (*x == ')' || *x == ' ')
342 while (*x == ')' || *x == ' ')
343 x++;
343 x++;
344 if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
344 if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
345 die("proc syntax error 2");
345 die("proc syntax error 2");
346 //printf("%s - %d\n",x,ticks_per_sec);
346 //printf("%s - %d\n",x,ticks_per_sec);
347 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
347 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
348 }
348 }
349 if (verbose > 1)
349 if (verbose > 1)
350 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
350 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
351 if (sec > timeout) {
351 if (sec > timeout) {
352 die_report("Time limit exceeded.");
352 die_report("Time limit exceeded.");
353 }
353 }
354 }
354 }
355
355
356 int max_mem_used = 0;
356 int max_mem_used = 0;
357
357
358 static void
358 static void
359 check_memory_usage()
359 check_memory_usage()
360 {
360 {
361 char proc_fname[100];
361 char proc_fname[100];
362 sprintf(proc_fname,"/proc/%d/statm",box_pid);
362 sprintf(proc_fname,"/proc/%d/statm",box_pid);
363 //printf("proc fname: %s\n",proc_fname);
363 //printf("proc fname: %s\n",proc_fname);
364 FILE *fp = fopen(proc_fname,"r");
364 FILE *fp = fopen(proc_fname,"r");
365 if(fp!=NULL) {
365 if(fp!=NULL) {
366 char line[1000];
366 char line[1000];
367 fgets(line,999,fp);
367 fgets(line,999,fp);
368 //printf("%s\n",line);
368 //printf("%s\n",line);
369 int m;
369 int m;
370
370
371 if(sscanf(line,"%d",&m)==1)
371 if(sscanf(line,"%d",&m)==1)
372 if(m>max_mem_used)
372 if(m>max_mem_used)
373 max_mem_used = m;
373 max_mem_used = m;
374
374
375 fclose(fp);
375 fclose(fp);
376 }
376 }
377 }
377 }
378
378
379 static void
379 static void
380 boxkeeper(void)
380 boxkeeper(void)
381 {
381 {
382 int syscall_count = 0;
382 int syscall_count = 0;
383 struct sigaction sa;
383 struct sigaction sa;
384
384
385 is_ptraced = 1;
385 is_ptraced = 1;
386 bzero(&sa, sizeof(sa));
386 bzero(&sa, sizeof(sa));
387 sa.sa_handler = signal_int;
387 sa.sa_handler = signal_int;
388 sigaction(SIGINT, &sa, NULL);
388 sigaction(SIGINT, &sa, NULL);
389 start_time = time(NULL);
389 start_time = time(NULL);
390 ticks_per_sec = sysconf(_SC_CLK_TCK);
390 ticks_per_sec = sysconf(_SC_CLK_TCK);
391 if (ticks_per_sec <= 0)
391 if (ticks_per_sec <= 0)
392 die("Invalid ticks_per_sec!");
392 die("Invalid ticks_per_sec!");
393
393
394 check_memory_usage();
394 check_memory_usage();
395
395
396 sa.sa_handler = signal_alarm;
396 sa.sa_handler = signal_alarm;
397 sigaction(SIGALRM, &sa, NULL);
397 sigaction(SIGALRM, &sa, NULL);
398 //alarm(1);
398 //alarm(1);
399
399
400 struct itimerval val;
400 struct itimerval val;
401 val.it_interval.tv_sec = 0;
401 val.it_interval.tv_sec = 0;
402 val.it_interval.tv_usec = 50000;
402 val.it_interval.tv_usec = 50000;
403 val.it_value.tv_sec = 0;
403 val.it_value.tv_sec = 0;
404 val.it_value.tv_usec = 50000;
404 val.it_value.tv_usec = 50000;
405 setitimer(ITIMER_REAL,&val,NULL);
405 setitimer(ITIMER_REAL,&val,NULL);
406
406
407 /*
407 /*
408 --- add alarm handler no matter what..
408 --- add alarm handler no matter what..
409 if (timeout)
409 if (timeout)
410 {
410 {
411 sa.sa_handler = signal_alarm;
411 sa.sa_handler = signal_alarm;
412 sigaction(SIGALRM, &sa, NULL);
412 sigaction(SIGALRM, &sa, NULL);
413 alarm(1);
413 alarm(1);
414 }
414 }
415 */
415 */
416
416
417 for(;;)
417 for(;;)
418 {
418 {
419 struct rusage rus;
419 struct rusage rus;
420 int stat;
420 int stat;
421 pid_t p;
421 pid_t p;
422
422
423 if (timer_tick)
423 if (timer_tick)
424 {
424 {
425 check_timeout();
425 check_timeout();
426 + check_memory_usage();
426 timer_tick = 0;
427 timer_tick = 0;
427 }
428 }
428 p = wait4(box_pid, &stat, WUNTRACED, &rus);
429 p = wait4(box_pid, &stat, WUNTRACED, &rus);
429
430
430 - if(!WIFEXITED(stat)) {
431 - // printf("CHECKING\n");
432 - check_memory_usage();
433 - }
434 -
435 if (p < 0)
431 if (p < 0)
436 {
432 {
437 if (errno == EINTR)
433 if (errno == EINTR)
438 continue;
434 continue;
439 die("wait4: %m");
435 die("wait4: %m");
440 }
436 }
441 if (p != box_pid)
437 if (p != box_pid)
442 die("wait4: unknown pid %d exited!", p);
438 die("wait4: unknown pid %d exited!", p);
443 if (WIFEXITED(stat))
439 if (WIFEXITED(stat))
444 {
440 {
445 struct timeval total;
441 struct timeval total;
446 int wall;
442 int wall;
447 wall = time(NULL) - start_time;
443 wall = time(NULL) - start_time;
448 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
444 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
449
445
450 box_pid = 0;
446 box_pid = 0;
451 if (WEXITSTATUS(stat))
447 if (WEXITSTATUS(stat))
452 - fprintf(stderr,"Exited with error status %d.", WEXITSTATUS(stat));
448 + fprintf(stderr,"Exited with error status %d.\n", WEXITSTATUS(stat));
453 else if ((use_wall_clock ? wall : total.tv_sec) > timeout)
449 else if ((use_wall_clock ? wall : total.tv_sec) > timeout)
454 - fprintf(stderr,"Time limit exceeded.");
450 + fprintf(stderr,"Time limit exceeded.\n");
455 else
451 else
456 // report OK and statistics
452 // report OK and statistics
457 fprintf(stderr,"OK\n");
453 fprintf(stderr,"OK\n");
458
454
459 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
455 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
460 wall,
456 wall,
461 (double) rus.ru_utime.tv_sec +
457 (double) rus.ru_utime.tv_sec +
462 ((double) rus.ru_utime.tv_usec/1000000.0),
458 ((double) rus.ru_utime.tv_usec/1000000.0),
463 (double) rus.ru_stime.tv_sec +
459 (double) rus.ru_stime.tv_sec +
464 ((double) rus.ru_stime.tv_usec/1000000.0),
460 ((double) rus.ru_stime.tv_usec/1000000.0),
465 max_mem_used);
461 max_mem_used);
466 /*
462 /*
467 (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n",
463 (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n",
468 (double) total.tv_sec + ((double)total.tv_usec / 1000000.0),
464 (double) total.tv_sec + ((double)total.tv_usec / 1000000.0),
469 (int) total.tv_usec,
465 (int) total.tv_usec,
470 wall,
466 wall,
471 syscall_count,
467 syscall_count,
472 max_mem_used);
468 max_mem_used);
473 */
469 */
474 exit(0);
470 exit(0);
475 }
471 }
476 if (WIFSIGNALED(stat))
472 if (WIFSIGNALED(stat))
477 {
473 {
478 box_pid = 0;
474 box_pid = 0;
479 - fprintf(stderr,"Caught fatal signal %d.", WTERMSIG(stat));
475 + fprintf(stderr,"Caught fatal signal %d.\n", WTERMSIG(stat));
480
476
481 struct timeval total;
477 struct timeval total;
482 int wall;
478 int wall;
483 wall = time(NULL) - start_time;
479 wall = time(NULL) - start_time;
484 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
480 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
485 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
481 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
486 wall,
482 wall,
487 (double) rus.ru_utime.tv_sec +
483 (double) rus.ru_utime.tv_sec +
488 ((double) rus.ru_utime.tv_usec/1000000.0),
484 ((double) rus.ru_utime.tv_usec/1000000.0),
489 (double) rus.ru_stime.tv_sec +
485 (double) rus.ru_stime.tv_sec +
490 ((double) rus.ru_stime.tv_usec/1000000.0),
486 ((double) rus.ru_stime.tv_usec/1000000.0),
491 max_mem_used);
487 max_mem_used);
492 exit(0);
488 exit(0);
493 }
489 }
494 if (WIFSTOPPED(stat))
490 if (WIFSTOPPED(stat))
495 {
491 {
496 int sig = WSTOPSIG(stat);
492 int sig = WSTOPSIG(stat);
497 if (sig == SIGTRAP)
493 if (sig == SIGTRAP)
498 {
494 {
499 struct user u;
495 struct user u;
500 static int stop_count = -1;
496 static int stop_count = -1;
501 if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
497 if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
502 die("ptrace(PTRACE_GETREGS): %m");
498 die("ptrace(PTRACE_GETREGS): %m");
503 stop_count++;
499 stop_count++;
504 if (!stop_count) /* Traceme request */
500 if (!stop_count) /* Traceme request */
505 log(">> Traceme request caught\n");
501 log(">> Traceme request caught\n");
506 else if (stop_count & 1) /* Syscall entry */
502 else if (stop_count & 1) /* Syscall entry */
507 {
503 {
508 log(">> Syscall %3ld (%08lx,%08lx,%08lx) ", u.regs.orig_eax, u.regs.ebx, u.regs.ecx, u.regs.edx);
504 log(">> Syscall %3ld (%08lx,%08lx,%08lx) ", u.regs.orig_eax, u.regs.ebx, u.regs.ecx, u.regs.edx);
509 syscall_count++;
505 syscall_count++;
510 if (!valid_syscall(&u))
506 if (!valid_syscall(&u))
511 {
507 {
512 /*
508 /*
513 * Unfortunately, PTRACE_KILL kills _after_ the syscall completes,
509 * Unfortunately, PTRACE_KILL kills _after_ the syscall completes,
514 * so we have to change it to something harmless (e.g., an undefined
510 * so we have to change it to something harmless (e.g., an undefined
515 * syscall) and make the program continue.
511 * syscall) and make the program continue.
516 */
512 */
517 unsigned int sys = u.regs.orig_eax;
513 unsigned int sys = u.regs.orig_eax;
518 u.regs.orig_eax = 0xffffffff;
514 u.regs.orig_eax = 0xffffffff;
519 if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
515 if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
520 die("ptrace(PTRACE_SETREGS): %m");
516 die("ptrace(PTRACE_SETREGS): %m");
521 die("Forbidden syscall %d.", sys);
517 die("Forbidden syscall %d.", sys);
522 }
518 }
523 }
519 }
524 else /* Syscall return */
520 else /* Syscall return */
525 log("= %ld\n", u.regs.eax);
521 log("= %ld\n", u.regs.eax);
526 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
522 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
527 }
523 }
528 else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
524 else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
529 {
525 {
530 log(">> Signal %d\n", sig);
526 log(">> Signal %d\n", sig);
531 ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
527 ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
532 }
528 }
533 else
529 else
534 die("Received signal %d.", sig);
530 die("Received signal %d.", sig);
535 }
531 }
536 else
532 else
537 die("wait4: unknown status %x, giving up!", stat);
533 die("wait4: unknown status %x, giving up!", stat);
538 }
534 }
539 }
535 }
540
536
541 static void
537 static void
542 box_inside(int argc, char **argv)
538 box_inside(int argc, char **argv)
543 {
539 {
544 struct rlimit rl;
540 struct rlimit rl;
545 char *args[argc+1];
541 char *args[argc+1];
546 char *env[1] = { NULL };
542 char *env[1] = { NULL };
547
543
548 memcpy(args, argv, argc * sizeof(char *));
544 memcpy(args, argv, argc * sizeof(char *));
549 args[argc] = NULL;
545 args[argc] = NULL;
550 if (set_cwd && chdir(set_cwd))
546 if (set_cwd && chdir(set_cwd))
551 die("chdir: %m");
547 die("chdir: %m");
552 if (redir_stdin)
548 if (redir_stdin)
553 {
549 {
554 close(0);
550 close(0);
555 if (open(redir_stdin, O_RDONLY) != 0)
551 if (open(redir_stdin, O_RDONLY) != 0)
556 die("open(\"%s\"): %m", redir_stdin);
552 die("open(\"%s\"): %m", redir_stdin);
557 }
553 }
558 if (redir_stdout)
554 if (redir_stdout)
559 {
555 {
560 close(1);
556 close(1);
561 if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1)
557 if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1)
562 die("open(\"%s\"): %m", redir_stdout);
558 die("open(\"%s\"): %m", redir_stdout);
563 }
559 }
564 dup2(1, 2);
560 dup2(1, 2);
565 setpgrp();
561 setpgrp();
566 if (memory_limit)
562 if (memory_limit)
567 {
563 {
568 rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
564 rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
569 if (setrlimit(RLIMIT_AS, &rl) < 0)
565 if (setrlimit(RLIMIT_AS, &rl) < 0)
570 die("setrlimit: %m");
566 die("setrlimit: %m");
571 }
567 }
572 rl.rlim_cur = rl.rlim_max = 64;
568 rl.rlim_cur = rl.rlim_max = 64;
573 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
569 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
574 die("setrlimit: %m");
570 die("setrlimit: %m");
575 if (filter_syscalls && ptrace(PTRACE_TRACEME) < 0)
571 if (filter_syscalls && ptrace(PTRACE_TRACEME) < 0)
576 die("ptrace(PTRACE_TRACEME): %m");
572 die("ptrace(PTRACE_TRACEME): %m");
577 execve(args[0], args, (pass_environ ? environ : env));
573 execve(args[0], args, (pass_environ ? environ : env));
578 die("execve(\"%s\"): %m", args[0]);
574 die("execve(\"%s\"): %m", args[0]);
579 }
575 }
580
576
581 static void
577 static void
582 usage(void)
578 usage(void)
583 {
579 {
584 fprintf(stderr, "Invalid arguments!\n");
580 fprintf(stderr, "Invalid arguments!\n");
585 printf("\
581 printf("\
586 Usage: box [<options>] -- <command> <arguments>\n\
582 Usage: box [<options>] -- <command> <arguments>\n\
587 \n\
583 \n\
588 Options:\n\
584 Options:\n\
589 -a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
585 -a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
590 -c <dir>\tChange directory to <dir> first\n\
586 -c <dir>\tChange directory to <dir> first\n\
591 -e\t\tPass full environment of parent process\n\
587 -e\t\tPass full environment of parent process\n\
592 -f\t\tFilter system calls (-ff=very restricted)\n\
588 -f\t\tFilter system calls (-ff=very restricted)\n\
593 -i <file>\tRedirect stdin from <file>\n\
589 -i <file>\tRedirect stdin from <file>\n\
594 -m <size>\tLimit address space to <size> KB\n\
590 -m <size>\tLimit address space to <size> KB\n\
595 -o <file>\tRedirect stdout to <file>\n\
591 -o <file>\tRedirect stdout to <file>\n\
596 -t <time>\tStop after <time> seconds\n\
592 -t <time>\tStop after <time> seconds\n\
597 -T\t\tAllow syscalls for measuring run time\n\
593 -T\t\tAllow syscalls for measuring run time\n\
598 -v\t\tBe verbose\n\
594 -v\t\tBe verbose\n\
599 -w\t\tMeasure wall clock time instead of run time\n\
595 -w\t\tMeasure wall clock time instead of run time\n\
600 ");
596 ");
601 exit(1);
597 exit(1);
602 }
598 }
603
599
604 int
600 int
605 main(int argc, char **argv)
601 main(int argc, char **argv)
606 {
602 {
607 int c;
603 int c;
608 uid_t uid;
604 uid_t uid;
609
605
610 while ((c = getopt(argc, argv, "a:c:efi:m:o:t:Tvw")) >= 0)
606 while ((c = getopt(argc, argv, "a:c:efi:m:o:t:Tvw")) >= 0)
611 switch (c)
607 switch (c)
612 {
608 {
613 case 'a':
609 case 'a':
614 file_access = atol(optarg);
610 file_access = atol(optarg);
615 break;
611 break;
616 case 'c':
612 case 'c':
617 set_cwd = optarg;
613 set_cwd = optarg;
618 break;
614 break;
619 case 'e':
615 case 'e':
620 pass_environ = 1;
616 pass_environ = 1;
621 break;
617 break;
622 case 'f':
618 case 'f':
623 filter_syscalls++;
619 filter_syscalls++;
624 break;
620 break;
625 case 'i':
621 case 'i':
626 redir_stdin = optarg;
622 redir_stdin = optarg;
627 break;
623 break;
628 case 'm':
624 case 'm':
629 memory_limit = atol(optarg);
625 memory_limit = atol(optarg);
630 break;
626 break;
631 case 'o':
627 case 'o':
632 redir_stdout = optarg;
628 redir_stdout = optarg;
633 break;
629 break;
634 case 't':
630 case 't':
635 timeout = atol(optarg);
631 timeout = atol(optarg);
636 break;
632 break;
637 case 'T':
633 case 'T':
638 allow_times++;
634 allow_times++;
639 break;
635 break;
640 case 'v':
636 case 'v':
641 verbose++;
637 verbose++;
642 break;
638 break;
643 case 'w':
639 case 'w':
644 use_wall_clock = 1;
640 use_wall_clock = 1;
645 break;
641 break;
646 default:
642 default:
647 usage();
643 usage();
648 }
644 }
649 if (optind >= argc)
645 if (optind >= argc)
650 usage();
646 usage();
651
647
652 uid = geteuid();
648 uid = geteuid();
653 if (setreuid(uid, uid) < 0)
649 if (setreuid(uid, uid) < 0)
654 die("setreuid: %m");
650 die("setreuid: %m");
655 box_pid = fork();
651 box_pid = fork();
656 if (box_pid < 0)
652 if (box_pid < 0)
657 die("fork: %m");
653 die("fork: %m");
658 if (!box_pid)
654 if (!box_pid)
659 box_inside(argc-optind, argv+optind);
655 box_inside(argc-optind, argv+optind);
660 else
656 else
661 boxkeeper();
657 boxkeeper();
662 die("Internal error: fell over edge of the world");
658 die("Internal error: fell over edge of the world");
663 }
659 }
@@ -1,23 +1,41
1 #include <stdio.h>
1 #include <stdio.h>
2 #include <stdlib.h>
2 #include <stdlib.h>
3 #include <unistd.h>
3 #include <unistd.h>
4 + #include <sys/time.h>
5 + #include <time.h>
6 + #include <sys/resource.h>
4
7
5 int main()
8 int main()
6 {
9 {
7 int a,b;
10 int a,b;
8
11
9 int c=0;
12 int c=0;
10
13
11 scanf("%d %d",&a,&b);
14 scanf("%d %d",&a,&b);
12 printf("%d\n",a+b);
15 printf("%d\n",a+b);
13
16
14 - sleep(1);
17 + struct rusage ru;
15
18
16 - c = 0;
19 + while(1) {
17 - while(c<1000000000) {
20 + c++;
21 + b+=c;
22 + while(c<1000000000) {
23 + c++;
24 + b+=c;
25 + }
26 + getrusage(RUSAGE_SELF,&ru);
27 + if((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec)>=1)
28 + break;
29 + }
30 + printf("%d\n",b);
31 + c=0;
32 + while(c<100000000) {
18 c++;
33 c++;
19 b+=c;
34 b+=c;
20 }
35 }
36 + if(b==10)
37 + printf("hello\n");
38 +
21 exit(0);
39 exit(0);
22 }
40 }
23
41
You need to be logged in to leave comments. Login now