Debian Codegraph @

tests/linux_kernel/helpers/test_fs.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import ctypes
5 import mmap
6 import os
7 import os.path
8 from pathlib import Path
9 import socket
10 import stat
11 import tempfile
12
13 from drgn.helpers.linux.fs import (
14 d_path,
15 decode_file_type,
16 dentry_path,
17 fget,
18 for_each_file,
19 for_each_mount,
20 inode_for_each_page,
21 inode_path,
22 inode_paths,
23 mount_dst,
24 path_lookup,
25 )
26 from drgn.helpers.linux.pid import find_task
27 from tests.linux_kernel import MS_BIND, LinuxKernelTestCase, mlock, mount, umount
28
29
30 class TestFs(LinuxKernelTestCase):
31 def test_path_lookup(self):
32 with tempfile.NamedTemporaryFile(prefix="drgn-tests-") as f:
33 path = path_lookup(self.prog, os.path.abspath(f.name))
34 self.assertEqual(
35 path.dentry.d_name.name.string_(), os.fsencode(os.path.basename(f.name))
36 )
37
38 def test_path_lookup_bind_mount(self):
39 with tempfile.NamedTemporaryFile(prefix="drgn-tests-") as f:
40 old_mnt = path_lookup(self.prog, os.path.abspath(f.name)).mnt
41 mount(f.name, f.name, "", MS_BIND)
42 try:
43 new_mnt = path_lookup(self.prog, os.path.abspath(f.name)).mnt
44 self.assertNotEqual(old_mnt, new_mnt)
45 finally:
46 umount(f.name)
47
48 def test_d_path(self):
49 task = find_task(self.prog, os.getpid())
50 self.assertEqual(d_path(task.fs.pwd.address_of_()), os.fsencode(os.getcwd()))
51
52 def test_d_path_dentry_only(self):
53 # Since d_path(dentry) picks an arbitrary mount containing the dentry,
54 # this should be a directory that is unlikely to be bind mounted
55 # anywhere else.
56 with tempfile.NamedTemporaryFile(dir="/dev/shm") as f:
57 dentry = fget(find_task(self.prog, os.getpid()), f.fileno()).f_path.dentry
58 self.assertEqual(d_path(dentry), os.fsencode(f.name))
59
60 def test_d_path_no_internal_mount(self):
61 if not os.path.isdir("/sys/kernel/tracing"):
62 self.skipTest("The /sys/kernel/tracing directory is not mounted")
63 path = path_lookup(self.prog, "/sys/kernel/tracing/trace_pipe")
64 # The first mount for this super block is usually MNT_INTERNAL, but we
65 # don't want that one. Ensure we skip it.
66 self.assertEqual(d_path(path.dentry), b"/sys/kernel/tracing/trace_pipe")
67
68 def test_dentry_path(self):
69 pwd = os.fsencode(os.getcwd())
70 task = find_task(self.prog, os.getpid())
71 self.assertTrue(pwd.endswith(dentry_path(task.fs.pwd.dentry)))
72
73 def test_inode_paths(self):
74 with tempfile.TemporaryDirectory(prefix="drgn-tests-") as dir:
75 path1 = os.fsencode(os.path.abspath(os.path.join(dir, "a")))
76 path2 = os.fsencode(os.path.abspath(os.path.join(dir, "b")))
77 with open(path1, "w"):
78 os.link(path1, path2)
79 with open(path2, "r"):
80 inode = path_lookup(self.prog, path1).dentry.d_inode
81 paths = list(inode_paths(inode))
82 self.assertEqual(len(paths), 2)
83 self.assertTrue(
84 (path1.endswith(paths[0]) and path2.endswith(paths[1]))
85 or (path1.endswith(paths[1]) and path2.endswith(paths[0]))
86 )
87 self.assertIn(inode_path(inode), paths)
88
89 def test_inode_for_each_page(self):
90 with tempfile.TemporaryFile(dir="/dev/shm") as f:
91 f.write(bytes(2 * mmap.PAGESIZE))
92 f.flush()
93 inode = fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode.read_()
94 with mmap.mmap(f.fileno(), 2 * mmap.PAGESIZE) as map:
95 f.close()
96 address = ctypes.addressof(ctypes.c_char.from_buffer(map))
97 # Make sure the pages are faulted in and stay that way.
98 mlock(address, 2 * mmap.PAGESIZE)
99
100 indices = []
101 i_mapping = inode.i_mapping.read_()
102 for index, page in inode_for_each_page(inode):
103 indices.append(index)
104 self.assertEqual(page.mapping, i_mapping)
105 self.assertEqual(indices, [0, 1])
106
107 def test_for_each_mount(self):
108 with open("/proc/self/mounts", "rb") as f:
109 self.assertEqual(
110 {line.split()[1].decode("unicode-escape") for line in f},
111 {os.fsdecode(mount_dst(mount)) for mount in for_each_mount(self.prog)},
112 )
113
114 def test_for_each_mount_cursor(self):
115 fd = os.open("/proc/self/mounts", os.O_RDONLY)
116 try:
117 # Read a small amount of data so that we leave a cursor in the
118 # middle of the mount list, on kernel versions where this happens.
119 # Cursors were introduced in v5.8 with 9f6c61f96f2d9 ("proc/mounts:
120 # add cursor"), and were eliminated in v6.8 with 2eea9ce4310d8
121 # ("mounts: keep list of mounts in an rbtree"). They were marked
122 # with the flag MNT_CURSOR, but that was only defined as a
123 # preprocessor constant. It's easiest to detect cursors via their
124 # NULL superblock, which should never be present.
125 os.read(fd, 64)
126 for mnt in for_each_mount(self.prog):
127 self.assertNotEqual(mnt.mnt.mnt_sb.value_(), 0)
128 finally:
129 os.close(fd)
130
131 def test_fget(self):
132 with tempfile.NamedTemporaryFile(prefix="drgn-tests-") as f:
133 file = fget(find_task(self.prog, os.getpid()), f.fileno())
134 self.assertEqual(d_path(file.f_path), os.fsencode(os.path.abspath(f.name)))
135
136 def test_for_each_file(self):
137 task = find_task(self.prog, os.getpid())
138 with os.scandir("/proc/self/fd") as dir:
139 # NB: The call to for_each_file() comes first so that it will
140 # include the scandir file descriptor.
141 self.assertEqual(
142 {fd for fd, file in for_each_file(task)},
143 {int(entry.name) for entry in dir},
144 )
145
146 def test_decode_file_type(self):
147 with tempfile.TemporaryDirectory() as tmp_dir:
148 tmp_dir = Path(tmp_dir)
149
150 with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as sock:
151 sock.bind(bytes(tmp_dir / "sock"))
152 self.assertEqual(
153 decode_file_type((tmp_dir / "sock").stat().st_mode), "SOCK"
154 )
155
156 (tmp_dir / "lnk").symlink_to("/dev/null")
157 self.assertEqual(
158 decode_file_type(
159 os.stat(tmp_dir / "lnk", follow_symlinks=False).st_mode
160 ),
161 "LNK",
162 )
163
164 (tmp_dir / "reg").touch()
165 self.assertEqual(decode_file_type((tmp_dir / "reg").stat().st_mode), "REG")
166
167 os.mknod(tmp_dir / "blk", stat.S_IFBLK | 0o600, device=os.makedev(0, 0))
168 self.assertEqual(decode_file_type((tmp_dir / "blk").stat().st_mode), "BLK")
169
170 self.assertEqual(decode_file_type(tmp_dir.stat().st_mode), "DIR")
171
172 self.assertEqual(decode_file_type(os.stat("/dev/null").st_mode), "CHR")
173
174 os.mkfifo(tmp_dir / "fifo")
175 self.assertEqual(
176 decode_file_type((tmp_dir / "fifo").stat().st_mode), "FIFO"
177 )
178
179 def test_decode_file_type_unknown(self):
180 self.assertEqual(decode_file_type(0), "000000")
181 self.assertEqual(decode_file_type(0o030000), "030000")
182 self.assertEqual(decode_file_type(0o050000), "050000")
183 self.assertEqual(decode_file_type(0o070000), "070000")
184 self.assertEqual(decode_file_type(0o110000), "110000")
185 self.assertEqual(decode_file_type(0o130000), "130000")
186 self.assertEqual(decode_file_type(0o150000), "150000")
187 self.assertEqual(decode_file_type(0o170000), "170000")