]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
kunit: tool: copy caller args in run_kernel to prevent mutation
authorShuvam Pandey <shuvampandey1@gmail.com>
Thu, 26 Feb 2026 15:29:10 +0000 (21:14 +0545)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 2 Mar 2026 17:02:06 +0000 (10:02 -0700)
run_kernel() appended KUnit flags directly to the caller-provided args
list. When exec_tests() calls run_kernel() repeatedly (e.g. with
--run_isolated), each call mutated the same list, causing later runs
to inherit stale filter_glob values and duplicate kunit.enable flags.

Fix this by copying args at the start of run_kernel(). Add a regression
test that calls run_kernel() twice with the same list and verifies the
original remains unchanged.

Fixes: ff9e09a3762f ("kunit: tool: support running each suite/test separately")
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
Reviewed-by: David Gow <david@davidgow.net>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/kunit/kunit_kernel.py
tools/testing/kunit/kunit_tool_test.py

index 260d8d9aa1db4ac3c04fa755fb738dd834b976db..2998e1bc088b28f73f5377826f682230a6f3ce1a 100644 (file)
@@ -346,8 +346,10 @@ class LinuxSourceTree:
                return self.validate_config(build_dir)
 
        def run_kernel(self, args: Optional[List[str]]=None, build_dir: str='', filter_glob: str='', filter: str='', filter_action: Optional[str]=None, timeout: Optional[int]=None) -> Iterator[str]:
-               if not args:
-                       args = []
+               # Copy to avoid mutating the caller-supplied list. exec_tests() reuses
+               # the same args across repeated run_kernel() calls (e.g. --run_isolated),
+               # so appending to the original would accumulate stale flags on each call.
+               args = list(args) if args else []
                if filter_glob:
                        args.append('kunit.filter_glob=' + filter_glob)
                if filter:
index b67408147c1faaab12b168aabe3bfba8bf1b00aa..f6383884c59987bb5ae74c045d1dd4bb6f77b9ce 100755 (executable)
@@ -503,6 +503,32 @@ class LinuxSourceTreeTest(unittest.TestCase):
                        with open(kunit_kernel.get_outfile_path(build_dir), 'rt') as outfile:
                                self.assertEqual(outfile.read(), 'hi\nbye\n', msg='Missing some output')
 
+       def test_run_kernel_args_not_mutated(self):
+               """Verify run_kernel() copies args so callers can reuse them."""
+               start_calls = []
+
+               def fake_start(start_args, unused_build_dir):
+                       start_calls.append(list(start_args))
+                       return subprocess.Popen(['printf', 'KTAP version 1\n'],
+                                               text=True, stdout=subprocess.PIPE)
+
+               with tempfile.TemporaryDirectory('') as build_dir:
+                       tree = kunit_kernel.LinuxSourceTree(build_dir,
+                                       kunitconfig_paths=[os.devnull])
+                       with mock.patch.object(tree._ops, 'start', side_effect=fake_start), \
+                            mock.patch.object(kunit_kernel.subprocess, 'call'):
+                               kernel_args = ['mem=1G']
+                               for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir,
+                                                        filter_glob='suite.test1'):
+                                       pass
+                               for _ in tree.run_kernel(args=kernel_args, build_dir=build_dir,
+                                                        filter_glob='suite.test2'):
+                                       pass
+                               self.assertEqual(kernel_args, ['mem=1G'],
+                                       'run_kernel() should not modify caller args')
+                               self.assertIn('kunit.filter_glob=suite.test1', start_calls[0])
+                               self.assertIn('kunit.filter_glob=suite.test2', start_calls[1])
+
        def test_build_reconfig_no_config(self):
                with tempfile.TemporaryDirectory('') as build_dir:
                        with open(kunit_kernel.get_kunitconfig_path(build_dir), 'w') as f: