Struct Interpreter
pub struct Interpreter { /* private fields */ }Expand description
Core CIL instruction interpreter.
The interpreter executes CIL instructions one at a time, managing the
evaluation stack, local variables, and control flow. It uses a step-based
execution model where each call to Self::step executes a single instruction.
§Example
use dotscope::emulation::{Interpreter, EmulationContext, EmulationLimits};
let limits = EmulationLimits::default();
let mut interpreter = Interpreter::new(limits, address_space, PointerSize::Bit64);
// Execute instructions one at a time
loop {
match interpreter.step(&mut thread, &instruction)? {
StepResult::Continue => continue,
StepResult::Return { value } => break,
StepResult::Branch { target } => {
// Handle branch...
}
_ => {}
}
}Implementations§
§impl Interpreter
impl Interpreter
pub fn new(
limits: EmulationLimits,
address_space: Arc<AddressSpace>,
pointer_size: PointerSize,
) -> Self
pub fn new( limits: EmulationLimits, address_space: Arc<AddressSpace>, pointer_size: PointerSize, ) -> Self
Creates a new interpreter with the given execution limits, address space, and target pointer size.
§Arguments
limits- Execution limits to enforce during emulation.address_space- Shared address space for all memory operations.pointer_size- Target pointer size for native int/uint types.
§Returns
A new interpreter ready for execution.
pub fn address_space(&self) -> &Arc<AddressSpace>
pub fn address_space(&self) -> &Arc<AddressSpace>
Returns a reference to the address space.
pub fn ip(&self) -> &InstructionPointer
pub fn ip(&self) -> &InstructionPointer
Returns a reference to the current instruction pointer.
pub fn ip_mut(&mut self) -> &mut InstructionPointer
pub fn ip_mut(&mut self) -> &mut InstructionPointer
Returns a mutable reference to the instruction pointer.
pub fn stats(&self) -> &ExecutionStats
pub fn stats(&self) -> &ExecutionStats
Returns a reference to the execution statistics.
pub fn stats_mut(&mut self) -> &mut ExecutionStats
pub fn stats_mut(&mut self) -> &mut ExecutionStats
Returns a mutable reference to the execution statistics.
pub fn limits(&self) -> &EmulationLimits
pub fn limits(&self) -> &EmulationLimits
Returns a reference to the execution limits.
pub fn set_method(&mut self, method: Token)
pub fn set_method(&mut self, method: Token)
Sets the current method being executed.
§Arguments
method- The metadata token of the method to execute.
pub fn set_offset(&mut self, offset: u32)
pub fn set_offset(&mut self, offset: u32)
Sets the instruction pointer to a specific offset within the current method.
§Arguments
offset- The bytecode offset to branch to.
pub fn start(&mut self)
pub fn start(&mut self)
Starts execution timing for statistics tracking.
Call this before beginning instruction execution to enable timeout checks and elapsed time tracking.
pub fn reset(&mut self)
pub fn reset(&mut self)
Resets the interpreter state for a new execution.
Clears execution statistics and all active prefixes. The instruction
pointer is not modified; use set_method to change the target method.
pub fn check_limits(&self, call_depth: usize) -> Result<()>
pub fn check_limits(&self, call_depth: usize) -> Result<()>
pub fn step(
&mut self,
thread: &mut EmulationThread,
instruction: &Instruction,
) -> Result<StepResult>
pub fn step( &mut self, thread: &mut EmulationThread, instruction: &Instruction, ) -> Result<StepResult>
Executes a single instruction.
This is the main dispatch method that examines the instruction and delegates to the appropriate handler based on the opcode.
§Arguments
memory- The memory state containing stack, locals, and heap._context- The emulation context (currently unused but reserved for future use).instruction- The instruction to execute.
§Returns
A StepResult indicating the outcome:
Continue- Proceed to the next instructionBranch { target }- Branch to the specified offsetReturn { value }- Return from the current methodCall { method, args, is_virtual }- Call another method- Various other results for specific operations
§Errors
Returns an error if:
- Execution limits are exceeded (instruction count, call depth, timeout, memory)
- The instruction operand is invalid or missing
- A type mismatch occurs during execution
- The opcode is unsupported
Auto Trait Implementations§
impl Freeze for Interpreter
impl RefUnwindSafe for Interpreter
impl Send for Interpreter
impl Sync for Interpreter
impl Unpin for Interpreter
impl UnwindSafe for Interpreter
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
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