-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathisolate_job.rb
371 lines (311 loc) · 11.9 KB
/
isolate_job.rb
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
class IsolateJob < ApplicationJob
retry_on RuntimeError, wait: 0.1.seconds, attempts: 100
queue_as ENV["JUDGE0_VERSION"].to_sym
STDIN_FILE_NAME = "stdin.txt"
STDOUT_FILE_NAME = "stdout.txt"
STDERR_FILE_NAME = "stderr.txt"
METADATA_FILE_NAME = "metadata.txt"
ADDITIONAL_FILES_ARCHIVE_FILE_NAME = "additional_files.zip"
attr_reader :submission, :cgroups,
:box_id, :workdir, :boxdir, :tmpdir,
:source_file, :stdin_file, :stdout_file,
:stderr_file, :metadata_file, :additional_files_archive_file
def perform(submission_id)
@submission = Submission.find(submission_id)
submission.update(status: Status.process, started_at: DateTime.now, execution_host: ENV["HOSTNAME"])
time = []
memory = []
submission.number_of_runs.times do
initialize_workdir
if compile == :failure
cleanup
return
end
run
verify
time << submission.time
memory << submission.memory
cleanup
break if submission.status != Status.ac
end
submission.time = time.inject(&:+).to_f / time.size
submission.memory = memory.inject(&:+).to_f / memory.size
submission.save
rescue Exception => e
raise e.message unless submission
submission.update(message: e.message, status: Status.boxerr, finished_at: DateTime.now)
cleanup(raise_exception = false)
ensure
call_callback
end
private
def initialize_workdir
@box_id = submission.id%2147483647
@cgroups = (!submission.enable_per_process_and_thread_time_limit || !submission.enable_per_process_and_thread_memory_limit) ? "--cg" : ""
@workdir = `isolate #{cgroups} -b #{box_id} --init`.chomp
@boxdir = workdir + "/box"
@tmpdir = workdir + "/tmp"
@source_file = boxdir + "/" + submission.language.source_file.to_s
@stdin_file = workdir + "/" + STDIN_FILE_NAME
@stdout_file = workdir + "/" + STDOUT_FILE_NAME
@stderr_file = workdir + "/" + STDERR_FILE_NAME
@metadata_file = workdir + "/" + METADATA_FILE_NAME
@additional_files_archive_file = boxdir + "/" + ADDITIONAL_FILES_ARCHIVE_FILE_NAME
[stdin_file, stdout_file, stderr_file, metadata_file].each do |f|
initialize_file(f)
end
File.open(source_file, "wb") { |f| f.write(submission.source_code) } unless submission.is_project
File.open(stdin_file, "wb") { |f| f.write(submission.stdin) }
extract_archive
end
def initialize_file(file)
`sudo touch #{file} && sudo chown $(whoami): #{file}`
end
def extract_archive
return unless submission.additional_files?
File.open(additional_files_archive_file, "wb") { |f| f.write(submission.additional_files) }
command = "isolate #{cgroups} \
-s \
-b #{box_id} \
--stderr-to-stdout \
-t 2 \
-x 1 \
-w 4 \
-k #{Config::MAX_STACK_LIMIT} \
-p#{Config::MAX_MAX_PROCESSES_AND_OR_THREADS} \
#{submission.enable_per_process_and_thread_time_limit ? (cgroups.present? ? "--no-cg-timing" : "") : "--cg-timing"} \
#{submission.enable_per_process_and_thread_memory_limit ? "-m " : "--cg-mem="}#{Config::MAX_MEMORY_LIMIT} \
-f #{Config::MAX_EXTRACT_SIZE} \
--run \
-- /usr/bin/unzip -n -qq #{ADDITIONAL_FILES_ARCHIVE_FILE_NAME} \
"
puts "[#{DateTime.now}] Extracting archive for submission #{submission.token} (#{submission.id}):"
puts command.gsub(/\s+/, " ")
puts
`#{command}`
File.delete(additional_files_archive_file)
end
def compile
unless submission.is_project
return :success unless submission.language.compile_cmd
end
compile_script = boxdir + "/" + "compile.sh"
acceptable_project_compile_scripts = [compile_script, boxdir + "/" + "compile"]
if submission.is_project
compile_file_exists = false
acceptable_project_compile_scripts.each do |f|
if File.file?(f)
compile_script = f
compile_file_exists = true
break
end
end
unless compile_file_exists
return :success # If compile script does not exist then this project does not need to be compiled.
end
else
# gsub can be skipped if compile script is used, but is kept for additional security.
compiler_options = submission.compiler_options.to_s.strip.encode("UTF-8", invalid: :replace).gsub(/[$&;<>|`]/, "")
File.open(compile_script, "w") { |f| f.write("#{submission.language.compile_cmd % compiler_options}") }
end
compile_output_file = workdir + "/" + "compile_output.txt"
initialize_file(compile_output_file)
command = "isolate #{cgroups} \
-s \
-b #{box_id} \
-M #{metadata_file} \
--stderr-to-stdout \
-i /dev/null \
-t #{Config::MAX_CPU_TIME_LIMIT} \
-x 0 \
-w #{Config::MAX_WALL_TIME_LIMIT} \
-k #{Config::MAX_STACK_LIMIT} \
-p#{Config::MAX_MAX_PROCESSES_AND_OR_THREADS} \
#{submission.enable_per_process_and_thread_time_limit ? (cgroups.present? ? "--no-cg-timing" : "") : "--cg-timing"} \
#{submission.enable_per_process_and_thread_memory_limit ? "-m " : "--cg-mem="}#{Config::MAX_MEMORY_LIMIT} \
-f #{Config::MAX_MAX_FILE_SIZE} \
-E HOME=/tmp \
-E PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\" \
-E LANG -E LANGUAGE -E LC_ALL -E JUDGE0_HOMEPAGE -E JUDGE0_SOURCE_CODE -E JUDGE0_MAINTAINER -E JUDGE0_VERSION \
-d /etc:noexec \
--run \
-- /bin/bash $(basename #{compile_script}) > #{compile_output_file} \
"
puts "[#{DateTime.now}] Compiling submission #{submission.token} (#{submission.id}):"
puts command.gsub(/\s+/, " ")
puts
`#{command}`
process_status = $?
compile_output = File.read(compile_output_file)
compile_output = nil if compile_output.empty?
submission.compile_output = compile_output
metadata = get_metadata
reset_metadata_file
files_to_remove = [compile_output_file]
files_to_remove << compile_script unless submission.is_project
files_to_remove.each do |f|
`sudo rm -rf #{f}`
end
return :success if process_status.success?
if metadata[:status] == "TO"
submission.compile_output = "Compilation time limit exceeded."
end
submission.finished_at = DateTime.now
submission.time = nil
submission.wall_time = nil
submission.memory = nil
submission.stdout = nil
submission.stderr = nil
submission.exit_code = nil
submission.exit_signal = nil
submission.message = nil
submission.status = Status.ce
submission.save
return :failure
end
def run
run_script = boxdir + "/" + "run.sh"
acceptable_project_run_scripts = [run_script, boxdir + "/" + "run"]
acceptable_project_run_scripts.each do |f|
if File.file?(f)
run_script = f
break
end
end
unless submission.is_project
# gsub is mandatory!
command_line_arguments = submission.command_line_arguments.to_s.strip.encode("UTF-8", invalid: :replace).gsub(/[$&;<>|`]/, "")
File.open(run_script, "w") { |f| f.write("#{submission.language.run_cmd} #{command_line_arguments}")}
end
command = "isolate #{cgroups} \
-s \
-b #{box_id} \
-M #{metadata_file} \
#{submission.redirect_stderr_to_stdout ? "--stderr-to-stdout" : ""} \
#{submission.enable_network ? "--share-net" : ""} \
-t #{submission.cpu_time_limit} \
-x #{submission.cpu_extra_time} \
-w #{submission.wall_time_limit} \
-k #{submission.stack_limit} \
-p#{submission.max_processes_and_or_threads} \
#{submission.enable_per_process_and_thread_time_limit ? (cgroups.present? ? "--no-cg-timing" : "") : "--cg-timing"} \
#{submission.enable_per_process_and_thread_memory_limit ? "-m " : "--cg-mem="}#{submission.memory_limit} \
-f #{submission.max_file_size} \
-E HOME=/tmp \
-E PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\" \
-E LANG -E LANGUAGE -E LC_ALL -E JUDGE0_HOMEPAGE -E JUDGE0_SOURCE_CODE -E JUDGE0_MAINTAINER -E JUDGE0_VERSION \
-d /etc:noexec \
--run \
-- /bin/bash $(basename #{run_script}) \
< #{stdin_file} > #{stdout_file} 2> #{stderr_file} \
"
puts "[#{DateTime.now}] Running submission #{submission.token} (#{submission.id}):"
puts command.gsub(/\s+/, " ")
puts
`#{command}`
`sudo rm #{run_script}` unless submission.is_project
end
def verify
submission.finished_at = DateTime.now
metadata = get_metadata
program_stdout = File.read(stdout_file)
program_stdout = nil if program_stdout.empty?
program_stderr = File.read(stderr_file)
program_stderr = nil if program_stderr.empty?
submission.time = metadata[:time]
submission.wall_time = metadata[:"time-wall"]
submission.memory = (cgroups.present? ? metadata[:"cg-mem"] : metadata[:"max-rss"])
submission.stdout = program_stdout
submission.stderr = program_stderr
submission.exit_code = metadata[:exitcode].try(:to_i) || 0
submission.exit_signal = metadata[:exitsig].try(:to_i)
submission.message = metadata[:message]
submission.status = determine_status(metadata[:status], submission.exit_signal)
# After adding support for compiler_options and command_line_arguments
# status "Exec Format Error" will no longer occur because compile and run
# is done inside a dynamically created bash script, thus isolate doesn't call
# execve directily on submission.language.compile_cmd or submission.langauge.run_cmd.
# Consequence of running compile and run through bash script is that when
# target binary is not found then submission gets status "Runtime Error (NZEC)".
#
# I think this is for now O.K. behaviour, but I will leave this if block
# here until I am 100% sure that "Exec Format Error" can be deprecated.
if submission.status == Status.boxerr &&
(
submission.message.to_s.match(/^execve\(.+\): Exec format error$/) ||
submission.message.to_s.match(/^execve\(.+\): No such file or directory$/) ||
submission.message.to_s.match(/^execve\(.+\): Permission denied$/)
)
submission.status = Status.exeerr
end
end
def cleanup(raise_exception = true)
fix_permissions
`sudo rm -rf #{boxdir}/* #{tmpdir}/*`
[stdin_file, stdout_file, stderr_file, metadata_file].each do |f|
`sudo rm -rf #{f}`
end
`isolate #{cgroups} -b #{box_id} --cleanup`
raise "Cleanup of sandbox #{box_id} failed." if raise_exception && Dir.exists?(workdir)
end
def reset_metadata_file
`sudo rm -rf #{metadata_file}`
initialize_file(metadata_file)
end
def fix_permissions
`sudo chown -R $(whoami): #{boxdir}`
end
def call_callback
return unless submission.callback_url.present?
serialized_submission = ActiveModelSerializers::SerializableResource.new(
submission,
{
serializer: SubmissionSerializer,
base64_encoded: true,
fields: SubmissionSerializer.default_fields
}
).to_json
Config::CALLBACKS_MAX_TRIES.times do
begin
response = HTTParty.put(
submission.callback_url,
body: serialized_submission,
headers: {
"Content-Type" => "application/json"
},
timeout: Config::CALLBACKS_TIMEOUT
)
break
rescue Exception => e
end
end
rescue Exception => e
end
def get_metadata
metadata = File.read(metadata_file).split("\n").collect do |e|
{ e.split(":").first.to_sym => e.split(":")[1..-1].join(":") }
end.reduce({}, :merge)
return metadata
end
def determine_status(status, exit_signal)
if status == "TO"
return Status.tle
elsif status == "SG"
return Status.find_runtime_error_by_status_code(exit_signal)
elsif status == "RE"
return Status.nzec
elsif status == "XX"
return Status.boxerr
elsif submission.expected_output.nil? || strip(submission.expected_output) == strip(submission.stdout)
return Status.ac
else
return Status.wa
end
end
def strip(text)
return nil unless text
text.split("\n").collect(&:rstrip).join("\n").rstrip
rescue ArgumentError
return text
end
end