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
impl MemoryManager
pub fn new(pid: Pid) -> Self
Sourcepub fn read_prefix<T: Pod>(
&self,
ptr: ForeignArrayPtr<T>,
) -> Result<Vec<T>, Errno>
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.
Sourcepub fn reader(&self, ptr: ForeignArrayPtr<u8>) -> MemoryReaderCursor<'_> ⓘ
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.
Sourcepub fn read<T: Pod>(&self, ptr: ForeignPtr<T>) -> Result<T, Errno>
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)?;Sourcepub fn read_vec<T: Pod>(&self, ptr: ForeignArrayPtr<T>) -> Result<Vec<T>, Errno>
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.
Sourcepub fn write<T: Pod>(
&mut self,
ptr: ForeignPtr<T>,
val: &T,
) -> Result<(), Errno>
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)?;Sourcepub fn copy_from_ptr<T: Pod>(
&self,
dst: &mut [T],
src: ForeignArrayPtr<T>,
) -> Result<(), Errno>
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.
Sourcepub fn copy_str_from_ptr<'a>(
&self,
dst: &'a mut [u8],
src: ForeignArrayPtr<u8>,
) -> Result<&'a CStr, Errno>
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.
Sourcepub fn copy_to_ptr<T: Pod>(
&mut self,
dst: ForeignArrayPtr<T>,
src: &[T],
) -> Result<(), Errno>
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.
Sourcepub fn writer(&mut self, ptr: ForeignArrayPtr<u8>) -> MemoryWriterCursor<'_> ⓘ
pub fn writer(&mut self, ptr: ForeignArrayPtr<u8>) -> MemoryWriterCursor<'_> ⓘ
Create a write accessor for the specified plugin memory.
pub fn handle_brk( &mut self, _ctx: &ThreadContext<'_>, _ptr: ForeignPtr<u8>, ) -> Result<ForeignPtr<u8>, SyscallError>
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>
pub fn handle_munmap( &mut self, _ctx: &ThreadContext<'_>, _addr: ForeignPtr<u8>, _length: usize, ) -> Result<(), SyscallError>
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>
pub fn handle_mprotect( &mut self, _ctx: &ThreadContext<'_>, _addr: ForeignPtr<u8>, _size: usize, _prot: ProtFlags, ) -> Result<(), SyscallError>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for MemoryManager
impl RefUnwindSafe for MemoryManager
impl Send for MemoryManager
impl Sync for MemoryManager
impl Unpin for MemoryManager
impl UnsafeUnpin for MemoryManager
impl UnwindSafe for MemoryManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> Host for T
impl<T> Host for T
impl<T> Host for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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