| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import contextlib |
| 5 |
import ctypes |
| 6 |
import enum |
| 7 |
import errno |
| 8 |
from fcntl import ioctl |
| 9 |
import functools |
| 10 |
import mmap |
| 11 |
import os |
| 12 |
from pathlib import Path |
| 13 |
import pickle |
| 14 |
import random |
| 15 |
import re |
| 16 |
import signal |
| 17 |
import socket |
| 18 |
import stat |
| 19 |
import string |
| 20 |
import struct |
| 21 |
import subprocess |
| 22 |
import sys |
| 23 |
import time |
| 24 |
import traceback |
| 25 |
from typing import NamedTuple |
| 26 |
import unittest |
| 27 |
|
| 28 |
from _drgn_util.platform import _IO, _IOR, NORMALIZED_MACHINE_NAME, SYS |
| 29 |
import drgn |
| 30 |
from tests import TestCase |
| 31 |
from util import verrevcmp |
| 32 |
|
| 33 |
try: |
| 34 |
import pyroute2 |
| 35 |
|
| 36 |
have_pyroute2 = True |
| 37 |
|
| 38 |
# Before Pyroute2 commit 1eb08312de30 ("iproute/linux: try to improve flags |
| 39 |
# when sending del messages") (in v0.6.10), Pyroute2 incorrectly passes the |
| 40 |
# NLM_F_CREATE and NLM_F_EXCL flags to deletion requests, which fails with |
| 41 |
# ENOTSUP since Linux kernel commit 545528d78855 ("net: netlink: add |
| 42 |
# NLM_F_BULK delete request modifier") (in v5.19). Monkey patch the put() |
| 43 |
# method to strip these flags. |
| 44 |
if verrevcmp(getattr(pyroute2, "__version__", "0"), "0.6.10") < 0: |
| 45 |
# RTNL_API was named IPRouteMixin before Pyroute2 0.5.2 and was in |
| 46 |
# pyroute2.iproute instead of pyroute2.iproute.linux. |
| 47 |
try: |
| 48 |
from pyroute2.iproute.linux import RTNL_API as _rtnl_api |
| 49 |
except ImportError: |
| 50 |
from pyroute2.iproute import IPRouteMixin as _rtnl_api |
| 51 |
|
| 52 |
def _fixed_put(self, msg, msg_type, msg_flags=0x1, *args, **kwargs): |
| 53 |
# RTM_DEL* message types have msg_type % 4 == 1. |
| 54 |
if msg_type >= 16 and msg_type % 4 == 1: |
| 55 |
msg_flags &= ~0x600 # NLM_F_CREATE | NLM_F_EXCL |
| 56 |
return super(_rtnl_api, self).put(msg, msg_type, msg_flags, *args, **kwargs) |
| 57 |
|
| 58 |
_rtnl_api.put = _fixed_put |
| 59 |
|
| 60 |
# Before Pyroute2 commit f0df9a49b41f ("Fix "failed to open netns" error in |
| 61 |
# RISCV64.") (in v0.7.10), Pyroute2 uses the wrong syscall number on |
| 62 |
# RISC-V. Monkey patch the correct number. |
| 63 |
if verrevcmp( |
| 64 |
getattr(pyroute2, "__version__", "0"), "0.7.10" |
| 65 |
) < 0 and os.uname().machine.startswith("riscv"): |
| 66 |
import pyroute2.netns |
| 67 |
|
| 68 |
pyroute2.netns.__NR_setns = 268 |
| 69 |
except ImportError: |
| 70 |
have_pyroute2 = False |
| 71 |
|
| 72 |
|
| 73 |
class LinuxKernelTestCase(TestCase): |
| 74 |
prog = None |
| 75 |
skip_reason = None |
| 76 |
|
| 77 |
@staticmethod |
| 78 |
def _load_debug_info(prog): |
| 79 |
paths = [] |
| 80 |
try: |
| 81 |
paths.append(os.environ["DRGN_TEST_KMOD"]) |
| 82 |
except KeyError: |
| 83 |
pass |
| 84 |
prog.load_debug_info(paths, True) |
| 85 |
|
| 86 |
@classmethod |
| 87 |
def setUpClass(cls): |
| 88 |
super().setUpClass() |
| 89 |
# We only want to create the Program once for all tests, so it's cached |
| 90 |
# as a class variable (in the base class). If we can't run these tests |
| 91 |
# for whatever reason, we also cache that. |
| 92 |
if LinuxKernelTestCase.prog is not None: |
| 93 |
return |
| 94 |
if LinuxKernelTestCase.skip_reason is None: |
| 95 |
try: |
| 96 |
run_tests = int(os.environ["DRGN_RUN_LINUX_KERNEL_TESTS"]) != 0 |
| 97 |
except (KeyError, ValueError): |
| 98 |
run_tests = True |
| 99 |
force_run = False |
| 100 |
else: |
| 101 |
force_run = run_tests |
| 102 |
if run_tests: |
| 103 |
prog = drgn.Program() |
| 104 |
try: |
| 105 |
prog.set_kernel() |
| 106 |
except PermissionError: |
| 107 |
if force_run: |
| 108 |
raise |
| 109 |
LinuxKernelTestCase.skip_reason = ( |
| 110 |
"Linux kernel tests must be run as root " |
| 111 |
"(run with env DRGN_RUN_LINUX_KERNEL_TESTS=1 to force)" |
| 112 |
) |
| 113 |
except (FileNotFoundError, ValueError): |
| 114 |
if force_run: |
| 115 |
raise |
| 116 |
LinuxKernelTestCase.skip_reason = ( |
| 117 |
"Linux kernel tests require /proc/kcore " |
| 118 |
"(run with env DRGN_RUN_LINUX_KERNEL_TESTS=1 to force)" |
| 119 |
) |
| 120 |
else: |
| 121 |
# Load modules that are used by test cases. |
| 122 |
subprocess.check_call( |
| 123 |
["modprobe", "-a", "btrfs", "configs", "loop"] |
| 124 |
) |
| 125 |
try: |
| 126 |
cls._load_debug_info(prog) |
| 127 |
LinuxKernelTestCase.prog = prog |
| 128 |
return |
| 129 |
except drgn.MissingDebugInfoError as e: |
| 130 |
if force_run: |
| 131 |
raise |
| 132 |
LinuxKernelTestCase.skip_reason = str(e) |
| 133 |
else: |
| 134 |
LinuxKernelTestCase.skip_reason = "env DRGN_RUN_LINUX_KERNEL_TESTS=0" |
| 135 |
raise unittest.SkipTest(LinuxKernelTestCase.skip_reason) |
| 136 |
|
| 137 |
|
| 138 |
skip_unless_have_test_kmod = unittest.skipUnless( |
| 139 |
"DRGN_TEST_KMOD" in os.environ, "test requires drgn_test Linux kernel module" |
| 140 |
) |
| 141 |
|
| 142 |
skip_unless_have_test_disk = unittest.skipUnless( |
| 143 |
"DRGN_TEST_DISK" in os.environ, |
| 144 |
"test requires disk to overwrite; set DRGN_TEST_DISK environment variable (e.g., DRGN_TEST_DISK=/dev/vda)", |
| 145 |
) |
| 146 |
|
| 147 |
# Please keep this in sync with docs/support_matrix.rst. |
| 148 |
HAVE_FULL_MM_SUPPORT = NORMALIZED_MACHINE_NAME in ( |
| 149 |
"aarch64", |
| 150 |
"arm", |
| 151 |
"ppc64", |
| 152 |
"s390x", |
| 153 |
"x86_64", |
| 154 |
) |
| 155 |
|
| 156 |
skip_unless_have_full_mm_support = unittest.skipUnless( |
| 157 |
HAVE_FULL_MM_SUPPORT, |
| 158 |
f"mm support is not implemented for {NORMALIZED_MACHINE_NAME}", |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def skip_if_slob(f): |
| 163 |
@skip_unless_have_test_kmod |
| 164 |
@functools.wraps(f) |
| 165 |
def wrapper(self, *args, **kwargs): |
| 166 |
if self.prog["drgn_test_slob"]: |
| 167 |
self.skipTest("test does not support SLOB") |
| 168 |
|
| 169 |
return wrapper |
| 170 |
|
| 171 |
|
| 172 |
def skip_if_highmem(f): |
| 173 |
@functools.wraps(f) |
| 174 |
def wrapper(self, *args, **kwargs): |
| 175 |
if self.prog["max_low_pfn"] < self.prog["max_pfn"]: |
| 176 |
self.skipTest("high memory is not supported") |
| 177 |
|
| 178 |
return wrapper |
| 179 |
|
| 180 |
|
| 181 |
# This is a false positive if CONFIG_HIGHMEM=y and CONFIG_HIGHPTE=n, but it's |
| 182 |
# good enough for now. |
| 183 |
skip_if_highpte = skip_if_highmem |
| 184 |
|
| 185 |
# Please keep this in sync with docs/support_matrix.rst. |
| 186 |
skip_unless_have_stack_tracing = unittest.skipUnless( |
| 187 |
NORMALIZED_MACHINE_NAME in {"aarch64", "arm", "ppc64", "s390x", "x86_64"}, |
| 188 |
f"stack tracing is not implemented for {NORMALIZED_MACHINE_NAME}", |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
skip_unless_have_memory_hotplug = unittest.skipUnless( |
| 193 |
Path("/sys/bus/memory").exists(), "memory hotplug is not supported" |
| 194 |
) |
| 195 |
|
| 196 |
|
| 197 |
skip_unless_have_pyroute2 = unittest.skipUnless(have_pyroute2, "pyroute2 not found") |
| 198 |
|
| 199 |
|
| 200 |
# PRNG used by the test kernel module. |
| 201 |
def prng32(seed): |
| 202 |
seed = seed.encode("ascii") |
| 203 |
assert len(seed) == 4 |
| 204 |
x = int.from_bytes(seed, "big") |
| 205 |
while True: |
| 206 |
x ^= (x << 13) & 0xFFFFFFFF |
| 207 |
x ^= x >> 17 |
| 208 |
x ^= (x << 5) & 0xFFFFFFFF |
| 209 |
yield x |
| 210 |
|
| 211 |
|
| 212 |
def wait_until(fn, *args, **kwds): |
| 213 |
TIMEOUT = 5 |
| 214 |
deadline = time.monotonic() + TIMEOUT |
| 215 |
sleep = 1e-6 |
| 216 |
while True: |
| 217 |
if fn(*args, **kwds): |
| 218 |
break |
| 219 |
now = time.monotonic() |
| 220 |
if now >= deadline: |
| 221 |
raise Exception(f"condition was not met in {TIMEOUT} seconds") |
| 222 |
time.sleep(min(deadline - now, sleep)) |
| 223 |
sleep *= 2 |
| 224 |
|
| 225 |
|
| 226 |
def proc_state(pid): |
| 227 |
with open(f"/proc/{pid}/status", "r") as f: |
| 228 |
return re.search(r"State:\s*(\S)", f.read(), re.M).group(1) |
| 229 |
|
| 230 |
|
| 231 |
# Context manager that: |
| 232 |
# 1. Forks a process which optionally calls a function and then stops with |
| 233 |
# SIGSTOP. |
| 234 |
# 2. Waits for the child process to stop and unschedule. |
| 235 |
# 3. Returns the PID of the child process, and return value of the function if |
| 236 |
# provided, from __enter__(). |
| 237 |
# 4. Kills the child process in __exit__(). |
| 238 |
@contextlib.contextmanager |
| 239 |
def fork_and_stop(fn=None, *args, **kwds): |
| 240 |
with contextlib.ExitStack() as exit_stack: |
| 241 |
if fn: |
| 242 |
r, w = os.pipe() |
| 243 |
pipe_r = exit_stack.enter_context(open(r, "rb")) |
| 244 |
pipe_w = exit_stack.enter_context(open(w, "wb")) |
| 245 |
pid = os.fork() |
| 246 |
try: |
| 247 |
if pid == 0: |
| 248 |
try: |
| 249 |
if fn: |
| 250 |
pipe_r.close() |
| 251 |
ret = fn(*args, **kwds) |
| 252 |
pickle.dump(ret, pipe_w) |
| 253 |
pipe_w.close() |
| 254 |
while True: |
| 255 |
os.kill(os.getpid(), signal.SIGSTOP) |
| 256 |
finally: |
| 257 |
traceback.print_exc() |
| 258 |
sys.stderr.flush() |
| 259 |
os._exit(1) |
| 260 |
|
| 261 |
if fn: |
| 262 |
pipe_w.close() |
| 263 |
ret = pickle.load(pipe_r) |
| 264 |
|
| 265 |
_, status = os.waitpid(pid, os.WUNTRACED) |
| 266 |
if not os.WIFSTOPPED(status): |
| 267 |
raise Exception("child process exited") |
| 268 |
# waitpid() can return as soon as the stopped flag is set on the |
| 269 |
# process; see wait_task_stopped() in the Linux kernel source code: |
| 270 |
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/exit.c?h=v6.17-rc5#n1313 |
| 271 |
# However, the process may still be on the CPU for a short window; |
| 272 |
# see do_signal_stop(): |
| 273 |
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/signal.c?h=v6.17-rc5#n2617 |
| 274 |
# So, we need to wait for it to fully unschedule. /proc/pid/syscall |
| 275 |
# contains "running" unless the process is unscheduled; see |
| 276 |
# proc_pid_syscall(): |
| 277 |
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/proc/base.c?h=v6.17-rc5#n675 |
| 278 |
# task_current_syscall(): |
| 279 |
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/syscall.c?h=v6.17-rc5#n69 |
| 280 |
# and wait_task_inactive(): |
| 281 |
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/sched/core.c?h=v6.17-rc5#n2257 |
| 282 |
syscall_path = Path(f"/proc/{pid}/syscall") |
| 283 |
while syscall_path.read_text() == "running\n": |
| 284 |
os.sched_yield() |
| 285 |
|
| 286 |
if fn: |
| 287 |
yield pid, ret |
| 288 |
else: |
| 289 |
yield pid |
| 290 |
finally: |
| 291 |
os.kill(pid, signal.SIGKILL) |
| 292 |
os.waitpid(pid, 0) |
| 293 |
|
| 294 |
|
| 295 |
def parse_range_list(s): |
| 296 |
values = set() |
| 297 |
s = s.strip() |
| 298 |
if s: |
| 299 |
for range_str in s.split(","): |
| 300 |
first, sep, last = range_str.partition("-") |
| 301 |
if sep: |
| 302 |
values.update(range(int(first), int(last) + 1)) |
| 303 |
else: |
| 304 |
values.add(int(first)) |
| 305 |
return values |
| 306 |
|
| 307 |
|
| 308 |
def online_cpus(): |
| 309 |
return parse_range_list(Path("/sys/devices/system/cpu/online").read_text()) |
| 310 |
|
| 311 |
|
| 312 |
def possible_cpus(): |
| 313 |
return parse_range_list(Path("/sys/devices/system/cpu/possible").read_text()) |
| 314 |
|
| 315 |
|
| 316 |
def meminfo_field_in_pages(name: str) -> int: |
| 317 |
return ( |
| 318 |
int( |
| 319 |
re.search( |
| 320 |
rf"^{name}:\s*([0-9]+)\s*kB", |
| 321 |
Path("/proc/meminfo").read_text(), |
| 322 |
flags=re.M, |
| 323 |
).group(1) |
| 324 |
) |
| 325 |
* 1024 |
| 326 |
// mmap.PAGESIZE |
| 327 |
) |
| 328 |
|
| 329 |
|
| 330 |
@contextlib.contextmanager |
| 331 |
def temp_netns(): |
| 332 |
while True: |
| 333 |
try: |
| 334 |
name = "".join(random.choice(string.ascii_letters) for _ in range(16)) |
| 335 |
ns = pyroute2.NetNS(name, flags=os.O_CREAT | os.O_EXCL) |
| 336 |
break |
| 337 |
except FileExistsError: |
| 338 |
pass |
| 339 |
try: |
| 340 |
with ns: |
| 341 |
yield name, ns |
| 342 |
finally: |
| 343 |
ns.remove() |
| 344 |
|
| 345 |
|
| 346 |
_c = ctypes.CDLL(None, use_errno=True) |
| 347 |
|
| 348 |
_mount = _c.mount |
| 349 |
_mount.restype = ctypes.c_int |
| 350 |
_mount.argtypes = [ |
| 351 |
ctypes.c_char_p, |
| 352 |
ctypes.c_char_p, |
| 353 |
ctypes.c_char_p, |
| 354 |
ctypes.c_ulong, |
| 355 |
ctypes.c_void_p, |
| 356 |
] |
| 357 |
MS_RDONLY = 1 |
| 358 |
MS_NOSUID = 2 |
| 359 |
MS_NODEV = 4 |
| 360 |
MS_NOEXEC = 8 |
| 361 |
MS_SYNCHRONOUS = 16 |
| 362 |
MS_REMOUNT = 32 |
| 363 |
MS_MANDLOCK = 64 |
| 364 |
MS_DIRSYNC = 128 |
| 365 |
MS_NOSYMFOLLOW = 256 |
| 366 |
MS_NOATIME = 1024 |
| 367 |
MS_NODIRATIME = 2048 |
| 368 |
MS_BIND = 4096 |
| 369 |
MS_MOVE = 8192 |
| 370 |
MS_REC = 16384 |
| 371 |
MS_SILENT = 32768 |
| 372 |
MS_POSIXACL = 1 << 16 |
| 373 |
MS_UNBINDABLE = 1 << 17 |
| 374 |
MS_PRIVATE = 1 << 18 |
| 375 |
MS_SLAVE = 1 << 19 |
| 376 |
MS_SHARED = 1 << 20 |
| 377 |
MS_RELATIME = 1 << 21 |
| 378 |
MS_KERNMOUNT = 1 << 22 |
| 379 |
MS_I_VERSION = 1 << 23 |
| 380 |
MS_STRICTATIME = 1 << 24 |
| 381 |
MS_LAZYTIME = 1 << 25 |
| 382 |
|
| 383 |
|
| 384 |
def _check_ctypes_syscall(ret, *args): |
| 385 |
if ret == -1: |
| 386 |
errno = ctypes.get_errno() |
| 387 |
raise OSError(errno, os.strerror(errno), *args) |
| 388 |
return ret |
| 389 |
|
| 390 |
|
| 391 |
def mount(source, target, fstype, flags=0, data=None): |
| 392 |
_check_ctypes_syscall( |
| 393 |
_mount( |
| 394 |
os.fsencode(source), |
| 395 |
os.fsencode(target), |
| 396 |
fstype.encode(), |
| 397 |
flags, |
| 398 |
None if data is None else data.encode(), |
| 399 |
), |
| 400 |
source, |
| 401 |
None, |
| 402 |
target, |
| 403 |
) |
| 404 |
|
| 405 |
|
| 406 |
_umount2 = _c.umount2 |
| 407 |
_umount2.restype = ctypes.c_int |
| 408 |
_umount2.argtypes = [ctypes.c_char_p, ctypes.c_int] |
| 409 |
|
| 410 |
|
| 411 |
def umount(target, flags=0): |
| 412 |
_check_ctypes_syscall(_umount2(os.fsencode(target), flags), target) |
| 413 |
|
| 414 |
|
| 415 |
_MOUNTS_RE = re.compile( |
| 416 |
rb"(?P<source>[^ ]+) (?P<mount_point>[^ ]+) (?P<fstype>[^ ]+) " |
| 417 |
rb"(?P<mount_options>[^ ]+) [0-9]+ [0-9]+" |
| 418 |
) |
| 419 |
|
| 420 |
|
| 421 |
class Mount(NamedTuple): |
| 422 |
source: str |
| 423 |
mount_point: Path |
| 424 |
fstype: str |
| 425 |
mount_options: str |
| 426 |
|
| 427 |
|
| 428 |
def iter_mounts(pid="self"): |
| 429 |
with open(f"/proc/{pid}/mounts", "rb") as f: |
| 430 |
for line in f: |
| 431 |
match = _MOUNTS_RE.match(line) |
| 432 |
assert match |
| 433 |
yield Mount( |
| 434 |
source=match["source"].decode("unicode-escape"), |
| 435 |
mount_point=Path(match["mount_point"].decode("unicode-escape")), |
| 436 |
fstype=match["fstype"].decode("unicode-escape"), |
| 437 |
mount_options=match["mount_options"].decode("unicode-escape"), |
| 438 |
) |
| 439 |
|
| 440 |
|
| 441 |
_mlock = _c.mlock |
| 442 |
_mlock.restype = ctypes.c_int |
| 443 |
_mlock.argtypes = [ctypes.c_void_p, ctypes.c_size_t] |
| 444 |
|
| 445 |
|
| 446 |
_MAPS_RE = re.compile( |
| 447 |
rb"(?P<start>[0-9a-f]+)-(?P<end>[0-9a-f]+) (?P<flags>\S+) (?P<offset>[0-9a-f]+) (?P<dev_major>[0-9a-f]+):(?P<dev_minor>[0-9a-f]+) (?P<ino>[0-9]+)\s*(?P<path>.*)" |
| 448 |
) |
| 449 |
|
| 450 |
|
| 451 |
class VmMap(NamedTuple): |
| 452 |
start: int |
| 453 |
end: int |
| 454 |
read: bool |
| 455 |
write: bool |
| 456 |
execute: bool |
| 457 |
shared: bool |
| 458 |
offset: int |
| 459 |
dev: int |
| 460 |
ino: int |
| 461 |
path: str |
| 462 |
|
| 463 |
def is_gate(self) -> bool: |
| 464 |
# Arm has a gate VMA named "vectors", and x86 has one named "vsyscall". |
| 465 |
return self.path in ("[vectors]", "[vsyscall]") |
| 466 |
|
| 467 |
|
| 468 |
def iter_maps(pid="self"): |
| 469 |
with open(f"/proc/{pid}/maps", "rb") as f: |
| 470 |
for line in f: |
| 471 |
match = _MAPS_RE.match(line) |
| 472 |
flags = match["flags"] |
| 473 |
yield VmMap( |
| 474 |
start=int(match["start"], 16), |
| 475 |
end=int(match["end"], 16), |
| 476 |
read=b"r" in flags, |
| 477 |
write=b"w" in flags, |
| 478 |
execute=b"x" in flags, |
| 479 |
shared=b"s" in flags, |
| 480 |
offset=int(match["offset"], 16), |
| 481 |
dev=os.makedev( |
| 482 |
int(match["dev_major"], 16), int(match["dev_minor"], 16) |
| 483 |
), |
| 484 |
ino=int(match["ino"]), |
| 485 |
path=os.fsdecode(match["path"]), |
| 486 |
) |
| 487 |
|
| 488 |
|
| 489 |
def mlock(addr, len): |
| 490 |
_check_ctypes_syscall(_mlock(addr, len)) |
| 491 |
|
| 492 |
|
| 493 |
_prctl = _c.prctl |
| 494 |
_prctl.argtypes = [ctypes.c_int] |
| 495 |
_prctl.restype = ctypes.c_int |
| 496 |
|
| 497 |
|
| 498 |
def prctl_set_vma_anon_name(addr, size, name): |
| 499 |
_check_ctypes_syscall( |
| 500 |
_prctl( |
| 501 |
ctypes.c_int(0x53564D41), # PR_SET_VMA |
| 502 |
ctypes.c_long(0), # PR_SET_VMA_ANON_NAME, |
| 503 |
ctypes.c_ulong(addr), |
| 504 |
ctypes.c_ulong(size), |
| 505 |
ctypes.c_char_p(None if name is None else os.fsencode(name)), |
| 506 |
) |
| 507 |
) |
| 508 |
|
| 509 |
|
| 510 |
CSIGNAL = 0x000000FF |
| 511 |
CLONE_VM = 0x00000100 |
| 512 |
CLONE_FS = 0x00000200 |
| 513 |
CLONE_FILES = 0x00000400 |
| 514 |
CLONE_SIGHAND = 0x00000800 |
| 515 |
CLONE_PIDFD = 0x00001000 |
| 516 |
CLONE_PTRACE = 0x00002000 |
| 517 |
CLONE_VFORK = 0x00004000 |
| 518 |
CLONE_PARENT = 0x00008000 |
| 519 |
CLONE_THREAD = 0x00010000 |
| 520 |
CLONE_NEWNS = 0x00020000 |
| 521 |
CLONE_SYSVSEM = 0x00040000 |
| 522 |
CLONE_SETTLS = 0x00080000 |
| 523 |
CLONE_PARENT_SETTID = 0x00100000 |
| 524 |
CLONE_CHILD_CLEARTID = 0x00200000 |
| 525 |
CLONE_DETACHED = 0x00400000 |
| 526 |
CLONE_UNTRACED = 0x00800000 |
| 527 |
CLONE_CHILD_SETTID = 0x01000000 |
| 528 |
CLONE_NEWCGROUP = 0x02000000 |
| 529 |
CLONE_NEWUTS = 0x04000000 |
| 530 |
CLONE_NEWIPC = 0x08000000 |
| 531 |
CLONE_NEWUSER = 0x10000000 |
| 532 |
CLONE_NEWPID = 0x20000000 |
| 533 |
CLONE_NEWNET = 0x40000000 |
| 534 |
CLONE_IO = 0x80000000 |
| 535 |
CLONE_CLEAR_SIGHAND = 0x100000000 |
| 536 |
CLONE_INTO_CGROUP = 0x200000000 |
| 537 |
CLONE_NEWTIME = 0x00000080 |
| 538 |
|
| 539 |
|
| 540 |
_unshare = _c.unshare |
| 541 |
_unshare.argtypes = [ctypes.c_int] |
| 542 |
_unshare.restype = ctypes.c_int |
| 543 |
|
| 544 |
|
| 545 |
def unshare(flags): |
| 546 |
_check_ctypes_syscall(_unshare(flags)) |
| 547 |
|
| 548 |
|
| 549 |
_LOOP_SET_FD = 0x4C00 |
| 550 |
_LOOP_SET_STATUS64 = 0x4C04 |
| 551 |
_LOOP_GET_STATUS64 = 0x4C05 |
| 552 |
_LOOP_CONFIGURE = 0x4C0A |
| 553 |
_LOOP_CTL_GET_FREE = 0x4C82 |
| 554 |
|
| 555 |
_LO_FLAGS_AUTOCLEAR = 4 |
| 556 |
|
| 557 |
|
| 558 |
def losetup(fd): |
| 559 |
have_loop_configure = True |
| 560 |
with open("/dev/loop-control", "r") as loop_control: |
| 561 |
while True: |
| 562 |
index = ioctl(loop_control.fileno(), _LOOP_CTL_GET_FREE) |
| 563 |
loop = open(f"/dev/loop{index}", "rb") |
| 564 |
close_loop = True |
| 565 |
try: |
| 566 |
# Since Linux kernel commit 3448914e8cc5 ("loop: Add |
| 567 |
# LOOP_CONFIGURE ioctl") (in v5.8), we can set the file |
| 568 |
# descriptor and the autoclear flag atomically with the |
| 569 |
# LOOP_CONFIGURE ioctl. Before that, we have to use |
| 570 |
# LOOP_SET_FD, then LOOP_GET_STATUS64 and LOOP_SET_STATUS64. |
| 571 |
config = bytearray(304) # sizeof(struct loop_config) |
| 572 |
config[:4] = fd.to_bytes(4, sys.byteorder) |
| 573 |
config[60:64] = _LO_FLAGS_AUTOCLEAR.to_bytes(4, sys.byteorder) |
| 574 |
try: |
| 575 |
if have_loop_configure: |
| 576 |
ioctl(loop.fileno(), _LOOP_CONFIGURE, config) |
| 577 |
else: |
| 578 |
ioctl(loop.fileno(), _LOOP_SET_FD, fd) |
| 579 |
except OSError as e: |
| 580 |
if e.errno == errno.EBUSY: |
| 581 |
continue |
| 582 |
elif have_loop_configure and e.errno == errno.EINVAL: |
| 583 |
have_loop_configure = False |
| 584 |
continue |
| 585 |
raise |
| 586 |
if not have_loop_configure: |
| 587 |
info = bytearray(232) # sizeof(struct loop_info64) |
| 588 |
ioctl(loop.fileno(), _LOOP_GET_STATUS64, info) |
| 589 |
lo_flags = int.from_bytes(info[52:56], sys.byteorder) |
| 590 |
lo_flags |= _LO_FLAGS_AUTOCLEAR |
| 591 |
info[52:56] = lo_flags.to_bytes(4, sys.byteorder) |
| 592 |
ioctl(loop.fileno(), _LOOP_SET_STATUS64, info, False) |
| 593 |
close_loop = False |
| 594 |
return loop |
| 595 |
finally: |
| 596 |
if close_loop: |
| 597 |
loop.close() |
| 598 |
|
| 599 |
|
| 600 |
def fallocate(path, offset, len): |
| 601 |
fd = os.open(path, os.O_WRONLY | os.O_CREAT, 0o666) |
| 602 |
try: |
| 603 |
os.posix_fallocate(fd, offset, len) |
| 604 |
finally: |
| 605 |
os.close(fd) |
| 606 |
|
| 607 |
|
| 608 |
_swapon = _c.swapon |
| 609 |
_swapon.argtypes = [ctypes.c_char_p, ctypes.c_int] |
| 610 |
_swapon.restype = ctypes.c_int |
| 611 |
|
| 612 |
_swapoff = _c.swapoff |
| 613 |
_swapoff.argtypes = [ctypes.c_char_p] |
| 614 |
_swapoff.restype = ctypes.c_int |
| 615 |
|
| 616 |
|
| 617 |
def swapon(path, flags=0): |
| 618 |
_check_ctypes_syscall(_swapon(os.fsencode(path), flags), path) |
| 619 |
|
| 620 |
|
| 621 |
def swapoff(path): |
| 622 |
_check_ctypes_syscall(_swapoff(os.fsencode(path)), path) |
| 623 |
|
| 624 |
|
| 625 |
_BLKRRPART = _IO(0x12, 95) |
| 626 |
_BLKSSZGET = _IO(0x12, 104) |
| 627 |
_BLKGETSIZE64 = _IOR(0x12, 114, ctypes.sizeof(ctypes.c_size_t)) |
| 628 |
|
| 629 |
|
| 630 |
def mkswap(path, size=None): |
| 631 |
fd = os.open(path, os.O_WRONLY) |
| 632 |
try: |
| 633 |
if size is None: |
| 634 |
st = os.stat(fd) |
| 635 |
if stat.S_ISBLK(st.st_mode): |
| 636 |
size = ctypes.c_uint64() |
| 637 |
ioctl(fd, _BLKGETSIZE64, size) |
| 638 |
size = size.value |
| 639 |
else: |
| 640 |
size = st.st_size |
| 641 |
|
| 642 |
header = bytearray(mmap.PAGESIZE) |
| 643 |
header[1024:1028] = (1).to_bytes(4, sys.byteorder) # version |
| 644 |
header[1028:1032] = (size // mmap.PAGESIZE - 1).to_bytes( |
| 645 |
4, sys.byteorder |
| 646 |
) # last_page |
| 647 |
header[1036:1052] = os.urandom(16) # sws_uuid |
| 648 |
magic = b"SWAPSPACE2" |
| 649 |
header[-len(magic) :] = magic |
| 650 |
|
| 651 |
n = os.write(fd, header) |
| 652 |
while n < len(header): |
| 653 |
n += os.write(fd, header[n:]) |
| 654 |
finally: |
| 655 |
os.close(fd) |
| 656 |
|
| 657 |
|
| 658 |
class MbrPartitionType(enum.IntEnum): |
| 659 |
LINUX_SWAP = 0x82 |
| 660 |
LINUX = 0x83 |
| 661 |
|
| 662 |
|
| 663 |
class MbrPartition(NamedTuple): |
| 664 |
type: int |
| 665 |
start: int |
| 666 |
size: int |
| 667 |
bootable: bool = False |
| 668 |
|
| 669 |
|
| 670 |
_MBR_PARTITION_STRUCT = struct.Struct("<8BII") |
| 671 |
|
| 672 |
|
| 673 |
def write_mbr(path, partitions, sector_size=None): |
| 674 |
fd = os.open(path, os.O_WRONLY) |
| 675 |
try: |
| 676 |
if sector_size is None: |
| 677 |
sector_size = ctypes.c_int() |
| 678 |
ioctl(fd, _BLKSSZGET, sector_size) |
| 679 |
sector_size = sector_size.value |
| 680 |
|
| 681 |
buf = bytearray(sector_size) |
| 682 |
|
| 683 |
buf[0x1B8:0x1BC] = os.urandom(4) # Disk ID. |
| 684 |
|
| 685 |
offset = 0x01BE |
| 686 |
for partition in partitions: |
| 687 |
if partition.start % sector_size != 0: |
| 688 |
raise ValueError("partition start is not sector-aligned") |
| 689 |
if partition.size % sector_size != 0: |
| 690 |
raise ValueError("partition size is not sector-aligned") |
| 691 |
_MBR_PARTITION_STRUCT.pack_into( |
| 692 |
buf, |
| 693 |
offset, |
| 694 |
0x80 if partition.bootable else 0x0, |
| 695 |
# Placeholder first CHS. |
| 696 |
0xFE, |
| 697 |
0xFF, |
| 698 |
0xFF, |
| 699 |
partition.type, |
| 700 |
# Placeholder last CHS. |
| 701 |
0xFE, |
| 702 |
0xFF, |
| 703 |
0xFF, |
| 704 |
partition.start // sector_size, |
| 705 |
partition.size // sector_size, |
| 706 |
) |
| 707 |
offset += _MBR_PARTITION_STRUCT.size |
| 708 |
|
| 709 |
buf[0x1FE] = 0x55 |
| 710 |
buf[0x1FF] = 0xAA |
| 711 |
|
| 712 |
n = os.write(fd, buf) |
| 713 |
while n < len(buf): |
| 714 |
n += os.write(fd, buf[n:]) |
| 715 |
ioctl(fd, _BLKRRPART) |
| 716 |
finally: |
| 717 |
os.close(fd) |
| 718 |
|
| 719 |
|
| 720 |
class _perf_event_attr_sample_period_or_freq(ctypes.Union): |
| 721 |
_fields_ = ( |
| 722 |
("sample_period", ctypes.c_uint64), |
| 723 |
("sample_freq", ctypes.c_uint64), |
| 724 |
) |
| 725 |
|
| 726 |
|
| 727 |
class _perf_event_attr_wakeup_events_or_watermark(ctypes.Union): |
| 728 |
_fields_ = ( |
| 729 |
("wakeup_events", ctypes.c_uint32), |
| 730 |
("wakeup_watermark", ctypes.c_uint32), |
| 731 |
) |
| 732 |
|
| 733 |
|
| 734 |
class _perf_event_attr_config1(ctypes.Union): |
| 735 |
_fields_ = ( |
| 736 |
("bp_addr", ctypes.c_uint64), |
| 737 |
("kprobe_func", ctypes.c_uint64), |
| 738 |
("uprobe_path", ctypes.c_uint64), |
| 739 |
("config1", ctypes.c_uint64), |
| 740 |
) |
| 741 |
|
| 742 |
|
| 743 |
class _perf_event_attr_config2(ctypes.Union): |
| 744 |
_fields_ = ( |
| 745 |
("bp_len", ctypes.c_uint64), |
| 746 |
("kprobe_addr", ctypes.c_uint64), |
| 747 |
("probe_offset", ctypes.c_uint64), |
| 748 |
("config2", ctypes.c_uint64), |
| 749 |
) |
| 750 |
|
| 751 |
|
| 752 |
class perf_event_attr(ctypes.Structure): |
| 753 |
_fields_ = ( |
| 754 |
("type", ctypes.c_uint32), |
| 755 |
("size", ctypes.c_uint32), |
| 756 |
("config", ctypes.c_uint64), |
| 757 |
("_sample_period_or_freq", _perf_event_attr_sample_period_or_freq), |
| 758 |
("sample_type", ctypes.c_uint64), |
| 759 |
("read_format", ctypes.c_uint64), |
| 760 |
("_bitfields1", ctypes.c_uint64), |
| 761 |
("_wakeup_events_or_watermark", _perf_event_attr_wakeup_events_or_watermark), |
| 762 |
("bp_type", ctypes.c_uint32), |
| 763 |
("_config1", _perf_event_attr_config1), |
| 764 |
("_config2", _perf_event_attr_config2), |
| 765 |
("branch_sample_type", ctypes.c_uint64), |
| 766 |
("sample_regs_user", ctypes.c_uint64), |
| 767 |
("sample_stack_user", ctypes.c_uint32), |
| 768 |
("clockid", ctypes.c_int32), |
| 769 |
("sample_regs_intr", ctypes.c_uint64), |
| 770 |
("aux_watermark", ctypes.c_uint32), |
| 771 |
("sample_max_stack", ctypes.c_uint16), |
| 772 |
("__reserved2", ctypes.c_uint16), |
| 773 |
("aux_sample_size", ctypes.c_uint32), |
| 774 |
("__reserved3", ctypes.c_uint32), |
| 775 |
("sig_data", ctypes.c_uint64), |
| 776 |
("config3", ctypes.c_uint64), |
| 777 |
) |
| 778 |
_anonymous_ = ( |
| 779 |
"_sample_period_or_freq", |
| 780 |
"_wakeup_events_or_watermark", |
| 781 |
"_config1", |
| 782 |
"_config2", |
| 783 |
) |
| 784 |
|
| 785 |
|
| 786 |
PERF_FLAG_FD_NO_GROUP = 1 << 0 |
| 787 |
PERF_FLAG_FD_OUTPUT = 1 << 1 |
| 788 |
PERF_FLAG_PID_CGROUP = 1 << 2 |
| 789 |
PERF_FLAG_FD_CLOEXEC = 1 << 3 |
| 790 |
|
| 791 |
|
| 792 |
def perf_event_open(attr, pid, cpu, group_fd=-1, flags=PERF_FLAG_FD_CLOEXEC): |
| 793 |
attr.size = ctypes.sizeof(perf_event_attr) |
| 794 |
return _check_ctypes_syscall( |
| 795 |
_syscall( |
| 796 |
SYS["perf_event_open"], |
| 797 |
ctypes.byref(attr), |
| 798 |
ctypes.c_int(pid), |
| 799 |
ctypes.c_int(cpu), |
| 800 |
ctypes.c_int(group_fd), |
| 801 |
ctypes.c_ulong(flags), |
| 802 |
) |
| 803 |
) |
| 804 |
|
| 805 |
|
| 806 |
_syscall = _c.syscall |
| 807 |
_syscall.restype = ctypes.c_long |
| 808 |
|
| 809 |
|
| 810 |
def create_socket(*args, **kwds): |
| 811 |
try: |
| 812 |
return socket.socket(*args, **kwds) |
| 813 |
except OSError as e: |
| 814 |
if e.errno in (errno.ENOSYS, errno.EAFNOSUPPORT, errno.ESOCKTNOSUPPORT): |
| 815 |
raise unittest.SkipTest("kernel does not support TCP") |
| 816 |
else: |
| 817 |
raise |
| 818 |
|
| 819 |
|
| 820 |
IPC_PRIVATE = 0 |
| 821 |
IPC_CREAT = 0o1000 |
| 822 |
IPC_EXCL = 0o2000 |
| 823 |
IPC_NOWAIT = 0o4000 |
| 824 |
|
| 825 |
IPC_RMID = 0 |
| 826 |
IPC_SET = 1 |
| 827 |
IPC_STAT = 2 |
| 828 |
IPC_INFO = 3 |
| 829 |
|
| 830 |
_key_t = ctypes.c_int |
| 831 |
|
| 832 |
_ftok = _c.ftok |
| 833 |
_ftok.argtypes = [ctypes.c_char_p, ctypes.c_int] |
| 834 |
_ftok.restype = _key_t |
| 835 |
|
| 836 |
|
| 837 |
def ftok(path, proj_id: int) -> int: |
| 838 |
return _check_ctypes_syscall(_ftok(os.fsencode(path), proj_id)) |
| 839 |
|
| 840 |
|
| 841 |
_msgget = _c.msgget |
| 842 |
_msgget.argtypes = [_key_t, ctypes.c_int] |
| 843 |
_msgget.restype = ctypes.c_int |
| 844 |
|
| 845 |
|
| 846 |
def msgget(key: int, msgflg: int = IPC_CREAT | 0o666) -> int: |
| 847 |
return _check_ctypes_syscall(_msgget(key, msgflg)) |
| 848 |
|
| 849 |
|
| 850 |
_msgctl = _c.msgctl |
| 851 |
_msgctl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_void_p] |
| 852 |
_msgctl.restype = ctypes.c_int |
| 853 |
|
| 854 |
|
| 855 |
def msgctl(msqid: int, op: int) -> None: |
| 856 |
_check_ctypes_syscall(_msgctl(msqid, op, 0)) |
| 857 |
|
| 858 |
|
| 859 |
_semget = _c.semget |
| 860 |
_semget.argtypes = [_key_t, ctypes.c_int, ctypes.c_int] |
| 861 |
_semget.restype = ctypes.c_int |
| 862 |
|
| 863 |
|
| 864 |
def semget(key: int, nsems: int, semflg: int = IPC_CREAT | 0o666) -> int: |
| 865 |
return _check_ctypes_syscall(_semget(key, nsems, semflg)) |
| 866 |
|
| 867 |
|
| 868 |
_semctl = _c.semctl |
| 869 |
_semctl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int] |
| 870 |
_semctl.restype = ctypes.c_int |
| 871 |
|
| 872 |
|
| 873 |
def semctl(semid: int, semnum: int, op: int) -> None: |
| 874 |
_check_ctypes_syscall(_semctl(semid, semnum, op)) |
| 875 |
|
| 876 |
|
| 877 |
SHM_LOCK = 11 |
| 878 |
|
| 879 |
|
| 880 |
_shmget = _c.shmget |
| 881 |
_shmget.argtypes = [_key_t, ctypes.c_int, ctypes.c_int] |
| 882 |
_shmget.restype = ctypes.c_int |
| 883 |
|
| 884 |
|
| 885 |
def shmget(key: int, size: int, shmflg: int = IPC_CREAT | 0o666) -> int: |
| 886 |
return _check_ctypes_syscall(_shmget(key, size, shmflg)) |
| 887 |
|
| 888 |
|
| 889 |
_shmctl = _c.shmctl |
| 890 |
_shmctl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_void_p] |
| 891 |
_shmctl.restype = ctypes.c_int |
| 892 |
|
| 893 |
|
| 894 |
def shmctl(shmid: int, op: int) -> None: |
| 895 |
_check_ctypes_syscall(_shmctl(shmid, op, 0)) |
| 896 |
|
| 897 |
|
| 898 |
_aio_context_t = ctypes.c_ulong |
| 899 |
|
| 900 |
|
| 901 |
def io_setup(nr_events): |
| 902 |
ctx_id = _aio_context_t() |
| 903 |
_check_ctypes_syscall( |
| 904 |
_syscall(SYS["io_setup"], ctypes.c_uint(nr_events), ctypes.byref(ctx_id)) |
| 905 |
) |
| 906 |
return ctx_id.value |
| 907 |
|
| 908 |
|
| 909 |
def io_destroy(ctx_id): |
| 910 |
_check_ctypes_syscall(_syscall(SYS["io_destroy"], _aio_context_t(ctx_id))) |
| 911 |
|
| 912 |
|
| 913 |
_aio_key_and_aio_rw_flags = [ |
| 914 |
("aio_key", ctypes.c_uint32), |
| 915 |
("aio_rw_flags", ctypes.c_int), |
| 916 |
] |
| 917 |
if sys.byteorder == "big": |
| 918 |
_aio_key_and_aio_rw_flags.reverse() |
| 919 |
|
| 920 |
|
| 921 |
IOCB_CMD_PREAD = 0 |
| 922 |
IOCB_CMD_PWRITE = 1 |
| 923 |
IOCB_CMD_FSYNC = 2 |
| 924 |
IOCB_CMD_FDSYNC = 3 |
| 925 |
IOCB_CMD_POLL = 5 |
| 926 |
IOCB_CMD_NOOP = 6 |
| 927 |
IOCB_CMD_PREADV = 7 |
| 928 |
IOCB_CMD_PWRITEV = 8 |
| 929 |
|
| 930 |
|
| 931 |
class iocb(ctypes.Structure): |
| 932 |
_fields_ = ( |
| 933 |
("aio_data", ctypes.c_uint64), |
| 934 |
*_aio_key_and_aio_rw_flags, |
| 935 |
("aio_lio_opcode", ctypes.c_uint16), |
| 936 |
("aio_reqprio", ctypes.c_int16), |
| 937 |
("aio_fildes", ctypes.c_uint32), |
| 938 |
("aio_buf", ctypes.c_uint64), |
| 939 |
("aio_nbytes", ctypes.c_uint64), |
| 940 |
("aio_offset", ctypes.c_int64), |
| 941 |
("aio_reserved2", ctypes.c_uint64), |
| 942 |
("aio_flags", ctypes.c_uint32), |
| 943 |
("aio_resfd", ctypes.c_uint32), |
| 944 |
) |
| 945 |
|
| 946 |
|
| 947 |
def io_submit(ctx_id, iocbs): |
| 948 |
nr = len(iocbs) |
| 949 |
iocbp = (ctypes.POINTER(iocb) * nr)(*(ctypes.pointer(iocb) for iocb in iocbs)) |
| 950 |
return _check_ctypes_syscall( |
| 951 |
_syscall( |
| 952 |
SYS["io_submit"], |
| 953 |
_aio_context_t(ctx_id), |
| 954 |
ctypes.c_long(nr), |
| 955 |
ctypes.byref(iocbp), |
| 956 |
) |
| 957 |
) |