Skip to main content

MemoryManager

Struct MemoryManager 

Source
pub struct MemoryManager { /* private fields */ }
Expand description

Provides accessors for reading and writing another process’s memory.

When in use, any operation that touches that process’s memory must go through the MemoryManager to ensure soundness. See MemoryManager::new.

Implementations§

Source§

impl MemoryManager

Source

pub fn new(pid: Pid) -> Self

Source

pub fn read_prefix<T: Pod>( &self, ptr: ForeignArrayPtr<T>, ) -> Result<Vec<T>, Errno>

Copy data from the beginning of the given pointer to the last address in the pointer that’s accessible. Useful for accessing string data of unknown size.

The returned Vec will have capacity for the full ptr; caller might want to call shrink_to_fit if it will be long-lived.

Source

pub fn reader(&self, ptr: ForeignArrayPtr<u8>) -> MemoryReaderCursor<'_> ⓘ

Creates a std::io::Read accessor for the specified plugin memory. Useful for handing off the ability to read process memory to non-Shadow APIs, without copying it to local memory first.

Source

pub fn read<T: Pod>(&self, ptr: ForeignPtr<T>) -> Result<T, Errno>

Reads the memory into a local copy.

Examples:

let ptr: ForeignPtr<u32> = todo!();
let val: u32 = memory_manager.read(ptr)?;
let ptr: ForeignPtr<[u32; 2]> = todo!();
let val: [u32; 2] = memory_manager.read(ptr)?;
Source

pub fn read_vec<T: Pod>(&self, ptr: ForeignArrayPtr<T>) -> Result<Vec<T>, Errno>

Read an array of T into a Vec. If you already have storage allocated, consider copy_from_ptr instead.

Source

pub fn write<T: Pod>( &mut self, ptr: ForeignPtr<T>, val: &T, ) -> Result<(), Errno>

Writes a local value val into the memory at ptr.

let ptr: ForeignPtr<u32> = todo!();
let val = 5;
memory_manager.write(ptr, &val)?;
Source

pub fn copy_from_ptr<T: Pod>( &self, dst: &mut [T], src: ForeignArrayPtr<T>, ) -> Result<(), Errno>

Similar to read, but saves a copy if you already have a dst to copy the data into.

Source

pub fn copy_str_from_ptr<'a>( &self, dst: &'a mut [u8], src: ForeignArrayPtr<u8>, ) -> Result<&'a CStr, Errno>

Copies a NULL-terminated string starting from the beginning of src and contained completely within src. Still works if some of src isn’t readable, as long as a NULL-terminated-string is contained in the readable prefix.

If holding a reference to the MemoryManager for the lifetime of the string is acceptable, use memory_ref_prefix and ProcessMemoryRef::get_str to potentially avoid an extra copy.

Source

pub fn copy_to_ptr<T: Pod>( &mut self, dst: ForeignArrayPtr<T>, src: &[T], ) -> Result<(), Errno>

Writes the memory from a local copy. If src doesn’t already exist, using memory_ref_mut_uninit and initializing the data in that reference saves a copy.

Source

pub fn pid(&self) -> Pid

Which process’s address space this MemoryManager manages.

Source

pub fn writer(&mut self, ptr: ForeignArrayPtr<u8>) -> MemoryWriterCursor<'_> ⓘ

Create a write accessor for the specified plugin memory.

Source

pub fn handle_brk( &mut self, _ctx: &ThreadContext<'_>, _ptr: ForeignPtr<u8>, ) -> Result<ForeignPtr<u8>, SyscallError>

Source

pub fn do_mmap( &mut self, ctx: &ThreadContext<'_>, addr: ForeignPtr<u8>, length: usize, prot: ProtFlags, flags: MapFlags, fd: i32, offset: i64, ) -> Result<ForeignPtr<u8>, Errno>

Source

pub fn handle_munmap( &mut self, _ctx: &ThreadContext<'_>, _addr: ForeignPtr<u8>, _length: usize, ) -> Result<(), SyscallError>

Source

pub fn handle_mremap( &mut self, _ctx: &ThreadContext<'_>, _old_address: ForeignPtr<u8>, _old_size: usize, _new_size: usize, _flags: i32, _new_address: ForeignPtr<u8>, ) -> Result<ForeignPtr<u8>, SyscallError>

Source

pub fn handle_mprotect( &mut self, _ctx: &ThreadContext<'_>, _addr: ForeignPtr<u8>, _size: usize, _prot: ProtFlags, ) -> Result<(), SyscallError>

Trait Implementations§

Source§

impl Debug for MemoryManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Host for T
where T: Debug + Send,

Source§

impl<T> Host for T
where T: Debug + Send + 'static,

Source§

impl<T> Host for T
where T: Host + Host,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> NoTypeInference for T

Source§

type This = T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.