Code Monkey home page Code Monkey logo

process_vm_io's Introduction

crates.io docs.rs license dependency status

I/O access to virtual memory contents of processes

Read and write data from/to the current process and other processes. This can be used for process monitoring, debugging, testing, communication, etc.

Examples

Reading the stack of the currently running process, through this library:

# fn main() -> Result<(), Box<dyn std::error::Error>> {
use process_vm_io::ProcessVirtualMemoryIO;
use std::io::Read;

// Perform I/O on this current process.
let process_id = std::process::id();
let address_of_pid = &process_id as *const _ as u64;
let mut process_io = unsafe { ProcessVirtualMemoryIO::new(process_id, address_of_pid) }?;

// Read the stack of this current thread.
let mut buffer = [0u8; std::mem::size_of::<u32>()];
process_io.read_exact(&mut buffer)?;
let also_pid = u32::from_ne_bytes(buffer);
assert_eq!(process_id, also_pid);
# Ok(())
# }

Writing to the heap of the currently running process, through this library:

# fn main() -> Result<(), Box<dyn std::error::Error>> {
use process_vm_io::ProcessVirtualMemoryIO;
use std::io::{Seek, Write};

// Perform I/O on this current process.
let process_id = std::process::id();
let mut process_io = unsafe { ProcessVirtualMemoryIO::new(process_id, 0) }?;

// Some location on the heap that we will write to.
let mut pid_on_the_heap = Box::new(0_u32);

// Seek to that location and write the PID there.
process_io.seek(std::io::SeekFrom::Start(pid_on_the_heap.as_mut() as *mut _ as u64))?;
process_io.write(&process_id.to_ne_bytes())?;

assert_eq!(process_id, *pid_on_the_heap);
# Ok(())
# }

Safety

Memory safety

Writing to the virtual memory of a process is a potentially unsafe operation because it may introduce memory unsafety in that process, and may lead to unexpected states in that process. This is even more dangerous when the target process is the currently running process.

Running processes

Performing I/O on a running process is not recommended, because the layout of its virtual memory can change at any time, or the process could simply terminate and vanish. Consider pausing all threads of the specified process before performing I/O on it. This can usually be done via the SIGSTOP and SIGCONT POSIX signals.

Platform-specific notes

For the moment, only Linux is supported.

Versioning

This project adheres to Semantic Versioning. The CHANGELOG.md file details notable changes over time.

License

Copyright (c) 2020-2023 MicroDoc Software GmbH.

See the LICENSE.txt file at the top-level directory of this distribution.

Licensed under the MIT license. This file may not be copied, modified, or distributed except according to those terms.

process_vm_io's People

Contributors

koutheir avatar mychris avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.