Skip to content
Browse files

Make code checker work for C and C++

  • Loading branch information...
1 parent 8198480 commit 938b272d81602d013e1af81067a122b41001113b @surenm committed
Showing with 43 additions and 200 deletions.
  1. +0 −110 install.py
  2. +25 −52 src/checker/cc_backend/Checker.py
  3. +10 −30 src/checker/cc_backend/evaluator/eval.py
  4. +8 −8 src/checker/cc_backend/se/secexec.c
View
110 install.py
@@ -1,110 +0,0 @@
-import os
-import sys
-import shutil
-from distutils.core import setup
-from django.core.management import execute_manager
-
-
-# TODO:One step installation file
-
-prefix = "/usr/local/"
-backend_conf = os.path.join(prefix, 'etc/checker/')
-frontend_conf = os.path.join(prefix, 'etc/apache/')
-media_dir = os.path.join(prefix, 'share/checker/')
-secexec = os.path.join(prefix, 'bin/secexec')
-
-# check if django exists first
-try :
- import django
-except ImportError :
- sys.stderr.write( '''
----------------------------------------------------------------------------
-Could not find Django, please install Django first.\n
-sys.stderr.write( "Please install Django from www.djagoproject.com.\n
----------------------------------------------------------------------------
-''' )
- exit(1)
-# check if python mysql exists
-try :
- import MySQLdb
-except ImportError :
- sys.stderr.write( '''
----------------------------------------------------------------------------
-python-mysqldb is not installed. Please install from apt/yum\n
----------------------------------------------------------------------------
-''' )
- exit(1)
-
-# create etc/checker conf directory if not exists already
-if not os.path.exists( backend_conf ):
- os.mkdir( backend_conf )
-
-# create etc/apache conf directory if not exists already
-if not os.path.exists( frontend_conf ):
- os.mkdir( frontend_conf )
-
-# create media directory
-if not os.path.exists( media_dir ) :
- os.mkdir( media_dir )
-
-# Copy the settings.py from sample file and ask to populate the mysql data
-shutil.copy( os.path.join( os.getcwd(), 'conf/settings.conf' ),
- os.path.join( backend_conf, 'settings.conf' ) )
-
-# copy all python modules
-ret_code = os.system( "python setup.py install" )
-if not ret_code == 0 :
- sys.exit(ret_code)
-
-# copy the codechecker.conf to @prefix/etc/checker
-shutil.copy( os.path.join( os.getcwd(), 'conf/codechecker.conf'), os.path.join( backend_conf, 'codechecker.conf' ) )
-
-# copy the apache conf file
-shutil.copy( os.path.join( os.getcwd(), 'conf/django.conf'), os.path.join( frontend_conf, 'django.conf') )
-
-# set permissions
-os.system('sudo chmod -R 750 %s' % backend_conf)
-os.system('sudo chmod -R 750 %s' % frontend_conf)
-
-
-# copy the media folder to /usr/local/share/checker/media
-# and remove it if already present
-if os.path.exists( media_dir + 'media/'):
- shutil.rmtree( media_dir + 'media/' )
-
-if os.path.exists( media_dir + 'templates/'):
- shutil.rmtree( media_dir + 'templates/' )
-
-if os.path.exists( '/var/www/media/' ):
- shutil.rmtree('/var/www/media/')
-
-shutil.copytree(os.getcwd() + '/media', media_dir + 'media/' )
-
-# Copy templates to /usr/local/share/checker
-shutil.copytree(os.getcwd() + '/src/checker/cc_frontend/templates', media_dir + 'templates/')
-
-# this is a temporary copy to /www/media
-shutil.copytree(os.getcwd() + '/media', '/var/www/media/' )
-
-# copy secexec to /usr/local/bin
-os.system('sudo bash ./build_secexec.sh')
-
-# mount bind stuff for jail
-dirs = [ 'bin', 'etc', 'lib', 'opt', 'proc', 'sbin', 'usr', 'var']
-for dir in dirs:
- os.system('sudo umount /jail/%s' % dir)
- os.system('sudo mkdir -p /jail/%s' % dir)
- os.system('sudo mount --bind /%s /jail/%s' % (dir, dir) )
-
-if not os.path.exists('/jail/runs/autobot/submissions'):
- os.system('sudo mkdir -p /jail/runs/autobot/submissions')
-
-# Now to run syncdb - settings should already be in place
-# It would not have come to this level else
-# currently running manage.py syndb, how to call it here ?
-
-# TODO: call manage.py syncdb from here
-# TODO: add django.conf to apache's include directory
-# TODO: copy media to /usr/share/checker/media - DOcRoot has already added
-# TODO: Default populate
-# TODO: Run a Unit test
View
77 src/checker/cc_backend/Checker.py
@@ -15,7 +15,7 @@
from Config import Config
logger = logging.getLogger('main')
-handler = logging.FileHandler('/tmp/run.log')
+handler = logging.StreamHandler()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
@@ -54,8 +54,8 @@ def compile_submission(compiler, store, submission):
def main():
+ print "Starting Checker"
config = Config("/usr/local/etc/checker/codechecker.conf")
- print "Starting checker"
store = Default(config)
compiler = Compiler(config)
evaluator = Evaluate(config)
@@ -63,22 +63,30 @@ def main():
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind('tcp://*:5555')
+ print "Bound to ZMQ socket"
while True:
try:
+ response = {}
request_object = socket.recv()
+ print "Request is %s" % request_object
request = json.loads(request_object)
new_submission = store.set_submission(request)
submission = store.get_submission_by_id(new_submission.id)
+
compiler_result = compile_submission(compiler, store, submission)
+ response['compiler_output'] = compiler_result['compiler_output']
if compiler_result == None:
- socket.send("compile result none")
+ response['compile_status'] = False
+
+ print "Compile failed - %s" % str(response)
+ socket.send(json.dumps(response))
continue
- print compiler_result
- # Evaluate the queued submission. Somewhere in the following
- # loop it is also possible that the program fails - need to
- # set the status to runtime error status.
+
+ # if we have come here, then we have passed compilation
+ response['compile_status'] = True
+ response['outputs'] = []
for testset in store.get_all_testsets(submission["problem"]):
testset_info = testset
testset_info["inputs"] = []
@@ -87,55 +95,20 @@ def main():
testset_info["inputs"].append(testcase["input"])
testset_info["reference_outputs"].append(testcase["reference_output"])
- result_set = evaluator.eval_submission(submission, testset_info, compiler_result["run_command"])
+ results = evaluator.eval_submission(submission, testset_info, compiler_result["run_command"])
- socket.send("Hello world")
-
- except Exception as e:
- print e, type(e)
- socket.send("Hello world")
+ for result in results:
+ response['outputs'].append((result['input'], result['output']))
+ socket.send(json.dumps(response))
-def CheckerInfiniteLoop():
- config = Config("/usr/local/etc/checker/codechecker.conf")
- store = Default(config)
- compiler = Compiler(config)
- evaluator = Evaluate(config)
- score = Score()
-
- # Each iteration of the loop below evalutes a submission.
- for submission in wait_for_submission(store):
- logger.info("Starting to process submission %s" % str(submission['id']))
- # compile the submission
-
- compiler_result = compile_submission(compiler, store, submission)
- if compiler_result == None:
- continue
-
- # Evaluate the queued submission. Somewhere in the following
- # loop it is also possible that the program fails - need to
- # set the status to runtime error status.
- test_group_scores = []
- for testset in store.get_all_testsets(submission["problem"]):
- testset_info = testset
- testset_info["inputs"] = []
- testset_info["reference_outputs"] = []
- for testcase in store.get_all_testcases(testset["id"]):
- testset_info["inputs"].append(testcase["input"])
- testset_info["reference_outputs"].append(testcase["reference_output"])
-
- result_set = evaluator.eval_submission(submission, testset_info, compiler_result["run_command"])
-
- #TODO: implement the below functions properly.
- #test_grp_score = score.score_group(prob_id, result_set)
- #store.set_test_group_score(test_grp_score, problem_id=prob_id, test_group_id=test_grp["id"], submission_id=submission["id"])
- #test_group_scores.append(test_grp_score)
+ except Exception as e:
+ print e, type(e)
+ response['error_type'] = type(e)
+ response['error'] = e
+ socket.send("Error processing submission.")
- # compute and set the overall score.
- #final_score = score.overall(test_group_scores, problem_id=prob_id)
- #store.set_submission_score(final_score, submission_id=submission["id"])
- #store.set_submission_run_status("PASS", submission_id=submission["id"])
- logger.info("Completed submission %s" % str(submission["id"]))
+ logger.info("Completed processing %s" % str(submission["id"]))
if __name__ == '__main__':
main()
View
40 src/checker/cc_backend/evaluator/eval.py
@@ -60,21 +60,14 @@ def eval_submission(self, submission, test_set, run_template):
abs_input_file = abs_index_file_base + '.in'
abs_out_file = abs_index_file_base + '.out'
abs_error_file = abs_index_file_base + '.err'
-
- input_file = runs_index_file_base + '.in'
- out_file = runs_index_file_base + '.out'
- error_file = runs_index_file_base + '.err'
os.system('touch %s' % abs_out_file)
os.system('touch %s' % abs_error_file)
+ os.system('chown -R %d %s' % (effective_user_id, os.path.join(self.config.abs_path, str(submission["id"]))))
- os.system('chown %d %s' % (effective_user_id, abs_input_file))
- os.system('chown %d %s' % (effective_user_id, abs_out_file))
- os.system('chown %d %s' % (effective_user_id, abs_error_file))
-
- os.chmod(abs_input_file, stat.S_IRUSR)
- os.chmod(abs_out_file, stat.S_IRUSR | stat.S_IWUSR)
- os.chmod(abs_error_file, stat.S_IRUSR | stat.S_IWUSR)
+ os.chmod(abs_input_file, stat.S_IRUSR | stat.S_IROTH)
+ os.chmod(abs_out_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IWOTH | stat.S_IROTH)
+ os.chmod(abs_error_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IWOTH | stat.S_IROTH)
executable_file = os.path.join(self.config.abs_path, str(submission['id']), 'solution.exe')
executable_file = self._get_executable(submission)
@@ -83,9 +76,9 @@ def eval_submission(self, submission, test_set, run_template):
reference_file = os.path.join(self.reference_outputs_base, str(submission["id"]), str(index) + '.ref')
- args = ["--infile=%s" % input_file,
- "--outfile=%s" % out_file,
- "--errfile=%s" % error_file,
+ args = ["--infile=%s" % abs_input_file,
+ "--outfile=%s" % abs_out_file,
+ "--errfile=%s" % abs_error_file,
"--memlimit=%d" % mlimit,
"--timelimit=%d" % tlimit,
"--maxfilesz=%d" % max_file_size,
@@ -95,19 +88,6 @@ def eval_submission(self, submission, test_set, run_template):
ret_code = secexec.secure_spawn(args)
- #End of executing the submission_exec
- if test_set["is_cust_scored"] == True:
- # use the custom executable to run
- pass
- else:
- check = subprocess.Popen('diff -Bb %s %s' % (abs_out_file, reference_file), shell=True, stdout=subprocess.PIPE)
- diff_op = check.communicate()[0]
- if diff_op == '':
- #testcase passed
- pass
- else:
- #testcase failed
- pass
- pass
- pass
- pass
+ response = []
+ return response
+
View
16 src/checker/cc_backend/se/secexec.c
@@ -26,24 +26,24 @@ int secure_spawn(ExecArgs ea) {
p = fork();
if (!p) {
-
+ freopen(ea.infile, "r", stdin);
+ freopen(ea.outfile, "w", stdout);
+ freopen(ea.errfile, "w", stderr);
+
chdir(ea.jailroot);
- chroot(ea.jailroot);
+ ret = chroot(ea.jailroot);
#ifdef JAIL
FILE *fp = fopen("/chstuff", "a");
if(fp) {
+ fprintf(fp, "Jail root: %s\n", ea.jailroot);
fprintf(fp, "ret = %d errno = %d\n", ret, errno);
fclose(fp);
}
#endif /* JAIL */
//drop privileges
-
- //setuid(ea.euid);
- freopen(ea.infile, "r", stdin);
- freopen(ea.outfile, "w", stdout);
- freopen(ea.errfile, "w", stderr);
-
+ setuid(ea.euid);
+
struct rlimit lim ;
lim.rlim_cur = lim.rlim_max = 4;
ret = setrlimit(RLIMIT_NPROC, &lim);

0 comments on commit 938b272

Please sign in to comment.
Something went wrong with that request. Please try again.