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 23148 : #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 23166 : ulong * refcnt ) { 46 23166 : borrowed_acct->acc = acc; 47 23166 : borrowed_acct->instr_ctx = instr_ctx; 48 23166 : borrowed_acct->index_in_instruction = index_in_instruction; 49 23166 : borrowed_acct->refcnt = refcnt; 50 23166 : } 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 29628 : fd_borrowed_account_drop( fd_borrowed_account_t * borrowed_acct ) { 57 29628 : if( FD_LIKELY( borrowed_acct->refcnt ) ) *borrowed_acct->refcnt = 0; 58 29628 : borrowed_acct->refcnt = NULL; 59 29628 : } 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 5088 : fd_borrowed_account_get_data( fd_borrowed_account_t const * borrowed_acct ) { 70 5088 : return borrowed_acct->acc->data; 71 5088 : } 72 : 73 : static inline ulong 74 9195 : fd_borrowed_account_get_data_len( fd_borrowed_account_t const * borrowed_acct ) { 75 9195 : return borrowed_acct->acc->data_len; 76 9195 : } 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 9558 : fd_borrowed_account_get_owner( fd_borrowed_account_t const * borrowed_acct ) { 94 9558 : return (fd_pubkey_t const *)borrowed_acct->acc->owner; 95 9558 : } 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 7155 : fd_borrowed_account_get_lamports( fd_borrowed_account_t const * borrowed_acct ) { 107 7155 : return borrowed_acct->acc->lamports; 108 7155 : } 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_sdk::transaction_context::BorrowedAccount::set_data_from_slice. 137 : 138 : In the firedancer client, it also mirrors the Agave function 139 : solana_sdk::transaction_context::BorrowedAccount::set_data. 140 : Assumes that destination account already has enough space to fit 141 : data. Acquires a writable handle. 142 : 143 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L864 */ 144 : 145 : int 146 : fd_borrowed_account_set_data_from_slice( fd_borrowed_account_t * borrowed_acct, 147 : uchar const * data, 148 : ulong data_sz ); 149 : 150 : /* fd_borrowed_account_set_data_length mirrors Agave function 151 : solana_sdk::transaction_context::BorrowedAccount::set_data_length. 152 : 153 : Acquires a writable handle. Returns 0 on success. 154 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L882 */ 155 : 156 : int 157 : fd_borrowed_account_set_data_length( fd_borrowed_account_t * borrowed_acct, 158 : ulong new_len ); 159 : 160 : /* fd_borrowed_account_set_executable mirrors Agave function 161 : solana_sdk::transaction_context::BorrowedAccount::set_executable. 162 : 163 : Returns FD_EXECUTOR_INSTR_SUCCESS if the set is successful. 164 : 165 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L10015 */ 166 : 167 : int 168 : fd_borrowed_account_set_executable( fd_borrowed_account_t * borrowed_acct, 169 : int is_executable ); 170 : 171 : /* Operators */ 172 : 173 : /* fd_borrowed_account_checked_add_lamports mirros Agave function 174 : solana_sdk::transaction_context::BorrowedAccount::checked_add_lamports. 175 : 176 : Does not update global capitalization. Returns 0 on 177 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 178 : Gracefully handles underflow. 179 : 180 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L797 */ 181 : 182 : static inline int 183 : fd_borrowed_account_checked_add_lamports( fd_borrowed_account_t * borrowed_acct, 184 18 : ulong lamports ) { 185 18 : ulong balance_post = 0UL; 186 18 : int err = fd_ulong_checked_add( borrowed_acct->acc->lamports, 187 18 : lamports, 188 18 : &balance_post ); 189 18 : if( FD_UNLIKELY( err ) ) { 190 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 191 0 : } 192 : 193 18 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 194 18 : } 195 : 196 : /* fd_borrowed_account_checked_sub_lamports mirrors Agave function 197 : solana_sdk::transaction_context::BorrowedAccount::checked_sub_lamports. 198 : 199 : Does not update global capitalization. Returns 0 on 200 : success or an FD_EXECUTOR_INSTR_ERR_{...} code on failure. 201 : Gracefully handles underflow. 202 : 203 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L807 */ 204 : 205 : static inline int 206 : fd_borrowed_account_checked_sub_lamports( fd_borrowed_account_t * borrowed_acct, 207 12 : ulong lamports ) { 208 12 : ulong balance_post = 0UL; 209 12 : int err = fd_ulong_checked_sub( borrowed_acct->acc->lamports, 210 12 : lamports, 211 12 : &balance_post ); 212 12 : if( FD_UNLIKELY( err ) ) { 213 0 : return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW; 214 0 : } 215 : 216 12 : return fd_borrowed_account_set_lamports( borrowed_acct, balance_post ); 217 12 : } 218 : 219 : /* fd_borrowed_account_update_acounts_resize_delta mirrors Agave function 220 : solana_sdk::transaction_context:BorrowedAccount::update_accounts_resize_delta. 221 : 222 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1123 */ 223 : 224 : int 225 : fd_borrowed_account_update_accounts_resize_delta( fd_borrowed_account_t * borrowed_acct, 226 : ulong new_len, 227 : int * err ); 228 : 229 : /* Accessors */ 230 : 231 : /* fd_borrowed_account_is_rent_exempt_at_data_length mirrors Agave function 232 : solana_sdk::transaction_context::BorrowedAccount::is_rent_exempt_at_data_length. 233 : 234 : Returns 1 if an account is rent exempt at it's current data length. 235 : 236 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L987 */ 237 : 238 : static inline int 239 0 : fd_borrowed_account_is_rent_exempt_at_data_length( fd_borrowed_account_t const * borrowed_acct ) { 240 0 : if( FD_UNLIKELY( !borrowed_acct->acc ) ) FD_LOG_ERR(( "account is not setup" )); 241 0 : 242 0 : /* TODO: Add an is_exempt rent API to better match Agave and clean up code 243 0 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L990 */ 244 0 : fd_rent_t const * rent = &borrowed_acct->instr_ctx->bank->f.rent; 245 0 : ulong min_balance = fd_rent_exempt_minimum_balance( rent, borrowed_acct->acc->data_len ); 246 0 : return borrowed_acct->acc->lamports>=min_balance; 247 0 : } 248 : 249 : /* fd_borrowed_account_is_executable mirrors Agave function 250 : solana_sdk::transaction_context::BorrowedAccount::is_executable. 251 : 252 : Returns 1 if the given account has the 253 : executable flag set. Otherwise, returns 0. 254 : 255 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L995 */ 256 : 257 : FD_FN_PURE static inline int 258 4080 : fd_borrowed_account_is_executable( fd_borrowed_account_t const * borrowed_acct ) { 259 4080 : return borrowed_acct->acc->executable; 260 4080 : } 261 : 262 : /* fd_borrowed_account_is_signer mirrors the Agave function 263 : solana_sdk::transaction_context::BorrowedAccount::is_signer. 264 : Returns 1 if the account is a signer or is writable and 0 otherwise. 265 : 266 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1039 */ 267 : 268 : static inline int 269 0 : fd_borrowed_account_is_signer( fd_borrowed_account_t const * borrowed_acct ) { 270 0 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 271 0 : fd_instr_info_t const * instr = instr_ctx->instr; 272 0 : 273 0 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 274 0 : return 0; 275 0 : } 276 0 : 277 0 : return fd_instr_acc_is_signer_idx( instr, borrowed_acct->index_in_instruction, NULL ); 278 0 : } 279 : 280 : /* fd_borrowed_account_is_writer mirrors the Agave function 281 : solana_sdk::transaction_context::BorrowedAccount::is_writer. 282 : Returns 1 if the account is a signer or is writable and 0 otherwise. 283 : 284 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1052 */ 285 : 286 : static inline int 287 14643 : fd_borrowed_account_is_writable( fd_borrowed_account_t const * borrowed_acct ) { 288 14643 : fd_exec_instr_ctx_t const * instr_ctx = borrowed_acct->instr_ctx; 289 14643 : fd_instr_info_t const * instr = instr_ctx->instr; 290 : 291 14643 : if( FD_UNLIKELY( borrowed_acct->index_in_instruction>=instr_ctx->instr->acct_cnt ) ) { 292 0 : return 0; 293 0 : } 294 : 295 14643 : return fd_instr_acc_is_writable_idx( instr, borrowed_acct->index_in_instruction ); 296 14643 : } 297 : 298 : /* fd_borrowed_account_is_owned_by_current_program mirrors Agave's 299 : solana_sdk::transaction_context::BorrowedAccount::is_owned_by_current_program. 300 : 301 : Returns 1 if the given 302 : account is owned by the program invoked in the current instruction. 303 : Otherwise, returns 0. 304 : 305 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1065 */ 306 : 307 : FD_FN_PURE static inline int 308 22233 : fd_borrowed_account_is_owned_by_current_program( fd_borrowed_account_t const * borrowed_acct ) { 309 22233 : fd_pubkey_t const * program_id_pubkey = NULL; 310 22233 : int err = fd_exec_instr_ctx_get_last_program_key( borrowed_acct->instr_ctx, &program_id_pubkey ); 311 22233 : if( FD_UNLIKELY( err ) ) { 312 0 : return 0; 313 0 : } 314 : 315 22233 : return !memcmp( program_id_pubkey->key, borrowed_acct->acc->owner, sizeof(fd_pubkey_t) ); 316 22233 : } 317 : 318 : /* fd_borrowed_account_can_data_be changed mirrors Agave function 319 : solana_sdk::transaction_context::BorrowedAccount::can_data_be_changed. 320 : 321 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1074 */ 322 : 323 : static inline int 324 : fd_borrowed_account_can_data_be_changed( fd_borrowed_account_t const * borrowed_acct, 325 13968 : int * err ) { 326 : /* Only writable accounts can be changed 327 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1080 */ 328 13968 : if( FD_UNLIKELY( !fd_borrowed_account_is_writable( borrowed_acct ) ) ) { 329 912 : *err = FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED; 330 912 : return 0; 331 912 : } 332 : 333 : /* And only if we are the owner 334 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1084 */ 335 13056 : if( FD_UNLIKELY( !fd_borrowed_account_is_owned_by_current_program( borrowed_acct ) ) ) { 336 747 : *err = FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED; 337 747 : return 0; 338 747 : } 339 : 340 12309 : *err = FD_EXECUTOR_INSTR_SUCCESS; 341 12309 : return 1; 342 13056 : } 343 : 344 : /* fd_borrowed_account_can_data_be_resized mirrors Agave function 345 : solana_sdk::transaction_context::BorrowedAccount::can_data_be_resized 346 : 347 : https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L1092 */ 348 : 349 : int 350 : fd_borrowed_account_can_data_be_resized( fd_borrowed_account_t const * borrowed_acct, 351 : ulong new_length, 352 : int * err ); 353 : 354 : FD_FN_PURE static inline int 355 333 : fd_borrowed_account_is_zeroed( fd_borrowed_account_t const * borrowed_acct ) { 356 : /* TODO: optimize this */ 357 333 : uchar const * data = borrowed_acct->acc->data; 358 31461885 : for( ulong i=0UL; i<borrowed_acct->acc->data_len; i++ ) { 359 31461552 : if( data[i] != 0 ) { 360 0 : return 0; 361 0 : } 362 31461552 : } 363 333 : return 1; 364 333 : } 365 : 366 : FD_PROTOTYPES_END 367 : 368 : #endif /* HEADER_fd_src_flamenco_runtime_fd_borrowed_account_h */