Skip to content

Commit

Permalink
Fix #21346 (#21362)
Browse files Browse the repository at this point in the history
Fixes the empty transaction bug in ThisInvokeContext::push() and adds a test for it to the bank.
  • Loading branch information
Lichtso committed Nov 19, 2021
1 parent 0bda0c3 commit 8a50b63
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
3 changes: 2 additions & 1 deletion program-runtime/src/instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ impl InstructionProcessor {
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let root_account = keyed_account_at_index(keyed_accounts, 0)?;
let root_account = keyed_account_at_index(keyed_accounts, 0)
.map_err(|_| InstructionError::UnsupportedProgramId)?;
let root_id = root_account.unsigned_key();
let owner_id = &root_account.owner()?;
if solana_sdk::native_loader::check_id(owner_id) {
Expand Down
41 changes: 19 additions & 22 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,38 +327,21 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
return Err(InstructionError::CallDepth);
}

let program_id = if let Some(index_of_program_id) = program_indices.last() {
let program_id = &self.accounts[*index_of_program_id].0;
let contains = self
.invoke_stack
.iter()
.any(|frame| frame.program_id() == Some(program_id));
let is_last = if let Some(last_frame) = self.invoke_stack.last() {
last_frame.program_id() == Some(program_id)
} else {
false
};
if contains && !is_last {
// Reentrancy not allowed unless caller is calling itself
return Err(InstructionError::ReentrancyNotAllowed);
}
*program_id
} else {
return Err(InstructionError::UnsupportedProgramId);
};

let program_id = program_indices
.last()
.map(|index_of_program_id| &self.accounts[*index_of_program_id].0);
if self.invoke_stack.is_empty() {
let mut compute_budget = self.compute_budget;
if !self.is_feature_active(&tx_wide_compute_cap::id())
&& self.is_feature_active(&neon_evm_compute_budget::id())
&& program_id == crate::neon_evm_program::id()
&& program_id == Some(&crate::neon_evm_program::id())
{
// Bump the compute budget for neon_evm
compute_budget.max_units = compute_budget.max_units.max(500_000);
}
if !self.is_feature_active(&requestable_heap_size::id())
&& self.is_feature_active(&neon_evm_compute_budget::id())
&& program_id == crate::neon_evm_program::id()
&& program_id == Some(&crate::neon_evm_program::id())
{
// Bump the compute budget for neon_evm
compute_budget.heap_size = Some(256_usize.saturating_mul(1024));
Expand All @@ -381,6 +364,20 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
Err(InstructionError::MissingAccount)
};
instruction.visit_each_account(&mut work)?;
} else {
let contains = self
.invoke_stack
.iter()
.any(|frame| frame.program_id() == program_id);
let is_last = if let Some(last_frame) = self.invoke_stack.last() {
last_frame.program_id() == program_id
} else {
false
};
if contains && !is_last {
// Reentrancy not allowed unless caller is calling itself
return Err(InstructionError::ReentrancyNotAllowed);
}
}

// Create the KeyedAccounts that will be passed to the program
Expand Down
14 changes: 14 additions & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15391,4 +15391,18 @@ pub(crate) mod tests {
.unwrap();
assert_eq!(Bank::calculate_fee(&message, 1), 11);
}

#[test]
fn test_an_empty_transaction_without_program() {
let (genesis_config, mint_keypair) = create_genesis_config(1);
let bank = Bank::new_for_tests(&genesis_config);

let destination = solana_sdk::pubkey::new_rand();
let mut ix = system_instruction::transfer(&mint_keypair.pubkey(), &destination, 0);
ix.program_id = native_loader::id(); // Empty executable account chain

let message = Message::new(&[ix], Some(&mint_keypair.pubkey()));
let tx = Transaction::new(&[&mint_keypair], message, genesis_config.hash());
bank.process_transaction(&tx).unwrap();
}
}
2 changes: 0 additions & 2 deletions runtime/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ fn process_instruction_with_program_logging(
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
debug_assert_eq!(first_instruction_account, 1);

let logger = invoke_context.get_logger();
let program_id = invoke_context.get_caller()?;
stable_log::program_invoke(&logger, program_id, invoke_context.invoke_depth());
Expand Down

0 comments on commit 8a50b63

Please sign in to comment.