Merge branch 'newnew' into memory

# Conflicts:
#	runtest.sh
#	tests/Python_and_core/testcase/base.py
#	tests/Python_and_core/testcase/integration/test.py
This commit is contained in:
LiYang 2017-05-16 21:30:57 +08:00
commit e30adb8281
15 changed files with 159 additions and 49 deletions

3
.gitignore vendored
View File

@ -5,4 +5,5 @@ build/
*.swp
*.so
__pycache__
*.pyc
*.pyc
cmake-build-debug/

View File

@ -1,6 +1,8 @@
language: python
dist: trusty
python:
- "2.7"
- "3.5"
sudo: required
before_install:
- sudo apt-get -qq update
@ -8,8 +10,8 @@ before_install:
install:
- mkdir build && cd build
- cmake .. && make && sudo make install
- cd ../bindings/Python && sudo python setup.py install
- cd ../bindings/Python && sudo `which python` setup.py install
script:
- cd ../../tests/Python_and_core && sudo python test.py
- cd ../../tests/Python_and_core && sudo `which python` test.py
notifications:
slack: onlinejudgeteam:BzBz8UFgmS5crpiblof17K2W

View File

@ -2,7 +2,7 @@
CC = gcc
LDFLAGS ?= -pthread -lseccomp -fPIC -shared
LUAVER ?= 5.1
LUAVER ?= 5.3
LUAVERPURE ?= $(subst .,,${LUAVER})
CFLAGS ?= -Wall -Werror -O3 -std=c99 -fPIC -I/usr/include/lua${LUAVER} -DLUA${LUAVERPURE}

View File

@ -1,7 +1,7 @@
#include <unistd.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <python2.7/Python.h>
#include <Python.h>
#include "../../src/runner.h"
@ -11,6 +11,10 @@
return NULL; \
}
#if PY_MAJOR_VERSION >= 3
#define PyString_Check PyUnicode_Check
#define PyString_AsString PyUnicode_AsUTF8
#endif
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
struct config _config;
@ -20,14 +24,16 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
int count = 0, i = 0;
static char *kwargs_list[] = {"max_cpu_time", "max_real_time", "max_memory",
static char *kwargs_list[] = {"max_cpu_time", "max_real_time",
"max_memory", "max_stack",
"max_process_number", "max_output_size",
"exe_path", "input_path", "output_path",
"error_path", "args", "env", "log_path",
"seccomp_rule_name", "uid", "gid", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iililssssOOsOii", kwargs_list,
&(_config.max_cpu_time), &(_config.max_real_time), &(_config.max_memory),
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iillilssssOOsOii", kwargs_list,
&(_config.max_cpu_time), &(_config.max_real_time),
&(_config.max_memory), &(_config.max_stack),
&(_config.max_process_number), &(_config.max_output_size),
&(_config.exe_path), &(_config.input_path), &(_config.output_path),
&(_config.error_path), &args_list, &env_list, &(_config.log_path),
@ -36,7 +42,6 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
}
if (!PyList_Check(args_list)) {
// fixme decref
RaiseValueError("args must be a list");
}
_config.args[count++] = _config.exe_path;
@ -47,21 +52,21 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
break;
}
if (!PyString_Check(next)) {
// Py_DECREF(next);
// free memory before the program exits.
Py_DECREF(next);
Py_DECREF(args_iter);
RaiseValueError("arg item must be a string");
}
_config.args[count] = PyString_AsString(next);
// Py_DECREF(next);
Py_DECREF(next);
count++;
}
_config.args[count] = NULL;
// Py_DECREF(args_list);
// Py_DECREF(args_iter);
Py_DECREF(args_iter);
count = 0;
if (!PyList_Check(env_list)) {
// fixme decref
RaiseValueError("env must be a list");
}
env_iter = PyObject_GetIter(env_list);
@ -71,28 +76,25 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
break;
}
if (!PyString_Check(next)) {
// Py_DECREF(next);
Py_DECREF(next);
Py_DECREF(env_iter);
RaiseValueError("env item must be a string");
}
_config.env[count] = PyString_AsString(next);
// Py_DECREF(next);
count++;
Py_DECREF(next);
}
_config.env[count] = NULL;
// Py_DECREF(env_list);
// Py_DECREF(env_iter);
Py_DECREF(env_iter);
if (PyString_Check(rule_name)) {
_config.seccomp_rule_name = PyString_AsString(rule_name);
// Py_DECREF(rule_path);
}
else {
if (rule_name == Py_None) {
// Py_DECREF(rule_path);
_config.seccomp_rule_name = NULL;
}
else {
// fixme decref
RaiseValueError("seccomp_rule_name must be string or None");
}
}
@ -101,7 +103,6 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
int (*judger_run)(struct config *, struct result *);
if (!handler) {
// fixme decref
RaiseValueError("dlopen error")
}
judger_run = dlsym(handler, "run");
@ -119,13 +120,36 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
static PyMethodDef judger_methods[] = {
#if PY_MAJOR_VERSION >= 3
{"run", (PyCFunction) judger_run, METH_VARARGS | METH_KEYWORDS, NULL},
#else
{"run", (PyCFunction) judger_run, METH_KEYWORDS, NULL},
#endif
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC init_judger(void) {
static PyObject* moduleinit(void) {
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef judger_def = {
PyModuleDef_HEAD_INIT,
"_judger", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
judger_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&judger_def);
#else
PyObject *module = Py_InitModule3("_judger", judger_methods, NULL);
#endif
PyModule_AddIntConstant(module, "VERSION", VERSION);
PyModule_AddIntConstant(module, "UNLIMITED", UNLIMITED);
PyModule_AddIntConstant(module, "RESULT_WRONG_ANSWER", WRONG_ANSWER);
@ -147,4 +171,18 @@ PyMODINIT_FUNC init_judger(void) {
PyModule_AddIntConstant(module, "ERROR_SETUID_FAILED", SETUID_FAILED);
PyModule_AddIntConstant(module, "ERROR_EXECVE_FAILED", EXECVE_FAILED);
PyModule_AddIntConstant(module, "ERROR_SPJ_ERROR", SPJ_ERROR);
return module;
}
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit__judger(void)
{
return moduleinit();
}
#else
PyMODINIT_FUNC init_judger(void)
{
moduleinit();
}
#endif

View File

@ -3,5 +3,5 @@ import platform
from distutils.core import setup, Extension
setup(name='_judger',
version='2.0',
version='2.1',
ext_modules=[Extension('_judger', sources=['_judger.c'], libraries=["dl"])])

View File

@ -1,5 +1,5 @@
rm -rf build && mkdir build && cd build && cmake ..
make || exit 1
make install
apt-get update
apt-get install cmake liblua5.3-dev
rm -rf build && mkdir build && cd build && cmake .. && make && make install
cd ../bindings/Python && rm -rf build && python setup.py install || exit 1
cd ../../tests/Python_and_core && python test.py

View File

@ -1,4 +1,4 @@
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#define _POSIX_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
@ -40,6 +40,12 @@ int child_process(FILE *log_fp, struct config *_config, int pipe_fd, long *pipe_
FILE *input_file = NULL, *output_file = NULL, *error_file = NULL;
struct rusage child_resource_usage;
struct rlimit max_stack;
max_stack.rlim_cur = max_stack.rlim_max = (rlim_t) (_config->max_stack);
if (setrlimit(RLIMIT_STACK, &max_stack) != 0) {
CHILD_ERROR_EXIT(SETRLIMIT_FAILED);
}
// set memory limit
if (_config->max_memory != UNLIMITED) {
struct rlimit max_memory;

View File

@ -51,6 +51,7 @@ void run(struct config *_config, struct result *_result) {
// check args
if ((_config->max_cpu_time < 1 && _config->max_cpu_time != UNLIMITED) ||
(_config->max_real_time < 1 && _config->max_real_time != UNLIMITED) ||
(_config->max_stack < 1) ||
(_config->max_memory < 1 && _config->max_memory != UNLIMITED) ||
(_config->max_process_number < 1 && _config->max_process_number != UNLIMITED) ||
(_config->max_output_size < 1 && _config->max_output_size != UNLIMITED)) {

View File

@ -45,6 +45,7 @@ struct config {
int max_cpu_time;
int max_real_time;
long max_memory;
long max_stack;
int max_process_number;
long max_output_size;
char *exe_path;

View File

@ -1,4 +1,6 @@
# coding=utf-8
from __future__ import print_function
import sys
import _judger
from unittest import TestCase, main
@ -6,5 +8,6 @@ from testcase.integration.test import IntegrationTest
from testcase.seccomp.test import SeccompTest
ver = _judger.VERSION
print "Judger version %d.%d.%d" % ((ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff)
main()
print("Judger version %d.%d.%d" % ((ver >> 16) & 0xff, (ver >> 8) & 0xff, ver & 0xff))
print(sys.version)
main()

9
tests/Python_and_core/test.sh Executable file
View File

@ -0,0 +1,9 @@
cd ../../bindings/Python
rm -r build
python setup.py install
rm -r build
python3 setup.py install
cd ../../tests/Python_and_core
python test.py
slepp 3
python3 test.py

View File

@ -1,5 +1,7 @@
# coding=utf-8
from __future__ import print_function
import os
import random
import shutil
import _judger
from unittest import TestCase
@ -22,7 +24,7 @@ class BaseTestCase(TestCase):
return workspace
def rand_str(self):
return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16)))
return "".join([random.choice("123456789abcdef") for _ in range(12)])
def _compile_c(self, src_name, extra_flags=None):
path = os.path.dirname(os.path.abspath(__file__))
@ -57,4 +59,4 @@ class BaseTestCase(TestCase):
return f.read()
def judger_run(self, **config):
return _judger.run(**config)
return _judger.run(**config)

View File

@ -1,4 +1,6 @@
# coding=utf-8
from __future__ import print_function
import sys
import _judger
import signal
import os
@ -8,10 +10,11 @@ from .. import base
class IntegrationTest(base.BaseTestCase):
def setUp(self):
print "Running", self._testMethodName
print("Running", self._testMethodName)
self.config = {"max_cpu_time": 1000,
"max_real_time": 3000,
"max_memory": 1024 * 1024 * 128,
"max_memory": 128 * 1024 * 1024,
"max_stack": 32 * 1024 * 1024,
"max_process_number": 10,
"max_output_size": 1024 * 1024,
"exe_path": "/bin/ls",
@ -40,13 +43,16 @@ class IntegrationTest(base.BaseTestCase):
def test_args_must_be_list(self):
with self.assertRaisesRegexp(ValueError, "args must be a list"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args="12344", env=["a=b"], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
with self.assertRaisesRegexp(ValueError, "args must be a list"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args={"k": "v"}, env=["a=b"], log_path="1.log",
@ -54,36 +60,45 @@ class IntegrationTest(base.BaseTestCase):
def test_args_item_must_be_string(self):
with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234", 1234], env=["a=b"], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234", None], env=["a=b"], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
if sys.version_info >= (3, 5):
args = ["哈哈哈".encode("utf-8")]
else:
args = [u"哈哈哈"]
with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=[u"哈哈哈"], env=["a=b"], log_path="1.log",
args=args, env=["a="], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
def test_env_must_be_list(self):
with self.assertRaisesRegexp(ValueError, "env must be a list"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env="1234", log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
with self.assertRaisesRegexp(ValueError, "env must be a list"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env={"k": "v"}, log_path="1.log",
@ -91,34 +106,43 @@ class IntegrationTest(base.BaseTestCase):
def test_env_item_must_be_string(self):
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env=["1234", 1234], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env=["a=b", None], log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
if sys.version_info >= (3, 5):
env = ["哈哈哈".encode("utf-8")]
else:
env = [u"哈哈哈"]
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="1.out",
input_path="1.in", output_path="1.out", error_path="1.out",
args=["1234"], env=[u"哈哈哈"], log_path="1.log",
args=["1234"], env=env, log_path="1.log",
seccomp_rule_name="1.so", uid=0, gid=0)
def test_seccomp_rule_can_be_none(self):
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="/bin/ls",
input_path="/dev/null", output_path="/dev/null", error_path="/dev/null",
args=["12344"], env=["a=b"], log_path="/dev/null",
seccomp_rule_name="c_cpp", uid=0, gid=0)
self.judger_run(max_cpu_time=1000, max_real_time=2000, max_memory=1000000000,
_judger.run(max_cpu_time=1000, max_real_time=2000,
max_memory=1024 * 1024 * 128, max_stack=32 * 1024 * 1024,
max_process_number=200, max_output_size=10000, exe_path="/bin/ls",
input_path="/dev/null", output_path="/dev/null", error_path="/dev/null",
args=["12344"], env=["a=b"], log_path="/dev/null",
@ -281,3 +305,17 @@ class IntegrationTest(base.BaseTestCase):
config["max_output_size"] = 1000 * 10
result = self.judger_run(**config)
self.assertEqual(result["exit_code"], 2)
def test_stack_size(self):
config = self.config
config["max_memory"] = 256 * 1024 * 1024
config["exe_path"] = self._compile_c("stack.c")
config["output_path"] = config["error_path"] = self.output_path()
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_RUNTIME_ERROR)
config["max_stack"] = 128 * 1024 * 1024
result = _judger.run(**config)
self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
self.assertEqual("big stack", self.output_content(config["output_path"]))

View File

@ -1,4 +1,5 @@
# coding=utf-8
from __future__ import print_function
import _judger
import signal
import os
@ -8,10 +9,11 @@ from .. import base
class SeccompTest(base.BaseTestCase):
def setUp(self):
print "Running", self._testMethodName
print("Running", self._testMethodName)
self.config = {"max_cpu_time": 1000,
"max_real_time": 3000,
"max_memory": 1024 * 1024 * 128,
"max_stack": 32 * 1024 * 1024,
"max_process_number": 10,
"max_output_size": 1024 * 1024,
"exe_path": "/bin/ls",

View File

@ -0,0 +1,7 @@
#include <stdio.h>
int main() {
int big_stack[1024 * 1024 * 20] = {0};
printf("big stack");
return 0;
}