LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_borrowed_account.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 124 0.0 %
Date: 2025-12-13 04:39:40 Functions: 0 304 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h
       2             : #define HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h
       3             : 
       4             : #include "fd_bank.h"
       5             : #include "fd_executor_err.h"
       6             : #include "context/fd_exec_instr_ctx.h"
       7             : #include "sysvar/fd_sysvar_rent.h"
       8             : #include "program/fd_program_util.h"
       9             : 
      10           0 : #define MAX_PERMITTED_DATA_LENGTH                 (FD_RUNTIME_ACC_SZ_MAX) /* 10MiB */
      11             : #define MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN (10UL<<21) /* 20MiB */
      12             : 
      13             : /* TODO: Not all Agave Borrowed Account API functions are implemented here */
      14             : 
      15             : /* TODO: check that borrow is active when calling these APIs */
      16             : 
      17             : struct fd_borrowed_account {
      18             :   ulong                       magic;
      19             :   fd_txn_account_t *          acct;
      20             :   fd_exec_instr_ctx_t const * instr_ctx;
      21             : 
      22             :   /* index_in_instruction will be USHORT_MAX for borrowed program accounts because
      23             :      they are not stored in the list of instruction accounts in the instruction context */
      24             :   ushort                      index_in_instruction;
      25             : };
      26             : typedef struct fd_borrowed_account fd_borrowed_account_t;
      27             : 
      28           0 : #define FD_BORROWED_ACCOUNT_MAGIC (0xFDB07703ACC736C0UL) /* FD BORROW ACCT MGC version 0 */
      29             : /* prevents borrowed accounts from going out of scope without releasing a borrow */
      30           0 : #define fd_guarded_borrowed_account_t __attribute__((cleanup(fd_borrowed_account_destroy))) fd_borrowed_account_t
      31             : 
      32             : FD_PROTOTYPES_BEGIN
      33             : 
      34             : /* Constructor */
      35             : 
      36             : static inline void
      37             : fd_borrowed_account_init( fd_borrowed_account_t *     borrowed_acct,
      38             :                           fd_txn_account_t *          acct,
      39             :                           fd_exec_instr_ctx_t const * instr_ctx,
      40           0 :                           ushort                      index_in_instruction ) {
      41           0 :   borrowed_acct->acct                 = acct;
      42           0 :   borrowed_acct->instr_ctx            = instr_ctx;
      43           0 :   borrowed_acct->index_in_instruction = index_in_instruction;
      44             : 
      45           0 :   FD_COMPILER_MFENCE();
      46           0 :   borrowed_acct->magic = FD_BORROWED_ACCOUNT_MAGIC;
      47           0 :   FD_COMPILER_MFENCE();
      48           0 : }
      49             : 
      50             : /* Drop mirrors the behavior of rust's std::mem::drop on mutable borrows.
      51             :    Releases the acquired write on the borrowed account object. */
      52             : 
      53             : static inline void
      54           0 : fd_borrowed_account_drop( fd_borrowed_account_t * borrowed_acct ) {
      55           0 :   fd_txn_account_drop( borrowed_acct->acct );
      56           0 : }
      57             : 
      58             : /* Destructor  */
      59             : 
      60             : static inline void
      61           0 : fd_borrowed_account_destroy( fd_borrowed_account_t * borrowed_acct ) {
      62           0 :   if( FD_LIKELY( borrowed_acct->magic == FD_BORROWED_ACCOUNT_MAGIC ) ) {
      63           0 :     fd_borrowed_account_drop( borrowed_acct );
      64           0 :   }
      65             : 
      66           0 :   FD_COMPILER_MFENCE();
      67           0 :   FD_VOLATILE( borrowed_acct->magic ) = 0UL;
      68           0 :   FD_COMPILER_MFENCE();
      69             : 
      70           0 :   borrowed_acct = NULL;
      71           0 : }
      72             : 
      73             : /* Getters */
      74             : 
      75             : /* fd_borrowed_account_get_data mirrors Agave function
      76             :    solana_sdk::transaction_context::BorrowedAccount::get_data.
      77             : 
      78             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L817 */
      79             : 
      80             : static inline uchar const *
      81           0 : fd_borrowed_account_get_data( fd_borrowed_account_t const * borrowed_acct ) {
      82           0 :   return fd_txn_account_get_data( borrowed_acct->acct );
      83           0 : }
      84             : 
      85             : static inline ulong
      86           0 : fd_borrowed_account_get_data_len( fd_borrowed_account_t const * borrowed_acct ) {
      87           0 :   return fd_txn_account_get_data_len( borrowed_acct->acct );
      88           0 : }
      89             : 
      90             : /* fd_borrowed_account_get_data_mut mirrors Agave function
      91             :    solana_sdk::transaction_context::BorrowedAccount::get_data_mut.
      92             : 
      93             :    Returns a writable slice of the account data (transaction wide).
      94             :    Acquires a writable handle. This function assumes that the relevant
      95             :    borrowed has already acquired exclusive write access.
      96             : 
      97             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L823 */
      98             : 
      99             : int
     100             : fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct,
     101             :                                   uchar * *               data_out,
     102             :                                   ulong *                 dlen_out );
     103             : 
     104             : static inline fd_pubkey_t const *
     105           0 : fd_borrowed_account_get_owner( fd_borrowed_account_t const * borrowed_acct ) {
     106           0 :   return fd_txn_account_get_owner( borrowed_acct->acct );
     107           0 : }
     108             : 
     109             : /* fd_borrowed_account_get_lamports mirrors Agave function
     110             :    solana_sdk::transaction_context::BorrowedAccount::get_lamports.
     111             : 
     112             :    Returns current number of lamports in account.  Well behaved if meta
     113             :    is NULL.
     114             : 
     115             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L767 */
     116             : 
     117             : static inline ulong
     118           0 : fd_borrowed_account_get_lamports( fd_borrowed_account_t const * borrowed_acct ) {
     119           0 :   return fd_txn_account_get_lamports( borrowed_acct->acct );
     120           0 : }
     121             : 
     122             : /* fd_borrowed_account_get_rent_epoch mirrors Agave function
     123             :    solana_sdk::transaction_context::BorrowedAccount::get_rent_epoch.
     124             : 
     125             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1034 */
     126             : 
     127             : static inline ulong
     128           0 : fd_borrowed_account_get_rent_epoch( fd_borrowed_account_t const * borrowed_acct ) {
     129           0 :   return fd_txn_account_get_rent_epoch( borrowed_acct->acct );
     130           0 : }
     131             : 
     132             : static inline fd_account_meta_t const *
     133           0 : fd_borrowed_account_get_acc_meta( fd_borrowed_account_t const * borrowed_acct ) {
     134           0 :   return fd_txn_account_get_meta( borrowed_acct->acct );
     135           0 : }
     136             : 
     137             : /* Setters */
     138             : 
     139             : /* fd_borrowed_account_set_owner mirrors Agave function
     140             :    solana_sdk::transaction_context::BorrowedAccount::set_owner.
     141             : 
     142             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L739 */
     143             : 
     144             : int
     145             : fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct,
     146             :                                fd_pubkey_t const *     owner );
     147             : 
     148             : /* fd_borrowed_account_set_lamports mirrors Agave function
     149             :    solana_sdk::transaction_context::BorrowedAccount::set_lamports.
     150             : 
     151             :    Runs through a sequence of permission checks, then sets the account
     152             :    balance.  Does not update global capitalization.  On success, returns
     153             :    0 and updates meta->lamports.  On failure, returns an
     154             :    FD_EXECUTOR_INSTR_ERR_{...} code.  Acquires a writable handle.
     155             : 
     156             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L773 */
     157             : 
     158             : int
     159             : fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct,
     160             :                                   ulong                   lamports );
     161             : 
     162             : /* fd_borrowed_account_set_data_from_slice mirrors Agave function
     163             :    solana_sdk::transaction_context::BorrowedAccount::set_data_from_slice.
     164             : 
     165             :    In the firedancer client, it also mirrors the Agave function
     166             :    solana_sdk::transaction_context::BorrowedAccount::set_data.
     167             :    Assumes that destination account already has enough space to fit
     168             :    data.  Acquires a writable handle.
     169             : 
     170             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L864 */
     171             : 
     172             : int
     173             : fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct,
     174             :                                          uchar const *           data,
     175             :                                          ulong                   data_sz );
     176             : 
     177             : /* fd_borrowed_account_set_data_length mirrors Agave function
     178             :    solana_sdk::transaction_context::BorrowedAccount::set_data_length.
     179             : 
     180             :    Acquires a writable handle. Returns 0 on success.
     181             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L882 */
     182             : 
     183             : int
     184             : fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct,
     185             :                                      ulong                   new_len );
     186             : 
     187             : /* fd_borrowed_account_set_executable mirrors Agave function
     188             :    solana_sdk::transaction_context::BorrowedAccount::set_executable.
     189             : 
     190             :    Returns FD_EXECUTOR_INSTR_SUCCESS if the set is successful.
     191             : 
     192             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L10015 */
     193             : 
     194             : int
     195             : fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct,
     196             :                                     int                     is_executable );
     197             : 
     198             : /* Operators */
     199             : 
     200             : /* fd_borrowed_account_checked_add_lamports mirros Agave function
     201             :    solana_sdk::transaction_context::BorrowedAccount::checked_add_lamports.
     202             : 
     203             :    Does not update global capitalization. Returns 0 on
     204             :    success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure.
     205             :    Gracefully handles underflow.
     206             : 
     207             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L797 */
     208             : 
     209             : static inline int
     210             : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * borrowed_acct,
     211           0 :                                           ulong                   lamports ) {
     212           0 :   ulong balance_post = 0UL;
     213           0 :   int err = fd_ulong_checked_add( fd_txn_account_get_lamports( borrowed_acct->acct ),
     214           0 :                                   lamports,
     215           0 :                                   &balance_post );
     216           0 :   if( FD_UNLIKELY( err ) ) {
     217           0 :     return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
     218           0 :   }
     219             : 
     220           0 :   return fd_borrowed_account_set_lamports( borrowed_acct, balance_post );
     221           0 : }
     222             : 
     223             : /* fd_borrowed_account_checked_sub_lamports mirrors Agave function
     224             :    solana_sdk::transaction_context::BorrowedAccount::checked_sub_lamports.
     225             : 
     226             :    Does not update global capitalization. Returns 0 on
     227             :    success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure.
     228             :    Gracefully handles underflow.
     229             : 
     230             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L807 */
     231             : 
     232             : static inline int
     233             : fd_borrowed_account_checked_sub_lamports( fd_borrowed_account_t * borrowed_acct,
     234           0 :                                           ulong                   lamports ) {
     235           0 :   ulong balance_post = 0UL;
     236           0 :   int err = fd_ulong_checked_sub( fd_txn_account_get_lamports( borrowed_acct->acct ),
     237           0 :                                   lamports,
     238           0 :                                   &balance_post );
     239           0 :   if( FD_UNLIKELY( err ) ) {
     240           0 :     return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
     241           0 :   }
     242             : 
     243           0 :   return fd_borrowed_account_set_lamports( borrowed_acct, balance_post );
     244           0 : }
     245             : 
     246             : /* fd_borrowed_account_update_acounts_resize_delta mirrors Agave function
     247             :    solana_sdk::transaction_context:BorrowedAccount::update_accounts_resize_delta.
     248             : 
     249             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1123 */
     250             : 
     251             : int
     252             : fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrowed_acct,
     253             :                                                   ulong                         new_len,
     254             :                                                   int *                         err );
     255             : 
     256             : /* Accessors */
     257             : 
     258             : /* fd_borrowed_account_is_rent_exempt_at_data_length mirrors Agave function
     259             :    solana_sdk::transaction_context::BorrowedAccount::is_rent_exempt_at_data_length.
     260             : 
     261             :    Returns 1 if an account is rent exempt at it's current data length.
     262             : 
     263             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L987 */
     264             : 
     265             : static inline int
     266           0 : fd_borrowed_account_is_rent_exempt_at_data_length( fd_borrowed_account_t const * borrowed_acct ) {
     267           0 :   fd_txn_account_t * acct = borrowed_acct->acct;
     268           0 :   if( FD_UNLIKELY( !fd_txn_account_get_meta( acct ) ) ) FD_LOG_ERR(( "account is not setup" ));
     269           0 : 
     270           0 :   /* TODO: Add an is_exempt rent API to better match Agave and clean up code
     271           0 :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L990 */
     272           0 :   fd_rent_t const * rent        = fd_bank_rent_query( borrowed_acct->instr_ctx->bank );
     273           0 :   ulong             min_balance = fd_rent_exempt_minimum_balance( rent, fd_txn_account_get_data_len( acct ) );
     274           0 :   return fd_txn_account_get_lamports( acct )>=min_balance;
     275           0 : }
     276             : 
     277             : /* fd_borrowed_account_is_executable mirrors Agave function
     278             :    solana_sdk::transaction_context::BorrowedAccount::is_executable.
     279             : 
     280             :    Returns 1 if the given account has the
     281             :    executable flag set.  Otherwise, returns 0.
     282             : 
     283             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L995 */
     284             : 
     285             : FD_FN_PURE static inline int
     286           0 : fd_borrowed_account_is_executable( fd_borrowed_account_t const * borrowed_acct ) {
     287           0 :   return fd_txn_account_is_executable( borrowed_acct->acct );
     288           0 : }
     289             : 
     290             : FD_FN_PURE static inline int
     291           0 : fd_borrowed_account_is_mutable( fd_borrowed_account_t const * borrowed_acct ) {
     292           0 :   return fd_txn_account_is_mutable( borrowed_acct->acct );
     293           0 : }
     294             : 
     295             : /* fd_borrowed_account_is_signer mirrors the Agave function
     296             :    solana_sdk::transaction_context::BorrowedAccount::is_signer.
     297             :    Returns 1 if the account is a signer or is writable and 0 otherwise.
     298             : 
     299             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1039 */
     300             : 
     301             : static inline int
     302           0 : fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) {
     303           0 :   fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
     304           0 :   fd_instr_info_t     const * instr     = instr_ctx->instr;
     305             : 
     306           0 :   if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
     307           0 :     return 0;
     308           0 :   }
     309             : 
     310           0 :   return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction, NULL );
     311           0 : }
     312             : 
     313             : /* fd_borrowed_account_is_writer mirrors the Agave function
     314             :    solana_sdk::transaction_context::BorrowedAccount::is_writer.
     315             :    Returns 1 if the account is a signer or is writable and 0 otherwise.
     316             : 
     317             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1052 */
     318             : 
     319             : static inline int
     320           0 : fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) {
     321           0 :   fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
     322           0 :   fd_instr_info_t     const * instr     = instr_ctx->instr;
     323             : 
     324           0 :   if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
     325           0 :     return 0;
     326           0 :   }
     327             : 
     328           0 :   return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction );
     329           0 : }
     330             : 
     331             : /* fd_borrowed_account_is_owned_by_current_program mirrors Agave's
     332             :    solana_sdk::transaction_context::BorrowedAccount::is_owned_by_current_program.
     333             : 
     334             :    Returns 1 if the given
     335             :    account is owned by the program invoked in the current instruction.
     336             :    Otherwise, returns 0.
     337             : 
     338             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1065 */
     339             : 
     340             : FD_FN_PURE static inline int
     341           0 : fd_borrowed_account_is_owned_by_current_program( fd_borrowed_account_t const * borrowed_acct ) {
     342           0 :   fd_pubkey_t const * program_id_pubkey = NULL;
     343           0 :   int err = fd_exec_instr_ctx_get_last_program_key( borrowed_acct->instr_ctx, &program_id_pubkey );
     344           0 :   if( FD_UNLIKELY( err ) ) {
     345           0 :     return 0;
     346           0 :   }
     347             : 
     348           0 :   return !memcmp( program_id_pubkey->key,
     349           0 :                  fd_txn_account_get_owner( borrowed_acct->acct ), sizeof(fd_pubkey_t) );
     350           0 : }
     351             : 
     352             : /* fd_borrowed_account_can_data_be changed mirrors Agave function
     353             :    solana_sdk::transaction_context::BorrowedAccount::can_data_be_changed.
     354             : 
     355             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1074 */
     356             : 
     357             : static inline int
     358             : fd_borrowed_account_can_data_be_changed( fd_borrowed_account_t const * borrowed_acct,
     359           0 :                                          int *                       err ) {
     360             :   /* Only writable accounts can be changed
     361             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1080 */
     362           0 :   if( FD_UNLIKELY( !fd_borrowed_account_is_writable( borrowed_acct ) ) ) {
     363           0 :     *err = FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED;
     364           0 :     return 0;
     365           0 :   }
     366             : 
     367             :   /* And only if we are the owner
     368             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1084 */
     369           0 :   if( FD_UNLIKELY( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) {
     370           0 :     *err = FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED;
     371           0 :     return 0;
     372           0 :   }
     373             : 
     374           0 :   *err = FD_EXECUTOR_INSTR_SUCCESS;
     375           0 :   return 1;
     376           0 : }
     377             : 
     378             : /* fd_borrowed_account_can_data_be_resized mirrors Agave function
     379             :    solana_sdk::transaction_context::BorrowedAccount::can_data_be_resized
     380             : 
     381             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1092 */
     382             : 
     383             : int
     384             : fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct,
     385             :                                          ulong                         new_length,
     386             :                                          int *                         err );
     387             : 
     388             : FD_FN_PURE static inline int
     389           0 : fd_borrowed_account_is_zeroed( fd_borrowed_account_t const * borrowed_acct ) {
     390           0 :   fd_txn_account_t * acct = borrowed_acct->acct;
     391             :   /* TODO: optimize this */
     392           0 :   uchar const * data = fd_txn_account_get_data( acct );
     393           0 :   for( ulong i=0UL; i<fd_txn_account_get_data_len( acct ); i++ ) {
     394           0 :     if( data[i] != 0 ) {
     395           0 :       return 0;
     396           0 :     }
     397           0 :   }
     398           0 :   return 1;
     399           0 : }
     400             : 
     401             : FD_PROTOTYPES_END
     402             : 
     403             : #endif /* HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h */

Generated by: LCOV version 1.14