Debian Codegraph @

tests/linux_kernel/helpers/test_module.py

raw
1 # Copyright (c) 2024 Oracle and/or its affiliates
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 from drgn.helpers.linux.module import (
4 address_to_module,
5 find_module,
6 for_each_module,
7 module_address_regions,
8 module_percpu_region,
9 module_taints,
10 )
11 from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod
12
13
14 class TestListModules(LinuxKernelTestCase):
15 def test_for_each_module(self):
16 with open("/proc/modules") as f:
17 sys_modules = set(line.split(maxsplit=1)[0] for line in f)
18 drgn_modules = set()
19 for module in for_each_module(self.prog):
20 drgn_modules.add(module.name.string_().decode())
21
22 self.assertEqual(sys_modules, drgn_modules)
23
24
25 @skip_unless_have_test_kmod
26 class TestModules(LinuxKernelTestCase):
27 @classmethod
28 def setUpClass(cls):
29 super().setUpClass()
30 cls.mod = find_module(cls.prog, "drgn_test")
31
32 def test_module_percpu_region(self):
33 pcpu_addr = self.prog.symbol("drgn_test_percpu_static").address
34 start, size = module_percpu_region(self.mod)
35 if start == 0:
36 self.skipTest("No module percpu region on !SMP")
37 self.assertTrue(start <= pcpu_addr <= start + size)
38
39 def test_module_address_regions(self):
40 regions = module_address_regions(self.mod)
41
42 def assertInRegions(addr):
43 for start, size in regions:
44 if start <= addr < start + size:
45 break
46 else:
47 self.fail(f"address {addr:x} not found in drgn_test module regions")
48
49 self.assertEqual(address_to_module(self.prog, addr), self.mod)
50
51 # function symbol (should be in .text)
52 assertInRegions(self.prog.symbol("drgn_test_function").address)
53 # variable symbol (should be in .data)
54 assertInRegions(self.prog.symbol("drgn_test_empty_list").address)
55 # constant variable (should be in .rodata)
56 assertInRegions(self.prog.symbol("drgn_test_have_maple_tree").address)
57
58 def test_module_taints(self):
59 self.assertIn("O", module_taints(self.mod))