Skip to content

Commit

Permalink
Bump solana_rbpf to 0.2.36 (solana-labs#28794)
Browse files Browse the repository at this point in the history
* Bumps solana_rbpf to v0.2.36

* Removes ThisInstructionMeter.

* Removes one "unsafe" expression.

* Removes redundant call to solana_rbpf::elf::register_bpf_function().

* Adjusts SyscallFunction and SyscallRegistry.

* Inlines ProgramEnvironment into EbpfVm.

* Refactors trait SyscallConsume into fn consume_compute_meter().

* Inlines ComputeMeter into InvokeContext.

* Removes solana-metrics dependency from bpf_loader.

* Replaces RBPF tracer functionality by the debugger.

* Take compute_units_consumed from execute_program().

* Merges execute_program_interpreted() and execute_program_jit().
  • Loading branch information
Lichtso authored Nov 15, 2022
1 parent 84cfdf2 commit ff1ff58
Show file tree
Hide file tree
Showing 21 changed files with 354 additions and 528 deletions.
33 changes: 30 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ solana-tpu-client = { path = "../tpu-client", version = "=1.15.0" }
solana-transaction-status = { path = "../transaction-status", version = "=1.15.0" }
solana-version = { path = "../version", version = "=1.15.0" }
solana-vote-program = { path = "../programs/vote", version = "=1.15.0" }
solana_rbpf = "=0.2.35"
solana_rbpf = "=0.2.36"
spl-memo = { version = "=3.0.1", features = ["no-entrypoint"] }
thiserror = "1.0.31"
tiny-bip39 = "0.8.2"
Expand Down
9 changes: 4 additions & 5 deletions cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
clap::{App, AppSettings, Arg, ArgMatches, SubCommand},
log::*,
solana_account_decoder::{UiAccountEncoding, UiDataSliceConfig},
solana_bpf_loader_program::{syscalls::register_syscalls, ThisInstructionMeter},
solana_bpf_loader_program::syscalls::register_syscalls,
solana_clap_utils::{self, input_parsers::*, input_validators::*, keypair::*},
solana_cli_output::{
CliProgram, CliProgramAccountType, CliProgramAuthority, CliProgramBuffer, CliProgramId,
Expand Down Expand Up @@ -2004,7 +2004,7 @@ fn read_and_verify_elf(program_location: &str) -> Result<Vec<u8>, Box<dyn std::e
let invoke_context = InvokeContext::new_mock(&mut transaction_context, &[]);

// Verify the program
let executable = Executable::<ThisInstructionMeter>::from_elf(
let executable = Executable::<InvokeContext>::from_elf(
&program_data,
Config {
reject_broken_elfs: true,
Expand All @@ -2014,9 +2014,8 @@ fn read_and_verify_elf(program_location: &str) -> Result<Vec<u8>, Box<dyn std::e
)
.map_err(|err| format!("ELF error: {}", err))?;

let _ =
VerifiedExecutable::<RequisiteVerifier, ThisInstructionMeter>::from_executable(executable)
.map_err(|err| format!("ELF error: {}", err))?;
let _ = VerifiedExecutable::<RequisiteVerifier, InvokeContext>::from_executable(executable)
.map_err(|err| format!("ELF error: {}", err))?;

Ok(program_data)
}
Expand Down
1 change: 1 addition & 0 deletions program-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ solana-frozen-abi = { path = "../frozen-abi", version = "=1.15.0" }
solana-frozen-abi-macro = { path = "../frozen-abi/macro", version = "=1.15.0" }
solana-measure = { path = "../measure", version = "=1.15.0" }
solana-metrics = { path = "../metrics", version = "=1.15.0" }
solana_rbpf = "=0.2.36"
solana-sdk = { path = "../sdk", version = "=1.15.0" }
thiserror = "1.0"

Expand Down
72 changes: 36 additions & 36 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
timings::{ExecuteDetailsTimings, ExecuteTimings},
},
solana_measure::measure::Measure,
solana_rbpf::vm::ContextObject,
solana_sdk::{
account::{AccountSharedData, ReadableAccount},
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
Expand Down Expand Up @@ -58,33 +59,20 @@ impl std::fmt::Debug for BuiltinProgram {
}
}

/// Compute meter
pub struct ComputeMeter {
remaining: u64,
}
impl ComputeMeter {
/// Consume compute units
pub fn consume(&mut self, amount: u64) -> Result<(), InstructionError> {
let exceeded = self.remaining < amount;
self.remaining = self.remaining.saturating_sub(amount);
if exceeded {
return Err(InstructionError::ComputationalBudgetExceeded);
}
Ok(())
impl<'a> ContextObject for InvokeContext<'a> {
fn trace(&mut self, state: [u64; 12]) {
self.trace_log.push(state);
}
/// Get the number of remaining compute units
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;

fn consume(&mut self, amount: u64) {
// 1 to 1 instruction to compute unit mapping
// ignore overflow, Ebpf will bail if exceeded
let mut compute_meter = self.compute_meter.borrow_mut();
*compute_meter = compute_meter.saturating_sub(amount);
}
/// Construct a new one with the given remaining units
pub fn new_ref(remaining: u64) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self { remaining }))

fn get_remaining(&self) -> u64 {
*self.compute_meter.borrow()
}
}

Expand Down Expand Up @@ -116,10 +104,11 @@ pub struct InvokeContext<'a> {
pre_accounts: Vec<PreAccount>,
builtin_programs: &'a [BuiltinProgram],
pub sysvar_cache: Cow<'a, SysvarCache>,
pub trace_log: Vec<[u64; 12]>,
log_collector: Option<Rc<RefCell<LogCollector>>>,
compute_budget: ComputeBudget,
current_compute_budget: ComputeBudget,
compute_meter: Rc<RefCell<ComputeMeter>>,
compute_meter: RefCell<u64>,
accounts_data_meter: AccountsDataMeter,
pub tx_executor_cache: Rc<RefCell<TransactionExecutorCache>>,
pub feature_set: Arc<FeatureSet>,
Expand Down Expand Up @@ -150,10 +139,11 @@ impl<'a> InvokeContext<'a> {
pre_accounts: Vec::new(),
builtin_programs,
sysvar_cache,
trace_log: Vec::new(),
log_collector,
current_compute_budget: compute_budget,
compute_budget,
compute_meter: ComputeMeter::new_ref(compute_budget.compute_unit_limit),
compute_meter: RefCell::new(compute_budget.compute_unit_limit),
accounts_data_meter: AccountsDataMeter::new(prev_accounts_data_len),
tx_executor_cache,
feature_set,
Expand Down Expand Up @@ -746,7 +736,7 @@ impl<'a> InvokeContext<'a> {
self.transaction_context
.set_return_data(program_id, Vec::new())?;

let pre_remaining_units = self.compute_meter.borrow().get_remaining();
let pre_remaining_units = self.get_remaining();
let result = if builtin_id == program_id {
let logger = self.get_log_collector();
stable_log::program_invoke(&logger, &program_id, self.get_stack_height());
Expand All @@ -761,7 +751,7 @@ impl<'a> InvokeContext<'a> {
} else {
(entry.process_instruction)(first_instruction_account, self)
};
let post_remaining_units = self.compute_meter.borrow().get_remaining();
let post_remaining_units = self.get_remaining();
*compute_units_consumed = pre_remaining_units.saturating_sub(post_remaining_units);

process_executable_chain_time.stop();
Expand All @@ -784,9 +774,22 @@ impl<'a> InvokeContext<'a> {
self.log_collector.clone()
}

/// Get this invocation's ComputeMeter
pub fn get_compute_meter(&self) -> Rc<RefCell<ComputeMeter>> {
self.compute_meter.clone()
/// Consume compute units
pub fn consume_checked(&self, amount: u64) -> Result<(), InstructionError> {
let mut compute_meter = self.compute_meter.borrow_mut();
let exceeded = *compute_meter < amount;
*compute_meter = compute_meter.saturating_sub(amount);
if exceeded {
return Err(InstructionError::ComputationalBudgetExceeded);
}
Ok(())
}

/// Set compute units
///
/// Only use for tests and benchmarks
pub fn mock_set_remaining(&self, remaining: u64) {
*self.compute_meter.borrow_mut() = remaining;
}

/// Get this invocation's AccountsDataMeter
Expand Down Expand Up @@ -1138,10 +1141,7 @@ mod tests {
compute_units_to_consume,
desired_result,
} => {
invoke_context
.get_compute_meter()
.borrow_mut()
.consume(compute_units_to_consume)?;
invoke_context.consume_checked(compute_units_to_consume)?;
return desired_result;
}
MockInstruction::Resize { new_len } => instruction_context
Expand Down
8 changes: 2 additions & 6 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use {
instruction::{Instruction, InstructionError},
native_token::sol_to_lamports,
poh_config::PohConfig,
program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR},
program_error::{ProgramError, UNSUPPORTED_SYSVAR},
pubkey::Pubkey,
rent::Rent,
signature::{Keypair, Signer},
Expand Down Expand Up @@ -197,11 +197,7 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned + Clone>
) -> u64 {
let invoke_context = get_invoke_context();
if invoke_context
.get_compute_meter()
.try_borrow_mut()
.map_err(|_| ACCOUNT_BORROW_FAILED)
.unwrap()
.consume(invoke_context.get_compute_budget().sysvar_base_cost + T::size_of() as u64)
.consume_checked(invoke_context.get_compute_budget().sysvar_base_cost + T::size_of() as u64)
.is_err()
{
panic!("Exceeded compute budget");
Expand Down
3 changes: 1 addition & 2 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ byteorder = "1.4.3"
libsecp256k1 = "0.6.0"
log = "0.4.17"
solana-measure = { path = "../../measure", version = "=1.15.0" }
solana-metrics = { path = "../../metrics", version = "=1.15.0" }
solana-program-runtime = { path = "../../program-runtime", version = "=1.15.0" }
solana-sdk = { path = "../../sdk", version = "=1.15.0" }
solana-zk-token-sdk = { path = "../../zk-token-sdk", version = "=1.15.0" }
solana_rbpf = "=0.2.35"
solana_rbpf = "=0.2.36"
thiserror = "1.0"

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions programs/bpf_loader/src/allocator_bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl BpfAllocator {
pos: 0,
}
}

pub fn get_heap(&mut self) -> &mut [u8] {
self.heap.as_slice_mut()
}
}

impl Alloc for BpfAllocator {
Expand Down
Loading

0 comments on commit ff1ff58

Please sign in to comment.