8000 Isolated Processes 14: Add traits for process-backend implementations by marti4d · Pull Request #202 · rust-minidump/minidump-writer · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions crates/linux/process-backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![no_std]
#![cfg(any(target_os = "linux", target_os = "android"))]

use core::ffi::CStr;
use regs::*;

pub use drop_fail_handler::Handler as DropFailHandler;

#[macro_use]
Expand All @@ -11,13 +14,85 @@ pub mod regs;

mod wrapper;

pub type Result<T> = core::result::Result<T, Error>;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub const PTRACE_DATA_LEN: usize = core::mem::size_of::<libc::c_long>();

pub trait Backend: core::fmt::Debug {
type ProcessReader<'a>: ProcessReader
where
Self: 'a;
type FileReader<'a>: FileReader
where
Self: 'a;
type DirReader<'a>: DirReader
where
Self: 'a;
type MappedModuleMemoryReader<'a>: MappedModuleMemoryReader
where
Self: 'a;

fn pid(&self) -> Result<libc::pid_t>;
fn process_reader<'a>(&'a self) -> Self::ProcessReader<'a>;
fn stop_process(&self) -> Result<()>;
fn continue_process(&self) -> Result<()>;
fn suspend_thread(&self, tid: libc::pid_t) -> Result<()>;
fn resume_thread(&self, tid: libc::pid_t) -> Result<()>;
fn map_module_into_memory<'a>(
&'a self,
path: &CStr,
offset: u64,
) -> Result<Self::MappedModuleMemoryReader<'a>>;
fn stat_file(&self, path: &CStr) -> Result<libc::stat>;
fn read_file<'a>(&'a self, path: &CStr) -> Result<Self::FileReader<'a>>;
fn read_dir<'a>(&'a self, path: &CStr) -> Result<Self::DirReader<'a>>;
fn read_link(&self, path: &CStr, buf: &mut [u8]) -> Result<usize>;
fn get_gen_regs(&self, tid: libc::pid_t) -> Result<GenRegs>;
fn get_fp_regs(&self, tid: libc::pid_t) -> Result<FpRegs>;

#[cfg(target_arch = "x86")]
fn get_fpx_regs(&self, tid: libc::pid_t) -> Result<FpxRegs>;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn ptrace_peekuser(&self, addr: usize) -> Result<[u8; PTRACE_DATA_LEN]>;

fn force_process_reader_kind(&mut self, kind: ProcessReaderKind) -> Result<()>;

#[cfg(feature = "testing")]
fn fail_one_syscall_with(&self, errno: core::ffi::c_int);
}

pub trait FileReader: core::fmt::Debug {
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
}

pub trait DirReader: core::fmt::Debug {
fn read_next_name(&mut self, buf: &mut [u8]) -> Result<usize>;
}

pub trait ProcessReader: core::fmt::Debug {
fn read_at(&self, address: usize, buf: &mut [u8]) -> Result<usize>;
}

pub trait MappedModuleMemoryReader: core::fmt::Debug {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize>;
fn len(&self) -> Result<usize>;
fn is_empty(&self) -> Result<bool>;
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum ProcessReaderKind {
Unspecified,
VirtualMem,
File,
Ptrace,
}

#[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)]
pub enum Error {
#[error("there was a failure making a direct syscall")]
Local(#[source] local::Error),
#[error("buffer was too small to contain the result")]
BufferTooSmall,
}
110 changes: 110 additions & 0 deletions crates/linux/process-backend/src/local/mod.rs
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,91 @@ impl Backend {
}
}

impl crate::Backend for Backend {
type ProcessReader<'a> = ProcessReader<'a>;

type FileReader<'a> = FileReader;

type DirReader<'a> = DirReader;

type MappedModuleMemoryReader<'a> = MappedModuleMemoryReader;

fn pid(&self) -> crate::Result<libc::pid_t> {
Ok(Backend::pid(self))
}

fn process_reader<'a>(&'a self) -> Self::ProcessReader<'a> {
Backend::process_reader(self)
}

fn stop_process(&self) -> crate::Result<()> {
Backend::stop_process(self).map_err(crate::Error::Local)
}

fn continue_process(&self) -> crate::Result<()> {
Backend::continue_process(self).map_err(crate::Error::Local)
}

fn suspend_thread(&self, tid: libc::pid_t) -> crate::Result<()> {
Backend::suspend_thread(self, tid).map_err(crate::Error::Local)
}

fn resume_thread(&self, tid: libc::pid_t) -> crate::Result<()> {
Backend::resume_thread(self, tid).map_err(crate::Error::Local)
}

fn map_module_into_memory<'a>(
&'a self,
path: &CStr,
offset: u64,
) -> crate::Result<Self::MappedModuleMemoryReader<'a>> {
Backend::map_module_into_memory(self, path, offset).map_err(crate::Error::Local)
}

fn stat_file(&self, path: &CStr) -> crate::Result<libc::stat> {
Backend::stat_file(self, path).map_err(crate::Error::Local)
}

fn read_file<'a>(&'a self, path: &CStr) -> crate::Result<Self::FileReader<'a>> {
Backend::read_file(self, path).map_err(crate::Error::Local)
}

fn read_dir<'a>(&'a self, path: &CStr) -> crate::Result<Self::DirReader<'a>> {
Backend::read_dir(self, path).map_err(crate::Error::Local)
}

fn read_link(&self, path: &CStr, buf: &mut [u8]) -> crate::Result<usize> {
Backend::read_link(self, path, buf).map_err(crate::Error::Local)
}

fn get_gen_regs(&self, tid: libc::pid_t) -> crate::Result<GenRegs> {
Backend::get_gen_regs(self, tid).map_err(crate::Error::Local)
}

fn get_fp_regs(&self, tid: libc::pid_t) -> crate::Result<FpRegs> {
Backend::get_fp_regs(self, tid).map_err(crate::Error::Local)
}

#[cfg(target_arch = "x86")]
fn get_fpx_regs(&self, tid: libc::pid_t) -> crate::Result<FpxRegs> {
Backend::get_fpx_regs(self, tid).map_err(crate::Error::Local)
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn ptrace_peekuser(&self, addr: usize) -> crate::Result<[u8; crate::PTRACE_DATA_LEN]> {
Backend::ptrace_peekuser(self, addr).map_err(crate::Error::Local)
}

fn force_process_reader_kind(&mut self, kind: ProcessReaderKind) -> crate::Result<()> {
Backend::force_process_reader_kind(self, kind).map_err(crate::Error::Local)
}

#[cfg(feature = "testing")]
fn fail_one_syscall_with(&self, errno: c_int) {
Backend::fail_one_syscall_with(self, errno)
}
}

#[derive(Debug)]
pub struct FileReader(OwnedFd);

Expand Down Expand Up @@ -345,6 +430,12 @@ impl FileReader {
}
}

impl crate::FileReader for FileReader {
fn read(&mut self, buf: &mut [u8]) -> crate::Result<usize> {
FileReader::read(self, buf).map_err(crate::Error::Local)
}
}

#[derive(Debug)]
pub struct DirReader {
dirp: *mut libc::DIR,
Expand Down Expand Up @@ -394,6 +485,19 @@ impl Drop for DirReader {
}
}

impl crate::DirReader for DirReader {
fn read_next_name(&mut self, buf: &mut [u8]) -> crate::Result<usize> {
let Some(name) = DirReader::read_next_name(self).map_err(crate::Error::Local)? else {
return Ok(0);
};
let buf = buf
.get_mut(0..name.len())
.ok_or(crate::Error::BufferTooSmall)?;
buf.copy_from_slice(name);
Ok(name.len())
}
}

#[derive(Debug)]
pub struct ProcessReader<'a>(&'a process_reader::ProcessReader);

Expand All @@ -403,6 +507,12 @@ impl<'a> ProcessReader<'a> {
}
}

impl<'a> crate::ProcessReader for ProcessReader<'a> {
fn read_at(&self, address: usize, buf: &mut [u8]) -> crate::Result<usize> {
ProcessReader::read_at(self, address, buf).map_err(crate::Error::Local)
}
}

/// This is just a typesafe wrapper around ptrace(), which is vararg... But this is Rust, and
/// playing loosey-goosey with types is really more of a C thing ;)
unsafe fn ptrace(
Expand Down
17 changes: 17 additions & 0 deletions crates/linux/process-backend/src/local/module_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ impl MappedModuleMemoryReader {
}
}

impl crate::MappedModuleMemoryReader for MappedModuleMemoryReader {
fn read_at(&self, offset: usize, buf: &mut [u8]) -> core::result::Result<usize, crate::Error> {
let bytes = MappedModuleMemoryReader::read_at(self, offset, buf.len())
.map_err(crate::Error::Local)?;
buf[0..bytes.len()].copy_from_slice(bytes);
Ok(bytes.len())
}

fn len(&self) -> core::result::Result<usize, crate::Error> {
Ok(MappedModuleMemoryReader::len(self))
}

fn is_empty(&self) -> core::result::Result<bool, crate::Error> {
Ok(MappedModuleMemoryReader::is_empty(self))
}
}

#[derive(Debug)]
struct Mapped {
ptr: *mut c_void,
Expand Down
Loading
Loading
0