Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
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
23 changes: 18 additions & 5 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,25 @@ impl ComputeMeter {
pub fn get_remaining(&self) -> u64 {
self.remaining
}
/// Set compute units
///
/// Only use for tests and benchmarks
pub fn mock_set_remaining(&mut self, remaining: u64) {
self.remaining = remaining;
}
/// Construct a new one with the given remaining units
pub fn new_ref(remaining: u64) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self { remaining }))
}
}

pub struct InvokeContextStackFrame<'a> {
pub struct StackFrame<'a> {
pub number_of_program_accounts: usize,
pub keyed_accounts: Vec<KeyedAccount<'a>>,
pub keyed_accounts_range: std::ops::Range<usize>,
}

impl<'a> InvokeContextStackFrame<'a> {
impl<'a> StackFrame<'a> {
pub fn new(number_of_program_accounts: usize, keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
let keyed_accounts_range = std::ops::Range {
start: 0,
Expand All @@ -126,7 +132,7 @@ impl<'a> InvokeContextStackFrame<'a> {

pub struct ThisInvokeContext<'a> {
instruction_index: usize,
invoke_stack: Vec<InvokeContextStackFrame<'a>>,
invoke_stack: Vec<StackFrame<'a>>,
rent: Rent,
pre_accounts: Vec<PreAccount>,
accounts: &'a [(Pubkey, Rc<RefCell<AccountSharedData>>)],
Expand Down Expand Up @@ -193,7 +199,7 @@ impl<'a> ThisInvokeContext<'a> {
accounts,
builtin_programs,
sysvars,
None,
Some(LogCollector::new_ref()),
ComputeBudget::default(),
ComputeMeter::new_ref(std::i64::MAX as u64),
Rc::new(RefCell::new(Executors::default())),
Expand Down Expand Up @@ -269,6 +275,8 @@ pub trait InvokeContext {
fn process_instruction(&mut self, instruction_data: &[u8]) -> Result<(), InstructionError>;
/// Get the program ID of the currently executing program
fn get_caller(&self) -> Result<&Pubkey, InstructionError>;
/// Get the owner of the currently executing program
fn get_loader(&self) -> Result<Pubkey, InstructionError>;
/// Removes the first keyed account
#[deprecated(
since = "1.9.0",
Expand Down Expand Up @@ -416,7 +424,7 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
}))
.collect::<Vec<_>>();

self.invoke_stack.push(InvokeContextStackFrame::new(
self.invoke_stack.push(StackFrame::new(
program_indices.len(),
create_keyed_accounts_unified(keyed_accounts.as_slice()),
));
Expand Down Expand Up @@ -800,6 +808,11 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
.and_then(|frame| frame.program_id())
.ok_or(InstructionError::CallDepth)
}
fn get_loader(&self) -> Result<Pubkey, InstructionError> {
self.get_instruction_keyed_accounts()
.and_then(|keyed_accounts| keyed_accounts.first().ok_or(InstructionError::CallDepth))
.and_then(|keyed_account| keyed_account.owner())
}
fn remove_first_keyed_account(&mut self) -> Result<(), InstructionError> {
if !self.is_feature_active(&remove_native_loader::id()) {
let stack_frame = &mut self
Expand Down
4 changes: 4 additions & 0 deletions program-runtime/src/log_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl LogCollector {
}
}

pub fn get_recorded_content(&self) -> &[String] {
self.messages.as_slice()
}

pub fn new_ref() -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self::default()))
}
Expand Down
23 changes: 7 additions & 16 deletions programs/bpf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use solana_bpf_loader_program::{
};
use solana_measure::measure::Measure;
use solana_program_runtime::invoke_context::{with_mock_invoke_context, InvokeContext};
use solana_rbpf::{elf::Executable, vm::{Config, InstructionMeter, SyscallRegistry}};
use solana_rbpf::{
elf::Executable,
vm::{Config, InstructionMeter, SyscallRegistry},
};
use solana_runtime::{
bank::Bank,
bank_client::BankClient,
Expand Down Expand Up @@ -107,7 +110,6 @@ fn bench_program_alu(bencher: &mut Bencher) {
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };
let mut vm = create_vm(
&loader_id,
&executable,
&mut inner_iter,
invoke_context,
Expand Down Expand Up @@ -203,12 +205,7 @@ fn bench_create_vm(bencher: &mut Bencher) {
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, |invoke_context| {
const BUDGET: u64 = 200_000;
let compute_meter = invoke_context.get_compute_meter();
{
let mut compute_meter = compute_meter.borrow_mut();
let to_consume = compute_meter.get_remaining() - BUDGET;
compute_meter.consume(to_consume).unwrap();
}
invoke_context.get_compute_meter().borrow_mut().mock_set_remaining(BUDGET);

// Serialize account data
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
Expand All @@ -230,7 +227,6 @@ fn bench_create_vm(bencher: &mut Bencher) {

bencher.iter(|| {
let _ = create_vm(
&loader_id,
&executable,
serialized.as_slice_mut(),
invoke_context,
Expand All @@ -247,12 +243,7 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
let loader_id = bpf_loader::id();
with_mock_invoke_context(loader_id, 10000001, |invoke_context| {
const BUDGET: u64 = 200_000;
let compute_meter = invoke_context.get_compute_meter();
{
let mut compute_meter = compute_meter.borrow_mut();
let to_consume = compute_meter.get_remaining() - BUDGET;
compute_meter.consume(to_consume).unwrap();
}
invoke_context.get_compute_meter().borrow_mut().mock_set_remaining(BUDGET);

// Serialize account data
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
Expand All @@ -271,9 +262,9 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
register_syscalls(invoke_context).unwrap(),
)
.unwrap();
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };
let mut vm = create_vm(
&loader_id,
&executable,
serialized.as_slice_mut(),
invoke_context,
Expand Down
1 change: 0 additions & 1 deletion programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ fn run_program(name: &str) -> u64 {
let mut parameter_bytes = parameter_bytes.clone();
{
let mut vm = create_vm(
&loader_id,
&executable,
parameter_bytes.as_slice_mut(),
invoke_context,
Expand Down
10 changes: 1 addition & 9 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ fn check_loader_id(id: &Pubkey) -> bool {

/// Create the BPF virtual machine
pub fn create_vm<'a>(
loader_id: &'a Pubkey,
program: &'a Executable<BpfError, ThisInstructionMeter>,
parameter_bytes: &mut [u8],
invoke_context: &'a mut dyn InvokeContext,
Expand All @@ -168,13 +167,7 @@ pub fn create_vm<'a>(
let mut heap =
AlignedMemory::new_with_size(compute_budget.heap_size.unwrap_or(HEAP_LENGTH), HOST_ALIGN);
let mut vm = EbpfVm::new(program, heap.as_slice_mut(), parameter_bytes)?;
syscalls::bind_syscall_context_objects(
loader_id,
&mut vm,
invoke_context,
heap,
orig_data_lens,
)?;
syscalls::bind_syscall_context_objects(&mut vm, invoke_context, heap, orig_data_lens)?;
Ok(vm)
}

Expand Down Expand Up @@ -984,7 +977,6 @@ impl Executor for BpfExecutor {
let program_id = &invoke_context.get_caller()?.clone();
let compute_meter = invoke_context.get_compute_meter();
let mut vm = match create_vm(
loader_id,
&self.executable,
parameter_bytes.as_slice_mut(),
invoke_context,
Expand Down
Loading