8 Replies - 1666 Views - Last Post: 16 March 2013 - 06:15 AM

#1 vojka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-April 12

Question about C++ evaluator

Posted 13 February 2013 - 10:17 AM

For a project at my high school, I am making online C++ evaluator. Frontend will be PHP, where users (students) can register, login, submit their solution to some problems. Everything works fine, except the evaluation part. I compile their uploaded cpp file with :

compiler\bin\g++.exe -Wall test.cpp -o text.exe


Then I try to execute it with

test.exe < in.txt > out.txt


so it outputs STDIN and STDOUT to textfiles. Then, I just compare those files to records from database about test examples.

My problem is : how do I limit execution time and memory for executing their code ?

P.S. If moderators think that topic is in wrong subforum, please transfer it into correct one. I thought that this one was the most appropriate.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about C++ evaluator

#2 vojka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-April 12

Re: Question about C++ evaluator

Posted 13 February 2013 - 10:23 AM

P.P.S It will run on Windows Server 2003 machine.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Question about C++ evaluator

Posted 13 February 2013 - 01:36 PM

Are you getting the process id back from your call to the system to run the program? If so, then you can pass the same process id to a kill command to kill the program if its been running too long or using up too much memory.

It seems like a great way to get a trojan into your machine if you aren't running the program within a sandbox environment. What if the test.cpp code that is submitted to you contains code to reformat your hard drive?
Was This Post Helpful? 0
  • +
  • -

#4 vojka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-April 12

Re: Question about C++ evaluator

Posted 14 February 2013 - 07:12 AM

View PostSkydiver, on 13 February 2013 - 01:36 PM, said:

Are you getting the process id back from your call to the system to run the program? If so, then you can pass the same process id to a kill command to kill the program if its been running too long or using up too much memory.


I think I could get PID, but how to check how much memory is it using ?

View PostSkydiver, on 13 February 2013 - 01:36 PM, said:

It seems like a great way to get a trojan into your machine if you aren't running the program within a sandbox environment. What if the test.cpp code that is submitted to you contains code to reformat your hard drive?


I will write a script that will forbid test.cpp to have #include <windows.h>, #include <fstream>, system calls...
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Question about C++ evaluator

Posted 14 February 2013 - 07:21 AM

You can query the OS how much memory a process is using by making the appropriate API calls. See MSDN: http://msdn.microsof...1(v=vs.85).aspx

As for a script checking against a blacklist, you are just going to get into an arms race. A person need not include windows.h. They can just declare the appropriate functions themselves and do the needed calls to load the OS DLLs to make the calls to your blacklisted APIs.

This post has been edited by Skydiver: 14 February 2013 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 vojka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-April 12

Re: Question about C++ evaluator

Posted 20 February 2013 - 08:33 AM

View PostSkydiver, on 14 February 2013 - 07:21 AM, said:

You can query the OS how much memory a process is using by making the appropriate API calls. See MSDN: http://msdn.microsof...1(v=vs.85).aspx

As for a script checking against a blacklist, you are just going to get into an arms race. A person need not include windows.h. They can just declare the appropriate functions themselves and do the needed calls to load the OS DLLs to make the calls to your blacklisted APIs.


OK, I have been playing with a WinAPI for a while, and tried GetProcessMemoryInfo function, and it outputs bunch of data in hex format:

Process ID: 3364
	PageFaultCount: 0x00000137
	PeakWorkingSetSize: 0x0013A000
	WorkingSetSize: 0x0013A000
	QuotaPeakPagedPoolUsage: 0x00003BA4
	QuotaPagedPoolUsage: 0x00003BA4
	QuotaPeakNonPagedPoolUsage: 0x000004B0
	QuotaNonPagedPoolUsage: 0x000004B0
	PagefileUsage: 0x00053000
	PeakPagefileUsage: 0x00053000


What of this can I convert to kilobytes, megabytes and compare it to limit ?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Question about C++ evaluator

Posted 20 February 2013 - 10:25 AM

On top of playing with the API, you should also read the documentation. It tells you the units of measurement used for the values returned: http://msdn.microsof...7(v=vs.85).aspx
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5764
  • View blog
  • Posts: 12,580
  • Joined: 16-October 07

Re: Question about C++ evaluator

Posted 20 February 2013 - 11:00 AM

First thing that springs to mind is a virtual machine. It seems a little extreme, but it will isolate the process, no doubt.

The windowy headers stuff could be automatically restricted depending on how you set up your compile environment. Simply don't include the headers ( and libs ) you don't want them to have. As noted, in Windows, there's only so far you can take this.

Strange to see g++ there. Cygwin, or just some CodeBlocks cruft on your Windows box?

On a Linux box, and potentially cygwin, you could chroot the thing. In a chroot jail I believe you can set a ulimit which will prevent memory getting gobbled up over s certain point.
Was This Post Helpful? 0
  • +
  • -

#9 vojka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-April 12

Re: Question about C++ evaluator

Posted 16 March 2013 - 06:15 AM

I gave up windows and we installed linux on server. I found kind of solution for this problem : The Moe Contest Environment (http://www.ucw.cz/moe/) has a sandbox written for linux which i will use. Thank you all for help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1