Skip to content

Commit

Permalink
Fix: #20598 (#20669)
Browse files Browse the repository at this point in the history
* Makes argument / parameter interfaces more clear.

* Fixes the bug in #20598
  • Loading branch information
Lichtso authored Oct 14, 2021
1 parent 588168b commit bbc0bb4
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ fn map_ebpf_error(invoke_context: &dyn InvokeContext, e: EbpfError<BpfError>) ->
}

pub fn create_executor(
program_account_index: usize,
program_data_offset: usize,
programdata_account_index: usize,
programdata_offset: usize,
invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<Arc<BpfExecutor>, InstructionError> {
Expand All @@ -91,11 +91,9 @@ pub fn create_executor(
};
let mut executable = {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, program_account_index)?;
let account = program.try_account_ref()?;
let data = &account.data()[program_data_offset..];
let programdata = keyed_account_at_index(keyed_accounts, programdata_account_index)?;
<dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
data,
&programdata.try_account_ref()?.data()[programdata_offset..],
None,
config,
syscall_registry,
Expand Down Expand Up @@ -263,7 +261,7 @@ fn process_instruction_common(
Some(executor) => executor,
None => {
let executor = create_executor(
next_first_instruction_account,
first_instruction_account,
program_data_offset,
invoke_context,
use_jit,
Expand Down Expand Up @@ -349,7 +347,7 @@ fn process_loader_upgradeable_instruction(
return Err(InstructionError::InvalidAccountData);
}
write_program_data(
1,
first_instruction_account,
UpgradeableLoaderState::buffer_data_offset()? + offset as usize,
&bytes,
invoke_context,
Expand Down Expand Up @@ -472,7 +470,12 @@ fn process_loader_upgradeable_instruction(
)?;

// Load and verify the program bits
let executor = create_executor(4, buffer_data_offset, invoke_context, use_jit)?;
let executor = create_executor(
first_instruction_account + 3,
buffer_data_offset,
invoke_context,
use_jit,
)?;
invoke_context.add_executor(&new_program_id, executor);

let keyed_accounts = invoke_context.get_keyed_accounts()?;
Expand Down Expand Up @@ -611,7 +614,12 @@ fn process_loader_upgradeable_instruction(
}

// Load and verify the program bits
let executor = create_executor(3, buffer_data_offset, invoke_context, use_jit)?;
let executor = create_executor(
first_instruction_account + 2,
buffer_data_offset,
invoke_context,
use_jit,
)?;
invoke_context.add_executor(&new_program_id, executor);

let keyed_accounts = invoke_context.get_keyed_accounts()?;
Expand Down Expand Up @@ -878,15 +886,20 @@ fn process_loader_instruction(
ic_msg!(invoke_context, "Program account did not sign");
return Err(InstructionError::MissingRequiredSignature);
}
write_program_data(1, offset as usize, &bytes, invoke_context)?;
write_program_data(
first_instruction_account,
offset as usize,
&bytes,
invoke_context,
)?;
}
LoaderInstruction::Finalize => {
if program.signer_key().is_none() {
ic_msg!(invoke_context, "key[0] did not sign the transaction");
return Err(InstructionError::MissingRequiredSignature);
}

let executor = create_executor(1, 0, invoke_context, use_jit)?;
let executor = create_executor(first_instruction_account, 0, invoke_context, use_jit)?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
invoke_context.add_executor(program.unsigned_key(), executor);
Expand Down

0 comments on commit bbc0bb4

Please sign in to comment.