Skip to content

Commit

Permalink
add feature gate to account and borrowed account apis
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Dec 19, 2023
1 parent ec55086 commit 8872bf2
Show file tree
Hide file tree
Showing 24 changed files with 619 additions and 299 deletions.
13 changes: 11 additions & 2 deletions cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,15 @@ fn get_default_program_keypair(program_location: &Option<String>) -> Keypair {
program_keypair
}

fn is_account_executable(account: &Account) -> bool {
if account.owner == bpf_loader_deprecated::id() || account.owner == bpf_loader::id() {
account.executable
} else {
let feature_set = FeatureSet::all_enabled();
is_executable(account, &feature_set)
}
}

/// Deploy program using upgradeable loader. It also can process program upgrades
#[allow(clippy::too_many_arguments)]
fn process_program_deploy(
Expand Down Expand Up @@ -1092,7 +1101,7 @@ fn process_program_deploy(
.into());
}

if !is_executable(&account) {
if !is_account_executable(&account) {
// Continue an initial deploy
true
} else if let Ok(UpgradeableLoaderState::Program {
Expand Down Expand Up @@ -2444,7 +2453,7 @@ fn complete_partial_program_init(
) -> Result<(Vec<Instruction>, u64), Box<dyn std::error::Error>> {
let mut instructions: Vec<Instruction> = vec![];
let mut balance_needed = 0;
if is_executable(account) {
if is_account_executable(account) {
return Err("Buffer account is already executable".into());
}
if account.owner != *loader_id && !system_program::check_id(&account.owner) {
Expand Down
43 changes: 31 additions & 12 deletions cli/tests/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use {
account_utils::StateMut,
bpf_loader_upgradeable::{self, UpgradeableLoaderState},
commitment_config::CommitmentConfig,
feature_set::FeatureSet,
pubkey::Pubkey,
signature::{Keypair, NullSigner, Signer},
},
Expand Down Expand Up @@ -101,7 +102,7 @@ fn test_cli_program_deploy_non_upgradeable() {
let account0 = rpc_client.get_account(&program_id).unwrap();
assert_eq!(account0.lamports, minimum_balance_for_program);
assert_eq!(account0.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&account0));
assert!(is_executable(&account0, &FeatureSet::all_enabled()));

let (programdata_pubkey, _) =
Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id());
Expand All @@ -111,7 +112,10 @@ fn test_cli_program_deploy_non_upgradeable() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand Down Expand Up @@ -139,7 +143,7 @@ fn test_cli_program_deploy_non_upgradeable() {
.unwrap();
assert_eq!(account1.lamports, minimum_balance_for_program);
assert_eq!(account1.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&account1));
assert!(is_executable(&account1, &FeatureSet::all_enabled()));
let (programdata_pubkey, _) = Pubkey::find_program_address(
&[custom_address_keypair.pubkey().as_ref()],
&bpf_loader_upgradeable::id(),
Expand All @@ -150,7 +154,10 @@ fn test_cli_program_deploy_non_upgradeable() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand Down Expand Up @@ -378,7 +385,7 @@ fn test_cli_program_deploy_with_authority() {
let program_account = rpc_client.get_account(&program_keypair.pubkey()).unwrap();
assert_eq!(program_account.lamports, minimum_balance_for_program);
assert_eq!(program_account.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&program_account));
assert!(is_executable(&program_account, &FeatureSet::all_enabled()));
let (programdata_pubkey, _) = Pubkey::find_program_address(
&[program_keypair.pubkey().as_ref()],
&bpf_loader_upgradeable::id(),
Expand All @@ -389,7 +396,10 @@ fn test_cli_program_deploy_with_authority() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand Down Expand Up @@ -423,7 +433,7 @@ fn test_cli_program_deploy_with_authority() {
let program_account = rpc_client.get_account(&program_pubkey).unwrap();
assert_eq!(program_account.lamports, minimum_balance_for_program);
assert_eq!(program_account.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&program_account));
assert!(is_executable(&program_account, &FeatureSet::all_enabled()));
let (programdata_pubkey, _) =
Pubkey::find_program_address(&[program_pubkey.as_ref()], &bpf_loader_upgradeable::id());
let programdata_account = rpc_client.get_account(&programdata_pubkey).unwrap();
Expand All @@ -432,7 +442,10 @@ fn test_cli_program_deploy_with_authority() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand All @@ -457,7 +470,7 @@ fn test_cli_program_deploy_with_authority() {
let program_account = rpc_client.get_account(&program_pubkey).unwrap();
assert_eq!(program_account.lamports, minimum_balance_for_program);
assert_eq!(program_account.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&program_account));
assert!(is_executable(&program_account, &FeatureSet::all_enabled()));
let (programdata_pubkey, _) =
Pubkey::find_program_address(&[program_pubkey.as_ref()], &bpf_loader_upgradeable::id());
let programdata_account = rpc_client.get_account(&programdata_pubkey).unwrap();
Expand All @@ -466,7 +479,10 @@ fn test_cli_program_deploy_with_authority() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand Down Expand Up @@ -513,7 +529,7 @@ fn test_cli_program_deploy_with_authority() {
let program_account = rpc_client.get_account(&program_pubkey).unwrap();
assert_eq!(program_account.lamports, minimum_balance_for_program);
assert_eq!(program_account.owner, bpf_loader_upgradeable::id());
assert!(is_executable(&program_account));
assert!(is_executable(&program_account, &FeatureSet::all_enabled()));
let (programdata_pubkey, _) =
Pubkey::find_program_address(&[program_pubkey.as_ref()], &bpf_loader_upgradeable::id());
let programdata_account = rpc_client.get_account(&programdata_pubkey).unwrap();
Expand All @@ -522,7 +538,10 @@ fn test_cli_program_deploy_with_authority() {
minimum_balance_for_programdata
);
assert_eq!(programdata_account.owner, bpf_loader_upgradeable::id());
assert!(!is_executable(&programdata_account));
assert!(!is_executable(
&programdata_account,
&FeatureSet::all_enabled()
));
assert_eq!(
programdata_account.data[UpgradeableLoaderState::size_of_programdata_metadata()..],
program_data[..]
Expand Down
1 change: 1 addition & 0 deletions ledger-tool/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ pub fn program(ledger_path: &Path, matches: &ArgMatches<'_>) {
.get_current_instruction_context()
.unwrap(),
true, // copy_account_data
&invoke_context.feature_set,
)
.unwrap();

Expand Down
14 changes: 7 additions & 7 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl<'a> InvokeContext<'a> {
})?;
let borrowed_program_account = instruction_context
.try_borrow_instruction_account(self.transaction_context, program_account_index)?;
if !borrowed_program_account.is_executable() {
if !borrowed_program_account.is_executable(&self.feature_set) {
ic_msg!(self, "Account {} is not executable", callee_program_id);
return Err(InstructionError::AccountNotExecutable);
}
Expand Down Expand Up @@ -821,17 +821,17 @@ mod tests {
MockInstruction::NoopFail => return Err(InstructionError::GenericError),
MockInstruction::ModifyOwned => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::ModifyNotOwned => instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::ModifyReadonly => instruction_context
.try_borrow_instruction_account(transaction_context, 2)?
.set_data_from_slice(&[1])?,
.set_data_from_slice(&[1], &invoke_context.feature_set)?,
MockInstruction::UnbalancedPush => {
instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_add_lamports(1)?;
.checked_add_lamports(1, &invoke_context.feature_set)?;
let program_id = *transaction_context.get_key_of_account_at_index(3)?;
let metas = vec![
AccountMeta::new_readonly(
Expand Down Expand Up @@ -862,7 +862,7 @@ mod tests {
}
MockInstruction::UnbalancedPop => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_add_lamports(1)?,
.checked_add_lamports(1, &invoke_context.feature_set)?,
MockInstruction::ConsumeComputeUnits {
compute_units_to_consume,
desired_result,
Expand All @@ -874,7 +874,7 @@ mod tests {
}
MockInstruction::Resize { new_len } => instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.set_data(vec![0; new_len as usize])?,
.set_data(vec![0; new_len as usize], &invoke_context.feature_set)?,
}
} else {
return Err(InstructionError::InvalidInstructionData);
Expand Down
16 changes: 8 additions & 8 deletions program-runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,16 @@ mod tests {
MockSystemInstruction::TransferLamports { lamports } => {
instruction_context
.try_borrow_instruction_account(transaction_context, 0)?
.checked_sub_lamports(lamports)?;
.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.checked_add_lamports(lamports)?;
.checked_add_lamports(lamports, &invoke_context.feature_set)?;
Ok(())
}
MockSystemInstruction::ChangeData { data } => {
instruction_context
.try_borrow_instruction_account(transaction_context, 1)?
.set_data(vec![data])?;
.set_data(vec![data], &invoke_context.feature_set)?;
Ok(())
}
}
Expand Down Expand Up @@ -457,14 +457,14 @@ mod tests {
MockSystemInstruction::DoWork { lamports, data } => {
let mut dup_account = instruction_context
.try_borrow_instruction_account(transaction_context, 2)?;
dup_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
dup_account.set_data(vec![data])?;
dup_account.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
to_account.checked_add_lamports(lamports, &invoke_context.feature_set)?;
dup_account.set_data(vec![data], &invoke_context.feature_set)?;
drop(dup_account);
let mut from_account = instruction_context
.try_borrow_instruction_account(transaction_context, 0)?;
from_account.checked_sub_lamports(lamports)?;
to_account.checked_add_lamports(lamports)?;
from_account.checked_sub_lamports(lamports, &invoke_context.feature_set)?;
to_account.checked_add_lamports(lamports, &invoke_context.feature_set)?;
Ok(())
}
}
Expand Down
24 changes: 16 additions & 8 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub fn invoke_builtin_function(
.transaction_context
.get_current_instruction_context()?,
true, // copy_account_data // There is no VM so direct mapping can not be implemented here
&invoke_context.feature_set,
)?;

// Deserialize data back into instruction params
Expand Down Expand Up @@ -157,18 +158,25 @@ pub fn invoke_builtin_function(
if borrowed_account.is_writable() {
if let Some(account_info) = account_info_map.get(borrowed_account.get_key()) {
if borrowed_account.get_lamports() != account_info.lamports() {
borrowed_account.set_lamports(account_info.lamports())?;
borrowed_account
.set_lamports(account_info.lamports(), &invoke_context.feature_set)?;
}

if borrowed_account
.can_data_be_resized(account_info.data_len())
.is_ok()
&& borrowed_account.can_data_be_changed().is_ok()
&& borrowed_account
.can_data_be_changed(&invoke_context.feature_set)
.is_ok()
{
borrowed_account.set_data_from_slice(&account_info.data.borrow())?;
borrowed_account.set_data_from_slice(
&account_info.data.borrow(),
&invoke_context.feature_set,
)?;
}
if borrowed_account.get_owner() != account_info.owner {
borrowed_account.set_owner(account_info.owner.as_ref())?;
borrowed_account
.set_owner(account_info.owner.as_ref(), &invoke_context.feature_set)?;
}
}
}
Expand Down Expand Up @@ -279,17 +287,17 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
.unwrap();
if borrowed_account.get_lamports() != account_info.lamports() {
borrowed_account
.set_lamports(account_info.lamports())
.set_lamports(account_info.lamports(), &invoke_context.feature_set)
.unwrap();
}
let account_info_data = account_info.try_borrow_data().unwrap();
// The redundant check helps to avoid the expensive data comparison if we can
match borrowed_account
.can_data_be_resized(account_info_data.len())
.and_then(|_| borrowed_account.can_data_be_changed())
.and_then(|_| borrowed_account.can_data_be_changed(&invoke_context.feature_set))
{
Ok(()) => borrowed_account
.set_data_from_slice(&account_info_data)
.set_data_from_slice(&account_info_data, &invoke_context.feature_set)
.unwrap(),
Err(err) if borrowed_account.get_data() != *account_info_data => {
panic!("{err:?}");
Expand All @@ -299,7 +307,7 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
// Change the owner at the end so that we are allowed to change the lamports and data before
if borrowed_account.get_owner() != account_info.owner {
borrowed_account
.set_owner(account_info.owner.as_ref())
.set_owner(account_info.owner.as_ref(), &invoke_context.feature_set)
.unwrap();
}
if instruction_account.is_writable {
Expand Down
22 changes: 12 additions & 10 deletions programs/address-lookup-table/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ impl Processor {
let instruction_context = transaction_context.get_current_instruction_context()?;
let mut lookup_table_account =
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
lookup_table_account.set_state(&ProgramState::LookupTable(LookupTableMeta::new(
authority_key,
)))?;
lookup_table_account.set_state(
&ProgramState::LookupTable(LookupTableMeta::new(authority_key)),
&invoke_context.feature_set,
)?;

Ok(())
}
Expand Down Expand Up @@ -213,7 +214,7 @@ impl Processor {
let mut lookup_table_meta = lookup_table.meta;
lookup_table_meta.authority = None;
AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;

Expand Down Expand Up @@ -305,11 +306,12 @@ impl Processor {
)?;
{
AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;
for new_address in new_addresses {
lookup_table_account.extend_from_slice(new_address.as_ref())?;
lookup_table_account
.extend_from_slice(new_address.as_ref(), &invoke_context.feature_set)?;
}
}
drop(lookup_table_account);
Expand Down Expand Up @@ -381,7 +383,7 @@ impl Processor {
lookup_table_meta.deactivation_slot = clock.slot;

AddressLookupTable::overwrite_meta_data(
lookup_table_account.get_data_mut()?,
lookup_table_account.get_data_mut(&invoke_context.feature_set)?,
lookup_table_meta,
)?;

Expand Down Expand Up @@ -456,13 +458,13 @@ impl Processor {

let mut recipient_account =
instruction_context.try_borrow_instruction_account(transaction_context, 2)?;
recipient_account.checked_add_lamports(withdrawn_lamports)?;
recipient_account.checked_add_lamports(withdrawn_lamports, &invoke_context.feature_set)?;
drop(recipient_account);

let mut lookup_table_account =
instruction_context.try_borrow_instruction_account(transaction_context, 0)?;
lookup_table_account.set_data_length(0)?;
lookup_table_account.set_lamports(0)?;
lookup_table_account.set_data_length(0, &invoke_context.feature_set)?;
lookup_table_account.set_lamports(0, &invoke_context.feature_set)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 8872bf2

Please sign in to comment.