Repository navigation
Isolated Processes 17: Implement executor to run syscalls in remote process #217
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
2 commits
Select commit
Hold shift + click to select a range
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
269 changes: 269 additions & 0 deletions
269
crates/linux/process-backend/src/remote/executor/handler.rs
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,269 @@ | ||
| use super as executor; | ||
| use super::remote; | ||
| use crate::{ProcessReaderKind, regs::*, wrapper::Stat}; | ||
| use executor::{Error, Executor, Result}; | ||
| use remote::wire; | ||
| use wire::{CStrWithNull, HandlesRequests}; | ||
|
|
||
| impl HandlesRequests for &mut Executor<'_> { | ||
| fn GetPid<'args, 'output>(self, _args: ()) -> Result<libc::pid_t> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| Ok(self.local_backend.pid()) | ||
| } | ||
|
|
||
| fn ProcessReaderReadAt<'args, 'output>( | ||
| self, | ||
| args: wire::ProcessReaderReadAtArgs, | ||
| ) -> Result<&'output [u8]> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let scratch_buf = borrow_with_limit(self.scratch_buf, args.requested_len); | ||
|
|
||
| let bytes_read = self | ||
| .local_backend | ||
| .process_reader() | ||
| .read_at(args.address, scratch_buf) | ||
| .map_err(Error::Local)?; | ||
|
|
||
| Ok(&scratch_buf[0..bytes_read]) | ||
| } | ||
|
|
||
| fn StopProcess<'args, 'output>(self, _args: ()) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.stop_process().map_err(Error::Local) | ||
| } | ||
|
|
||
| fn ContinueProcess<'args, 'output>(self, _args: ()) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.continue_process().map_err(Error::Local) | ||
| } | ||
|
|
||
| fn SuspendThread<'args, 'output>(self, tid: libc::pid_t) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.suspend_thread(tid).map_err(Error::Local) | ||
| } | ||
|
|
||
| fn ResumeThread<'args, 'output>(self, tid: libc::pid_t) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.resume_thread(tid).map_err(Error::Local) | ||
| } | ||
|
|
||
| fn MappedModuleMemoryReaderOpen<'args, 'output>( | ||
| self, | ||
| args: wire::MappedModuleMemoryReaderOpenArgs, | ||
| ) -> Result<usize> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self | ||
| .local_backend | ||
| .map_module_into_memory(&args.path, args.offset) | ||
| .map_err(Error::Local)?; | ||
| self.mapped_module_memory_readers | ||
| .assign_slot(reader) | ||
| .ok_or(Error::MappedModuleMemoryReaderSlotsExhausted) | ||
| } | ||
|
|
||
| fn MappedModuleMemoryReaderReadAt<'args, 'output>( | ||
| self, | ||
| args: wire::MappedModuleMemoryReaderReadAtArgs, | ||
| ) -> Result<&'output [u8]> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self | ||
| .mapped_module_memory_readers | ||
| .get_slot_mut(args.handle) | ||
| .ok_or(Error::EmptyMappedModuleMemoryReaderSlotRequested)?; | ||
| reader | ||
| .read_at(args.offset, args.requested_len) | ||
| .map_err(Error::Local) | ||
| } | ||
|
|
||
| fn MappedModuleMemoryReaderLen<'args, 'output>(self, handle: usize) -> Result<usize> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self | ||
| .mapped_module_memory_readers | ||
| .get_slot_mut(handle) | ||
| .ok_or(Error::EmptyMappedModuleMemoryReaderSlotRequested)?; | ||
| Ok(reader.len()) | ||
| } | ||
|
|
||
| fn MappedModuleMemoryReaderClose<'args, 'output>(self, handle: usize) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let _reader = self | ||
| .mapped_module_memory_readers | ||
| .free_slot(handle) | ||
| .ok_or(Error::EmptyMappedModuleMemoryReaderSlotRequested)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn StatFile<'args, 'output>(self, path: CStrWithNull<'_>) -> Result<Stat> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.stat_file(&path).map_err(Error::Local) | ||
| } | ||
|
|
||
| fn FileReaderOpen<'args, 'output>(self, path: CStrWithNull<'_>) -> Result<usize> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self.local_backend.read_file(&path).map_err(Error::Local)?; | ||
| self.file_readers | ||
| .assign_slot(reader) | ||
| .ok_or(Error::FileReaderSlotsExhausted) | ||
| } | ||
|
|
||
| fn FileReaderRead<'args, 'output>(self, args: wire::FileReaderArgs) -> Result<&'output [u8]> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let scratch_buf = borrow_with_limit(self.scratch_buf, args.requested_len); | ||
|
|
||
| let reader = self | ||
| .file_readers | ||
| .get_slot_mut(args.handle) | ||
| .ok_or(Error::EmptyFileSlotRequested)?; | ||
|
|
||
| let bytes_read = reader.read(scratch_buf).map_err(Error::Local)?; | ||
|
|
||
| Ok(&scratch_buf[0..bytes_read]) | ||
| } | ||
|
|
||
| fn FileReaderClose<'args, 'output>(self, handle: usize) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let _reader = self | ||
| .file_readers | ||
| .free_slot(handle) | ||
| .ok_or(Error::EmptyFileSlotRequested)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn DirReaderOpen<'args, 'output>(self, path: CStrWithNull<'_>) -> Result<usize> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self.local_backend.read_dir(&path).map_err(Error::Local)?; | ||
| self.dir_readers | ||
| .assign_slot(reader) | ||
| .ok_or(Error::DirReaderSlotsExhausted) | ||
| } | ||
|
|
||
| fn DirReaderRead<'args, 'output>(self, handle: usize) -> Result<Option<&'output [u8]>> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let reader = self | ||
| .dir_readers | ||
| .get_slot_mut(handle) | ||
| .ok_or(Error::EmptyDirSlotRequested)?; | ||
|
|
||
| reader.read_next_name().map_err(Error::Local) | ||
| } | ||
|
|
||
| fn DirReaderClose<'args, 'output>(self, handle: usize) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let _reader = self | ||
| .dir_readers | ||
| .free_slot(handle) | ||
| .ok_or(Error::EmptyDirSlotRequested)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn ReadLink<'args, 'output>(self, args: wire::ReadLinkArgs<'_>) -> Result<&'output [u8]> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| let scratch_buf = borrow_with_limit(self.scratch_buf, args.requested_len); | ||
|
|
||
| let bytes_read = self | ||
| .local_backend | ||
| .read_link(&args.path, scratch_buf) | ||
| .map_err(Error::Local)?; | ||
|
|
||
| Ok(&scratch_buf[0..bytes_read]) | ||
| } | ||
|
|
||
| fn GetGenRegs<'args, 'output>(self, tid: libc::pid_t) -> Result<GenRegs> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.get_gen_regs(tid).map_err(Error::Local) | ||
| } | ||
|
|
||
| fn GetFpRegs<'args, 'output>(self, tid: libc::pid_t) -> Result<FpRegs> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.get_fp_regs(tid).map_err(Error::Local) | ||
| } | ||
|
|
||
| #[cfg(target_arch = "x86")] | ||
| fn GetFpxRegs<'args, 'output>(self, tid: libc::pid_t) -> Result<FpxRegs> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.get_fpx_regs(tid).map_err(Error::Local) | ||
| } | ||
|
|
||
| #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] | ||
| fn PtracePeekUser<'args, 'output>(self, addr: usize) -> Result<[u8; crate::PTRACE_DATA_LEN]> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend | ||
| .ptrace_peekuser(addr) | ||
| .map_err(Error::Local) | ||
| } | ||
|
|
||
| fn ForceProcessReaderKind<'args, 'output>(self, kind: ProcessReaderKind) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend | ||
| .force_process_reader_kind(kind) | ||
| .map_err(Error::Local) | ||
| } | ||
|
|
||
| #[cfg(feature = "testing")] | ||
| fn FailOneSyscallWith<'args, 'output>(self, errno: core::ffi::c_int) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.local_backend.fail_one_syscall_with(errno); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn Quit<'args, 'output>(self, _args: ()) -> Result<()> | ||
| where | ||
| Self: 'output, | ||
| { | ||
| self.quit = true; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn borrow_with_limit(buf: &mut [u8], limit: usize) -> &mut [u8] { | ||
| let len = usize::min(buf.len(), limit); | ||
| &mut buf[0..len] | ||
| } | ||
8BD0
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,60 @@ | ||
| use super as remote; | ||
| use crate::local; | ||
| use remote::{transport, wire}; | ||
| use resources::Pool; | ||
|
|
||
| pub use resources::Resources; | ||
|
|
||
| pub mod resources; | ||
|
|
||
| mod handler; | ||
|
|
||
| pub type Result<T> = core::result::Result<T, Error>; | ||
|
|
||
| pub fn run<T: transport::Executor>( | ||
| pid: libc::pid_t, | ||
| mut transport: T, | ||
| resources: Resources<'_>, | ||
| ) -> core::result::Result<(), transport::ExecutorError> { | ||
| let mut executor = Executor { | ||
| local_backend: local::Backend::new(pid), | ||
| quit: false, | ||
| file_readers: Pool::new(resources.file_readers), | ||
| dir_readers: Pool::new(resources.dir_readers), | ||
| mapped_module_memory_readers: Pool::new(resources.mapped_module_memory_readers), | ||
| scratch_buf: resources.scratch_buf, | ||
| }; | ||
|
|
||
| while !executor.quit { | ||
| wire::serve_request(&mut transport, &mut executor)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| struct Executor<'a> { | ||
| local_backend: local::Backend, | ||
| quit: bool, | ||
| file_readers: Pool<'a, resources::FileReader>, | ||
| dir_readers: Pool<'a, resources::DirReader>, | ||
| mapped_module_memory_readers: Pool<'a, resources::MappedModuleMemoryReader>, | ||
| scratch_buf: &'a mut [u8], | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)] | ||
| pub enum Error { | ||
| #[error("an error was returned by a local syscall")] | ||
| Local(#[source] local::Error), | ||
| #[error("ran out of slots for a new MappedModuleMemoryReader")] | ||
| MappedModuleMemoryReaderSlotsExhausted, | ||
| #[error("requested an empty MappedModuleMemoryReader slot")] | ||
| EmptyMappedModuleMemoryReaderSlotRequested, | ||
| #[error("ran out of slots for a new FileReader")] | ||
| FileReaderSlotsExhausted, | ||
| #[error("requested an empty file slot")] | ||
| EmptyFileSlotRequested, | ||
| #[error("ran out of slots for a new DirReader")] | ||
| DirReaderSlotsExhausted, | ||
| #[error("requested an empty directory slot")] | ||
| EmptyDirSlotRequested, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add a blank line between each function