Skip to content

Commit

Permalink
Refactor - SyscallInvokeSignedRust::translate_instruction() (#1644)
Browse files Browse the repository at this point in the history
* Reorders all translate_slice() to the beginning of SyscallInvokeSignedRust::translate_instruction().

* Uses the host slice instead the guest one.

* Reorders consume_compute_meter() to happen before heap allocation.
  • Loading branch information
Lichtso authored Jun 11, 2024
1 parent e6902be commit 475e918
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions programs/bpf_loader/src/syscalls/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,50 +428,48 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust {
addr,
invoke_context.get_check_aligned(),
)?;

check_instruction_size(ix.accounts.len(), ix.data.len(), invoke_context)?;

let account_metas = translate_slice::<AccountMeta>(
memory_mapping,
ix.accounts.as_ptr() as u64,
ix.accounts.len() as u64,
invoke_context.get_check_aligned(),
)?;
let mut accounts = Vec::with_capacity(ix.accounts.len());
#[allow(clippy::needless_range_loop)]
for account_index in 0..ix.accounts.len() {
#[allow(clippy::indexing_slicing)]
let account_meta = &account_metas[account_index];
if unsafe {
std::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1
|| std::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8)
> 1
} {
return Err(Box::new(InstructionError::InvalidArgument));
}
accounts.push(account_meta.clone());
}
let data = translate_slice::<u8>(
memory_mapping,
ix.data.as_ptr() as u64,
ix.data.len() as u64,
invoke_context.get_check_aligned(),
)?
.to_vec();

check_instruction_size(account_metas.len(), data.len(), invoke_context)?;

let ix_data_len = ix.data.len() as u64;
if invoke_context
.get_feature_set()
.is_active(&feature_set::loosen_cpi_size_restriction::id())
{
consume_compute_meter(
invoke_context,
(ix_data_len)
(data.len() as u64)
.checked_div(invoke_context.get_compute_budget().cpi_bytes_per_unit)
.unwrap_or(u64::MAX),
)?;
}

let data = translate_slice::<u8>(
memory_mapping,
ix.data.as_ptr() as u64,
ix_data_len,
invoke_context.get_check_aligned(),
)?
.to_vec();
let mut accounts = Vec::with_capacity(account_metas.len());
#[allow(clippy::needless_range_loop)]
for account_index in 0..account_metas.len() {
#[allow(clippy::indexing_slicing)]
let account_meta = &account_metas[account_index];
if unsafe {
std::ptr::read_volatile(&account_meta.is_signer as *const _ as *const u8) > 1
|| std::ptr::read_volatile(&account_meta.is_writable as *const _ as *const u8)
> 1
} {
return Err(Box::new(InstructionError::InvalidArgument));
}
accounts.push(account_meta.clone());
}

Ok(StableInstruction {
accounts: accounts.into(),
Expand Down

0 comments on commit 475e918

Please sign in to comment.