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: 0 423 0.0 %
Date: 2025-12-16 05:08:26 Functions: 0 10 0.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/v3.0.4/syscalls/src/cpi.rs#L21-L38
      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           0 :   if( FD_UNLIKELY( vm_addr!=expected_vm_addr )) {                                                         \
      27           0 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,                                             \
      28           0 :       "Invalid account info pointer `%s': %#lx != %#lx", field_name, vm_addr, expected_vm_addr );         \
      29           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );                                   \
      30           0 :     return FD_VM_SYSCALL_ERR_INVALID_POINTER;                                                             \
      31           0 :   }
      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           0 : #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           0 :                                           fd_pubkey_t                       out_instr_acct_keys[ FD_INSTR_ACCT_MAX ] ) {
      63             : 
      64           0 :   out_instr->program_id   = UCHAR_MAX;
      65           0 :   out_instr->stack_height = vm->instr_ctx->runtime->instr.stack_sz+1;
      66           0 :   out_instr->data_sz      = (ushort)VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instr );
      67           0 :   out_instr->acct_cnt     = (ushort)VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr );
      68           0 :   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           0 :   int program_id_idx = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, program_id );
      72           0 :   if( FD_LIKELY( program_id_idx != -1 ) ) {
      73           0 :     out_instr->program_id = (uchar)program_id_idx;
      74           0 :   }
      75             : 
      76           0 :   uchar acc_idx_seen[ FD_INSTR_ACCT_MAX ] = {0};
      77             : 
      78           0 :   for( ushort i=0; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr ); i++ ) {
      79           0 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_acct_metas[i];
      80           0 :     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           0 :     out_instr->accounts[i] = fd_instruction_account_init( USHORT_MAX,
      88           0 :                                                           USHORT_MAX,
      89           0 :                                                           USHORT_MAX,
      90           0 :                                                           VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
      91           0 :                                                           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           0 :     int idx_in_txn    = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, pubkey );
      96           0 :     int idx_in_caller = fd_exec_instr_ctx_find_idx_of_instr_account( vm->instr_ctx, pubkey );
      97             : 
      98           0 :     fd_instr_info_setup_instr_account( out_instr,
      99           0 :                                        acc_idx_seen,
     100           0 :                                        idx_in_txn!=-1 ? (ushort)idx_in_txn : USHORT_MAX,
     101           0 :                                        idx_in_caller!=-1 ? (ushort)idx_in_caller : USHORT_MAX,
     102           0 :                                        i,
     103           0 :                                        VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
     104           0 :                                        VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
     105             : 
     106           0 :   }
     107             : 
     108           0 :   return FD_VM_SUCCESS;
     109           0 : }
     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/v3.0.4/syscalls/src/cpi.rs#L1067-L1132
     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             : Paramaters:
     126             : - vm: pointer to the virtual machine handle
     127             : - account_info: account info object
     128             : - callee_acc_pubkey: pubkey of the account. this is used to look up the account in the borrowed accounts cache
     129             :   (TODO: this seems redundant? we can probably remove this, as the account_info contains the pubkey)
     130             : */
     131           0 : #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           0 :                                       uchar                              instr_acc_idx ) {
     136           0 :   int err;
     137             : 
     138             :   /* Borrow the callee account.
     139             :      TODO: Agave borrows before this function call. Consider refactoring to borrow the account at the same place as Agave.
     140             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L817 */
     141           0 :   fd_guarded_borrowed_account_t callee_acc = {0};
     142           0 :   err = fd_exec_instr_ctx_try_borrow_instr_account( vm->instr_ctx, instr_acc_idx, &callee_acc );
     143           0 :   if( FD_UNLIKELY( err ) ) {
     144             :     /* No need to do anything if the account is missing from the borrowed accounts cache */
     145           0 :     return FD_VM_SUCCESS;
     146           0 :   }
     147             : 
     148             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1087-L1089 */
     149           0 :   if( fd_borrowed_account_get_lamports( &callee_acc )!=*(caller_account->lamports) ) {
     150           0 :     err = fd_borrowed_account_set_lamports( &callee_acc, *(caller_account->lamports) );
     151           0 :     if( FD_UNLIKELY( err ) ) {
     152           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     153           0 :       return -1;
     154           0 :     }
     155           0 :   }
     156             : 
     157             :   /* With stricter_abi_and_runtime_constraints enabled, we validate account
     158             :      length changes and update the associated borrowed account with any
     159             :      changed made. If direct mapping is also enabled, we skip actually copying
     160             :      the data back to the borrowed account, as it is already updated in-place.
     161             : 
     162             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1091-L1113 */
     163           0 :   if( vm->stricter_abi_and_runtime_constraints ) {
     164           0 :     ulong prev_len = fd_borrowed_account_get_data_len( &callee_acc );
     165           0 :     ulong post_len = *caller_account->ref_to_len_in_vm;
     166             : 
     167             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1094-L1109 */
     168           0 :     if( FD_UNLIKELY( prev_len!=post_len ) ) {
     169           0 :       ulong address_space_reserved_for_account;
     170           0 :       if( vm->is_deprecated ) {
     171           0 :         address_space_reserved_for_account = caller_account->orig_data_len;
     172           0 :       } else {
     173           0 :         address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
     174           0 :       }
     175             : 
     176             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1103-L1105 */
     177           0 :       if( FD_UNLIKELY( post_len>address_space_reserved_for_account ) ) {
     178           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     179           0 :         return -1;
     180           0 :       }
     181             : 
     182             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1106 */
     183           0 :       err = fd_borrowed_account_set_data_length( &callee_acc, post_len );
     184           0 :       if( FD_UNLIKELY( err ) ) {
     185           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     186           0 :         return -1;
     187           0 :       }
     188           0 :     }
     189             : 
     190             :     /* Without direct mapping, we need to copy the account data from the VM's
     191             :        serialized buffer back to the borrowed account. With direct mapping,
     192             :        data is modified in-place so no copy is needed.
     193             :        https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1110-L1112 */
     194           0 :     int err;
     195           0 :     if( !vm->direct_mapping && fd_borrowed_account_can_data_be_changed( &callee_acc, &err ) ) {
     196           0 :       err = fd_borrowed_account_set_data_from_slice( &callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
     197           0 :       if( FD_UNLIKELY( err ) ) {
     198           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     199           0 :         return -1;
     200           0 :       }
     201           0 :     }
     202           0 :   } else {
     203             :     /* Direct mapping is not enabled, so we need to copy the account data
     204             :        from the VM's serialized buffer back to the borrowed account.
     205             : 
     206             :        https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1114-L1121 */
     207           0 :     int err;
     208           0 :     if( fd_borrowed_account_can_data_be_resized( &callee_acc, caller_account->serialized_data_len, &err ) &&
     209           0 :         fd_borrowed_account_can_data_be_changed( &callee_acc, &err ) ) {
     210             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1116 */
     211           0 :       err = fd_borrowed_account_set_data_from_slice( &callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
     212           0 :       if( FD_UNLIKELY( err ) ) {
     213           0 :         FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     214           0 :         return -1;
     215           0 :       }
     216           0 :     } else if( FD_UNLIKELY( caller_account->serialized_data_len!=fd_borrowed_account_get_data_len( &callee_acc ) ||
     217           0 :                             (caller_account->serialized_data_len &&
     218           0 :                               memcmp( fd_borrowed_account_get_data( &callee_acc ), caller_account->serialized_data, caller_account->serialized_data_len )) ) ) {
     219             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1117-L1119 */
     220           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     221           0 :       return -1;
     222           0 :     }
     223           0 :   }
     224             : 
     225             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1124-L1129 */
     226           0 :   if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &callee_acc ), caller_account->owner, sizeof(fd_pubkey_t) ) ) ) {
     227           0 :     err = fd_borrowed_account_set_owner( &callee_acc, caller_account->owner );
     228           0 :     if( FD_UNLIKELY( err ) ) {
     229           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, err );
     230           0 :       return -1;
     231           0 :     }
     232           0 :   }
     233             : 
     234           0 :   return FD_VM_SUCCESS;
     235           0 : }
     236             : 
     237             : /*
     238             : fd_vm_syscall_cpi_translate_and_update_accounts_ mirrors the behaviour of
     239             : solana_bpf_loader_program::syscalls::cpi::translate_and_update_accounts:
     240             : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L767-L892
     241             : 
     242             : It translates the caller accounts to the host address space, and then calls
     243             : fd_vm_syscall_cpi_update_callee_acc to update the callee borrowed account with any changes
     244             : the caller has made to the account during execution before this CPI call.
     245             : 
     246             : It also populates the out_callee_indices and out_caller_indices arrays:
     247             : - out_callee_indices: indices of the callee accounts in the transaction
     248             : - out_caller_indices: indices of the caller accounts in the account_infos array
     249             : 
     250             : Parameters:
     251             : - vm: pointer to the virtual machine handle
     252             : - instruction_accounts: array of instruction accounts
     253             : - instruction_accounts_cnt: length of the instruction_accounts array
     254             : - account_infos: array of account infos
     255             : - account_infos_length: length of the account_infos array
     256             : 
     257             : Populates the given out_callee_indices and out_caller_indices arrays:
     258             : - out_callee_indices: indices of the callee accounts in the transaction
     259             : - out_caller_indices: indices of the caller accounts in the account_infos array
     260             : - out_len: length of the out_callee_indices and out_caller_indices arrays
     261             : */
     262           0 : #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)
     263             : static int
     264             : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     265             :                               fd_vm_t *                         vm,
     266             :                               fd_instruction_account_t const *  instruction_accounts,
     267             :                               ulong const                       instruction_accounts_cnt,
     268             :                               ulong                             acct_infos_va,
     269             :                               fd_pubkey_t const * *             account_info_keys, /* same length as account_infos_length */
     270             :                               VM_SYSCALL_CPI_ACC_INFO_T const * account_infos,
     271             :                               ulong const                       account_infos_length,
     272             :                               ushort *                          out_callee_indices,
     273             :                               ushort *                          out_caller_indices,
     274             :                               fd_vm_cpi_caller_account_t *      caller_accounts,
     275           0 :                               ulong *                           out_len ) {
     276           0 :   for( ulong i=0UL; i<instruction_accounts_cnt; i++ ) {
     277           0 :     if( i!=instruction_accounts[i].index_in_callee ) {
     278             :       /* Skip duplicate accounts */
     279           0 :       continue;
     280           0 :     }
     281             : 
     282             :     /* `fd_vm_prepare_instruction()` will always set up a valid index for `index_in_caller`, so we can access the borrowed account directly.
     283             :        A borrowed account will always have non-NULL meta (if the account doesn't exist, `fd_executor_setup_accounts_for_txn()`
     284             :        will set its meta up) */
     285             : 
     286             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L817 */
     287           0 :     fd_guarded_borrowed_account_t callee_acct = {0};
     288           0 :     FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, instruction_accounts[i].index_in_caller, &callee_acct );
     289             : 
     290           0 :     fd_pubkey_t const *       account_key = callee_acct.acct->pubkey;
     291           0 :     fd_account_meta_t const * acc_meta    = fd_borrowed_account_get_acc_meta( &callee_acct );
     292             : 
     293             :     /* If the account is known and executable, we only need to consume the compute units.
     294             :        Executable accounts can't be modified, so we don't need to update the callee account. */
     295           0 :     if( fd_borrowed_account_is_executable( &callee_acct ) ) {
     296             :       // FIXME: should this be FD_VM_CU_MEM_UPDATE? Changing this changes the CU behaviour from main (because of the base cost)
     297           0 :       FD_VM_CU_UPDATE( vm, acc_meta->dlen / FD_VM_CPI_BYTES_PER_UNIT );
     298           0 :       continue;
     299           0 :     }
     300             : 
     301             :     /* FIXME: we should not need to drop the account here to avoid a double borrow.
     302             :        Instead, we should borrow the account before entering this function. */
     303           0 :     fd_borrowed_account_drop( &callee_acct );
     304             : 
     305             :     /* Find the indicies of the account in the caller and callee instructions */
     306           0 :     uint found = 0;
     307           0 :     for( ushort j=0; j<account_infos_length && !found; j++ ) {
     308           0 :       fd_pubkey_t const * acct_addr = account_info_keys[ j ];
     309             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L832
     310             :        */
     311           0 :       if( memcmp( account_key->uc, acct_addr->uc, sizeof(fd_pubkey_t) ) != 0 ) {
     312           0 :         continue;
     313           0 :       }
     314             : 
     315             :       /* The next iteration will overwrite this if it turns out that we
     316             :          do not need to preserve this for update_caller().
     317             :        */
     318           0 :       fd_vm_cpi_caller_account_t * caller_account = caller_accounts + *out_len;
     319             :       /* Record the indicies of this account */
     320           0 :       ushort index_in_caller = instruction_accounts[i].index_in_caller;
     321           0 :       if( vm->stricter_abi_and_runtime_constraints || instruction_accounts[i].is_writable ) {
     322           0 :         out_callee_indices[*out_len] = index_in_caller;
     323           0 :         out_caller_indices[*out_len] = j;
     324           0 :         (*out_len)++;
     325           0 :       }
     326           0 :       found = 1;
     327             : 
     328             :       /* Logically this check isn't ever going to fail due to how the
     329             :          account_info_keys array is set up.  We replicate the check for
     330             :          clarity and also to guard against accidental violation of the
     331             :          assumed invariant in the future.
     332             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L846-L849
     333             :        */
     334           0 :       if( FD_UNLIKELY( j >= account_infos_length ) ) {
     335           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
     336           0 :         return FD_VM_SYSCALL_ERR_INVALID_LENGTH;
     337           0 :       }
     338             : 
     339             :       /* The following implements the checks in from_account_info which
     340             :          is invoked as do_translate() in translate_and_update_accounts()
     341             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L850-L861
     342             :        */
     343             :       ////// BEGIN from_account_info
     344             : 
     345           0 :       fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[index_in_caller];
     346             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L138 */
     347           0 :       if( FD_LIKELY( vm->stricter_abi_and_runtime_constraints ) ) {
     348             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L139-L144 */
     349           0 :         ulong expected_pubkey_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
     350           0 :           vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
     351           0 :           acc_region_meta->expected_pubkey_offset;
     352             :         /* Max msg_sz: 40 + 18 + 18 = 76 < 127 */
     353           0 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].pubkey_addr, expected_pubkey_vaddr, "key");
     354             : 
     355             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L145-L150 */
     356           0 :         ulong expected_owner_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
     357           0 :           vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
     358           0 :           acc_region_meta->expected_owner_offset;
     359             :         /* Max msg_sz: 42 + 18 + 18 = 78 < 127 */
     360           0 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].owner_addr, expected_owner_vaddr, "owner");
     361           0 :       }
     362             : 
     363             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L155-L175 */
     364           0 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, (account_infos + j), lamports_vaddr );
     365             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L162-L173  */
     366           0 :       if( FD_LIKELY( vm->stricter_abi_and_runtime_constraints ) ) {
     367             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L163-L165
     368             :            Check that the account's lamports Rc<RefCell<&mut u64>> is not
     369             :            stored in the account region. Because a refcell is only present if
     370             :            the Rust SDK is used, we only need to check this for the Rust ABI. */
     371             :         #ifdef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
     372           0 :         VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, (account_infos + j), lamports_rc_vaddr )
     373           0 :         if ( FD_UNLIKELY( lamports_rc_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     374           0 :           FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     375           0 :           return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     376           0 :         }
     377           0 :         #endif
     378             : 
     379             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L167-L172 */
     380           0 :         ulong expected_lamports_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
     381           0 :           vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
     382           0 :           acc_region_meta->expected_lamports_offset;
     383             :         /* Max msg_sz: 45 + 18 + 18 = 81 < 127 */
     384           0 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, lamports_vaddr, expected_lamports_vaddr, "lamports");
     385           0 :       }
     386             : 
     387             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L153-L175
     388             :        */
     389           0 :       VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, (account_infos + j), lamports_haddr );
     390           0 :       caller_account->lamports = lamports_haddr;
     391             : 
     392             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L177-L181
     393             :        */
     394           0 :       caller_account->owner = FD_VM_MEM_HADDR_ST( vm, (account_infos + j)->owner_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     395             : 
     396             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L190-L203
     397             :        */
     398           0 :       VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, (account_infos + j), data_vaddr );
     399             : 
     400             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L196-L203 */
     401           0 :       if( vm->stricter_abi_and_runtime_constraints ) {
     402           0 :         fd_vm_input_region_t * region = &vm->input_mem_regions[ acc_region_meta->region_idx ];
     403           0 :         ulong expected_data_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
     404           0 :           region->vaddr_offset + region->address_space_reserved;
     405           0 :         VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, data_vaddr, expected_data_vaddr, "data");
     406           0 :       }
     407             : 
     408             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L205-L210
     409             :        */
     410           0 :       VM_SYSCALL_CPI_SET_ACC_INFO_DATA_GET_LEN( vm, (account_infos + j), data_vaddr );
     411           0 :       FD_VM_CU_UPDATE( vm, data_vaddr_len / FD_VM_CPI_BYTES_PER_UNIT );
     412             : 
     413             :       #ifdef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
     414             :       /* Rust ABI
     415             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L212-L221 */
     416           0 :       VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, (account_infos + j), data_len_vaddr );
     417           0 :       if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && data_len_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     418           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     419           0 :         return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     420           0 :       }
     421           0 :       (void)acct_infos_va;
     422             :       #else
     423             :       /* C ABI
     424             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L310-L316 */
     425           0 :       ulong data_len_vaddr = vm_syscall_cpi_data_len_vaddr_c(
     426           0 :         fd_ulong_sat_add( acct_infos_va, fd_ulong_sat_mul( j, VM_SYSCALL_CPI_ACC_INFO_SIZE ) ),
     427             :         (ulong)&((account_infos + j)->data_sz),
     428             :         (ulong)(account_infos + j)
     429             :       );
     430             :       #endif
     431             : 
     432             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L226
     433             :          C ABI:    https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L324 */
     434           0 :       caller_account->vm_data_vaddr = data_vaddr;
     435             : 
     436             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L224-L230
     437             :          C ABI:    https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L302-L308
     438             : 
     439             :          Both ABIs call CallerAccount::get_serialized_data:
     440             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L90-L123
     441             : 
     442             :          With both stricter_abi_and_runtime_constraints and direct_mapping,
     443             :          account data is modified in-place so we don't track the
     444             :          serialized_data pointer.
     445             : 
     446             :          With stricter_abi only (no direct_mapping), data was copied into the input
     447             :          region buffer. We don't apply the extra memory translation checks, as
     448             :          we have checked the data pointer is valid above. So instead we add
     449             :          the vaddr to the start of the input region address space - copying
     450             :          this logic from Agave.
     451             : 
     452             :          In legacy mode, we translate the data pointer directly, as it just
     453             :          maps to a location in the single input region. */
     454           0 :       if( vm->stricter_abi_and_runtime_constraints && vm->direct_mapping ) {
     455             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L97-L99 */
     456           0 :         caller_account->serialized_data     = NULL;
     457           0 :         caller_account->serialized_data_len = 0UL;
     458           0 :       } else if( vm->stricter_abi_and_runtime_constraints ) {
     459             :         /* Skip translation checks here, following the Agave logic:
     460             :            https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L99-L115 */
     461           0 :         uchar * serialization_ptr           = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
     462           0 :         caller_account->serialized_data     = serialization_ptr + fd_ulong_sat_sub( data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
     463           0 :         caller_account->serialized_data_len = data_vaddr_len;
     464           0 :       } else {
     465             :         /* https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L115-L122 */
     466           0 :         VM_SYSCALL_CPI_ACC_INFO_DATA( vm, (account_infos + j), data_haddr );
     467           0 :         (void)data_haddr_vm_addr;
     468           0 :         caller_account->serialized_data     = data_haddr;
     469           0 :         caller_account->serialized_data_len = data_haddr_len;
     470           0 :       }
     471             : 
     472             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L237
     473             :          C ABI:    https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L322 */
     474           0 :       caller_account->orig_data_len = acc_region_meta->original_data_len;
     475             : 
     476             :       /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L222
     477             :          C ABI:    https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L317 */
     478           0 :       ulong * data_len = FD_VM_MEM_HADDR_ST( vm, data_len_vaddr, 1UL, sizeof(ulong) );
     479           0 :       caller_account->ref_to_len_in_vm = data_len;
     480             : 
     481             :       ////// END from_account_info
     482             : 
     483             :       // TODO We should be able to cache the results of translation and reuse them in the update function.
     484             :       /* Update the callee account to reflect any changes the caller has made.
     485             :          This code is split out under stricter_abi_and_runtime_constraints
     486             :          https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L1092-L1106 */
     487           0 :       if( !vm->stricter_abi_and_runtime_constraints ) {
     488           0 :         int err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, caller_account, (uchar)index_in_caller );
     489           0 :         if( FD_UNLIKELY( err ) ) {
     490           0 :           return err;
     491           0 :         }
     492           0 :       }
     493           0 :     }
     494             : 
     495           0 :     if( !found ) {
     496             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L882-L887 */
     497           0 :       FD_BASE58_ENCODE_32_BYTES( account_key->uc, id_b58 );
     498           0 :       fd_log_collector_msg_many( vm->instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
     499           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_MISSING_ACC );
     500           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     501           0 :     }
     502           0 :   }
     503             : 
     504           0 :   return FD_VM_SUCCESS;
     505           0 : }
     506             : 
     507             : /* fd_vm_cpi_update_caller_acc_{rust/c} mirrors the behaviour of
     508             : solana_bpf_loader_program::syscalls::cpi::update_caller_account:
     509             : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1171-L1268
     510             : 
     511             : This method should be called after a CPI instruction execution has
     512             : returned. It updates the given caller account info with any changes the callee
     513             : has made to this account during execution, so that those changes are
     514             : reflected in the rest of the caller's execution.
     515             : 
     516             : Those changes will be in the instructions borrowed accounts cache.
     517             : 
     518             : Paramaters:
     519             : - vm: handle to the vm
     520             : - caller_acc_info: caller account info object, which should be updated
     521             : - pubkey: pubkey of the account
     522             : 
     523             : TODO: error codes
     524             : */
     525           0 : #define VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_cpi_update_caller_acc_, VM_SYSCALL_CPI_ABI)
     526             : static int
     527             : VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( fd_vm_t *                          vm,
     528             :                                        VM_SYSCALL_CPI_ACC_INFO_T const *  caller_acc_info FD_FN_UNUSED,
     529             :                                        fd_vm_cpi_caller_account_t *       caller_account,
     530             :                                        uchar                              instr_acc_idx FD_FN_UNUSED,
     531           0 :                                        fd_pubkey_t const *                pubkey ) {
     532           0 :   int err;
     533             : 
     534             :   /* Look up the borrowed account from the instruction context, which will contain
     535             :      the callee's changes.
     536             :      TODO: Agave borrows before entering this function. We should consider doing the same.
     537             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1033-L1034 */
     538           0 :   fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
     539           0 :   err = fd_exec_instr_ctx_try_borrow_instr_account_with_key( vm->instr_ctx, pubkey, &borrowed_callee_acc );
     540           0 :   if( FD_UNLIKELY( err && ( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) ) {
     541           0 :     return 1;
     542           0 :   }
     543             : 
     544           0 :   fd_txn_account_t * callee_acc = borrowed_callee_acc.acct;
     545             :   /* Update the caller account lamports with the value from the callee
     546             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1191 */
     547           0 :   *(caller_account->lamports) = fd_txn_account_get_lamports( callee_acc );
     548             : 
     549             :   /* Update the caller account owner with the value from the callee
     550             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1192 */
     551           0 :   fd_pubkey_t const * updated_owner = fd_txn_account_get_owner( callee_acc );
     552           0 :   if( updated_owner ) *caller_account->owner = *updated_owner;
     553           0 :   else                fd_memset( caller_account->owner, 0,             sizeof(fd_pubkey_t) );
     554             : 
     555             :   /* Update the caller account data with the value from the callee
     556             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1194-L1195 */
     557           0 :   ulong prev_len = *caller_account->ref_to_len_in_vm;
     558           0 :   ulong post_len = fd_txn_account_get_data_len( callee_acc );
     559             : 
     560             :   /* Calculate the address space reserved for the account. With stricter_abi_and_runtime_constraints
     561             :      and deprecated loader, the reserved space equals original length (no realloc space).
     562             :      Otherwise, we add MAX_PERMITTED_DATA_INCREASE for reallocation.
     563             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1197-L1204 */
     564           0 :   ulong address_space_reserved_for_account;
     565           0 :   if( vm->stricter_abi_and_runtime_constraints && vm->is_deprecated ) {
     566           0 :     address_space_reserved_for_account = caller_account->orig_data_len;
     567           0 :   } else {
     568           0 :     address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
     569           0 :   }
     570             : 
     571             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1206-L1216 */
     572           0 :   if( post_len > address_space_reserved_for_account &&
     573           0 :     ( vm->stricter_abi_and_runtime_constraints || prev_len != post_len ) ) {
     574           0 :     ulong max_increase = fd_ulong_sat_sub( address_space_reserved_for_account, caller_account->orig_data_len );
     575           0 :     fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, "Account data size realloc limited to %lu in inner instructions", max_increase );
     576           0 :     FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
     577           0 :     return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
     578           0 :   }
     579             : 
     580             :   /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1218-L1252 */
     581           0 :   if( prev_len != post_len ) {
     582             : 
     583             :     /* Without direct mapping, we need to adjust the serialized data buffer
     584             :        when the length changes.
     585             : 
     586             :        With direct mapping, data is mapped in-place so no buffer manipulation
     587             :        is needed.
     588             : 
     589             :        https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1219-L1239 */
     590           0 :     if( !( vm->stricter_abi_and_runtime_constraints && vm->direct_mapping ) ) {
     591             : 
     592             :       /* If the account has shrunk, zero out memory that was previously used
     593             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1222-L1230 */
     594           0 :       if( post_len < prev_len ) {
     595             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1227-L1228 */
     596           0 :         if( caller_account->serialized_data_len < post_len ) {
     597           0 :           FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL );
     598           0 :           return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
     599           0 :         }
     600             : 
     601             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1225-L1229 */
     602           0 :         fd_memset( caller_account->serialized_data + post_len, 0, caller_account->serialized_data_len - post_len );
     603           0 :       }
     604             : 
     605             :       /* Set caller_account.serialized_data to post_len.
     606             :          https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1231-L1238 */
     607           0 :       if( vm->stricter_abi_and_runtime_constraints ) {
     608             :         /* Calculate the serialized data pointer from the input region base,
     609             :            as described above.
     610             : 
     611             :            https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L99-L115 */
     612           0 :         uchar * serialization_ptr           = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
     613             :         /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1234 */
     614           0 :         caller_account->serialized_data     = serialization_ptr + fd_ulong_sat_sub( caller_account->vm_data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
     615           0 :         caller_account->serialized_data_len = post_len;
     616           0 :       } else {
     617             :         /* Translate the data pointer directly from the VM address, if
     618             :            stricter_abi_and_runtime_constraints (or direct mapping) is not
     619             :            enabled.
     620             : 
     621             :           https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L115-L122 */
     622           0 :         caller_account->serialized_data     = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, caller_account->vm_data_vaddr, alignof(uchar), post_len );
     623           0 :         caller_account->serialized_data_len = post_len;
     624           0 :       }
     625           0 :     }
     626             : 
     627             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1240-L1241 */
     628           0 :     *caller_account->ref_to_len_in_vm = post_len;
     629             : 
     630             :     /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1243-L1251 */
     631           0 :     ulong * caller_len = FD_VM_MEM_HADDR_ST( vm, fd_ulong_sat_sub(caller_account->vm_data_vaddr, sizeof(ulong)), alignof(ulong), sizeof(ulong) );
     632           0 :     *caller_len = post_len;
     633           0 :   }
     634             : 
     635             :   /* Without direct mapping, copy the updated account data from the callee's
     636             :      account back to the caller's serialized data buffer. With direct mapping,
     637             :      data was modified in-place so no copy is needed.
     638             : 
     639             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1254-L1265 */
     640           0 :   if( !(vm->stricter_abi_and_runtime_constraints && vm->direct_mapping) ) {
     641           0 :     fd_memcpy( caller_account->serialized_data, fd_txn_account_get_data( callee_acc ), post_len );
     642           0 :   }
     643             : 
     644             : 
     645           0 :   return FD_VM_SUCCESS;
     646           0 : }
     647             : 
     648             : /* fd_vm_syscall_cpi_{rust/c} is the entrypoint for the sol_invoke_signed_{rust/c} syscalls.
     649             : 
     650             : The bulk of the high-level logic mirrors Solana's cpi_common entrypoint function at
     651             : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L964-L1065
     652             : The only differences should be in the order of the error checks, which does not affect consensus.
     653             : 
     654             : 100-foot flow:
     655             : - Translate the CPI ABI structures to the FD runtime's instruction format
     656             : - Update the callee accounts with any changes made by the caller prior to this CPI instruction
     657             : - Dispatch the instruction to the FD runtime (actually making the CPI call)
     658             : - Update the caller accounts with any changes made by the callee during CPI execution
     659             : 
     660             : Paramaters:
     661             : - vm: pointer to the virtual machine handle
     662             : - instruction_va: vm address of the instruction to execute, which will be in the language-specific ABI format.
     663             : - acct_infos_va: vm address of the account infos, which will be in the language-specific ABI format.
     664             : - acct_info_cnt: number of account infos
     665             : - signers_seeds_va: vm address of the signers seeds
     666             : - signers_seeds_cnt: number of signers seeds
     667             : - _ret: pointer to the return value
     668             : */
     669             : #define VM_SYSCALL_CPI_ENTRYPOINT FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_, VM_SYSCALL_CPI_ABI)
     670             : int
     671             : VM_SYSCALL_CPI_ENTRYPOINT( void *  _vm,
     672             :                            ulong   instruction_va,
     673             :                            ulong   acct_infos_va,
     674             :                            ulong   acct_info_cnt,
     675             :                            ulong   signers_seeds_va,
     676             :                            ulong   signers_seeds_cnt,
     677           0 :                            ulong * _ret ) {
     678             : 
     679           0 :   fd_vm_t * vm = (fd_vm_t *)_vm;
     680             : 
     681           0 :   FD_VM_CU_UPDATE( vm, FD_VM_INVOKE_UNITS );
     682             : 
     683             :   /* Translate instruction ********************************************/
     684             :   /* translate_instruction is the first thing that agave does
     685             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L1089 */
     686             : 
     687             :   /* Translating the CPI instruction
     688             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L420-L424 */
     689           0 :   VM_SYSCALL_CPI_INSTR_T const * cpi_instruction =
     690           0 :     FD_VM_MEM_HADDR_LD( vm, instruction_va, VM_SYSCALL_CPI_INSTR_ALIGN, VM_SYSCALL_CPI_INSTR_SIZE );
     691             : 
     692             :   /* This needs to be here for the C ABI
     693             :      https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L655
     694             :    */
     695           0 :   fd_pubkey_t const * program_id = (fd_pubkey_t *)VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, cpi_instruction );
     696             : 
     697             :   /* Translate CPI account metas *************************************************/
     698           0 :   VM_SYSCALL_CPI_ACC_META_T const * cpi_account_metas =
     699           0 :     FD_VM_MEM_SLICE_HADDR_LD( vm, VM_SYSCALL_CPI_INSTR_ACCS_ADDR( cpi_instruction ),
     700           0 :                               VM_SYSCALL_CPI_ACC_META_ALIGN,
     701           0 :                               fd_ulong_sat_mul( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_ACC_META_SIZE ) );
     702             : 
     703             :   /* Translate instruction data *************************************************/
     704             : 
     705           0 :   uchar const * data = FD_VM_MEM_SLICE_HADDR_LD(
     706           0 :     vm, VM_SYSCALL_CPI_INSTR_DATA_ADDR( cpi_instruction ),
     707           0 :     FD_VM_ALIGN_RUST_U8,
     708           0 :     VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ));
     709             : 
     710             : 
     711             :   /* Instruction checks ***********************************************/
     712             : 
     713           0 :   int err = fd_vm_syscall_cpi_check_instruction( vm, VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) );
     714           0 :   if( FD_UNLIKELY( err ) ) {
     715           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
     716           0 :     return err;
     717           0 :   }
     718             : 
     719             :   /* Agave consumes CU in translate_instruction
     720             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L445 */
     721           0 :   if( FD_FEATURE_ACTIVE_BANK( vm->instr_ctx->bank, loosen_cpi_size_restriction ) ) {
     722           0 :     FD_VM_CU_UPDATE( vm, VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) / FD_VM_CPI_BYTES_PER_UNIT );
     723           0 :   }
     724             : 
     725             :   /* Final checks for translate_instruction
     726             :    */
     727           0 :   for( ulong i=0UL; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ); i++ ) {
     728           0 :     VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_account_metas[i];
     729           0 :     if( FD_UNLIKELY( cpi_acct_meta->is_signer > 1U || cpi_acct_meta->is_writable > 1U ) ) {
     730             :       /* https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L471
     731             :          https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L698
     732             :        */
     733           0 :       FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_ARG );
     734           0 :       return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
     735           0 :     }
     736             :     /* https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L700
     737             :      */
     738           0 :     (void)VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta );
     739           0 :   }
     740             : 
     741             :   /* Derive PDA signers ************************************************/
     742           0 :   fd_pubkey_t signers[ FD_CPI_MAX_SIGNER_CNT ] = {0};
     743           0 :   fd_pubkey_t * caller_program_id = &vm->instr_ctx->txn_out->accounts.account_keys[ vm->instr_ctx->instr->program_id ];
     744             :   /* This is the equivalent of translate_slice in translate_signers:
     745             :      https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/programs/bpf_loader/src/syscalls/cpi.rs#L595 */
     746           0 :   if( FD_LIKELY( signers_seeds_cnt > 0UL ) ) {
     747           0 :     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 ) );
     748             :     /* Right after translating, Agave checks against MAX_SIGNERS:
     749             :        https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/programs/bpf_loader/src/syscalls/cpi.rs#L602 */
     750           0 :     if( FD_UNLIKELY( signers_seeds_cnt > FD_CPI_MAX_SIGNER_CNT ) ) {
     751           0 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS );
     752           0 :       return FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS;
     753           0 :     }
     754             : 
     755           0 :     for( ulong i=0UL; i<signers_seeds_cnt; i++ ) {
     756             : 
     757             :       /* This function will precompute the memory translation required and do
     758             :         some preflight checks. */
     759           0 :       void const * signer_seed_haddrs[ FD_VM_PDA_SEEDS_MAX ];
     760           0 :       ulong        signer_seed_lens  [ FD_VM_PDA_SEEDS_MAX ];
     761             : 
     762           0 :       int err = fd_vm_translate_and_check_program_address_inputs( vm,
     763           0 :                                                                   signers_seeds[i].addr,
     764           0 :                                                                   signers_seeds[i].len,
     765           0 :                                                                   0UL,
     766           0 :                                                                   signer_seed_haddrs,
     767           0 :                                                                   signer_seed_lens ,
     768           0 :                                                                   NULL,
     769           0 :                                                                   0U );
     770           0 :       if( FD_UNLIKELY( err ) ) {
     771           0 :         return err;
     772           0 :       }
     773             : 
     774           0 :       err = fd_vm_derive_pda( vm, caller_program_id, signer_seed_haddrs, signer_seed_lens, signers_seeds[i].len, NULL, &signers[i] );
     775           0 :       if( FD_UNLIKELY( err ) ) {
     776           0 :         FD_TXN_PREPARE_ERR_OVERWRITE( vm->instr_ctx->txn_out );
     777           0 :         FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_BAD_SEEDS );
     778           0 :         return FD_VM_SYSCALL_ERR_BAD_SEEDS;
     779           0 :       }
     780           0 :     }
     781           0 :   }
     782             : 
     783             :   /* Create the instruction to execute (in the input format the FD runtime expects) from
     784             :      the translated CPI ABI inputs. */
     785           0 :   fd_pubkey_t cpi_instr_acct_keys[ FD_INSTR_ACCT_MAX ];
     786           0 :   fd_instr_info_t * instruction_to_execute = &vm->instr_ctx->runtime->instr.trace[ vm->instr_ctx->runtime->instr.trace_length++ ];
     787             : 
     788           0 :   err = VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( vm, cpi_instruction, cpi_account_metas, program_id, data, instruction_to_execute, cpi_instr_acct_keys );
     789           0 :   if( FD_UNLIKELY( err ) ) {
     790           0 :     return err;
     791           0 :   }
     792             : 
     793             :   /* Authorized program check *************************************************/
     794             : 
     795           0 :   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 ) ) ) ) {
     796             :     /* https://github.com/solana-labs/solana/blob/2afde1b028ed4593da5b6c735729d8994c4bfac6/programs/bpf_loader/src/syscalls/cpi.rs#L1054 */
     797           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED );
     798           0 :     return FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED;
     799           0 :   }
     800             : 
     801             :   /* Prepare the instruction for execution in the runtime. This is required by the runtime
     802             :      before we can pass an instruction to the executor. */
     803           0 :   fd_instruction_account_t instruction_accounts[256];
     804           0 :   ulong instruction_accounts_cnt;
     805           0 :   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 );
     806             :   /* Errors are propagated in the function itself. */
     807           0 :   if( FD_UNLIKELY( err ) ) {
     808           0 :     return err;
     809           0 :   }
     810             : 
     811             :   /* Translate account infos ******************************************/
     812             : 
     813             :   /* With stricter_abi_and_runtime_constraints, verify that the account_infos array
     814             :      is not inside the input region. This prevents programs from passing pointers to
     815             :      the serialized account data region as account_infos, which would allow them to
     816             :      bypass pointer validation checks.
     817             :      https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L735-L744 */
     818           0 :   ulong acc_info_total_sz = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
     819           0 :   if( vm->stricter_abi_and_runtime_constraints ) {
     820           0 :     if( FD_UNLIKELY( fd_ulong_sat_add( acct_infos_va, acc_info_total_sz ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
     821           0 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
     822           0 :       return FD_VM_SYSCALL_ERR_INVALID_POINTER;
     823           0 :     }
     824           0 :   }
     825             : 
     826             :   /* This is the equivalent of translate_slice in translate_account_infos:
     827             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L816 */
     828           0 :   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 );
     829             : 
     830             :   /* Right after translating, Agave checks the number of account infos:
     831             :      https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L752 */
     832           0 :   if( FD_UNLIKELY( acct_info_cnt > get_cpi_max_account_infos( vm->instr_ctx->bank ) ) ) {
     833           0 :     FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED );
     834           0 :     return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
     835           0 :   }
     836             : 
     837           0 :   fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS ];
     838           0 :   for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
     839             :     /* Translate each pubkey address specified in account_infos.
     840             :        Failed translation should lead to an access violation and
     841             :        implies that obviously bad account_info has been supplied.
     842             :        https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L833-L841 */
     843           0 :       acct_info_keys[ acct_idx ] = FD_VM_MEM_HADDR_LD( vm, acc_infos[ acct_idx ].pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) );
     844           0 :   }
     845             : 
     846             :   /* translate_and_update_accounts ************************************************************
     847             :      Update the callee accounts with any changes made by the caller prior to this CPI execution
     848             : 
     849             :      https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L767-L892 */
     850           0 :   fd_vm_cpi_caller_account_t caller_accounts[ 256 ];
     851           0 :   ushort callee_account_keys[256];
     852           0 :   ushort caller_accounts_to_update[256];
     853           0 :   ulong caller_accounts_to_update_len = 0;
     854           0 :   err = VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
     855           0 :     vm,
     856           0 :     instruction_accounts,
     857           0 :     instruction_accounts_cnt,
     858           0 :     acct_infos_va,
     859           0 :     acct_info_keys,
     860           0 :     acc_infos,
     861           0 :     acct_info_cnt,
     862           0 :     callee_account_keys,
     863           0 :     caller_accounts_to_update,
     864           0 :     caller_accounts,
     865           0 :     &caller_accounts_to_update_len
     866           0 :   );
     867             :   /* errors are propagated in the function itself. */
     868           0 :   if( FD_UNLIKELY( err ) ) return err;
     869             : 
     870             :   /* Before stricter_abi_and_runtime_constraints, this happens in
     871             :      VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC.
     872             :      https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L856-L876 */
     873           0 :   if( vm->stricter_abi_and_runtime_constraints ) {
     874           0 :     for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
     875             :       /* Update the callee account to reflect any changes the caller has made
     876             :          https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L866-L872 */
     877           0 :       err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, caller_accounts + i, (uchar)callee_account_keys[i] );
     878           0 :       if( FD_UNLIKELY( err ) ) {
     879           0 :         return err;
     880           0 :       }
     881           0 :     }
     882           0 :   }
     883             : 
     884             :   /* Set the transaction compute meter to be the same as the VM's compute meter,
     885             :      so that the callee cannot use compute units that the caller has already used. */
     886           0 :   vm->instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
     887             : 
     888             :   /* Execute the CPI instruction in the runtime */
     889           0 :   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 );
     890           0 :   ulong instr_exec_res = (ulong)err_exec;
     891             : 
     892             :   /* Set the CU meter to the instruction context's transaction context's compute meter,
     893             :      so that the caller can't use compute units that the callee has already used. */
     894           0 :   vm->cu = vm->instr_ctx->txn_out->details.compute_budget.compute_meter;
     895             : 
     896           0 :   *_ret = instr_exec_res;
     897             : 
     898             :   /* Errors are propagated in fd_execute_instr. */
     899           0 :   if( FD_UNLIKELY( err_exec ) ) return err_exec;
     900             : 
     901             :   /* Update the caller accounts with any changes made by the callee during CPI execution */
     902           0 :   for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
     903             :     /* https://github.com/firedancer-io/solana/blob/508f325e19c0fd8e16683ea047d7c1a85f127e74/programs/bpf_loader/src/syscalls/cpi.rs#L939-L943 */
     904             :     /* We only want to update the writable accounts, because the non-writable
     905             :        caller accounts can't be changed during a CPI execution. */
     906           0 :     if( fd_instr_acc_is_writable_idx( vm->instr_ctx->instr, callee_account_keys[i] ) ) {
     907           0 :       ushort              idx_in_txn = vm->instr_ctx->instr->accounts[ callee_account_keys[i] ].index_in_transaction;
     908           0 :       fd_pubkey_t const * callee     = &vm->instr_ctx->txn_out->accounts.account_keys[ idx_in_txn ];
     909           0 :       err = VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC(vm, &acc_infos[ caller_accounts_to_update[i] ], caller_accounts + i, (uchar)callee_account_keys[i], callee);
     910           0 :       if( FD_UNLIKELY( err ) ) {
     911           0 :         return err;
     912           0 :       }
     913           0 :     }
     914           0 :   }
     915             : 
     916             :   /* With stricter_abi_and_runtime_constraints, update the caller's memory regions
     917             :      to reflect any changes the callee made to account data. This ensures the caller's
     918             :      view of account regions (tracked in acc_region_metas) remains consistent.
     919             :      https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1047-L1061 */
     920           0 :   if( vm->stricter_abi_and_runtime_constraints ) {
     921           0 :     for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
     922             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1033-L1034 */
     923           0 :       fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
     924           0 :       ushort idx_in_txn          = vm->instr_ctx->instr->accounts[ callee_account_keys[i] ].index_in_transaction;
     925           0 :       fd_pubkey_t const * callee = &vm->instr_ctx->txn_out->accounts.account_keys[ idx_in_txn ];
     926           0 :       err = fd_exec_instr_ctx_try_borrow_instr_account_with_key( vm->instr_ctx, callee, &borrowed_callee_acc );
     927           0 :       if( FD_UNLIKELY( err && ( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) ) {
     928           0 :         return 1;
     929           0 :       }
     930             : 
     931             :       /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1052-L1058 */
     932           0 :       err = fd_vm_cpi_update_caller_account_region( vm, (ulong)callee_account_keys[i], caller_accounts + i, &borrowed_callee_acc );
     933           0 :       if( FD_UNLIKELY( err ) ) {
     934           0 :         return err;
     935           0 :       }
     936           0 :     }
     937           0 :   }
     938             : 
     939           0 :   return FD_VM_SUCCESS;
     940           0 : }
     941             : 
     942             : #undef VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC
     943             : #undef VM_SYSCALL_CPI_FROM_ACC_INFO_FUNC
     944             : #undef VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC
     945             : #undef VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC
     946             : #undef VM_SYSCALL_CPI_FUNC

Generated by: LCOV version 1.14