LCOV - code coverage report
Current view: top level - flamenco/vm/syscall - fd_vm_syscall_cpi.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 166 197 84.3 %
Date: 2026-12-04 04:30:24 Functions: 11 11 100.0 %

          Line data    Source code
       1             : #include "fd_vm_syscall.h"
       2             : #include "../../runtime/fd_borrowed_account.h"
       3             : #include "../../runtime/fd_system_ids.h"
       4             : #include "../../runtime/fd_executor.h"
       5             : 
       6             : /* FIXME: ALGO EFFICIENCY */
       7             : static inline int
       8             : fd_vm_syscall_cpi_is_signer( fd_pubkey_t const * account,
       9             :            fd_pubkey_t const * signers,
      10          90 :            ulong               signers_cnt ) {
      11         108 :   for( ulong i=0UL; i<signers_cnt; i++ ) if( !memcmp( account->uc, signers[i].uc, sizeof(fd_pubkey_t) ) ) return 1;
      12          18 :   return 0;
      13          90 : }
      14             : 
      15             : /*
      16             : fd_vm_prepare_instruction populates instruction_accounts and instruction_accounts_cnt
      17             : with the instruction accounts ready for execution.
      18             : 
      19             : The majority of this logic is taken from
      20             : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/invoke_context.rs#L311-L455,
      21             : and is not vm-specific, but a part of the runtime.
      22             : TODO: should we move this out of the CPI section?
      23             : 
      24             : The bulk of the logic is concerned with unifying the privileges for each duplicated account,
      25             : ensuring that each duplicate account referenced has the same privileges. It also performs some
      26             : privilege checks, for example ensuring the necessary signatures are present.
      27             : 
      28             : TODO: instruction calling convention: const parameters after non-const.
      29             : 
      30             : Assumptions:
      31             : - callee_instr and instr_ctx are valid.
      32             : - callee_instr->acct_cnt is at most FD_TXN_INSTR_ACCT_MAX.
      33             : - instr_acct_keys has at least callee_instr->acct_cnt entries.
      34             : - Account index_in_transaction values are USHORT_MAX for missing accounts
      35             :   or valid transaction indices below MAX_TX_ACCOUNT_LOCKS.
      36             : - Account index_in_caller values are USHORT_MAX for missing accounts
      37             :   or valid indices into the caller instruction.
      38             : - callee_instr->program_id is UCHAR_MAX if the program is not in the transaction.
      39             : - instruction_accounts has room for FD_TXN_INSTR_ACCT_MAX entries.
      40             : 
      41             : Parameters:
      42             : - callee_instr
      43             : - instr_ctx
      44             : - instruction_accounts
      45             : - instruction_accounts_cnt
      46             : - signers
      47             : - signers_cnt
      48             : 
      49             : Returns:
      50             : - instruction_accounts
      51             : - instruction_accounts_cnt
      52             : Populated with the instruction accounts with normalized permissions.
      53             : 
      54             : TODO: is it possible to pass the transaction indexes of the accounts in?
      55             : This would allow us to make some of these algorithms more efficient.
      56             : */
      57             : int
      58             : fd_vm_prepare_instruction( fd_instr_info_t *        callee_instr,
      59             :                            fd_exec_instr_ctx_t *    instr_ctx,
      60             :                            fd_pubkey_t const *      callee_program_id_pubkey,
      61             :                            fd_pubkey_t const        instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ],
      62             :                            fd_instruction_account_t instruction_accounts[ FD_TXN_INSTR_ACCT_MAX ],
      63             :                            ulong *                  instruction_accounts_cnt,
      64             :                            fd_pubkey_t const *      signers,
      65        1158 :                            ulong                    signers_cnt ) {
      66             : 
      67             :   /* De-duplicate the instruction accounts, using the same logic as Solana */
      68        1158 :   ulong deduplicated_instruction_accounts_cnt = 0;
      69        1158 :   fd_instruction_account_t deduplicated_instruction_accounts[ FD_TXN_INSTR_ACCT_MAX ] = {0};
      70        1158 :   ulong duplicate_indicies_cnt                                                                     = 0;
      71        1158 :   ulong duplicate_indices[ FD_TXN_INSTR_ACCT_MAX ]                                    = {0};
      72        1158 :   uchar dedup_idx[ MAX_TX_ACCOUNT_LOCKS ]; /* index_in_transaction -> deduplicated index, UCHAR_MAX if none */
      73        1158 :   memset( dedup_idx, 0xff, sizeof(dedup_idx) );
      74             : 
      75             :   /* This function is either called by a true CPI or by a native cpi invocation.
      76             :      The native CPI invocation is never called with more than 3 instruction
      77             :      accounts, and the true CPI is never called with more than
      78             :      FD_TXN_INSTR_ACCT_MAX. */
      79        1158 :   if( FD_UNLIKELY( callee_instr->acct_cnt > FD_TXN_INSTR_ACCT_MAX ) ) {
      80           0 :     FD_LOG_CRIT(( "invariant violation: too many accounts %u", callee_instr->acct_cnt ));
      81           0 :   }
      82             : 
      83             :   /* Normalize the privileges of each instruction account in the callee, after de-duping
      84             :      the account references.
      85             :     https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/invoke_context.rs#L327-L367 */
      86        2739 :   for( ulong i=0UL; i<callee_instr->acct_cnt; i++ ) {
      87        1581 :     ushort index_in_transaction = callee_instr->accounts[i].index_in_transaction;
      88        1581 :     ushort index_in_caller      = callee_instr->accounts[i].index_in_caller;
      89             : 
      90        1581 :     if( index_in_transaction==USHORT_MAX ) {
      91             :       /* In this case the callee instruction is referencing an unknown account not listed in the
      92             :          transactions accounts. */
      93           0 :       FD_BASE58_ENCODE_32_BYTES( instr_acct_keys[i].uc, id_b58 );
      94           0 :       fd_log_collector_msg_many( instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
      95           0 :       FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
      96           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
      97           0 :     }
      98             : 
      99             :     /* If there was an instruction account before this one which referenced the same
     100             :        transaction account index, find it's index in the deduplicated_instruction_accounts
     101             :        array. */
     102        1581 :     ulong duplicate_index = dedup_idx[ index_in_transaction ]!=UCHAR_MAX ? (ulong)dedup_idx[ index_in_transaction ] : ULONG_MAX;
     103             : 
     104             :     /* If this was account referenced in a previous iteration, update the flags to include those set
     105             :        in this iteration. This ensures that after all the iterations, the de-duplicated account flags
     106             :        for each account are the union of all the flags in all the references to that account in this instruction. */
     107             : 
     108             :     /* TODO: FD_UNLIKELY? Need to check which branch is more common by running against a larger mainnet ledger */
     109             :     /* TODO: this code would maybe be easier to read if we inverted the branches */
     110        1581 :     if( duplicate_index!=ULONG_MAX ) {
     111          36 :       if ( FD_UNLIKELY( duplicate_index >= deduplicated_instruction_accounts_cnt ) ) {
     112           0 :         FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
     113           0 :         return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     114           0 :       }
     115             : 
     116          36 :       duplicate_indices[duplicate_indicies_cnt++] = duplicate_index;
     117          36 :       fd_instruction_account_t * instruction_account = &deduplicated_instruction_accounts[duplicate_index];
     118          36 :       instruction_account->is_signer   = !!(instruction_account->is_signer   | callee_instr->accounts[i].is_signer);
     119          36 :       instruction_account->is_writable = !!(instruction_account->is_writable | callee_instr->accounts[i].is_writable);
     120        1545 :     } else {
     121             :       /* In the case where the callee instruction is NOT a duplicate, we need to
     122             :          create the deduplicated_instruction_accounts fd_instruction_account_t object. */
     123             : 
     124             :       /* Add the instruction account to the duplicate indices array */
     125        1545 :       dedup_idx[ index_in_transaction ] = (uchar)deduplicated_instruction_accounts_cnt;
     126        1545 :       duplicate_indices[duplicate_indicies_cnt++] = deduplicated_instruction_accounts_cnt;
     127             : 
     128             :       /* Initialize the instruction account in the deduplicated_instruction_accounts array */
     129        1545 :       fd_instruction_account_t * instruction_account = &deduplicated_instruction_accounts[deduplicated_instruction_accounts_cnt++];
     130        1545 :       *instruction_account = fd_instruction_account_init( index_in_transaction,
     131        1545 :                                                           index_in_caller,
     132        1545 :                                                           (ushort)i,
     133        1545 :                                                           !!(callee_instr->accounts[i].is_writable),
     134        1545 :                                                           !!(callee_instr->accounts[i].is_signer) );
     135        1545 :     }
     136        1581 :   }
     137             : 
     138             :   /* The callee program must be one of the transaction accounts.  Agave
     139             :      checks this before the privilege checks below.
     140             :      https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/program-runtime/src/invoke_context.rs#L466-L472 */
     141        1158 :   ulong program_idx_in_txn = fd_runtime_find_index_of_account( instr_ctx->txn_out, callee_program_id_pubkey );
     142        1158 :   if( FD_UNLIKELY( program_idx_in_txn==ULONG_MAX ) ) {
     143           0 :     FD_BASE58_ENCODE_32_BYTES( callee_program_id_pubkey->uc, id_b58 );
     144           0 :     fd_log_collector_msg_many( instr_ctx, 2, "Unknown program ", 16UL, id_b58, id_b58_len );
     145           0 :     FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
     146           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     147           0 :   }
     148             : 
     149             :   /* Check the normalized account permissions for privilege escalation.
     150             :      https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/program-runtime/src/invoke_context.rs#L340-L377 */
     151        2664 :   for( ulong i = 0; i < deduplicated_instruction_accounts_cnt; i++ ) {
     152        1536 :     fd_instruction_account_t * instruction_account = &deduplicated_instruction_accounts[i];
     153             : 
     154             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/invoke_context.rs#L393-L403 */
     155        1536 :     ushort caller_idx                 = instruction_account->index_in_caller;
     156        1536 :     fd_pubkey_t const * caller_pubkey = NULL;
     157        1536 :     int err = fd_exec_instr_ctx_get_key_of_account_at_index( instr_ctx, caller_idx, &caller_pubkey );
     158        1536 :     if( FD_UNLIKELY( err ) ) {
     159           0 :       FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
     160           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     161           0 :     }
     162             : 
     163             :     /* Check that the account is not read-only in the caller but writable in the callee */
     164        1536 :     if( FD_UNLIKELY( instruction_account->is_writable && !fd_instr_acc_is_writable_idx( instr_ctx->instr, caller_idx ) ) ) {
     165          12 :       FD_BASE58_ENCODE_32_BYTES( caller_pubkey->uc, id_b58 );
     166          12 :       fd_log_collector_msg_many( instr_ctx, 2, id_b58, id_b58_len, "'s writable privilege escalated", 31UL );
     167          12 :       FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION, instr_ctx->txn_out->err.exec_err_idx );
     168          12 :       return FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION;
     169          12 :     }
     170             : 
     171             :     /* If the account is signed in the callee, it must be signed by the caller or the program */
     172        1524 :     if ( FD_UNLIKELY( instruction_account->is_signer && !( fd_instr_acc_is_signer_idx( instr_ctx->instr, caller_idx, NULL ) || fd_vm_syscall_cpi_is_signer( caller_pubkey, signers, signers_cnt ) ) ) ) {
     173          18 :       FD_BASE58_ENCODE_32_BYTES( caller_pubkey->uc, id_b58 );
     174          18 :       fd_log_collector_msg_many( instr_ctx, 2, id_b58, id_b58_len, "'s signer privilege escalated", 29UL );
     175          18 :       FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION, instr_ctx->txn_out->err.exec_err_idx );
     176          18 :       return FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION;
     177          18 :     }
     178        1524 :   }
     179             : 
     180             :   /* Copy the accounts with their normalized permissions over to the final instruction_accounts array,
     181             :      and set the callee_instr acct_flags. */
     182        2667 :   for (ulong i = 0; i < duplicate_indicies_cnt; i++) {
     183        1539 :     ulong duplicate_index = duplicate_indices[i];
     184             : 
     185             :     /* Failing this condition is technically impossible, but it is probably safest to keep this in
     186             :        so that we throw InstructionError::NotEnoughAccountKeys at the same point at Solana does,
     187             :        in the event any surrounding code is changed.
     188             :        https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/invoke_context.rs#L345-L366 */
     189        1539 :     if ( FD_LIKELY( duplicate_index < deduplicated_instruction_accounts_cnt ) ) {
     190        1539 :       instruction_accounts[i] = deduplicated_instruction_accounts[duplicate_index];
     191        1539 :       callee_instr->accounts[i].is_writable = !!(instruction_accounts[i].is_writable);
     192        1539 :       callee_instr->accounts[i].is_signer   = !!(instruction_accounts[i].is_signer);
     193        1539 :     } else {
     194           0 :       FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
     195           0 :       return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     196           0 :     }
     197        1539 :   }
     198             : 
     199             :   /* The callee program must also be one of the caller instruction
     200             :      accounts.
     201             :      https://github.com/anza-xyz/agave/blob/v4.5.0-beta.0/program-runtime/src/invoke_context.rs#L379-L392 */
     202        1128 :   ushort caller_map[ MAX_TX_ACCOUNT_LOCKS ];
     203        1128 :   fd_exec_instr_ctx_instr_account_map( instr_ctx, caller_map );
     204        1128 :   if( FD_UNLIKELY( caller_map[ program_idx_in_txn ]==USHORT_MAX ) ) {
     205           0 :     FD_BASE58_ENCODE_32_BYTES( callee_program_id_pubkey->uc, id_b58 );
     206           0 :     fd_log_collector_msg_many( instr_ctx, 2, "Unknown program ", 16UL, id_b58, id_b58_len );
     207           0 :     FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_MISSING_ACC, instr_ctx->txn_out->err.exec_err_idx );
     208           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
     209           0 :   }
     210             : 
     211        1128 :   *instruction_accounts_cnt = duplicate_indicies_cnt;
     212             : 
     213        1128 :   return 0;
     214        1128 : }
     215             : 
     216             : /**********************************************************************
     217             :    CROSS PROGRAM INVOCATION (Generic logic)
     218             :  **********************************************************************/
     219             : 
     220             : /* FD_CPI_MAX_SIGNER_CNT is the max amount of PDA signer addresses that
     221             :    a cross-program invocation can include in an instruction.
     222             : 
     223             :    https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/programs/bpf_loader/src/syscalls/mod.rs#L80 */
     224             : 
     225             : #define FD_CPI_MAX_SIGNER_CNT              (16UL)
     226             : 
     227             : /* "Maximum number of account info structs that can be used in a single CPI
     228             :    invocation. A limit on account info structs is effectively the same as
     229             :    limiting the number of unique accounts.
     230             : 
     231             :    https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/sdk/program/src/syscalls/mod.rs#L25
     232             :    https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L1011 */
     233             : 
     234        1047 : #define FD_CPI_MAX_ACCOUNT_INFOS (255UL)
     235             : 
     236             : /* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L168-L180 */
     237             : static inline ulong
     238        1047 : get_cpi_max_account_infos( void ) {
     239        1047 :   return FD_CPI_MAX_ACCOUNT_INFOS;
     240        1047 : }
     241             : 
     242             : /* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/execution_budget.rs#L25-L31 */
     243             : static inline ulong
     244        1113 : get_cpi_invoke_unit_cost( void ) {
     245        1113 :   return FD_VM_INVOKE_UNITS;
     246        1113 : }
     247             : 
     248             : /* fd_vm_syscall_cpi_check_instruction contains common instruction acct
     249             :    count and data sz checks. */
     250             : 
     251             : static int
     252             : fd_vm_syscall_cpi_check_instruction( ulong           acct_cnt,
     253        1113 :                                      ulong           data_sz ) {
     254             :   /* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L146-L161 */
     255        1113 :   if( FD_UNLIKELY( acct_cnt > FD_TXN_INSTR_ACCT_MAX ) ) {
     256             :     // SyscallError::MaxInstructionAccountsExceeded
     257           6 :     return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED;
     258           6 :   }
     259        1107 :   if( FD_UNLIKELY( data_sz>FD_RUNTIME_CPI_MAX_INSTR_DATA_LEN ) ) {
     260             :     // SyscallError::MaxInstructionDataLenExceeded
     261           0 :     return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_DATA_LEN_EXCEEDED;
     262           0 :   }
     263             : 
     264        1107 :   return FD_VM_SUCCESS;
     265        1107 : }
     266             : 
     267             : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1276-L1311 */
     268             : static inline int
     269             : fd_vm_cpi_update_caller_account_region( fd_vm_t *                              vm,
     270             :                                         fd_vm_cpi_translated_account_t const * translated_account,
     271         858 :                                         fd_borrowed_account_t *                borrowed_account ) {
     272         858 :   fd_vm_cpi_caller_account_t const * caller_account = &translated_account->caller_account;
     273             : 
     274             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1283-L1290 */
     275         858 :   ulong address_space_reserved_for_account;
     276         858 :   if( vm->is_deprecated ) {
     277         300 :     address_space_reserved_for_account = caller_account->orig_data_len;
     278         558 :   } else {
     279         558 :     address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
     280         558 :   }
     281             : 
     282             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1292-L1308 */
     283         858 :   if( address_space_reserved_for_account > 0UL ) {
     284             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1295-L1297 */
     285         852 :     fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[ translated_account->index_in_caller ];
     286         852 :     fd_vm_input_region_t *    region          = &vm->input_mem_regions[ acc_region_meta->region_idx + 1UL ];
     287             : 
     288             :     /* Serialization already created a data region for every account,
     289             :        so Agave's modify_memory_region_of_account and
     290             :        create_memory_region_of_account reduce to the update below. */
     291             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1301-L1307 */
     292             :     /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/serialization.rs#L23-L35 */
     293         852 :     region->region_sz = (uint)fd_borrowed_account_get_data_len( borrowed_account );
     294             : 
     295         852 :     int err;
     296         852 :     int is_writable = fd_borrowed_account_can_data_be_changed( borrowed_account, &err );
     297             : 
     298         852 :     region->is_writable = (uchar)is_writable && ( err == FD_EXECUTOR_INSTR_SUCCESS );
     299             : 
     300             :     /* CPI can change cached input bounds and write permissions. */
     301         852 :     vm->region_ld_sz[ FD_VM_INPUT_REGION ] = 0U;
     302         852 :     vm->region_st_sz[ FD_VM_INPUT_REGION ] = 0U;
     303         852 :   }
     304             : 
     305         858 :   return FD_VM_SUCCESS;
     306         858 : }
     307             : 
     308             : /**********************************************************************
     309             :   CROSS PROGRAM INVOCATION HELPERS
     310             :  **********************************************************************/
     311             : 
     312             : static inline int
     313             : fd_vm_syscall_cpi_check_id( fd_pubkey_t const * program_id,
     314        7623 :           uchar const * loader ) {
     315        7623 :   return !memcmp( program_id, loader, sizeof(fd_pubkey_t) );
     316        7623 : }
     317             : 
     318             : /* fd_vm_syscall_cpi_is_precompile returns true if the given program_id
     319             :    corresponds to a precompile. It does this by checking against a hardcoded
     320             :    list of known pre-compiles.
     321             : 
     322             :    This mirrors the behaviour in solana_sdk::precompiles::is_precompile
     323             :    https://github.com/solana-labs/solana/blob/2afde1b028ed4593da5b6c735729d8994c4bfac6/sdk/src/precompiles.rs#L93
     324             :  */
     325             : static inline int
     326        1089 : fd_vm_syscall_cpi_is_precompile( fd_pubkey_t const * program_id ) {
     327        1089 :   return fd_vm_syscall_cpi_check_id(program_id, fd_solana_keccak_secp_256k_program_id.key) |
     328        1089 :          fd_vm_syscall_cpi_check_id(program_id, fd_solana_ed25519_sig_verify_program_id.key) |
     329        1089 :          fd_vm_syscall_cpi_check_id(program_id, fd_solana_secp256r1_program_id.key);
     330        1089 : }
     331             : 
     332             : /* fd_vm_syscall_cpi_check_authorized_program corresponds to
     333             : solana_bpf_loader_program::syscalls::cpi::check_authorized_program:
     334             : https://github.com/solana-labs/solana/blob/2afde1b028ed4593da5b6c735729d8994c4bfac6/programs/bpf_loader/src/syscalls/cpi.rs#L1032
     335             : 
     336             : It determines if the given program_id is authorized to execute a CPI call.
     337             : Returns 1 if authorized, 0 otherwise.
     338             : */
     339             : static inline int
     340             : fd_vm_syscall_cpi_check_authorized_program( fd_pubkey_t const * program_id,
     341             :                                             uchar const *       instruction_data,
     342        1089 :                                             ulong               instruction_data_len ) {
     343             :   /* FIXME: do this in a branchless manner? using bitwise comparison would probably be faster */
     344        1089 :   return !( fd_vm_syscall_cpi_check_id(program_id, fd_solana_native_loader_id.key) ||
     345        1089 :            fd_vm_syscall_cpi_check_id(program_id, fd_solana_bpf_loader_program_id.key) ||
     346        1089 :            fd_vm_syscall_cpi_check_id(program_id, fd_solana_bpf_loader_deprecated_program_id.key) ||
     347        1089 :            ( fd_vm_syscall_cpi_check_id(program_id, fd_solana_bpf_loader_upgradeable_program_id.key) &&
     348        1089 :              !(( instruction_data_len != 0 && instruction_data[0] == FD_BPF_INSTR_UPGRADE ) ||
     349           0 :                ( instruction_data_len != 0 && instruction_data[0] == FD_BPF_INSTR_SET_AUTHORITY ) ||
     350           0 :                ( instruction_data_len != 0 && instruction_data[0] == FD_BPF_INSTR_SET_AUTHORITY_CHECKED ) ||
     351           0 :                ( instruction_data_len != 0 && instruction_data[0] == FD_BPF_INSTR_CLOSE ))) ||
     352        1089 :            fd_vm_syscall_cpi_is_precompile( program_id ) );
     353        1089 : }
     354             : 
     355             : /* The data and lamports fields are in an Rc<Refcell<T>> in the Rust ABI AccountInfo.
     356             :    These macros perform the equivalent of Rc<Refcell<T>>.as_ptr() in Agave.
     357             :    This function doesn't actually touch any memory.
     358             :    It performs pointer arithmetic.
     359             :  */
     360             : FD_FN_CONST static inline
     361        3942 : ulong vm_syscall_cpi_acc_info_rc_refcell_as_ptr( ulong rc_refcell_vaddr ) {
     362        3942 :   return rc_refcell_vaddr + offsetof(fd_vm_rc_refcell_t, payload);
     363        3942 : }
     364             : 
     365             : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L508-L514
     366             :  */
     367             : FD_FN_CONST static inline
     368         555 : ulong vm_syscall_cpi_data_len_vaddr_c( ulong acct_info_vaddr, ulong data_len_haddr, ulong acct_info_haddr ) {
     369         555 :   return fd_ulong_sat_sub( fd_ulong_sat_add( acct_info_vaddr, data_len_haddr ), acct_info_haddr );
     370         555 : }
     371             : 
     372             : /**********************************************************************
     373             :   CROSS PROGRAM INVOCATION (C ABI)
     374             :  **********************************************************************/
     375             : 
     376             : #define VM_SYSCALL_CPI_ABI                     c
     377         510 : #define VM_SYSCALL_CPI_INSTR_T                 fd_vm_c_instruction_t
     378             : #define VM_SYSCALL_CPI_INSTR_ALIGN             (FD_VM_C_INSTRUCTION_ALIGN)
     379             : #define VM_SYSCALL_CPI_INSTR_SIZE              (FD_VM_C_INSTRUCTION_SIZE)
     380        1800 : #define VM_SYSCALL_CPI_ACC_META_T              fd_vm_c_account_meta_t
     381             : #define VM_SYSCALL_CPI_ACC_META_ALIGN          (FD_VM_C_ACCOUNT_META_ALIGN)
     382             : #define VM_SYSCALL_CPI_ACC_META_SIZE           (FD_VM_C_ACCOUNT_META_SIZE)
     383         477 : #define VM_SYSCALL_CPI_ACC_INFO_T              fd_vm_c_account_info_t
     384             : #define VM_SYSCALL_CPI_ACC_INFO_ALIGN          (FD_VM_C_ACCOUNT_INFO_ALIGN)
     385        1038 : #define VM_SYSCALL_CPI_ACC_INFO_SIZE           (FD_VM_C_ACCOUNT_INFO_SIZE)
     386             : 
     387             : /* VM_SYSCALL_CPI_INSTR_T accessors */
     388             : #define VM_SYSCALL_CPI_INSTR_DATA_ADDR( instr ) instr->data_addr
     389        1512 : #define VM_SYSCALL_CPI_INSTR_DATA_LEN( instr )  instr->data_len
     390             : #define VM_SYSCALL_CPI_INSTR_ACCS_ADDR( instr ) instr->accounts_addr
     391        3804 : #define VM_SYSCALL_CPI_INSTR_ACCS_LEN( instr )  instr->accounts_len
     392             : #define VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, instr ) \
     393         510 :   FD_VM_MEM_HADDR_LD( vm, instr->program_id_addr, alignof(uchar), sizeof(fd_pubkey_t)  )
     394             : 
     395             : /* VM_SYSCALL_CPI_ACC_META_T accessors */
     396        1278 : #define VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( acc_meta ) acc_meta->is_writable
     397        1278 : #define VM_SYSCALL_CPI_ACC_META_IS_SIGNER( acc_meta ) acc_meta->is_signer
     398             : #define VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, acc_meta ) \
     399        1290 :   FD_VM_MEM_HADDR_LD( vm, acc_meta->pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) )
     400             : 
     401             : /* VM_SYSCALL_CPI_ACC_INFO_T accessors */
     402             : #define VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, acc_info, decl ) \
     403         573 :   ulong decl = acc_info->lamports_addr;
     404             : #define VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, acc_info, decl ) \
     405         567 :   void * decl = FD_VM_MEM_HADDR_ST( vm, acc_info->lamports_addr, alignof(ulong), sizeof(ulong) );
     406             : 
     407             : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L304 */
     408             : #define VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, acc_info, decl ) \
     409         567 :   ulong decl = acc_info->data_addr;
     410             : #define VM_SYSCALL_CPI_ACC_INFO_METADATA( vm, acc_info, decl ) \
     411             :   ulong FD_EXPAND_THEN_CONCAT2(decl, _vm_addr) = acc_info->data_addr; \
     412             :   ulong FD_EXPAND_THEN_CONCAT2(decl, _len) = acc_info->data_sz;
     413             : 
     414             : #include "fd_vm_syscall_cpi_common.c"
     415             : 
     416             : #undef VM_SYSCALL_CPI_ABI
     417             : #undef VM_SYSCALL_CPI_INSTR_T
     418             : #undef VM_SYSCALL_CPI_INSTR_ALIGN
     419             : #undef VM_SYSCALL_CPI_INSTR_SIZE
     420             : #undef VM_SYSCALL_CPI_ACC_META_T
     421             : #undef VM_SYSCALL_CPI_ACC_META_ALIGN
     422             : #undef VM_SYSCALL_CPI_ACC_META_SIZE
     423             : #undef VM_SYSCALL_CPI_ACC_INFO_T
     424             : #undef VM_SYSCALL_CPI_ACC_INFO_ALIGN
     425             : #undef VM_SYSCALL_CPI_ACC_INFO_SIZE
     426             : #undef VM_SYSCALL_CPI_INSTR_DATA_ADDR
     427             : #undef VM_SYSCALL_CPI_INSTR_DATA_LEN
     428             : #undef VM_SYSCALL_CPI_INSTR_ACCS_ADDR
     429             : #undef VM_SYSCALL_CPI_INSTR_ACCS_LEN
     430             : #undef VM_SYSCALL_CPI_INSTR_PROGRAM_ID
     431             : #undef VM_SYSCALL_CPI_ACC_META_IS_WRITABLE
     432             : #undef VM_SYSCALL_CPI_ACC_META_IS_SIGNER
     433             : #undef VM_SYSCALL_CPI_ACC_META_PUBKEY
     434             : #undef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR
     435             : #undef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS
     436             : #undef VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR
     437             : #undef VM_SYSCALL_CPI_ACC_INFO_METADATA
     438             : 
     439             : /**********************************************************************
     440             :    CROSS PROGRAM INVOCATION (Rust ABI)
     441             :  **********************************************************************/
     442             : 
     443             : #define VM_SYSCALL_CPI_ABI                     rust
     444         603 : #define VM_SYSCALL_CPI_INSTR_T                 fd_vm_rust_instruction_t
     445             : #define VM_SYSCALL_CPI_INSTR_ALIGN             (FD_VM_RUST_INSTRUCTION_ALIGN)
     446             : #define VM_SYSCALL_CPI_INSTR_SIZE              (FD_VM_RUST_INSTRUCTION_SIZE)
     447        2085 : #define VM_SYSCALL_CPI_ACC_META_T              fd_vm_rust_account_meta_t
     448             : #define VM_SYSCALL_CPI_ACC_META_ALIGN          (FD_VM_RUST_ACCOUNT_META_ALIGN)
     449             : #define VM_SYSCALL_CPI_ACC_META_SIZE           (FD_VM_RUST_ACCOUNT_META_SIZE)
     450         570 : #define VM_SYSCALL_CPI_ACC_INFO_T              fd_vm_rust_account_info_t
     451             : #define VM_SYSCALL_CPI_ACC_INFO_ALIGN          (FD_VM_RUST_ACCOUNT_INFO_ALIGN)
     452         576 : #define VM_SYSCALL_CPI_ACC_INFO_SIZE           (FD_VM_RUST_ACCOUNT_INFO_SIZE)
     453             : 
     454             : /* VM_SYSCALL_CPI_INSTR_T accessors */
     455             : #define VM_SYSCALL_CPI_INSTR_DATA_ADDR( instr ) instr->data.addr
     456        1791 : #define VM_SYSCALL_CPI_INSTR_DATA_LEN( instr )  instr->data.len
     457             : #define VM_SYSCALL_CPI_INSTR_ACCS_ADDR( instr ) instr->accounts.addr
     458        4461 : #define VM_SYSCALL_CPI_INSTR_ACCS_LEN( instr )  instr->accounts.len
     459         603 : #define VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, instr ) instr->pubkey
     460             : 
     461             : /* VM_SYSCALL_CPI_ACC_META_T accessors */
     462        1470 : #define VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( acc_meta ) acc_meta->is_writable
     463        1470 : #define VM_SYSCALL_CPI_ACC_META_IS_SIGNER( acc_meta ) acc_meta->is_signer
     464        1482 : #define VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, acc_meta ) acc_meta->pubkey
     465             : 
     466             : /* VM_SYSCALL_CPI_ACC_INFO_T accessors */
     467             : 
     468             : /* The lamports and the account data are stored behind RefCells,
     469             :    so we have an additional layer of indirection to unwrap. */
     470             : #define VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, acc_info, decl )                                                                             \
     471         669 :     void const * FD_EXPAND_THEN_CONCAT2(decl, _hptr_) =                                                                                          \
     472         669 :       FD_VM_MEM_HADDR_LD( vm, vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->lamports_box_addr ), FD_VM_RC_REFCELL_ALIGN, sizeof(ulong) ); \
     473         669 :     /* Extract the vaddr embedded in the RefCell */                                                                                              \
     474         669 :     ulong decl = FD_LOAD( ulong, FD_EXPAND_THEN_CONCAT2(decl, _hptr_) );
     475             : 
     476             : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L184-L195 */
     477             : #define VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, acc_info, decl )                                                                                                     \
     478         657 :     ulong FD_EXPAND_THEN_CONCAT2(decl, _vaddr_) =                                                                                                                  \
     479         657 :       FD_LOAD( ulong, FD_VM_MEM_HADDR_LD( vm, vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->lamports_box_addr ), FD_VM_RC_REFCELL_ALIGN, sizeof(ulong) ) ); \
     480         657 :     void * decl = FD_VM_MEM_HADDR_ST( vm, FD_EXPAND_THEN_CONCAT2(decl, _vaddr_), alignof(ulong), sizeof(ulong) );
     481             : 
     482             : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L367-L378 */
     483             : #define VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, acc_info, decl )                                                                                   \
     484         657 :     if( FD_UNLIKELY( vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->data_box_addr ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {                \
     485           6 :       FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );                                                                          \
     486           6 :       return FD_VM_SYSCALL_ERR_INVALID_POINTER;                                                                                                    \
     487           6 :     }                                                                                                                                              \
     488         657 :     /* Translate the vaddr to the slice */                                                                                                         \
     489         657 :     fd_vm_vec_t const * FD_EXPAND_THEN_CONCAT2(decl, _hptr_) =                                                                                     \
     490         651 :       FD_VM_MEM_HADDR_LD( vm, vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->data_box_addr ), FD_VM_RC_REFCELL_ALIGN, sizeof(fd_vm_vec_t) ); \
     491         651 :     /* Extract the vaddr embedded in the slice */                                                                                                  \
     492         651 :     ulong decl = FD_EXPAND_THEN_CONCAT2(decl, _hptr_)->addr;
     493             : 
     494             : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L212-L221 */
     495             : #define VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, acc_info, decl ) \
     496         639 :     ulong decl = fd_ulong_sat_add( vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->data_box_addr ), sizeof(ulong) );
     497             : 
     498             : #define VM_SYSCALL_CPI_ACC_INFO_METADATA( vm, acc_info, decl )                                                                                     \
     499             :     /* Translate the vaddr to the slice */                                                                                                         \
     500             :     fd_vm_vec_t const * FD_EXPAND_THEN_CONCAT2(decl, _hptr_) =                                                                                     \
     501             :       FD_VM_MEM_HADDR_LD( vm, vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->data_box_addr ), FD_VM_RC_REFCELL_ALIGN, sizeof(fd_vm_vec_t) ); \
     502             :     /* Declare the vaddr of the slice's underlying byte array */                                                                                   \
     503             :     ulong FD_EXPAND_THEN_CONCAT2(decl, _vm_addr) = FD_EXPAND_THEN_CONCAT2(decl, _hptr_)->addr;                                                     \
     504             :     /* Declare the size of the slice's underlying byte array */                                                                                    \
     505             :     ulong FD_EXPAND_THEN_CONCAT2(decl, _len) = FD_EXPAND_THEN_CONCAT2(decl, _hptr_)->len;
     506             : 
     507             : #define VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, acc_info, decl ) \
     508         669 :     ulong decl = vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->lamports_box_addr );
     509             : 
     510             : #define VM_SYSCALL_CPI_ACC_INFO_DATA_RC_REFCELL_VADDR( vm, acc_info, decl ) \
     511             :     ulong decl = vm_syscall_cpi_acc_info_rc_refcell_as_ptr( acc_info->data_box_addr );
     512             : 
     513             : #include "fd_vm_syscall_cpi_common.c"
     514             : 
     515             : #undef VM_SYSCALL_CPI_ABI
     516             : #undef VM_SYSCALL_CPI_INSTR_T
     517             : #undef VM_SYSCALL_CPI_INSTR_ALIGN
     518             : #undef VM_SYSCALL_CPI_INSTR_SIZE
     519             : #undef VM_SYSCALL_CPI_ACC_META_T
     520             : #undef VM_SYSCALL_CPI_ACC_META_ALIGN
     521             : #undef VM_SYSCALL_CPI_ACC_META_SIZE
     522             : #undef VM_SYSCALL_CPI_ACC_INFO_T
     523             : #undef VM_SYSCALL_CPI_ACC_INFO_ALIGN
     524             : #undef VM_SYSCALL_CPI_ACC_INFO_SIZE
     525             : #undef VM_SYSCALL_CPI_INSTR_DATA_ADDR
     526             : #undef VM_SYSCALL_CPI_INSTR_DATA_LEN
     527             : #undef VM_SYSCALL_CPI_INSTR_ACCS_ADDR
     528             : #undef VM_SYSCALL_CPI_INSTR_ACCS_LEN
     529             : #undef VM_SYSCALL_CPI_INSTR_PROGRAM_ID
     530             : #undef VM_SYSCALL_CPI_ACC_META_IS_WRITABLE
     531             : #undef VM_SYSCALL_CPI_ACC_META_IS_SIGNER
     532             : #undef VM_SYSCALL_CPI_ACC_META_PUBKEY
     533             : #undef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR
     534             : #undef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS
     535             : #undef VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR
     536             : #undef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
     537             : #undef VM_SYSCALL_CPI_ACC_INFO_METADATA
     538             : #undef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
     539             : #undef VM_SYSCALL_CPI_ACC_INFO_DATA_RC_REFCELL_VADDR

Generated by: LCOV version 1.14