-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix fast_page_fault_helper crashing instead of causing page faults #5054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70e50a8
fix formatting in fast_page_fault_helper.c
roypat b5b4838
fix(test): check for mmap errors before using pointer
roypat 0e1ab41
fast_page_fault_helper: return 1 instead of -1 in case of error
roypat e3f1c05
fix(test): install signal mask in fast_page_fault_helper
roypat 094a14a
refactor(test): introduce utils_uffd.py
roypat febb888
refactor(test): make uffd_handle_paths not be a fixture
roypat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
"""UFFD related utility functions""" | ||
|
||
import os | ||
import stat | ||
import subprocess | ||
import time | ||
from pathlib import Path | ||
|
||
from framework.utils import chroot | ||
from host_tools import cargo_build | ||
|
||
SOCKET_PATH = "/firecracker-uffd.sock" | ||
|
||
|
||
class UffdHandler: | ||
"""Describe the UFFD page fault handler process.""" | ||
|
||
def __init__(self, name, socket_path, mem_file, chroot_path, log_file_name): | ||
"""Instantiate the handler process with arguments.""" | ||
self._proc = None | ||
self._handler_name = name | ||
self._socket_path = socket_path | ||
self._mem_file = mem_file | ||
self._chroot = chroot_path | ||
self._log_file = log_file_name | ||
|
||
def spawn(self, uid, gid): | ||
"""Spawn handler process using arguments provided.""" | ||
|
||
with chroot(self._chroot): | ||
st = os.stat(self._handler_name) | ||
os.chmod(self._handler_name, st.st_mode | stat.S_IEXEC) | ||
|
||
chroot_log_file = Path("/") / self._log_file | ||
with open(chroot_log_file, "w", encoding="utf-8") as logfile: | ||
args = [f"/{self._handler_name}", self._socket_path, self._mem_file] | ||
self._proc = subprocess.Popen( | ||
args, stdout=logfile, stderr=subprocess.STDOUT | ||
) | ||
|
||
# Give it time start and fail, if it really has too (bad things happen). | ||
time.sleep(1) | ||
if not self.is_running(): | ||
print(chroot_log_file.read_text(encoding="utf-8")) | ||
assert False, "Could not start PF handler!" | ||
|
||
# The page fault handler will create the socket path with root rights. | ||
# Change rights to the jailer's. | ||
os.chown(self._socket_path, uid, gid) | ||
|
||
@property | ||
def proc(self): | ||
"""Return UFFD handler process.""" | ||
return self._proc | ||
|
||
def is_running(self): | ||
"""Check if UFFD process is running""" | ||
return self.proc is not None and self.proc.poll() is None | ||
|
||
@property | ||
def log_file(self): | ||
"""Return the path to the UFFD handler's log file""" | ||
return Path(self._chroot) / Path(self._log_file) | ||
|
||
@property | ||
def log_data(self): | ||
"""Return the log data of the UFFD handler""" | ||
if self.log_file is None: | ||
return "" | ||
return self.log_file.read_text(encoding="utf-8") | ||
|
||
def __del__(self): | ||
"""Tear down the UFFD handler process.""" | ||
if self.proc is not None: | ||
self.proc.kill() | ||
|
||
|
||
def spawn_pf_handler(vm, handler_path, mem_path): | ||
"""Spawn page fault handler process.""" | ||
# Copy snapshot memory file into chroot of microVM. | ||
jailed_mem = vm.create_jailed_resource(mem_path) | ||
# Copy the valid page fault binary into chroot of microVM. | ||
jailed_handler = vm.create_jailed_resource(handler_path) | ||
handler_name = os.path.basename(jailed_handler) | ||
|
||
uffd_handler = UffdHandler( | ||
handler_name, SOCKET_PATH, jailed_mem, vm.chroot(), "uffd.log" | ||
) | ||
uffd_handler.spawn(vm.jailer.uid, vm.jailer.gid) | ||
|
||
return uffd_handler | ||
|
||
|
||
def uffd_handler(handler_name): | ||
"""Retrieves the uffd handler with the given name""" | ||
return cargo_build.get_example(f"uffd_{handler_name}_handler") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.