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
7 changes: 5 additions & 2 deletions program-runtime/src/instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_sdk::{
},
ic_msg,
instruction::{Instruction, InstructionError},
keyed_account::keyed_account_at_index,
message::Message,
process_instruction::{Executor, InvokeContext, ProcessInstructionWithContext},
pubkey::Pubkey,
Expand Down Expand Up @@ -355,7 +356,8 @@ impl InstructionProcessor {
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
if let Some(root_account) = invoke_context.get_keyed_accounts()?.iter().next() {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
if let Ok(root_account) = keyed_account_at_index(keyed_accounts, 0) {
let root_id = root_account.unsigned_key();
let owner_id = &root_account.owner()?;
if solana_sdk::native_loader::check_id(owner_id) {
Expand Down Expand Up @@ -536,7 +538,8 @@ impl InstructionProcessor {
caller_write_privileges = Vec::with_capacity(1 + keyed_account_indices_obsolete.len());
caller_write_privileges.push(false);
for index in keyed_account_indices_obsolete.iter() {
caller_write_privileges.push(caller_keyed_accounts[*index].is_writable());
caller_write_privileges
.push(keyed_account_at_index(caller_keyed_accounts, *index)?.is_writable());
}
};
let mut account_indices = Vec::with_capacity(message.account_keys.len());
Expand Down
28 changes: 8 additions & 20 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use {
message::Message,
native_token::sol_to_lamports,
poh_config::PohConfig,
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
process_instruction::{self, stable_log, InvokeContext, ProcessInstructionWithContext},
program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR},
pubkey::Pubkey,
rent::Rent,
Expand Down Expand Up @@ -204,24 +204,6 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
var_addr: *mut u8,
) -> u64 {
let invoke_context = get_invoke_context();

let sysvar_data = match invoke_context.get_sysvar_data(id).ok_or_else(|| {
ic_msg!(invoke_context, "Unable to get Sysvar {}", id);
UNSUPPORTED_SYSVAR
}) {
Ok(sysvar_data) => sysvar_data,
Err(err) => return err,
};

let var: T = match bincode::deserialize(&sysvar_data) {
Ok(sysvar_data) => sysvar_data,
Err(_) => return UNSUPPORTED_SYSVAR,
};

unsafe {
*(var_addr as *mut _ as *mut T) = var;
}

if invoke_context
.get_compute_meter()
.try_borrow_mut()
Expand All @@ -233,7 +215,13 @@ fn get_sysvar<T: Default + Sysvar + Sized + serde::de::DeserializeOwned>(
panic!("Exceeded compute budget");
}

SUCCESS
match process_instruction::get_sysvar::<T>(invoke_context, id) {
Ok(sysvar_data) => unsafe {
*(var_addr as *mut _ as *mut T) = sysvar_data;
SUCCESS
},
Err(_) => UNSUPPORTED_SYSVAR,
}
}

struct SyscallStubs {}
Expand Down
56 changes: 41 additions & 15 deletions programs/bpf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,28 @@ fn bench_program_alu(bencher: &mut Bencher) {
.write_u64::<LittleEndian>(ARMSTRONG_LIMIT)
.unwrap();
inner_iter.write_u64::<LittleEndian>(0).unwrap();

let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let program_id = solana_sdk::pubkey::new_rand();
let accounts = [
(
program_id,
RefCell::new(AccountSharedData::new(0, 0, &loader_id)),
),
(
solana_sdk::pubkey::new_rand(),
RefCell::new(AccountSharedData::new(
1,
10000001,
&solana_sdk::pubkey::new_rand(),
)),
),
];
let keyed_accounts: Vec<_> = accounts
.iter()
.map(|(key, account)| solana_sdk::keyed_account::KeyedAccount::new(&key, false, &account))
.collect();
let mut invoke_context = MockInvokeContext::new(&program_id, keyed_accounts);

let elf = load_elf("bench_alu").unwrap();
let mut executable = <dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
Expand Down Expand Up @@ -254,29 +274,35 @@ fn bench_create_vm(bencher: &mut Bencher) {
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
const BUDGET: u64 = 200_000;
let loader_id = bpf_loader::id();

let accounts = [RefCell::new(AccountSharedData::new(
1,
10000001,
&solana_sdk::pubkey::new_rand(),
))];
let keys = [solana_sdk::pubkey::new_rand()];
let keyed_accounts: Vec<_> = keys
let program_id = solana_sdk::pubkey::new_rand();

let accounts = [
(
program_id,
RefCell::new(AccountSharedData::new(0, 0, &loader_id)),
),
(
solana_sdk::pubkey::new_rand(),
RefCell::new(AccountSharedData::new(
1,
10000001,
&solana_sdk::pubkey::new_rand(),
)),
),
];
let keyed_accounts: Vec<_> = accounts
.iter()
.zip(&accounts)
.map(|(key, account)| solana_sdk::keyed_account::KeyedAccount::new(&key, false, &account))
.collect();
let instruction_data = vec![0u8];

let mut invoke_context = MockInvokeContext::new(&loader_id, keyed_accounts);
let mut invoke_context = MockInvokeContext::new(&program_id, keyed_accounts);
invoke_context.compute_meter.remaining = BUDGET;

// Serialize account data
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
let (mut serialized, account_lengths) = serialize_parameters(
&loader_id,
&solana_sdk::pubkey::new_rand(),
keyed_accounts,
&program_id,
&invoke_context.get_keyed_accounts().unwrap()[1..],
&instruction_data,
)
.unwrap();
Expand Down
21 changes: 13 additions & 8 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,23 @@ fn upgrade_bpf_program(

fn run_program(
name: &str,
loader_id: &Pubkey,
program_id: &Pubkey,
parameter_accounts: Vec<KeyedAccount>,
instruction_data: &[u8],
) -> Result<u64, InstructionError> {
let path = create_bpf_path(name);
let mut file = File::open(path).unwrap();

let mut file = File::open(create_bpf_path(name)).unwrap();
let mut data = vec![];
file.read_to_end(&mut data).unwrap();
let loader_id = bpf_loader::id();

let mut invoke_context = MockInvokeContext::new(&program_id, parameter_accounts);
let (parameter_bytes, account_lengths) = serialize_parameters(
&loader_id,
program_id,
&parameter_accounts,
&invoke_context.get_keyed_accounts().unwrap()[1..],
&instruction_data,
)
.unwrap();
let mut invoke_context = MockInvokeContext::new(&loader_id, parameter_accounts);
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };

Expand Down Expand Up @@ -1405,11 +1404,17 @@ fn assert_instruction_count() {
let mut passed = true;
println!("\n {:36} expected actual diff", "BPF program");
for program in programs.iter() {
let loader_id = bpf_loader::id();
let program_id = Pubkey::new_unique();
let key = Pubkey::new_unique();
let mut program_account = RefCell::new(AccountSharedData::new(0, 0, &loader_id));
let mut account = RefCell::new(AccountSharedData::default());
let parameter_accounts = vec![KeyedAccount::new(&key, false, &mut account)];
let count = run_program(program.0, &program_id, parameter_accounts, &[]).unwrap();
let parameter_accounts = vec![
KeyedAccount::new(&program_id, false, &mut program_account),
KeyedAccount::new(&key, false, &mut account),
];
let count =
run_program(program.0, &loader_id, &program_id, parameter_accounts, &[]).unwrap();
let diff: i64 = count as i64 - program.1 as i64;
println!(
" {:36} {:8} {:6} {:+5} ({:+3.0}%)",
Expand Down
12 changes: 8 additions & 4 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3335,7 +3335,8 @@ mod tests {
let mut data = vec![];
bincode::serialize_into(&mut data, &src_clock).unwrap();
invoke_context
.sysvars
.get_sysvars()
.borrow_mut()
.push((sysvar::clock::id(), Some(Rc::new(data))));

let mut syscall = SyscallGetClockSysvar {
Expand Down Expand Up @@ -3380,7 +3381,8 @@ mod tests {
let mut data = vec![];
bincode::serialize_into(&mut data, &src_epochschedule).unwrap();
invoke_context
.sysvars
.get_sysvars()
.borrow_mut()
.push((sysvar::epoch_schedule::id(), Some(Rc::new(data))));

let mut syscall = SyscallGetEpochScheduleSysvar {
Expand Down Expand Up @@ -3432,7 +3434,8 @@ mod tests {
let mut data = vec![];
bincode::serialize_into(&mut data, &src_fees).unwrap();
invoke_context
.sysvars
.get_sysvars()
.borrow_mut()
.push((sysvar::fees::id(), Some(Rc::new(data))));

let mut syscall = SyscallGetFeesSysvar {
Expand Down Expand Up @@ -3475,7 +3478,8 @@ mod tests {
let mut data = vec![];
bincode::serialize_into(&mut data, &src_rent).unwrap();
invoke_context
.sysvars
.get_sysvars()
.borrow_mut()
.push((sysvar::rent::id(), Some(Rc::new(data))));

let mut syscall = SyscallGetRentSysvar {
Expand Down
21 changes: 10 additions & 11 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,19 @@ pub fn process_instruction(
}

let mut counter = 0;
let mut keyed_accounts_iter = keyed_accounts.iter().skip(2);
for (signer, _) in key_list.keys.iter().filter(|(_, is_signer)| *is_signer) {
counter += 1;
if signer != config_keyed_account.unsigned_key() {
let signer_account = keyed_accounts_iter.next();
if signer_account.is_none() {
ic_msg!(
invoke_context,
"account {:?} is not in account list",
signer
);
return Err(InstructionError::MissingRequiredSignature);
}
let signer_key = signer_account.unwrap().signer_key();
let signer_account =
keyed_account_at_index(keyed_accounts, counter + 1).map_err(|_| {
ic_msg!(
invoke_context,
"account {:?} is not in account list",
signer,
);
InstructionError::MissingRequiredSignature
})?;
let signer_key = signer_account.signer_key();
if signer_key.is_none() {
ic_msg!(
invoke_context,
Expand Down
26 changes: 13 additions & 13 deletions programs/stake/src/stake_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn process_instruction(
return Err(InstructionError::InvalidAccountOwner);
}

let signers = get_signers(&keyed_accounts[1..]);
let signers = get_signers(&keyed_accounts[first_instruction_account..]);
match limited_deserialize(data)? {
StakeInstruction::Initialize(authorized, lockup) => me.initialize(
&authorized,
Expand Down Expand Up @@ -330,15 +330,15 @@ mod tests {
account::{self, Account, AccountSharedData, WritableAccount},
instruction::{AccountMeta, Instruction},
keyed_account::create_keyed_accounts_unified,
process_instruction::{mock_set_sysvar, MockInvokeContext},
process_instruction::MockInvokeContext,
pubkey::Pubkey,
rent::Rent,
stake::{
config as stake_config,
instruction::{self, LockupArgs},
state::{Authorized, Lockup, StakeAuthorize},
},
sysvar::stake_history::StakeHistory,
sysvar::{stake_history::StakeHistory, Sysvar},
};
use std::{cell::RefCell, rc::Rc, str::FromStr};

Expand Down Expand Up @@ -442,12 +442,12 @@ mod tests {
&processor_id,
create_keyed_accounts_unified(&keyed_accounts),
);
mock_set_sysvar(
&mut invoke_context,
sysvar::clock::id(),
sysvar::clock::Clock::default(),
)
.unwrap();
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
invoke_context
.get_sysvars()
.borrow_mut()
.push((sysvar::clock::id(), Some(Rc::new(data))));
super::process_instruction(1, &instruction.data, &mut invoke_context)
}
}
Expand Down Expand Up @@ -1100,11 +1100,11 @@ mod tests {
];
let mut invoke_context =
MockInvokeContext::new(&id(), create_keyed_accounts_unified(&keyed_accounts));
let clock = Clock::default();
let mut data = vec![];
bincode::serialize_into(&mut data, &clock).unwrap();
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
invoke_context
.sysvars
.get_sysvars()
.borrow_mut()
.push((sysvar::clock::id(), Some(Rc::new(data))));

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion programs/vote/src/vote_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub fn process_instruction(
return Err(InstructionError::InvalidAccountOwner);
}

let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[1..]);
let signers: HashSet<Pubkey> = get_signers(&keyed_accounts[first_instruction_account..]);
match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => {
verify_rent_exemption(
Expand Down
Loading