| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import re |
| 5 |
|
| 6 |
from drgn import Object |
| 7 |
from drgn.helpers.linux.ipc import find_sysv_msg_queue, find_sysv_shm |
| 8 |
from tests.linux_kernel.crash_commands import CrashCommandTestCase |
| 9 |
from tests.linux_kernel.helpers.test_ipc import tmp_sysv_ipc |
| 10 |
|
| 11 |
|
| 12 |
class TestIpcs(CrashCommandTestCase): |
| 13 |
@classmethod |
| 14 |
def setUpClass(cls): |
| 15 |
super().setUpClass() |
| 16 |
cls.run_crash_command("set -p") |
| 17 |
cls.ipcs = cls.enterClassContext(tmp_sysv_ipc()) |
| 18 |
|
| 19 |
def test_ipcs(self): |
| 20 |
cmd = self.check_crash_command("ipcs") |
| 21 |
found_shm = {} |
| 22 |
found_sem = {} |
| 23 |
found_msg = {} |
| 24 |
for line in cmd.stdout.splitlines(): |
| 25 |
if line.startswith("SHMID_KERNEL"): |
| 26 |
found = found_shm |
| 27 |
elif line.startswith("SEM_ARRAY"): |
| 28 |
found = found_sem |
| 29 |
elif line.startswith("MSG_QUEUE"): |
| 30 |
found = found_msg |
| 31 |
elif match := re.match(r"[0-9a-f]+\s+([0-9a-f]+)\s+([0-9]+)", line): |
| 32 |
found[(int(match.group(1), 16), int(match.group(2)))] = line |
| 33 |
|
| 34 |
for key, id in self.ipcs.shm: |
| 35 |
self.assertIn((key, id), found_shm) |
| 36 |
self.assertIn("SHM_LOCKED", found_shm[self.ipcs.shm[0]]) |
| 37 |
|
| 38 |
for key, id in self.ipcs.sem: |
| 39 |
self.assertIn((key, id), found_sem) |
| 40 |
|
| 41 |
for key, id in self.ipcs.msg: |
| 42 |
self.assertIn((key, id), found_msg) |
| 43 |
|
| 44 |
self.assertEqual( |
| 45 |
cmd.drgn_option.globals["shm"].type_.type_name(), "struct shmid_kernel *" |
| 46 |
) |
| 47 |
self.assertEqual( |
| 48 |
cmd.drgn_option.globals["sem_array"].type_.type_name(), "struct sem_array *" |
| 49 |
) |
| 50 |
self.assertEqual( |
| 51 |
cmd.drgn_option.globals["msg_queue"].type_.type_name(), "struct msg_queue *" |
| 52 |
) |
| 53 |
|
| 54 |
for variable in ( |
| 55 |
"key", |
| 56 |
"id", |
| 57 |
"perms", |
| 58 |
"bytes", |
| 59 |
"nattch", |
| 60 |
"nsems", |
| 61 |
"used_bytes", |
| 62 |
"messages", |
| 63 |
): |
| 64 |
with self.subTest(variable=variable): |
| 65 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 66 |
|
| 67 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 68 |
self.assertIsInstance(cmd.drgn_option.globals["status"], str) |
| 69 |
|
| 70 |
def test_s(self): |
| 71 |
cmd = self.check_crash_command("ipcs -s") |
| 72 |
self.assertIn("SEM_ARRAY", cmd.stdout) |
| 73 |
self.assertNotIn("SHMID_KERNEL", cmd.stdout) |
| 74 |
self.assertNotIn("MSG_QUEUE", cmd.stdout) |
| 75 |
|
| 76 |
self.assertEqual( |
| 77 |
cmd.drgn_option.globals["sem_array"].type_.type_name(), "struct sem_array *" |
| 78 |
) |
| 79 |
self.assertNotIn("shm", cmd.drgn_option.globals) |
| 80 |
self.assertNotIn("msg_queue", cmd.drgn_option.globals) |
| 81 |
|
| 82 |
for variable in ( |
| 83 |
"key", |
| 84 |
"id", |
| 85 |
"perms", |
| 86 |
"nsems", |
| 87 |
): |
| 88 |
with self.subTest(variable=variable): |
| 89 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 90 |
|
| 91 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 92 |
|
| 93 |
self.assertNotIn("bytes", cmd.drgn_option.globals) |
| 94 |
self.assertNotIn("nattch", cmd.drgn_option.globals) |
| 95 |
self.assertNotIn("used_bytes", cmd.drgn_option.globals) |
| 96 |
self.assertNotIn("messages", cmd.drgn_option.globals) |
| 97 |
self.assertNotIn("status", cmd.drgn_option.globals) |
| 98 |
|
| 99 |
def test_m(self): |
| 100 |
cmd = self.check_crash_command("ipcs -m") |
| 101 |
self.assertIn("SHMID_KERNEL", cmd.stdout) |
| 102 |
self.assertNotIn("SEM_ARRAY", cmd.stdout) |
| 103 |
self.assertNotIn("MSG_QUEUE", cmd.stdout) |
| 104 |
|
| 105 |
self.assertEqual( |
| 106 |
cmd.drgn_option.globals["shm"].type_.type_name(), "struct shmid_kernel *" |
| 107 |
) |
| 108 |
self.assertNotIn("sem_array", cmd.drgn_option.globals) |
| 109 |
self.assertNotIn("msg_queue", cmd.drgn_option.globals) |
| 110 |
|
| 111 |
for variable in ( |
| 112 |
"key", |
| 113 |
"id", |
| 114 |
"perms", |
| 115 |
"bytes", |
| 116 |
"nattch", |
| 117 |
): |
| 118 |
with self.subTest(variable=variable): |
| 119 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 120 |
|
| 121 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 122 |
self.assertIsInstance(cmd.drgn_option.globals["status"], str) |
| 123 |
|
| 124 |
self.assertNotIn("nsems", cmd.drgn_option.globals) |
| 125 |
self.assertNotIn("used_bytes", cmd.drgn_option.globals) |
| 126 |
self.assertNotIn("messages", cmd.drgn_option.globals) |
| 127 |
|
| 128 |
def test_q(self): |
| 129 |
cmd = self.check_crash_command("ipcs -q") |
| 130 |
self.assertIn("MSG_QUEUE", cmd.stdout) |
| 131 |
self.assertNotIn("SHMID_KERNEL", cmd.stdout) |
| 132 |
self.assertNotIn("SEM_ARRAY", cmd.stdout) |
| 133 |
|
| 134 |
self.assertEqual( |
| 135 |
cmd.drgn_option.globals["msg_queue"].type_.type_name(), "struct msg_queue *" |
| 136 |
) |
| 137 |
self.assertNotIn("shm", cmd.drgn_option.globals) |
| 138 |
self.assertNotIn("sem_array", cmd.drgn_option.globals) |
| 139 |
|
| 140 |
for variable in ( |
| 141 |
"key", |
| 142 |
"id", |
| 143 |
"perms", |
| 144 |
"used_bytes", |
| 145 |
"messages", |
| 146 |
): |
| 147 |
with self.subTest(variable=variable): |
| 148 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 149 |
|
| 150 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 151 |
|
| 152 |
self.assertNotIn("bytes", cmd.drgn_option.globals) |
| 153 |
self.assertNotIn("nattch", cmd.drgn_option.globals) |
| 154 |
self.assertNotIn("status", cmd.drgn_option.globals) |
| 155 |
self.assertNotIn("nsems", cmd.drgn_option.globals) |
| 156 |
|
| 157 |
def test_id(self): |
| 158 |
shm_key, shm_id = self.ipcs.shm[0] |
| 159 |
|
| 160 |
cmd = self.check_crash_command(f"ipcs {shm_id}") |
| 161 |
found_shm = {} |
| 162 |
found_sem = {} |
| 163 |
found_msg = {} |
| 164 |
for line in cmd.stdout.splitlines(): |
| 165 |
if line.startswith("SHMID_KERNEL"): |
| 166 |
found = found_shm |
| 167 |
elif line.startswith("SEM_ARRAY"): |
| 168 |
found = found_sem |
| 169 |
elif line.startswith("MSG_QUEUE"): |
| 170 |
found = found_msg |
| 171 |
elif match := re.match(r"[0-9a-f]+\s+([0-9a-f]+)\s+([0-9]+)", line): |
| 172 |
found[(int(match.group(1), 16), int(match.group(2)))] = line |
| 173 |
|
| 174 |
for key, id in self.ipcs.shm: |
| 175 |
if (key, id) == (shm_key, shm_id): |
| 176 |
self.assertIn((key, id), found_shm) |
| 177 |
else: |
| 178 |
self.assertNotIn((key, id), found_shm) |
| 179 |
self.assertIn("SHM_LOCKED", found_shm[self.ipcs.shm[0]]) |
| 180 |
|
| 181 |
self.assertEqual( |
| 182 |
cmd.drgn_option.globals["shm"], find_sysv_shm(self.prog, shm_id) |
| 183 |
) |
| 184 |
|
| 185 |
for variable in ( |
| 186 |
"key", |
| 187 |
"id", |
| 188 |
"perms", |
| 189 |
"bytes", |
| 190 |
"nattch", |
| 191 |
): |
| 192 |
with self.subTest(variable=variable): |
| 193 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 194 |
|
| 195 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 196 |
self.assertIsInstance(cmd.drgn_option.globals["status"], str) |
| 197 |
|
| 198 |
def test_addr(self): |
| 199 |
msg_key, msg_id = self.ipcs.msg[0] |
| 200 |
msg_queue = find_sysv_msg_queue(self.prog, msg_id) |
| 201 |
|
| 202 |
cmd = self.check_crash_command(f"ipcs {hex(msg_queue)}") |
| 203 |
found_shm = {} |
| 204 |
found_sem = {} |
| 205 |
found_msg = {} |
| 206 |
for line in cmd.stdout.splitlines(): |
| 207 |
if line.startswith("SHMID_KERNEL"): |
| 208 |
found = found_shm |
| 209 |
elif line.startswith("SEM_ARRAY"): |
| 210 |
found = found_sem |
| 211 |
elif line.startswith("MSG_QUEUE"): |
| 212 |
found = found_msg |
| 213 |
elif match := re.match(r"[0-9a-f]+\s+([0-9a-f]+)\s+([0-9]+)", line): |
| 214 |
found[(int(match.group(1), 16), int(match.group(2)))] = line |
| 215 |
|
| 216 |
for key, id in self.ipcs.msg: |
| 217 |
if (key, id) == (msg_key, msg_id): |
| 218 |
self.assertIn((key, id), found_msg) |
| 219 |
else: |
| 220 |
self.assertNotIn((key, id), found_msg) |
| 221 |
|
| 222 |
self.assertFalse(found_shm) |
| 223 |
self.assertFalse(found_sem) |
| 224 |
|
| 225 |
self.assertEqual( |
| 226 |
cmd.drgn_option.globals["msg_queue"].type_.type_name(), "struct msg_queue *" |
| 227 |
) |
| 228 |
|
| 229 |
for variable in ( |
| 230 |
"key", |
| 231 |
"id", |
| 232 |
"perms", |
| 233 |
"used_bytes", |
| 234 |
"messages", |
| 235 |
): |
| 236 |
with self.subTest(variable=variable): |
| 237 |
self.assertIsInstance(cmd.drgn_option.globals[variable], Object) |
| 238 |
|
| 239 |
self.assertIsInstance(cmd.drgn_option.globals["uid"], int) |
| 240 |
|
| 241 |
self.assertNotIn("bytes", cmd.drgn_option.globals) |
| 242 |
self.assertNotIn("nattch", cmd.drgn_option.globals) |
| 243 |
self.assertNotIn("status", cmd.drgn_option.globals) |
| 244 |
self.assertNotIn("nsems", cmd.drgn_option.globals) |
| 245 |
|
| 246 |
def test_n_pid(self): |
| 247 |
cmd = self.check_crash_command("ipcs -n 1") |
| 248 |
self.assertIn("SHM_LOCKED", cmd.stdout) |
| 249 |
self.assertIn("for_each_sysv_shm(ipc_ns)", cmd.drgn_option.stdout) |
| 250 |
self.assertEqual( |
| 251 |
cmd.drgn_option.globals["ipc_ns"], self.prog["init_ipc_ns"].address_of_() |
| 252 |
) |
| 253 |
|
| 254 |
def test_n_task(self): |
| 255 |
task = self.prog["init_task"].address_of_() |
| 256 |
cmd = self.check_crash_command(f"ipcs -n {hex(task)}") |
| 257 |
self.assertIn("SHM_LOCKED", cmd.stdout) |
| 258 |
self.assertIn("for_each_sysv_shm(ipc_ns)", cmd.drgn_option.stdout) |
| 259 |
self.assertEqual( |
| 260 |
cmd.drgn_option.globals["ipc_ns"], self.prog["init_ipc_ns"].address_of_() |
| 261 |
) |
| 262 |
|
| 263 |
def test_n_and_id(self): |
| 264 |
shm_key, shm_id = self.ipcs.shm[0] |
| 265 |
cmd = self.check_crash_command(f"ipcs -n 1 {shm_id}") |
| 266 |
self.assertIn("SHM_LOCKED", cmd.stdout) |
| 267 |
self.assertIn("find_sysv_shm(ipc_ns,", cmd.drgn_option.stdout) |
| 268 |
self.assertEqual( |
| 269 |
cmd.drgn_option.globals["ipc_ns"], self.prog["init_ipc_ns"].address_of_() |
| 270 |
) |
| 271 |
|
| 272 |
def test_n_and_addr(self): |
| 273 |
msg_key, msg_id = self.ipcs.msg[0] |
| 274 |
msg_queue = find_sysv_msg_queue(self.prog, msg_id) |
| 275 |
|
| 276 |
cmd = self.check_crash_command(f"ipcs -n 1 {hex(msg_queue)}") |
| 277 |
self.assertIn("for_each_sysv_msg_queue(ipc_ns)", cmd.drgn_option.stdout) |
| 278 |
self.assertEqual( |
| 279 |
cmd.drgn_option.globals["ipc_ns"], self.prog["init_ipc_ns"].address_of_() |
| 280 |
) |