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: 346 439 78.8 %
Date: 2026-10-22 18:10: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        4609 :   if( FD_UNLIKELY( vm_addr!=expected_vm_addr )) {                                                         \
      27         104 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,                                             \
      28         104 :       "Invalid account info pointer `%s': 0x%lx != 0x%lx", field_name, vm_addr, expected_vm_addr );         \
      29         104 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );                                   \
      30         104 :     return FD_VM_SYSCALL_ERR_INVALID_POINTER;                                                             \
      31         104 :   }
      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             : - signers: derived signers for this CPI call
      49             : - signers_cnt: length of the signers list
      50             : - cpi_instr_data: instruction data in host address space
      51             : 
      52             : TODO: return codes/errors?
      53             : */
      54        1787 : #define VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_instruction_to_instr_, VM_SYSCALL_CPI_ABI)
      55             : static int
      56             : VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( fd_vm_t *                         vm,
      57             :                                           VM_SYSCALL_CPI_INSTR_T const *    cpi_instr,
      58             :                                           VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_metas,
      59             :                                           fd_pubkey_t const *               program_id,
      60             :                                           uchar const *                     cpi_instr_data,
      61             :                                           fd_instr_info_t *                 out_instr,
      62        1788 :                                           fd_pubkey_t                       out_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ] ) {
      63             : 
      64        1788 :   out_instr->program_id   = UCHAR_MAX;
      65        1788 :   out_instr->stack_height = (uchar)( vm->instr_ctx->runtime->instr.stack_sz+1 );
      66        1788 :   out_instr->data_sz      = (ushort)VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instr );
      67        1788 :   out_instr->acct_cnt     = (ushort)VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr );
      68        1788 :   if( FD_LIKELY( out_instr->data_sz ) ) memcpy( out_instr->data, cpi_instr_data, out_instr->data_sz );
      69             : 
      70             :   /* Find the index of the CPI instruction's program account in the transaction */
      71        1788 :   ulong program_id_idx = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, program_id );
      72        1788 :   if( FD_LIKELY( program_id_idx!=ULONG_MAX ) ) out_instr->program_id = (uchar)program_id_idx;
      73             : 
      74        1788 :   uchar  acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0};
      75        1788 :   ushort caller_map[ MAX_TX_ACCOUNT_LOCKS ];
      76        1788 :   fd_exec_instr_ctx_instr_account_map( vm->instr_ctx, caller_map );
      77             : 
      78        7067 :   for( ushort i=0; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr ); i++ ) {
      79        5279 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_acct_metas[i];
      80        6372 :     fd_pubkey_t const * pubkey = fd_type_pun_const( VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta ) );
      81           0 :     out_instr_acct_keys[i] = *pubkey;
      82             : 
      83             :     /* The parent flag(s) for is writable/signer is checked in
      84             :        fd_vm_prepare_instruction. Signer privilege is allowed iff the account
      85             :        is a signer in the caller or if it is a derived signer. */
      86             :     /* TODO: error if flags are wrong */
      87        6372 :     out_instr->accounts[i] = fd_instruction_account_init( USHORT_MAX,
      88        6372 :                                                           USHORT_MAX,
      89        6372 :                                                           USHORT_MAX,
      90        6372 :                                                           VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
      91        6372 :                                                           VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
      92             : 
      93             :     /* Use USHORT_MAX to indicate account not found
      94             :         https://github.com/anza-xyz/agave/blob/v3.0.4/program-runtime/src/invoke_context.rs#L395-L397 */
      95        6372 :     ulong idx_in_txn = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, pubkey );
      96             : 
      97        6372 :     fd_instr_info_setup_instr_account( out_instr,
      98        6372 :                                        acc_idx_seen,
      99        6372 :                                        idx_in_txn!=ULONG_MAX ? (ushort)idx_in_txn : USHORT_MAX,
     100        6372 :                                        idx_in_txn!=ULONG_MAX ? caller_map[ idx_in_txn ] : USHORT_MAX,
     101        6372 :                                        i,
     102        6372 :                                        VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
     103        6372 :                                        VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
     104             : 
     105        6372 :   }
     106             : 
     107        1788 :   return FD_VM_SUCCESS;
     108        1788 : }
     109             : 
     110             : /*
     111             : fd_vm_syscall_cpi_update_callee_acc_{rust/c} corresponds to solana_bpf_loader_program::syscalls::cpi::update_callee_account:
     112             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1211-L1273
     113             : 
     114             : (the copy of the account stored in the instruction context's
     115             : borrowed accounts cache)
     116             : 
     117             : This function should be called before the CPI instruction is executed. Its purpose is to
     118             : update the callee account's view of the given account with any changes the caller may made
     119             : to the account before the CPI instruction is executed.
     120             : 
     121             : The callee's view of the account is the borrowed accounts cache, so to update the
     122             : callee account we look up the account in the borrowed accounts cache and update it.
     123             : 
     124             : Parameters:
     125             : - vm: pointer to the virtual machine handle
     126             : - account_info: account info object
     127             : - callee_acc_pubkey: pubkey of the account. this is used to look up the account in the borrowed accounts cache
     128             :   (TODO: this seems redundant? we can probably remove this, as the account_info contains the pubkey)
     129             : */
     130        1099 : #define VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_update_callee_acc_, VM_SYSCALL_CPI_ABI)
     131             : static int
     132             : VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( fd_vm_t *                          vm,
     133             :                                       fd_vm_cpi_caller_account_t const * caller_account,
     134             :                                       fd_borrowed_account_t *            callee_acc,
     135        1099 :                                       uchar *                            out_must_update_caller ) {
     136        1099 :   int err;
     137        1099 :   *out_must_update_caller = 0;
     138             : 
     139             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1222-L1224 */
     140        1099 :   if( fd_borrowed_account_get_lamports( callee_acc )!=FD_LOAD( ulong, caller_account->lamports ) ) {
     141           0 :     err = fd_borrowed_account_set_lamports( callee_acc, FD_LOAD( ulong, caller_account->lamports ) );
     142           0 :     if( FD_UNLIKELY( err ) ) {
     143           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     144           0 :       return -1;
     145           0 :     }
     146           0 :   }
     147             : 
     148             :   /* With virtual_address_space_adjustments enabled, we validate account
     149             :      length changes and update the associated borrowed account with any
     150             :      changed made. If direct mapping is also enabled, we skip actually copying
     151             :      the data back to the borrowed account, as it is already updated in-place.
     152             : 
     153             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1226-L1255 */
     154        1099 :   if( vm->virtual_address_space_adjustments ) {
     155          43 :     ulong prev_len = fd_borrowed_account_get_data_len( callee_acc );
     156          43 :     ulong post_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
     157             : 
     158             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1229-L1251 */
     159          43 :     if( FD_UNLIKELY( prev_len!=post_len ) ) {
     160             :       /* If the account has been shrunk, we're going to zero the unused
     161             :          memory that was previously used. */
     162             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1230-L1247 */
     163           0 :       if( FD_UNLIKELY( !vm->direct_mapping && ( post_len < prev_len ) ) ) {
     164           0 :         fd_memset( caller_account->serialized_data + post_len, 0, prev_len - post_len );
     165           0 :       }
     166             : 
     167             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1248 */
     168           0 :       err = fd_borrowed_account_set_data_length( callee_acc, post_len );
     169           0 :       if( FD_UNLIKELY( err ) ) {
     170           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     171           0 :         return -1;
     172           0 :       }
     173             :       /* Pointer to data may have changed, caller must be updated.
     174             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1248-L1250 */
     175           0 :       *out_must_update_caller = 1;
     176           0 :     }
     177             : 
     178             :     /* Without direct mapping, we need to copy the account data from the VM's
     179             :        serialized buffer back to the borrowed account. With direct mapping,
     180             :        data is modified in-place so no copy is needed.
     181             :        https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1252-L1254 */
     182          43 :     int err;
     183          43 :     if( !vm->direct_mapping && fd_borrowed_account_can_data_be_changed( callee_acc, &err ) ) {
     184           0 :       err = fd_borrowed_account_set_data_from_slice( callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
     185           0 :       if( FD_UNLIKELY( err ) ) {
     186           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     187           0 :         return -1;
     188           0 :       }
     189           0 :     }
     190        1056 :   } else {
     191             :     /* Direct mapping is not enabled, so we need to copy the account data
     192             :        from the VM's serialized buffer back to the borrowed account.
     193             : 
     194             :        https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/program-runtime/src/cpi.rs#L1155-L1162 */
     195        1056 :     int err;
     196        1056 :     if( fd_borrowed_account_can_data_be_resized( callee_acc, caller_account->serialized_data_len, &err ) ) {
     197             :       /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/program-runtime/src/cpi.rs#L1157 */
     198           8 :       err = fd_borrowed_account_set_data_from_slice( callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
     199           8 :       if( FD_UNLIKELY( err ) ) {
     200           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     201           0 :         return -1;
     202           0 :       }
     203        1048 :     } else if( FD_UNLIKELY( caller_account->serialized_data_len!=fd_borrowed_account_get_data_len( callee_acc ) ||
     204        1048 :                             (caller_account->serialized_data_len &&
     205        1048 :                               memcmp( fd_borrowed_account_get_data( callee_acc ), caller_account->serialized_data, caller_account->serialized_data_len )) ) ) {
     206             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1259-L1261 */
     207           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     208           0 :       return -1;
     209           0 :     }
     210        1056 :   }
     211             : 
     212             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1266-L1271 */
     213        1099 :   if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( callee_acc ), caller_account->owner, sizeof(fd_pubkey_t) ) ) ) {
     214           0 :     err = fd_borrowed_account_set_owner( callee_acc, caller_account->owner );
     215           0 :     if( FD_UNLIKELY( err ) ) {
     216           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     217           0 :       return -1;
     218           0 :     }
     219             :     /* Caller gave ownership and thus write access away, so caller must be updated.
     220             :        https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1268-L1270 */
     221           0 :     *out_must_update_caller = 1;
     222           0 :   }
     223             : 
     224        1099 :   return FD_VM_SUCCESS;
     225        1099 : }
     226             : 
     227             : /*
     228             : fd_vm_syscall_cpi_translate_and_update_accounts_ mirrors the behaviour of
     229             : solana_program_runtime::cpi::SyscallInvokeSigned::translate_accounts_common:
     230             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193
     231             : 
     232             : It translates the caller accounts to the host address space, and then calls
     233             : fd_vm_syscall_cpi_update_callee_acc to update the callee borrowed account with any changes
     234             : the caller has made to the account during execution before this CPI call.
     235             : 
     236             : Parameters:
     237             : - vm: pointer to the virtual machine handle
     238             : - instruction_accounts: array of instruction accounts
     239             : - instruction_accounts_cnt: length of the instruction_accounts array
     240             : - account_infos: array of account infos
     241             : - account_infos_length: length of the account_infos array
     242             : 
     243             : Populates:
     244             : - translated_accounts: the translated account entries
     245             : - out_len: number of translated account entries
     246             : */
     247        1312 : #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)
     248             : static int
     249             : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     250             :                               fd_vm_t *                         vm,
     251             :                               fd_instruction_account_t const *  instruction_accounts,
     252             :                               ulong const                       instruction_accounts_cnt,
     253             :                               ulong                             acct_infos_va,
     254             :                               fd_pubkey_t const * *             account_info_keys, /* same length as account_infos_length */
     255             :                               VM_SYSCALL_CPI_ACC_INFO_T const * account_infos,
     256             :                               ulong const                       account_infos_length,
     257             :                               fd_vm_cpi_translated_account_t *  translated_accounts,
     258        1313 :                               ulong *                           out_len ) {
     259        4068 :   for( ulong i=0UL; i<instruction_accounts_cnt; i++ ) {
     260        2867 :     if( i!=instruction_accounts[i].index_in_callee ) {
     261             :       /* Skip duplicate accounts */
     262         226 :       continue;
     263         226 :     }
     264             : 
     265             :     /* `fd_vm_prepare_instruction()` will always set up a valid index for `index_in_caller`, so we can access the borrowed account directly.
     266             :        A borrowed account will always have non-NULL meta (if the account doesn't exist, `fd_executor_setup_accounts_for_txn()`
     267             :        will set its meta up) */
     268             : 
     269             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1102 */
     270        2641 :     fd_guarded_borrowed_account_t callee_acct = {0};
     271        2641 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, instruction_accounts[i].index_in_caller, &callee_acct );
     272             : 
     273        2641 :     fd_pubkey_t const *      account_key = (fd_pubkey_t*)callee_acct.acc->pubkey;
     274             : 
     275             :     /* If the account is known and executable, we only need to consume the compute units.
     276             :        Executable accounts can't be modified, so we don't need to update the callee account. */
     277        2641 :     if( fd_borrowed_account_is_executable( &callee_acct ) ) {
     278             :       // FIXME: should this be FD_VM_CU_MEM_UPDATE? Changing this changes the CU behaviour from main (because of the base cost)
     279        1419 :       FD_VM_CU_UPDATE( vm, callee_acct.acc->data_len / FD_VM_CPI_BYTES_PER_UNIT );
     280           0 :       continue;
     281        1419 :     }
     282             : 
     283             :     /* FIXME: we should not need to drop the account here to avoid a double borrow.
     284             :        Instead, we should borrow the account before entering this function. */
     285        1222 :     fd_borrowed_account_drop( &callee_acct );
     286             : 
     287             :     /* Find the indices of the account in the caller and callee instructions */
     288        1222 :     uint found = 0;
     289        9505 :     for( ushort j=0; j<account_infos_length && !found; j++ ) {
     290        8387 :       fd_pubkey_t const * acct_addr = account_info_keys[ j ];
     291             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1117
     292             :        */
     293        8387 :       if( memcmp( account_key->uc, acct_addr->uc, sizeof(fd_pubkey_t) ) != 0 ) {
     294        7173 :         continue;
     295        7173 :       }
     296             : 
     297        1214 :       fd_vm_cpi_translated_account_t * translated_account = translated_accounts + *out_len;
     298        1214 :       fd_vm_cpi_caller_account_t *     caller_account     = &translated_account->caller_account;
     299        1214 :       ushort                           index_in_caller    = instruction_accounts[i].index_in_caller;
     300        1214 :       translated_account->index_in_caller                 = index_in_caller;
     301        1214 :       translated_account->update_caller_account_info      = (uchar)!!instruction_accounts[i].is_writable;
     302        1214 :       found = 1;
     303             : 
     304             :       /* Logically this check isn't ever going to fail due to how the
     305             :          account_info_keys array is set up.  We replicate the check for
     306             :          clarity and also to guard against accidental violation of the
     307             :          assumed invariant in the future.
     308             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1131-L1134
     309             :        */
     310        1214 :       if( FD_UNLIKELY( j >= account_infos_length ) ) {
     311           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     312           0 :         return FD_VM_SYSCALL_ERR_INVALID_LENGTH;
     313           0 :       }
     314             : 
     315             :       /* The following implements the checks in from_account_info which
     316             :          is invoked as do_translate() in translate_and_update_accounts()
     317             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1135-L1146
     318             :        */
     319             :       ////// BEGIN from_account_info
     320             : 
     321        1214 :       fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[index_in_caller];
     322             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L321-L334 */
     323        1214 :       if( FD_LIKELY( vm->syscall_parameter_address_restrictions ) ) {
     324             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L322-L327 */
     325        1214 :         ulong expected_pubkey_vaddr = acc_region_meta->vm_key_addr;
     326             :         /* Max msg_sz: 40 + 18 + 18 = 76 < 127 */
     327        1214 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].pubkey_addr, expected_pubkey_vaddr, "key");
     328             : 
     329             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L328-L333 */
     330        1138 :         ulong expected_owner_vaddr = acc_region_meta->vm_owner_addr;
     331             :         /* Max msg_sz: 42 + 18 + 18 = 78 < 127 */
     332        1138 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].owner_addr, expected_owner_vaddr, "owner");
     333        1138 :       }
     334             : 
     335             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L336-L358 */
     336        3997 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, (account_infos + j), lamports_vaddr );
     337             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L345-L356  */
     338        3997 :       if( FD_LIKELY( vm->syscall_parameter_address_restrictions ) ) {
     339             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L346-L348
     340             :            Check that the account's lamports Rc<RefCell<&mut u64>> is not
     341             :            stored in the account region. Because a refcell is only present if
     342             :            the Rust SDK is used, we only need to check this for the Rust ABI. */
     343             :         #ifdef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
     344         953 :         VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, (account_infos + j), lamports_rc_vaddr )
     345         953 :         if ( FD_UNLIKELY( lamports_rc_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     346           0 :           FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     347           0 :           return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     348           0 :         }
     349         953 :         #endif
     350             : 
     351             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L350-L355 */
     352         953 :         ulong expected_lamports_vaddr = acc_region_meta->vm_lamports_addr;
     353             :         /* Max msg_sz: 45 + 18 + 18 = 81 < 127 */
     354        1138 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, lamports_vaddr, expected_lamports_vaddr, "lamports");
     355        1119 :       }
     356             : 
     357             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L357
     358             :        */
     359        5040 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, (account_infos + j), lamports_haddr );
     360        5040 :       caller_account->lamports = lamports_haddr;
     361             : 
     362             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L360-L364
     363             :        */
     364        5040 :       caller_account->owner = FD_VM_MEM_HADDR_ST( vm, (account_infos + j)->owner_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     365             : 
     366             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L367-L378
     367             :        */
     368        3736 :       VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, (account_infos + j), data_vaddr );
     369             : 
     370             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L379-L386 */
     371        3736 :       if( vm->syscall_parameter_address_restrictions ) {
     372        1119 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(
     373        1119 :           vm, data_vaddr, acc_region_meta->vm_data_addr, "data");
     374        1110 :       } else {
     375             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L388-L392 */
     376           0 :         VM_SYSCALL_CPI_SET_ACC_INFO_DATA_GET_LEN( vm, (account_infos + j), data_vaddr );
     377           0 :         FD_VM_CU_UPDATE( vm, data_vaddr_len / FD_VM_CPI_BYTES_PER_UNIT );
     378           0 :       }
     379             : 
     380             :       #ifdef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
     381             :       /* Rust ABI
     382             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L395-L404 */
     383         925 :       VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, (account_infos + j), data_len_vaddr );
     384         925 :       (void)acct_infos_va;
     385             :       #else
     386             :       /* C ABI
     387             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L508-L514 */
     388         185 :       ulong data_len_vaddr = vm_syscall_cpi_data_len_vaddr_c(
     389         185 :         fd_ulong_sat_add( acct_infos_va, fd_ulong_sat_mul( j, VM_SYSCALL_CPI_ACC_INFO_SIZE ) ),
     390         185 :         (ulong)&((account_infos + j)->data_sz),
     391         185 :         (ulong)(account_infos + j)
     392         185 :       );
     393         185 :       #endif
     394             : 
     395             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L397-L404
     396             :          C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L515-L522 */
     397        1110 :       if( FD_UNLIKELY( vm->syscall_parameter_address_restrictions && data_len_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     398           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     399           0 :         return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     400           0 :       }
     401             : 
     402             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L411
     403             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L545 */
     404        1110 :       caller_account->vm_data_vaddr = data_vaddr;
     405             : 
     406             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L405-L406
     407             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L523-L524 */
     408        1110 :       void * data_len = FD_VM_MEM_HADDR_ST( vm, data_len_vaddr, 1UL, sizeof(ulong) );
     409           0 :       caller_account->ref_to_len_in_vm = data_len;
     410             : 
     411             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L408-L421
     412             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L525-L538
     413             : 
     414             :          Both ABIs call CallerAccount::get_serialized_data:
     415             :          https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L250-L299 */
     416             : 
     417             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L262-L272 */
     418        1110 :       if( vm->syscall_parameter_address_restrictions ) {
     419        1110 :         ulong address_space_reserved_for_account;
     420        1110 :         if( vm->is_deprecated ) {
     421           0 :           address_space_reserved_for_account = acc_region_meta->original_data_len;
     422        1110 :         } else {
     423        1110 :           address_space_reserved_for_account = fd_ulong_sat_add( acc_region_meta->original_data_len, MAX_PERMITTED_DATA_INCREASE );
     424        1110 :         }
     425        1110 :         if( FD_UNLIKELY( FD_LOAD( ulong, data_len ) > address_space_reserved_for_account ) ) {
     426           0 :           FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     427           0 :           return -1;
     428           0 :         }
     429        1110 :       }
     430             : 
     431             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L273-L298
     432             : 
     433             :          With both virtual_address_space_adjustments and direct_mapping,
     434             :          account data is modified in-place so we don't track the
     435             :          serialized_data pointer.
     436             : 
     437             :          With virtual_address_space_adjustments only (no direct_mapping), data was copied into the input
     438             :          region buffer. We don't apply the extra memory translation checks, as
     439             :          we have checked the data pointer is valid above. So instead we add
     440             :          the vaddr to the start of the input region address space - copying
     441             :          this logic from Agave.
     442             : 
     443             :          In legacy mode, we translate the data pointer directly, as it just
     444             :          maps to a location in the single input region. */
     445        1110 :       if( vm->virtual_address_space_adjustments && vm->direct_mapping ) {
     446             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L273-L275 */
     447          28 :         caller_account->serialized_data     = NULL;
     448          28 :         caller_account->serialized_data_len = 0UL;
     449        1082 :       } else if( vm->virtual_address_space_adjustments ) {
     450             :         /* Skip translation checks here, following the Agave logic:
     451             :            https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L275-L291 */
     452          30 :         uchar * serialization_ptr           = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
     453          15 :         caller_account->serialized_data     = serialization_ptr + fd_ulong_sat_sub( data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
     454          30 :         caller_account->serialized_data_len = FD_LOAD( ulong, data_len );
     455        1067 :       } else {
     456             :         /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L291-L298 */
     457        4822 :         VM_SYSCALL_CPI_ACC_INFO_DATA( vm, (account_infos + j), data_haddr );
     458        4822 :         (void)data_haddr_vm_addr;
     459        4822 :         caller_account->serialized_data     = data_haddr;
     460        4822 :         caller_account->serialized_data_len = data_haddr_len;
     461        4822 :       }
     462             : 
     463             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428
     464             :          C ABI:    https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428 */
     465        1110 :       caller_account->orig_data_len = acc_region_meta->original_data_len;
     466             : 
     467             :       ////// END from_account_info
     468             : 
     469             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1148-L1156 */
     470        1110 :       if( vm->syscall_parameter_address_restrictions ) {
     471        1110 :         FD_VM_CU_UPDATE( vm, FD_LOAD( ulong, data_len ) / FD_VM_CPI_BYTES_PER_UNIT );
     472        1110 :       }
     473             : 
     474             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1157-L1181 */
     475        1110 :       uchar update_caller = 0;
     476        1110 :       if( vm->syscall_parameter_address_restrictions ) {
     477        1110 :         update_caller = 1;
     478        1110 :       } else {
     479           0 :         fd_guarded_borrowed_account_t callee_acc = {0};
     480           0 :         FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, index_in_caller, &callee_acc );
     481           0 :         int err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, caller_account, &callee_acc, &update_caller );
     482           0 :         if( FD_UNLIKELY( err ) ) {
     483           0 :           return err;
     484           0 :         }
     485           0 :       }
     486        1110 :       translated_account->update_caller_account_region =
     487        1110 :           (uchar)( translated_account->update_caller_account_info || update_caller );
     488        1110 :       (*out_len)++;
     489        1110 :     }
     490             : 
     491        1118 :     if( !found ) {
     492             :       /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1183-L1188 */
     493           8 :       FD_BASE58_ENCODE_32_BYTES( account_key->uc, id_b58 );
     494           8 :       fd_log_collector_msg_many( vm->instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
     495           8 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_MISSING_ACC );
     496           8 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     497           8 :     }
     498        1118 :   }
     499             : 
     500        1201 :   return FD_VM_SUCCESS;
     501        1313 : }
     502             : 
     503             : /* fd_vm_cpi_update_caller_acc_{rust/c} mirrors the behaviour of
     504             : solana_bpf_loader_program::syscalls::cpi::update_caller_account:
     505             : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1171-L1268
     506             : 
     507             : This method should be called after a CPI instruction execution has
     508             : returned. It updates the given caller account info with any changes the callee
     509             : has made to this account during execution, so that those changes are
     510             : reflected in the rest of the caller's execution.
     511             : 
     512             : Those changes will be in the instructions borrowed accounts cache.
     513             : 
     514             : Parameters:
     515             : - vm: handle to the vm
     516             : - caller_acc_info: caller account info object, which should be updated
     517             : - borrowed_callee_acc: already-borrowed callee account
     518             : */
     519         290 : #define VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_cpi_update_caller_acc_, VM_SYSCALL_CPI_ABI)
     520             : static int
     521             : VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( fd_vm_t *                          vm,
     522             :                                        fd_vm_cpi_caller_account_t *       caller_account,
     523         290 :                                        fd_borrowed_account_t *            borrowed_callee_acc ) {
     524             : 
     525             :   /* Update the caller account lamports with the value from the callee
     526             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1191 */
     527         290 :   FD_STORE( ulong, caller_account->lamports, borrowed_callee_acc->acc->lamports );
     528             : 
     529             :   /* Update the caller account owner with the value from the callee
     530             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1192 */
     531         290 :   fd_pubkey_t const * updated_owner = (fd_pubkey_t const *)borrowed_callee_acc->acc->owner;
     532         290 :   if( updated_owner ) *caller_account->owner = *updated_owner;
     533           0 :   else                fd_memset( caller_account->owner, 0,             sizeof(fd_pubkey_t) );
     534             : 
     535             :   /* Update the caller account data with the value from the callee
     536             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1194-L1195 */
     537         290 :   ulong prev_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
     538         290 :   ulong post_len = borrowed_callee_acc->acc->data_len;
     539             : 
     540             :   /* Calculate the address space reserved for the account. With syscall_parameter_address_restrictions
     541             :      and deprecated loader, the reserved space equals original length (no realloc space).
     542             :      Otherwise, we add MAX_PERMITTED_DATA_INCREASE for reallocation.
     543             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1197-L1204 */
     544         290 :   ulong address_space_reserved_for_account;
     545         290 :   if( vm->syscall_parameter_address_restrictions && vm->is_deprecated ) {
     546           0 :     address_space_reserved_for_account = caller_account->orig_data_len;
     547         290 :   } else {
     548         290 :     address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
     549         290 :   }
     550             : 
     551             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1206-L1216 */
     552         290 :   if( post_len > address_space_reserved_for_account &&
     553         290 :     ( vm->syscall_parameter_address_restrictions || prev_len != post_len ) ) {
     554           0 :     ulong max_increase = fd_ulong_sat_sub( address_space_reserved_for_account, caller_account->orig_data_len );
     555           0 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, "Account data size realloc limited to %lu in inner instructions", max_increase );
     556           0 :     FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     557           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     558           0 :   }
     559             : 
     560             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1218-L1252 */
     561         290 :   if( prev_len != post_len ) {
     562             : 
     563             :     /* Without direct mapping, we need to adjust the serialized data buffer
     564             :        when the length changes.
     565             : 
     566             :        With direct mapping, data is mapped in-place so no buffer manipulation
     567             :        is needed.
     568             : 
     569             :        https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1219-L1239 */
     570          33 :     if( !( vm->virtual_address_space_adjustments && vm->direct_mapping ) ) {
     571             : 
     572             :       /* If the account has shrunk, zero out memory that was previously used
     573             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1222-L1230 */
     574          33 :       if( post_len < prev_len ) {
     575             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1227-L1228 */
     576           2 :         if( caller_account->serialized_data_len < post_len ) {
     577           0 :           FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL );
     578           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     579           0 :         }
     580             : 
     581             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1225-L1229 */
     582           2 :         fd_memset( caller_account->serialized_data + post_len, 0, caller_account->serialized_data_len - post_len );
     583           2 :       }
     584             : 
     585             :       /* Set caller_account.serialized_data to post_len.
     586             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1231-L1238 */
     587          33 :       if( vm->virtual_address_space_adjustments ) {
     588             :         /* Calculate the serialized data pointer from the input region base,
     589             :            as described above.
     590             : 
     591             :            https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L99-L115 */
     592           0 :         uchar * serialization_ptr           = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
     593             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1234 */
     594           0 :         caller_account->serialized_data     = serialization_ptr + fd_ulong_sat_sub( caller_account->vm_data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
     595           0 :         caller_account->serialized_data_len = post_len;
     596          33 :       } else {
     597             :         /* Translate the data pointer directly from the VM address, if
     598             :            virtual_address_space_adjustments (or direct mapping) is not
     599             :            enabled.
     600             : 
     601             :           https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L115-L122 */
     602          66 :         caller_account->serialized_data     = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, caller_account->vm_data_vaddr, alignof(uchar), post_len );
     603           0 :         caller_account->serialized_data_len = post_len;
     604          66 :       }
     605          33 :     }
     606             : 
     607             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1240-L1241 */
     608          33 :     FD_STORE( ulong, caller_account->ref_to_len_in_vm, post_len );
     609             : 
     610             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1243-L1251 */
     611          33 :     void * caller_len = FD_VM_MEM_HADDR_ST( vm, fd_ulong_sat_sub(caller_account->vm_data_vaddr, sizeof(ulong)), alignof(ulong), sizeof(ulong) );
     612          33 :     FD_STORE( ulong, caller_len, post_len );
     613          33 :   }
     614             : 
     615             :   /* Without direct mapping, copy the updated account data from the callee's
     616             :      account back to the caller's serialized data buffer. With direct mapping,
     617             :      data was modified in-place so no copy is needed.
     618             : 
     619             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1254-L1265 */
     620         290 :   if( !(vm->virtual_address_space_adjustments && vm->direct_mapping) ) {
     621             : 
     622             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1261-L1263 */
     623         277 :     if( FD_UNLIKELY( caller_account->serialized_data_len!=post_len ) ) {
     624           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL );
     625           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     626           0 :     }
     627             : 
     628         277 :     fd_memcpy( caller_account->serialized_data, borrowed_callee_acc->acc->data, post_len );
     629         277 :   }
     630             : 
     631             : 
     632         290 :   return FD_VM_SUCCESS;
     633         290 : }
     634             : 
     635             : /* fd_vm_syscall_cpi_{rust/c} is the entrypoint for the sol_invoke_signed_{rust/c} syscalls.
     636             : 
     637             : The bulk of the high-level logic mirrors Solana's cpi_common entrypoint function at
     638             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L847-L977
     639             : The only differences should be in the order of the error checks, which does not affect consensus.
     640             : 
     641             : 100-foot flow:
     642             : - Translate the CPI ABI structures to the FD runtime's instruction format
     643             : - Update the callee accounts with any changes made by the caller prior to this CPI instruction
     644             : - Dispatch the instruction to the FD runtime (actually making the CPI call)
     645             : - Update the caller accounts with any changes made by the callee during CPI execution
     646             : 
     647             : Parameters:
     648             : - vm: pointer to the virtual machine handle
     649             : - instruction_va: vm address of the instruction to execute, which will be in the language-specific ABI format.
     650             : - acct_infos_va: vm address of the account infos, which will be in the language-specific ABI format.
     651             : - acct_info_cnt: number of account infos
     652             : - signers_seeds_va: vm address of the signers seeds
     653             : - signers_seeds_cnt: number of signers seeds
     654             : */
     655             : #define VM_SYSCALL_CPI_ENTRYPOINT FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_, VM_SYSCALL_CPI_ABI)
     656             : int
     657             : VM_SYSCALL_CPI_ENTRYPOINT( void *  _vm,
     658             :                            ulong   instruction_va,
     659             :                            ulong   acct_infos_va,
     660             :                            ulong   acct_info_cnt,
     661             :                            ulong   signers_seeds_va,
     662        2139 :                            ulong   signers_seeds_cnt ) {
     663        2139 :   long const regime0 = fd_tickcount();
     664             : 
     665        2139 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     666             : 
     667             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L859-L864 */
     668        2139 :   FD_VM_CU_UPDATE( vm, get_cpi_invoke_unit_cost() );
     669             : 
     670             :   /* Translate instruction ********************************************/
     671             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L878-L883
     672             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L575-L636
     673             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L709-L774 */
     674             : 
     675             :   /* Translating the CPI instruction
     676             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L581
     677             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L715 */
     678        2139 :   VM_SYSCALL_CPI_INSTR_T const * cpi_instruction =
     679        6415 :     FD_VM_MEM_HADDR_LD( vm, instruction_va, VM_SYSCALL_CPI_INSTR_ALIGN, VM_SYSCALL_CPI_INSTR_SIZE );
     680             : 
     681             :   /* This needs to be here for the C ABI
     682             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L717
     683             :    */
     684        3591 :   fd_pubkey_t const * program_id = (fd_pubkey_t *)VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, cpi_instruction );
     685             : 
     686             :   /* Translate CPI account metas
     687             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L582-L587
     688             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L718-L723 */
     689        2099 :   VM_SYSCALL_CPI_ACC_META_T const * cpi_account_metas =
     690        4188 :     FD_VM_MEM_SLICE_HADDR_LD( vm, VM_SYSCALL_CPI_INSTR_ACCS_ADDR( cpi_instruction ),
     691        4188 :                               VM_SYSCALL_CPI_ACC_META_ALIGN,
     692        4188 :                               fd_ulong_sat_mul( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_ACC_META_SIZE ) );
     693             : 
     694             :   /* Translate instruction data
     695             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L588-L593
     696             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L724 */
     697             : 
     698        4073 :   uchar const * data = FD_VM_MEM_SLICE_HADDR_LD(
     699        4073 :     vm, VM_SYSCALL_CPI_INSTR_DATA_ADDR( cpi_instruction ),
     700        4073 :     FD_VM_ALIGN_RUST_U8,
     701        4073 :     VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ));
     702             : 
     703             : 
     704             :   /* Instruction checks
     705             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L595
     706             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L726 */
     707             : 
     708        1987 :   int err = fd_vm_syscall_cpi_check_instruction( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) );
     709        4073 :   if( FD_UNLIKELY( err ) ) {
     710           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
     711           0 :     return err;
     712           0 :   }
     713             : 
     714             :   /* Agave consumes CU in translate_instruction
     715             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L597-L599
     716             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L728-L730 */
     717        1987 :   ulong total_cu_translation_cost = VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) / FD_VM_CPI_BYTES_PER_UNIT;
     718             : 
     719             :   /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L601-L613
     720             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L732-L745 */
     721             :   /* Agave bills the same regardless of ABI */
     722        1987 :   ulong account_meta_translation_cost =
     723        1987 :     fd_ulong_sat_mul(
     724        1987 :       VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ),
     725        1987 :       FD_VM_RUST_ACCOUNT_META_SIZE ) /
     726        1987 :     FD_VM_CPI_BYTES_PER_UNIT;
     727        1987 :   total_cu_translation_cost = fd_ulong_sat_add( total_cu_translation_cost, account_meta_translation_cost );
     728        1987 :   FD_VM_CU_UPDATE( vm, total_cu_translation_cost );
     729             : 
     730             :   /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L617-L629
     731             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L749-L767 */
     732        8430 :   for( ulong i=0UL; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ); i++ ) {
     733        6493 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_account_metas[i];
     734        6493 :     if( FD_UNLIKELY( cpi_acct_meta->is_signer > 1U || cpi_acct_meta->is_writable > 1U ) ) {
     735          11 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_ARG );
     736          11 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     737          11 :     }
     738             :     /* Rust ABI: no-op
     739             :        C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L760-L761
     740             :      */
     741        9201 :     (void)VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta );
     742        9201 :   }
     743             : 
     744             :   /* Derive PDA signers
     745             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L887-L893
     746             :      Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L665-L707
     747             :      C    ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L803-L845
     748             : 
     749             :      Note that we don't need any ABI-specific logic here, because the two ABIs are actually identical for the seeds.*/
     750        1937 :   fd_pubkey_t signers[ FD_CPI_MAX_SIGNER_CNT ] = {0};
     751        1937 :   fd_pubkey_t * caller_program_id = &vm->instr_ctx->txn_out->accounts.keys[ vm->instr_ctx->instr->program_id ];
     752        1937 :   if( FD_LIKELY( signers_seeds_cnt > 0UL ) ) {
     753         554 :     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 ) );
     754         277 :     if( FD_UNLIKELY( signers_seeds_cnt > FD_CPI_MAX_SIGNER_CNT ) ) {
     755           8 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS );
     756           8 :       return FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS;
     757           8 :     }
     758             : 
     759         488 :     for( ulong i=0UL; i<signers_seeds_cnt; i++ ) {
     760             : 
     761             :       /* This function will precompute the memory translation required and do
     762             :         some preflight checks. */
     763         303 :       void const * signer_seed_haddrs[ FD_VM_PDA_SEEDS_MAX ];
     764         303 :       ulong        signer_seed_lens  [ FD_VM_PDA_SEEDS_MAX ];
     765             : 
     766         303 :       int err = fd_vm_translate_and_check_program_address_inputs( vm,
     767         303 :                                                                   signers_seeds[i].addr,
     768         303 :                                                                   signers_seeds[i].len,
     769         303 :                                                                   0UL,
     770         303 :                                                                   signer_seed_haddrs,
     771         303 :                                                                   signer_seed_lens ,
     772         303 :                                                                   NULL,
     773         303 :                                                                   0U );
     774         303 :       if( FD_UNLIKELY( err ) ) {
     775           9 :         return err;
     776           9 :       }
     777             : 
     778         294 :       err = fd_vm_derive_pda( vm, caller_program_id, signer_seed_haddrs, signer_seed_lens, signers_seeds[i].len, NULL, &signers[i] );
     779         294 :       if( FD_UNLIKELY( err ) ) {
     780          75 :         FD_TXN_PREPARE_ERR_OVERWRITE( vm->instr_ctx->txn_out );
     781          75 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_BAD_SEEDS );
     782          75 :         return FD_VM_SYSCALL_ERR_BAD_SEEDS;
     783          75 :       }
     784         294 :     }
     785         269 :   }
     786             : 
     787             :   /* Authorized program check
     788             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L894 */
     789        1844 :   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 ) ) ) ) {
     790          57 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED );
     791          57 :     return FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED;
     792          57 :   }
     793             : 
     794             :   /* Create the instruction to execute (in the input format the FD runtime expects) from
     795             :      the translated CPI ABI inputs.
     796             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L895 */
     797        1787 :   fd_pubkey_t cpi_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ];
     798        1787 :   fd_instr_info_t * instruction_to_execute = &vm->instr_ctx->runtime->instr.trace[ vm->instr_ctx->runtime->instr.trace_length++ ];
     799             : 
     800        1787 :   err = VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( vm, cpi_instruction, cpi_account_metas, program_id, data, instruction_to_execute, cpi_instr_acct_keys );
     801        1787 :   if( FD_UNLIKELY( err ) ) {
     802           0 :     return err;
     803           0 :   }
     804             : 
     805             :   /* Prepare the instruction for execution in the runtime. This is required by the runtime
     806             :      before we can pass an instruction to the executor. */
     807        1787 :   fd_instruction_account_t instruction_accounts[ FD_TXN_INSTR_ACCT_MAX ];
     808        1787 :   ulong instruction_accounts_cnt;
     809        1787 :   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 );
     810             :   /* Errors are propagated in the function itself. */
     811        1787 :   if( FD_UNLIKELY( err ) ) {
     812         435 :     return err;
     813         435 :   }
     814             : 
     815             :   /* Translate account infos
     816             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L897-L903
     817             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L987-L1047 */
     818             : 
     819             :   /* With syscall_parameter_address_restrictions, verify that the account_infos array
     820             :      is not inside the input region. This prevents programs from passing pointers to
     821             :      the serialized account data region as account_infos, which would allow them to
     822             :      bypass pointer validation checks.
     823             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1002-L1011 */
     824        1352 :   ulong acc_info_total_sz = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
     825        1353 :   if( vm->syscall_parameter_address_restrictions ) {
     826        1353 :     if( FD_UNLIKELY( fd_ulong_sat_add( acct_infos_va, acc_info_total_sz ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     827          21 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     828          21 :       return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     829          21 :     }
     830        1353 :   }
     831             : 
     832             :   /* This is the equivalent of translate_slice in translate_account_infos */
     833        2660 :   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 );
     834             : 
     835             :   /* Right after translating, Agave checks the number of account infos */
     836        1329 :   if( FD_UNLIKELY( acct_info_cnt > get_cpi_max_account_infos() ) ) {
     837           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED );
     838           0 :     return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
     839           0 :   }
     840             : 
     841             :   /* Consume compute units proportional to the number of account infos */
     842        1329 :   ulong account_infos_bytes = fd_ulong_sat_mul( acct_info_cnt, FD_VM_ACCOUNT_INFO_BYTE_SIZE );
     843        1329 :   FD_VM_CU_UPDATE( vm, account_infos_bytes / FD_VM_CPI_BYTES_PER_UNIT );
     844             : 
     845           0 :   fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS ];
     846        4713 :   for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
     847             :     /* Translate each pubkey address specified in account_infos.
     848             :        Failed translation should lead to an access violation and
     849             :        implies that obviously bad account_info has been supplied. */
     850       10169 :       acct_info_keys[ acct_idx ] = FD_VM_MEM_HADDR_LD( vm, acc_infos[ acct_idx ].pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     851       10169 :   }
     852             : 
     853             :   /* translate_accounts_common ***************************************************************
     854             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193 */
     855        1312 :   fd_vm_cpi_translated_account_t translated_accounts[ FD_TXN_INSTR_ACCT_MAX ];
     856        1312 :   ulong translated_accounts_len = 0UL;
     857        1312 :   err = VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     858        1312 :     vm,
     859        1312 :     instruction_accounts,
     860        1312 :     instruction_accounts_cnt,
     861        1312 :     acct_infos_va,
     862        1312 :     acct_info_keys,
     863        1312 :     acc_infos,
     864        1312 :     acct_info_cnt,
     865        1312 :     translated_accounts,
     866        1312 :     &translated_accounts_len
     867        1312 :   );
     868             :   /* errors are propagated in the function itself. */
     869        1312 :   if( FD_UNLIKELY( err ) ) return err;
     870             : 
     871             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L905-L928 */
     872        1201 :   if( vm->syscall_parameter_address_restrictions ) {
     873        2300 :     for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     874        1099 :       fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     875        1099 :       fd_guarded_borrowed_account_t callee_acc = {0};
     876        1099 :       FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
     877        1099 :       uchar update_caller = 0;
     878        1099 :       err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc, &update_caller );
     879        1099 :       if( FD_UNLIKELY( err ) ) {
     880           0 :         return err;
     881           0 :       }
     882        1099 :       translated_account->update_caller_account_region =
     883        1099 :           (uchar)( translated_account->update_caller_account_info || update_caller );
     884        1099 :     }
     885        1201 :   }
     886             : 
     887             :   /* Set the transaction compute meter to be the same as the VM's compute meter,
     888             :      so that the callee cannot use compute units that the caller has already used. */
     889        1200 :   vm->instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
     890             : 
     891        1200 :   long const regime1 = fd_tickcount();
     892             : 
     893             :   /* Execute the CPI instruction in the runtime */
     894        1200 :   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 );
     895        1200 :   ulong instr_exec_res = (ulong)err_exec;
     896             : 
     897        1200 :   long const regime2 = fd_tickcount();
     898        1200 :   vm->instr_ctx->runtime->metrics.cpi_setup_cum_ticks += (ulong)( regime1-regime0 );
     899             : 
     900             :   /* Set the CU meter to the instruction context's transaction context's compute meter,
     901             :      so that the caller can't use compute units that the callee has already used. */
     902        1200 :   vm->cu = vm->instr_ctx->txn_out->details.compute_budget.compute_meter;
     903             : 
     904        1200 :   vm->reg[0] = instr_exec_res;
     905             : 
     906             :   /* Errors are propagated in fd_execute_instr. */
     907        1200 :   if( FD_UNLIKELY( err_exec ) ) return err_exec;
     908             : 
     909             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L942-L957 */
     910         918 :   for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     911         495 :     fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     912         495 :     fd_guarded_borrowed_account_t callee_acc = {0};
     913         495 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
     914         495 :     if( !translated_account->update_caller_account_info ) continue;
     915         290 :     err = VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc );
     916         290 :     if( FD_UNLIKELY( err ) ) {
     917           0 :       return err;
     918           0 :     }
     919         290 :   }
     920             : 
     921             :   /* With virtual_address_space_adjustments, update the caller's memory regions
     922             :      to reflect any changes the callee made to account data.
     923             :      https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L959-L973 */
     924         423 :   if( vm->virtual_address_space_adjustments ) {
     925          41 :     for( ulong i=0UL; i<translated_accounts_len; i++ ) {
     926          27 :       fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
     927          27 :       fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
     928          27 :       err = fd_exec_instr_ctx_try_borrow_instr_account( vm->instr_ctx, translated_account->index_in_caller, &borrowed_callee_acc );
     929          27 :       if( FD_UNLIKELY( err ) ) return err;
     930          27 :       if( !translated_account->update_caller_account_region ) continue;
     931             : 
     932          13 :       err = fd_vm_cpi_update_caller_account_region( vm, translated_account, &borrowed_callee_acc );
     933          13 :       if( FD_UNLIKELY( err ) ) {
     934           0 :         return err;
     935           0 :       }
     936          13 :     }
     937          14 :   }
     938             : 
     939         423 :   long const regime3 = fd_tickcount();
     940         423 :   vm->instr_ctx->runtime->metrics.cpi_commit_cum_ticks += (ulong)( regime3-regime2 );
     941             : 
     942         423 :   return FD_VM_SUCCESS;
     943         423 : }
     944             : 
     945             : #undef VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC
     946             : #undef VM_SYSCALL_CPI_FROM_ACC_INFO_FUNC
     947             : #undef VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC
     948             : #undef VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC
     949             : #undef VM_SYSCALL_CPI_FUNC

Generated by: LCOV version 1.14