Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Refactor - Adds a reference to the loader in the VM #533

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn main() {
let memory_mapping = MemoryMapping::new(regions, config, sbpf_version).unwrap();

let mut vm = EbpfVm::new(
executable.get_config(),
executable.get_loader().clone(),
executable.get_sbpf_version(),
&mut context_object,
memory_mapping,
Expand Down
2 changes: 1 addition & 1 deletion src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ mod tests {
let executable = create_mockup_executable(&[]);
let mut context_object = TestContextObject::new(0);
let env = EbpfVm::new(
executable.get_config(),
executable.get_loader().clone(),
executable.get_sbpf_version(),
&mut context_object,
MemoryMapping::new_identity(),
Expand Down
16 changes: 10 additions & 6 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,12 @@ pub struct CallFrame {
///
/// let loader = std::sync::Arc::new(BuiltinProgram::new_mock());
/// let function_registry = FunctionRegistry::default();
/// let mut executable = Executable::<TestContextObject>::from_text_bytes(prog, loader, SBPFVersion::V2, function_registry).unwrap();
/// let mut executable = Executable::<TestContextObject>::from_text_bytes(prog, loader.clone(), SBPFVersion::V2, function_registry).unwrap();
/// executable.verify::<RequisiteVerifier>().unwrap();
/// let mut context_object = TestContextObject::new(1);
/// let config = executable.get_config();
/// let sbpf_version = executable.get_sbpf_version();
///
/// let mut stack = AlignedMemory::<{ebpf::HOST_ALIGN}>::zero_filled(config.stack_size());
/// let mut stack = AlignedMemory::<{ebpf::HOST_ALIGN}>::zero_filled(executable.get_config().stack_size());
/// let stack_len = stack.len();
/// let mut heap = AlignedMemory::<{ebpf::HOST_ALIGN}>::with_capacity(0);
///
Expand All @@ -339,9 +338,9 @@ pub struct CallFrame {
/// MemoryRegion::new_writable(mem, ebpf::MM_INPUT_START),
/// ];
///
/// let memory_mapping = MemoryMapping::new(regions, config, sbpf_version).unwrap();
/// let memory_mapping = MemoryMapping::new(regions, executable.get_config(), sbpf_version).unwrap();
///
/// let mut vm = EbpfVm::new(config, sbpf_version, &mut context_object, memory_mapping, stack_len);
/// let mut vm = EbpfVm::new(loader, sbpf_version, &mut context_object, memory_mapping, stack_len);
///
/// let (instruction_count, result) = vm.execute_program(&executable, true);
/// assert_eq!(instruction_count, 1);
Expand Down Expand Up @@ -379,6 +378,8 @@ pub struct EbpfVm<'a, C: ContextObject> {
pub memory_mapping: MemoryMapping<'a>,
/// Stack of CallFrames used by the Interpreter
pub call_frames: Vec<CallFrame>,
/// Loader built-in program
pub loader: Arc<BuiltinProgram<C>>,
/// TCP port for the debugger interface
#[cfg(feature = "debugger")]
pub debug_port: Option<u16>,
Expand All @@ -387,12 +388,13 @@ pub struct EbpfVm<'a, C: ContextObject> {
impl<'a, C: ContextObject> EbpfVm<'a, C> {
/// Creates a new virtual machine instance.
pub fn new(
config: &Config,
loader: Arc<BuiltinProgram<C>>,
sbpf_version: &SBPFVersion,
context_object: &'a mut C,
mut memory_mapping: MemoryMapping<'a>,
stack_len: usize,
) -> Self {
let config = loader.get_config();
let stack_pointer =
ebpf::MM_STACK_START.saturating_add(if sbpf_version.dynamic_stack_frames() {
// the stack is fully descending, frames start as empty and change size anytime r11 is modified
Expand All @@ -416,6 +418,7 @@ impl<'a, C: ContextObject> EbpfVm<'a, C> {
program_result: ProgramResult::Ok(0),
memory_mapping,
call_frames: vec![CallFrame::default(); config.max_call_depth],
loader,
#[cfg(feature = "debugger")]
debug_port: None,
}
Expand All @@ -429,6 +432,7 @@ impl<'a, C: ContextObject> EbpfVm<'a, C> {
executable: &Executable<C>,
interpreted: bool,
) -> (u64, ProgramResult) {
debug_assert!(Arc::ptr_eq(&self.loader, executable.get_loader()));
// R1 points to beginning of input memory, R10 to the stack of the first frame, R11 is the pc (hidden)
self.registers[1] = ebpf::MM_INPUT_START;
self.registers[ebpf::FRAME_PTR_REG] = self.stack_pointer;
Expand Down
2 changes: 1 addition & 1 deletion test_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ macro_rules! create_vm {
)
.unwrap();
let mut $vm_name = solana_rbpf::vm::EbpfVm::new(
$verified_executable.get_config(),
$verified_executable.get_loader().clone(),
$verified_executable.get_sbpf_version(),
$context_object,
memory_mapping,
Expand Down