Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
2b095e82
Commit
2b095e82
authored
Jan 05, 2015
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #34 from edx/ned/better-cpu-information
Set CPU limit to get better info. More specific tests
parents
75307b25
9cf4bae2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
6 deletions
+15
-6
codejail/jail_code.py
+5
-1
codejail/tests/test_jail_code.py
+10
-5
No files found.
codejail/jail_code.py
View file @
2b095e82
...
@@ -264,7 +264,11 @@ def set_process_limits(): # pragma: no cover
...
@@ -264,7 +264,11 @@ def set_process_limits(): # pragma: no cover
# CPU seconds, not wall clock time.
# CPU seconds, not wall clock time.
cpu
=
LIMITS
[
"CPU"
]
cpu
=
LIMITS
[
"CPU"
]
if
cpu
:
if
cpu
:
resource
.
setrlimit
(
resource
.
RLIMIT_CPU
,
(
cpu
,
cpu
))
# Set the soft limit and the hard limit differently. When the process
# reaches the soft limit, a SIGXCPU will be sent, which should kill the
# process. If you set the soft and hard limits the same, then the hard
# limit is reached, and a SIGKILL is sent, which is less distinctive.
resource
.
setrlimit
(
resource
.
RLIMIT_CPU
,
(
cpu
,
cpu
+
1
))
# Total process virtual memory.
# Total process virtual memory.
vmem
=
LIMITS
[
"VMEM"
]
vmem
=
LIMITS
[
"VMEM"
]
...
...
codejail/tests/test_jail_code.py
View file @
2b095e82
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
import
os
import
os
import
os.path
import
os.path
import
shutil
import
shutil
import
signal
import
textwrap
import
textwrap
import
tempfile
import
tempfile
import
unittest
import
unittest
...
@@ -180,7 +181,8 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
...
@@ -180,7 +181,8 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
set_limit
(
'VMEM'
,
30000000
)
set_limit
(
'VMEM'
,
30000000
)
res
=
jailpy
(
code
=
"print len(bytearray(40000000))"
)
res
=
jailpy
(
code
=
"print len(bytearray(40000000))"
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertIn
(
"MemoryError"
,
res
.
stderr
)
self
.
assertEqual
(
res
.
status
,
1
)
def
test_changing_vmem_limit
(
self
):
def
test_changing_vmem_limit
(
self
):
# Up the limit, it will succeed.
# Up the limit, it will succeed.
...
@@ -199,15 +201,18 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
...
@@ -199,15 +201,18 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
self
.
assertEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
status
,
0
)
def
test_cant_use_too_much_cpu
(
self
):
def
test_cant_use_too_much_cpu
(
self
):
set_limit
(
'CPU'
,
1
)
set_limit
(
'REALTIME'
,
100
)
res
=
jailpy
(
code
=
"print sum(xrange(2**31-1))"
)
res
=
jailpy
(
code
=
"print sum(xrange(2**31-1))"
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assert
NotEqual
(
res
.
status
,
0
)
self
.
assert
Equal
(
res
.
status
,
128
+
signal
.
SIGXCPU
)
# 137
def
test_cant_use_too_much_time
(
self
):
def
test_cant_use_too_much_time
(
self
):
# Default time limit is 1 second. Sleep for 1.5 seconds.
# Default time limit is 1 second. Sleep for 1.5 seconds.
set_limit
(
'CPU'
,
100
)
res
=
jailpy
(
code
=
"import time; time.sleep(1.5); print 'Done!'"
)
res
=
jailpy
(
code
=
"import time; time.sleep(1.5); print 'Done!'"
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
stdout
,
""
)
self
.
assertEqual
(
res
.
status
,
-
signal
.
SIGKILL
)
# -9
def
test_changing_realtime_limit
(
self
):
def
test_changing_realtime_limit
(
self
):
# Change time limit to 2 seconds, sleeping for 1.5 will be fine.
# Change time limit to 2 seconds, sleeping for 1.5 will be fine.
...
@@ -336,12 +341,12 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
...
@@ -336,12 +341,12 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
def
test_reading_dev_random
(
self
):
def
test_reading_dev_random
(
self
):
# We can read 10 bytes just fine.
# We can read 10 bytes just fine.
res
=
jailpy
(
code
=
"x = open('/dev/random').read(10); print len(x)"
)
res
=
jailpy
(
code
=
"x = open('/dev/
u
random').read(10); print len(x)"
)
self
.
assertResultOk
(
res
)
self
.
assertResultOk
(
res
)
self
.
assertEqual
(
res
.
stdout
,
"10
\n
"
)
self
.
assertEqual
(
res
.
stdout
,
"10
\n
"
)
# If we try to read all of it, we'll be killed by the real-time limit.
# If we try to read all of it, we'll be killed by the real-time limit.
res
=
jailpy
(
code
=
"x = open('/dev/random').read(); print 'Done!'"
)
res
=
jailpy
(
code
=
"x = open('/dev/
u
random').read(); print 'Done!'"
)
self
.
assertNotEqual
(
res
.
status
,
0
)
self
.
assertNotEqual
(
res
.
status
,
0
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment