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

@@ -234,430 +234,426
234 234 case __NR_fcntl64:
235 235 case __NR_mmap:
236 236 case __NR_munmap:
237 237 case __NR_ioctl:
238 238 case __NR_uname:
239 239 case 252:
240 240 case 243:
241 241 return 1;
242 242 // case __NR_time:
243 243 case __NR_alarm:
244 244 // case __NR_pause:
245 245 case __NR_signal:
246 246 case __NR_fchmod:
247 247 case __NR_sigaction:
248 248 case __NR_sgetmask:
249 249 case __NR_ssetmask:
250 250 case __NR_sigsuspend:
251 251 case __NR_sigpending:
252 252 case __NR_getrlimit:
253 253 case __NR_getrusage:
254 254 case __NR_gettimeofday:
255 255 case __NR_select:
256 256 case __NR_readdir:
257 257 case __NR_setitimer:
258 258 case __NR_getitimer:
259 259 case __NR_sigreturn:
260 260 case __NR_mprotect:
261 261 case __NR_sigprocmask:
262 262 case __NR_getdents:
263 263 case __NR_getdents64:
264 264 case __NR__newselect:
265 265 case __NR_fdatasync:
266 266 case __NR_mremap:
267 267 case __NR_poll:
268 268 case __NR_getcwd:
269 269 case __NR_nanosleep:
270 270 case __NR_rt_sigreturn:
271 271 case __NR_rt_sigaction:
272 272 case __NR_rt_sigprocmask:
273 273 case __NR_rt_sigpending:
274 274 case __NR_rt_sigtimedwait:
275 275 case __NR_rt_sigqueueinfo:
276 276 case __NR_rt_sigsuspend:
277 277 case __NR_mmap2:
278 278 case __NR__sysctl:
279 279 return (filter_syscalls == 1);
280 280 case __NR_times:
281 281 return allow_times;
282 282 case __NR_kill:
283 283 if (u->regs.ebx == box_pid)
284 284 die("Commited suicide by signal %d.", (int)u->regs.ecx);
285 285 return 0;
286 286 default:
287 287 return 0;
288 288 }
289 289 }
290 290
291 291 static void
292 292 signal_alarm(int unused UNUSED)
293 293 {
294 294 /* Time limit checks are synchronous, so we only schedule them there. */
295 295 timer_tick = 1;
296 296
297 297 //NOTE: do not use alarm, changed to setitimer for precision
298 298 // alarm(1);
299 299 }
300 300
301 301 static void
302 302 signal_int(int unused UNUSED)
303 303 {
304 304 /* Interrupts are fatal, so no synchronization requirements. */
305 305 die("Interrupted.");
306 306 }
307 307
308 308 static void
309 309 check_timeout(void)
310 310 {
311 311 int sec;
312 312
313 313 if (use_wall_clock)
314 314 sec = time(NULL) - start_time;
315 315 else
316 316 {
317 317 char buf[4096], *x;
318 318 int c, utime, stime;
319 319 static int proc_status_fd;
320 320 if (!proc_status_fd)
321 321 {
322 322 sprintf(buf, "/proc/%d/stat", (int) box_pid);
323 323 proc_status_fd = open(buf, O_RDONLY);
324 324 if (proc_status_fd < 0)
325 325 die("open(%s): %m", buf);
326 326 }
327 327 lseek(proc_status_fd, 0, SEEK_SET);
328 328 if ((c = read(proc_status_fd, buf, sizeof(buf)-1)) < 0)
329 329 die("read on /proc/$pid/stat: %m");
330 330 if (c >= (int) sizeof(buf) - 1)
331 331 die("/proc/$pid/stat too long");
332 332 buf[c] = 0;
333 333 x = buf;
334 334 while (*x && *x != ' ')
335 335 x++;
336 336 while (*x == ' ')
337 337 x++;
338 338 if (*x++ != '(')
339 339 die("proc syntax error 1");
340 340 while (*x && (*x != ')' || x[1] != ' '))
341 341 x++;
342 342 while (*x == ')' || *x == ' ')
343 343 x++;
344 344 if (sscanf(x, "%*c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d %d", &utime, &stime) != 2)
345 345 die("proc syntax error 2");
346 346 //printf("%s - %d\n",x,ticks_per_sec);
347 347 sec = (utime + stime + ticks_per_sec-1)/ticks_per_sec;
348 348 }
349 349 if (verbose > 1)
350 350 fprintf(stderr, "[timecheck: %d seconds]\n", sec);
351 351 if (sec > timeout) {
352 352 die_report("Time limit exceeded.");
353 353 }
354 354 }
355 355
356 356 int max_mem_used = 0;
357 357
358 358 static void
359 359 check_memory_usage()
360 360 {
361 361 char proc_fname[100];
362 362 sprintf(proc_fname,"/proc/%d/statm",box_pid);
363 363 //printf("proc fname: %s\n",proc_fname);
364 364 FILE *fp = fopen(proc_fname,"r");
365 365 if(fp!=NULL) {
366 366 char line[1000];
367 367 fgets(line,999,fp);
368 368 //printf("%s\n",line);
369 369 int m;
370 370
371 371 if(sscanf(line,"%d",&m)==1)
372 372 if(m>max_mem_used)
373 373 max_mem_used = m;
374 374
375 375 fclose(fp);
376 376 }
377 377 }
378 378
379 379 static void
380 380 boxkeeper(void)
381 381 {
382 382 int syscall_count = 0;
383 383 struct sigaction sa;
384 384
385 385 is_ptraced = 1;
386 386 bzero(&sa, sizeof(sa));
387 387 sa.sa_handler = signal_int;
388 388 sigaction(SIGINT, &sa, NULL);
389 389 start_time = time(NULL);
390 390 ticks_per_sec = sysconf(_SC_CLK_TCK);
391 391 if (ticks_per_sec <= 0)
392 392 die("Invalid ticks_per_sec!");
393 393
394 394 check_memory_usage();
395 395
396 396 sa.sa_handler = signal_alarm;
397 397 sigaction(SIGALRM, &sa, NULL);
398 398 //alarm(1);
399 399
400 400 struct itimerval val;
401 401 val.it_interval.tv_sec = 0;
402 402 val.it_interval.tv_usec = 50000;
403 403 val.it_value.tv_sec = 0;
404 404 val.it_value.tv_usec = 50000;
405 405 setitimer(ITIMER_REAL,&val,NULL);
406 406
407 407 /*
408 408 --- add alarm handler no matter what..
409 409 if (timeout)
410 410 {
411 411 sa.sa_handler = signal_alarm;
412 412 sigaction(SIGALRM, &sa, NULL);
413 413 alarm(1);
414 414 }
415 415 */
416 416
417 417 for(;;)
418 418 {
419 419 struct rusage rus;
420 420 int stat;
421 421 pid_t p;
422 422
423 423 if (timer_tick)
424 424 {
425 425 check_timeout();
426 + check_memory_usage();
426 427 timer_tick = 0;
427 428 }
428 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 431 if (p < 0)
436 432 {
437 433 if (errno == EINTR)
438 434 continue;
439 435 die("wait4: %m");
440 436 }
441 437 if (p != box_pid)
442 438 die("wait4: unknown pid %d exited!", p);
443 439 if (WIFEXITED(stat))
444 440 {
445 441 struct timeval total;
446 442 int wall;
447 443 wall = time(NULL) - start_time;
448 444 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
449 445
450 446 box_pid = 0;
451 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 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 451 else
456 452 // report OK and statistics
457 453 fprintf(stderr,"OK\n");
458 454
459 455 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
460 456 wall,
461 457 (double) rus.ru_utime.tv_sec +
462 458 ((double) rus.ru_utime.tv_usec/1000000.0),
463 459 (double) rus.ru_stime.tv_sec +
464 460 ((double) rus.ru_stime.tv_usec/1000000.0),
465 461 max_mem_used);
466 462 /*
467 463 (%.4lf sec real (%d), %d sec wall, %d syscalls, %d kb)\n",
468 464 (double) total.tv_sec + ((double)total.tv_usec / 1000000.0),
469 465 (int) total.tv_usec,
470 466 wall,
471 467 syscall_count,
472 468 max_mem_used);
473 469 */
474 470 exit(0);
475 471 }
476 472 if (WIFSIGNALED(stat))
477 473 {
478 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 477 struct timeval total;
482 478 int wall;
483 479 wall = time(NULL) - start_time;
484 480 timeradd(&rus.ru_utime, &rus.ru_stime, &total);
485 481 fprintf(stderr,"%dr%.4lfu%.4lfs%dm\n",
486 482 wall,
487 483 (double) rus.ru_utime.tv_sec +
488 484 ((double) rus.ru_utime.tv_usec/1000000.0),
489 485 (double) rus.ru_stime.tv_sec +
490 486 ((double) rus.ru_stime.tv_usec/1000000.0),
491 487 max_mem_used);
492 488 exit(0);
493 489 }
494 490 if (WIFSTOPPED(stat))
495 491 {
496 492 int sig = WSTOPSIG(stat);
497 493 if (sig == SIGTRAP)
498 494 {
499 495 struct user u;
500 496 static int stop_count = -1;
501 497 if (ptrace(PTRACE_GETREGS, box_pid, NULL, &u) < 0)
502 498 die("ptrace(PTRACE_GETREGS): %m");
503 499 stop_count++;
504 500 if (!stop_count) /* Traceme request */
505 501 log(">> Traceme request caught\n");
506 502 else if (stop_count & 1) /* Syscall entry */
507 503 {
508 504 log(">> Syscall %3ld (%08lx,%08lx,%08lx) ", u.regs.orig_eax, u.regs.ebx, u.regs.ecx, u.regs.edx);
509 505 syscall_count++;
510 506 if (!valid_syscall(&u))
511 507 {
512 508 /*
513 509 * Unfortunately, PTRACE_KILL kills _after_ the syscall completes,
514 510 * so we have to change it to something harmless (e.g., an undefined
515 511 * syscall) and make the program continue.
516 512 */
517 513 unsigned int sys = u.regs.orig_eax;
518 514 u.regs.orig_eax = 0xffffffff;
519 515 if (ptrace(PTRACE_SETREGS, box_pid, NULL, &u) < 0)
520 516 die("ptrace(PTRACE_SETREGS): %m");
521 517 die("Forbidden syscall %d.", sys);
522 518 }
523 519 }
524 520 else /* Syscall return */
525 521 log("= %ld\n", u.regs.eax);
526 522 ptrace(PTRACE_SYSCALL, box_pid, 0, 0);
527 523 }
528 524 else if (sig != SIGSTOP && sig != SIGXCPU && sig != SIGXFSZ)
529 525 {
530 526 log(">> Signal %d\n", sig);
531 527 ptrace(PTRACE_SYSCALL, box_pid, 0, sig);
532 528 }
533 529 else
534 530 die("Received signal %d.", sig);
535 531 }
536 532 else
537 533 die("wait4: unknown status %x, giving up!", stat);
538 534 }
539 535 }
540 536
541 537 static void
542 538 box_inside(int argc, char **argv)
543 539 {
544 540 struct rlimit rl;
545 541 char *args[argc+1];
546 542 char *env[1] = { NULL };
547 543
548 544 memcpy(args, argv, argc * sizeof(char *));
549 545 args[argc] = NULL;
550 546 if (set_cwd && chdir(set_cwd))
551 547 die("chdir: %m");
552 548 if (redir_stdin)
553 549 {
554 550 close(0);
555 551 if (open(redir_stdin, O_RDONLY) != 0)
556 552 die("open(\"%s\"): %m", redir_stdin);
557 553 }
558 554 if (redir_stdout)
559 555 {
560 556 close(1);
561 557 if (open(redir_stdout, O_WRONLY | O_CREAT | O_TRUNC, 0666) != 1)
562 558 die("open(\"%s\"): %m", redir_stdout);
563 559 }
564 560 dup2(1, 2);
565 561 setpgrp();
566 562 if (memory_limit)
567 563 {
568 564 rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
569 565 if (setrlimit(RLIMIT_AS, &rl) < 0)
570 566 die("setrlimit: %m");
571 567 }
572 568 rl.rlim_cur = rl.rlim_max = 64;
573 569 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
574 570 die("setrlimit: %m");
575 571 if (filter_syscalls && ptrace(PTRACE_TRACEME) < 0)
576 572 die("ptrace(PTRACE_TRACEME): %m");
577 573 execve(args[0], args, (pass_environ ? environ : env));
578 574 die("execve(\"%s\"): %m", args[0]);
579 575 }
580 576
581 577 static void
582 578 usage(void)
583 579 {
584 580 fprintf(stderr, "Invalid arguments!\n");
585 581 printf("\
586 582 Usage: box [<options>] -- <command> <arguments>\n\
587 583 \n\
588 584 Options:\n\
589 585 -a <level>\tSet file access level (0=none, 1=cwd, 2=/etc,/lib,..., 3=whole fs, 9=no checks; needs -f)\n\
590 586 -c <dir>\tChange directory to <dir> first\n\
591 587 -e\t\tPass full environment of parent process\n\
592 588 -f\t\tFilter system calls (-ff=very restricted)\n\
593 589 -i <file>\tRedirect stdin from <file>\n\
594 590 -m <size>\tLimit address space to <size> KB\n\
595 591 -o <file>\tRedirect stdout to <file>\n\
596 592 -t <time>\tStop after <time> seconds\n\
597 593 -T\t\tAllow syscalls for measuring run time\n\
598 594 -v\t\tBe verbose\n\
599 595 -w\t\tMeasure wall clock time instead of run time\n\
600 596 ");
601 597 exit(1);
602 598 }
603 599
604 600 int
605 601 main(int argc, char **argv)
606 602 {
607 603 int c;
608 604 uid_t uid;
609 605
610 606 while ((c = getopt(argc, argv, "a:c:efi:m:o:t:Tvw")) >= 0)
611 607 switch (c)
612 608 {
613 609 case 'a':
614 610 file_access = atol(optarg);
615 611 break;
616 612 case 'c':
617 613 set_cwd = optarg;
618 614 break;
619 615 case 'e':
620 616 pass_environ = 1;
621 617 break;
622 618 case 'f':
623 619 filter_syscalls++;
624 620 break;
625 621 case 'i':
626 622 redir_stdin = optarg;
627 623 break;
628 624 case 'm':
629 625 memory_limit = atol(optarg);
630 626 break;
631 627 case 'o':
632 628 redir_stdout = optarg;
633 629 break;
634 630 case 't':
635 631 timeout = atol(optarg);
636 632 break;
637 633 case 'T':
638 634 allow_times++;
639 635 break;
640 636 case 'v':
641 637 verbose++;
642 638 break;
643 639 case 'w':
644 640 use_wall_clock = 1;
645 641 break;
646 642 default:
647 643 usage();
648 644 }
649 645 if (optind >= argc)
650 646 usage();
651 647
652 648 uid = geteuid();
653 649 if (setreuid(uid, uid) < 0)
654 650 die("setreuid: %m");
655 651 box_pid = fork();
656 652 if (box_pid < 0)
657 653 die("fork: %m");
658 654 if (!box_pid)
659 655 box_inside(argc-optind, argv+optind);
660 656 else
661 657 boxkeeper();
662 658 die("Internal error: fell over edge of the world");
663 659 }
@@ -1,23 +1,41
1 1 #include <stdio.h>
2 2 #include <stdlib.h>
3 3 #include <unistd.h>
4 + #include <sys/time.h>
5 + #include <time.h>
6 + #include <sys/resource.h>
4 7
5 8 int main()
6 9 {
7 10 int a,b;
8 11
9 12 int c=0;
10 13
11 14 scanf("%d %d",&a,&b);
12 15 printf("%d\n",a+b);
13 16
14 - sleep(1);
17 + struct rusage ru;
15 18
16 - c = 0;
17 - while(c<1000000000) {
19 + while(1) {
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 33 c++;
19 34 b+=c;
20 35 }
36 + if(b==10)
37 + printf("hello\n");
38 +
21 39 exit(0);
22 40 }
23 41
You need to be logged in to leave comments. Login now