Skip to content

Commit

Permalink
Limit size of test files
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Jan 13, 2024
1 parent 360f99e commit 357e677
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions dmoj/judgeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'compiler_output_character_limit': 65536, # Number of characters allowed in compile output
'compiled_binary_cache_dir': None, # Location to store cached binaries, defaults to tempdir
'compiled_binary_cache_size': 100, # Maximum number of executables to cache (LRU order)
'test_size_limit': 262144, # Maximum allowable test size, 256mb
'runtime': {},
# Map of executor: fs_config, used to configure
# the filesystem sandbox on a per-machine basis, without having to hack
Expand Down
7 changes: 7 additions & 0 deletions dmoj/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from dmoj import checkers
from dmoj.checkers import Checker
from dmoj.config import ConfigNode, InvalidInitException
from dmoj.error import InternalError
from dmoj.judgeenv import env, get_problem_root
from dmoj.utils.helper_files import compile_with_auxiliary_files, parse_helper_file_error
from dmoj.utils.module import load_module_from_file
Expand Down Expand Up @@ -80,6 +81,9 @@ def __init__(self, problem_id: str, time_limit: float, memory_limit: int, meta:

self.problem_data.archive = self._resolve_archive_files()

if self.config.test_size_limit:
self.problem_data.test_size_limit = self.config.test_size_limit

if not self._resolve_test_cases():
raise InvalidInitException('No test cases? What am I judging?')

Expand Down Expand Up @@ -267,6 +271,7 @@ def __init__(self, problem_root_dir: str, **kwargs):
super().__init__(**kwargs)
self.problem_root_dir = problem_root_dir
self.archive = None
self.test_size_limit = env.test_size_limit

def __missing__(self, key: str) -> bytes:
f: IO[bytes]
Expand All @@ -276,6 +281,8 @@ def __missing__(self, key: str) -> bytes:
except IOError:
if self.archive:
zipinfo = self.archive.getinfo(key)
if zipinfo.file_size > self.test_size_limit * 1024:
raise InternalError('test file is too large: %s' % key)
with self.archive.open(zipinfo) as f:
return f.read()
raise KeyError('file "%s" could not be found in "%s"' % (key, self.problem_root_dir))
Expand Down
Binary file added testsuite/test_size_limit/bomb.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions testsuite/test_size_limit/init.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
archive: bomb.zip
test_size_limit: 65536
test_cases:
- { in: 01.inp, out: 01.out }
1 change: 1 addition & 0 deletions testsuite/test_size_limit/tests/py_ie/ie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pass
6 changes: 6 additions & 0 deletions testsuite/test_size_limit/tests/py_ie/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: PY3
time: 1
memory: 65536
source: ie.py
expect: IE
feedback: 'test file is too large: 01.inp'

0 comments on commit 357e677

Please sign in to comment.