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

v2.0: use new feature gate for cu depletion (backport of #3994) #4067

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,27 @@ fn execute<'a, 'b: 'a>(
Err(Box::new(error) as Box<dyn std::error::Error>)
}
ProgramResult::Err(mut error) => {
<<<<<<< HEAD
=======
if invoke_context
.get_feature_set()
.is_active(&solana_feature_set::deplete_cu_meter_on_vm_failure::id())
&& !matches!(error, EbpfError::SyscallError(_))
{
// when an exception is thrown during the execution of a
// Basic Block (e.g., a null memory dereference or other
// faults), determining the exact number of CUs consumed
// up to the point of failure requires additional effort
// and is unnecessary since these cases are rare.
//
// In order to simplify CU tracking, simply consume all
// remaining compute units so that the block cost
// tracker uses the full requested compute unit cost for
// this failed transaction.
invoke_context.consume(invoke_context.get_remaining());
}

>>>>>>> 11467d9221 (use new feature gate for cu depletion (#3994))
if direct_mapping {
if let EbpfError::AccessViolation(
AccessType::Store,
Expand Down
131 changes: 131 additions & 0 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4593,6 +4593,137 @@ fn test_cpi_invalid_account_info_pointers() {

#[test]
#[cfg(feature = "sbf_rust")]
<<<<<<< HEAD
=======
fn test_deplete_cost_meter_with_access_violation() {
solana_logger::setup();
let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(100_123_456_789);

for deplete_cu_meter_on_vm_failure in [false, true] {
let mut bank = Bank::new_for_tests(&genesis_config);
let feature_set = Arc::make_mut(&mut bank.feature_set);
// by default test banks have all features enabled, so we only need to
// disable when needed
if !deplete_cu_meter_on_vm_failure {
feature_set.deactivate(&feature_set::deplete_cu_meter_on_vm_failure::id());
}
let (bank, bank_forks) = bank.wrap_with_bank_forks_for_tests();
let mut bank_client = BankClient::new_shared(bank.clone());
let authority_keypair = Keypair::new();
let (bank, invoke_program_id) = load_upgradeable_program_and_advance_slot(
&mut bank_client,
bank_forks.as_ref(),
&mint_keypair,
&authority_keypair,
"solana_sbf_rust_invoke",
);

let account_keypair = Keypair::new();
let mint_pubkey = mint_keypair.pubkey();
let account_metas = vec![
AccountMeta::new(mint_pubkey, true),
AccountMeta::new(account_keypair.pubkey(), false),
AccountMeta::new_readonly(invoke_program_id, false),
];

let mut instruction_data = vec![TEST_WRITE_ACCOUNT, 2];
instruction_data.extend_from_slice(3usize.to_le_bytes().as_ref());
instruction_data.push(42);

let instruction = Instruction::new_with_bytes(
invoke_program_id,
&instruction_data,
account_metas.clone(),
);

let compute_unit_limit = 10_000u32;
let message = Message::new(
&[
ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit),
instruction,
],
Some(&mint_keypair.pubkey()),
);
let tx = Transaction::new(&[&mint_keypair], message, bank.last_blockhash());

let result = load_execute_and_commit_transaction(&bank, tx).unwrap();

assert_eq!(
result.status.unwrap_err(),
TransactionError::InstructionError(1, InstructionError::ReadonlyDataModified)
);

if deplete_cu_meter_on_vm_failure {
assert_eq!(result.executed_units, u64::from(compute_unit_limit));
} else {
assert!(result.executed_units < u64::from(compute_unit_limit));
}
}
}

#[test]
#[cfg(feature = "sbf_rust")]
fn test_program_sbf_deplete_cost_meter_with_divide_by_zero() {
solana_logger::setup();

let GenesisConfigInfo {
genesis_config,
mint_keypair,
..
} = create_genesis_config(50);

for deplete_cu_meter_on_vm_failure in [false, true] {
let mut bank = Bank::new_for_tests(&genesis_config);
let feature_set = Arc::make_mut(&mut bank.feature_set);
// by default test banks have all features enabled, so we only need to
// disable when needed
if !deplete_cu_meter_on_vm_failure {
feature_set.deactivate(&feature_set::deplete_cu_meter_on_vm_failure::id());
}
let (bank, bank_forks) = bank.wrap_with_bank_forks_for_tests();
let mut bank_client = BankClient::new_shared(bank.clone());
let authority_keypair = Keypair::new();
let (bank, program_id) = load_upgradeable_program_and_advance_slot(
&mut bank_client,
bank_forks.as_ref(),
&mint_keypair,
&authority_keypair,
"solana_sbf_rust_divide_by_zero",
);

let instruction = Instruction::new_with_bytes(program_id, &[], vec![]);
let compute_unit_limit = 10_000;
let message = Message::new(
&[
ComputeBudgetInstruction::set_compute_unit_limit(compute_unit_limit),
instruction,
],
Some(&mint_keypair.pubkey()),
);
let tx = Transaction::new(&[&mint_keypair], message, bank.last_blockhash());

let result = load_execute_and_commit_transaction(&bank, tx).unwrap();

assert_eq!(
result.status.unwrap_err(),
TransactionError::InstructionError(1, InstructionError::ProgramFailedToComplete)
);

if deplete_cu_meter_on_vm_failure {
assert_eq!(result.executed_units, u64::from(compute_unit_limit));
} else {
assert!(result.executed_units < u64::from(compute_unit_limit));
}
}
}

#[test]
#[cfg(feature = "sbf_rust")]
>>>>>>> 11467d9221 (use new feature gate for cu depletion (#3994))
fn test_deny_executable_write() {
solana_logger::setup();

Expand Down
41 changes: 41 additions & 0 deletions sdk/src/feature_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,11 @@ pub mod delay_visibility_of_program_deployment {
}

pub mod apply_cost_tracker_during_replay {
<<<<<<< HEAD:sdk/src/feature_set.rs
solana_sdk::declare_id!("2ry7ygxiYURULZCrypHhveanvP5tzZ4toRwVp89oCNSj");
=======
solana_pubkey::declare_id!("2ry7ygxiYURULZCrypHhveanvP5tzZ4toRwVp89oCNSj");
>>>>>>> 11467d9221 (use new feature gate for cu depletion (#3994)):sdk/feature-set/src/lib.rs
}
pub mod bpf_account_data_direct_mapping {
solana_sdk::declare_id!("EenyoWx9UMXYKpR8mW5Jmfmy2fRjzUtM7NduYMY8bx33");
Expand Down Expand Up @@ -858,7 +862,31 @@ pub mod deprecate_legacy_vote_ixs {
}

pub mod disable_account_loader_special_case {
<<<<<<< HEAD:sdk/src/feature_set.rs
solana_program::declare_id!("EQUMpNFr7Nacb1sva56xn1aLfBxppEoSBH8RRVdkcD1x");
=======
solana_pubkey::declare_id!("EQUMpNFr7Nacb1sva56xn1aLfBxppEoSBH8RRVdkcD1x");
}

pub mod enable_secp256r1_precompile {
solana_pubkey::declare_id!("sr11RdZWgbHTHxSroPALe6zgaT5A1K9LcE4nfsZS4gi");
}

pub mod accounts_lt_hash {
solana_pubkey::declare_id!("LtHaSHHsUge7EWTPVrmpuexKz6uVHZXZL6cgJa7W7Zn");
}

pub mod migrate_stake_program_to_core_bpf {
solana_pubkey::declare_id!("6M4oQ6eXneVhtLoiAr4yRYQY43eVLjrKbiDZDJc892yk");
}

pub mod deplete_cu_meter_on_vm_failure {
solana_pubkey::declare_id!("B7H2caeia4ZFcpE3QcgMqbiWiBtWrdBRBSJ1DY6Ktxbq");
}

pub mod reserve_minimal_cus_for_builtin_instructions {
solana_pubkey::declare_id!("C9oAhLxDBm3ssWtJx1yBGzPY55r2rArHmN1pbQn6HogH");
>>>>>>> 11467d9221 (use new feature gate for cu depletion (#3994)):sdk/feature-set/src/lib.rs
}

lazy_static! {
Expand Down Expand Up @@ -1068,9 +1096,22 @@ lazy_static! {
(verify_retransmitter_signature::id(), "Verify retransmitter signature #1840"),
(vote_only_retransmitter_signed_fec_sets::id(), "vote only on retransmitter signed fec sets"),
(partitioned_epoch_rewards_superfeature::id(), "replaces enable_partitioned_epoch_reward to enable partitioned rewards at epoch boundary SIMD-0118"),
<<<<<<< HEAD:sdk/src/feature_set.rs
(enable_turbine_extended_fanout_experiments::id(), "enable turbine extended fanout experiments #2373"),
(deprecate_legacy_vote_ixs::id(), "Deprecate legacy vote instructions"),
(disable_account_loader_special_case::id(), "Disable account loader special case"),
=======
(disable_sbpf_v1_execution::id(), "Disables execution of SBPFv1 programs"),
(reenable_sbpf_v1_execution::id(), "Re-enables execution of SBPFv1 programs"),
(remove_accounts_executable_flag_checks::id(), "Remove checks of accounts is_executable flag SIMD-0162"),
(lift_cpi_caller_restriction::id(), "Lift the restriction in CPI that the caller must have the callee as an instruction account #2202"),
(disable_account_loader_special_case::id(), "Disable account loader special case #3513"),
(accounts_lt_hash::id(), "enables lattice-based accounts hash #3333"),
(enable_secp256r1_precompile::id(), "Enable secp256r1 precompile SIMD-0075"),
(migrate_stake_program_to_core_bpf::id(), "Migrate Stake program to Core BPF SIMD-0196 #3655"),
(deplete_cu_meter_on_vm_failure::id(), "Deplete compute meter for vm errors SIMD-0182 #3993"),
(reserve_minimal_cus_for_builtin_instructions::id(), "Reserve minimal CUs for builtin instructions SIMD-170 #2562"),
>>>>>>> 11467d9221 (use new feature gate for cu depletion (#3994)):sdk/feature-set/src/lib.rs
/*************** ADD NEW FEATURES HERE ***************/
]
.iter()
Expand Down
Loading