run

Function run 

Source
pub fn run<B: AsRef<[u8]>>(bytes: B) -> Result<i32, RunError>
Expand description

Executes an in-memory ELF binary by creating a child process.

This is the simplest way to execute an ELF binary from memory. It does a very basic ELF header verification, creates a memory file descriptor, writes the ELF data to it, then forks a child process and executes the binary via /proc/self/fd/{fd}.

§Arguments

  • bytes - The ELF binary data to execute (must have valid ELF magic bytes)

§Returns

  • Ok(exit_code) - The exit code of the executed process (0-255)
  • Err(RunError) - Various error conditions during execution

§Examples

// Execute /usr/bin/ls from memory
let elf_bytes = std::fs::read("/usr/bin/ls").unwrap();
let exit_code = memfd_runner::run(&elf_bytes).unwrap();
println!("Process exited with code: {}", exit_code);