LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_borrowed_account.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 79 117 67.5 %
Date: 2026-01-08 05:16:19 Functions: 20 425 4.7 %

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

Generated by: LCOV version 1.14