LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_borrowed_account.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 73 103 70.9 %
Date: 2026-08-29 04:30:01 Functions: 51 465 11.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 (10L<<21)  /* 20MiB */
      12             : 
      13             : /* Kept equal to the canonical constant in fd_runtime_const.h, which the
      14             :    bpf loader serialization buffer bound relies on. */
      15             : FD_STATIC_ASSERT( MAX_PERMITTED_ACCOUNT_DATA_ALLOCS_PER_TXN==FD_RUNTIME_ACC_DATA_GROWTH_MAX_PER_TXN, account_data_growth_cap );
      16             : 
      17             : /* TODO: Not all Agave Borrowed Account API functions are implemented here */
      18             : 
      19             : /* TODO: check that borrow is active when calling these APIs */
      20             : 
      21             : struct fd_borrowed_account {
      22             :   fd_acc_t *                  acc;
      23             :   fd_exec_instr_ctx_t const * instr_ctx;
      24             : 
      25             :   /* index_in_instruction will be USHORT_MAX for borrowed program accounts because
      26             :      they are not stored in the list of instruction accounts in the instruction context */
      27             :   ushort                      index_in_instruction;
      28             : 
      29             :   ulong *                     refcnt;
      30             : };
      31             : 
      32             : typedef struct fd_borrowed_account fd_borrowed_account_t;
      33             : 
      34             : /* prevents borrowed accounts from going out of scope without releasing a borrow */
      35             : /* TODO: Remove ... */
      36       23160 : #define fd_guarded_borrowed_account_t __attribute__((cleanup(fd_borrowed_account_drop))) fd_borrowed_account_t
      37             : 
      38             : FD_PROTOTYPES_BEGIN
      39             : 
      40             : static inline void
      41             : fd_borrowed_account_init( fd_borrowed_account_t *     borrowed_acct,
      42             :                           fd_acc_t *                  acc,
      43             :                           fd_exec_instr_ctx_t const * instr_ctx,
      44             :                           ushort                      index_in_instruction,
      45       23181 :                           ulong *                     refcnt ) {
      46       23181 :   borrowed_acct->acc                  = acc;
      47       23181 :   borrowed_acct->instr_ctx            = instr_ctx;
      48       23181 :   borrowed_acct->index_in_instruction = index_in_instruction;
      49       23181 :   borrowed_acct->refcnt               = refcnt;
      50       23181 : }
      51             : 
      52             : /* Drop mirrors the behavior of rust's std::mem::drop on mutable borrows.
      53             :    Releases the acquired write on the borrowed account object. */
      54             : 
      55             : static inline void
      56       29751 : fd_borrowed_account_drop( fd_borrowed_account_t * borrowed_acct ) {
      57       29751 :   if( FD_LIKELY( borrowed_acct->refcnt ) ) *borrowed_acct->refcnt = 0;
      58       29751 :   borrowed_acct->refcnt = NULL;
      59       29751 : }
      60             : 
      61             : /* Getters */
      62             : 
      63             : /* fd_borrowed_account_get_data mirrors Agave function
      64             :    solana_sdk::transaction_context::BorrowedAccount::get_data.
      65             : 
      66             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L817 */
      67             : 
      68             : static inline uchar const *
      69        4722 : fd_borrowed_account_get_data( fd_borrowed_account_t const * borrowed_acct ) {
      70        4722 :   return borrowed_acct->acc->data;
      71        4722 : }
      72             : 
      73             : static inline ulong
      74        9024 : fd_borrowed_account_get_data_len( fd_borrowed_account_t const * borrowed_acct ) {
      75        9024 :   return borrowed_acct->acc->data_len;
      76        9024 : }
      77             : 
      78             : /* fd_borrowed_account_get_data_mut mirrors Agave function
      79             :    solana_sdk::transaction_context::BorrowedAccount::get_data_mut.
      80             : 
      81             :    Returns a writable slice of the account data (transaction wide).
      82             :    Acquires a writable handle. This function assumes that the relevant
      83             :    borrowed has already acquired exclusive write access.
      84             : 
      85             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L823 */
      86             : 
      87             : int
      88             : fd_borrowed_account_get_data_mut( fd_borrowed_account_t * borrowed_acct,
      89             :                                   uchar * *               data_out,
      90             :                                   ulong *                 dlen_out );
      91             : 
      92             : static inline fd_pubkey_t const *
      93        9816 : fd_borrowed_account_get_owner( fd_borrowed_account_t const * borrowed_acct ) {
      94        9816 :   return (fd_pubkey_t const *)borrowed_acct->acc->owner;
      95        9816 : }
      96             : 
      97             : /* fd_borrowed_account_get_lamports mirrors Agave function
      98             :    solana_sdk::transaction_context::BorrowedAccount::get_lamports.
      99             : 
     100             :    Returns current number of lamports in account.  Well behaved if meta
     101             :    is NULL.
     102             : 
     103             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L767 */
     104             : 
     105             : static inline ulong
     106        7323 : fd_borrowed_account_get_lamports( fd_borrowed_account_t const * borrowed_acct ) {
     107        7323 :   return borrowed_acct->acc->lamports;
     108        7323 : }
     109             : 
     110             : /* Setters */
     111             : 
     112             : /* fd_borrowed_account_set_owner mirrors Agave function
     113             :    solana_sdk::transaction_context::BorrowedAccount::set_owner.
     114             : 
     115             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L739 */
     116             : 
     117             : int
     118             : fd_borrowed_account_set_owner( fd_borrowed_account_t * borrowed_acct,
     119             :                                fd_pubkey_t const *     owner );
     120             : 
     121             : /* fd_borrowed_account_set_lamports mirrors Agave function
     122             :    solana_sdk::transaction_context::BorrowedAccount::set_lamports.
     123             : 
     124             :    Runs through a sequence of permission checks, then sets the account
     125             :    balance.  Does not update global capitalization.  On success, returns
     126             :    0 and updates meta->lamports.  On failure, returns an
     127             :    FD_EXECUTOR_INSTR_ERR_{...} code.  Acquires a writable handle.
     128             : 
     129             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L773 */
     130             : 
     131             : int
     132             : fd_borrowed_account_set_lamports( fd_borrowed_account_t * borrowed_acct,
     133             :                                   ulong                   lamports );
     134             : 
     135             : /* fd_borrowed_account_set_data_from_slice mirrors Agave function
     136             :    solana_transaction_context::instruction_accounts::BorrowedInstructionAccount::set_data_from_slice.
     137             : 
     138             :    Firedancer account storage is preallocated, so the destination
     139             :    account must already have enough space to fit data.  Acquires a
     140             :    writable handle.
     141             : 
     142             :    https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L177-L192 */
     143             : 
     144             : int
     145             : fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct,
     146             :                                          uchar const *           data,
     147             :                                          ulong                   data_sz );
     148             : 
     149             : /* fd_borrowed_account_set_data_length mirrors Agave function
     150             :    solana_transaction_context::instruction_accounts::BorrowedInstructionAccount::set_data_length.
     151             : 
     152             :    Acquires a writable handle. Returns 0 on success.
     153             :    https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L194-L207 */
     154             : 
     155             : int
     156             : fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct,
     157             :                                      ulong                   new_len );
     158             : 
     159             : /* fd_borrowed_account_set_executable mirrors Agave function
     160             :    solana_sdk::transaction_context::BorrowedAccount::set_executable.
     161             : 
     162             :    Returns FD_EXECUTOR_INSTR_SUCCESS if the set is successful.
     163             : 
     164             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L10015 */
     165             : 
     166             : int
     167             : fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct,
     168             :                                     int                     is_executable );
     169             : 
     170             : /* Operators */
     171             : 
     172             : /* fd_borrowed_account_checked_add_lamports mirrors Agave function
     173             :    solana_sdk::transaction_context::BorrowedAccount::checked_add_lamports.
     174             : 
     175             :    Does not update global capitalization. Returns 0 on
     176             :    success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure.
     177             :    Gracefully handles underflow.
     178             : 
     179             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L797 */
     180             : 
     181             : static inline int
     182             : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * borrowed_acct,
     183          57 :                                           ulong                   lamports ) {
     184          57 :   ulong balance_post = 0UL;
     185          57 :   int err = fd_ulong_checked_add( borrowed_acct->acc->lamports,
     186          57 :                                   lamports,
     187          57 :                                   &balance_post );
     188          57 :   if( FD_UNLIKELY( err ) ) {
     189           0 :     return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
     190           0 :   }
     191             : 
     192          57 :   return fd_borrowed_account_set_lamports( borrowed_acct, balance_post );
     193          57 : }
     194             : 
     195             : /* fd_borrowed_account_checked_sub_lamports mirrors Agave function
     196             :    solana_sdk::transaction_context::BorrowedAccount::checked_sub_lamports.
     197             : 
     198             :    Does not update global capitalization. Returns 0 on
     199             :    success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure.
     200             :    Gracefully handles underflow.
     201             : 
     202             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L807 */
     203             : 
     204             : static inline int
     205             : fd_borrowed_account_checked_sub_lamports( fd_borrowed_account_t * borrowed_acct,
     206          36 :                                           ulong                   lamports ) {
     207          36 :   ulong balance_post = 0UL;
     208          36 :   int err = fd_ulong_checked_sub( borrowed_acct->acc->lamports,
     209          36 :                                   lamports,
     210          36 :                                   &balance_post );
     211          36 :   if( FD_UNLIKELY( err ) ) {
     212           0 :     return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
     213           0 :   }
     214             : 
     215          36 :   return fd_borrowed_account_set_lamports( borrowed_acct, balance_post );
     216          36 : }
     217             : 
     218             : /* fd_borrowed_account_update_acounts_resize_delta mirrors Agave function
     219             :    solana_sdk::transaction_context:BorrowedAccount::update_accounts_resize_delta.
     220             : 
     221             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1123 */
     222             : 
     223             : int
     224             : fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrowed_acct,
     225             :                                                   ulong                   new_len,
     226             :                                                   int *                   err );
     227             : 
     228             : /* Accessors */
     229             : 
     230             : /* fd_borrowed_account_is_rent_exempt_at_data_length mirrors Agave function
     231             :    solana_sdk::transaction_context::BorrowedAccount::is_rent_exempt_at_data_length.
     232             : 
     233             :    Returns 1 if an account is rent exempt at it's current data length.
     234             : 
     235             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L987 */
     236             : 
     237             : static inline int
     238           0 : fd_borrowed_account_is_rent_exempt_at_data_length( fd_borrowed_account_t const * borrowed_acct ) {
     239           0 :   if( FD_UNLIKELY( !borrowed_acct->acc ) ) FD_LOG_ERR(( "account is not setup" ));
     240           0 : 
     241           0 :   /* TODO: Add an is_exempt rent API to better match Agave and clean up code
     242           0 :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L990 */
     243           0 :   fd_rent_t const * rent        = &borrowed_acct->instr_ctx->bank->f.rent;
     244           0 :   ulong             min_balance = fd_rent_exempt_minimum_balance( rent, borrowed_acct->acc->data_len );
     245           0 :   return borrowed_acct->acc->lamports>=min_balance;
     246           0 : }
     247             : 
     248             : /* fd_borrowed_account_is_executable mirrors Agave function
     249             :    solana_sdk::transaction_context::BorrowedAccount::is_executable.
     250             : 
     251             :    Returns 1 if the given account has the
     252             :    executable flag set.  Otherwise, returns 0.
     253             : 
     254             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L995 */
     255             : 
     256             : FD_FN_PURE static inline int
     257        4086 : fd_borrowed_account_is_executable( fd_borrowed_account_t const * borrowed_acct ) {
     258        4086 :   return borrowed_acct->acc->executable;
     259        4086 : }
     260             : 
     261             : /* fd_borrowed_account_is_signer mirrors the Agave function
     262             :    solana_sdk::transaction_context::BorrowedAccount::is_signer.
     263             :    Returns 1 if the account is a signer or is writable and 0 otherwise.
     264             : 
     265             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1039 */
     266             : 
     267             : static inline int
     268           0 : fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) {
     269           0 :   fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
     270           0 :   fd_instr_info_t     const * instr     = instr_ctx->instr;
     271           0 : 
     272           0 :   if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
     273           0 :     return 0;
     274           0 :   }
     275           0 : 
     276           0 :   return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction, NULL );
     277           0 : }
     278             : 
     279             : /* fd_borrowed_account_is_writer mirrors the Agave function
     280             :    solana_sdk::transaction_context::BorrowedAccount::is_writer.
     281             :    Returns 1 if the account is a signer or is writable and 0 otherwise.
     282             : 
     283             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1052 */
     284             : 
     285             : static inline int
     286       14523 : fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) {
     287       14523 :   fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx;
     288       14523 :   fd_instr_info_t     const * instr     = instr_ctx->instr;
     289             : 
     290       14523 :   if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) {
     291           0 :     return 0;
     292           0 :   }
     293             : 
     294       14523 :   return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction );
     295       14523 : }
     296             : 
     297             : /* fd_borrowed_account_is_owned_by_current_program mirrors Agave's
     298             :    solana_sdk::transaction_context::BorrowedAccount::is_owned_by_current_program.
     299             : 
     300             :    Returns 1 if the given
     301             :    account is owned by the program invoked in the current instruction.
     302             :    Otherwise, returns 0.
     303             : 
     304             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1065 */
     305             : 
     306             : FD_FN_PURE static inline int
     307       13560 : fd_borrowed_account_is_owned_by_current_program( fd_borrowed_account_t const * borrowed_acct ) {
     308       13560 :   fd_pubkey_t const * program_id_pubkey = NULL;
     309       13560 :   int err = fd_exec_instr_ctx_get_last_program_key( borrowed_acct->instr_ctx, &program_id_pubkey );
     310       13560 :   if( FD_UNLIKELY( err ) ) {
     311           0 :     return 0;
     312           0 :   }
     313             : 
     314       13560 :   return !memcmp( program_id_pubkey->key, borrowed_acct->acc->owner, sizeof(fd_pubkey_t) );
     315       13560 : }
     316             : 
     317             : /* fd_borrowed_account_can_data_be changed mirrors Agave function
     318             :    solana_sdk::transaction_context::BorrowedAccount::can_data_be_changed.
     319             : 
     320             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1074 */
     321             : 
     322             : static inline int
     323             : fd_borrowed_account_can_data_be_changed( fd_borrowed_account_t const * borrowed_acct,
     324       13710 :                                          int *                       err ) {
     325             :   /* Only writable accounts can be changed
     326             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1080 */
     327       13710 :   if( FD_UNLIKELY( !fd_borrowed_account_is_writable( borrowed_acct ) ) ) {
     328         912 :     *err = FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED;
     329         912 :     return 0;
     330         912 :   }
     331             : 
     332             :   /* And only if we are the owner
     333             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1084 */
     334       12798 :   if( FD_UNLIKELY( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) {
     335         366 :     *err = FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED;
     336         366 :     return 0;
     337         366 :   }
     338             : 
     339       12432 :   *err = FD_EXECUTOR_INSTR_SUCCESS;
     340       12432 :   return 1;
     341       12798 : }
     342             : 
     343             : /* fd_borrowed_account_can_data_be_resized mirrors Agave function
     344             :    solana_sdk::transaction_context::BorrowedAccount::can_data_be_resized
     345             : 
     346             :    https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/transaction-context/src/instruction_accounts.rs#L351-L357 */
     347             : 
     348             : int
     349             : fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct,
     350             :                                          ulong                         new_length,
     351             :                                          int *                         err );
     352             : 
     353             : FD_FN_PURE static inline int
     354         339 : fd_borrowed_account_is_zeroed( fd_borrowed_account_t const * borrowed_acct ) {
     355             :   /* TODO: optimize this */
     356         339 :   uchar const * data = borrowed_acct->acc->data;
     357    31674609 :   for( ulong i=0UL; i<borrowed_acct->acc->data_len; i++ ) {
     358    31674270 :     if( data[i] != 0 ) {
     359           0 :       return 0;
     360           0 :     }
     361    31674270 :   }
     362         339 :   return 1;
     363         339 : }
     364             : 
     365             : FD_PROTOTYPES_END
     366             : 
     367             : #endif /* HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h */

Generated by: LCOV version 1.14