8000 Isolated Processes 16: Lay out default remote IO and wire format by marti4d · Pull Request #216 · 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
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ byteorder = "1.4"
error-graph = { version = "0.1.1", features = ["serde"] }
failspot = "0.2.0"
plain = { version = "0.2.3", default-features = false }
process-backend = { version = "0.1.0", path = "crates/linux/process-backend", features = ["testing"] }
process-backend = { version = "0.1.0", path = "crates/linux/process-backend", features = ["postcard", "unix_stream", "testing"] }

# Used for parsing procfs info.
# default-features is disabled since it pulls in chrono
Expand Down
3 changes: 3 additions & 0 deletions crates/linux/process-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ rust-version.workspace = true
license.workspace = true

[features]
postcard = ["dep:postcard"]
testing = []
unix_stream = []

[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
libc.workspace = true
postcard = { version = "1.1.3", default-features = false, optional = true }
process-reader = { version = "0.1.0", path = "../../process-reader", features = ["serde"] }
serde.workspace = true
thiserror.workspace = true
2 changes: 2 additions & 0 deletions crates/linux/process-backend/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
#![allow(dead_code)]
#![cfg(any(target_os = "linux", target_os = "android"))]

use core::ffi::CStr;
Expand All @@ -12,6 +13,7 @@ mod drop_fail_handler;

pub mod local;
pub mod regs;
pub mod remote;

mod wrapper;

Expand Down
22 changes: 22 additions & 0 deletions crates/linux/process-backend/src/remote/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use core::ffi::c_int;

#[cfg(feature = "unix_stream")]
pub use unix_stream::UnixStream;

#[cfg(feature = "unix_stream")]
mod unix_stream;

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

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

#[derive(Debug, serde::Deserialize, serde::Serialize, thiserror::Error)]
pub enum Error {
#[error("an OS error occurred. Errno: {0}")]
OsError(c_int),
#[error("unexpected end-of-file reached")]
UnexpectedEof,
}
71 changes: 71 additions & 0 deletions crates/linux/process-backend/src/remote/io/unix_stream.rs
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use super as io;
use crate::wrapper::{OwnedFd, errno};
use core::ffi::c_int;
use io::{Error, Result};

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

impl UnixStream {
pub fn pair() -> Result<(UnixStream, UnixStream)> {
let mut fds = [0; 2];
let rv = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) };
if rv == -1 {
return Err(Error::OsError(errno()));
}
let stream0 = unsafe { Self::from_raw_fd(fds[0]) };
let stream1 = unsafe { Self::from_raw_fd(fds[1]) };
Ok((stream0, stream1))
}

/// # Safety
///
/// 'fd' must be a valid UNIX stream socket
pub unsafe fn from_raw_fd(fd: c_int) -> Self {
unsafe { Self(OwnedFd::new(fd)) }
}

pub fn as_raw_fd(&self) -> c_int {
self.0.as_raw_fd()
}

fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let rv = unsafe { libc::read(self.0.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) };
if rv == -1 {
return Err(Error::OsError(errno()));
}
let bytes_read = usize::try_from(rv).unwrap();
Ok(bytes_read)
}

fn write(&mut self, buf: &[u8]) -> Result<usize> {
let rv = unsafe { libc::write(self.0.as_raw_fd(), buf.as_ptr().cast(), buf.len()) };
if rv == -1 {
return Err(Error::OsError(errno()));
}
let bytes_written = usize::try_from(rv).unwrap();
Ok(bytes_written)
}
Comment thread
marti4d marked this conversation as resolved.
Resolved
}

impl io::Io for UnixStream {
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
while !buf.is_empty() {
let bytes_read = self.read(buf)?;
if bytes_read == 0 {
return Err(Error::UnexpectedEof);
}
buf = &mut buf[bytes_read..];
}
Ok(())
}

fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
while !buf.is_empty() {
let bytes_written = self.write(buf)?;
assert_ne!(bytes_written, 0);
buf = &buf[bytes_written..];
}
Ok(())
}
}
4 changes: 4 additions & 0 deletions crates/linux/process-backend/src/remote/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod io;
pub mod transport;

mod wire;
55 changes: 55 additions & 0 deletions crates/linux/process-backend/src/remote/transport/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use super as remote;
use remote::io;
use serde::{Deserialize, Serialize};

#[cfg(feature = "postcard")]
pub mod postcard;

pub trait Backend: core::fmt::Debug {
fn max_response_output_len(&self) -> usize;
fn send_request<'output, Req: Serialize, Resp: Deserialize<'output>>(
&'output mut self,
req: Req,
) -> Result<Resp, BackendError>;
}

pub trait Executor: core::fmt::Debug {
fn read_request<'args, D: Deserialize<'args>>(&'args mut self) -> Result<D, ExecutorError>;
fn write_response<S: Serialize>(&mut self, resp: S) -> Result<(), ExecutorError>;
}

#[derive(Debug, thiserror::Error, Deserialize, Serialize)]
pub enum BackendError {
#[error("an error occurred serializing the request")]
SerializeRequest(#[source] SerializeError),
#[error("an error occurred deserializing the response")]
DeserializeResponse(#[source] DeserializeError),
}

#[derive(Debug, thiserror::Error, Deserialize, Serialize)]
pub enum ExecutorError {
#[error("an error occurred deserializing the request")]
DeserializeRequest(#[source] DeserializeError),
#[error("an error occurred serializing the response")]
SerializeResponse(#[source] SerializeError),
}

#[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)]
pub enum SerializeError {
#[cfg(feature = "postcard")]
#[error("a Postcard-specific serialize error occurred")]
Postcard(#[source] postcard::Error),
#[error("an I/O occurred during serialization")]
Io(#[source] io::Error),
}

#[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)]
pub enum DeserializeError {
#[cfg(feature = "postcard")]
#[error("a Postcard-specific deserialize error occurred")]
Postcard(#[source] postcard::Error),
#[error("an I/O occurred during deserialization")]
Io(#[source] io::Error),
#[error("the I/O buffer was too small")]
BufferTooSmall,
}
Loading
Loading
0