Struct solana_rbpf::vm::EbpfVm[][src]

pub struct EbpfVm<'a, E: UserDefinedError, I: InstructionMeter> { /* fields omitted */ }
Expand description

A virtual machine to run eBPF program.

Examples

use solana_rbpf::{vm::{Config, Executable, EbpfVm, DefaultInstructionMeter}, user_error::UserError};

let prog = &[
    0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // exit
];
let mem = &mut [
    0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
];

// Instantiate a VM.
let executable = Executable::<UserError, DefaultInstructionMeter>::from_text_bytes(prog, None, Config::default()).unwrap();
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), mem, &[]).unwrap();

// Provide a reference to the packet data.
let res = vm.execute_program_interpreted(&mut DefaultInstructionMeter {}).unwrap();
assert_eq!(res, 0);

Implementations

impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I>[src]

pub fn new(
    executable: &'a dyn Executable<E, I>,
    mem: &mut [u8],
    granted_regions: &[MemoryRegion]
) -> Result<EbpfVm<'a, E, I>, EbpfError<E>>
[src]

Create a new virtual machine instance, and load an eBPF program into that instance. When attempting to load the program, it passes through a simple verifier.

Examples

use solana_rbpf::{vm::{Config, Executable, EbpfVm, DefaultInstructionMeter}, user_error::UserError};

let prog = &[
    0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // exit
];

// Instantiate a VM.
let executable = Executable::<UserError, DefaultInstructionMeter>::from_text_bytes(prog, None, Config::default()).unwrap();
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap();

pub fn get_total_instruction_count(&self) -> u64[src]

Returns the number of instructions executed by the last program.

pub fn get_program(&self) -> &[u8][src]

Returns the program

pub fn get_tracer(&self) -> &Tracer[src]

Returns the tracer

pub fn bind_syscall_context_object(
    &mut self,
    syscall_context_object: Box<dyn SyscallObject<E> + 'a>,
    hash: Option<u32>
) -> Result<(), EbpfError<E>>
[src]

Bind a context object instance to a previously registered syscall

Examples

use solana_rbpf::{vm::{Config, Executable, EbpfVm, SyscallObject, SyscallRegistry, DefaultInstructionMeter}, syscalls::BpfTracePrintf, user_error::UserError};

// This program was compiled with clang, from a C program containing the following single
// instruction: `return bpf_trace_printk("foo %c %c %c\n", 10, 1, 2, 3);`
let prog = &[
    0x18, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // load 0 as u64 into r1 (That would be
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // replaced by tc by the address of
                                                    // the format string, in the .map
                                                    // section of the ELF file).
    0xb7, 0x02, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, // mov r2, 10
    0xb7, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, // mov r3, 1
    0xb7, 0x04, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov r4, 2
    0xb7, 0x05, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, // mov r5, 3
    0x85, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, // call syscall with key 6
    0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // exit
];

// Register a syscall.
// On running the program this syscall will print the content of registers r3, r4 and r5 to
// standard output.
let mut syscall_registry = SyscallRegistry::default();
syscall_registry.register_syscall_by_hash(6, BpfTracePrintf::call).unwrap();
// Instantiate an Executable and VM
let mut executable = Executable::<UserError, DefaultInstructionMeter>::from_text_bytes(prog, None, Config::default()).unwrap();
executable.set_syscall_registry(syscall_registry);
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), &mut [], &[]).unwrap();
// Bind a context object instance to the previously registered syscall
vm.bind_syscall_context_object(Box::new(BpfTracePrintf {}), None);

pub fn get_syscall_context_object(
    &self,
    syscall_function: usize
) -> Option<*mut u8>
[src]

Lookup a syscall context object by its function pointer. Used for testing and validation.

pub fn execute_program_interpreted(
    &mut self,
    instruction_meter: &mut I
) -> ProgramResult<E>
[src]

Execute the program loaded, with the given packet data.

Warning: The program is executed without limiting the number of instructions that can be executed

Examples

use solana_rbpf::{vm::{Config, Executable, EbpfVm, DefaultInstructionMeter}, user_error::UserError};

let prog = &[
    0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // exit
];
let mem = &mut [
    0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
];

// Instantiate a VM.
let executable = Executable::<UserError, DefaultInstructionMeter>::from_text_bytes(prog, None, Config::default()).unwrap();
let mut vm = EbpfVm::<UserError, DefaultInstructionMeter>::new(executable.as_ref(), mem, &[]).unwrap();

// Provide a reference to the packet data.
let res = vm.execute_program_interpreted(&mut DefaultInstructionMeter {}).unwrap();
assert_eq!(res, 0);

pub fn execute_program_jit(
    &mut self,
    instruction_meter: &mut I
) -> ProgramResult<E>
[src]

Execute the previously JIT-compiled program, with the given packet data in a manner very similar to execute_program_interpreted().

Safety

WARNING: JIT-compiled assembly code is not safe. It may be wise to check that the program works with the interpreter before running the JIT-compiled version of it.

Auto Trait Implementations

impl<'a, E, I> !RefUnwindSafe for EbpfVm<'a, E, I>

impl<'a, E, I> !Send for EbpfVm<'a, E, I>

impl<'a, E, I> !Sync for EbpfVm<'a, E, I>

impl<'a, E, I> Unpin for EbpfVm<'a, E, I>

impl<'a, E, I> !UnwindSafe for EbpfVm<'a, E, I>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V