| 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 |
import re |
| 9 |
import tempfile |
| 10 |
|
| 11 |
from drgn import Object |
| 12 |
from drgn.helpers.linux.fs import fget |
| 13 |
from drgn.helpers.linux.pid import find_task |
| 14 |
from tests.linux_kernel import CLONE_NEWNS, fork_and_stop, mlock, unshare |
| 15 |
from tests.linux_kernel.crash_commands import CrashCommandTestCase |
| 16 |
|
| 17 |
|
| 18 |
class TestFiles(CrashCommandTestCase): |
| 19 |
@classmethod |
| 20 |
def setUpClass(cls): |
| 21 |
super().setUpClass() |
| 22 |
cls.run_crash_command("set -p") |
| 23 |
|
| 24 |
def _test_drgn_common(self, cmd, cache=False): |
| 25 |
self.assertIn("for_each_file(", cmd.drgn_option.stdout) |
| 26 |
|
| 27 |
for variable in ( |
| 28 |
"task", |
| 29 |
"pid", |
| 30 |
"command", |
| 31 |
"inode", |
| 32 |
): |
| 33 |
with self.subTest(variable=variable): |
| 34 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 35 |
|
| 36 |
if cache: |
| 37 |
for variable in ("i_mapping", "nrpages"): |
| 38 |
with self.subTest(variable=variable): |
| 39 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 40 |
|
| 41 |
self.assertNotIn("dentry", cmd.drgn_option.globals) |
| 42 |
else: |
| 43 |
for variable in ("file", "dentry"): |
| 44 |
with self.subTest(variable=variable): |
| 45 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 46 |
|
| 47 |
self.assertNotIn("i_mapping", cmd.drgn_option.globals) |
| 48 |
self.assertNotIn("nrpages", cmd.drgn_option.globals) |
| 49 |
|
| 50 |
self.assertIsInstance(cmd.drgn_option.globals["cpu"], int) |
| 51 |
self.assertIsInstance(cmd.drgn_option.globals["fd"], int) |
| 52 |
self.assertIsInstance(cmd.drgn_option.globals["type"], str) |
| 53 |
for variable in ( |
| 54 |
"root", |
| 55 |
"cwd", |
| 56 |
"path", |
| 57 |
): |
| 58 |
with self.subTest(variable=variable): |
| 59 |
self.assertIsInstance(cmd.drgn_option.globals[variable], bytes) |
| 60 |
|
| 61 |
def test_no_options(self): |
| 62 |
with tempfile.NamedTemporaryFile() as f: |
| 63 |
cmd = self.check_crash_command("files") |
| 64 |
self.assertRegex(cmd.stdout, r"(?m)^ROOT: .* CWD:") |
| 65 |
self.assertRegex( |
| 66 |
cmd.stdout, |
| 67 |
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+{re.escape(f.name)}", |
| 68 |
) |
| 69 |
|
| 70 |
self._test_drgn_common(cmd) |
| 71 |
|
| 72 |
def test_tasks(self): |
| 73 |
with tempfile.NamedTemporaryFile() as f: |
| 74 |
with fork_and_stop() as pid: |
| 75 |
cmd = self.check_crash_command(f"files {os.getpid()} {pid}") |
| 76 |
foreach_cmd = self.check_crash_command( |
| 77 |
f"foreach {os.getpid()} {pid} files", mode="capture" |
| 78 |
) |
| 79 |
|
| 80 |
for c in (cmd, foreach_cmd): |
| 81 |
self.assertIn(f"PID: {os.getpid()}", c.stdout) |
| 82 |
self.assertIn(f"PID: {pid}", c.stdout) |
| 83 |
self.assertRegex(c.stdout, r"(?m)^ROOT: .* CWD:") |
| 84 |
self.assertEqual( |
| 85 |
len(re.findall(rf"^\s*{f.fileno()}\b", c.stdout, flags=re.M)), 2 |
| 86 |
) |
| 87 |
|
| 88 |
self._test_drgn_common(cmd) |
| 89 |
|
| 90 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 91 |
|
| 92 |
def test_c(self): |
| 93 |
with tempfile.NamedTemporaryFile() as f: |
| 94 |
cmd = self.check_crash_command(f"files -c {os.getpid()}") |
| 95 |
foreach_cmd = self.check_crash_command( |
| 96 |
f"foreach {os.getpid()} files -c", mode="capture" |
| 97 |
) |
| 98 |
|
| 99 |
for c in (cmd, foreach_cmd): |
| 100 |
self.assertRegex(c.stdout, r"(?m)^ROOT: .* CWD:") |
| 101 |
self.assertRegex( |
| 102 |
c.stdout, |
| 103 |
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9]+\s+REG\s+{re.escape(f.name)}", |
| 104 |
) |
| 105 |
|
| 106 |
self._test_drgn_common(cmd, cache=True) |
| 107 |
|
| 108 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 109 |
|
| 110 |
def test_d(self): |
| 111 |
with tempfile.NamedTemporaryFile() as f: |
| 112 |
dentry = fget( |
| 113 |
find_task(self.prog, os.getpid()), f.fileno() |
| 114 |
).f_path.dentry.read_() |
| 115 |
cmd = self.check_crash_command(f"files -d {dentry.value_():x}") |
| 116 |
|
| 117 |
self.assertRegex( |
| 118 |
cmd.stdout, |
| 119 |
rf"(?m)^\s*{dentry.value_():x}\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+.*{re.escape(os.path.basename(f.name))}", |
| 120 |
) |
| 121 |
|
| 122 |
self.assertEqual(cmd.drgn_option.globals["dentry"], dentry) |
| 123 |
|
| 124 |
for variable in ("inode", "sb"): |
| 125 |
with self.subTest(variable=variable): |
| 126 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 127 |
self.assertEqual(cmd.drgn_option.globals["type"], "REG") |
| 128 |
self.assertIsInstance(cmd.drgn_option.globals["path"], bytes) |
| 129 |
|
| 130 |
def test_p(self): |
| 131 |
with tempfile.TemporaryFile(dir="/dev/shm") as f: |
| 132 |
f.write(os.urandom(2 * mmap.PAGESIZE)) |
| 133 |
f.flush() |
| 134 |
inode = fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode.read_() |
| 135 |
with mmap.mmap(f.fileno(), 2 * mmap.PAGESIZE) as map: |
| 136 |
f.close() |
| 137 |
address = ctypes.addressof(ctypes.c_char.from_buffer(map)) |
| 138 |
# Make sure the pages are faulted in and stay that way. |
| 139 |
mlock(address, 2 * mmap.PAGESIZE) |
| 140 |
|
| 141 |
cmd = self.check_crash_command(f"files -p {inode.value_():x}") |
| 142 |
|
| 143 |
self.assertRegex(cmd.stdout, r"(?m)^\s*INODE\s+NRPAGES.*\n\s*[0-9a-f]+\s+2") |
| 144 |
self.assertRegex( |
| 145 |
cmd.stdout, r"(?m)^\s*PAGE\s+PHYSICAL\s+MAPPING\s+INDEX\s+CNT\s+FLAGS.*\n" |
| 146 |
) |
| 147 |
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+\b") |
| 148 |
|
| 149 |
self.assertIn("inode_for_each_page(", cmd.drgn_option.stdout) |
| 150 |
for variable in ( |
| 151 |
"inode", |
| 152 |
"i_mapping", |
| 153 |
"nrpages", |
| 154 |
"page", |
| 155 |
"physical", |
| 156 |
"cnt", |
| 157 |
"flags", |
| 158 |
): |
| 159 |
with self.subTest(variable=variable): |
| 160 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 161 |
|
| 162 |
self.assertIsInstance(cmd.drgn_option.globals["index"], int) |
| 163 |
self.assertIsInstance(cmd.drgn_option.globals["decoded_flags"], str) |
| 164 |
|
| 165 |
def _test_R(self, arg_fn, expect_no_cache, expect_cache, test_foreach=False): |
| 166 |
with tempfile.NamedTemporaryFile() as f: |
| 167 |
arg = arg_fn(f) |
| 168 |
|
| 169 |
cmd = self.check_crash_command(f"files -R {arg} {os.getpid()}") |
| 170 |
cmds = [cmd] |
| 171 |
if test_foreach: |
| 172 |
foreach_cmd = self.check_crash_command( |
| 173 |
f"foreach {os.getpid()} files -R {arg}", mode="capture" |
| 174 |
) |
| 175 |
cmds.append(foreach_cmd) |
| 176 |
|
| 177 |
for c in cmds: |
| 178 |
if expect_no_cache: |
| 179 |
self.assertRegex( |
| 180 |
c.stdout, |
| 181 |
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9a-f]+\s+REG\s+{re.escape(f.name)}", |
| 182 |
) |
| 183 |
else: |
| 184 |
self.assertFalse(c.stdout) |
| 185 |
self._test_drgn_common(cmd) |
| 186 |
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool) |
| 187 |
if test_foreach: |
| 188 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 189 |
|
| 190 |
cmd = self.check_crash_command(f"files -c -R {arg} {os.getpid()}") |
| 191 |
cmds = [cmd] |
| 192 |
if test_foreach: |
| 193 |
foreach_cmd = self.check_crash_command( |
| 194 |
f"foreach {os.getpid()} files -c -R {arg}", mode="capture" |
| 195 |
) |
| 196 |
cmds.append(foreach_cmd) |
| 197 |
|
| 198 |
for c in cmds: |
| 199 |
if expect_cache: |
| 200 |
self.assertRegex( |
| 201 |
c.stdout, |
| 202 |
rf"(?m)^\s*{f.fileno()}\s+[0-9a-f]+\s+[0-9a-f]+\s+[0-9]+\s+REG\s+{re.escape(f.name)}", |
| 203 |
) |
| 204 |
else: |
| 205 |
self.assertFalse(c.stdout) |
| 206 |
self._test_drgn_common(cmd, cache=True) |
| 207 |
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool) |
| 208 |
if test_foreach: |
| 209 |
self.assertEqual(cmd.drgn_option.stdout, foreach_cmd.drgn_option.stdout) |
| 210 |
|
| 211 |
def test_R_fd(self): |
| 212 |
self._test_R(lambda f: str(f.fileno()), True, True, True) |
| 213 |
|
| 214 |
def test_R_filename(self): |
| 215 |
self._test_R(lambda f: f.name, True, True) |
| 216 |
|
| 217 |
def test_R_dentry(self): |
| 218 |
self._test_R( |
| 219 |
lambda f: hex( |
| 220 |
fget(find_task(self.prog, os.getpid()), f.fileno()).f_path.dentry |
| 221 |
), |
| 222 |
True, |
| 223 |
False, |
| 224 |
) |
| 225 |
|
| 226 |
def test_R_inode(self): |
| 227 |
self._test_R( |
| 228 |
lambda f: hex(fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode), |
| 229 |
True, |
| 230 |
True, |
| 231 |
) |
| 232 |
|
| 233 |
def test_R_address_space(self): |
| 234 |
self._test_R( |
| 235 |
lambda f: hex( |
| 236 |
fget(find_task(self.prog, os.getpid()), f.fileno()).f_inode.i_mapping |
| 237 |
), |
| 238 |
False, |
| 239 |
True, |
| 240 |
) |
| 241 |
|
| 242 |
def test_R_file(self): |
| 243 |
self._test_R( |
| 244 |
lambda f: hex(fget(find_task(self.prog, os.getpid()), f.fileno())), |
| 245 |
True, |
| 246 |
False, |
| 247 |
) |
| 248 |
|
| 249 |
def test_R_cwd(self): |
| 250 |
with tempfile.TemporaryDirectory() as tmp_dir: |
| 251 |
with fork_and_stop(os.chdir, tmp_dir) as (pid, _): |
| 252 |
cmd = self.check_crash_command(f"files -R {tmp_dir} {pid}") |
| 253 |
|
| 254 |
self.assertRegex(cmd.stdout, rf"CWD: {re.escape(tmp_dir)}\n$") |
| 255 |
|
| 256 |
self.assertEqual(cmd.drgn_option.globals["cwd"], os.fsencode(tmp_dir)) |
| 257 |
self.assertIsInstance(cmd.drgn_option.globals["is_match"], bool) |
| 258 |
|
| 259 |
def test_no_files(self): |
| 260 |
cmd = self.check_crash_command(f"files {hex(self.prog['init_task'].address_)}") |
| 261 |
self.assertRegex(cmd.stdout, r"\nNo open files\n$") |
| 262 |
|
| 263 |
|
| 264 |
class TestMount(CrashCommandTestCase): |
| 265 |
def test_no_options(self): |
| 266 |
self.run_crash_command("set 1") |
| 267 |
cmd = self.check_crash_command("mount") |
| 268 |
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc") |
| 269 |
self.assertIn("for_each_mount()", cmd.drgn_option.stdout) |
| 270 |
self.assertNotIn("mnt_ns", cmd.drgn_option.globals) |
| 271 |
|
| 272 |
def test_no_options_in_namespace(self): |
| 273 |
with fork_and_stop(unshare, CLONE_NEWNS) as (pid, _): |
| 274 |
self.run_crash_command(f"set {pid}") |
| 275 |
cmd = self.check_crash_command("mount") |
| 276 |
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout) |
| 277 |
self.assertEqual( |
| 278 |
cmd.drgn_option.globals["mnt_ns"], |
| 279 |
find_task(self.prog, pid).nsproxy.mnt_ns, |
| 280 |
) |
| 281 |
|
| 282 |
def test_n_pid(self): |
| 283 |
cmd = self.check_crash_command("mount -n 1") |
| 284 |
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc") |
| 285 |
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout) |
| 286 |
self.assertEqual( |
| 287 |
cmd.drgn_option.globals["mnt_ns"], find_task(self.prog, 1).nsproxy.mnt_ns |
| 288 |
) |
| 289 |
|
| 290 |
def test_n_task(self): |
| 291 |
task = self.prog["init_task"].address_of_() |
| 292 |
cmd = self.check_crash_command(f"mount -n {hex(task)}") |
| 293 |
self.assertRegex(cmd.stdout, r"(?m)^\s*[0-9a-f]+\s+[0-9a-f]+.*proc") |
| 294 |
self.assertIn("for_each_mount(mnt_ns)", cmd.drgn_option.stdout) |
| 295 |
self.assertEqual( |
| 296 |
cmd.drgn_option.globals["mnt_ns"], self.prog["init_task"].nsproxy.mnt_ns |
| 297 |
) |