LCOV - code coverage report
Current view: top level - flamenco/vm/syscall - fd_vm_syscall_cpi_common.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 280 334 83.8 %
Date: 2026-11-26 02:16:43 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* This file contains all the logic that is common to both the C and Rust
       2             :    CPI syscalls (sol_invoke_signed_{rust/c}). As such, all of the functions in
       3             :    here are templated and will be instantiated for both the C and Rust CPI ABIs.
       4             : 
       5             :    The only difference between the C and Rust CPI syscalls is the ABI data layout
       6             :    of the parameters to these calls - all the logic is identical. As such, we have
       7             :    defined a series of macros to abstract away the ABI differences from the CPI implementation.
       8             : 
       9             :    The entry-point for these syscalls is VM_SYSCALL_CPI_ENTRYPOINT.
      10             : 
      11             :    Note that the code for these syscalls could be simplified somewhat, but we have opted to keep
      12             :    it as close to the Solana code as possible to make it easier to audit that we execute equivalently.
      13             :    Most of the top-level functions in this file correspond directly to functions in the Solana codebase
      14             :    and links to the source have been provided.
      15             :  */
      16             : 
      17             : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L126-L144
      18             : 
      19             :    This is used for checking that the account info pointers given by the
      20             :    user match up with the addresses in the serialized account metadata.
      21             : 
      22             :    Field name length is restricted to 54 because
      23             :    127 - (37 + 18 + 18) leaves 54 characters for the field name
      24             :  */
      25             : #define VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, vm_addr, expected_vm_addr, field_name) \
      26        1344 :   if( FD_UNLIKELY( vm_addr!=expected_vm_addr )) {                                                         \
      27          87 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,                                             \
      28          87 :       "Invalid account info pointer `%s': 0x%lx != 0x%lx", field_name, vm_addr, expected_vm_addr );         \
      29          87 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );                                   \
      30          87 :     return FD_VM_SYSCALL_ERR_INVALID_POINTER;                                                             \
      31          87 :   }
      32             : 
      33             : /* fd_vm_syscall_cpi_instruction_to_instr_{c/rust} takes the translated
      34             :    CPI ABI structures (instruction and account meta list), and uses these
      35             :    to populate a fd_instr_info_t struct. This struct can then be given to the
      36             :    FD runtime for execution.
      37             : 
      38             :    WARNING:  out_instr will be partially filled if there are unmatched account
      39             :    metas (i.e,. no corresponding entry in the transaction accounts list). This
      40             :    is not an error condition. fd_vm_prepare_instruction has to handle that case
      41             :    in order to match Agave's behavior of checking presence in both transaction
      42             :    accounts list and caller instruction accounts list in a single loop iteration.
      43             : 
      44             : Parameters:
      45             : - vm: handle to the vm
      46             : - cpi_instr: instruction to execute laid out in the CPI ABI format (Rust or C)
      47             : - cpi_acc_metas: list of account metas, again in the CPI ABI format
      48             : - program_id: translated program pubkey
      49             : - out_instr: output instruction
      50             : - out_instr_acct_keys: output instruction account pubkeys
      51             : - cpi_instr_data: instruction data in host address space
      52             : 
      53             : TODO: return codes/errors?
      54             : */
      55        1687 : #define VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_instruction_to_instr_, VM_SYSCALL_CPI_ABI)
      56             : static int
      57             : VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( fd_vm_t *                         vm,
      58             :                                           VM_SYSCALL_CPI_INSTR_T const *    cpi_instr,
      59             :                                           VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_metas,
      60             :                                           fd_pubkey_t const *               program_id,
      61             :                                           uchar const *                     cpi_instr_data,
      62             :                                           fd_instr_info_t *                 out_instr,
      63        1688 :                                           fd_pubkey_t                       out_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ] ) {
      64             : 
      65        1688 :   out_instr->program_id   = UCHAR_MAX;
      66        1688 :   out_instr->stack_height = (uchar)( vm->instr_ctx->runtime->instr.stack_sz+1 );
      67        1688 :   out_instr->data_sz      = (ushort)VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instr );
      68        1688 :   out_instr->acct_cnt     = (ushort)VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr );
      69        1688 :   if( FD_LIKELY( out_instr->data_sz ) ) memcpy( out_instr->data, cpi_instr_data, out_instr->data_sz );
      70             : 
      71             :   /* Find the index of the CPI instruction's program account in the transaction */
      72        1688 :   ulong program_id_idx = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, program_id );
      73        1688 :   if( FD_LIKELY( program_id_idx!=ULONG_MAX ) ) out_instr->program_id = (uchar)program_id_idx;
      74             : 
      75        1688 :   uchar  acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0};
      76        1688 :   ushort caller_map[ MAX_TX_ACCOUNT_LOCKS ];
      77        1688 :   fd_exec_instr_ctx_instr_account_map( vm->instr_ctx, caller_map );
      78             : 
      79        5930 :   for( ushort i=0; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr ); i++ ) {
      80        4242 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_acct_metas[i];
      81        5919 :     fd_pubkey_t const * pubkey = fd_type_pun_const( VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta ) );
      82           0 :     out_instr_acct_keys[i] = *pubkey;
      83             : 
      84             :     /* The parent flag(s) for is writable/signer is checked in
      85             :        fd_vm_prepare_instruction. Signer privilege is allowed iff the account
      86             :        is a signer in the caller or if it is a derived signer. */
      87             :     /* TODO: error if flags are wrong */
      88        5919 :     out_instr->accounts[i] = fd_instruction_account_init( USHORT_MAX,
      89        5919 :                                                           USHORT_MAX,
      90        5919 :                                                           USHORT_MAX,
      91        5919 :                                                           VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
      92        5919 :                                                           VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
      93             : 
      94             :     /* Use USHORT_MAX to indicate account not found
      95             :         https://github.com/anza-xyz/agave/blob/v3.0.4/program-runtime/src/invoke_context.rs#L395-L397 */
      96        5919 :     ulong idx_in_txn = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, pubkey );
      97             : 
      98        5919 :     fd_instr_info_setup_instr_account( out_instr,
      99        5919 :                                        acc_idx_seen,
     100        5919 :                                        idx_in_txn!=ULONG_MAX ? (ushort)idx_in_txn : USHORT_MAX,
     101        5919 :                                        idx_in_txn!=ULONG_MAX ? caller_map[ idx_in_txn ] : USHORT_MAX,
     102        5919 :                                        i,
     103        5919 :                                        VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
     104        5919 :                                        VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
     105             : 
     106        5919 :   }
     107             : 
     108        1688 :   return FD_VM_SUCCESS;
     109        1688 : }
     110             : 
     111             : /*
     112             : fd_vm_syscall_cpi_update_callee_acc_{rust/c} corresponds to solana_bpf_loader_program::syscalls::cpi::update_callee_account:
     113             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1211-L1273
     114             : 
     115             : (the copy of the account stored in the instruction context's
     116             : borrowed accounts cache)
     117             : 
     118             : This function should be called before the CPI instruction is executed. Its purpose is to
     119             : update the callee account's view of the given account with any changes the caller may made
     120             : to the account before the CPI instruction is executed.
     121             : 
     122             : The callee's view of the account is the borrowed accounts cache, so to update the
     123             : callee account we look up the account in the borrowed accounts cache and update it.
     124             : 
     125             : Parameters:
     126             : - vm: pointer to the virtual machine handle
     127             : - caller_account: translated caller account
     128             : - callee_acc: borrowed callee account
     129             : - out_must_update_caller: set when caller metadata must be updated after CPI
     130             : */
     131         297 : #define VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_update_callee_acc_, VM_SYSCALL_CPI_ABI)
     132             : static int
     133             : VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( fd_vm_t *                          vm,
     134             :                                       fd_vm_cpi_caller_account_t const * caller_account,
     135             :                                       fd_borrowed_account_t *            callee_acc,
     136         297 :                                       uchar *                            out_must_update_caller ) {
     137         297 :   int err;
     138         297 :   *out_must_update_caller = 0;
     139             : 
     140             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1222-L1224 */
     141         297 :   if( fd_borrowed_account_get_lamports( callee_acc )!=FD_LOAD( ulong, caller_account->lamports ) ) {
     142           0 :     err = fd_borrowed_account_set_lamports( callee_acc, FD_LOAD( ulong, caller_account->lamports ) );
     143           0 :     if( FD_UNLIKELY( err ) ) {
     144           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     145           0 :       return -1;
     146           0 :     }
     147           0 :   }
     148             : 
     149             :   /* Account data is modified in place; propagate the length.
     150             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1226-L1255 */
     151         297 :   ulong prev_len = fd_borrowed_account_get_data_len( callee_acc );
     152         297 :   ulong post_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
     153             : 
     154             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1229-L1251 */
     155         297 :   if( FD_UNLIKELY( prev_len!=post_len ) ) {
     156             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1248 */
     157           1 :     err = fd_borrowed_account_set_data_length( callee_acc, post_len );
     158           1 :     if( FD_UNLIKELY( err ) ) {
     159           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     160           0 :       return -1;
     161           0 :     }
     162             :     /* Pointer to data may have changed, caller must be updated.
     163             :        https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1248-L1250 */
     164           1 :     *out_must_update_caller = 1;
     165           1 :   }
     166             : 
     167             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1266-L1271 */
     168         297 :   if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( callee_acc ), caller_account->owner, sizeof(fd_pubkey_t) ) ) ) {
     169           0 :     err = fd_borrowed_account_set_owner( callee_acc, caller_account->owner );
     170           0 :     if( FD_UNLIKELY( err ) ) {
     171           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     172           0 :       return -1;
     173           0 :     }
     174             :     /* Caller gave ownership and thus write access away, so caller must be updated.
     175             :        https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1268-L1270 */
     176           0 :     *out_must_update_caller = 1;
     177           0 :   }
     178             : 
     179         297 :   return FD_VM_SUCCESS;
     180         297 : }
     181             : 
     182             : /*
     183             : fd_vm_syscall_cpi_translate_and_update_accounts_ mirrors the behaviour of
     184             : solana_program_runtime::cpi::SyscallInvokeSigned::translate_accounts_common:
     185             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193
     186             : 
     187             : It translates the caller accounts to the host address space.  The
     188             : syscall entrypoint updates the callee accounts after translation.
     189             : 
     190             : Parameters:
     191             : - vm: pointer to the virtual machine handle
     192             : - instruction_accounts: array of instruction accounts
     193             : - instruction_accounts_cnt: length of the instruction_accounts array
     194             : - account_infos: array of account infos
     195             : - account_infos_length: length of the account_infos array
     196             : 
     197             : Populates:
     198             : - translated_accounts: the translated account entries
     199             : - out_len: number of translated account entries
     200             : */
     201        1013 : #define VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_translate_and_update_accounts_, VM_SYSCALL_CPI_ABI)
     202             : static int
     203             : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     204             :                               fd_vm_t *                         vm,
     205             :                               fd_instruction_account_t const *  instruction_accounts,
     206             :                               ulong const                       instruction_accounts_cnt,
     207             :                               ulong                             acct_infos_va,
     208             :                               fd_pubkey_t const * *             account_info_keys, /* same length as account_infos_length */
     209             :                               VM_SYSCALL_CPI_ACC_INFO_T const * account_infos,
     210             :                               ulong const                       account_infos_length,
     211             :                               fd_vm_cpi_translated_account_t *  translated_accounts,
     212        1015 :                               ulong *                           out_len ) {
     213        2392 :   for( ulong i=0UL; i<instruction_accounts_cnt; i++ ) {
     214        1472 :     if( i!=instruction_accounts[i].index_in_callee ) {
     215             :       /* Skip duplicate accounts */
     216          18 :       continue;
     217          18 :     }
     218             : 
     219             :     /* `fd_vm_prepare_instruction()` will always set up a valid index for `index_in_caller`, so we can access the borrowed account directly.
     220             :        A borrowed account will always have non-NULL meta (if the account doesn't exist, `fd_executor_setup_accounts_for_txn()`
     221             :        will set its meta up) */
     222             : 
     223             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1102 */
     224        1454 :     fd_guarded_borrowed_account_t callee_acct = {0};
     225        1454 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, instruction_accounts[i].index_in_caller, &callee_acct );
     226             : 
     227        1454 :     fd_pubkey_t const *      account_key = (fd_pubkey_t*)callee_acct.acc->pubkey;
     228             : 
     229             :     /* If the account is known and executable, we only need to consume the compute units.
     230             :        Executable accounts can't be modified, so we don't need to update the callee account. */
     231        1454 :     if( fd_borrowed_account_is_executable( &callee_acct ) ) {
     232             :       // FIXME: should this be FD_VM_CU_MEM_OP_UPDATE? Changing this changes the CU behaviour from main (because of the base cost)
     233        1057 :       FD_VM_CU_UPDATE( vm, callee_acct.acc->data_len / FD_VM_CPI_BYTES_PER_UNIT );
     234           0 :       continue;
     235        1057 :     }
     236             : 
     237             :     /* FIXME: we should not need to drop the account here to avoid a double borrow.
     238             :        Instead, we should borrow the account before entering this function. */
     239         397 :     fd_borrowed_account_drop( &callee_acct );
     240             : 
     241             :     /* Find the indices of the account in the caller and callee instructions */
     242         397 :     uint found = 0;
     243        1062 :     for( ushort j=0; j<account_infos_length && !found; j++ ) {
     244         752 :       fd_pubkey_t const * acct_addr = account_info_keys[ j ];
     245             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1117
     246             :        */
     247         752 :       if( memcmp( account_key->uc, acct_addr->uc, sizeof(fd_pubkey_t) ) != 0 ) {
     248         363 :         continue;
     249         363 :       }
     250             : 
     251         389 :       fd_vm_cpi_translated_account_t * translated_account = translated_accounts + *out_len;
     252         389 :       fd_vm_cpi_caller_account_t *     caller_account     = &translated_account->caller_account;
     253         389 :       ushort                           index_in_caller    = instruction_accounts[i].index_in_caller;
     254         389 :       translated_account->index_in_caller                 = index_in_caller;
     255         389 :       translated_account->update_caller_account_info      = (uchar)!!instruction_accounts[i].is_writable;
     256         389 :       found = 1;
     257             : 
     258             :       /* Logically this check isn't ever going to fail due to how the
     259             :          account_info_keys array is set up.  We replicate the check for
     260             :          clarity and also to guard against accidental violation of the
     261             :          assumed invariant in the future.
     262             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1131-L1134
     263             :        */
     264         389 :       if( FD_UNLIKELY( j >= account_infos_length ) ) {
     265           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     266           0 :         return FD_VM_SYSCALL_ERR_INVALID_LENGTH;
     267           0 :       }
     268             : 
     269             :       /* The following implements the checks in from_account_info which
     270             :          is invoked as do_translate() in translate_and_update_accounts()
     271             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1135-L1146
     272             :        */
     273             :       ////// BEGIN from_account_info
     274             : 
     275         389 :       fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[index_in_caller];
     276             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L321-L334 */
     277             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L322-L327 */
     278         389 :       ulong expected_pubkey_vaddr = acc_region_meta->vm_key_addr;
     279             :       /* Max msg_sz: 40 + 18 + 18 = 76 < 127 */
     280         389 :       VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].pubkey_addr, expected_pubkey_vaddr, "key");
     281             : 
     282             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L328-L333 */
     283         322 :       ulong expected_owner_vaddr = acc_region_meta->vm_owner_addr;
     284             :       /* Max msg_sz: 42 + 18 + 18 = 78 < 127 */
     285         322 :       VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].owner_addr, expected_owner_vaddr, "owner");
     286             : 
     287             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L336-L358 */
     288         762 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, (account_infos + j), lamports_vaddr );
     289             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L345-L356  */
     290             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L346-L348
     291             :          Check that the account's lamports Rc<RefCell<&mut u64>> is not
     292             :          stored in the account region. Because a refcell is only present if
     293             :          the Rust SDK is used, we only need to check this for the Rust ABI. */
     294             :       #ifdef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
     295         254 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, (account_infos + j), lamports_rc_vaddr )
     296         254 :       if ( FD_UNLIKELY( lamports_rc_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     297           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     298           0 :         return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     299           0 :       }
     300         254 :       #endif
     301             : 
     302             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L350-L355 */
     303         254 :       ulong expected_lamports_vaddr = acc_region_meta->vm_lamports_addr;
     304             :       /* Max msg_sz: 45 + 18 + 18 = 81 < 127 */
     305         322 :       VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, lamports_vaddr, expected_lamports_vaddr, "lamports");
     306             : 
     307             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L357
     308             :        */
     309        1040 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, (account_infos + j), lamports_haddr );
     310        1040 :       caller_account->lamports = lamports_haddr;
     311             : 
     312             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L360-L364
     313             :        */
     314        1040 :       caller_account->owner = FD_VM_MEM_HADDR_ST( vm, (account_infos + j)->owner_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     315             : 
     316             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L367-L378
     317             :        */
     318         972 :       VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, (account_infos + j), data_vaddr );
     319             : 
     320             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L379-L386 */
     321         972 :       VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(
     322         972 :         vm, data_vaddr, acc_region_meta->vm_data_addr, "data");
     323             : 
     324             :       #ifdef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
     325             :       /* Rust ABI
     326             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L395-L404 */
     327         234 :       VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, (account_infos + j), data_len_vaddr );
     328             :       (void)acct_infos_va;
     329             :       #else
     330             :       /* C ABI
     331             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L508-L514 */
     332             :       ulong data_len_vaddr = vm_syscall_cpi_data_len_vaddr_c(
     333          68 :         fd_ulong_sat_add( acct_infos_va, fd_ulong_sat_mul( j, VM_SYSCALL_CPI_ACC_INFO_SIZE ) ),
     334             :         (ulong)&((account_infos + j)->data_sz),
     335             :         (ulong)(account_infos + j)
     336             :       );
     337             :       #endif
     338             : 
     339             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L397-L404
     340             :          C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L515-L522 */
     341         302 :       if( FD_UNLIKELY( data_len_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     342           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     343           0 :         return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     344           0 :       }
     345             : 
     346             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L411
     347             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L545 */
     348         302 :       caller_account->vm_data_vaddr = data_vaddr;
     349             : 
     350             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L405-L406
     351             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L523-L524 */
     352         302 :       void * data_len = FD_VM_MEM_HADDR_ST( vm, data_len_vaddr, 1UL, sizeof(ulong) );
     353           0 :       caller_account->ref_to_len_in_vm = data_len;
     354             : 
     355             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L408-L421
     356             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L525-L538
     357             : 
     358             :          Both ABIs call CallerAccount::get_serialized_data:
     359             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L250-L299 */
     360             : 
     361             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L262-L272 */
     362         302 :       ulong address_space_reserved_for_account;
     363         302 :       if( vm->is_deprecated ) {
     364           0 :         address_space_reserved_for_account = acc_region_meta->original_data_len;
     365         302 :       } else {
     366         302 :         address_space_reserved_for_account = fd_ulong_sat_add( acc_region_meta->original_data_len, MAX_PERMITTED_DATA_INCREASE );
     367         302 :       }
     368         302 :       if( FD_UNLIKELY( FD_LOAD( ulong, data_len ) > address_space_reserved_for_account ) ) {
     369           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     370           0 :         return -1;
     371           0 :       }
     372             : 
     373             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428
     374             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428 */
     375         302 :       caller_account->orig_data_len = acc_region_meta->original_data_len;
     376             : 
     377             :       ////// END from_account_info
     378             : 
     379             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1148-L1156 */
     380         302 :       FD_VM_CU_UPDATE( vm, FD_LOAD( ulong, data_len ) / FD_VM_CPI_BYTES_PER_UNIT );
     381           0 :       (*out_len)++;
     382         302 :     }
     383             : 
     384         310 :     if( !found ) {
     385             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1183-L1188 */
     386           8 :       FD_BASE58_ENCODE_32_BYTES( account_key->uc, id_b58 );
     387           8 :       fd_log_collector_msg_many( vm->instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
     388           8 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_MISSING_ACC );
     389           8 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     390           8 :     }
     391         310 :   }
     392             : 
     393         920 :   return FD_VM_SUCCESS;
     394        1015 : }
     395             : 
     396             : /* fd_vm_cpi_update_caller_acc_{rust/c} mirrors the behaviour of
     397             : solana_bpf_loader_program::syscalls::cpi::update_caller_account:
     398             : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1171-L1268
     399             : 
     400             : This method should be called after a CPI instruction execution has
     401             : returned. It updates the given caller account info with any changes the callee
     402             : has made to this account during execution, so that those changes are
     403             : reflected in the rest of the caller's execution.
     404             : 
     405             : Those changes will be in the instructions borrowed accounts cache.
     406             : 
     407             : Parameters:
     408             : - vm: handle to the vm
     409             : - caller_account: translated caller account to update
     410             : - borrowed_callee_acc: already-borrowed callee account
     411             : */
     412         156 : #define VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_cpi_update_caller_acc_, VM_SYSCALL_CPI_ABI)
     413             : static int
     414             : VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( fd_vm_t *                          vm,
     415             :                                        fd_vm_cpi_caller_account_t *       caller_account,
     416         156 :                                        fd_borrowed_account_t *            borrowed_callee_acc ) {
     417             : 
     418             :   /* Update the caller account lamports with the value from the callee
     419             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1191 */
     420         156 :   FD_STORE( ulong, caller_account->lamports, borrowed_callee_acc->acc->lamports );
     421             : 
     422             :   /* Update the caller account owner with the value from the callee
     423             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1192 */
     424         156 :   fd_memcpy( caller_account->owner, borrowed_callee_acc->acc->owner, sizeof(fd_pubkey_t) );
     425             : 
     426             :   /* Update the caller account data with the value from the callee
     427             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1194-L1195 */
     428         156 :   ulong prev_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
     429         156 :   ulong post_len = borrowed_callee_acc->acc->data_len;
     430             : 
     431             :   /* With the deprecated loader, the reserved space equals the original
     432             :      length.
     433             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1197-L1204 */
     434         156 :   ulong address_space_reserved_for_account;
     435         156 :   if( vm->is_deprecated ) {
     436           0 :     address_space_reserved_for_account = caller_account->orig_data_len;
     437         156 :   } else {
     438         156 :     address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
     439         156 :   }
     440             : 
     441             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1206-L1216 */
     442         156 :   if( post_len > address_space_reserved_for_account ) {
     443           0 :     ulong max_increase = fd_ulong_sat_sub( address_space_reserved_for_account, caller_account->orig_data_len );
     444           0 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, "Account data size realloc limited to %lu in inner instructions", max_increase );
     445           0 :     FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     446           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     447           0 :   }
     448             : 
     449             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1218-L1252 */
     450         156 :   if( prev_len != post_len ) {
     451             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1240-L1241 */
     452          26 :     FD_STORE( ulong, caller_account->ref_to_len_in_vm, post_len );
     453             : 
     454             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1243-L1251 */
     455          26 :     void * caller_len = FD_VM_MEM_HADDR_ST( vm, fd_ulong_sat_sub(caller_account->vm_data_vaddr, sizeof(ulong)), alignof(ulong), sizeof(ulong) );
     456          26 :     FD_STORE( ulong, caller_len, post_len );
     457          26 :   }
     458             : 
     459         156 :   return FD_VM_SUCCESS;
     460         156 : }
     461             : 
     462             : /* fd_vm_syscall_cpi_{rust/c} is the entrypoint for the sol_invoke_signed_{rust/c} syscalls.
     463             : 
     464             : The bulk of the high-level logic mirrors Solana's cpi_common entrypoint function at
     465             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L847-L977
     466             : 
     467             : 100-foot flow:
     468             : - Translate the CPI ABI structures to the FD runtime's instruction format
     469             : - Update the callee accounts with any changes made by the caller prior to this CPI instruction
     470             : - Dispatch the instruction to the FD runtime (actually making the CPI call)
     471             : - Update the caller accounts with any changes made by the callee during CPI execution
     472             : 
     473             : Parameters:
     474             : - vm: pointer to the virtual machine handle
     475             : - instruction_va: vm address of the instruction to execute, which will be in the language-specific ABI format.
     476             : - acct_infos_va: vm address of the account infos, which will be in the language-specific ABI format.
     477             : - acct_info_cnt: number of account infos
     478             : - signers_seeds_va: vm address of the signers seeds
     479             : - signers_seeds_cnt: number of signers seeds
     480             : */
     481             : #define VM_SYSCALL_CPI_ENTRYPOINT FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_, VM_SYSCALL_CPI_ABI)
     482             : int
     483             : VM_SYSCALL_CPI_ENTRYPOINT( void *  _vm,
     484             :                            ulong   instruction_va,
     485             :                            ulong   acct_infos_va,
     486             :                            ulong   acct_info_cnt,
     487             :                            ulong   signers_seeds_va,
     488        2048 :                            ulong   signers_seeds_cnt ) {
     489        2048 :   long const regime0 = fd_tickcount();
     490             : 
     491        2048 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     492             : 
     493             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L859-L864 */
     494        2048 :   FD_VM_CU_UPDATE( vm, get_cpi_invoke_unit_cost() );
     495             : 
     496             :   /* Translate instruction ********************************************/
     497             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L878-L883
     498             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L575-L636
     499             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L709-L774 */
     500             : 
     501             :   /* Translating the CPI instruction
     502             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L581
     503             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L715 */
     504        2048 :   VM_SYSCALL_CPI_INSTR_T const * cpi_instruction =
     505        6054 :     FD_VM_MEM_HADDR_LD( vm, instruction_va, VM_SYSCALL_CPI_INSTR_ALIGN, VM_SYSCALL_CPI_INSTR_SIZE );
     506             : 
     507             :   /* This needs to be here for the C ABI
     508             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L717
     509             :    */
     510        3491 :   fd_pubkey_t const * program_id = (fd_pubkey_t *)VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, cpi_instruction );
     511             : 
     512             :   /* Translate CPI account metas
     513             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L582-L587
     514             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L718-L723 */
     515        1965 :   VM_SYSCALL_CPI_ACC_META_T const * cpi_account_metas =
     516        3925 :     FD_VM_MEM_SLICE_HADDR_LD( vm, VM_SYSCALL_CPI_INSTR_ACCS_ADDR( cpi_instruction ),
     517        3925 :                               VM_SYSCALL_CPI_ACC_META_ALIGN,
     518        3925 :                               fd_ulong_sat_mul( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_ACC_META_SIZE ) );
     519             : 
     520             :   /* Translate instruction data
     521             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L588-L593
     522             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L724 */
     523             : 
     524        3860 :   uchar const * data = FD_VM_MEM_SLICE_HADDR_LD(
     525        3860 :     vm, VM_SYSCALL_CPI_INSTR_DATA_ADDR( cpi_instruction ),
     526        3860 :     FD_VM_ALIGN_RUST_U8,
     527        3860 :     VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ));
     528             : 
     529             : 
     530             :   /* Instruction checks
     531             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L595
     532             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L726 */
     533             : 
     534        1899 :   int err = fd_vm_syscall_cpi_check_instruction( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) );
     535        3860 :   if( FD_UNLIKELY( err ) ) {
     536           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
     537           0 :     return err;
     538           0 :   }
     539             : 
     540             :   /* Agave consumes CU in translate_instruction
     541             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L597-L599
     542             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L728-L730 */
     543        1899 :   ulong total_cu_translation_cost = VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) / FD_VM_CPI_BYTES_PER_UNIT;
     544             : 
     545             :   /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L601-L613
     546             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L732-L745 */
     547             :   /* Agave bills the same regardless of ABI */
     548        1899 :   ulong account_meta_translation_cost =
     549        1899 :     fd_ulong_sat_mul(
     550        1899 :       VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ),
     551        1899 :       FD_VM_RUST_ACCOUNT_META_SIZE ) /
     552        1899 :     FD_VM_CPI_BYTES_PER_UNIT;
     553        1899 :   total_cu_translation_cost = fd_ulong_sat_add( total_cu_translation_cost, account_meta_translation_cost );
     554        1899 :   FD_VM_CU_UPDATE( vm, total_cu_translation_cost );
     555             : 
     556             :   /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L617-L629
     557             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L749-L767 */
     558        7232 :   for( ulong i=0UL; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ); i++ ) {
     559        5386 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_account_metas[i];
     560        5386 :     if( FD_UNLIKELY( cpi_acct_meta->is_signer > 1U || cpi_acct_meta->is_writable > 1U ) ) {
     561          11 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_ARG );
     562          11 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     563          11 :     }
     564             :     /* Rust ABI: no-op
     565             :        C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L760-L761
     566             :      */
     567        8532 :     (void)VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta );
     568        8532 :   }
     569             : 
     570             :   /* Derive PDA signers
     571             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L887-L893
     572             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L665-L707
     573             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L803-L845
     574             : 
     575             :      Note that we don't need any ABI-specific logic here, because the two ABIs are actually identical for the seeds.*/
     576        1846 :   fd_pubkey_t signers[ FD_CPI_MAX_SIGNER_CNT ] = {0};
     577        1846 :   fd_pubkey_t * caller_program_id = &vm->instr_ctx->txn_out->accounts.keys[ vm->instr_ctx->instr->program_id ];
     578        1846 :   if( FD_LIKELY( signers_seeds_cnt > 0UL ) ) {
     579         441 :     fd_vm_vec_t const * signers_seeds = FD_VM_MEM_SLICE_HADDR_LD( vm, signers_seeds_va, FD_VM_ALIGN_RUST_SLICE_U8_REF, fd_ulong_sat_mul( signers_seeds_cnt, FD_VM_VEC_SIZE ) );
     580         216 :     if( FD_UNLIKELY( signers_seeds_cnt > FD_CPI_MAX_SIGNER_CNT ) ) {
     581           5 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS );
     582           5 :       return FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS;
     583           5 :     }
     584             : 
     585         378 :     for( ulong i=0UL; i<signers_seeds_cnt; i++ ) {
     586             : 
     587             :       /* This function will precompute the memory translation required and do
     588             :         some preflight checks. */
     589         254 :       void const * signer_seed_haddrs[ FD_VM_PDA_SEEDS_MAX ];
     590         254 :       ulong        signer_seed_lens  [ FD_VM_PDA_SEEDS_MAX ];
     591             : 
     592         254 :       int err = fd_vm_translate_and_check_program_address_inputs( vm,
     593         254 :                                                                   signers_seeds[i].addr,
     594         254 :                                                                   signers_seeds[i].len,
     595         254 :                                                                   0UL,
     596         254 :                                                                   signer_seed_haddrs,
     597         254 :                                                                   signer_seed_lens ,
     598         254 :                                                                   NULL,
     599         254 :                                                                   0U );
     600         254 :       if( FD_UNLIKELY( err ) ) {
     601          11 :         return err;
     602          11 :       }
     603             : 
     604         243 :       err = fd_vm_derive_pda( vm, caller_program_id, signer_seed_haddrs, signer_seed_lens, signers_seeds[i].len, NULL, &signers[i] );
     605         243 :       if( FD_UNLIKELY( err ) ) {
     606          76 :         FD_TXN_PREPARE_ERR_OVERWRITE( vm->instr_ctx->txn_out );
     607          76 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_BAD_SEEDS );
     608          76 :         return FD_VM_SYSCALL_ERR_BAD_SEEDS;
     609          76 :       }
     610         243 :     }
     611         211 :   }
     612             : 
     613             :   /* Authorized program check
     614             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L894 */
     615        1744 :   if( FD_UNLIKELY( !fd_vm_syscall_cpi_check_authorized_program( program_id, vm->instr_ctx->bank, data, VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) ) ) ) {
     616          57 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED );
     617          57 :     return FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED;
     618          57 :   }
     619             : 
     620             :   /* Create the instruction to execute (in the input format the FD runtime expects) from
     621             :      the translated CPI ABI inputs.
     622             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L895 */
     623        1687 :   fd_pubkey_t cpi_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ];
     624        1687 :   fd_instr_info_t * instruction_to_execute = &vm->instr_ctx->runtime->instr.trace[ vm->instr_ctx->runtime->instr.trace_length++ ];
     625             : 
     626        1687 :   err = VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( vm, cpi_instruction, cpi_account_metas, program_id, data, instruction_to_execute, cpi_instr_acct_keys );
     627        1687 :   if( FD_UNLIKELY( err ) ) {
     628           0 :     return err;
     629           0 :   }
     630             : 
     631             :   /* Prepare the instruction for execution in the runtime. This is required by the runtime
     632             :      before we can pass an instruction to the executor. */
     633        1687 :   fd_instruction_account_t instruction_accounts[ FD_TXN_INSTR_ACCT_MAX ];
     634        1687 :   ulong instruction_accounts_cnt;
     635        1687 :   err = fd_vm_prepare_instruction( instruction_to_execute, vm->instr_ctx, program_id, cpi_instr_acct_keys, instruction_accounts, &instruction_accounts_cnt, signers, signers_seeds_cnt );
     636             :   /* Errors are propagated in the function itself. */
     637        1687 :   if( FD_UNLIKELY( err ) ) {
     638         451 :     return err;
     639         451 :   }
     640             : 
     641             :   /* Translate account infos
     642             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L897-L903
     643             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L987-L1047 */
     644             : 
     645             :   /* Verify that the account_infos array is not inside the input
     646             :      region. This prevents programs from passing pointers to
     647             :      the serialized account data region as account_infos, which would allow them to
     648             :      bypass pointer validation checks.
     649             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1002-L1011 */
     650        1236 :   ulong acc_info_total_sz = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
     651        1236 :   if( FD_UNLIKELY( fd_ulong_sat_add( acct_infos_va, acc_info_total_sz ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     652          21 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     653          21 :     return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     654          21 :   }
     655             : 
     656             :   /* This is the equivalent of translate_slice in translate_account_infos */
     657        2428 :   VM_SYSCALL_CPI_ACC_INFO_T const * acc_infos = FD_VM_MEM_SLICE_HADDR_LD( vm, acct_infos_va, VM_SYSCALL_CPI_ACC_INFO_ALIGN, acc_info_total_sz );
     658             : 
     659             :   /* Right after translating, Agave checks the number of account infos */
     660        1213 :   if( FD_UNLIKELY( acct_info_cnt > get_cpi_max_account_infos() ) ) {
     661           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED );
     662           0 :     return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
     663           0 :   }
     664             : 
     665             :   /* Consume compute units proportional to the number of account infos */
     666        1213 :   ulong account_infos_bytes = fd_ulong_sat_mul( acct_info_cnt, FD_VM_ACCOUNT_INFO_BYTE_SIZE );
     667        1213 :   FD_VM_CU_UPDATE( vm, account_infos_bytes / FD_VM_CPI_BYTES_PER_UNIT );
     668             : 
     669           0 :   fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS ];
     670        3367 :   for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
     671             :     /* Translate each pubkey address specified in account_infos.
     672             :        Failed translation should lead to an access violation and
     673             :        implies that obviously bad account_info has been supplied. */
     674        6662 :       acct_info_keys[ acct_idx ] = FD_VM_MEM_HADDR_LD( vm, acc_infos[ acct_idx ].pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     675        6662 :   }
     676             : 
     677             :   /* translate_accounts_common ***************************************************************
     678             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193 */
     679        1013 :   fd_vm_cpi_translated_account_t translated_accounts[ FD_TXN_INSTR_ACCT_MAX ];
     680        1013 :   ulong translated_accounts_len = 0UL;
     681        1013 :   err = VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     682        1013 :     vm,
     683        1013 :     instruction_accounts,
     684        1013 :     instruction_accounts_cnt,
     685        1013 :     acct_infos_va,
     686        1013 :     acct_info_keys,
     687        1013 :     acc_infos,
     688        1013 :     acct_info_cnt,
     689        1013 :     translated_accounts,
     690        1013 :     &translated_accounts_len
     691        1013 :   );
     692             :   /* errors are propagated in the function itself. */
     693        1013 :   if( FD_UNLIKELY( err ) ) return err;
     694             : 
     695             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L905-L928 */
     696        1215 :   for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     697         297 :     fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     698         297 :     fd_guarded_borrowed_account_t callee_acc = {0};
     699         297 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
     700         297 :     uchar update_caller = 0;
     701         297 :     err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc, &update_caller );
     702         297 :     if( FD_UNLIKELY( err ) ) {
     703           0 :       return err;
     704           0 :     }
     705         297 :     translated_account->update_caller_account_region =
     706         297 :         (uchar)( translated_account->update_caller_account_info || update_caller );
     707         297 :   }
     708             : 
     709             :   /* Set the transaction compute meter to be the same as the VM's compute meter,
     710             :      so that the callee cannot use compute units that the caller has already used. */
     711         918 :   vm->instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
     712             : 
     713         918 :   long const regime1 = fd_tickcount();
     714             : 
     715             :   /* Execute the CPI instruction in the runtime */
     716         918 :   int err_exec = fd_execute_instr( vm->instr_ctx->runtime, vm->instr_ctx->bank, vm->instr_ctx->txn_in, vm->instr_ctx->txn_out, instruction_to_execute );
     717         918 :   ulong instr_exec_res = (ulong)err_exec;
     718             : 
     719         918 :   long const regime2 = fd_tickcount();
     720         918 :   vm->instr_ctx->runtime->metrics.cpi_setup_cum_ticks += (ulong)( regime1-regime0 );
     721             : 
     722             :   /* Set the CU meter to the instruction context's transaction context's compute meter,
     723             :      so that the caller can't use compute units that the callee has already used. */
     724         918 :   vm->cu = vm->instr_ctx->txn_out->details.compute_budget.compute_meter;
     725             : 
     726         918 :   vm->reg[0] = instr_exec_res;
     727             : 
     728             :   /* Errors are propagated in fd_execute_instr. */
     729         918 :   if( FD_UNLIKELY( err_exec ) ) return err_exec;
     730             : 
     731             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L942-L957 */
     732         588 :   for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     733         239 :     fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     734         239 :     fd_guarded_borrowed_account_t callee_acc = {0};
     735         239 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
     736         239 :     if( !translated_account->update_caller_account_info ) continue;
     737         156 :     err = VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc );
     738         156 :     if( FD_UNLIKELY( err ) ) {
     739           0 :       return err;
     740           0 :     }
     741         156 :   }
     742             : 
     743             :   /* Update the caller's memory regions to reflect any changes the
     744             :      callee made to account data.
     745             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L959-L973 */
     746         588 :   for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     747         239 :     fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     748         239 :     fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
     749         239 :     err = fd_exec_instr_ctx_try_borrow_instr_account( vm->instr_ctx, translated_account->index_in_caller, &borrowed_callee_acc );
     750         239 :     if( FD_UNLIKELY( err ) ) return err;
     751         239 :     if( !translated_account->update_caller_account_region ) continue;
     752             : 
     753         156 :     err = fd_vm_cpi_update_caller_account_region( vm, translated_account, &borrowed_callee_acc );
     754         156 :     if( FD_UNLIKELY( err ) ) {
     755           0 :       return err;
     756           0 :     }
     757         156 :   }
     758             : 
     759         349 :   long const regime3 = fd_tickcount();
     760         349 :   vm->instr_ctx->runtime->metrics.cpi_commit_cum_ticks += (ulong)( regime3-regime2 );
     761             : 
     762         349 :   return FD_VM_SUCCESS;
     763         349 : }
     764             : 
     765             : #undef VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC
     766             : #undef VM_SYSCALL_CPI_FROM_ACC_INFO_FUNC
     767             : #undef VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC
     768             : #undef VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC
     769             : #undef VM_SYSCALL_CPI_FUNC

Generated by: LCOV version 1.14