From 096a8d25b7d99032a99f85e07eec1c57b4cabde5 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sat, 17 Jul 2021 16:26:20 +0100 Subject: test: add test_multiprocess_resource_limits to test memory limit sharing This test ensures that spawned child processes inherit the same resource group as the parent by spawning 2 child processes which each allocate a 40MB object, it then verifies that one of the child processes was killed with SIGKILL for violating the resource quota. --- tests/test_nsjail.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests') diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index ec5c56f..c0ed96b 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -67,6 +67,34 @@ class NsJailTests(unittest.TestCase): self.assertIn("Resource temporarily unavailable", result.stdout) self.assertEqual(result.stderr, None) + def test_multiprocess_resource_limits(self): + code = dedent(""" + import time + from multiprocessing import Process + + def f(): + object = "A" * 40_000_000 + time.sleep(0.5) + + + proc_1 = Process(target=f) + proc_2 = Process(target=f) + + proc_1.start() + proc_2.start() + + proc_1.join() + proc_2.join() + + print(proc_1.exitcode, proc_2.exitcode) + """) + + result = self.nsjail.python3(code) + + exit_codes = result.stdout.strip().split() + self.assertIn("-9", exit_codes) + self.assertEqual(result.stderr, None) + def test_read_only_file_system(self): for path in ("/", "/etc", "/lib", "/lib64", "/snekbox", "/usr"): with self.subTest(path=path): -- cgit v1.2.3