Skip to content

Commit

Permalink
Adds a type alias for Box<dyn std::error::Error> in syscalls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Mar 14, 2023
1 parent 737fbe7 commit 16115c6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 94 deletions.
68 changes: 30 additions & 38 deletions programs/bpf_loader/src/syscalls/cpi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> CallerAccount<'a> {
_vm_addr: u64,
account_info: &AccountInfo,
original_data_len: usize,
) -> Result<CallerAccount<'a>, Box<dyn std::error::Error>> {
) -> Result<CallerAccount<'a>, Error> {
// account_info points to host memory. The addresses used internally are
// in vm space so they need to be translated.

Expand Down Expand Up @@ -129,7 +129,7 @@ impl<'a> CallerAccount<'a> {
vm_addr: u64,
account_info: &SolAccountInfo,
original_data_len: usize,
) -> Result<CallerAccount<'a>, Box<dyn std::error::Error>> {
) -> Result<CallerAccount<'a>, Error> {
// account_info points to host memory. The addresses used internally are
// in vm space so they need to be translated.

Expand Down Expand Up @@ -212,22 +212,22 @@ trait SyscallInvokeSigned {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<StableInstruction, Box<dyn std::error::Error>>;
) -> Result<StableInstruction, Error>;
fn translate_accounts<'a>(
instruction_accounts: &[InstructionAccount],
program_indices: &[IndexOfAccount],
account_infos_addr: u64,
account_infos_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<TranslatedAccounts<'a>, Box<dyn std::error::Error>>;
) -> Result<TranslatedAccounts<'a>, Error>;
fn translate_signers(
program_id: &Pubkey,
signers_seeds_addr: u64,
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &InvokeContext,
) -> Result<Vec<Pubkey>, Box<dyn std::error::Error>>;
) -> Result<Vec<Pubkey>, Error>;
}

declare_syscall!(
Expand All @@ -241,7 +241,7 @@ declare_syscall!(
signers_seeds_addr: u64,
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
cpi_common::<Self>(
invoke_context,
instruction_addr,
Expand All @@ -259,7 +259,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<StableInstruction, Box<dyn std::error::Error>> {
) -> Result<StableInstruction, Error> {
let ix = translate_type::<StableInstruction>(
memory_mapping,
addr,
Expand Down Expand Up @@ -312,7 +312,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust {
account_infos_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<TranslatedAccounts<'a>, Box<dyn std::error::Error>> {
) -> Result<TranslatedAccounts<'a>, Error> {
let (account_infos, account_info_keys) = translate_account_infos(
account_infos_addr,
account_infos_len,
Expand All @@ -339,7 +339,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust {
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &InvokeContext,
) -> Result<Vec<Pubkey>, Box<dyn std::error::Error>> {
) -> Result<Vec<Pubkey>, Error> {
let mut signers = Vec::new();
if signers_seeds_len > 0 {
let signers_seeds = translate_slice::<&[&[u8]]>(
Expand Down Expand Up @@ -374,7 +374,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedRust {
invoke_context.get_check_size(),
)
})
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?;
.collect::<Result<Vec<_>, Error>>()?;
let signer = Pubkey::create_program_address(&seeds, program_id)
.map_err(SyscallError::BadSeeds)?;
signers.push(signer);
Expand Down Expand Up @@ -450,7 +450,7 @@ declare_syscall!(
signers_seeds_addr: u64,
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
cpi_common::<Self>(
invoke_context,
instruction_addr,
Expand All @@ -468,7 +468,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
addr: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<StableInstruction, Box<dyn std::error::Error>> {
) -> Result<StableInstruction, Error> {
let ix_c = translate_type::<SolInstruction>(
memory_mapping,
addr,
Expand Down Expand Up @@ -527,7 +527,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
is_writable: meta_c.is_writable,
})
})
.collect::<Result<Vec<AccountMeta>, Box<dyn std::error::Error>>>()?;
.collect::<Result<Vec<AccountMeta>, Error>>()?;

Ok(StableInstruction {
accounts: accounts.into(),
Expand All @@ -543,7 +543,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
account_infos_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<TranslatedAccounts<'a>, Box<dyn std::error::Error>> {
) -> Result<TranslatedAccounts<'a>, Error> {
let (account_infos, account_info_keys) = translate_account_infos(
account_infos_addr,
account_infos_len,
Expand All @@ -570,7 +570,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
invoke_context: &InvokeContext,
) -> Result<Vec<Pubkey>, Box<dyn std::error::Error>> {
) -> Result<Vec<Pubkey>, Error> {
if signers_seeds_len > 0 {
let signers_seeds = translate_slice::<SolSignerSeedsC>(
memory_mapping,
Expand All @@ -593,8 +593,7 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
invoke_context.get_check_size(),
)?;
if seeds.len() > MAX_SEEDS {
return Err(Box::new(InstructionError::MaxSeedLengthExceeded)
as Box<dyn std::error::Error>);
return Err(Box::new(InstructionError::MaxSeedLengthExceeded) as Error);
}
let seeds_bytes = seeds
.iter()
Expand All @@ -607,12 +606,11 @@ impl SyscallInvokeSigned for SyscallInvokeSignedC {
invoke_context.get_check_size(),
)
})
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?;
Pubkey::create_program_address(&seeds_bytes, program_id).map_err(|err| {
Box::new(SyscallError::BadSeeds(err)) as Box<dyn std::error::Error>
})
.collect::<Result<Vec<_>, Error>>()?;
Pubkey::create_program_address(&seeds_bytes, program_id)
.map_err(|err| Box::new(SyscallError::BadSeeds(err)) as Error)
})
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?)
.collect::<Result<Vec<_>, Error>>()?)
} else {
Ok(vec![])
}
Expand All @@ -625,7 +623,7 @@ fn translate_account_infos<'a, T, F>(
key_addr: F,
memory_mapping: &mut MemoryMapping,
invoke_context: &mut InvokeContext,
) -> Result<(&'a [T], Vec<&'a Pubkey>), Box<dyn std::error::Error>>
) -> Result<(&'a [T], Vec<&'a Pubkey>), Error>
where
F: Fn(&T) -> u64,
{
Expand All @@ -646,7 +644,7 @@ where
invoke_context.get_check_aligned(),
)
})
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?;
.collect::<Result<Vec<_>, Error>>()?;

Ok((account_infos, account_info_keys))
}
Expand All @@ -662,15 +660,9 @@ fn translate_and_update_accounts<'a, T, F>(
invoke_context: &mut InvokeContext,
memory_mapping: &MemoryMapping,
do_translate: F,
) -> Result<TranslatedAccounts<'a>, Box<dyn std::error::Error>>
) -> Result<TranslatedAccounts<'a>, Error>
where
F: Fn(
&InvokeContext,
&MemoryMapping,
u64,
&T,
usize,
) -> Result<CallerAccount<'a>, Box<dyn std::error::Error>>,
F: Fn(&InvokeContext, &MemoryMapping, u64, &T, usize) -> Result<CallerAccount<'a>, Error>,
{
let transaction_context = &invoke_context.transaction_context;
let instruction_context = transaction_context.get_current_instruction_context()?;
Expand Down Expand Up @@ -765,7 +757,7 @@ fn check_instruction_size(
num_accounts: usize,
data_len: usize,
invoke_context: &mut InvokeContext,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), Error> {
if invoke_context
.feature_set
.is_active(&feature_set::loosen_cpi_size_restriction::id())
Expand Down Expand Up @@ -802,7 +794,7 @@ fn check_instruction_size(
fn check_account_infos(
num_account_infos: usize,
invoke_context: &mut InvokeContext,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), Error> {
if invoke_context
.feature_set
.is_active(&feature_set::loosen_cpi_size_restriction::id())
Expand Down Expand Up @@ -839,7 +831,7 @@ fn check_authorized_program(
program_id: &Pubkey,
instruction_data: &[u8],
invoke_context: &InvokeContext,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), Error> {
if native_loader::check_id(program_id)
|| bpf_loader::check_id(program_id)
|| bpf_loader_deprecated::check_id(program_id)
Expand Down Expand Up @@ -871,7 +863,7 @@ fn cpi_common<S: SyscallInvokeSigned>(
signers_seeds_addr: u64,
signers_seeds_len: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
// CPI entry.
//
// Translate the inputs to the syscall and synchronize the caller's account
Expand Down Expand Up @@ -949,7 +941,7 @@ fn update_callee_account(
invoke_context: &InvokeContext,
caller_account: &CallerAccount,
mut callee_account: BorrowedAccount<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), Error> {
let is_disable_cpi_setting_executable_and_rent_epoch_active = invoke_context
.feature_set
.is_active(&disable_cpi_setting_executable_and_rent_epoch::id());
Expand Down Expand Up @@ -1019,7 +1011,7 @@ fn update_caller_account(
memory_mapping: &MemoryMapping,
caller_account: &mut CallerAccount,
callee_account: &BorrowedAccount<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
) -> Result<(), Error> {
*caller_account.lamports = callee_account.get_lamports();
*caller_account.owner = *callee_account.get_owner();
let new_len = callee_account.get_data().len();
Expand Down
10 changes: 5 additions & 5 deletions programs/bpf_loader/src/syscalls/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
let cost = invoke_context
.get_compute_budget()
.syscall_base_cost
Expand Down Expand Up @@ -44,7 +44,7 @@ declare_syscall!(
arg4: u64,
arg5: u64,
_memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
let cost = invoke_context.get_compute_budget().log_64_units;
consume_compute_meter(invoke_context, cost)?;

Expand All @@ -67,7 +67,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
_memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
let cost = invoke_context.get_compute_budget().syscall_base_cost;
consume_compute_meter(invoke_context, cost)?;

Expand All @@ -91,7 +91,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
let cost = invoke_context.get_compute_budget().log_pubkey_units;
consume_compute_meter(invoke_context, cost)?;

Expand All @@ -116,7 +116,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
let budget = invoke_context.get_compute_budget();

consume_compute_meter(invoke_context, budget.syscall_base_cost)?;
Expand Down
13 changes: 5 additions & 8 deletions programs/bpf_loader/src/syscalls/mem_ops.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use {super::*, crate::declare_syscall};

fn mem_op_consume(
invoke_context: &mut InvokeContext,
n: u64,
) -> Result<(), Box<dyn std::error::Error>> {
fn mem_op_consume(invoke_context: &mut InvokeContext, n: u64) -> Result<(), Error> {
let compute_budget = invoke_context.get_compute_budget();
let cost = compute_budget
.mem_op_base_cost
Expand All @@ -22,7 +19,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
mem_op_consume(invoke_context, n)?;

if !is_nonoverlapping(src_addr, n, dst_addr, n) {
Expand Down Expand Up @@ -69,7 +66,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
mem_op_consume(invoke_context, n)?;

let dst = translate_slice_mut::<u8>(
Expand Down Expand Up @@ -104,7 +101,7 @@ declare_syscall!(
cmp_result_addr: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
mem_op_consume(invoke_context, n)?;

let s1 = translate_slice::<u8>(
Expand Down Expand Up @@ -152,7 +149,7 @@ declare_syscall!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Box<dyn std::error::Error>> {
) -> Result<u64, Error> {
mem_op_consume(invoke_context, n)?;

let s = translate_slice_mut::<u8>(
Expand Down
Loading

0 comments on commit 16115c6

Please sign in to comment.