LCOV - code coverage report
Current view: top level - flamenco/runtime/context - fd_exec_txn_ctx.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 25 202 12.4 %
Date: 2025-10-13 04:42:14 Functions: 3 19 15.8 %

          Line data    Source code
       1             : #include "fd_exec_txn_ctx.h"
       2             : #include "../fd_acc_mgr.h"
       3             : #include "../fd_executor.h"
       4             : #include "../../vm/fd_vm.h"
       5             : #include "../fd_system_ids.h"
       6             : 
       7             : void *
       8           0 : fd_exec_txn_ctx_new( void * mem ) {
       9           0 :   if( FD_UNLIKELY( !mem ) ) {
      10           0 :     FD_LOG_WARNING(( "NULL mem" ));
      11           0 :     return NULL;
      12           0 :   }
      13             : 
      14           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_TXN_CTX_ALIGN ) ) ) {
      15           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      16           0 :     return NULL;
      17           0 :   }
      18             : 
      19           0 :   fd_exec_txn_ctx_t * self = (fd_exec_txn_ctx_t *) mem;
      20           0 :   fd_memset( self, 0, sizeof(fd_exec_txn_ctx_t) );
      21             : 
      22           0 :   FD_COMPILER_MFENCE();
      23           0 :   self->magic = FD_EXEC_TXN_CTX_MAGIC;
      24           0 :   FD_COMPILER_MFENCE();
      25             : 
      26           0 :   return mem;
      27           0 : }
      28             : 
      29             : fd_exec_txn_ctx_t *
      30             : fd_exec_txn_ctx_join( void *      mem,
      31             :                       fd_spad_t * spad,
      32           0 :                       fd_wksp_t * spad_wksp ) {
      33           0 :   if( FD_UNLIKELY( !mem ) ) {
      34           0 :     FD_LOG_WARNING(( "NULL block" ));
      35           0 :     return NULL;
      36           0 :   }
      37             : 
      38           0 :   fd_exec_txn_ctx_t * ctx = (fd_exec_txn_ctx_t *) mem;
      39             : 
      40           0 :   if( FD_UNLIKELY( ctx->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
      41           0 :     FD_LOG_WARNING(( "bad magic" ));
      42           0 :     return NULL;
      43           0 :   }
      44             : 
      45             :   /* Rejoin the wksp */
      46           0 :   ctx->spad      = spad;
      47           0 :   ctx->spad_wksp = spad_wksp;
      48             : 
      49           0 :   return ctx;
      50           0 : }
      51             : 
      52             : void *
      53           0 : fd_exec_txn_ctx_leave( fd_exec_txn_ctx_t * ctx) {
      54           0 :   if( FD_UNLIKELY( !ctx ) ) {
      55           0 :     FD_LOG_WARNING(( "NULL block" ));
      56           0 :     return NULL;
      57           0 :   }
      58             : 
      59           0 :   if( FD_UNLIKELY( ctx->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
      60           0 :     FD_LOG_WARNING(( "bad magic" ));
      61           0 :     return NULL;
      62           0 :   }
      63             : 
      64           0 :   return (void *) ctx;
      65           0 : }
      66             : 
      67             : void *
      68           0 : fd_exec_txn_ctx_delete( void * mem ) {
      69           0 :   if( FD_UNLIKELY( !mem ) ) {
      70           0 :     FD_LOG_WARNING(( "NULL mem" ));
      71           0 :     return NULL;
      72           0 :   }
      73             : 
      74           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_TXN_CTX_ALIGN) ) )  {
      75           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      76           0 :     return NULL;
      77           0 :   }
      78             : 
      79           0 :   fd_exec_txn_ctx_t * hdr = (fd_exec_txn_ctx_t *)mem;
      80           0 :   if( FD_UNLIKELY( hdr->magic!=FD_EXEC_TXN_CTX_MAGIC ) ) {
      81           0 :     FD_LOG_WARNING(( "bad magic" ));
      82           0 :     return NULL;
      83           0 :   }
      84             : 
      85           0 :   FD_COMPILER_MFENCE();
      86           0 :   FD_VOLATILE( hdr->magic ) = 0UL;
      87           0 :   FD_COMPILER_MFENCE();
      88             : 
      89           0 :   return mem;
      90           0 : }
      91             : 
      92             : int
      93             : fd_exec_txn_ctx_get_account_at_index( fd_exec_txn_ctx_t *             ctx,
      94             :                                       ushort                          idx,
      95             :                                       fd_txn_account_t * *            account,
      96           0 :                                       fd_txn_account_condition_fn_t * condition ) {
      97           0 :   if( FD_UNLIKELY( idx>=ctx->accounts_cnt ) ) {
      98           0 :     return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
      99           0 :   }
     100             : 
     101           0 :   fd_txn_account_t * txn_account = &ctx->accounts[idx];
     102           0 :   *account = txn_account;
     103             : 
     104           0 :   if( FD_LIKELY( condition != NULL ) ) {
     105           0 :     if( FD_UNLIKELY( !condition( *account, ctx, idx ) ) ) {
     106           0 :       return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     107           0 :     }
     108           0 :   }
     109             : 
     110           0 :   return FD_ACC_MGR_SUCCESS;
     111           0 : }
     112             : 
     113             : int
     114             : fd_exec_txn_ctx_get_account_with_key( fd_exec_txn_ctx_t *             ctx,
     115             :                                       fd_pubkey_t const *             pubkey,
     116             :                                       fd_txn_account_t * *            account,
     117           0 :                                       fd_txn_account_condition_fn_t * condition ) {
     118           0 :   int index = fd_exec_txn_ctx_find_index_of_account( ctx, pubkey );
     119           0 :   if( FD_UNLIKELY( index==-1 ) ) {
     120           0 :     return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     121           0 :   }
     122             : 
     123           0 :   return fd_exec_txn_ctx_get_account_at_index( ctx,
     124           0 :                                                (uchar)index,
     125           0 :                                                account,
     126           0 :                                                condition );
     127           0 : }
     128             : 
     129             : int
     130             : fd_exec_txn_ctx_get_executable_account( fd_exec_txn_ctx_t *             ctx,
     131             :                                         fd_pubkey_t const *             pubkey,
     132             :                                         fd_txn_account_t * *            account,
     133           0 :                                         fd_txn_account_condition_fn_t * condition ) {
     134             :   /* First try to fetch the executable account from the existing borrowed accounts.
     135             :      If the pubkey is in the account keys, then we want to re-use that
     136             :      borrowed account since it reflects changes from prior instructions. Referencing the
     137             :      read-only executable accounts list is incorrect behavior when the program
     138             :      data account is written to in a prior instruction (e.g. program upgrade + invoke within the same txn) */
     139           0 :   int err = fd_exec_txn_ctx_get_account_with_key( ctx, pubkey, account, condition );
     140           0 :   if( FD_UNLIKELY( err==FD_ACC_MGR_SUCCESS ) ) {
     141           0 :     return FD_ACC_MGR_SUCCESS;
     142           0 :   }
     143             : 
     144           0 :   for( ushort i=0; i<ctx->executable_cnt; i++ ) {
     145           0 :     if( memcmp( pubkey->uc, ctx->executable_accounts[i].pubkey->uc, sizeof(fd_pubkey_t) )==0 ) {
     146           0 :       fd_txn_account_t * txn_account = &ctx->executable_accounts[i];
     147           0 :       *account = txn_account;
     148             : 
     149           0 :       if( FD_LIKELY( condition != NULL ) ) {
     150           0 :         if( FD_UNLIKELY( !condition( *account, ctx, i ) ) ) {
     151           0 :           return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     152           0 :         }
     153           0 :       }
     154             : 
     155           0 :       return FD_ACC_MGR_SUCCESS;
     156           0 :     }
     157           0 :   }
     158             : 
     159           0 :   return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     160           0 : }
     161             : 
     162             : int
     163             : fd_exec_txn_ctx_get_key_of_account_at_index( fd_exec_txn_ctx_t *  ctx,
     164             :                                              ushort               idx,
     165           0 :                                              fd_pubkey_t const * * key ) {
     166             :   /* Return a NotEnoughAccountKeys error if idx is out of bounds.
     167             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L218 */
     168           0 :   if( FD_UNLIKELY( idx>=ctx->accounts_cnt ) ) {
     169           0 :     return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
     170           0 :   }
     171             : 
     172           0 :   *key = &ctx->account_keys[ idx ];
     173           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     174           0 : }
     175             : 
     176             : void
     177           0 : fd_exec_txn_ctx_setup_basic( fd_exec_txn_ctx_t * ctx ) {
     178           0 :   fd_compute_budget_details_new( &ctx->compute_budget_details );
     179             : 
     180           0 :   ctx->custom_err                = 0U;
     181           0 :   ctx->instr_stack_sz            = 0;
     182           0 :   ctx->accounts_cnt              = 0UL;
     183           0 :   ctx->executable_cnt            = 0UL;
     184           0 :   ctx->programs_to_reverify_cnt  = 0UL;
     185             : 
     186           0 :   ctx->paid_fees                      = 0UL;
     187           0 :   ctx->loaded_accounts_data_size      = 0UL;
     188           0 :   ctx->loaded_accounts_data_size_cost = 0UL;
     189           0 :   ctx->accounts_resize_delta          = 0UL;
     190           0 :   ctx->collected_rent                 = 0UL;
     191             : 
     192           0 :   ctx->num_instructions = 0;
     193           0 :   memset( ctx->return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
     194           0 :   ctx->return_data.len = 0;
     195             : 
     196           0 :   ctx->dirty_vote_acc  = 0;
     197           0 :   ctx->failed_instr    = NULL;
     198           0 :   ctx->instr_err_idx   = INT_MAX;
     199           0 :   ctx->capture_ctx     = NULL;
     200             : 
     201           0 :   ctx->instr_info_cnt     = 0UL;
     202           0 :   ctx->cpi_instr_info_cnt = 0UL;
     203           0 :   ctx->instr_trace_length = 0UL;
     204             : 
     205           0 :   ctx->exec_err          = 0;
     206           0 :   ctx->exec_err_kind     = FD_EXECUTOR_ERR_KIND_NONE;
     207           0 :   ctx->current_instr_idx = 0;
     208           0 : }
     209             : 
     210             : void
     211           0 : fd_exec_txn_ctx_teardown( fd_exec_txn_ctx_t * ctx ) {
     212           0 :   (void)ctx;
     213           0 : }
     214             : 
     215             : void
     216           0 : fd_exec_txn_ctx_reset_return_data( fd_exec_txn_ctx_t * txn_ctx ) {
     217           0 :   txn_ctx->return_data.len = 0;
     218           0 : }
     219             : 
     220             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
     221             : int
     222             : fd_txn_account_is_demotion( const int        idx,
     223             :                             const fd_txn_t * txn_descriptor,
     224         108 :                             const uint       bpf_upgradeable_in_txn ) {
     225         108 :   uint is_program = 0U;
     226         216 :   for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
     227         108 :     if( txn_descriptor->instr[j].program_id == idx ) {
     228           0 :       is_program = 1U;
     229           0 :       break;
     230           0 :     }
     231         108 :   }
     232             : 
     233         108 :   return (is_program && !bpf_upgradeable_in_txn);
     234         108 : }
     235             : 
     236             : uint
     237             : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
     238          51 :                                            const ulong         accounts_cnt ) {
     239         225 :   for( ulong j=0; j<accounts_cnt; j++ ) {
     240         174 :     const fd_pubkey_t * acc = &account_keys[j];
     241         174 :     if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
     242           0 :       return 1U;
     243           0 :     }
     244         174 :   }
     245          51 :   return 0U;
     246          51 : }
     247             : 
     248             : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
     249             :    to determine if accounts are writable or not.
     250             : 
     251             :    https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
     252             : int
     253           0 : fd_exec_txn_ctx_account_is_writable_idx( fd_exec_txn_ctx_t const * txn_ctx, ushort idx ) {
     254           0 :   uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_ctx->account_keys, txn_ctx->accounts_cnt );
     255           0 :   return fd_exec_txn_account_is_writable_idx_flat( txn_ctx->slot,
     256           0 :                                                    idx,
     257           0 :                                                    &txn_ctx->account_keys[idx],
     258           0 :                                                    TXN( &txn_ctx->txn ),
     259           0 :                                                    &txn_ctx->features,
     260           0 :                                                    bpf_upgradeable );
     261           0 : }
     262             : 
     263             : int
     264             : fd_exec_txn_account_is_writable_idx_flat( const ulong           slot,
     265             :                                           const ushort          idx,
     266             :                                           const fd_pubkey_t *   addr_at_idx,
     267             :                                           const fd_txn_t *      txn_descriptor,
     268             :                                           const fd_features_t * features,
     269         111 :                                           const uint            bpf_upgradeable_in_txn ) {
     270             :   /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
     271         111 :   if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
     272           0 :     return 0;
     273           0 :   }
     274             : 
     275             :   /* See comments in fd_system_ids.h.
     276             :      https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
     277         111 :   if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
     278         111 :       fd_pubkey_is_pending_reserved_key( addr_at_idx ) ||
     279         111 :       ( FD_FEATURE_ACTIVE( slot, features, enable_secp256r1_precompile ) &&
     280         108 :                            fd_pubkey_is_secp256r1_key( addr_at_idx ) ) ) {
     281             : 
     282           3 :     return 0;
     283           3 :   }
     284             : 
     285         108 :   if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
     286           0 :     return 0;
     287           0 :   }
     288             : 
     289         108 :   return 1;
     290         108 : }
     291             : 
     292             : /* Account pre-condition filtering functions */
     293             : 
     294             : int
     295             : fd_txn_account_check_exists( fd_txn_account_t *        acc,
     296             :                              fd_exec_txn_ctx_t const * ctx,
     297           0 :                              ushort                    idx ) {
     298           0 :   (void) ctx;
     299           0 :   (void) idx;
     300           0 :   return fd_account_meta_exists( fd_txn_account_get_meta( acc ) );
     301           0 : }
     302             : 
     303             : int
     304             : fd_txn_account_check_is_writable( fd_txn_account_t *        acc,
     305             :                                   fd_exec_txn_ctx_t const * ctx,
     306           0 :                                   ushort                    idx ) {
     307           0 :   (void) acc;
     308           0 :   return fd_exec_txn_ctx_account_is_writable_idx( ctx, idx );
     309           0 : }
     310             : 
     311             : int
     312             : fd_txn_account_check_fee_payer_writable( fd_txn_account_t *        acc,
     313             :                                          fd_exec_txn_ctx_t const * ctx,
     314           0 :                                          ushort                    idx ) {
     315           0 :   (void) acc;
     316           0 :   return fd_txn_is_writable( TXN( &ctx->txn ), idx );
     317           0 : }
     318             : 
     319             : int
     320             : fd_txn_account_check_borrow_mut( fd_txn_account_t *        acc,
     321             :                                  fd_exec_txn_ctx_t const * ctx,
     322           0 :                                  ushort                    idx ) {
     323           0 :   (void) ctx;
     324           0 :   (void) idx;
     325           0 :   return fd_txn_account_is_mutable( acc ) && fd_txn_account_try_borrow_mut( acc );
     326           0 : }

Generated by: LCOV version 1.14