Exemplo n.º 1
0
 def get_fs(self):
     name = self.get_executor_name()
     fs = BASE_FILESYSTEM + self.fs + env.get('extra_fs', {}).get(
         name, [])
     fs += [
         re.escape(self._file('setbufsize.so')) + '$',
         re.escape(self._dir) + '$'
     ]
     return fs
Exemplo n.º 2
0
    def _load_extra_fs(self) -> List[FilesystemAccessRule]:
        name = self.get_executor_name()
        extra_fs_config = env.get('extra_fs', {}).get(name, [])
        extra_fs = []
        constructors: Dict[str, Type[FilesystemAccessRule]] = dict(
            exact_file=ExactFile,
            exact_dir=ExactDir,
            recursive_dir=RecursiveDir)
        for rules in extra_fs_config:
            for type, path in rules.iteritems():
                constructor = constructors.get(type)
                assert constructor, f"Can't load rule for extra path with rule type {type}"
                extra_fs.append(constructor(path))

        return extra_fs
Exemplo n.º 3
0
def load_executors():
    __executors = set(
        i.group(1)
        for i in map(_reexecutor.match, os.listdir(os.path.dirname(__file__)))
        if i is not None)

    if only_executors:
        __executors &= only_executors
    if exclude_executors:
        __executors -= exclude_executors
    __executors = sorted(__executors)

    import traceback

    def __load_module(executor):
        try:
            module = import_module('%s.%s' % (__name__, executor))
        except ImportError as e:
            if e.message not in ('No module named _cptbox',
                                 'No module named msvcrt',
                                 'No module named _wbox',
                                 'No module named termios'):
                traceback.print_exc()
            return None
        return module

    print 'Self-testing executors...'

    for name in __executors:
        if name in _unsupported_executors:
            continue
        executor = __load_module(name)
        if executor is None:
            continue
        if hasattr(executor, 'initialize') and not executor.initialize(
                sandbox=env.get('selftest_sandboxing', True)):
            continue
        if hasattr(executor, 'aliases'):
            for alias in executor.aliases():
                executors[alias] = executor
        else:
            executors[name] = executor

    print
Exemplo n.º 4
0
def load_executors():
    __executors = set(i.group(1) for i in map(_reexecutor.match,
                                              os.listdir(os.path.dirname(__file__)))
                      if i is not None)

    if only_executors:
        __executors &= only_executors
    if exclude_executors:
        __executors -= exclude_executors
    __executors = sorted(__executors)

    import traceback

    def __load_module(executor):
        try:
            module = import_module('%s.%s' % (__name__, executor))
        except ImportError as e:
            if e.message not in ('No module named _cptbox',
                                 'No module named msvcrt',
                                 'No module named _wbox',
                                 'No module named termios'):
                traceback.print_exc()
            return None
        return module

    print 'Self-testing executors...'

    for name in __executors:
        if name in _unsupported_executors:
            continue
        executor = __load_module(name)
        if executor is None:
            continue
        if hasattr(executor, 'initialize') and not executor.initialize(sandbox=env.get('selftest_sandboxing', True)):
            continue
        if hasattr(executor, 'aliases'):
            for alias in executor.aliases():
                executors[alias] = executor
        else:
            executors[name] = executor

    print
Exemplo n.º 5
0
def load_executors():
    to_load = get_available()

    print 'Self-testing executors...'

    for name in to_load:
        executor = load_executor(name)

        if executor is None or not hasattr(executor, 'Executor'):
            continue

        cls = executor.Executor
        if hasattr(cls, 'initialize') and not cls.initialize(sandbox=env.get('selftest_sandboxing', True)):
            continue

        if hasattr(executor, 'aliases'):
            for alias in executor.aliases():
                if alias not in _unsupported_executors:
                    executors[alias] = executor
        else:
            executors[name] = executor

    print
Exemplo n.º 6
0
 def get_fs(self):
     name = self.get_executor_name()
     fs = BASE_FILESYSTEM + self.fs + env.get('extra_fs', {}).get(name, []) + [re.escape(self._dir)]
     return fs
Exemplo n.º 7
0
 def get_fs(self):
     name = self.get_executor_name()
     return BASE_FILESYSTEM + self.fs + env.get('extra_fs', {}).get(
         name, [])
Exemplo n.º 8
0
 def __init__(self):
     self._dir = tempfile.mkdtemp(dir=env.get('tempdir'))
Exemplo n.º 9
0
class BaseExecutor(ResourceProxy):
    ext = None
    network_block = True
    address_grace = 65536
    nproc = 0
    fs = ['.*\.so']
    syscalls = []
    command = None
    name = '(unknown)'
    inject32 = env.get('inject32', default_inject32)
    inject64 = env.get('inject64', default_inject64)
    inject_func = env.get('inject_func', default_inject_func)
    test_program = ''
    test_name = 'self_test'
    test_time = 10
    test_memory = 65536

    def __init__(self, problem_id, source_code):
        super(BaseExecutor, self).__init__()
        self.problem = problem_id
        self.source = source_code

    def get_fs(self):
        return self.fs

    def get_allowed_syscalls(self):
        return self.syscalls

    def get_security(self):
        if CHROOTSecurity is None:
            raise NotImplementedError('No security manager on Windows')
        sec = CHROOTSecurity(self.get_fs())
        for name in self.get_allowed_syscalls():
            sec[getattr(syscalls, 'sys_' + name)] = ALLOW
        return sec

    def get_executable(self):
        return None

    def get_cmdline(self):
        raise NotImplementedError()

    def get_env(self):
        if WBoxPopen is not None:
            return None
        return {'LANG': 'C'}

    def get_network_block(self):
        assert WBoxPopen is not None
        return self.network_block

    def get_address_grace(self):
        assert SecurePopen is not None
        return self.address_grace

    def get_nproc(self):
        return self.nproc

    def get_inject32(self):
        file = self._file('dmsec32.dll')
        copyfile(self.inject32, file)
        return file

    def get_inject64(self):
        file = self._file('dmsec64.dll')
        copyfile(self.inject64, file)
        return file

    def get_inject_func(self):
        return self.inject_func

    if SecurePopen is None:
        def launch(self, *args, **kwargs):
            return WBoxPopen(self.get_cmdline() + list(args),
                             time=kwargs.get('time'), memory=kwargs.get('memory'),
                             cwd=self._dir, executable=self.get_executable(),
                             network_block=True, env=self.get_env(),
                             nproc=self.get_nproc() + 1,
                             inject32=self.get_inject32(),
                             inject64=self.get_inject64(),
                             inject_func=self.get_inject_func())
    else:
        def launch(self, *args, **kwargs):
            return SecurePopen(self.get_cmdline() + list(args), executable=self.get_executable(),
                               security=self.get_security(), address_grace=self.get_address_grace(),
                               time=kwargs.get('time'), memory=kwargs.get('memory'),
                               stderr=(PIPE if kwargs.get('pipe_stderr', False) else None),
                               env=self.get_env(), cwd=self._dir, nproc=self.get_nproc(),
                               unbuffered=kwargs.get('unbuffered', False))

    def launch_unsafe(self, *args, **kwargs):
        return Popen(self.get_cmdline() + list(args),
                     env=self.get_env(), executable=self.get_executable(),
                     cwd=self._dir, **kwargs)

    @classmethod
    def get_command(cls):
        return cls.command

    @classmethod
    def initialize(cls, sandbox=True):
        if cls.get_command() is None:
            return False
        if not os.path.isfile(cls.get_command()):
            return False
        return cls.run_self_test(sandbox)

    @classmethod
    def run_self_test(cls, sandbox=True):
        if not cls.test_program:
            return True

        print ansi_style("%-39s%s" % ('Self-testing #ansi[%s](|underline):' % cls.name, '')),
        try:
            executor = cls(cls.test_name, cls.test_program)
            proc = executor.launch(time=cls.test_time, memory=cls.test_memory) if sandbox else executor.launch_unsafe()
            test_message = 'echo: Hello, World!'
            stdout, stderr = proc.communicate(test_message + '\n')
            res = stdout.strip() == test_message and not stderr
            print ansi_style(['#ansi[Failed](red|bold)', '#ansi[Success](green|bold)'][res])
            if stderr:
                print>> sys.stderr, stderr
            return res
        except Exception:
            print 'Failed'
            import traceback
            traceback.print_exc()
            return False
Exemplo n.º 10
0
 def get_fs(self):
     name = self.__class__.__module__.split('.')[-1]
     return BASE_FILESYSTEM + self.fs + env.get('extra_fs', {}).get(name, [])