|
| 1 | +# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""UFFD related utility functions""" |
| 4 | + |
| 5 | +import os |
| 6 | +import stat |
| 7 | +import subprocess |
| 8 | +import time |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from framework.utils import chroot |
| 12 | +from host_tools import cargo_build |
| 13 | + |
| 14 | +SOCKET_PATH = "/firecracker-uffd.sock" |
| 15 | + |
| 16 | + |
| 17 | +class UffdHandler: |
| 18 | + """Describe the UFFD page fault handler process.""" |
| 19 | + |
| 20 | + def __init__(self, name, socket_path, mem_file, chroot_path, log_file_name): |
| 21 | + """Instantiate the handler process with arguments.""" |
| 22 | + self._proc = None |
| 23 | + self._handler_name = name |
| 24 | + self._socket_path = socket_path |
| 25 | + self._mem_file = mem_file |
| 26 | + self._chroot = chroot_path |
| 27 | + self._log_file = log_file_name |
| 28 | + |
| 29 | + def spawn(self, uid, gid): |
| 30 | + """Spawn handler process using arguments provided.""" |
| 31 | + |
| 32 | + with chroot(self._chroot): |
| 33 | + st = os.stat(self._handler_name) |
| 34 | + os.chmod(self._handler_name, st.st_mode | stat.S_IEXEC) |
| 35 | + |
| 36 | + chroot_log_file = Path("/") / self._log_file |
| 37 | + with open(chroot_log_file, "w", encoding="utf-8") as logfile: |
| 38 | + args = [f"/{self._handler_name}", self._socket_path, self._mem_file] |
| 39 | + self._proc = subprocess.Popen( |
| 40 | + args, stdout=logfile, stderr=subprocess.STDOUT |
| 41 | + ) |
| 42 | + |
| 43 | + # Give it time start and fail, if it really has too (bad things happen). |
| 44 | + time.sleep(1) |
| 45 | + if not self.is_running(): |
| 46 | + print(chroot_log_file.read_text(encoding="utf-8")) |
| 47 | + assert False, "Could not start PF handler!" |
| 48 | + |
| 49 | + # The page fault handler will create the socket path with root rights. |
| 50 | + # Change rights to the jailer's. |
| 51 | + os.chown(self._socket_path, uid, gid) |
| 52 | + |
| 53 | + @property |
| 54 | + def proc(self): |
| 55 | + """Return UFFD handler process.""" |
| 56 | + return self._proc |
| 57 | + |
| 58 | + def is_running(self): |
| 59 | + """Check if UFFD process is running""" |
| 60 | + return self.proc is not None and self.proc.poll() is None |
| 61 | + |
| 62 | + @property |
| 63 | + def log_file(self): |
| 64 | + """Return the path to the UFFD handler's log file""" |
| 65 | + return Path(self._chroot) / Path(self._log_file) |
| 66 | + |
| 67 | + @property |
| 68 | + def log_data(self): |
| 69 | + """Return the log data of the UFFD handler""" |
| 70 | + if self.log_file is None: |
| 71 | + return "" |
| 72 | + return self.log_file.read_text(encoding="utf-8") |
| 73 | + |
| 74 | + def __del__(self): |
| 75 | + """Tear down the UFFD handler process.""" |
| 76 | + if self.proc is not None: |
| 77 | + self.proc.kill() |
| 78 | + |
| 79 | + |
| 80 | +def spawn_pf_handler(vm, handler_path, mem_path): |
| 81 | + """Spawn page fault handler process.""" |
| 82 | + # Copy snapshot memory file into chroot of microVM. |
| 83 | + jailed_mem = vm.create_jailed_resource(mem_path) |
| 84 | + # Copy the valid page fault binary into chroot of microVM. |
| 85 | + jailed_handler = vm.create_jailed_resource(handler_path) |
| 86 | + handler_name = os.path.basename(jailed_handler) |
| 87 | + |
| 88 | + uffd_handler = UffdHandler( |
| 89 | + handler_name, SOCKET_PATH, jailed_mem, vm.chroot(), "uffd.log" |
| 90 | + ) |
| 91 | + uffd_handler.spawn(vm.jailer.uid, vm.jailer.gid) |
| 92 | + |
| 93 | + return uffd_handler |
| 94 | + |
| 95 | + |
| 96 | +def uffd_handler(handler_name): |
| 97 | + """Retrieves the uffd handler with the given name""" |
| 98 | + return cargo_build.get_example(f"uffd_{handler_name}_handler") |
0 commit comments