From 475e918c18dc1b18855917baded3df04e0f7a7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Tue, 11 Jun 2024 10:20:57 +0200 Subject: [PATCH] Refactor - `SyscallInvokeSignedRust::translate_instruction()` (#1644) * 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. --- programs/bpf_loader/src/syscalls/cpi.rs | 50 ++++++++++++------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/programs/bpf_loader/src/syscalls/cpi.rs b/programs/bpf_loader/src/syscalls/cpi.rs index 9237882ea8a869..9b94844995e12d 100644 --- a/programs/bpf_loader/src/syscalls/cpi.rs +++ b/programs/bpf_loader/src/syscalls/cpi.rs @@ -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::( 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::( + 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::( - 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(),