Skip to content

Commit

Permalink
Fix logic for choosing cgroup flags. Change default value of enable_p…
Browse files Browse the repository at this point in the history
…er_process_and_thread_memory_limit to false.
  • Loading branch information
hermanzdosilovic committed Jan 13, 2020
1 parent a6f693a commit 4116b9c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/helpers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Config
MAX_MAX_PROCESSES_AND_OR_THREADS = (ENV['MAX_MAX_PROCESSES_AND_OR_THREADS'].presence || 120).to_i
ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT = ENV['ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT'] == "true"
ALLOW_ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT = ENV['ALLOW_ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT'] != "false"
ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT = ENV['ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT'] != "false"
ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT = ENV['ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT'] == "true"
ALLOW_ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT = ENV['ALLOW_ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT'] != "false"
MAX_FILE_SIZE = (ENV['MAX_FILE_SIZE'].presence || 1024).to_i
MAX_MAX_FILE_SIZE = (ENV['MAX_MAX_FILE_SIZE'].presence || 4096).to_i
Expand Down
22 changes: 11 additions & 11 deletions app/jobs/isolate_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class IsolateJob < ApplicationJob
STDERR_FILE_NAME = "stderr.txt"
METADATA_FILE_NAME = "metadata.txt"

attr_reader :submission, :cgroups_flag,
attr_reader :submission, :cgroups,
:box_id, :workdir, :boxdir, :tmpdir,
:source_file, :stdin_file, :stdout_file, :stderr_file, :metadata_file

Expand Down Expand Up @@ -47,8 +47,8 @@ def perform(submission)

def initialize_workdir
@box_id = submission.id%2147483647
@cgroups_flag = (submission.enable_per_process_and_thread_time_limit || submission.enable_per_process_and_thread_memory_limit) ? "--cg" : ""
@workdir = `isolate #{cgroups_flag} -b #{box_id} --init`.chomp
@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
Expand Down Expand Up @@ -78,7 +78,7 @@ def compile
compile_script = boxdir + "/" + "compile"
File.open(compile_script, "w") { |f| f.write("#{submission.language.compile_cmd % compiler_options}")}

command = "isolate #{cgroups_flag} \
command = "isolate #{cgroups} \
-s \
-b #{box_id} \
-M #{metadata_file} \
Expand All @@ -87,8 +87,8 @@ def compile
-w 10 \
-k #{Config::MAX_STACK_LIMIT} \
-p#{Config::MAX_MAX_PROCESSES_AND_OR_THREADS} \
#{submission.enable_per_process_and_thread_time_limit ? "--cg-timing" : (cgroups_flag.present? ? "--no-cg-timing" : "")} \
#{submission.enable_per_process_and_thread_memory_limit ? "--cg-mem=" : "-m "}#{submission.memory_limit} \
#{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=#{workdir} \
-E PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\" \
Expand Down Expand Up @@ -141,7 +141,7 @@ def run
run_script = boxdir + "/" + "run"
File.open(run_script, "w") { |f| f.write("#{submission.language.run_cmd} #{command_line_arguments}")}

command = "isolate #{cgroups_flag} \
command = "isolate #{cgroups} \
-s \
-b #{box_id} \
-M #{metadata_file} \
Expand All @@ -150,8 +150,8 @@ def run
-w #{submission.wall_time_limit} \
-k #{submission.stack_limit} \
-p#{submission.max_processes_and_or_threads} \
#{submission.enable_per_process_and_thread_time_limit ? "--cg-timing" : (cgroups_flag.present? ? "--no-cg-timing" : "")} \
#{submission.enable_per_process_and_thread_memory_limit ? "--cg-mem=" : "-m "}#{submission.memory_limit} \
#{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=#{workdir} \
-E PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\" \
Expand Down Expand Up @@ -183,7 +183,7 @@ def verify

submission.time = metadata[:time]
submission.wall_time = metadata[:"time-wall"]
submission.memory = (cgroups_flag.present? ? metadata[:"cg-mem"] : metadata[:"max-rss"])
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
Expand Down Expand Up @@ -213,7 +213,7 @@ def verify
def cleanup(raise_exception = true)
fix_permissions
`sudo rm -rf #{boxdir}/* #{tmpdir}/*`
`isolate #{cgroups_flag} -b #{box_id} --cleanup`
`isolate #{cgroups} -b #{box_id} --cleanup`
raise "Cleanup of sandbox #{box_id} failed." if raise_exception && Dir.exists?(workdir)
end

Expand Down
2 changes: 1 addition & 1 deletion judge0-api.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT=
ALLOW_ENABLE_PER_PROCESS_AND_THREAD_TIME_LIMIT=

# If true then MEMORY_LIMIT will be used as per process and thread.
# Default: true, i.e. MEMORY_LIMIT is set as per process and thread.
# Default: false, i.e. MEMORY_LIMIT is set as a total limit for all processes and threads.
ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT=

# If false, user won't be able to set ENABLE_PER_PROCESS_AND_THREAD_MEMORY_LIMIT.
Expand Down

0 comments on commit 4116b9c

Please sign in to comment.