mirror of
https://github.com/QingdaoU/Judger.git
synced 2024-12-28 16:01:41 +00:00
Merge pull request #11 from QingdaoU/limit_stack_size
add stack size limit
This commit is contained in:
commit
48da00080e
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,4 +5,5 @@ build/
|
||||
*.swp
|
||||
*.so
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyc
|
||||
cmake-build-debug/
|
||||
|
@ -24,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),
|
||||
|
@ -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"])])
|
||||
|
@ -39,6 +39,12 @@ void close_file(FILE *fp, ...) {
|
||||
int child_process(FILE *log_fp, struct config *_config) {
|
||||
FILE *input_file = NULL, *output_file = NULL, *error_file = NULL;
|
||||
|
||||
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;
|
||||
|
@ -42,6 +42,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)) {
|
||||
|
@ -43,6 +43,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;
|
||||
|
@ -13,7 +13,8 @@ class IntegrationTest(base.BaseTestCase):
|
||||
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",
|
||||
@ -41,14 +42,16 @@ class IntegrationTest(base.BaseTestCase):
|
||||
|
||||
def test_args_must_be_list(self):
|
||||
with self.assertRaisesRegexp(ValueError, "args must be a list"):
|
||||
_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"):
|
||||
_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",
|
||||
@ -56,14 +59,16 @@ class IntegrationTest(base.BaseTestCase):
|
||||
|
||||
def test_args_item_must_be_string(self):
|
||||
with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
|
||||
_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"):
|
||||
_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",
|
||||
@ -74,7 +79,8 @@ class IntegrationTest(base.BaseTestCase):
|
||||
else:
|
||||
args = [u"哈哈哈"]
|
||||
with self.assertRaisesRegexp(ValueError, "arg item must be a string"):
|
||||
_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=args, env=["a="], log_path="1.log",
|
||||
@ -82,14 +88,16 @@ class IntegrationTest(base.BaseTestCase):
|
||||
|
||||
def test_env_must_be_list(self):
|
||||
with self.assertRaisesRegexp(ValueError, "env must be a list"):
|
||||
_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"):
|
||||
_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",
|
||||
@ -97,14 +105,16 @@ class IntegrationTest(base.BaseTestCase):
|
||||
|
||||
def test_env_item_must_be_string(self):
|
||||
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
|
||||
_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"):
|
||||
_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",
|
||||
@ -115,20 +125,23 @@ class IntegrationTest(base.BaseTestCase):
|
||||
else:
|
||||
env = [u"哈哈哈"]
|
||||
with self.assertRaisesRegexp(ValueError, "env item must be a string"):
|
||||
_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=env, log_path="1.log",
|
||||
seccomp_rule_name="1.so", uid=0, gid=0)
|
||||
|
||||
def test_seccomp_rule_can_be_none(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)
|
||||
|
||||
_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",
|
||||
@ -288,3 +301,17 @@ class IntegrationTest(base.BaseTestCase):
|
||||
config["max_output_size"] = 1000 * 10
|
||||
result = _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"]))
|
@ -13,6 +13,7 @@ class SeccompTest(base.BaseTestCase):
|
||||
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",
|
||||
|
7
tests/test_src/integration/stack.c
Normal file
7
tests/test_src/integration/stack.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int big_stack[1024 * 1024 * 20] = {0};
|
||||
printf("big stack");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user