1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# coding=utf-8
import ast
from fcntl import fcntl, F_GETFL, F_SETFL
import io
import multiprocessing
import os
import queue
import signal
import subprocess
import sys
import threading
import time
import server
from db.models import Problem
__all__ = ['PythonSession']
class PythonSession(server.LanguageSession):
"""Abstracts a Python session.
Only public methods are available to the outside world due to the use of multiprocessing managers.
Therefore prefix any private methods with an underscore (_).
No properties are accessible; use getters and setters instead.
Values are passed by value instead of by reference (deep copy!).
"""
def __init__(self, output_cb=None):
self._access_lock = threading.Lock()
self._sent_hints = []
self._control = queue.Queue()
self._interpreter = threading.Thread(target=_interpreter,
kwargs={'control': self._control, 'callback': output_cb})
self._interpreter.start()
def run(self, code=None, inputs=None, timeout=1.0):
# Launch processes.
futures = []
for expr, stdin in inputs:
conn_parent, conn_child = multiprocessing.Pipe()
p = multiprocessing.Process(target=_run_exec, args=(conn_child, code, expr, stdin))
p.start()
futures.append((p, conn_parent))
# Wait for results.
results = []
start = time.monotonic()
for p, conn in futures:
now = time.monotonic()
real_timeout = max(0, timeout - (now - start))
if conn.poll(real_timeout):
results.append(conn.recv())
else:
results.append((None, None, None, 'timed out'))
p.terminate()
return results
def exec(self, program):
self._control.put_nowait(('exec', program))
def push(self, stdin):
self._control.put_nowait(('push', stdin))
def stop(self):
self._control.put_nowait(('stop', None))
def destroy(self):
self._control.put_nowait(('done', None))
def __del__(self):
self.destroy()
def hint(self, sid, problem_id, program):
language, problem_group, problem = Problem.get_identifier(problem_id)
language_module = server.problems.load_language(language, 'common')
problem_module = server.problems.load_problem(language, problem_group, problem, 'common')
hints = []
# check if the program is already correct
passed, _ = problem_module.test(self.run, program)
if passed:
hints = [{'id': 'program_already_correct'}]
if not hints and hasattr(language_module, 'hint'):
hints = language_module.hint(self.run, program)
if not hints and hasattr(problem_module, 'hint'):
hints = problem_module.hint(self.run, program)
if not hints:
hints = [{'id': 'no_hint'}]
self._instantiate_and_save_hints(language_module, problem_module, hints)
return hints
def test(self, sid, problem_id, program):
language, problem_group, problem = Problem.get_identifier(problem_id)
language_module = server.problems.load_language(language, 'common')
problem_module = server.problems.load_problem(language, problem_group, problem, 'common')
try:
passed, hints = problem_module.test(self.run, program)
except AttributeError as ex:
hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}]
self._instantiate_and_save_hints(language_module, problem_module, hints)
return hints
# Add hint parameters (such as message index) based on hint class. Append
# the finalized hints to the list of sent hints.
def _instantiate_and_save_hints(self, language_mod, problem_mod, hints):
with self._access_lock:
for hint in hints:
for mod in [language_mod, problem_mod]:
if hasattr(mod, 'hint_type') and hint['id'] in mod.hint_type:
hint_type = mod.hint_type[hint['id']]
hint_type.instantiate(hint, self._sent_hints)
self._sent_hints.extend(hints)
def _interpreter(control, callback):
directory = os.path.dirname(os.path.realpath(__file__))
# TODO drop privileges using a wrapper
script = os.path.join(directory, '..', 'python', 'interpreter.py')
proc = None
while True:
# Ensure the interpreter process is running.
if proc is None:
proc = subprocess.Popen([script],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
# Set the non-blocking flag for stdout.
flags = fcntl(proc.stdout.fileno(), F_GETFL)
fcntl(proc.stdout.fileno(), F_SETFL, flags | os.O_NONBLOCK)
# Get a control command.
try:
cmd, data = control.get_nowait()
if cmd == 'exec':
exec_str = 'exec("""\\\n{}\n""")\n'.format(data.replace('"', '\\"'))
proc.stdin.write(exec_str.encode('utf-8'))
proc.stdin.flush()
elif cmd == 'push':
proc.stdin.write(data.encode('utf-8'))
proc.stdin.flush()
elif cmd == 'stop':
proc.send_signal(signal.SIGINT)
elif cmd == 'done':
break
except:
pass
# Communicate with child process.
retcode = proc.poll()
if retcode is None:
data = proc.stdout.read()
if data:
if len(data) > 20000:
proc.kill()
proc = None
callback('Child killed for talking too much.\n')
else:
# NOTE this might fail if read() stops in the middle of utf8 sequence
text = data.decode('utf-8')
if text:
callback(text)
else:
if retcode == -9: # killed by ulimit
callback('Child killed due to overconsumption.\n')
elif retcode == -31: # killed by seccomp
callback('Child killed due to sandbox misbehavior.\n')
else:
callback('Child exited with status "{}".\n'.format(retcode))
proc = None
# TODO we should select() on control and proc.stdout instead of polling
time.sleep(0.1)
# We are done, kill the child.
if proc is not None:
proc.kill()
# Execute [code] and evaluate [expr]. Input is given by the string [stdin].
# Return result of evaluation, the contents of stdout and stderr, and the
# exception traceback.
# TODO sandbox this
def _run_exec(conn, code, expr=None, stdin=''):
result, out, err, exc = None, None, None, None
sys.stdin = io.StringIO(stdin)
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
try:
env = {}
if code:
exec(code, env)
if expr:
result = eval(expr, env)
except Exception as ex:
# Exception is not JSON serializable, so return traceback as string
# (without the first entry, which is this function).
import traceback
e_type, e_value, e_tb = sys.exc_info()
stack = traceback.extract_tb(e_tb)
exc = ''.join(
['Traceback (most recent call last):\n'] +
[' line {}, in {}\n'.format(lineno, name) + (line+'\n' if line else '')
for filename, lineno, name, line in stack[1:]] +
traceback.format_exception_only(e_type, e_value)
).rstrip()
finally:
out = sys.stdout.getvalue()
err = sys.stderr.getvalue()
sys.stdin.close()
sys.stdout.close()
sys.stderr.close()
conn.send((result, out, err, exc))
server.language_session_handlers['python'] = lambda user_session, problem_id, language_identifier, group_identifier, problem_identifier: PythonSession(lambda text: user_session.send({'event': 'terminal_output', 'text': text}))
|