Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete code for lazily loading programs #31395

Merged
merged 7 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
140 changes: 82 additions & 58 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use {
solana_measure::measure::Measure,
solana_program_runtime::{
compute_budget::ComputeBudget,
executor_cache::TransactionExecutorCache,
ic_logger_msg, ic_msg,
invoke_context::{BpfAllocator, InvokeContext, SyscallContext},
loaded_programs::{LoadProgramMetrics, LoadedProgram, LoadedProgramType},
Expand Down Expand Up @@ -55,7 +54,7 @@ use {
},
},
std::{
cell::{RefCell, RefMut},
cell::RefCell,
mem,
rc::Rc,
sync::{atomic::Ordering, Arc},
Expand Down Expand Up @@ -146,7 +145,6 @@ pub fn load_program_from_account(
feature_set: &FeatureSet,
compute_budget: &ComputeBudget,
log_collector: Option<Rc<RefCell<LogCollector>>>,
tx_executor_cache: Option<RefMut<TransactionExecutorCache>>,
program: &BorrowedAccount,
programdata: &BorrowedAccount,
debugging_features: bool,
Expand All @@ -159,17 +157,6 @@ pub fn load_program_from_account(
return Err(InstructionError::IncorrectProgramId);
}

if let Some(ref tx_executor_cache) = tx_executor_cache {
if let Some(loaded_program) = tx_executor_cache.get(program.get_key()) {
if loaded_program.is_tombstone() {
// We cached that the Executor does not exist, abort
return Err(InstructionError::InvalidAccountData);
}
// Executor exists and is cached, use it
return Ok((loaded_program, None));
}
}

let (programdata_offset, deployment_slot) =
get_programdata_offset_and_deployment_offset(&log_collector, program, programdata)?;

Expand Down Expand Up @@ -199,15 +186,6 @@ pub fn load_program_from_account(
false, /* reject_deployment_of_broken_elfs */
debugging_features,
)?);
if let Some(mut tx_executor_cache) = tx_executor_cache {
tx_executor_cache.set(
*program.get_key(),
loaded_program.clone(),
false,
feature_set.is_active(&delay_visibility_of_program_deployment::id()),
deployment_slot,
);
}

Ok((loaded_program, Some(load_program_metrics)))
}
Expand Down Expand Up @@ -583,39 +561,23 @@ fn process_instruction_inner(
return Err(Box::new(InstructionError::IncorrectProgramId));
}

let programdata_account = if bpf_loader_upgradeable::check_id(program_account.get_owner()) {
instruction_context
.try_borrow_program_account(
transaction_context,
instruction_context
.get_number_of_program_accounts()
.saturating_sub(2),
)
.ok()
} else {
None
};

let mut get_or_create_executor_time = Measure::start("get_or_create_executor_time");
let (executor, load_program_metrics) = load_program_from_account(
&invoke_context.feature_set,
invoke_context.get_compute_budget(),
log_collector,
Some(invoke_context.tx_executor_cache.borrow_mut()),
&program_account,
programdata_account.as_ref().unwrap_or(&program_account),
false, /* debugging_features */
)?;
let executor = invoke_context
.tx_executor_cache
.borrow()
.get(program_account.get_key())
.ok_or(InstructionError::InvalidAccountData)?;

if executor.is_tombstone() {
return Err(Box::new(InstructionError::InvalidAccountData));
}

drop(program_account);
drop(programdata_account);
get_or_create_executor_time.stop();
saturating_add_assign!(
invoke_context.timings.get_or_create_executor_us,
get_or_create_executor_time.as_us()
);
if let Some(load_program_metrics) = load_program_metrics {
load_program_metrics.submit_datapoint(&mut invoke_context.timings);
}

executor.usage_counter.fetch_add(1, Ordering::Relaxed);
match &executor.program {
Expand Down Expand Up @@ -1759,6 +1721,51 @@ mod tests {
)
}

fn process_instruction_with_loaded_programs(
loader_id: &Pubkey,
program_indices: &[IndexOfAccount],
instruction_data: &[u8],
transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>,
loaded_programs: &[(Pubkey, Arc<LoadedProgram>)],
) -> Vec<AccountSharedData> {
mock_process_instruction(
loader_id,
program_indices.to_vec(),
instruction_data,
transaction_accounts,
instruction_accounts,
expected_result,
super::process_instruction,
|invoke_context| {
let mut cache = invoke_context.tx_executor_cache.borrow_mut();
loaded_programs.iter().for_each(|(pubkey, program)| {
cache.set(*pubkey, program.clone(), true, false, 0)
});
},
|_invoke_context| {},
)
}

fn load_test_program(program_account: &AccountSharedData, loader_id: Pubkey) -> LoadedProgram {
let mut load_program_metrics = LoadProgramMetrics::default();

load_program_from_bytes(
&FeatureSet::all_enabled(),
&ComputeBudget::default(),
None,
&mut load_program_metrics,
program_account.data(),
&loader_id,
program_account.data().len(),
0,
true,
false,
)
.expect("Failed to load the test program")
}

fn load_program_account_from_elf(loader_id: &Pubkey, path: &str) -> AccountSharedData {
let mut file = File::open(path).expect("file open failed");
let mut elf = Vec::new();
Expand Down Expand Up @@ -1936,28 +1943,32 @@ mod tests {
is_writable: false,
};

let loaded_program = Arc::new(load_test_program(&program_account, loader_id));
pgarg66 marked this conversation as resolved.
Show resolved Hide resolved

// Case: No program account
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[],
&[],
Vec::new(),
Vec::new(),
Err(InstructionError::NotEnoughAccountKeys),
&[(program_id, loaded_program.clone())],
);

// Case: Only a program account
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
vec![(program_id, program_account.clone())],
Vec::new(),
Ok(()),
&[(program_id, loaded_program.clone())],
);

// Case: With program and parameter account
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -1967,10 +1978,11 @@ mod tests {
],
vec![parameter_meta.clone()],
Ok(()),
&[(program_id, loaded_program.clone())],
);

// Case: With duplicate accounts
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -1980,6 +1992,7 @@ mod tests {
],
vec![parameter_meta.clone(), parameter_meta],
Ok(()),
&[(program_id, loaded_program.clone())],
);

// Case: limited budget
Expand All @@ -1993,19 +2006,22 @@ mod tests {
super::process_instruction,
|invoke_context| {
invoke_context.mock_set_remaining(0);
let mut cache = invoke_context.tx_executor_cache.borrow_mut();
cache.set(program_id, loaded_program.clone(), true, false, 0);
},
|_invoke_context| {},
);

// Case: Account not a program
program_account.set_executable(false);
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
vec![(program_id, program_account)],
Vec::new(),
Err(InstructionError::IncorrectProgramId),
&[(program_id, loaded_program)],
);
}

Expand All @@ -2023,8 +2039,10 @@ mod tests {
is_writable: false,
};

let loaded_program = Arc::new(load_test_program(&program_account, loader_id));

// Case: With program and parameter account
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -2034,10 +2052,11 @@ mod tests {
],
vec![parameter_meta.clone()],
Ok(()),
&[(program_id, loaded_program.clone())],
);

// Case: With duplicate accounts
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -2047,6 +2066,7 @@ mod tests {
],
vec![parameter_meta.clone(), parameter_meta],
Ok(()),
&[(program_id, loaded_program)],
);
}

Expand All @@ -2064,8 +2084,10 @@ mod tests {
is_writable: false,
};

let loaded_program = Arc::new(load_test_program(&program_account, loader_id));

// Case: With program and parameter account
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -2075,10 +2097,11 @@ mod tests {
],
vec![parameter_meta.clone()],
Ok(()),
&[(program_id, loaded_program.clone())],
);

// Case: With duplicate accounts
process_instruction(
process_instruction_with_loaded_programs(
&loader_id,
&[0],
&[],
Expand All @@ -2088,6 +2111,7 @@ mod tests {
],
vec![parameter_meta.clone(), parameter_meta],
Ok(()),
&[(program_id, loaded_program)],
);
}

Expand Down
29 changes: 25 additions & 4 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use {
solana_account_decoder::parse_bpf_loader::{
parse_bpf_upgradeable_loader, BpfUpgradeableLoaderAccountType,
},
solana_bpf_loader_program::load_program_from_bytes,
solana_ledger::token_balances::collect_token_balances,
solana_program_runtime::{compute_budget::ComputeBudget, timings::ExecuteTimings},
solana_program_runtime::{
compute_budget::ComputeBudget, loaded_programs::LoadProgramMetrics, timings::ExecuteTimings,
},
solana_rbpf::vm::ContextObject,
solana_runtime::{
bank::{
Expand Down Expand Up @@ -1384,11 +1387,27 @@ fn assert_instruction_count() {
is_signer: false,
is_writable: false,
}];
transaction_accounts[0]
.1
.set_data_from_slice(&load_program_from_file(program_name));
let program_data = load_program_from_file(program_name);
transaction_accounts[0].1.set_data_from_slice(&program_data);
transaction_accounts[0].1.set_executable(true);

let mut load_program_metrics = LoadProgramMetrics::default();
let loaded_program = Arc::new(
load_program_from_bytes(
&FeatureSet::all_enabled(),
&ComputeBudget::default(),
None,
&mut load_program_metrics,
&program_data,
&loader_id,
program_data.len(),
0,
true,
false,
)
.expect("Failed to load the program"),
);

let prev_compute_meter = RefCell::new(0);
print!(" {:36} {:8}", program_name, *expected_consumption);
mock_process_instruction(
Expand All @@ -1401,6 +1420,8 @@ fn assert_instruction_count() {
solana_bpf_loader_program::process_instruction,
|invoke_context| {
*prev_compute_meter.borrow_mut() = invoke_context.get_remaining();
let mut cache = invoke_context.tx_executor_cache.borrow_mut();
cache.set(program_key, loaded_program.clone(), true, false, 0);
},
|invoke_context| {
let consumption = prev_compute_meter
Expand Down
1 change: 0 additions & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4210,7 +4210,6 @@ impl Bank {
&self.feature_set,
&self.runtime_config.compute_budget.unwrap_or_default(),
None, // log_collector
None,
&program,
programdata.as_ref().unwrap_or(&program),
debugging_features,
Expand Down
15 changes: 14 additions & 1 deletion runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7712,6 +7712,10 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
assert_eq!(*elf.get(i).unwrap(), *byte);
}

let loaded_program = bank
.load_program(&program_keypair.pubkey(), false)
.expect("Failed to load the program");

// Invoke deployed program
mock_process_instruction(
&bpf_loader_upgradeable::id(),
Expand All @@ -7724,7 +7728,16 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
Vec::new(),
Ok(()),
solana_bpf_loader_program::process_instruction,
|_invoke_context| {},
|invoke_context| {
Lichtso marked this conversation as resolved.
Show resolved Hide resolved
let mut cache = invoke_context.tx_executor_cache.borrow_mut();
cache.set(
program_keypair.pubkey(),
loaded_program.clone(),
true,
false,
0,
);
},
|_invoke_context| {},
);

Expand Down