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: 0 198 0.0 %
Date: 2025-11-07 04:31:33 Functions: 0 19 0.0 %

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

Generated by: LCOV version 1.14